//var fechaDesdeInic="01/05/2002";

function seleccionarTodos()
{
if (document.forCriterios.chkSelTodos.checked)
	for (var i=0; i<document.forCriterios.chkPolenes.length;i++)
		document.forCriterios.chkPolenes[i].checked=true;
else
	for (var i=0; i<document.forCriterios.chkPolenes.length;i++)
		document.forCriterios.chkPolenes[i].checked=false;
	
}


function ocultarSintomas()
{
document.getElementById("sintomas").style.display="none";
document.getElementById("medic").style.display="none";
}

function mostrarSintomas()
{
document.getElementById("sintomas").style.display="block";
document.getElementById("medic").style.display="block";

}


////////////////////////////////////////////////
// VALIDACION DE DATOS Y ENVÍO
////////////////////////////////////////////////
function Enviar(fechaDesdeInic, idioma)
{
	

	var fechadesde = "";
	var fechahasta = "";

	// Validar que se ha informado la estación
	if (document.forCriterios.selEstacion.selectedIndex==0)
	{
		alert("No se ha seleccionado ninguna estación");
		return false;
	}
		

	// Validación Fecha desde
	if(trim(document.forCriterios.txtFDesde.value) != "" )
	{
		/* Comprobamos que el formato de la fecha es correcta */
		fechadesde = gbAQCheckFecha (trim(document.forCriterios.txtFDesde.value));
		if ( ! fechadesde )
		{
			if (idioma=="IN")
				alert("The From Date isn´t correct. Introduce it with format dd/mm/yyyy");	
			else
				alert("Fecha Desde no es válida. No tiene el formato dd/mm/yyyy");	
			
			return false;
		}
		else
		{
			document.forCriterios.txtFDesde.value = fechadesde;

//			if ( parseInt(fechadesde.substring(6,10)) < 1990)
//			{
//				alert("Fecha Desde no es válida. El año debe ser superior a 1990");	
//				return;
//			}
			
			// Validar que la fecha desde no es previa a la fecha de solape
			var bEsMenor=false;
/*			
			alert("fechadesde=" + fechadesde + "-fechaDesdeInic=" + fechaDesdeInic + "*");

			var nAnioDesde = parseInt(fechadesde.substring(6,10));
			var nMesDesde = parseInt(fechadesde.substring(3,5));
			var nDiaDesde = parseInt(fechadesde.substring(0,2));
					
			var nAnioDesdeInic = parseInt(fechaDesdeInic.substring(6,10));
			var nMesDesdeInic = parseInt(fechaDesdeInic.substring(3,5));
			var nDiaDesdeInic = parseInt(fechaDesdeInic.substring(0,2));

			if (nAnioDesde < nAnioDesdeInic)
			{
				bEsMenor=true;
				alert("1- nAnioDesde=" + nAnioDesde + "-nAnioDesdeInic=" + nAnioDesdeInic);
			}			 
			else if ((nAnioDesde==nAnioDesdeInic) && (nMesDesde<nMesDesdeInic))
			{
				bEsMenor=true;
				alert("2- nMesDesde=" + nMesDesde + "-nMesDesdeInic=" + nMesDesdeInic);
			}			 
			else if ((nMesDesde==nMesDesdeInic) && (nDiaDesde<nDiaDesdeInic))
			{
				bEsMenor=true;	
				alert("3- nDiaDesde=" + nDiaDesde + "-nDiaDesdeInic=" + nDiaDesdeInic);
			}
*/			 

			var nFechaDesde = parseInt(fechadesde.substring(6,10) + fechadesde.substring(3,5) + fechadesde.substring(0,2), 10);					
			
			var nFechaDesdeInic = parseInt(fechaDesdeInic.substring(6,10) + fechaDesdeInic.substring(3,5) + fechaDesdeInic.substring(0,2), 10);					

//			if (nFechaDesde < nFechaDesdeInic) bEsMenor=true;

			
//if (parseInt(fechadesde.substring(6,10)) < parseInt(fechaDesdeInic.substring(6,10))) bEsMenor=true;
//else if (parseInt(fechadesde.substring(3,5)) < parseInt(fechaDesdeInic.substring(3,5))) bEsMenor=true;
//else if (parseInt(fechadesde.substring(0,2)) < parseInt(fechaDesdeInic.substring(0,2))) bEsMenor=true;
			
			if (bEsMenor)
			{
				if (idioma=="IN")
					alert("The From Date must be equal or later of " + + fechaDesdeInic);	
				else
					alert("La fecha Desde debe ser igual o posterior a " + fechaDesdeInic);	
					
				return false;
			}
		}
	}
	
	
	// Validación Fecha hasta
	if (trim(document.forCriterios.txtFHasta.value) != "")
	{
		fechahasta = gbAQCheckFecha (trim(document.forCriterios.txtFHasta.value));
		//if ((! fechahasta) || ((trim(document.forCriterios.txtFHasta.value)).length != 10))
		if ( ! fechahasta )
		{
			if (idioma=="IN")
				alert("The Until Date isn´t correct. Introduce it with the format dd/mm/yyyy");	
			else			
				alert("Fecha Hasta no es válida. No tiene el formato dd/mm/yyyy");	
					
			return false;
		}
		else
		{
 			document.forCriterios.txtFHasta.value = fechahasta;
 
// 			if ( parseInt(fechahasta.substring(6,10)) < 1990)
//			{
//				alert("Fecha Hasta no es válida. El año debe ser superior a 1990");	
//				return;
//			}
		}
	}	

	// Validar que Fecha Desde < Fecha Hasta si ambas informadas
	//if ((trim(document.forCriterios.txtFDesde.value) != "") && (trim(document.forCriterios.txtFHasta.value) != ""))
	if (fechadesde != "" && fechahasta != "")
	{
		// Fecha hasta, posterior a fecha desde
		if (! EsFechaMayor(fechahasta, fechadesde) || 
			(fechadesde==fechahasta))
		{
			if (idioma=="IN")
				alert("The From Date must be previous of the Until Date");	
			else
				alert("Fecha Desde debe ser anterior a Fecha Hasta");
						
			return false;
		}
	}


	
	// Informar el campo hidden de pólenes
        var sDatosPolenes="";
        var polenesSeleccionados = 0;
	for (var i=0; i<document.forCriterios.chkPolenes.length;i++)
        {
            if (document.forCriterios.chkPolenes[i].checked){
                sDatosPolenes=sDatosPolenes + "&polen=" + document.forCriterios.chkPolenes[i].value;
                polenesSeleccionados++;
	    }
        }
	document.forCriterios.numPolenesSeleccionados.value = polenesSeleccionados;
        // Se debe informar al menos un polen
        if (sDatosPolenes=="") 
        {
		if (idioma=="IN")
			alert("You must select, at least, one pollen.");	
        	else
			alert("Debe seleccionar, al menos, un polen.");
        	
        	
        	return false;
        }
        //alert(sDatosPolenes);
    	document.forCriterios.hidPolen.value=sDatosPolenes;
	// Estación
	codEst=document.forCriterios.selEstacion.options[document.forCriterios.selEstacion.selectedIndex].value;
	codEst = codEst.substring(0,3);
	
	//alert("codigo de esta " + codEst);
	
	document.forCriterios.prov.value=codEst;

//7        // Informar el campo hidden de check de selección
//        if (document.forCriterios.chkSelTodos.checked)
//            document.forCriterios.hidCheckSel.value="CHECKED";
//        else
//            document.forCriterios.hidCheckSel.value="";

	// Enviar formulario
// document.forCriterios.submit();
    	return true;

}
	

function muestraGrafPolenSolo(sFechaDesdeInic)
{

   document.forCriterios.hidPolenSolo.value="S";
   Enviar(sFechaDesdeInic);

}


function ponTrimestre(sTrimestre)
{
//	alert("LONGITUD DE COMBO DE ANIOS ES " + document.forCriterios.selAnioTrimes.options.length);
	if (document.forCriterios.selAnioTrimes.options.length>0)
	{
		var sAnioTri = document.forCriterios.selAnioTrimes.options[document.forCriterios.selAnioTrimes.selectedIndex].value;
//	var sAnioTri = document.forCriterios.selAnioTrimes.value;
//	alert("sAnioTri=" + sAnioTri);


		if (sTrimestre=="1")
		{
			document.forCriterios.txtFDesde.value = "01/01/" + sAnioTri;
			document.forCriterios.txtFHasta.value = "30/04/" + sAnioTri;		
		}
		else if (sTrimestre=="2")
		{
			document.forCriterios.txtFDesde.value = "01/05/" + sAnioTri;
			document.forCriterios.txtFHasta.value = "31/08/" + sAnioTri;		
		}
		else if (sTrimestre=="3")
		{
			document.forCriterios.txtFDesde.value = "01/09/" + sAnioTri;
			document.forCriterios.txtFHasta.value = "31/12/" + sAnioTri;		
		}
	}
}


function ponTrimestreAnio()
{
	var sAnioTri = document.forCriterios.selAnioTrimes.options[document.forCriterios.selAnioTrimes.selectedIndex].value;

var sPer = document.forCriterios.selPeriodo.options[document.forCriterios.selPeriodo.selectedIndex].value;

	if (sPer=="14")
	{
		ponTrimestre("1");
		return;
	}		
	if (sPer=="24")
	{
		ponTrimestre("2");
		return;
	}		
	if (sPer=="34")
	{
		ponTrimestre("3");
		return;
	}			
	else 
	{
		var sFDesde = document.forCriterios.txtFDesde.value;
		var sFHasta = document.forCriterios.txtFHasta.value;

		if (sFDesde.length==10)
		{
			// Sustituir el año
			sFDesde = sFDesde.substring(0,6) + sAnioTri;
			document.forCriterios.txtFDesde.value = sFDesde;
		}

		if (sFHasta.length==10)
		{
			// Sustituir el año
			sFHasta = sFHasta.substring(0,6) + sAnioTri;
			document.forCriterios.txtFHasta.value = sFHasta;
		}

	}

}


function ponDesdeAnio(sAnio)
{

	document.forCriterios.txtFDesde.value = "01/01/" + sAnio;
	document.forCriterios.txtFHasta.value = "";		

}

function ponDesdeSemana(sFecha)
{

	document.forCriterios.txtFDesde.value = sFecha;
	document.forCriterios.txtFHasta.value = "";		

}





/************************************************************************************
**************************************************************************************/
function gbAQCheckFecha(inp)
{
	var j1,j2, x1, x2, aa, bb, cc;
	var dd,mm,yy;

	 if (inp.value!=null)
	  str=inp.value + "";
	 else
	  str=inp;

	var bisiesto=0;
	var alerta=0;

	var resto4=0;
	var resto100=0;
	var resto400=0;

	//SEPARAMOS LOS DATOS
	//*****************
	x1 = str.indexOf('/', 0);
	x2 = str.indexOf('/', x1+1);

	aa=str.substring(0, x1);
	bb=str.substring(x1+1, x2);
	cc=str.substring(x2+1, str.length);

	//COMPROBAR QUE SON NUMEROS
	if ((gbAQCheckNumEntero (aa)!=true) || (gbAQCheckNumEntero (bb)!=true) || (gbAQCheckNumEntero (cc)!=true))
	{
	return(false);
	}

	//FORMATO dia/mes/año
	//*******************

		dd=aa;
		mm=bb;
		yy=cc;

	//RESPECTO A LOS MESES Y DIAS
	//FORMATEAMOS A DOS CIFRAS, y desechamos largos y 0.
	//**************************************************

	if (mm.length > 2 || dd.length > 2 )
	{return(false);}

	if (mm.length == 1)
	{mm='0'+mm;}


	if (dd.length == 1)
	{dd='0'+dd;}

	if (dd=='00' || mm =='00')
	{return(false);}

	if (mm == '12' || mm == '11' || mm == '10' || mm == '09' || mm == '08' || mm == '07' || mm == '06' || mm == '05' || mm == '04' || mm == '03' || mm == '02' || mm == '01')
		{}
	else {return(false)}


	//descomponer los dias
	j1=dd.substring(0, 1);
	j2=dd.substring(1, 2);


	if (j1 == '4' || j1 == '5' || j1 == '6' || j1 == '7' || j1 == '8' || j1 == '9')
	{return(false);}


		//dentro de cada mes
	//***************************
	if (mm == '01')
	{
		if (j1 == '0' || j1 == '1' || j1 == '2')
			{}
		else if (j1 == '3' && (j2 == '0' || j2 == '1'))
			{}
		else
			{return (false);}
	}


	else if (mm == '02')
	{
		if (j1 == '0' || j1 == '1')
			{}
		else if (j1 == '3')
			{return(false);}
		else if (j1 == '2')	
			{
			if (j2 == '9')
				{alerta =1;}
			}		
	}

	else if (mm == '03')
	{
		if (j1 == '0' || j1 == '1' || j1 == '2')
			{}
		else if (j1 == '3' && (j2 == '0' || j2 == '1'))
			{}
		else
			{return (false);}
	}

	else if (mm == '04')
	{
		if (j1 == '0' || j1 == '1' || j1 == '2')
			{}
		else if (j1 == '3' && j2 == '0')
			{}
		else
			{return (false);}
	}
	else if (mm == '05')
	{
		if (j1 == '0' || j1 == '1' || j1 == '2')
			{}
		else if (j1 == '3' && (j2 == '0' || j2 == '1'))
			{}
		else
			{return (false);}
	}
	else if (mm == '06')
	{
		if (j1 == '0' || j1 == '1' || j1 == '2')
			{}
		else if (j1 == '3' && j2 == '0')
			{}
		else
			{return (false);}
	}

	else if (mm == '07')
	{
		if (j1 == '0' || j1 == '1' || j1 == '2')
			{}
		else if (j1 == '3' && (j2 == '0' || j2 == '1'))
			{}
		else
			{return (false);}
	}

	else if (mm == '08')
	{
		if (j1 == '0' || j1 == '1' || j1 == '2')
			{}
		else if (j1 == '3' && (j2 == '0' || j2 == '1'))
			{}
		else
			{return (false);}
	}
	else if (mm == '09')
	{
		if (j1 == '0' || j1 == '1' || j1 == '2')
			{}
		else if (j1 == '3' && j2 == '0')
			{}
		else
			{return (false);}
	}

	else if (mm == '10')
	{
		if (j1 == '0' || j1 == '1' || j1 == '2')
			{}
		else if (j1 == '3' && (j2 == '0' || j2 == '1'))
			{}
		else
			{return (false);}
	}
	else if (mm == '11')
	{
		if (j1 == '0' || j1 == '1' || j1 == '2')
			{}
		else if (j1 == '3' && j2 == '0')
			{}
		else
			{return (false);}
	}
	else if (mm == '12')
	{
		if (j1 == '0' || j1 == '1' || j1 == '2')
			{}
		else if (j1 == '3' && (j2 == '0' || j2 == '1'))
			{}
		else
			{return (false);}
	}
	//no debe darse
	else
	{return(false);}


		//***********************
		//sacar el año con 4 cifras=total.
	
	if (yy.length >4 || yy.length==0 || yy.length==1)
		{return(false);}

	else if (yy.length == 4)
		{}

	else if (yy.length == 2)
	{
	//leo el 1º caracter
	i1=yy.substring(0,1);

	if (i1 == '4' || i1 == '5' || i1 == '6' || i1 == '7' || i1 == '8' || i1 == '9')
		{yy='19'+yy;}
	else
		{yy='20'+yy;}
	}//fin elseif

		//año
	total=parseInt(yy);
		//*********************

	resto4=total % 4;
	resto100=total % 100;
	resto400=total % 400;
	//***********************


	//ESTUDIO DE BISIESTO
	//********
	//divisible entre 4
	if (resto4 == 0)
		{
		//no divis. entre 100
		if (resto100 != 0)
			{bisiesto=1;}
		//si div. entre 100
		else
			{
			//div. entre 400
			if (resto400 == 0)
			{bisiesto=1;}
			
			}//fin else

		}//fin if	

	//AÑO BISIESTO
	//*************
	if (alerta == 1 && bisiesto == 1)
		{} 
	else if (alerta ==1 && bisiesto == 0)
		{return(false);}
	else
		{}

	//FIN DE FUNCION: todo bien.
	//**************************
	fecha=dd+"/"+mm+"/"+yy;
	return(fecha);
}

/************************************************************************************
**************************************************************************************/
function gbAQCheckNumEntero(input)
{
	var str

	if (input.value!=null)
	str=input.value + "";
	else
	str=input;

	for (var j=0;j<str.length;j++)
	{
		var ch=str.substring(j,j+1)
		if ((ch<"0" || "9" < ch))
		{
		return(false);
		}
	}
	return(true);
}


/************************************************************************************
 ************************************************************************************/
function EsFechaMayor(vFecha1, vFecha2)
{
	var dd1, dd2, mm1, mm2, yy1, yy2 
	var miInd11,  miInd12, miInd21, miInd22
	var d1,m1,y1,d2,m2,y1

	//Obtener día, mes y año de la primera fecha
	miInd11 = vFecha1.indexOf('/', 0);  
	miInd12 = vFecha1.indexOf('/', miInd11 +1);

	d1=vFecha1.substring(0, miInd11);
	m1=vFecha1.substring(miInd11+1, miInd12);
	y1=vFecha1.substring(miInd12+1, vFecha1.length);


	//Obtener día, mes y año de la segunda fecha
	miInd21 = vFecha2.indexOf('/', 0);  
	miInd22 = vFecha2.indexOf('/', miInd21 +1);

	d2=vFecha2.substring(0, miInd21);
	m2=vFecha2.substring(miInd21+1, miInd22);
	y2=vFecha2.substring(miInd22+1, vFecha2.length);


	//Calcular el valor entero
	dd1=parseInt(d1,10);
	mm1=parseInt(m1,10);
	yy1=parseInt(y1,10);

	if (yy1<100)
	{
		yy1=yy1+1900; 
	}

	dd2=parseInt(d2,10);
	mm2=parseInt(m2,10);
	yy2=parseInt(y2,10);

	if (yy2<100)
	{
		yy2=yy2+1900; 
	}


	//Comparación de las fechas

	  //Si son iguales es válido
	if (yy1==yy2 && mm1==mm2 && dd1==dd2)
	{  
		 return(true);
	}

	 //Condiciones si son distintas
	if (yy1>yy2)
	{
		return(true); 
	}
	else if (yy1==yy2)
	{
		if (mm1>mm2)
    	{
  		return(true);
		}
		else if (mm1==mm2)
    	{
  			if (dd1>dd2)
   			{
			return(true);  
   			}
		}
	}

	return(false);

}


/************************************************************************************
**************************************************************************************/
function EsHoraOK(vHora)
{
	//Obtener día, mes y año de la primera fecha
	if (vHora.length == 5)
	{
		var hh, mm; 
		var miInd;

		//Obtener la hora y los minutos
		miInd = vHora.indexOf(':', 0);  

		hh=vHora.substring(0, miInd);
		mm=vHora.substring(miInd + 1, vHora.length);

		if((hh.length != 2) ||(mm.length != 2))
			return false;
		
	
		if ((parseInt(hh) <= 23) && (parseInt(hh) >= 0) && (parseInt(mm) <= 59) && (parseInt(mm) >= 0))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else 
		return false;
			
}			
	
/************************************************************************************
**************************************************************************************/
function EsHoraMayor(vHora1, vHora2)
{
	if ( (EsHoraOK(vHora1)) && (EsHoraOK(vHora2)) )
	{
		var hora1, hora2;
		var minuto1, minuto2;
		hora1 = vHora1.substring(0,2);  
		minuto1 = vHora1.substring(3,5);
		hora2 = vHora2.substring(0,2);  
		minuto2 = vHora2.substring(3,5);
		if(parseInt(hora1) > parseInt(hora2))
			return true;
		else if(parseInt(hora1) == parseInt(hora2))
		{
			if (parseInt(minuto1) > parseInt(minuto2))
				return true;
		}		
		return false
	}
	else
		return false;
}

function rtrim(str)
{
	str = "" + str  // evitar error si no se pasa nada
	while (str.substring(0,1) == ' ')
	{
		str = str.substring(1, str.length);
	}
	return str;
}

function ltrim(str)
{
	str=str + ""  // evitar error si no se pasa nada
	while (str.substring(str.length-1,str.length) == ' ')
	{
		str = str.substring(0,str.length-1);
	}
	return str;
}

function trim(str1)
{
	var str;
	str= ltrim(rtrim(str1));
	return str;
}

function bajaVideo()
{
	var video = document.frmVideos.selVideos.options[document.frmVideos.selVideos.selectedIndex].value;

	if(video != "" )
	{
		location.href=video;
	}


}

function PonAnios()
{
   var sListaAnios = document.forCriterios.selEstacion.options[document.forCriterios.selEstacion.selectedIndex].value;

   sEstacion=sListaAnios.substring(0,3);
   if (sEstacion=="MAD") 
	mostrarSintomas();
   else
	ocultarSintomas();
//alert("sListaAnios=" + sListaAnios);
sListaAnios=sListaAnios.substring(3,sListaAnios.length);

CargaComboIE(document.forCriterios.selAnioTrimes,sListaAnios,"",false);
}

function CargaComboIE(objSelect, sData, sSelItem, bCod)
{

	var nIniDat,nFinDat;
	var j,k;
	var sElemento;
	var bSelected;
	var sSepElement, sSepVal;
	var nSelectedIndex;
	
					  
	nIniDat=0;
	nSelectedIndex=0;
	
	//alert("dentro de Carga combo IE");

	/* 
	Vaciar el contenido incial de la combo
	*/
	objSelect.options.length=0;

	/* obtener los elementos separadores */
	if (bCod==true)
	{
		sSepVal="~";
		sSepElement="|";
		sData=sData.substring(2, sData.length);
	}
	else
	{
		sSepElement="|";
		//sData=sData.substring(1, sData.length);		
	}
	
	
    for (j=0;j<sData.length;j++)
    {
	     var ch=sData.substring(j,j+1);
	     if (ch==sSepElement)
	     {
	
	     	// obtener elemento
	     	nFinDat=j;
	     	sElemento=sData.substring(nIniDat,nFinDat);
			nIniDat=j+1;
	
			if (bCod==true)	   
			{
				/****** Valores con formato Value-Descripción *******/			
				// desglosar valor+texto
			   for (k=0;k<sElemento.length;k++)
			   {		   	
		     		var ch=sElemento.substring(k,k+1);
			   		if (ch==sSepVal)
			   		{		   	
			   			if (sElemento.substring(0,k)==sSelItem) nSelectedIndex=objSelect.options.length;
	
						// var opt= new Option(sElemento.substring(0,k),sElemento.substring(k+1,sElemento.length),true,bSelected);
						var opt= new Option(sElemento.substring(k+1,sElemento.length),sElemento.substring(0,k));	        			
						objSelect.options[objSelect.options.length]=opt;
	        			opt=null;
	        			
			   		}
			   }
			}
			else
			{
				/****** Valores con formato Descripción (Valor=Descripción) *****/
			   	if (sElemento==sSelItem) nSelectedIndex=objSelect.options.length;
					
				var opt= new Option(sElemento,sElemento);
				objSelect.options[objSelect.options.length]=opt;
				opt=null;
			
			}	     		     	  
	     	
	     }
	
	}

	// Seleccionar el elemento
	objSelect.options[nSelectedIndex].selected=true;
	
}
