$(document).ready(function() {

f = document.forms[0];
f.target="_self";


$.fn.removeCol = function(col){
    
    // Make sure col has value
    if(!col){ col = 1; }
    $('tr td:nth-child('+col+'), tr th:nth-child('+col+')', this).remove();
   
}



$("#formContacto").hide().fadeIn("slow");
$("#contenidoNews").hide().fadeIn("slow");
$("#MapaDelSitio").hide().fadeIn("slow");
$("#simulador").hide().fadeIn("slow");



$("#lasNoticias").css("cursor","pointer");
$("#losContactos").css("cursor","pointer");
$("#laIdentidad").css("cursor","pointer");
$("#losProductos").css("cursor","pointer");
$("#elhome").css("cursor","pointer");
$("#losGabinetes").css("cursor","pointer");
$("#imgSim").css("cursor","pointer");
$("#imgSus").css("cursor","pointer");



if(f.accion.value == "6")
  {
	 $("#fltNacional").show();
	 $("#fltRegion").hide();
	 $("#fltRegProvincia").hide();
	 $("#fltRegComuna").hide();
	 $("#fltCirc").hide();
	 $("#fltCircDist").hide();

	 var browser=navigator.appName;

	 if (browser=="Microsoft Internet Explorer")
	 {

	   $('tr td:nth-child(3), tr th:nth-child(3)', tblGrilla2).remove();
	   $('tr td:nth-child(3), tr th:nth-child(3)', tblGrilla2).remove();
	   $('tr td:nth-child(5), tr th:nth-child(5)', tblGrilla2).remove();
	   $('tr td:nth-child(5), tr th:nth-child(5)', tblGrilla2).remove();
	   $('tr td:nth-child(7), tr th:nth-child(7)', tblGrilla2).remove();
	   $('tr td:nth-child(10), tr th:nth-child(10)', tblGrilla2).remove();
	   $('tr td:nth-child(10), tr th:nth-child(10)', tblGrilla2).remove();

	   $('tr td:nth-child(12), tr th:nth-child(12)', tblGrilla2).remove();
	   $('tr td:nth-child(12), tr th:nth-child(12)', tblGrilla2).remove();

	 }  

	else
	{
	 $('#tblGrilla2').removeCol(3);
	 $('#tblGrilla2').removeCol(3); 
	 $('#tblGrilla2').removeCol(5);
	 $('#tblGrilla2').removeCol(5); 
	 $('#tblGrilla2').removeCol(7);
	 $('#tblGrilla2').removeCol(10); 
	 $('#tblGrilla2').removeCol(10);

	 $('#tblGrilla2').removeCol(12); 
	  $('#tblGrilla2').removeCol(12);



	}

}	





$(".gabHome").click(function(){ 
				f.target="_self";	
				if(this.target=="_self")
				   f.target="_blank";
				
				f.IdGabinete.value=this.name;				
				f.IdNodoSeleccionado.value=0;				
				f.accion.value="1";
				f.action="index.php";				
				f.submit();
                       
                                 })                               
                                 
                  

        
        
        
                                 
 $(function() { $('#2,#3').lavaLamp({
 			
		         fx: 'backout',
		         speed: 700,
		         click: function(event, menuItem) {
		             return false;
		         }
		     });
		 });                                 

                                                              

$("#home,#volver,#elhome").click(function(){ 				
				f.accion.value="";
				f.action="index.php";
				f.submit();
                       
                                 })


$("#productos,#losProductos").click(function(){ 
                                $("#divEdiciones").hide(); 
                                $(".content-in").hide().load("../html/productos.htm").fadeIn("slow"); //Le damos efecto
				
                                 })

                               
$("#identidad,#laIdentidad").click(function(){ 	
				
				$("#divEdiciones").hide();
                                $(".content-in").hide().load("../html/identidad.htm").fadeIn("slow"); //Le damos efecto

				
                                 })


$("#contacto,#losContactos").click(function(){ 				
				f.accion.value="4";
				f.action="index.php";
				f.submit();
                       
                                 })

$("#mapa").click(function(){ 				
				f.accion.value="5";
				f.action="index.php";
				f.submit();
                       
                                 })

                                 
                                 
                                 
                                 
$("#edicionesListado").change(function(){   				
   				 f.target= "_self";	
	  			 f.accion.value="3";                       
				 f.action="index.php";
				 f.submit();
 				})    
 				
$(".lnkNews").click(function(){ 				
				
				f.accion.value="2";
				f.IdNoticia.value = this.name;
				f.action="index.php";				
				f.submit();
                       
                                 })   

$("#noticias,#lasNoticias").click(function(){ 	
			        f.target= "_self";	
				f.accion.value="3";
				f.action="index.php";
				f.submit();
                       
                                 })
                                 

$("#imgsimulador").click(function(){ 	
			        f.target= "_self";	
				f.accion.value="6";
				f.action="index.php";
				f.submit();
                       
                                 })

$("#imgsuscribase").click(function(){ 	
			        f.target= "_self";	
				f.accion.value="7";
				f.action="index.php";
				f.submit();
                       
                                 })


$("#bntSbmSub").click(function(){ 				
				if(f.nombres.value=="" || f.apellido_pat.value=="" || f.cmbPaises.value=="" || f.email.value=="")
				{
				 alert("Nombre, apellido, pais y direccion de correo son datos obligatorios para suscribirse.")
				 return false;
				}
				
				if(!IsValid_EMail( f.email ))
				{
				 alert("La dirección de correo tiene un formato incorrecto.")
				 return false;
				}
				
				if(!IsValid_Date( f.FechaNacimiento ))
				{
				  alert("La fecha de nacimiento tiene un formato incorrecto.")
				 return false;
				}
				
				
				
				
			        f.target= "_self";	
				f.accion.value="7";
				f.action="index.php";
				f.submit();
                       
                                 })



                                 
                                 

$("#enviar").click(function(){ 				
				if(f.nombres.value=="" || f.email.value=="" || f.mensaje.value=="")
				{
				 alert("Nombre, dirección de correo y mensaje son datos obligatorios para el envió de mensajes.")
				 return false;
				}
				
				if(!IsValid_EMail( f.email ))
				{
				 alert("La dirección de correo tiene un formato incorrecto.")
				 return false;
				}
				
			        f.target= "_self";	
				f.accion.value="4";
				f.action="index.php";
				f.submit();
                       
                                 })
                                 


$("#zoom-in").click(function(){ 	
			        zoomText('aumentar','parrNoticias')				
                       
                                 })

$("#zoom-out").click(function(){ 	
			        zoomText('disminuir','parrNoticias')				
                       
                                 })
                                 
$("#zoom-eq").click(function(){ 	
			        zoomText('reestablecer','parrNoticias')				
                       
                                 })


$(".cambiaestado").click(function (){
				
                                var laclase = this.value;
                                var valor = this.checked;                                
                                jq = "$('."+laclase+"').";
                       	        jq += "each(function(){";
                       	        jq += "this.checked="+valor;
                       	        jq += "});";                       	       
                       	        eval(jq);
                       	  	      
                       	        

                       	        
					})

$(".rdFiltro").click(function(){
                                 switch(this.value) 
        			 {
        			   case "1":
                                    {                                    
                                     $("#fltNacional").fadeIn("slow").show();
                                     $("#fltRegion").fadeOut("slow").hide();
                                     $("#fltRegProvincia").fadeOut("slow").hide();                                     
				     $("#fltRegComuna").fadeOut("slow").hide();                                     
                                     $("#fltCirc").fadeOut("slow").hide();
                                     $("#fltCircDist").fadeOut("slow").hide();				    
			             break;
			            }
			            
			            case "2":
				    {
				    
				     $("#fltNacional").fadeOut("slow").hide();
				     $("#fltRegion").fadeIn("slow").show();
				     $("#fltRegProvincia").fadeOut("slow").hide();
				     $("#fltRegComuna").fadeOut("slow").hide();                                     
				     $("#fltCirc").fadeOut("slow").hide();				    
				     $("#fltCircDist").fadeOut("slow").hide();				    
				     break;
				    }
				    
				    case "3":
				    {
				    
				     $("#fltNacional").fadeOut("slow").hide();
				     $("#fltRegion").fadeOut("slow").hide();
				     $("#fltRegComuna").fadeIn("slow").show();
				     $("#fltRegProvincia").fadeOut("slow").hide();
				     $("#fltCirc").fadeOut("slow").hide();				    
				     $("#fltCircDist").fadeOut("slow").hide();				    
				     break;
				    }
				    
			           
			           case "4":
				    {
				    
				     $("#fltNacional").fadeOut("slow").hide();
				     $("#fltRegion").fadeOut("slow").hide();
				     $("#fltRegComuna").fadeOut("slow").hide();                                     
				     $("#fltRegProvincia").fadeIn("slow").show();
				     $("#fltCirc").fadeOut("slow").hide();				    
				     $("#fltCircDist").fadeOut("slow").hide();				    
				     break;
			            }
			            case "5":
				    { 
				     $("#fltNacional").fadeOut("slow").hide();
				     $("#fltRegion").fadeOut("slow").hide();
				     $("#fltRegProvincia").fadeOut("slow").hide();				    
				     $("#fltRegComuna").fadeOut("slow").hide();                                     
				     $("#fltCirc").fadeIn("slow").show();
				     $("#fltCircDist").fadeOut("slow").hide();				    
				     break;
			            }
			            
			            case "6":
				    { 
				     $("#fltNacional").fadeOut("slow").hide();
				     $("#fltRegion").fadeOut("slow").hide();
				     $("#fltRegProvincia").fadeOut("slow").hide();				    
				     $("#fltRegComuna").fadeOut("slow").hide();                                     
				     $("#fltCirc").fadeOut("slow").hide();
				     $("#fltCircDist").fadeIn("	slow").show();
				     break;
				    }
			            
			            
			            $("#fltCircDist").hide();
			            
			            
			            
			         }   
                       
                                 })

					
					
 $(".combo_txt2").change(function(){
 
         // Vector para saber cuál es el siguiente combo a llenar

         var combos = new Array();
         combos['cmbRegiones'] = "cmbProvincias";
         combos['cmbRegiones2'] = "cmbComunas";
         //combos['cmbCirc'] = "cmbDistritos";
         
         // Tomo el nombre del combo al que se le a dado el clic por ejemplo: país
         posicion = $(this).attr("name");
         // Tomo el valor de la opción seleccionada
         valor = $(this).val()
         
         // Evaluó  que si es país y el valor es 0, vacié los combos de estado y ciudad
         if(posicion == 'cmbRegiones' && valor==0){
             $("#cmbProvincias").html('    <option value="0" selected="selected">----------------</option>')
         
         /// $("#ciudad").html('    <option value="0" selected="selected">----------------</option>')
         
         }else{
         /* En caso contrario agregado el letreo de cargando a el combo siguiente
         Ejemplo: Si seleccione país voy a tener que el siguiente según mi vector combos es: estado  por qué  combos [país] = estado
             */
             
             $("#"+combos[posicion]).html('<option selected="selected" value="0">Cargando...</option>')
             /* Verificamos si el valor seleccionado es diferente de 0 y si el combo es diferente de ciudad, esto porque no tendría caso hacer la consulta a ciudad porque no existe un combo dependiente de este */
             if(valor!="0" || posicion !='cmbProvincias'){
             // Llamamos a pagina de combos.php donde ejecuto las consultas para llenar los combos
             
                 $.post("../class/FillComboBox.php",{
                                     combo:$(this).attr("name"), // Nombre del combo
                                     id:$(this).val() // Valor seleccionado
                                     },function(data){
                                                      $("#"+combos[posicion]).html(data);    //Tomo el resultado de pagina e inserto los datos en el combo indicado
                                                     })
             }
         }
     })					



 $("#consultar").click(function(){
 
                //Obtenemos los ids de las votaciones
 		var IdsVotaciones="";
 		var IdsPartidos="";
 		var IdsCoal="";
 		var opcion;
 		var nacional;
 		var IdRegion;
 		var IdComuna;
 		var IdProvincia;
 		var IdDistrito;
 		var IdCirc;
 		var IdDist;
 		var textoCombo;
 		
 		$(".classElec").each(function(){
 						if(this.checked)
 						  IdsVotaciones += this.value+','; 						  
 					  	})			
 		
 		
 		$(".cambiaestado").each(function(){
		 				if(this.checked)
		 				{
		 			         IdsCoal += this.value+','; 
		 			        } 
		 			         
		 			         var laclase = this.value;
						 var valor = this.checked;                                
						 jq = "$('."+laclase+"').";
						 jq += "each(function(){";
						 jq += "if(this.checked)";
						 jq += "IdsPartidos+=this.value+','";
						 jq += "});";                       	       
						 
                       	        		 eval(jq)
                       	        		 
		 			         
		 				})	
		 				

		
		$(".100").each(function(){
		
						if(this.checked)
						{
		        			 IdsPartidos += this.value+','; 
		 				 } 
						})
						
		
			 		
 		IdsVotaciones = IdsVotaciones.substring(0,IdsVotaciones.length-1);
		IdsPartidos   = IdsPartidos.substring(0,IdsPartidos.length-1);;
		IdsCoal       = IdsCoal.substring(0,IdsCoal.length-1);;
		
			
		opcion = $("input[@name='rdFiltro']:checked").val();
		
		 		
 		if(IdsVotaciones=="" || IdsPartidos=="")
 		{
 		  alert("Debe seleccionar elecciones y partidos para realizar el cálculo")
 		  return false;
 		}  
 		
 		
 		switch(opcion) 
		 {
		  case "1":// Nacional
                   {            
                   nacional = 1;
                   break;
                   }
                   
                   case "2":// Region
		    {            
		     if($("#cmbRegiones3").val()=="")
		     {
		       alert("Debe seleccionar una region")
		       return false;
		     }
		     IdRegion = $("#cmbRegiones3").val();
		     textoCombo = $("#cmbRegiones3 :selected").text();
		     break;
		     }
		     
		   case "3":// Comuna
		    {            
		     if($("#cmbRegiones2").val()=="")
		     {
		       alert("Debe seleccionar una Region y luego la comuna")
		       return false;
		     }
		     IdComuna = $("#cmbComunas").val();		     
		     textoCombo = $("#cmbComunas :selected").text();
		     
		     break;
		     }  
		     
		   case "4":// Provincia
		    {    
		     
		     if($("#cmbRegiones").val()=="")
		     {
		       alert("Debe seleccionar una Region y luego una provincia")
		       return false;
		     }
		     IdProvincia = $("#cmbProvincias").val();		     
		     textoCombo = $("#cmbProvincias :selected").text();
		     
		     break;
		     }  
		     
		   case "5":// Circ
		    {            
		     if($("#cmbCirc").val()=="")
		     {
		       alert("Debe seleccionar una Circunscripcion")
		       return false;
		     }
		     IdCirc = $("#cmbCirc").val();		     
		     textoCombo = $("#cmbCirc :selected").text();
		     
		     break;
		     } 
		   
		   
		   case "6":// Distrito
		    {            
		     if($("#cmbDistritos").val()=="")
		       {
		         
		         alert("Debe seleccionar un Distrito")
		         return false;
		       }
		       IdDist = $("#cmbDistritos").val();		     
		       textoCombo = $("#cmbDistritos :selected").text();		       
		      
		       break;
		      } 		  
                   
                 }  
                 
                 
                
                                  
                 $.post("../class/makeGraphs.php",{
		                                     opcion:opcion, 
		                                     IdsVotaciones:IdsVotaciones,
		                                     IdsPartidos:IdsPartidos,
		                                     IdsCoal:IdsCoal,
		                                     nacional:nacional,
		                                     IdRegion:IdRegion,
		                                     IdComuna:IdComuna,
		                                     IdProvincia:IdProvincia,
		                                     IdDist:IdDist,
		                                     IdCirc:IdCirc,
		                                     IdDist:IdDist,
		                                     textoCombo:textoCombo 
		                                     },function(data){
		                                                      
		                                                      $("#divResultado").hide().fadeIn("slow").html(data);    //Tomo el resultado de pagina e inserto los datos en el combo indicado
		                                                      switch(opcion) 
								      	 {
								      	  case "1":// Nacional
								      	  {
								           $("#tit1").html("<h6>Simulacion para Pais</h6>"); 
								           break;
								      	  }
								      	  case "2":// Regional
								      	  {
								      	   $("#tit1").html("<h6>Simulacion para Pais</h6>"); 
									   $("#tit2").html("<h6>Simulacion para "+textoCombo+"</h6>"); 			
									  break;
								      	  }
								      	  
								      	  case "3":// Comunal	
								      	  {
								      	   $("#tit2").html("<h6>Simulacion para "+$("#cmbRegiones2 :selected").text()+"</h6>"); 
								      	   $("#tit3").html("<h6>Simulacion para Comuna "+textoCombo+"</h6>"); 
								      	   break;
								      	  }
								      	  
								      	  case "4":// Provincia	
								      	  {
								      	    $("#tit2").html("<h6>Simulacion para "+$("#cmbRegiones :selected").text()+"</h6>"); 
								      	    $("#tit4").html("<h6>Simulacion para Provincia "+textoCombo+"</h6>"); 
								      	    break;
								      	  }
								      	  
								      	  case "5":// Circunscripcion	
								      	  {
								      	    $("#tit5").html("<h6>Simulacion para Circunscripcion "+textoCombo+"</h6>"); 
								      	    break;
								      	  }
								      	  
								      	  case "6":// Distrito
								      	  {
								      	    
								      	    $("#tit6").html("<h6>Simulacion para "+textoCombo+"</h6>"); 
								      	  
								      	     break;
								      	  }
								      	  
								      	  
								      	  
                      							 }
                      							 
                      							 //eliminacion de las columnas innecesarias
                      							 var browser=navigator.appName;
                      							if (browser=="Microsoft Internet Explorer")
                      							{ 
                      							 $('tr td:nth-child(3), tr th:nth-child(3)', tblGraficos).remove();
                      							 $('tr td:nth-child(3), tr th:nth-child(3)', tblGraficos).remove();
                      							 $('tr td:nth-child(5), tr th:nth-child(5)', tblGraficos).remove();
                      							 $('tr td:nth-child(5), tr th:nth-child(5)', tblGraficos).remove();
                      							 $('tr td:nth-child(7), tr th:nth-child(7)', tblGraficos).remove();
                      							 $('tr td:nth-child(10), tr th:nth-child(10)', tblGraficos).remove();
                      							 $('tr td:nth-child(10), tr th:nth-child(10)', tblGraficos).remove();
                      							 
                      							 $('tr td:nth-child(12), tr th:nth-child(12)', tblGraficos).remove();
                      							 $('tr td:nth-child(12), tr th:nth-child(12)', tblGraficos).remove();
                      							 
                      							}
                      							else
                      							{
                      							
                      							$('#tblGraficos').removeCol(3);
									$('#tblGraficos').removeCol(3); 
									$('#tblGraficos').removeCol(5);
									$('#tblGraficos').removeCol(5); 
									$('#tblGraficos').removeCol(7);
									$('#tblGraficos').removeCol(10); 
									$('#tblGraficos').removeCol(10);
									
									$('#tblGraficos').removeCol(12); 
									$('#tblGraficos').removeCol(12);
									
									}
									                      							
                      							
                      							
                      							
                      							
                      							
                      							
                      							
                      							
                      							
                      							
                      							
                      							
                      							/*$("#divResultado").height($("#divTablas").height());*/
                      							
                      							$("#tblGraficos").focus();
                      							 
		                                                     
                      							 
		                                                      
		                                                      
		                                                      
		                                                      
		                                                     })
             
                 
                 
 		
 		
 		
 
 })


				


function IsValid_EMail( a_objeto ) {

    var ExpresionRegular1_Str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var ExpresionRegular2_Str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";

var ExpresionRegular1 = new RegExp( ExpresionRegular1_Str );
  var ExpresionRegular2 = new RegExp( ExpresionRegular2_Str );



    return( !ExpresionRegular1.test( a_objeto.value )  &&
             ExpresionRegular2.test( a_objeto.value ) );
}             
				                                 




function zoomText(Accion,Elemento){
//inicializacion de variables y parámetros
var obj=document.getElementById(Elemento);
var max = 200 //tamaño máximo del fontSize
var min = 70 //tamaño mínimo del fontSize
if (obj.style.fontSize==""){
obj.style.fontSize="100%";
}
actual=parseInt(obj.style.fontSize); //valor actual del tamaño del texto
incremento=10;// el valor del incremento o decremento en el tamaño

//accion sobre el texto
if( Accion=="reestablecer" ){
obj.style.fontSize="100%"
}
if( Accion=="aumentar" && ((actual+incremento) <= max )){
valor=actual+incremento;
obj.style.fontSize=valor+"%"
}
if( Accion=="disminuir" && ((actual+incremento) >= min )){
valor=actual-incremento;
obj.style.fontSize=valor+"%"
}
}







})
