/****************************************************************************
 * Programa: rate.js                                             			*
 * Creador: Rodolfo Castro                                                  *
 * Fecha de creación: 30/03/2007                                            *
 * Descripción: Programa que muestra el ranking							 	*
 *                                                                 			*
 *                                                                          *
 * Derechos reservados(c) Comercio Mas S.A. de C.V. 2007                    *
 * El contenido de este programa es confidencial y propietario,             *
 * prohibida su utilización y/o modificación sin el consentimiento          *
 * de Comercio Mas S.A. de C.V.                                             *
 *                                                                          *
 * Historial de cambios                                                     *
 * 																	        *
 ****************************************************************************/


/********************************************************************************************
*	ratehttpGet. Funcion que realiza un HTTPREQUEST.Recibe como parametros la URL como 		*
*   string, el rate, el object_type y el object_guid										*
********************************************************************************************/
var rating;
var votes;
function ratehttpGet(strURL,rate,type,object_guid) {
		var rateHttpReq = false;
		var self = this;
		// Mozilla/Safari
		if (window.XMLHttpRequest) {
			self.rateHttpReq = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) {
			self.rateHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		self.rateHttpReq.open('POST', strURL, true);
		self.rateHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		self.rateHttpReq.onreadystatechange = function() {
			if (self.rateHttpReq.readyState == 4) {
				x=self.rateHttpReq.responseXML
								
				//Muestra los mensajes que le mando el archivo updateRanking.php
				if (!self.rateHttpReq.responseText.match(/second/)) 
				{	
					
					var xmlMesage = x.getElementsByTagName('tanks');
					mesage = xmlMesage[0].firstChild.nodeValue;
					alert(mesage); //Gracias por votar
					
					var xmlRating = x.getElementsByTagName('rating');
					rating = xmlRating[0].firstChild.nodeValue;
					var xmlVotes = x.getElementsByTagName('votes');
					votes = xmlVotes[0].firstChild.nodeValue;
										
					var ranking= document.getElementById("stars");
					ranking.setAttribute("style", "width:"+rating*20+"%");
					var votes_number= document.getElementById("rating_votes");
					votes_number.firstChild.nodeValue=rating + " ("+ votes + ")Votos";
				}
				else	
				{	
					var xmlMesage = x.getElementsByTagName('second');
					mesage = xmlMesage[0].firstChild.nodeValue;
					alert(mesage); //No se puede votar por segunda vez
				}
							
			}
		}
    self.rateHttpReq.send(rate_getquerystring(rate,type,object_guid));
return false;
}
/**********************************************************************************************
*	rate_getquerrystring(). Recibe como parametros el rate, object_type y object_guid, lo que *
*	regresa es un string que incluye los valores de esas variables y tambien del user_guid    *
**********************************************************************************************/

function rate_getquerystring(rate,type,object_guid){
	var cookie = ReadCookie(object_guid);
	//Se acutaliza cookie para que no se pueda volver a votar
	UpdateValueCookie(object_guid ,0,30);
	rate_qstr = '&cookie=' + escape(cookie);
	rate_qstr += '&object_type=' + escape(type);
	rate_qstr+= '&object_guid=' + escape(object_guid);
	rate_qstr+= '&rate=' + escape(rate); 
	return rate_qstr;				
}

/**************************************************************************************************
*	DisplayRate(). Regresa el codigo para mostrar el ranking,. Recibe como parametros object_type,*
*	object_guid,visitas,rating,votes_number.													  *
**************************************************************************************************/
function DisplayRate(object_type,object_guid,visitas,rating,votes_numer){

var out= '' +
		'<ul class="rating">' +
			//Se multiplica ranting por 20 por que se trabaja en porcentaje para mostrar el rate-current
			'<li id="stars" class="rate-current" style=" width:'+ rating*20 + '%"></li>' +
			'<li><a href="#" id=rate1 class="rate-one" onClick="ratehttpGet(\'../rating/updateRanking.php\',1,\''+ object_type +'\',\''+  object_guid + '\')">1</a></li>' +
			'<li><a href="#" id=rate2 class="rate-two" onClick="ratehttpGet(\'../rating/updateRanking.php\',2,\''+ object_type + '\',\''+ object_guid + '\')">2</a></li>' +
			'<li><a href="#" id=rate3 class="rate-three" onClick="ratehttpGet(\'../rating/updateRanking.php\',3,\''+ object_type + '\',\''+ object_guid + '\')">3</a></li>' +
			'<li><a href="#" id=rate4 class="rate-four" onClick="ratehttpGet(\'../rating/updateRanking.php\',4,\''+ object_type +'\',\''+ object_guid + '\')">4</a></li>' +
			'<li><a href="#" id=rate5 class="rate-five" onClick="ratehttpGet(\'../rating/updateRanking.php\',5,\''+ object_type +'\',\''+  object_guid + '\')">5</a></li>' +
		'</ul><div id="rating_votes" style=" padding-top:0px; margin-top:0px">' + rating + ' ('+ votes_number + ')Votos</div>'+
			'<br><b>Vistos:' + visitas +' </b>';
	document.write(out);
	//return out;
}
