
function ControlarFecha(Dame,Minimo,Maximo,Nombre)
{
var Fecha = new String(Dame);
if (Fecha=="") 
{
return true;
}
       
        var Topes = "312831303130313130313031"
        var Mensaje = Nombre + " es errónea ";
if ( (Fecha.length != 8) && (Fecha.length != 10) && (Fecha.length != 6))
  {
  
   alert(Mensaje);
   return false;
   }

if (Fecha.length == 10)
{
        if (Fecha.substring(2,3)  != '/' || Fecha.substring(5,6) != '/')
        {
         alert(Mensaje);
         return false;
        }
        else
        {
        var Apoyodia    = Fecha.substring(0,2)
        var Apoyomes    = Fecha.substring(3,5)
        var Apoyoano    = Fecha.substring(6)
        }
}
else
{
     var Apoyodia    = Fecha.substring(0,2)
     var Apoyomes    = Fecha.substring(2,4)    
        if (Fecha.length == 8)
        {
       
        var Apoyoano    = Fecha.substring(4)
        }
        else
        {
       
        var Apoyoano= '19' + Fecha.substring(4)
        }
}

        

        if ( (EsTodoNumero(Apoyodia)== false) || (EsTodoNumero(Apoyomes)== false) || ( EsTodoNumero(Apoyoano)== false) )
        {
                alert(Mensaje);
                return false;
        }
        /*
        if (Apoyoano < '1900')
        {
                alert(Mensaje);
                return false;
        }
        */
         if (Apoyoano > '2050')
        {
                alert(Mensaje);
                return false;
        }
        
        if ( (Apoyomes > '12') || (Apoyomes < '01') )
        {
        
                alert(Mensaje);
                return false;
        }

        var Febrerobis = '28';
        if (Apoyomes == '02')
        {
                if (VerBisiesto(Apoyoano))
                {
                        Febrerobis = '29'
                }
             
        }
        else
        {
                Febrerobis = Topes.substring((Apoyomes-1)*2, (Apoyomes-1)*2+2)
        }

        if ( (Apoyodia > Febrerobis) || (Apoyodia <'01') )
        {
                alert(Mensaje);
                return false;
        }


    
      if (Fecha=="")
      {
      return true;
      }

 
var devuelve =  new String(ComponerFecha(Fecha));    
var fechacompuesta =  devuelve.substring(6) + devuelve.substring(3,5) + devuelve.substring(0,2);
if (Minimo != 'N')
{
devuelve =  new String(ComponerFecha(Minimo));    
var minimocompuesto =  devuelve.substring(6) + devuelve.substring(3,5) + devuelve.substring(0,2);
//alert(fechacompuesta + ' ' + minimocompuesto)
   if (fechacompuesta < minimocompuesto)
   {
    alert(Nombre + ' no puede ser anterior a ' + devuelve);
    return false;
   }

} 

if (Maximo != 'N')
{
devuelve =  new String(ComponerFecha(Maximo));    
var maximocompuesto =  devuelve.substring(6) + devuelve.substring(3,5) + devuelve.substring(0,2);


   if (fechacompuesta  > maximocompuesto)
   {
    alert(Nombre + ' no puede ser posterior a ' + devuelve);
    return false;
   }

} 
return true;
}
function ComponerFecha(Dame)
{
var Fecha = new String(Dame);
if (Fecha.length == 10 || Fecha == "")
{
  return Fecha;
}  
else
{
 var Apoyodia    = new String(Fecha.substring(0,2));
 var Apoyomes    = new String(Fecha.substring(2,4));
 if (Fecha.length == 8)
 {
 var Apoyoano    = new String(Fecha.substring(4,8));
 }
 else
 {
 var Apoyoano    = '19'
 Apoyoano = Apoyoano + Fecha.substring(4,8);
 }

 return Apoyodia+"/"+Apoyomes+"/"+Apoyoano;
}

}

function VerBisiesto (Ano)
{
        if (((Ano % 4)==0) && ((Ano % 100)!=0) || ((Ano % 400)==0))
        {
          return (true);
        }
        else
        {
         return (false);
        }
}
