/*
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";
				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 = "";
			}
		}else{
			rating(preSet);
		}
	}
}

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

// Send the rating information somewhere using Ajax or something like that.
function sendRate(sel){
	
	//alert("Your rating was: "+sel.title);
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
		{
		alert ("Your browser does not support AJAX!");
		return;
		} 
	var article_rank = 	sel.title;
	var article_id = document.getElementById('artc_id').value;
	var url="add_ranke.php";
	url=url+"?sid="+Math.random();
	var params = "article_id="+article_id+"&article_rank="+article_rank;

	xmlHttp.onreadystatechange=stateChanged_rank;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
};
function stateChanged_rank() 
{ 
	if (xmlHttp.readyState==4)
	{ 
	  var res = xmlHttp.responseText;
	  var o = document.getElementById('rank_respond');
		if (res != 'ranked') 
			o.innerHTML = "אנו מודים לך על דירוג המאמר.";
		else 
			o.innerHTML ="מצטערים, כבר הצבעת למאמר זה בעבר ולכן אינך יכול להצביע שוב.";
		o.style.display = '';
	};
};
