

function valida(formulario) 
{ 

var checkStrg = document.getElementById('SearchString').value; 



  if (document.getElementById('SearchString').value == "")  
	{
    alert("Escriba un valor");
    document.getElementById('SearchString').focus();
    return (false);
  }

  if (checkStrg.length < 3)  {
    alert("La consulta debe tener mas de 2 letras.");
     document.getElementById('SearchString').focus();
    return (false);
  }

  if (document.getElementById('SearchString').value == "buscador")
{ 
    document.getElementById('SearchString').value="";
    document.getElementById('SearchString').focus();
    return (false);
  }


  return (true);
}


function limpia_caja_texto()
{
var busca_txt =document.getElementById('SearchString').value;
if(busca_txt == 'buscador')
	{document.getElementById('SearchString').value="";}
}


function colorea(cual)
	{
	if (cual==0) {
	document.getElementById("dia").style.backgroundColor="#FFFFFF";
	document.getElementById("mes").style.backgroundColor="#FFFFFF";
	document.getElementById("anio").style.backgroundColor="#FFFFFF";
	document.getElementById("diaentre1").style.backgroundColor="#E1E1E1";
	document.getElementById("mesentre1").style.backgroundColor="#E1E1E1";
	document.getElementById("anioentre1").style.backgroundColor="#E1E1E1";
	document.getElementById("diaentre2").style.backgroundColor="#E1E1E1";
	document.getElementById("mesentre2").style.backgroundColor="#E1E1E1";
	document.getElementById("anioentre2").style.backgroundColor="#E1E1E1";
	document.getElementById("literal").style.backgroundColor="#E1E1E1";}

	if (cual==1) {
	document.getElementById("dia").style.backgroundColor="#E1E1E1";
	document.getElementById("mes").style.backgroundColor="#E1E1E1";
	document.getElementById("anio").style.backgroundColor="#E1E1E1";
	document.getElementById("diaentre1").style.backgroundColor="#FFFFFF";
	document.getElementById("mesentre1").style.backgroundColor="#FFFFFF";
	document.getElementById("anioentre1").style.backgroundColor="#FFFFFF";
	document.getElementById("diaentre2").style.backgroundColor="#FFFFFF";
	document.getElementById("mesentre2").style.backgroundColor="#FFFFFF";
	document.getElementById("anioentre2").style.backgroundColor="#FFFFFF";
	document.getElementById("literal").style.backgroundColor="#E1E1E1";}

	if (cual==2) {
	document.getElementById("dia").style.backgroundColor="#E1E1E1";
	document.getElementById("mes").style.backgroundColor="#E1E1E1";
	document.getElementById("anio").style.backgroundColor="#E1E1E1";
	document.getElementById("diaentre1").style.backgroundColor="#E1E1E1";
	document.getElementById("mesentre1").style.backgroundColor="#E1E1E1";
	document.getElementById("anioentre1").style.backgroundColor="#E1E1E1";
	document.getElementById("diaentre2").style.backgroundColor="#E1E1E1";
	document.getElementById("mesentre2").style.backgroundColor="#E1E1E1";
	document.getElementById("anioentre2").style.backgroundColor="#E1E1E1";
	document.getElementById("literal").style.backgroundColor="#FFFFFF";
	}


	if (cual==3) {
	document.getElementById("dia").style.backgroundColor="#E1E1E1";
	document.getElementById("mes").style.backgroundColor="#E1E1E1";
	document.getElementById("anio").style.backgroundColor="#E1E1E1";
	document.getElementById("diaentre1").style.backgroundColor="#E1E1E1";
	document.getElementById("mesentre1").style.backgroundColor="#E1E1E1";
	document.getElementById("anioentre1").style.backgroundColor="#E1E1E1";
	document.getElementById("diaentre2").style.backgroundColor="#E1E1E1";
	document.getElementById("mesentre2").style.backgroundColor="#E1E1E1";
	document.getElementById("anioentre2").style.backgroundColor="#E1E1E1";
	document.getElementById("literal").style.backgroundColor="#FFFFFF";

  	if (document.getElementById('literal').value == "Escriba la/s palabra/s clave")
		{ 
    		document.getElementById('literal').value="";
    		document.getElementById('literal').focus();
    		return (false);
  		}

	}
	
	}


function comprueba(vari)
	{
	if (vari==0 && document.getElementById("mes").selectedIndex!=0 && document.getElementById("dia").selectedIndex!=0 && document.getElementById("anio").options[document.getElementById("anio").selectedIndex].value!=0)
		{isDate(vari, document.getElementById("mes").selectedIndex ,document.getElementById("dia").selectedIndex , document.getElementById("anio").options[document.getElementById("anio").selectedIndex].value);
		return true;
		}

	if (vari==1 && document.getElementById("mesentre1").selectedIndex!=0 && document.getElementById("diaentre1").selectedIndex!=0 && document.getElementById("anioentre1").options[document.getElementById("anioentre1").selectedIndex].value!=0)
		{isDate(vari,document.getElementById("mesentre1").selectedIndex,document.getElementById("diaentre1").selectedIndex,document.getElementById("anioentre1").options[document.getElementById("anioentre1").selectedIndex].value);
		return true;
		}

	if (vari==2 && document.getElementById("mesentre2").selectedIndex!=0 && document.getElementById("diaentre2").selectedIndex!=0 && document.getElementById("anioentre2").options[document.getElementById("anioentre2").selectedIndex].value!=0)
		{isDate(vari,document.getElementById("mesentre2").selectedIndex,document.getElementById("diaentre2").selectedIndex,document.getElementById("anioentre2").options[document.getElementById("anioentre2").selectedIndex].value);
		return true;
		}

	if (vari==3)
		{
		var literal_consulta = document.getElementById('literal').value;
		if (literal_consulta.length>0)
			{return true;}
		}

    	return false;
	}




function isDate(vari,C_mes,c_dia,c_anio) {
    dateStr=C_mes+"/"+c_dia+"/"+c_anio

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); 

    if (matchArray == null) {
        alert("Formato de fecha mm/dd/yyyy o mm-dd-yyyy.");
        return false;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("El mes no es correcto.");
        return false;
    }

    if (day < 1 || day > 31) {
        alert("El día no es correcto.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("El mes no tiene 31 días!")
	if (vari==0) {situa("1",C_mes,c_anio)}
	if (vari==1) {situa1("1",C_mes,c_anio)}
	if (vari==2) {situa2("1",C_mes,c_anio)}
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            alert("Febrero de " + year + " no tiene " + day + " días!");
	    if (vari==0) {situa("1",C_mes,c_anio)}
	    if (vari==1) {situa1("1",C_mes,c_anio)}
	    if (vari==2) {situa2("1",C_mes,c_anio)}
            return false;
        }
    }
    return true; 
}



function situa(dia,mes,anio)
	{
	document.getElementById("dia").selectedIndex=dia;
	document.getElementById("mes").selectedIndex=mes;
	for (m=0; m<document.getElementById("anio").length; m++) 
		{if (document.getElementById("anio").options[m].value==anio)
			{document.getElementById("anio").selectedIndex=m}
		}
	
	}

function situa1(dia,mes,anio)
	{
	document.getElementById("diaentre1").selectedIndex=dia;
	document.getElementById("mesentre1").selectedIndex=mes;
	for (m=0; m<document.getElementById("anioentre1").length; m++) 
		{if (document.getElementById("anioentre1").options[m].value==anio)
			{document.getElementById("anioentre1").selectedIndex=m}
		}
	}

function situa2(dia,mes,anio)
	{
	document.getElementById("diaentre2").selectedIndex=dia;
	document.getElementById("mesentre2").selectedIndex=mes;
	for (m=0; m<document.getElementById("anioentre2").length; m++) 
		{if (document.getElementById("anioentre2").options[m].value==anio)
			{document.getElementById("anioentre2").selectedIndex=m}
		}
	}





function enviar(formu)
	{

	if(formu.id=="formfecha") {

		if(comprueba(0)==false)
			{alert('La fecha está incompleta');
			return false;}
		return true;
			}
	if(formu.id=="formfechaentre") {

		if(comprueba(1)==false)
			{alert('La primera fecha está incompleta');
			return false;}
		if(comprueba(2)==false)
			{alert('La segunda fecha está incompleta');
			return false;}
			var fecha = new Date(document.getElementById("anioentre1").options[document.getElementById("anioentre1").selectedIndex].value,document.getElementById("mesentre1").selectedIndex,document.getElementById("diaentre1").selectedIndex) 
			var fecha1 = new Date(document.getElementById("anioentre2").options[document.getElementById("anioentre2").selectedIndex].value,document.getElementById("mesentre2").selectedIndex,document.getElementById("diaentre2").selectedIndex) 
			if (fecha > fecha1)
			{alert('la fecha inicial es posterior a la fecha final')
			return false;} 
			}
	if(formu.id=="formliteral") {

		if (document.getElementById("literal").value=="")
			{alert('Debe escribir alguna palabra clave');
			colorea(2);
			document.getElementById("literal").focus();
			return false;}

		if (document.getElementById("literal").value=="Escriba la/s palabra/s clave")
			{alert('Debe escribir alguna palabra clave');
			colorea(2);
			document.getElementById("literal").value="";
			document.getElementById("literal").focus();
			return false;}

		}

	}




	



