//funciones para ejecutar con ajax las valoraciones del libro

var promedio=0;
iluminada = new Image(19,19);
iluminada.src = "/images/rating_on.gif";
apagada = new Image(19,19);
apagada.src = "/images/rating_off.gif";
average = new Image(19,19);
average.src = "/images/rating_average.gif";
function rating(id){
	if(id==1){
		window.document['imagen6'].src = iluminada.src;	
		window.document['imagen7'].src = apagada.src;	
		window.document['imagen8'].src = apagada.src;	
		window.document['imagen9'].src = apagada.src;	
		window.document['imagen10'].src = apagada.src;							
	}
	if(id==2){
		window.document['imagen6'].src = iluminada.src;	
		window.document['imagen7'].src = iluminada.src;	
		window.document['imagen8'].src = apagada.src;	
		window.document['imagen9'].src = apagada.src;	
		window.document['imagen10'].src = apagada.src;							
	}
	if(id==3){
		window.document['imagen6'].src = iluminada.src;	
		window.document['imagen7'].src = iluminada.src;	
		window.document['imagen8'].src = iluminada.src;	
		window.document['imagen9'].src = apagada.src;	
		window.document['imagen10'].src = apagada.src;							
	}
	if(id==4){
		window.document['imagen6'].src = iluminada.src;	
		window.document['imagen7'].src = iluminada.src;	
		window.document['imagen8'].src = iluminada.src;	
		window.document['imagen9'].src = iluminada.src;	
		window.document['imagen10'].src = apagada.src;							
	}																												if(id==5){
		window.document['imagen6'].src = iluminada.src;	
		window.document['imagen7'].src = iluminada.src;	
		window.document['imagen8'].src = iluminada.src;	
		window.document['imagen9'].src = iluminada.src;	
		window.document['imagen10'].src = iluminada.src;							
	}	
	if(id==0){
		window.document['imagen6'].src = apagada.src;	
		window.document['imagen7'].src = apagada.src;	
		window.document['imagen8'].src = apagada.src;	
		window.document['imagen9'].src = apagada.src;	
		window.document['imagen10'].src = apagada.src;							
	}								
}
function average_rating(id){
	votosestrellas = document.getElementById('votosestre');
	votosestrellas.className = "estrellas floatl vot" + id;
	document.getElementById('textovotos').innerHTML = id + "&nbsp;estrellas";							
}
function cambia_valoracion()
{
    window.document['imagen6'].src = apagada.src;	
	window.document['imagen7'].src = apagada.src;	
	window.document['imagen8'].src = apagada.src;	
	window.document['imagen9'].src = apagada.src;	
	window.document['imagen10'].src = apagada.src;
	document.getElementById('cambia_votacion').style.visibility="hidden";
}
						   
function objetoAjax(){        
	var xmlhttp=false;        
	try {                
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");        } 
	catch (e) {                
		try {                   
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");                } 
		catch (E) {                        
			xmlhttp = false;               
	    }       
    }       
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {                
		xmlhttp = new XMLHttpRequest();      		
    }       
	return xmlhttp;
}
function insertaOpinion(datos){    
	divResultado = document.getElementById('resultado');   
	ajax=objetoAjax();        ajax.open("GET", datos);        
	ajax.onreadystatechange=function() {                
		if (ajax.readyState==4) {   
			average_rating(ajax.responseText.substr(0,1));
			rating(ajax.responseText.substr(2,1));
			promedio=ajax.responseText.substr(2,1);
			document.getElementById('cambia_votacion').style.display="inline";  
			alert('Valoración insertada correctamente.');             
		}        
	}       
	ajax.send(null)
}
function borraOpinion(datos){   
	divResultado = document.getElementById('resultado');   
	ajax=objetoAjax();        ajax.open("GET", datos);        
	ajax.onreadystatechange=function() {                
		if (ajax.readyState==4) {   
			average_rating(ajax.responseText.substr(0,1));
			rating(ajax.responseText.substr(2,1));
			promedio=ajax.responseText.substr(2,1);
			document.getElementById('cambia_votacion').style.display="none";            
		}        
	}       
	ajax.send(null)
}
