var xmlHttp
var postID

function ratePost(postid, rating)
{
	postID = postid;
	if (postid.length==0)
	{ 
		document.getElementById("txtRating"+postID).innerHTML=""
		return
	}
	if (rating.length==0)
	{ 
		document.getElementById("txtRating"+postID).innerHTML=""
		return
	}
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	
	var url="http://www.optimizacija.eu/include/ajax/glasovanje.php"
	url=url+"?p="+postid
	url=url+"&r="+rating
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateChanged
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
} 

function updatePostRating(postid)
{
	if (postid.length==0)
	{ 
		document.getElementById("txtRating"+postID).innerHTML=""
		return
	}
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	var url="http://www.optimizacija.eu/include/ajax/postrating.php"
	url=url+"?p="+postid
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=ratingLoaded
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


function ratingLoaded() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		if (xmlHttp.responseText.length > 0)
		{ 
			postRating=xmlHttp.responseText;
			//razbijemo tekst v array
			var rating_array=xmlHttp.responseText.split("|");
			var postRating = rating_array[0];
			var postImgRating = rating_array[1];
			var postRatingCount = rating_array[2];
			document.getElementById("ratingImg"+postID).alt = "ocena "+(Math.round(postRating*100)/100)+"; "+postRatingCount+" glasov";
			document.getElementById("ratingImg"+postID).title = "ocena "+(Math.round(postRating*100)/100)+"; "+postRatingCount+" glasov";
			document.getElementById("ratingImg"+postID).src = "http://www.optimizacija.eu/images/stars-"+postImgRating+".gif";
			//document.getElementById("txtRating"+postID).innerHTML=document.getElementById("txtRating"+postID).innerHTML+xmlHttp.responseText;
		}
	} 
} 

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		if (xmlHttp.responseText.length > 0)
		{ 
			document.getElementById("txtRating"+postID).innerHTML="Vaša ocena je bila uspešno zabeležena!";
			//ko smo zabelezili oceno v bazo osvezimo oceno tudi na spletni strani
			updatePostRating(postID)
		}
	} 
} 

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
} 