var vote_on = new Image();
var vote_off = new Image();
vote_on.src= "/media/images/misc/vote_on.jpg";
vote_off.src= "/media/images/misc/vote_off.jpg";

function saveRating(id,rating) {
	var ajaxRequest;
	try {
		ajaxRequest = new XMLHttpRequest(); // Opera 8.0+, Firefox, Safari
	} catch (e) {
		try {
			// Internet Explorer Browsers
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Could not save rating");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var out = ajaxRequest.responseText;
			if(out) {
				updateRatingBox(id,out);
				
				// Set the cookie
				var exdate=new Date();
				exdate.setDate(exdate.getDate()+30);
				document.cookie= "rating-"+id+ "=" +escape(rating)+";expires="+exdate.toGMTString();
			}
		}
	}
	ajaxRequest.open("GET", "/includes/ratings.php?site="+id+"&rating="+rating, true);
	ajaxRequest.send(null);
}

function updateRatingBox(id,data) {
	var d = document.getElementById("rating-"+id);	
	var rating = data.split(":");
	if(d)
		d.innerHTML = '<p class="voted">Rated '+rating[0]+' after '+rating[1]+' votes</p>';
}

function voteHover(sid,val) {
	for(var i=1; i<6; i++) {
		im = document.getElementById("vote_"+sid+"_"+i);
		if(i<=val) {
			im.src = vote_on.src;
		} else {
			im.src = vote_off.src;
		}
	}
}

function resetVotes(sid) {
	for(var i=1; i<6; i++) {
		im = document.getElementById("vote_"+sid+"_"+i);
		im.src = vote_off.src;
	}
}
