
var totalg = 1;
var voto = false;
var flood = false;
var axg = false;
var adv = false;

function advicePoll(text) {

	if (!adv) {
		
		// pinche fix para IE
		if ($('pollans').style.display = 'none') {
			$('pollans').style.display = 'block';
		}
		
		$('pollans').style.border = "1px solid #e8e8e8";
		$('pollans').style.marginBottom = "5px";
		adv = true;
	}

	$('pollans').innerHTML = text;
	//Effect.Pulsate('pollans', { duration: 1.0 });
}

function updatePoll() {

	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			var response = xmlHttp.responseText;
			var response = response.split('|');
			var status = response[0];
			var string = response[1];

			if (status == "E") {
				advicePoll('Hubo en error al procesar tu voto: ' + string);
				return;
			} else {

				var string = string.split(',');

				for($i=0;$i<=totalg;$i++) {
					document.getElementById('votelink-'+$i).removeAttribute('title');

					var cien = 100;
					var porc = parseInt(string[$i]);
					var meno = parseInt(cien-porc);

					document.getElementById("res"+$i).title = porc + '% de los votos';
					document.getElementById("res"+$i).style.backgroundImage = 'url(images/encuestas/fondo.gif)';
					document.getElementById("res"+$i).style.backgroundPosition = '-' + meno + 'px 0px';
					document.getElementById("res"+$i).style.backgroundRepeat = 'no-repeat';
				}

				if (axg == "1") {
					advicePoll('Gracias por tu voto!');
					voto = true;
				} else {
					advicePoll('Los resultados ya se muestran');
				}

			}


		}
	}
}

function vote(token,pollid,ans,total,ax) {
	totalg = total-1;
	axg = ax;

	if (flood == true && ax != "1") {
		advicePoll('Estas viendo los resultados');
		return;
	}

	if (voto == true && ax == "1") {
		advicePoll('Lo sentimos, ya has votado');
		return;
	}

	advicePoll('Espera por favor...');
	
	if (xmlHttp)
	{
		xmlHttp.onreadystatechange = function () {}
		xmlHttp.abort();
	}
	var url = "call/votar/?token=" + token + "&pollid=" + pollid + "&ans=" + ans + "&ax=" + ax;
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = updatePoll;

	xmlHttp.send(false);

	if (ax == "0") {
		flood = true;
	}

}

function pollthis(anid) {
	return;
}

//function pollthis(anid) {
//	document.getElementById("polla").style.backgroundImage = 'url(images/encuestas/41_' + anid + '.gif)';
//	document.getElementById("polla").style.backgroundRepeat = 'no-repeat';
//}