/**************************************************************************************
*File: rating.js
*Contents: Business rating system.
*All rights reserved
**************************************************************************************/


/*function getRating(table,code,business_type,lng) {
   xmlHttp = getXmlHttpObject();
   if (xmlHttp == null) {
      alert ("Browser does not support HTTP Request");
      return;
   } 



   var url = phpDir + "getrating.php";
   url = url + "?table=" + table;
   url = url + "&code=" + code;
   url = url + "&lng=eng";
   url += "&type=" + business_type;
   xmlHttp.onreadystatechange = stateChanged; 
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
}*/ 

function getRating(siteURL, phpDir, table,code,business_type,lng) {
	var url =  siteURL + phpDir + "/getrating.php" + "?table=" + table + "&code=" + code + "&lng=" + lng + "&type=" + business_type;
	var getRatingCallback = {
		success: function(o) {YAHOO.util.Event.onAvailable("rating", updateRating, {rating: parseInt(o.responseText), args: o.argument});},
		argument: {siteURL: siteURL, phpDir: phpDir, table: table, code: code, business_type: business_type, lng: lng}
	};

	YAHOO.util.Connect.asyncRequest('GET', url, getRatingCallback);
} 


var  updateRating = function(o) {
	var textPoor = {"eng": "Poor", "fr":  "Mauvais"};
	var textNSpecial = {"eng": "Nothing special", "fr":  "Rien de spécial"};
	var textInterest = {"eng": "Interesting", "fr":  "Intéressant"};
	var textGood = {"eng": "Good", "fr":  "Bien"};
	var textVGood = {"eng": "Very good", "fr":  "Très bien"};
	var textRate = {"eng": "Rate it ", "fr":  "Notez le "};

	var label = new Array(textPoor[o.args.lng], textNSpecial[o.args.lng], textInterest[o.args.lng], textGood[o.args.lng], textVGood[o.args.lng]);
	var t;
	var html = "<strong>" + textRate[o.args.lng] + "</strong>";
	var i = 1;

	while(i < o.rating) {
		t = label[i-1];
		html += "<a onclick=\"rate('" + o.args.siteURL + "','" + o.args.phpDir + "'," + i + ",'" + o.args.table + "','" + o.args.business_type + "'," + o.args.code + ",'" + o.args.lng + "'); return false\" href=\"#\" title=\""+ t + "\"><img style=\"border-width:0px;\" onmouseover=\"true;\" src=\"http://restocan.com/icones/star_red.png\" /></a>";
		i++;
	}


	while(i <= 5) {
		t = label[i-1];
		html += "<a onclick=\"rate('" + o.args.siteURL + "','" + o.args.phpDir + "'," + i + ",'" + o.args.table + "','" + o.args.business_type + "'," + o.args.code + ",'" + o.args.lng + "'); return false\" href=\"#\" title=\""+ t + "\"><img style=\"border-width:0px;\" onmouseover=\"true;\" src=\"http://restocan.com/icones/star_gray.png\" /></a>";
		i++;
	}

	document.getElementById("rating").innerHTML=html;
} 

function ratingToHTML(siteURL, phpDir, score,table,business_type,code,lng) {
	
}

/*function stateChanged() 
{ 
   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
      document.getElementById("rating").innerHTML=xmlHttp.responseText;
      
   } 
}*/ 


/*function rate(score,prefix,business_type,code) {
   
   xmlHttp = getXmlHttpObject();
   if (xmlHttp == null) {
      alert ("Browser does not support HTTP Request");
      return;
   } 

   document.getElementById("rating").innerHTML="<font color=\"red\">Processing your vote...</font>";

   var url = phpDir + "rate.php";
   url = url + "?score=" + score;
   url = url + "&code=" + code;
   url = url + "&prefix=" + prefix;
   url = url + "&business_type=" + business_type;
   xmlHttp.onreadystatechange = rated; 
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
}*/

function rate(siteURL, phpDir, score,table,business_type,code,lng) { 
	var textProcessing = {"eng": "Processing your vote", "fr": "Veuillez patienter"};
	document.getElementById("rating").innerHTML="<font color=\"red\">" + textProcessing[lng] + "...</font>";
	var url = siteURL + phpDir + "/rate.php" + "?score=" + score + "&code=" + code + "&table=" + table + "&business_type=" + business_type;
	YAHOO.util.Connect.asyncRequest('GET', url, rateCallback);
}

var rateCallback = {
	success: function(o) {document.getElementById("rating").innerHTML = o.responseText;}
};

/*function rated() 
{ 
   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
      document.getElementById("rating").innerHTML = xmlHttp.responseText;
      
   } 
}*/ 

