/*
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 rating(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 off(me, act_score){
	if(!rated){
		if(act_score == 0){	
			for(i=1; i<=sMax; i++){		
				document.getElementById("_"+i).className = "";
				document.getElementById("rateStatus").innerHTML = me.parentNode.title;
			}
		}else{
			for(i=1; i<=sMax; i++){		
			if(i<=act_score){
				document.getElementById("_"+i).className = "on";
				document.getElementById("rateStatus").innerHTML = "";	
			}else{
					document.getElementById("_"+i).className = "";
			}
			
			}
		}
	}
}

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

// Send the rating information somewhere using Ajax or something like that.
function sendRate(sel,upid,score){
	var xhr=null;
    
    if (window.XMLHttpRequest) { 
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) 
    {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() {
		if (xhr.status == 200)
		{
			document.getElementById("rateStatus").innerHTML = xhr.responseText;
		}
		else
		{
			document.getElementById("rateStatus").innerHTML = xhr.status;
       
		}

    };
            
    //on appelle le fichier 
    xhr.open("GET", "vote.php?upid="+upid+"&score="+score, true);
    xhr.send(null);
}




