function toggleBox(szDivID, iState) // 1 visible, 0 hidden
{
	var object = document.layers ? document.layers[szDivID] : document.getElementById ?  document.getElementById(szDivID).style : document.all[szDivID].style;
	object.display = document.layers ? (iState ? "show" : "hide") : (iState ? "block" : "none");
}

function showIt(item,i,j,max){
   var id;
   actualItem = item;
   
   // Show the selected boxes
   for (var x=1;x<=i;x++){
      id = item + "_" + x;
      document.getElementById(id).src = "images/star-blue.gif";
   }
   
   // Display the not selected ones
   for (var x=i+1;x<=max;x++){
      id = item + "_" + x;
      if (x<=j) document.getElementById(id).src = "images/star-orange.gif";
      else document.getElementById(id).src = "images/star-grey.gif";
   }
}

function showOriginal(i,max){
   for (var x=1;x<=max;x++){
      id = actualItem + "_" + x;
      if (x<=i) document.getElementById(id).src = "images/star-orange.gif";
      else document.getElementById(id).src = "images/star-grey.gif";
   }
}

// Get the HTTP Object
function getHTTPObject(){
   if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
   else if (window.XMLHttpRequest) return new XMLHttpRequest();
   else {
      alert("Your browser does not support AJAX.");
      return null;
   }
}   

// Change the value of the outputText field
function setOutput(){
   var result;
   var data;
   var rating;
   var totalRates;
   var max;
   if(httpObject.readyState == 4){
      result = httpObject.responseText;
      data = result.split(':::');
      rating     = data[0];
      totalRates = data[1];
      max        = data[2];
      
      removeActions(Math.round(rating),max);
      //updateTextRating(actualItem,rating,totalRates);
   }
}

function removeActions(){
      //showOriginal(rating,max);
      var t;
      for (var x=1;x<=maxRate;x++){
         id = actualItem + "_" + x;
         t = document.getElementById(id);
         t.onmouseover  = null;
         t.onmouseout   = null;
         t.onclick      = null;
         t.style.cursor = "default";
      }  
}

function updateTextRating(){
	
   message = document.getElementById('vote-message');
   
   if (message != null) message.innerHTML = '<i>Thank you, our system has successfully recorded your vote.</i>';
   
}

function updateRatingList(rate, mark, vote){
	var newRate;
	var totalVote = vote+1
	
	newRate = (mark+rate) / totalVote;
	
	newRate = Math.round(newRate);
	
	showOriginal(newRate, maxRate);
	
}


// Implement business logic
function submitRating(item, rate, max, mark, vote){
   httpObject = getHTTPObject();
   actualItem = item;
   maxRate = max;
   removeActions();
   if (httpObject != null) {
		httpObject.open("GET", urlDomain + "index.php?page=systems/rate&entry_id="+item+"&rate="+rate, true);
		httpObject.send(null);
		updateRatingList(rate, mark, vote);
		updateTextRating();
   }
}
 
var httpObject = null;
var actualItem = null;
var maxRate = null;
var urlDomain = 'http://www.blog4ft.com/';
