// JavaScript Document
var xmlHttp;
var grating = 0;
var currentrating = 0;

function rate(id)
{
	currentrating = id;
	var url="updaterating.php?sid=" + Math.random() + "&id=" + id;
	xmlHttp=GetXmlHttpObject(stateChanged);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);	
} 
function stateChanged() 
{ 
	if (xmlHttp.responseText == 1) {
		alert("Thanks for voting! Tune in to World's Funniest Moments to see how your choice measures up with other viewers!");	
	} else {
		alert("Thanks for voting! Tune in to World's Funniest Moments to see how your choice measures up with other viewers!");		
	}
} 

function starrate(rating, id)
{ 	 
	grating = rating;	
	currentrating = id;
	var url="updaterating.php?sid=" + Math.random() + "&id=" + id + "&rating=" + rating;
	xmlHttp=GetXmlHttpObject(starstateChanged);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);	
} 
function starstateChanged() 
{ 
	var newgrating = parseInt(grating) * 16;
	
	if (xmlHttp.responseText != 3) {
		//document.getElementById('rating'+currentrating).style.width = newgrating+'px';	
		alert("Thank you for your rating!");
		if (xmlHttp.responseText != 2) {
			var votes = document.getElementById('votes'+currentrating);
			var newvotes = document.createTextNode(parseInt(votes.innerHTML) + 1);
			
			if (parseInt(votes.innerHTML) == 0) {
				var rtext = document.getElementById('rtext'+currentrating);
				var rtextnew = document.createTextNode("Rating");

				rtext.removeChild(rtext.lastChild);							
				rtext.appendChild(rtextnew);
				
				delete rtext;
				delete rtextnew;
			}
			
			votes.removeChild(votes.lastChild);							
			votes.appendChild(newvotes);
			
			delete votes;
			delete newvotes;
			
				
		}
		
	}
} 

function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null

	if (navigator.userAgent.indexOf("Opera")>=0)
	{
		alert("You must use Internet Explorer or Firefox") 
		return 
	}
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
		{
			strName="Microsoft.XMLHTTP"
		} 
		try
		{ 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
		} 
		catch(e)
		{ 
			alert("Error. Scripting for ActiveX might be disabled") 
			return 
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
} 
