var xmlHttp;

function startInit(){
	var ime;
	if ( (document.getElementById("commentUserName")!=null) && (ime=readCookie("commentUserName"))){
		document.getElementById("commentUserName").value=ime;
	}
}

function pollVote(id,doWhat){

	xmlHttp=getXmlHttpObject();
	if (xmlHttp==null){
		document.write("Your browser does not support Ajax!!!");
	}
	var url="/pollVote.php";
	var radio=document.getElementsByName("poll"+id);
	var i=0;
	var radioValue=76;
	for (i=0; i<radio.length; i++){
		if (radio[i].checked) radioValue=radio[i].value;
	}
	url=url+"?id="+id;
	url=url+"&do="+doWhat;
	url=url+"&voteFor="+radioValue;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=pollVoted;

	if (((doWhat=="vote" ) && (readCookie("voted"+id)!="yes")) || doWhat=="getResults"){
		xmlHttp.open("GET",url,true);			
		xmlHttp.send(null);
	} else {
		if (document.getElementById("pollWarning"+id)) document.getElementById("pollWarning"+id).innerHTML="You are alredy voted"; else
		document.getElementById("pollContainer"+id).innerHTML="<div  id=\"pollWarning"+id+"\" class=\"warning\">You have already voted</div><br />"+document.getElementById("pollContainer"+id).innerHTML;
	}
	
}

function pollVoted(){

	if (xmlHttp.readyState==4  || xmlHttp.readyState=="complete" ){

		var result=xmlHttp.responseText.split("|");
		if (result[0]=="ok"){
			
			document.getElementById("pollContainer"+result[1]).innerHTML=result[2];
			createCookie("voted"+result[1],"yes",100);
		}
		if (result[0]=="results"){
			document.getElementById("pollContainer"+result[1]).innerHTML=result[2];
		}
	
		
	}
}

function addComment(contentId,contentType){
	xmlHttp=getXmlHttpObject();
	if (xmlHttp==null){
		document.write("Your browser does not support Ajax!!!");
	}
	var userName=document.getElementById("commentUserName").value;
	var commentText=document.getElementById("commentText").value;
	var message="";
	if (userName=="") message+="Name can't be blank. Enter Your name or nick!\n";
	if (commentText=="") message+="Comment text can't be blank.";
	if (message==""){
	createCookie("commentUserName",userName,100);
	document.getElementById("commentText").value="";
	var url="/comments.php";
	url=url+"?id="+"addComment";
	url=url+"&userName="+encodeURI(userName);
	url=url+"&commentText="+encodeURI(commentText);
	url=url+"&contentId="+encodeURI(contentId);
	url=url+"&contentType="+encodeURI(contentType);
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=commentAdded;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	} else alert(message);
}



function commentAdded(){
	if (xmlHttp.readyState==4  || xmlHttp.readyState=="complete" ){

		var result=xmlHttp.responseText.split("|");
		if (result[0]=='1'){
			getComments(result[1],result[2],1);
		} else {
			document.getElementById("commentMessage").innerHTML="Sorry! Error occured! Your comment is not added."
		}

	} else {
	
	}
}

function getComments(contentType, contentId, page){
	xmlHttp=getXmlHttpObject();
	if (xmlHttp==null){
		document.write("Your browser does not support Ajax!!!");
	}

	var url="/comments.php";
	url=url+"?id="+"getComments";
	url=url+"&contentId="+contentId;
	url=url+"&contentType="+contentType;
	url=url+"&page="+page;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=commentsObtained;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function commentsObtained(){
	if (xmlHttp.readyState==4  || xmlHttp.readyState=="complete" ){

		var result=xmlHttp.responseText.split("(|-|)");
		if (result[0]=='1'){
			document.getElementById("comments").innerHTML=result[1];
		} else {
			document.getElementById("commentMessage").innerHTML="Sorry! Error occured! Comments can't display"
		}

	} else {
	
	}
}
function getXmlHttpObject()
{
var xmlHttp=null;
try
	 {
	 // Firefox, Opera 8.0+, Safari
	 xmlHttp=new XMLHttpRequest();
	 }
catch (e)
	 {
	 // Internet Explorer
		 try
			  {
			  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			  }
		 catch (e)
			  {
			  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
	 }
return xmlHttp;
}