var xmlhttp = null;
var xmlhttpListado = null;

function doRequest(tipo, seccId, subSeccId, pseudo)
{
    if(tipo == "multiple"){
        loadXMLDoc(tipo, "/services/multiple.php?secc=" + seccId + "&encPseudo=" + (Math.random()).toString());
    }
    if(tipo == "listadoMas" || tipo == "listadoOtras"){
        //loadXMLDocListado(tipo, "/services/listado.php?tipo=" + tipo + "&secc=" + seccId + "&subsecc=" + subSeccId + "&encPseudo=" + (Math.random()).toString());
		noticias_masvistas(seccId);
    }
    if(tipo == "resultados"){
        loadXMLDoc(tipo, "/cgi-bin/test/graph1.pl?idEnc=" + obj('idEncuesta').value + "&encPseudo=" + (Math.random()).toString());
    }
    if(tipo == "votar"){
        if(obj('opcionHid').value != ""){
            loadXMLDoc(tipo, "/services/encuestaHit.php?accion=hit&idEnc=" + obj('idEncuesta').value + "&opcion=" + obj('opcionHid').value + "&secc=" + seccId + "&encPseudo=" + obj('encPseudo').value);
        }
    }
    if(tipo == "elecciones"){
        loadXMLDoc("resultados", "/cgi-bin/test/graphElecciones.pl?idEnc=" + obj('idEncuesta').value + "&encPseudo=" + (Math.random()).toString());
    }
    if(tipo == "debate"){
        if(obj('opcionHid').value != ""){
			window.location.href = "/html/debateGraph.php?hid=" + obj('opcionHid').value + "&encPseudo=" + obj('encPseudo').value;
        }
		else{
			window.alert("Elije el candidato que consideres haya ganado el debate");
		}
    }
}

function loadXMLDoc(accion, url)
{
	if(window.XMLHttpRequest){		// code for Mozilla, etc.
		if(xmlhttp){
			xmlhttp.abort;
		}
		xmlhttp = new XMLHttpRequest();
	}
	else{
		if(window.ActiveXObject){	// code for IE
            try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
			catch(e){
                try{
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
				catch(e){
					alert("Name:\t" + e.name + "\nMessage:\t" + e.message);
				}
            }
		}
	}
    if(xmlhttp){
		if(accion == "multiple"){
			xmlhttp.onreadystatechange = setMultiple;
		}
        if(accion == "votar"){
			xmlhttp.onreadystatechange = encuestaHit;
        }
		if(accion == "resultados"){
			xmlhttp.onreadystatechange = resultados;
		}
        xmlhttp.open("GET",url,true);		//asynchronous false para que se detenga; false no funciona en Firefox en modo Asincrono
        xmlhttp.send(null);
    }
	else{
    	alert('Giving up :( Cannot create an XMLHTTP instance');
    	return false;
	}
	return 0;
}

function loadXMLDocListado(accion, url)
{
	if(window.XMLHttpRequest){		// code for Mozilla, etc.
		if(xmlhttpListado){
			xmlhttpListado.abort;
		}
		xmlhttpListado = new XMLHttpRequest();
	}
	else{
		if(window.ActiveXObject){	// code for IE
            try{
                xmlhttpListado = new ActiveXObject("Msxml2.XMLHTTP");
            }
			catch(e){
                try{
                    xmlhttpListado = new ActiveXObject("Microsoft.XMLHTTP");
                }
				catch(e){
					alert("Name:\t" + e.name + "\nMessage:\t" + e.message);
				}
            }
		}
	}
    if(xmlhttpListado){
		if(accion == "listadoMas" || accion == "listadoOtras" || accion == 'recetas'){
			xmlhttpListado.onreadystatechange = setListado;
		}
        xmlhttpListado.open("GET",url,true);		//asynchronous false para que se detenga; false no funciona en Firefox en modo Asincrono
        xmlhttpListado.send(null);
    }
	else{
    	alert('Giving up :( Cannot create an XMLHTTP instance');
    	return false;
	}
	return 0;
}

function setMultiple()
{
	if(xmlhttp.readyState == 4){
  		if(xmlhttp.status == 200){
			if(xmlhttp.responseText){
				obj('multipleDiv').innerHTML = xmlhttp.responseText;
			}
        }
    }
	return 0;
}

function setListado()
{
	if(xmlhttpListado.readyState == 4){
  		if(xmlhttpListado.status == 200){
			if(xmlhttpListado.responseText){
				obj('listadoDiv').innerHTML = xmlhttpListado.responseText;
			}
        }
    }
	return 0;
}

function encuestaHit()
{
	if(xmlhttp.readyState == 4){
  		if(xmlhttp.status == 200){
			var jsonObj = eval("(" + xmlhttp.responseText + ")");
			if(jsonObj.msgError == "YES"){
                doRequest("resultados");
			}
			else{
				alert(jsonObj.msgError);
			}
        }
    }
	return 0;
}

function resultados()
{
	if(xmlhttp.readyState == 4){
  		if(xmlhttp.status == 200){
			if(xmlhttp.responseText){
				obj('encuestaDiv').innerHTML = xmlhttp.responseText;
			}
        }
    }
	return 0;
}

//////////////////////////////////
var get_noticias_masvistas = {
	success: function(o)
	{
		try { var jsonObj = YAHOO.lang.JSON.parse(o.responseText); }
		catch(e){
			//alert('Error parsing'); return 1;
		}
		if(jsonObj.exitMsg == "YES"){
			if(parseInt(jsonObj.num) > 0){
				var notas_masvistas =	"<table cellpadding='0px' cellspacing='0px'>" +
										"<tr><td class='multipleSel' align='center'>Lo m&aacute;s visto</td></tr>" +
										"<tr><td height='5'/></tr>" +
										"<tr><td><table cellpadding='3px' cellspacing='1px' style='border-bottom:1px solid #bbb; border-left:1px solid #bbb; border-right:1px solid #bbb;'>";
				for(var i = 0; i < jsonObj.num; i++){
					var tipo_icon = "TXT";
					if(jsonObj.notas[i].tipo == "TXT"){
						tipo_icon = "/img/page.png";
					}
					if(jsonObj.notas[i].tipo == "IMG"){
						tipo_icon = "/img/camera.png";
					}
					if(jsonObj.notas[i].tipo == "VIDEO"){
						tipo_icon = "/img/video_icon.png";
					}
					notas_masvistas += ("<tr><td class='texto2' align='left'>" +
										(parseInt(i) + 1) + ".&nbsp;<a href=" + jsonObj.notas[i].URL + " class='liga2'>" + jsonObj.notas[i].encabezado + "</a>" +
										"&nbsp;<img src='" + tipo_icon + "' width='14' style='vertical-align:middle;' alt=''/></td></tr>");
				}
				notas_masvistas +=	"</table></td></tr>" +
									"<tr><td height='5'/></tr>" +
									"</table>";
				obj('listadoDiv').innerHTML = notas_masvistas;
			}
		}
		else{
			//alert(jsonObj.exitMsg);
		}
	},
	failure: function(o)
	{
		//alert('Search failed');
	}
}

function noticias_masvistas(id_secc)
{
	if(request_mas != null){
		YAHOO.util.Connect.abort(request_mas);
	}

	var request_mas = YAHOO.util.Connect.asyncRequest('POST', '/a/services/bloques/masvistas_secc/' + id_secc, get_noticias_masvistas);
	
	return 0;
}


