
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;
var xmlHttp = false; // Create variable to hold XMLHttp object

// Rollover for image 
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; // Get the selected star
		a = 0;
		// Display the number of stars to be on 
		for(i=1; i<=sMax; i++)
    {		
			if(i <= s)
      {
        // Set the classname of the element to on so it will display a "rated" star
				document.getElementById(i).className = "on";
				// Set the status ("rating") to the title of the selected rating 
				document.getElementById("rateStatus").innerHTML = num.title;	
				holder = a+1;
				a++;
			}
      else
      {
				document.getElementById(i).className = "";
			}
		}
	}
}



// For when a user rolls off the stars
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)
  {
    var x = sendRate(me.id);
    if (x != 1)
    {
		document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML + " :: "+me.title;
		preSet = me;
		rated=1;
		}

	}
}

// Submit the rating to the DB 
function sendRate(selection)
{
	  xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return 1;
    }
    
  var comment_form = document.getElementById("comment_form"); // the reviewID
  var reviewID = comment_form.elements["reviewID"].value;
  var userID = document.getElementById("userID").value; // the users ID
  var url="/rating.php";
  url=url+"?rat="+selection;
  url=url+"&revID="+reviewID;
  url=url+"&userID="+userID;
  url=url+"&sid="+Math.random();
  xmlHttp.onreadystatechange=stateChanged(selection);
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
  return 0;
}

  function GetXmlHttpObject()
  {
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    return xmlHttp;
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      // IE 6+
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      return xmlHttp;
      }
    catch (e)
      {
      try
        {
        // IE 5.5
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        return xmlHttp;
        }
      catch (e)
        {
        // Browser doesn't support AJAX!
        return null;
        }
      }
    }
  }

/*
Server states for use with state change property
0	The request is not initialized
1	The request has been set up
2	The request has been sent
3	The request is in process
4	The request is complete
*/
function stateChanged(selection) 
{ 
    if (xmlHttp.readyState==4)
		{
		rating(selection);
		}
}
