// Booking Form Validation 
 
function validSubmit(forma){

 if (validateForm(forma)){
 	forma.submit();
 }
}

function validateForm(forma){
    if (forma.nombre.value == '') {
        if (forma.idioma.value == 'es') alert("Debe entrar su Nombre");
		else alert("You must enter your Name");
		forma.nombre.focus();
        return false;
      }
	if (forma.correo.value == '' || !validEmail(forma.correo.value)) {
        if (forma.idioma.value == 'es') alert("El correo no es válido");
		else alert("The email isn't valid");
		forma.correo.focus();
        return false;
      } 
	if (!validDate(forma.desde.value)) {
        if (forma.idioma.value == 'es') alert("La Fecha de inicio de la reservación no es válida");
		else alert("The Start Date of the reservation isn't valid");
		forma.desde.focus();
        return false;
      } 
	if (!validDate(forma.hasta.value)) {
        if (forma.idioma.value == 'es') alert("La Fecha de fin de la reservación no es válida");
		else alert("The End Date of the reservation isn't valid");
		forma.desde.focus();
    return false;
      }   

    return true;
     
}

function validEmail(text)
{
  return ((text.indexOf('@') > 1)&& (text.indexOf('@') < text.indexOf('.')));
}

function validDate(text, lang){
  ele = text.split('/');
  if (ele.length != 3) return ele.length;
  //in spanish
  day = ele[0];
  month = ele[1];
  year = ele[2];
  if (lang == 'en'){ //in english
    month = ele[0];
  	day = ele[1];
  }
  if ((day.length > 2)|| (month.length > 2) || (year.length > 4) ) 
  	return false;
  if ((day.length == 2) && (day.charAt(0) == "0")) 
  	day = parseInt(day.charAt(1));
  if (day < 1 || day > 31) 
  	return false;

  if ((month.length == 2) && (month.charAt(0) == "0")) 
  	month = parseInt(month.charAt(1));
  if (month < 1 || month > 12) 
  	return false;
  
  if (year.length == 2 )
  	year = parseInt("20" + year);
 else if (year.length == 4) 
  	year = parseInt(year);
 else 
 	return false;	
 if (year < 2005 || year > 2020)
 	return false;


  return true;
}

