var rating_xmlhttp = null;
var restoreRatingTimeout = null;
var restoreRatingTimeouts = new Array();
var ratingScore = 0;
var ratingText = "";
function setRestoreRatingTimeout (baseId, val, maxval, text){	
	var timeout = window.setTimeout('toggleStars(\'' + baseId + '\', ratingScore, ' + maxval + ', ratingText)', 1000);
	
	restoreRatingTimeout = timeout;
}

function clearRestoreRatingTimeout(){
	window.clearTimeout(restoreRatingTimeout);
}

function toggleStars(baseId, val, maxval, text) {

	clearRestoreRatingTimeout();
	
	for(i = 1; i <= val; i++){
		var elem = new String(String(baseId) + '' + String(i));
		if(document.getElementById(elem) != null)
			document.getElementById(elem).src = '/Images/star_full_19x20.jpg';
	}

	for(i = val + 1; i <= maxval; i++){	
		var elem = new String(String(baseId) + '' + String(i));
		if(document.getElementById(elem) != null)
			document.getElementById(elem).src = '/Images/star_empty_19x20.jpg';
	}
	
	var divId = "ratingtext";
	
	var div = document.getElementById(divId);
	//alert(divId + ", " + div);
	
	if(div)
		div.innerHTML = text;
}

function setRating(cat, val, text){
	document.getElementById("btnVote").disabled = false;
	ratingScore[cat] = val;
	ratingText[cat] = text;
}

function postRating(ent, val, lang) {
	//alert(ent);
	document.getElementById("entityratingresult").style.display = 'none';	
	document.getElementById("entityratingresult").innerHTML='';
	document.body.style.cursor = 'wait';
	document.getElementById("entityratingworking").style.display = 'block';
	rating_xmlhttp = null;

    if(window.XMLHttpRequest) {
    	try {
			rating_xmlhttp = new XMLHttpRequest();
        } catch(e) {
			rating_xmlhttp = null;
        }

    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	rating_xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		rating_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		rating_xmlhttp = null;
        	}
		}
	}
    
	if (rating_xmlhttp != null)
	{
		rating_xmlhttp.open("POST", "/" + lang + "/postrating/", true)
		rating_xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");         		
		rating_xmlhttp.onreadystatechange = fillentityratingResultList;
		var data = "ent=" + ent + "&score=" + val;
		rating_xmlhttp.send(data)
	}
}

function fillentityratingResultList(){

	if(rating_xmlhttp.readyState != 4) return;

	if (rating_xmlhttp.status==200) {
		var xml = rating_xmlhttp.responseText;
		document.getElementById("entityratingresult").style.display = 'block';
		document.getElementById("entityratingresult").innerHTML = xml;
	}
	else {
		alert("Problem retrieving data: " + rating_xmlhttp.status + " " + rating_xmlhttp.statusText)			
		document.getElementById("entityratingresult").style.display = 'none';
		document.getElementById("entityratingresult").innerHTML = "";
	}

	document.body.style.cursor = 'auto';	
	document.body.style.cursor = 'auto';
	document.getElementById("entityratingworking").style.display = 'none';
}