var xmlHttpRatings = false;
var score = 0;
var votes = 0;
var msg = '';

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;
}

function resultsVotesChanged(){
	var votes_counter;
	
	if ((xmlHttpRatings.readyState == 4) || (xmlHttpRatings.readyState == "complete")){
		msg = xmlHttpRatings.responseText.split("[BRK]")[0];
		score = xmlHttpRatings.responseText.split("[BRK]")[1];
		votes = xmlHttpRatings.responseText.split("[BRK]")[2];
		document.getElementById('ratings-msg').innerHTML = msg;
		if(votes > 1 || votes == 0){
			votes_counter = 'voturi';
		}else{
			votes_counter = 'un vot';
		}
		document.getElementById('ratings-result').innerHTML = 'Nota <span>' + score + '</span> din <span>' + votes + '</span> ' + votes_counter;
	} 
}

function vote(id_article, score){
	
	xmlHttpRatings = GetXmlHttpObject();
	
	if (xmlHttpRatings == null){
		alert ("Browser does not support HTTP Request");
		return;
	}
	
	xmlHttpRatings.onreadystatechange = resultsVotesChanged;
	
	var url = 'ajax-ratings-stiri.php?score=' + score + '&id_article=' + id_article;
	
	xmlHttpRatings.open('GET', url, true);
	xmlHttpRatings.send(null);
}

