var xmlHttp;

function createXmlHttpRequest()
{
 if(window.ActiveXObject) {
	 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
 }else if(window.XMLHttpRequest) {
	 xmlHttp = new XMLHttpRequest ();
 }
}

 function handleStateChange () {
     if(xmlHttp.readyState==4) {
         if (xmlHttp.status==200) {
             //var message = xmlHttp.responseXML.getElementsByTagName("valid")[0].childN
         }else{
             alert("Error loadign page"+xmlHttp.status+":"+xmlHttp.statusText);
         }
     }
 }

function swAjaxSubmit(value) {
 createXmlHttpRequest();

 var params = value; 
 
 xmlHttp.open("POST", "rating", true);
 xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
 xmlHttp.setRequestHeader("Content-length", params.length);
 xmlHttp.onreadystatechange=handleStateChange;
 xmlHttp.send(params);

 //alert ("swAjaxSubmit(): send rating to server:"+value);
}
 

function IsRated(postid) {
	if (0 != document.getElementById(postid+"_curRating").innerHTML) {
	 //alert ("rated already!");
	 return 1;
	}
	
	return 0;
}

function ratingme(me, postid, sel_starid){
	
	sMax = 5;
	if(!IsRated(postid)){
		s = sel_starid; // Get the selected star
	
		    //alert("Call rating() on star "+s+" postid:"+postid);
	  	
		for(i=1; i<=sMax; i++){		
	 			starid = postid+"_"+i;
			if(i<=s){
				document.getElementById(starid).className = "on";
				//document.getElementById(postid+"_rateStatus").innerHTML = me.title;	
			}else{
				document.getElementById(starid).className = "";
			}
		}
	}
}

// For when you roll out of the the whole thing //
function off(me, postid, saved_rating){
   if(!IsRated(postid)){
			ratingme(me, postid, saved_rating);
			//document.getElementById(postid+"_rateStatus").innerHTML = me.parentNode.title;
		}
}

// When you actually rate something //
function rateIt(me, postid, rating){
	if(!IsRated(postid)){
		//document.getElementById(postid+"_rateStatus").innerHTML = document.getElementById(postid+"_ratingSaved").innerHTML + " :: "+me.title;
		document.getElementById(postid+"_curRating").innerHTML = rating;
		sendRate(postid, rating);
		ratingme(me, postid, rating);
	}
}

// Send the rating information somewhere using Ajax or something like that.
function sendRate(postid, rating){
	//alert("Your rating for post "+postid+" is: "+rating);
	
	value="post="+postid+"&rating="+rating;
	swAjaxSubmit(value);
}

     