//------------------------------ Sistema de conexion Ajax -----------------------------------
var conexion;
var comboDep;
var pOption;
var capa;
var cargado;
var divLoad;

function iniciarRespuesta(){
	var XmlHttpRequest = null;	
	if(window.ActiveXObject){
	    XmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");	
	}else{
		if(window.XMLHttpRequest){
			XmlHttpRequest = new XMLHttpRequest();
		}
	}
	return XmlHttpRequest;
}

//---------------------------- Cargar el calendario -------------------------------------------

function cargarCalendario(dia,mes,ano){
	conexion = 	iniciarRespuesta();
	conexion.onreadystatechange = manejarEstados;
	conexion.open("GET", "includes/calendario/calendario.php?ano="+ ano +"&mes="+ mes +"&dia="+ dia, true);
	conexion.send(null);
}

function manejarEstados(){
	var capa = document.getElementById('agenda'); 
	if(conexion.readyState == 4){		
		capa.innerHTML = conexion.responseText;
	}
}

//---------------------------- Fin Cargar el calendario --------------------------------------

//-------------------------------- Cargar el video -------------------------------------------

function cambiarVideo(combo){
	var capa = document.getElementById('player');
	var s1='<object type="application/x-shockwave-flash" data="'+ combo.value +'&hl=es&fs=1&color1=0xe1600f&color2=0xfebd01" width="290" height="200" ><param name="movie" value="'+ combo.value +'&hl=es&fs=1&color1=0xe1600f&color2=0xfebd01" /><param name="wmode" value="transparent"  /><param name="quality" value="high"  /></object> ';
	capa.innerHTML = s1;
}

function cambiarVideoH(url,titulo){
	var capa = document.getElementById('playerh');
	var s1='<object type="application/x-shockwave-flash" data="'+ url +'&hl=es&fs=1&color1=0xe1600f&color2=0xfebd01&autoplay=1" width="450" height="380" ><param name="movie" value="'+ url +'&hl=es&fs=1&color1=0xe1600f&color2=0xfebd01&autoplay=1" /><param name="wmode" value="transparent"  /><param name="quality" value="high"  /></object> ';
	capa.innerHTML = s1;
	capa = document.getElementById('playert');
	s1 = '<b>Video Actual:<b> '+titulo;
	capa.innerHTML = s1;
}
//----------------------------- Fin Cargar el video -------------------------------------------

//------------------------------  Cargar la encuesta  ---------------------------------------------

function enviarEncuesta(){
	var seleccionado = document.getElementsByName('radio');
	var valor = "";
	for(i=0; i<seleccionado.length; i++){
		if(seleccionado[i].checked){
			valor = seleccionado[i].value;
			seleccionado[i].checked = false;
		}
	}
	if(valor == ""){
		alert("Para enviar la encuesta, debe marcar alguna de las opciones");
		return false;
	}else{
		var n = 2000000;
		var numero = Math.floor(Math.random()*(n+1))
		conexion = 	iniciarRespuesta();
		conexion.onreadystatechange = estadosEncuesta;
		conexion.open("GET", "guardarEncuesta.php?resp=" + valor +"&rd="+numero, true);
		conexion.send(null);	
	}	
}

function resultadoEncuesta(encuesta){
	/*alert("Resultado Encuesta: " + encuesta);*/
    var n = 2000000;
	var numero = Math.floor(Math.random()*(n+1))
	conexion = 	iniciarRespuesta();
	conexion.onreadystatechange = resultadoEncuestaG;
	conexion.open("GET", "resultadoEncuesta.php?enc=" + encuesta +"&rd="+numero, true);
	conexion.send(null);
}

function typeBrowser(){	
	if(window.ActiveXObject){
	    return true;	
	}else{
		return false;
	}
}

function hiddenFilter(){
	var div = document.getElementById('filter');
	div.style.visibility = 'hidden';	
	div = document.getElementById('display');
	div.style.visibility = 'hidden';
}

function displayFilter(contHTML){
	//--------------  Configurar y Mostrar el Filtro -----------------
	var div = document.getElementById('filter');
	if(typeBrowser())
		div.style.height = window.document.body.scrollHeight + "px";	
	else{
		var valor = 1300;
		div.style.height = valor + "px";
	}
	div.style.visibility = 'visible';
	//--------------  Agregar HTML a la Capa displayCont --------------
	div = document.getElementById('displayCont');
	div.innerHTML = contHTML;
	//-------------------  Mostrar Capa Display -----------------------
	div = document.getElementById('display');
	div.style.visibility = 'visible';
}

function generarColor(){
   var hexadecimal = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
   var pos;
   var n = hexadecimal.length;
   color = "#";
   for(var i = 0;i < 6;i++){
	  pos = Math.floor(Math.random()*n)
      color += hexadecimal[pos];
   }
   if(color == "#FFFFFF" || color.length < 7)
   	color = "#003366";
   return color;
} 

function estadosEncuesta(){
	if(conexion.readyState == 4){
		var texto = conexion.responseText.split('&');		
		var textoOP = texto[2].split(',');
		var auxHTML = "";
		var textOPC;
		var ancho;
		var color;
		var colorb;
		auxHTML = '<b>'+texto[1]+'</b><br><br>';		
		for(i = 0; i < textoOP.length; i++){
			textOPC = textoOP[i].split('-');			
			ancho = (parseInt(textOPC[1])/parseInt(texto[3]))*300;
			colorb = generarColor();
			if(ancho > 0)
				color = "#FFFFFF";
			else
				color = "#003366";
			auxHTML += '<div style="text-align:left; margin-left:10px;"><b>- '+textOPC[0]+'</b></div><br>'+
					   '<div style="background-color:' + colorb + '; text-align:center; width:'+ancho+'px; margin-left:10px; color:'+color+';">'+
					   parseInt(ancho/3)+'%</div><br>';
		}		
		auxHTML += '<br><br><div style="color:#003366; text-align:center;"><b>Total Votos: '+texto[3]+'</b></div>';
		auxHTML += '<br><div style="color:#CC0000; text-align:center;"><b>'+texto[0]+'</b></div>';
		/*displayFilter(auxHTML);*/
        var div_encuesta = document.getElementById('mostrar_encuesta');
        div_encuesta.innerHTML(auxHTML);
	}
}

function resultadoEncuestaG(){
    if(conexion.readyState == 4){
        /*alert("Resultado EncuestaG: " + conexion.responseText);*/
		var texto = conexion.responseText.split('&');		
		var textoOP = texto[1].split(',');
		var auxHTML = "";
		var textOPC;
		var ancho;
		var color;
		var colorb;
		auxHTML = '<b>'+texto[0]+'</b><br><br>';		
		for(i = 0; i < textoOP.length; i++){
			textOPC = textoOP[i].split('-');			
			ancho = (parseInt(textOPC[1])/parseInt(texto[2]))*300;
			colorb = generarColor();
			if(ancho > 0)
				color = "#FFFFFF";
			else
				color = "#003366";
			auxHTML += '<div style="text-align:left; margin-left:10px;"><b>- '+textOPC[0]+'</b></div><br>'+
					   '<div style="background-color:' + colorb + '; text-align:center; width:'+ancho+'px; margin-left:10px; color:'+color+';">'+
					   parseInt(ancho/3)+'%</div><br>';
		}		
		auxHTML += '<br><br><div style="color:#003366; text-align:center;"><b>Total Votos: '+texto[2]+'</b></div>';
		alert("auxHTML: " + auxHTML);
        /*displayFilter(auxHTML);*/
        fancy();
	}
}

function cargarCombo(combo1,combo2,datos,etiqueta,capaGif){	
	capa = capaGif;
	pOption = etiqueta
	comboDep = combo2;
	comboDep.options.length = 0;
	conexion = 	iniciarRespuesta();
	conexion.onreadystatechange = validarCombo;	
	conexion.open("GET", "cargarCombo.php?datos=" + datos +"&id="+combo1.value, true);
	conexion.send(null);
}

function validarCombo(){ 
	var divLoad = document.getElementById(capa);
	if(conexion.readyState == 4){
		divLoad.innerHTML="";
		var texto = conexion.responseText.split(',');
		var datos = "";	
		comboDep.options[0] = new Option(pOption,"",false,false);
		for(var i = 1; i<texto.length; i++){
			datos = texto[i-1].split('&');
			comboDep.options[i] = new Option(datos[1],datos[0],false,false);
		}
		cargado = true;
	}else{		
		cargado = false;
		divLoad.innerHTML="<img src='images/load.gif' />";
	}
}
