function emiteVoto(rutaId, voto) {

	var voteReq;

	if (window.XMLHttpRequest) {
		voteReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		voteReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (voteReq) {
		voteReq.open('GET','asistencia/vota-xml.php?rutaId='+rutaId+'&voto='+voto, true);
		voteReq.onreadystatechange = function() {
			if (voteReq.readyState == 4) {
				if (voteReq.status == 200) {
					//Modificamos el HTML para reflejar el voto!
					var xmlDoc = voteReq.responseXML.documentElement;
					biker = xmlDoc.getElementsByTagName('biker');
					if (biker.length>0) {
						bikerName = biker[0].getAttribute('name');

						bikerSpan = document.getElementById(bikerName+'Vote');
						if (bikerSpan) {
							if (voto>0)
								bikerSpan.innerHTML = bikerName + '(' + voto + ')';
							else
								bikerSpan.innerHTML = bikerName;
						}

						votoSpan = document.getElementById('miVotacion');
						if (votoSpan) {
							miHTML = '<a href="javascript:emiteVoto('+rutaId+',0)" onmouseover="sugiere(0)">'+
								'<img src="img/blackhole.png" alt="*" id="voto0"></a>';
							for(i=1;i<=voto;i++)
								miHTML = miHTML + '<a href="javascript:emiteVoto('+rutaId+','+i+')"><img src="img/star1.png" onmouseover="sugiere('+i+')" alt="*"></a>';
							for(i=voto+1;i<=5;i++)
								miHTML = miHTML + '<a href="javascript:emiteVoto('+rutaId+','+i+')"><img src="img/star0.png" onmouseover="sugiere('+i+')" alt="*"></a>';
							votoSpan.innerHTML = miHTML;
							changeOpinion(votoSpan.voto,voto);
							votoSpan.voto=voto;
							printValoracion();
						}
					}
				}
			}				
		}
		voteReq.send(null);
	}
}

function displayExplicacionValoracion(voto) {
	var explicacionVoto = document.getElementById('explicacionVoto');
	switch(voto) {
		case 0 : explicacionVoto.innerHTML = 'Sin valorar'; break;
		case 1 : explicacionVoto.innerHTML = 'Mala'; break;
		case 2 : explicacionVoto.innerHTML = 'Regular'; break;
		case 3 : explicacionVoto.innerHTML = 'Buena'; break;
		case 4 : explicacionVoto.innerHTML = 'Muy Buena'; break;
		case 5 : explicacionVoto.innerHTML = 'Excelente'; break;
	}
}

function sugiere(voto) {
	displayExplicacionValoracion(voto);
}

function piensatelo() {
	var miVotacion = document.getElementById('miVotacion');
	if (miVotacion) {
		displayExplicacionValoracion(miVotacion.voto);
	}
}

function setVoto(voto) {
	var miVotacion = document.getElementById('miVotacion');
	if ((miVotacion) && (voto!=null)) {
		miVotacion.voto = voto;
	}
}

function setValoracion(suma, cuantos) {
	var votoRecuento = document.getElementById('votoRecuento');
	if (votoRecuento) {
		votoRecuento.suma = suma;
		votoRecuento.cuantos = cuantos;
	}
}

function changeOpinion(antes, despues) {
	var valoracion = document.getElementById('votoRecuento');
	if (valoracion) {
		valoracion.suma = valoracion.suma - antes + despues;
	}
	if (antes==0) valoracion.cuantos++;
	if (despues==0) valoracion.cuantos--;
}

function printValoracion() {
	var votoRecuento = document.getElementById('votoRecuento');
	if (votoRecuento) {
		var valoracion=0;
		if (votoRecuento.cuantos > 0) 
			valoracion = votoRecuento.suma / votoRecuento.cuantos;

		var numStars = Math.round(valoracion);
		miHTML = '';
		for(i=1;i<=numStars;i++)
			miHTML = miHTML + '<img src="img/star1.png" alt="*">';
		for(i=numStars+1;i<=5;i++)
			miHTML = miHTML + '<img src="img/star0.png" alt="*">';
		miHTML = miHTML + '('+ (Math.round(valoracion*100)/100) + ')';
		votoRecuento.innerHTML = miHTML;

		var votoNumero = document.getElementById('votoNumero');
		if (votoNumero) {
			votoNumero.innerHTML = votoRecuento.cuantos;
		}
	}

}
