
strErrorArray = new Array();

strErrorArray["1~1"] = "Por favor, asegúrese de que el campo de ~ se ha completado correctamente.";
strErrorArray["2~1"] = "Por favor, revise dirección de correo electrónico.";
strErrorArray["3~1"] = "Año de nacimiento inválido.";
strErrorArray["4~1"] = "Cliente debe ser mayor de ~ años para poder rentar vehículo.";
strErrorArray["5~1"] = "Error en rango de fecha de renta. Fecha inicial debe ser menor que fecha final"
strErrorArray["6~1"] = "Error en rango de fecha de renta. Fecha de renta debe ser mayor que fecha actual.";
strErrorArray["7~1"] = "Error en rango de fecha de renta. Cliente debe ser mayor de ~ años para poder rentar vehículo.";

strErrorArray["1~2"] = "Please, make sure ~ field has been correctly filled in.";
strErrorArray["2~2"] = "Please check your email.";
strErrorArray["3~2"] = "Invalid birth year.";
strErrorArray["4~2"] = "Customer must be ~ years old or older in orde to rent a vehicle.";
strErrorArray["5~2"] = "Error in rental date range. Initial date must be less than final date."
strErrorArray["6~2"] = "Error in rental date range. Rental date must equal or exceed current date.";
strErrorArray["7~2"] = "Error in rental date range. Customer must be ~ years old or older in orde to rent a vehicle.";


function ErrorMsg(intErrorNumber, intLanguage, strParam)
{
  var strErrorStr, iResult;
  
  strErrorStr = strErrorArray[String(intErrorNumber) + "~" + String(intLanguage)];
  
  if (strErrorStr.indexOf("~") >= 0) 
    {
	strErrorStr = strErrorStr.replace("~", strParam);
	}
  
  alert(strErrorStr);
}


function MayoriaEdad(strDia, strMes, strAno, intMayoria)
  {
  var intDia, intMes, intAno, datFecha;
  
  intDia = parseInt(strDia);
  intMes = parseInt(strMes) - 1;
  intAno = parseInt(strAno) + intMayoria;
  
  datFecha = new Date(intAno, intMes, intDia);
  
  return datFecha;
  }

function StringToDate(strDia, strMes, strAno)
  {
  var datFecha;
  datFecha = new Date(parseInt(strAno), parseInt(strMes) - 1, parseInt(strDia));
  return datFecha;
  }


function CheckPositiveNumber(sNumero)
  {
  var iNumero;
  iNumero = parseInt(sNumero);

  if (isNaN(iNumero))
    return -1;

  if (iNumero < 0)
    return -2;
  else
    return iNumero;
  }


function EmptyFields(which)
  {
  var pass=true;
  
  if (document.images)
    {
    for (i=0;i<which.length;i++)
      {
      var tempobj=which.elements[i];
       
      if (tempobj.id.substring(0,3)=="req") 
        {
        if (((tempobj.type=="text"||tempobj.type=="textarea")&&
            tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
            tempobj.selectedIndex==0)) 
          {
          pass=false;
          break;
          }
        }
      }
    }

  if (!pass)
	{
	ErrorMsg(1, which.Language.value, tempobj.title);
	}

  return (!pass);
  }
  
  
function checkrequired(which) 
  {
  var intEdadMinima, intLanguage;
  var datFecha, datFechai, datFechaf, datFechaMayor;
  
  // Save current language.
  intLanguage = which.Language.value;

  if (EmptyFields(which)) 
    {
//    alert("Por favor, asegúrese de que el campo de "+shortFieldName+" se ha completado correctamente.");
    return false;
    }

  if (which.Email.value != which.Email2.value)
    {
    ErrorMsg(2, intLanguage);
//    alert("Por favor, revise dirección de correo electrónico.");
    return false;
	}

  if (CheckPositiveNumber(which.FechaNacAnio.value) < 0)
    {
    ErrorMsg(3, intLanguage);
//	alert("Año de nacimiento inválido.");
    return false;
	}

  // Edad mínima.
  intEdadMinima = 21;
  // Fecha actual.
  datFecha = new Date();
  // Fecha de mayoría de edad.
  datFechaMayor = MayoriaEdad(which.FechaNacDia.value, which.FechaNacMes.value, which.FechaNacAnio.value, intEdadMinima);

  if (datFechaMayor > datFecha)
    {
    ErrorMsg(4, intLanguage, String(intEdadMinima));
//	alert("Cliente debe ser mayor de "+String(intEdadMinima)+" años para poder rentar vehículo.");
	return false;
	}
	
  datFechai = StringToDate(which.Dia_Solicitud.value, which.Mes_Solicitud.value, which.Ano_Solicitud.value);
  datFechaf = StringToDate(which.Dia_Devolucion.value, which.Mes_Devolucion.value, which.Ano_Devolucion.value);
  
  if (datFechai >= datFechaf)
    {
    ErrorMsg(5, intLanguage);
//	alert("Error en rango de fecha de renta. Fecha inicial debe ser menor que fecha final.");
    return false;
	}

  if ((datFecha >= datFechai) || (datFecha >= datFechaf))
    {
    ErrorMsg(6, intLanguage);
//	alert("Error en rango de fecha de renta. Fecha de renta debe ser mayor que fecha actual.");
    return false;
	}

  if ((datFechaMayor > datFechai) || (datFechaMayor > datFechaf))
    {
    ErrorMsg(7, intLanguage, String(intEdadMinima));
//	alert("Error en rango de fecha de renta. Cliente debe ser mayor de "+String(intEdadMinima)+" años para poder rentar vehículo.");
    return false;
	}
  else
    return true;
  }