
function getPostRate(pid,w){
	
	//Single rating
	//var singlerating = readSasakiCookie("spr");
	if(singlerating){
		var getThisPostValues = singlerating.split("|");
		var getThisPostIDs = getThisPostValues[0].split(",");
		var getThisPostRate = getThisPostValues[1].split(",");
	}
	
	for (x=0;x<getThisPostIDs.length;x++){
		if(getThisPostIDs[x]==pid){
			var thisPostId=true;
			var thisPostRate = getThisPostRate[x];
		} 
	}
	
	//Connection and reading values
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		getPostRateRequest = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		getPostRateRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}

	refreshCode = new Date;
	getPostRateRequest.open('GET', 'http://www.sasakifujika.net/api/ratedpost/'+pid+'.txt?rc='+(refreshCode.getMilliseconds()*(1.7*refreshCode.getSeconds())), true);
	getPostRateRequest.send(null);
	getPostRateRequest.onreadystatechange=function() {
		if(getPostRateRequest.readyState==4) {
			if(getPostRateRequest.status==404){
				document.getElementById("cr").style.width = "0%";
			} else {
				
				var response = getPostRateRequest.responseText.split(",");
				var rate = response[0];
				var voters = response[1];
			 	
				if(w){
					var starHtml = "Vota il post <span id=\"vn\"></span><ul class=\"star-rating small-star\"><li id=\"cr\" class=\"current-rating\" title=\"hai gi&agrave; votato questo post\"></li></ul>";
					document.getElementById("rpb").innerHTML = starHtml;
				}
				
				if(thisPostId){
					var starHtml = "Vota il post <span id=\"vn\"></span><ul class=\"star-rating small-star\"><li id=\"cr\" class=\"current-rating\" title=\"hai gi&agrave; votato questo post - il tuo voto &egrave; stato: "+thisPostRate+"\"></li></ul>";
					document.getElementById("rpb").innerHTML = starHtml;
				}
				
				document.getElementById("cr").style.width = (rate*20)+"%";
				
				if(voters==1){
					document.getElementById("vn").innerHTML = "[un voto]";	
				} else {
					document.getElementById("vn").innerHTML = "["+voters+" voti]";	
				}			
			}
		}
	}
}

function sfr(pid, r) {
	
	//Adding new PID and rate in the cookie
	var singlerating = readSasakiCookie("spr");
	if(singlerating){
		var getThisPostValues = singlerating.split("|");
		var getThisPostIDs = getThisPostValues[0].split(",");
		var getThisPostRate = getThisPostValues[1].split(",");
		var theContent = getThisPostIDs+","+pid+"|"+getThisPostRate+","+r;
		writeSasakiCookie(theContent,365,"spr");
	} else {
		var theContent = pid+"|"+r;
		writeSasakiCookie(theContent,365,"spr");
	}
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		postPostRateRequest = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		postPostRateRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	refreshCode = new Date;
	postPostRateRequest.open('GET', 'http://www.sasakifujika.net/script/wgPostRate.php?pid='+pid+'&r='+r+'&rc='+(refreshCode.getMilliseconds()*(1.7*refreshCode.getSeconds())), true);
	postPostRateRequest.send(null);
	postPostRateRequest.onreadystatechange=function() {
		if (postPostRateRequest.readyState == 2){
			document.getElementById("rpb").innerHTML = "in elaborazione...";
		}
		if (postPostRateRequest.readyState == 4) {
			if (postPostRateRequest.status == 200) {
		    	document.getElementById("rpb").innerHTML = "grazie per aver votato!";
				var w = true;
				setTimeout('getPostRate('+pid+','+w+')', 5000);
		    } else {
	        	document.getElementById("rpb").innerHTML = "c'&eacute; stato un problema durante l'elaborazione del tuo voto"
	       	}
	    }
	}
}
