function sendRating(rating,movieid) {
    var url = 'ratefilm.asp?movieid=' + movieid + '&rating=' + rating + '&sid=' + Math.random() + Math.random() + Math.random();
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.open("GET", url, true);
    req.send(null);
}

/*
 * This is the source of the blank star image
 * It can be in the form of http://something.com/emptyStar.jpg
 */
var imageBlank = "images/star_off.gif";
/*
 * This is the source of the blank star image
 * It can be in the form of http://something.com/emptyStar.jpg
 */	
var imageFill = "images/star_on.gif";

/*
 * Number of stars
 */
var numberOfStars = 5;

/*
 * This array contain the messges to be 
 * shown when the mouse is over the stars
 * It has five elements, meaning that when one stars are
 * on then the message is "I Hate it",
 * when two stars are on then display messge "I Don't like it"
 * and son on. 
 * Remember that the indexes in javascript start in zero.
 * Note: If you change the numberOfStars variable you should
 * construct the array accordingly.
 */

//The text to show when no star is selected
var ratingText = "Rate this film!";
var rated=false;

var vote_tips = new Array();
vote_tips[0] = "";
vote_tips[1] = "";
vote_tips[2] = "";
vote_tips[3] = "";
vote_tips[4] = "Save 5 stars for the very best";


/*
 * This function replace the empty images by
 * colored images, according to the number of stars ON
 */
function switchImage(wich) {
	for(n=0; n<5;n++) { //Turn af many stars as the vote value
		imgObj = document.getElementById("img_"+n);
		imgObj.src = imageBlank;
	}
		
	startNumber = wich.id.substr(4); //How many stars are on
	for(n=0; n<startNumber;n++) { //Turn on as many stars as the vote value
		imgObj = document.getElementById("img_"+n); //find the image
		imgObj.src = imageFill; //replace image
	}
	wich.src = imageFill; //fill the star which has the mouse over
	divObj = document.getElementById("vote_tip"); //get the div element to change the text
	divObj.innerHTML = vote_tips[startNumber]; //change the text accordingly
}
/*
 * Switch back the images to original
 */

function switchImageBack(wich) {
	if (rated==false) {
		startNumber = wich.id.substr(4); //the start which the mouse out event occured
		for(n=0; n<startNumber;n++) { //Turn af many stars as the vote value
			imgObj = document.getElementById("img_"+n);
			imgObj.src = imageBlank;
		}
		wich.src = imageBlank;
		divObj = document.getElementById("vote_tip");
		divObj.innerHTML = ratingText;//Replae by the original text;
	}
	else {
		rated=false;
	}
}

/*
 *When the user click in a star this function is called
 */
function submitVote(vote,movieid) {

	//The vote value
	startNumber = parseInt(vote.id.substr(4))+1;

	
	//This simply display the rating, but a form could be submitted
	sendRating(startNumber,movieid);	
	switchImage(vote);
	document.getElementById("ratefilm").innerHTML="<br><b>Thank-you for your vote!</b>";
	//for (ind=0 ; ind<numberOfStars; ind++) {
	//	imgObj = document.getElementById("img_"+ind); //find the image
	//	imgObj.onmouseover="";
	//	imgObj.onmouseout="";
	//	imgObj.onclick="";
	//	imgObj.style["cursor"]="default";
	//	//imgObj.src=imageFill;
	//}

}

function changeVote(vote,movieid) {
	var rating = parseInt(vote.id.substr(4))+1;

	switchImage(vote);
	document.getElementById("Rating").value=rating;
	rated=true;
}

/*
 * Writes the HTML to show the stars
 */
function writeStars() {
	for (ind=0 ; ind<numberOfStars; ind++) {
		document.write('<img style="cursor:pointer" onClick="submitVote(this)" ');
		document.write('onMouseOver="switchImage(this)" onMouseOut="switchImageBack(this)"');
		document.write('id="img_'+ind+'" src="'+imageBlank+'" border="0" hspace="0" vspace="0" />');
	}
	document.write('&nbsp;&nbsp;<span id="vote_tip">'+ratingText+'</span>');
}
