﻿var itemId = 0;
var numVotes = 0;
var aveRating = 0;
var stars = new Array();

function loadVotes(itemid)
{
	//itemId = itemid;
	itemId = window.location.href ;
	var url = 'http://www.autotiss.com.br/ajax/xmlGetVotes.aspx?itemid=' + itemId + '&hash=' + Math.random();
	var xmlhttp = null;
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	if (xmlhttp != null)
	{
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
			{
				var result = xmlhttp.responseXML.documentElement.getElementsByTagName('RESULT')[0];
				numVotes = result.getAttribute('numvotes');
				aveRating = result.getAttribute('averating');
				document.getElementById('RateItBox').style.display = 'block';
				document.getElementById('starsLoading').style.display = 'none';
				//document.getElementById('belowStars').innerHTML = numVotes + ' votos';
				if(result.getAttribute('hasvoted') == 'yes')
				{
					document.getElementById('aboveStars').innerHTML = 'Voto enviado';
				}
				else
				{
					document.getElementById('aboveStars').innerHTML = 'Avalie esta página';
					document.getElementById('RateItBox').onmouseout = resetStars;
					for(var i = 1; i <= 5; i++)
					{
						stars[i] = document.getElementById('star' + i);
						stars[i].onmouseover = mouseOverStar;
						stars[i].onclick = starClick;
						stars[i].starNum = i;
					}
				}
				resetStars();
			}
			else if(xmlhttp.readyState == 4 && xmlhttp.status != 200)
			{
				document.getElementById('aboveStars').innerHTML = 'Erro';
			}
		}
		xmlhttp.open('GET',url,true);
		xmlhttp.send(null);
	}
	else
	{
		alert('Seu navegador não suporta XMLHTTP.');
	}
}

function mouseOverStar()
{
	for(var i = 1; i <= this.starNum; i++)
	{
		stars[i].className = 'starHover';
	}
	for(var i = this.starNum + 1; i <= 5; i++)
	{
		stars[i].className = 'star';
	}
	document.getElementById('belowStars').innerHTML = 'Sua nota: '+ this.starNum;
}

function resetStars()
{
	for(var i = 1; i <= 5; i++)
	{
		if(i > aveRating)
			stars[i].className = 'star';
		else
			stars[i].className = 'starHover';
	}
	document.getElementById('belowStars').innerHTML = '';
}


function starClick()
{
	var vote = this.starNum;
	document.getElementById('aboveStars').innerHTML = 'Enviando...';
	
	itemId = window.location.href ;
	var url = "http://www.autotiss.com.br/ajax/xmlSubmitVote.aspx?itemid=" + itemId + "&uservote=" + vote + "&hash=" + Math.random();
	
	var xmlhttp = null; 
	if (window.XMLHttpRequest) 
	{ 
		xmlhttp = new XMLHttpRequest(); 
	} 
	else if (window.ActiveXObject) 
	{ 
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
	if (xmlhttp!=null) 
	{ 
		xmlhttp.onreadystatechange=function() 
		{ 
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
			{
				var someDOM = xmlhttp.responseXML.documentElement;
				if(someDOM.getElementsByTagName('SUCCESS').length !== 1)
				{
					document.getElementById('aboveStars').innerHTML = 'Erro ao enviar.';
				}
				else
				{
					aveRating = aveRating * numVotes;
					aveRating = aveRating + vote;
					numVotes++;
					aveRating = aveRating / numVotes;
					document.getElementById('belowStars').innerHTML = numVotes + ' votos';
					document.getElementById('aboveStars').innerHTML = 'Voto enviado!';
				}
				for(var i = 1; i <= 5; i++)
				{
					stars[i].onmouseover = null;
					stars[i].onclick = null;
				}
				resetStars();
			}
			else if(xmlhttp.readyState == 4 && xmlhttp.status != 200)
			{
				alert('Problema ao receber dados do servidor');
			}
		}
		xmlhttp.open('GET',url,true);
		xmlhttp.send(null);
	}
	else
	{
		alert('Seu navegador não suporta XMLHTTP.');
	}
}