// JavaScript Document

var espacioBlanco = " \t\n\r";

//Esta Vacio o lleno de espacios en blanco
function validarEmail(valor) 
{
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
    return (true)
  } else {
    return (false);
  }
}


function estaVacio(valor){
	var cont;
	
	if((valor == null) || (valor.length == 0)){
 		return true;
	}
	for(cont=0;cont<valor.length;cont++) 
	{
		var c = valor.charAt(cont);
		if(espacioBlanco.indexOf(c) == -1){
	 	return false;	
		}
	}
	return true;

}

// c es un digito
function esDigito(c)
{   return ((c >= "0") && (c <= "9"))
}

function comprueba_extension(formulario, archivo) { 
   extensiones_permitidas = new Array(".gif", ".jpg"); 
   mierror = ""; 
  //recupero la extensión de este nombre de archivo 
  extension = (archivo.substring(archivo.lastIndexOf("."))).toLowerCase(); 
  //alert (extension); 
  //compruebo si la extensión está entre las permitidas 
  permitida = false; 
  for (var i = 0; i < extensiones_permitidas.length; i++) { 
       if (extensiones_permitidas[i] == extension) { 
       permitida = true; 
       break;} 
   } 
   if (!permitida) { 
        return false;
       }else{ 
          //submito! 
         return true; 
       }       
}


function validarNumero(valor)
{
	var i;
    var punto;
    punto = false;
	if(estaVacio(valor))
	{
		return false;
	}
	for (i = 0; i <valor.length; i++)
    {   
        var c = valor.charAt(i);
        if( i != 0 )
		 {
            if ( c == "." ) 
			 {
                if( !punto )
				  {
					 punto = true;
					 if(i+1>=valor.length){
						 return false;
					 }
				  }
                else
				  {
                    
					return false;
				  }
             } else{     
                   if (!esDigito(c)){
					   
				     return false;}
				  
				}
         }
		else //empieza else2
		 { 
             if ( c == "." ){
                if( !punto ){
                    punto = true;}
				if(i+1>=valor.length){
					
					return false;}
               }else{
                	if (!esDigito(c) && (c != "-") || (c == "+")){
						
						return false;
					}else{
						if(c == "-"){
							if(i+1>=valor.length){
								
								return false;}
					  	   }
				    	}
			        }
          }//Termina el else2
    }
    return true;
    
}//Termina la funcion validarNumero

//Habilita y desabilita un campo
function abilitaCampo(formulario){
	if(formulario.familia.value == "lunch" || formulario.familia.value == "postre"){
		formulario.precio.disabled=false;}
    else{
	    formulario.precio.disabled=true;}
	
}

//Checa si alguna de las opciones del radio han sido seleccionadas
function opcionRadio(radio,numElem){
	if(numElem<=0)
	{
		return false;
	}
	for(cont=0;cont<numElem;cont++)
	{
		if(radio[cont].checked)
		{
			return true;
		}
	}
	return false;
}

//Valida si el usuario ha seleccionado alguna de las opciones del campo select
function validarSelect(campo,valor){
	if(campo.value == valor)
		return true;
	else
	    return false;	
}

 function maximaLongitud(texto,maxlong)
 {
   var tecla, int_value, out_value;

   if (texto.value.length > maxlong)
   {
     /*con estas 3 sentencias se consigue que el texto se reduzca
     al tamaño maximo permitido, sustituyendo lo que se haya
     introducido, por los primeros caracteres hasta dicho limite*/
     in_value = texto.value;
     out_value = in_value.substring(0,maxlong);
     texto.value = out_value;
     alert("La longitud maxima es de " + maxlong + " caracteres");
     return false; 
   }
   return true;
  } 
  
  function compara_fecha(fecha, fecha2)
  {
   
  
   suma=new Date(fecha.substring(0,4),fecha.substring(5,7)-1,fecha.substring(8,10));
   var anio=suma.getYear();   
   var mes =suma.getMonth()+1;  
   var dia =suma.getDate();    
   suma2=new Date(fecha2.substring(0,4),fecha2.substring(5,7)-1,fecha2.substring(8,10));
   var anio2=suma2.getYear();   
   var mes2 =suma2.getMonth()+1;   
   var dia2 =suma2.getDate();   
   if(anio2<anio){	  
    return false;
   }   
   if(anio<=anio2 && mes<=mes2)
   {
    if(anio==anio2 && mes==mes2 && dia>dia2)
    {
     return false;
    }
    return true;
   }
   else
   {
    return false;
   }
 
}

  
      function esFechaValida(fecha){
  
              var patron =/^\d{4}\-\d{2}\-\d{2}$/;
			  
              if (!fecha.match(patron)){
  
                  alert("formato de fecha no valido (aaaa-mm-dd)");
  
                  return false;
  
              }
  
              var anio =  parseInt(fecha.substring(0,4),10);
			  
             
			  var mes  =  parseInt(fecha.substring(5,7),10);
			  
  
              var dia  =  parseInt(fecha.substring(8),10);
			  
 
          switch(mes){
  
              case 1:
  
              case 3:
  
              case 5:
  
              case 7:
  
              case 8:
  
              case 10:
  
              case 12:
  
                  numDias=31;
  
                  break;
  
              case 4: case 6: case 9: case 11:
  
                  numDias=30;
  
                  break;
  
              case 2:
  
                  if (comprobarSiBisisesto(anio)){ numDias=29 }else{ numDias=28};
  
                  break;
  
              default:
  
                  alert("El valor de fecha introducida no es valida");
  
                  return false;
  
          }
  
       
  
              if (dia>numDias || dia==0){
  
                  alert("El valor de fecha introducida no es valida");
  
                  return false;

              }

              return true;

        

      }
	  

function comprobarSiBisisesto(anio){

if ( ( anio % 100 != 0) && ((anio % 4 == 0) || (anio % 400 == 0))) {

    return true;

    }

else {

    return false;

    }

}


