function showIt(fId,i,j,fMax){
   var id;
   actualItem = fId;
   
   // Show the selected boxes
   for (var x=1;x<=i;x++){
      id = fId + "_" + x;
      document.getElementById(id).src = "Images/yellow_star.png";
   }
   
   // Display the not selected ones
   for (var x=i+1;x<=fMax;x++){
      id = fId + "_" + x;
      if (x<=j) document.getElementById(id).src = "Images/pale_star.png";
      else document.getElementById(id).src = "Images/grey_star.png";
   }
}

function showOriginal(i,fMax){
   for (var x=1;x<=fMax;x++){
      id = actualItem + "_" + x;
      if (x<=i) document.getElementById(id).src = "Images/pale_star.png";
      else document.getElementById(id).src = "Images/grey_star.png";
   }
}

// Change the value of the outputText field
function setOutput(){
   var result;
   var data;
   var rating;
   var totalRates;
   var fMax;

	if (http_request.readyState == 4) 
	{
		if (http_request.status == 200 && http_request.responseText != "") 
		{
			result = http_request.responseText;

			data = result.split(':::');
			rating     = data[0];
			totalRates = data[1];
			fMax        = data[2];
			document.getElementById("ratingbox").innerHTML = old_html;
			removeActions(Math.round(rating),fMax);
			updateTextRating(actualItem,rating,totalRates);
   		}
   }
}

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

function updateTextRating(fId,rating,total){
   var itemRating;
   var totalRating;
   var id;
   
   id = fId + "_ir";
   itemRating = document.getElementById(id);
   id = fId + "_tr";
   totalRating = document.getElementById(id);
   
   if (itemRating != null) itemRating.innerHTML = rating;
   if (totalRating != null) totalRating.innerHTML = total;
   
}

// Implement business logic
function submitRating(fId,rate,fMax){

	http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			//http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		/* XMLHTTP kunde inte startas */
		return false;
	}

	actualItem = fId;
	ourDate = new Date();	
	http_request.onreadystatechange = setOutput;
	old_html = document.getElementById("ratingbox").innerHTML;
	document.getElementById("ratingbox").innerHTML = '<img src="Images/loading.gif">';
	http_request.open('GET', "calls/submitRating.php?item="+fId+"&rating="+rate+"&max="+fMax+"&t="+ourDate.toLocaleString(), true);
	http_request.send(null);
}

function addedToFavorites()
{

	if (http_request.readyState == 4) 
	{
		if (http_request.status == 200) 
		{
		//	alert("Added")
			document.getElementById("favfolderimage").src = 'Images/folder_favorites.png';
		}
	}		

}

function addToFavorites(fId){

	http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			//http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		/* XMLHTTP kunde inte startas */
		return false;
	}


  document.getElementById("favfolderimage").src = 'Images/loading.gif'; 	
  http_request.onreadystatechange = addedToFavorites;
  http_request.open('GET', "calls/addtofavorites.php?item="+fId, true);
  http_request.send(null);
}

var http_request = false;
var actualItem = 0;
var old_html = "";
