// JavaScript Document
var oXmlHttp = null;
var iInterval= 1000;
//
function createXMLHttp()
{
	if (typeof XMLHttpRequest!="undefined")
	{
		return new XMLHttpRequest();
	} else if (window.ActiveXObject)
	{
		var aVersions = ["MSXML2.HMLHttp.5.0",
						 "MSXML2.HMLHttp.4.0", "MSXML2.HMLHttp.3.0",
						 "MSXML2.HMLHttp", "Microsoft.XMLHttp"
						 ];
		for (var i=1;i< aVersions.length;i++)
		{
			try {
				var oXmlHttp = new ActiveXObject(aVersions[i]);
				return oXmlHttp;
			} catch (oError) {
				//Do nothing
			}
		}
	}
	throw new Error("XMLHttp object could not be created.");
}
//
function MostrarGrafico(target, sVotos)
{
	var voto_tmp, porcentaje, porcentaje_txt;
	var tabla;
	var sTotal=0;
	var voto=sVotos.split(",");
	//
	tabla= "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr>";
	//
	for (i=0; i<=voto.length-1; i++)
	{
		voto_tmp=voto[i];
		sTotal=parseInt(sTotal)+parseInt(voto_tmp);
	}
	for (i=0; i<=voto.length-1; i++)
	{
		voto_tmp=voto[i];
		//Con este if evito el problema de la división entre 0
		if (sTotal>0)
		{
			fTotal	=	sTotal;
		}
		else
		{
			fTotal	=	1;
		}
		porcentaje=voto_tmp*100/fTotal;
		porcentaje_txt=porcentaje+'';
		//tabla=tabla+"<td align='center' valign='bottom'><table width='50' height='" + Math.round(porcentaje) + "' border='0' cellpadding='0' cellspacing='0' bgcolor='#CCCCCC'><tr><td>" + Math.round(porcentaje) + "%</td></tr></table></td>";
		tabla=tabla+"<td align='center' valign='bottom'><div class='Texto5' style='width:38px; height:15px'>" + porcentaje_txt.substr(0,4) + "%</div><div style='background:#b5da46; border-left:#99cc00 solid 1px; border-top:#99cc00 solid 1px; border-right:#99cc00 solid 1px; font-size:0; width:38px; height:" + Math.round(porcentaje) + "px'></div></td>"
	}
	tabla=tabla + "</tr></table>";
	document.getElementById(target).innerHTML = tabla;
	if (target=="grafico")
	{
		document.getElementById("total_votos_ppal").innerHTML = "Total votos: "+sTotal;
	}
}
//
function ActualizarGrafico()
{
	if (!oXmlHttp)
	{
		oXmlHttp=createXMLHttp();
	} else if (oXmlHttp.readyState!=0)
	{
		oXmlHttp.abort();
	}
	oXmlHttp.open("get", "/grafico/check_votos.php", true);
	oXmlHttp.onreadystatechange = function ()
	{
		if (oXmlHttp.readyState == 4)
		{
			if (oXmlHttp.status == 200)
			{
				var aData=oXmlHttp.responseText.split("||");
				MostrarGrafico("grafico",aData[0]);
			}
		}
	};
	setTimeout(ActualizarGrafico, iInterval);
	oXmlHttp.send(null);
}
//
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}
//
function cargar_contenido(target,url,metodo,parametros)
{
    document.getElementById(target).innerHTML = '<div style="width:150px; height:15px; margin:10px" align="center" class="Texto5b"><img src="../images/loading.gif" alt="Cargando" width="45" height="9"></div>';
    var myConn = new XHConn();
        if (!myConn) alert("Ajax not supported");
        var peticion = function (oXML) {  document.getElementById(target).innerHTML = oXML.responseText; };
        myConn.connect(url, metodo, parametros, peticion);
}
//