 /* Funcion para validar una fecha en un campo de texto con mensaje invalido */

function CheckDateField(data,campo)
{
  if(!CheckDate(data,campo))
  {
    MenssageData(campo)
  }
}

/* Funcion para validar una fecha en un campo de texto */

function CheckDate(data, campo)
{
  var dia;
  var mes;
  var ano;

  var bResult = false;
  /* Si los dos primeros digitos no son numericos */

  if(data == "")
  {
    return false;
  }
  
  if(isNaN(data.substring(0,2)) || data.substring(0,10).length < 5 || parseInt(data.substring(0,2),10) == 0)
  {
    bResult = false;
  }
  /* Si los dos primeros digitos si son numericos */
  else
  {
    /* Si el tercer digito es una barra invertida */

    if(data.substring(2,3)=="/")
    {
      /* Si no son numericos los 2 siguientes digitos */

      if(isNaN(data.substring(3,5)) || data.substring(3,5) > 12 || parseInt(data.substring(3,5),10) == 0)
      {
        bResult = false;
      }
      /* Si los 2 siguientes digitos si son numericos */
      else
      {
        if(data.substring(5,6)=="/")
        {
          if(isNaN(data.substring(6,10)) || data.substring(6,10).length > 4 || data.substring(6,10).length ==0)
          {
            bResult = false;
          }
          else
          {
            if(data.substring(6,10).length < 4)
            {
              if (parseInt(data.substring(6,10),10) < 50)
              {
                ano=2000+parseInt(data.substring(6,10),10);
              }
              else
              {
                ano=1900+parseInt(data.substring(6,10),10);
              }
            }
            else
            {
              ano=parseInt(data.substring(6,10),10);
            }

            mes=data.substring(3,5);
            dia=data.substring(0,2);

            if(dia > GetDaysInMonth(mes,ano))
            {
              bResult = false;
            }
            else
            {
              campo.value = dia + "/" + mes + "/" + ano;
              return true;
            }
          }
        }
        else
        {
          if(isNaN(data.substring(5,10)) || data.substring(5,10).length > 4 || data.substring(5,10).length ==0)
          {
            bResult = false;
          }
          else
          {                     
            if(data.substring(5,10).length < 4)
            {
              if (parseInt(data.substring(5,10),10) < 50)
              {
                ano=2000+parseInt(data.substring(5,10),10);
              }
              else
              {
                ano=1900+parseInt(data.substring(5,10),10);
              }
            }
            else
            {
              ano=parseInt(data.substring(5,10),10);
            }

            mes=data.substring(3,5);
            dia=data.substring(0,2);

            if(dia > GetDaysInMonth(mes,ano))
            {
              bResult = false;
            }
            else
            {
              campo.value = dia + "/" + mes + "/" + ano;
              return true;              
            }
          }
        }
      }
    }
    else
    {
      if(isNaN(data.substring(2,4)) || data.substring(2,4) > 12 || parseInt(data.substring(2,4),10) == 0)
      {
        bResult = false;
      }
      else
      {
        if(data.substring(4,5)=="/")
        {
          if(isNaN(data.substring(5,10)) || data.substring(5,10).length > 4 || data.substring(5,10).length ==0)
          {
            bResult = false;
          }
          else
          {
            if(data.substring(5,10).length < 4)
            {
              if (parseInt(data.substring(5,10),10) < 50)
              {
                ano=2000+parseInt(data.substring(5,10),10);
              }
              else
              {
                ano=1900+parseInt(data.substring(5,10),10);
              }
            }
            else
            {
              ano=parseInt(data.substring(5,10),10);
            }

            mes=data.substring(2,4);

            dia=data.substring(0,2);

            if(dia > GetDaysInMonth(mes,ano))
            {
              bResult = false;
            }
            else
            {
              campo.value = dia + "/" + mes + "/" + ano;
              return true;              
            }
          }
        }
        else
        {
          if(isNaN(data.substring(4,10)) || data.substring(4,10).length > 4 || data.substring(4,10).length ==0)
          {
            bResult = false;
          }
          else
          {                      
            if(data.substring(4,10).length < 4)
            {
              if (parseInt(data.substring(4,10),10) < 50)
              {
                ano=2000+parseInt(data.substring(4,10),10);
              }
              else
              {
                ano=1900+parseInt(data.substring(4,10),10);
              }
            }
            else
            {
              ano=parseInt(data.substring(4,10),10);
            }

            mes=data.substring(2,4);
            dia=data.substring(0,2);

            if(dia > GetDaysInMonth(mes,ano))
            {
              bResult = false;
            }
            else
            {
              campo.value = dia + "/" + mes + "/" + ano;
              return true;
            }
          }
        }
      }
    }
  }

  return bResult;
}

/* Funcion para obtener los dias de un mes concreto */

function GetDaysInMonth(Mes, Ano)
{
  var DaysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);  
  // Comprobar si es bisiesto en el caso de que se haya seleccionado Febrero.
  if (2 == Mes)
    return ((0 == Ano % 4) && (0 != (Ano % 100))) || (0 == Ano % 400) ? 29 : 28;
  else
    return DaysInMonth[Mes-1];
}

/* Funcion para presentar un mensaje en la pantalla */

function MenssageData(campo)
{
  alert("Data inválida. \nEx: dd/mm/aaaa");
  
  campo.value = "";
  campo.focus();  
}
