/*
Author: Addam M. Driver
Date: 10/31/2006
*/

var sMax;	// Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;

// Rollover for image Stars //
function ratingOn(num){
	sMax = 0;	// Isthe maximum number of stars
	for(n=0; n<num.parentNode.childNodes.length; n++){
		if(num.parentNode.childNodes[n].nodeName == "A"){
			sMax++;	
		}
	}
	
	if(!rated){
		s = num.id.replace("_", ''); // Get the selected star
		a = 0;
		for(i=1; i<=sMax; i++){		
			if(i<=s){
				document.getElementById("_"+i).className = "on";
				// document.getElementById("rateStatus").innerHTML = num.title;	
				holder = a+1;
				a++;
			}else{
				document.getElementById("_"+i).className = "";
			}
		}
	}
}

// For when you roll out of the the whole thing //
function ratingOff(me){
	if(!rated){
		if(!preSet){	
			for(i=1; i<=sMax; i++){		
				document.getElementById("_"+i).className = "";
			}
		}else{
			ratingOn(preSet);
		}
	}
}

// When you actually rate something //
function rateIt(me,qid){
	if(!rated){
		preSet = me;
		rated=1;
		sendRate(me, qid);
		ratingOn(me);
	}
}

// Send the rating information somewhere using Ajax or something like that.
function sendRate(sel, qid){
	s = sel.id.replace("_", '');

	var xhr; 
	try {	xhr = new ActiveXObject('Msxml2.XMLHTTP');	}
	catch (e) {
		try {	xhr = new ActiveXObject('Microsoft.XMLHTTP');	}
		catch (e2) {
			try {  xhr = new XMLHttpRequest();	}
			catch (e3) {  xhr = false;	}
		}
	}
	
	xhr.onreadystatechange  = function() {
		if(xhr.readyState  == 4) {
			if(xhr.status  == 200) {
				document.getElementById("ratingSaved").innerHTML = xhr.responseText; 
			}else{ 
				document.getElementById("ratingSaved").innerHTML = "Error code: " + xhr.status;
			}
		}
	};
	
	var cartadd = "vote.php?qid="+qid+"&rating="+s;
	xhr.open("GET", cartadd,  true);
	xhr.send(null);
	
	var xir; 
	try {	xir = new ActiveXObject('Msxml2.XMLHTTP');	}
	catch (e) {
		try {	xir = new ActiveXObject('Microsoft.XMLHTTP');	}
		catch (e2) {
			try {  xir = new XMLHttpRequest();	}
			catch (e3) {  xir = false;	}
		}
	}
	
	xir.onreadystatechange  = function() {
		if(xir.readyState  == 4) {
			if(xir.status  == 200) {
				document.getElementById("currentRating").innerHTML = xir.responseText; 
			}else{ 
				document.getElementById("currentRating").innerHTML = "Error code: " + xir.status;
			}
		}
	};
	
	var cartadd = "get_rating.php?qid="+qid;
	xir.open("GET", cartadd,  true);
	xir.send(null);
}

