/*
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){
 if(!rated){
  if(!preSet){
   for(i=1; i<=sMax; i++){
    document.getElementById("_"+i).className = "";
    document.getElementById("rateStatus").innerHTML = me.parentNode.title;
   }
  }else{
   rating(preSet);
   document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML;
  }
 }
}

// When you actually rate something //
function rateIt(me)
{
 if(!rated){
  document.getElementById("rateStatus").innerHTML = me.title;
  preSet = me;
  rated=1;
  sendRate(me);
  rating(me);
 }
}
/*
function sendRate(sel)
{
   alert( "Gracias por calificar la noticia");
}
*/
	function createRequestObject()
	{
	  var ro;
	  var browser = navigator.appName;
	  if (browser == 'Microsoft Internet Explorer') { ro = new ActiveXObject("Microsoft.XMLHTTP"); }else{ ro = new XMLHttpRequest();}
	  return ro;
	}

// Send the rating information somewhere using Ajax or something like that.
function sendRate(sel)
{
 	nid_noticia=document.getElementById('nid').value;
	// Creas el OBJETO solo la primara vez
	var http = createRequestObject();
	// --------------------
	// Llamas al PHP las veces que quieras
	// --------------------
	http.open("get", "http://www.rpp.com.pe/detalle_calificar.php?valor="+sel.id+"&nid="+nid_noticia,true);
    http.onreadystatechange = function handleResponse()
	{
	 if (http.readyState == 4)
	   {
	     var response = http.responseText;
	     alert( "Gracias por calificar la noticia");
	   }
	}
	http.send(null);	
	// --------------------
	// Este metodo se ejecuta solo ni bien termina la llamada debido a que
	// en el codigo de arriba dice
	//   http.onreadystatechange = handleResponse;
	// si tu funcion fuera function algo( ) tons
	//   http.onreadystatechange = algo;  es como el OnClick de C++
	// --------------------
}


