var id_pic;
var nom;
var typ;
var msg;
function add_msg()
{
	id_pic=document.getElementsByTagName('input')[0].value;	
	nom=document.getElementsByTagName('input')[1].value;
	typ=document.getElementsByTagName('input')[2].value;
	msg=document.getElementsByTagName('textarea')[0].value;
	var url="/add_comment.php";	
	if (msg!='')
	{
		request.open("POST",url,true);
		request.onreadystatechange=update_comment;
		request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
 		request.send("id_pic="+escape(id_pic)+"&nom="+nom+"&typ="+typ+"&msg="+msg);
	}
}
function update_comment()
{
	if (request.readyState==4)
	{
		if (request.status==200)
		{
			var date_c=request.responseText;
			alert (date_c);				
			var dummy=document.getElementsByTagName('form')[0].reset();
			var tab_comments=document.getElementById("tab_comments");	
			var cr_tr=document.createElement("tr");
			tab_comments.appendChild(cr_tr);
			var cr_td=document.createElement("td");
			cr_tr.appendChild(cr_td);
			cr_td.setAttribute("id","comm_b");
			var n_nom=document.createTextNode(nom);
			cr_td.appendChild(n_nom); 		
			var cr_td=document.createElement("td");
			cr_tr.appendChild(cr_td);
			cr_td.setAttribute("id","comm_t");
			cr_td.setAttribute("align","right");
			var n_date=document.createTextNode(date_c);
			cr_td.appendChild(n_date);
			var cr_tr=document.createElement("tr");
			tab_comments.appendChild(cr_tr);
			var cr_td=document.createElement("td");
			cr_tr.appendChild(cr_td);
			cr_td.setAttribute("id","bott");
			cr_td.setAttribute("colSpan","2");
			var n_msg=document.createTextNode(msg);
			cr_td.appendChild(n_msg);	
			document.getElementById('divscroll_2').scrollTop=10000;
		}
	}
}

var vote_n;

function vote(oc,id,typ)
{
	vote_n=oc;
	var url="/add_vote.php?oc="+oc+"&id="+id+"&typ="+typ;		
	var elem=document.getElementById("vote");
	clearText(elem);
	request.open("GET",url,true);
	request.onreadystatechange=updatepage;
	request.send(null);	
}
function updatepage()
{
	if (request.readyState==4)
	{
		if (request.status==200)
		{
			var rating = document.getElementById("ratings");
			clearText(rating);
			var elem=document.getElementById("vote");
			replaceText(elem,"Спасибо! Ваш голос учтен.");
			var oc_el=document.getElementById("oc");
			var g_num_el=document.getElementById("g_num");
			var end_el=document.getElementById("end");
			var ocenka=getText(oc_el);
			var g_numer=getText(g_num_el);
			if (ocenka=='-') {ocenka=0;}
			var g_num=g_numer;
			g_numer++;			
			var a=eval(ocenka*g_num)+eval(vote_n);
			ocenka_n=a/g_numer;
			ocenka_n=Math.round(ocenka_n*10)/10;
			var end=getEnd(g_numer);
			replaceText(oc_el,ocenka_n);
			replaceText(g_num_el,g_numer);
			replaceText(end_el,end);
		}
	}
}

function getEnd(n) {
	if ((n==0) || ((n>4) && (n<=20))) return ("й");
	if (n%10 == 1) return ("е");
	if ((n%10 >= 2) && (n%10 <= 4)) return ("я");	
	if (n%10 >= 5) return ("й");
}

function full_zv(num)
{
	for (i=1; i<=num; i++)
	{	
		var el=document.getElementById("zv"+i);
		el.src="/images/zv-1.gif";	
	}
}
function unfill()
{
	for (i=1; i<=5; i++)
	{	
		var el=document.getElementById("zv"+i);
		el.src="/images/zv-0.gif";	
	}
}


function replaceText(el, text) {
  if (el != null) {
    clearText(el);
    var newNode = document.createTextNode(text);
    el.appendChild(newNode);
  }
}

function clearText(el) {
  if (el != null) {
    while (el.childNodes.length>0) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        el.removeChild(childNode);
      }
    }
  }
}
function getText(el) {
  var text = "";
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        if (childNode.nodeValue != null) {
          text = text + childNode.nodeValue;
        }
      }
    }
  }
  return text;
}


$(document).ready(function(){
	$("#showCode").click(function(){
		$("#showCode").hide();
		$("#code").show();
		$("#code").select();
	});
});

