//begin date validation

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}

//end date validation

var reDigits = /^\d+$/;

function doDigits(pStr)
{
	if (reDigits.test(pStr)) 
	{
		return true;
	}
	else if (pStr != null && pStr != "") 
	{
		return false;
	}
	else
	{
		return false;
	}
}

var reDate1 = /^\d{1,2}\/\d{1,2}\/\d{1,4}$/;
var reDate2 = /^[0-3]?\d\/[01]?\d\/(\d{2}|\d{4})$/;
var reDate3 = /^(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(19|20)?\d{2}$/;
var reDate4 = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/;
var reDate5 = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
var reDate = reDate4;

function doDate(pStr, pFmt)
{
	eval("reDate = reDate" + pFmt);
	
	if (reDate.test(pStr)) 
	{
		return true;
	}
	else if (pStr != null && pStr != "") 
	{
		return false;
	}
	else
	{
		return false;	
	}
} // doDate


  var aFinMes = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  function finMes(nMes, nAno){
   return aFinMes[nMes - 1] + (((nMes == 2) && (nAno % 4) == 0)? 1: 0);
  }

   function padNmb(nStr, nLen, sChr){
    var sRes = String(nStr);
    for (var i = 0; i < nLen - String(nStr).length; i++)
     sRes = sChr + sRes;
    return sRes;
   }

   function makeDateFormat(nDay, nMonth, nYear){
    var sRes;
    sRes = padNmb(nDay, 2, "0") + "/" + padNmb(nMonth, 2, "0") + "/" + padNmb(nYear, 4, "0");
    return sRes;
   }

  function incDate(sFec0){
   var nDia = parseInt(sFec0.substr(0, 2), 10);
   var nMes = parseInt(sFec0.substr(3, 2), 10);
   var nAno = parseInt(sFec0.substr(6, 4), 10);
   nDia += 1;
   if (nDia > finMes(nMes, nAno)){
    nDia = 1;
    nMes += 1;
    if (nMes == 13){
     nMes = 1;
     nAno += 1;
    }
   }
   return makeDateFormat(nDia, nMes, nAno);
  }

  function decDate(sFec0){
   var nDia = Number(sFec0.substr(0, 2));
   var nMes = Number(sFec0.substr(3, 2));
   var nAno = Number(sFec0.substr(6, 4));
   nDia -= 1;
   if (nDia == 0){
    nMes -= 1;
    if (nMes == 0){
     nMes = 12;
     nAno -= 1;
    }
    nDia = finMes(nMes, nAno);
   }
   return makeDateFormat(nDia, nMes, nAno);
  }

  function addToDate(sFec0, sInc){
   var nInc = Math.abs(parseInt(sInc));
   var sRes = sFec0;
   if (parseInt(sInc) >= 0)
    for (var i = 0; i < nInc; i++) sRes = incDate(sRes);
   else
    for (var i = 0; i < nInc; i++) sRes = decDate(sRes);
   return sRes;
  }
  
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;

}

function IsNumericBR(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;

}


function validaForm()
{
	//requeridos
	d = document.formReservation;
	
	ListaDatasIndisponiveis = d.txtDatasIndisponiveis.value;
		
	if (d.txtFirst_Name.value == "")
	{
		alert("Field required: 'First name'");
		d.txtFirst_Name.focus();
		return false;
	}
	
	if (d.txtLast_Name.value == "")
	{
		alert("Field required: 'Last name'");
		d.txtLast_Name.focus();
		return false;
	}
	
	
	//if (!(isInteger(d.txtNumber_Pessengers.value)))
	//{
		//alert("Invalid field: Number of pessegers");
		//d.txtNumber_Pessengers.focus();
		//return false;
	//}

	//if (d.txtNumber_Pessengers.value <= 0)
	//{
		//alert("Invalid field: Number of pessegers");
		//d.txtNumber_Pessengers.focus();
		//return false;
	//}
	
  if (! validaPassageiros())
  {
    return false;
  }
		
	//datas
	str_data = d.txtMesArrival.value + "/" + d.txtDiaArrival.value + "/" + d.txtAnoArrival.value;

	if (!(isDate(str_data)))
	{
		alert("Invalid date: 'Date of arrival in Rio'");
		d.txtMesArrival.focus();
		return false;
	}
	
 	str_data = d.txtMesDeparture.value + "/" + d.txtDiaDeparture.value + "/" + d.txtAnoDeparture.value;
			
	if (!(isDate(str_data)))
	{
		alert("Invalid date: 'Date of departure'");
		d.txtMesDeparture.focus();
		return false;
	}

	str_data = d.txtMesPreferred.value + "/" + d.txtDiaPreferred.value + "/" + d.txtAnoPreferred.value;

	if (!(isDate(str_data)))
	{
		alert("Invalid date: 'Preferred date'");
		d.txtMesPreferred.focus();
		return false;
	}	

    AnoDigitado = 0;
    MesDigitado = 0;
    DiaDigitado = 0;
	
    AnoDigitado = d.txtAnoPreferred.value;
    MesDigitado = d.txtMesPreferred.value;
    DiaDigitado = d.txtDiaPreferred.value;

    dataHoje = new Date();
	dataVoo = new Date(AnoDigitado, MesDigitado, DiaDigitado);

    Dia = padNmb(dataHoje.getDate(), 2, "0" );
    Mes = padNmb(dataHoje.getMonth() + 1, 2, "0");
    Ano = padNmb(dataHoje.getFullYear(), 4, "0");

    //48Hrs
	
    strDataHoje = Dia + "/" + Mes + "/" + Ano;
	strDataHojeMaisDoisDias = addToDate(strDataHoje, 2);
	
	//Indisponiveis
	if (ListaDatasIndisponiveis.length > 0)
	{
		ListaDatasIndisponiveis = ";" + ListaDatasIndisponiveis + ";";

	    DiaI = padNmb(DiaDigitado, 2, "0");
    	MesI = padNmb(MesDigitado, 2, "0");
	    AnoI = padNmb(AnoDigitado, 4, "0");	
		strDataReserva = DiaI + "/" + MesI + "/" + AnoI;
		
		if (ListaDatasIndisponiveis.indexOf(";" + strDataReserva + ";") >= 0)
		{
			alert("Unavailable date for online reservation service, for this date please make contact by phone or Email");
			return false;
		}
	}	
	
    nDia = 0;
    nMes = 0;
    nAno = 0;	

    nDia = strDataHojeMaisDoisDias.substr(0, 2);
    nMes = strDataHojeMaisDoisDias.substr(3, 2);
    nAno = strDataHojeMaisDoisDias.substr(6, 4);

    oDataHojeMaisDoisDias = new Date(nAno, nMes, nDia);

    if (dataVoo <= oDataHojeMaisDoisDias)
    {
		alert("Reservations must be made not later than 48 hours before desired flight day, for last minute reservations please call 9843-9006");
		d.txtMesPreferred.focus();
		return false;
    }

    //365 dias
    strDataHojeMaisDoisDias = addToDate(Dia + "/" + Mes + "/" + Ano, 365);

	nDia = strDataHojeMaisDoisDias.substr(0, 2);
    nMes = strDataHojeMaisDoisDias.substr(3, 2);
    nAno = strDataHojeMaisDoisDias.substr(6, 4);

    oDataHojeMaisDoisDias = new Date(nAno, nMes, nDia);

    if (dataVoo >= oDataHojeMaisDoisDias)
    {
		alert("Our online service only accepts reservations up to one year from today");
		d.txtMesPreferred.focus();
		return false;
    }

	if (d.txtHotel.value == "")
	{
		alert("Field required: 'Hotel name'");
		d.txtHotel.focus();
		return false;
	}				
			
	//email
	parte1 = d.txtEMail.value.indexOf("@");
	parte2 = d.txtEMail.value.indexOf(".");
	parte3 = d.txtEMail.value.length;
 if (!(parte1 >= 1 && parte2 >= 1 && parte3 >= 1))
	{
		alert("Invalid value: 'Email adress'");
		d.txtEMail.focus();
		return false;
	}

	//checks
	if (!d.radRead_EMail[0].checked && !d.radRead_EMail[1].checked) 
	{
		alert("Required field: 'Will you be reading Email while in Rio?'");
		return false;
	}
	
	if (!d.radMode_Payment[0].checked && !d.radMode_Payment[1].checked) 
	{
		alert("Required field: 'Mode of payment'");
		return false;
	}	
	
    return true;
}


function validaFormBR()
{
	//requeridos
	d = document.formReservation;
	
	ListaDatasIndisponiveis = d.txtDatasIndisponiveis.value;
		
	if (d.txtFirst_Name.value == "")
	{
		alert("Campo obrigatório: 'Nome'");
		d.txtFirst_Name.focus();
		return false;
	}
	
	if (d.txtHoraPreferred.text == "")
	{
		alert("Campo obrigatório: 'Hora'");
		d.txtHoraPreferred.focus();
		return false;
    }
	
	//if (!(isInteger(d.txtNumber_Pessengers.value)))
	//{
		//alert("Campo inválido: Número de passageiros");
		//d.txtNumber_Pessengers.focus();
		//return false;
	//}

	//if (d.txtNumber_Pessengers.value <= 0)
	//{
		//alert("Campo inválido: Número de passageiros");
		//d.txtNumber_Pessengers.focus();
		//return false;
	//}
	
  if (! validaPassageiros())
  {
    return false;
  }
		
	//datas
	str_data =  d.txtDiaArrival.value + "/" + d.txtMesArrival.value + "/" + d.txtAnoArrival.value;

	if (!(isDate(str_data)))
	{
		alert("Data inválida: 'Data de chegada no Rio'");
		d.txtMesArrival.focus();
		return false;
	}
	
 	str_data = d.txtDiaDeparture.value + "/" + d.txtMesDeparture.value + "/" + d.txtAnoDeparture.value;
			
	if (!(isDate(str_data)))
	{
		alert("Data inválida: 'Data de partida'");
		d.txtMesDeparture.focus();
		return false;
	}

	str_data = d.txtDiaPreferred.value + "/" + d.txtMesPreferred.value + "/" + d.txtAnoPreferred.value;

	if (!(isDate(str_data)))
	{
		alert("Data inválida: 'data preferencial'");
		d.txtMesPreferred.focus();
		return false;
	}	

    AnoDigitado = 0;
    MesDigitado = 0;
    DiaDigitado = 0;
	
    AnoDigitado = d.txtAnoPreferred.value;
    DiaDigitado = d.txtMesPreferred.value;
    MesDigitado = d.txtDiaPreferred.value;

    dataHoje = new Date();
	dataVoo = new Date(AnoDigitado, MesDigitado, DiaDigitado);

    Dia = padNmb(dataHoje.getDate(), 2, "0" );
    Mes = padNmb(dataHoje.getMonth() + 1, 2, "0");
    Ano = padNmb(dataHoje.getFullYear(), 4, "0");

    //48Hrs
	
    strDataHoje = Dia + "/" + Mes + "/" + Ano;
	strDataHojeMaisDoisDias = addToDate(strDataHoje, 2);
	
	//Indisponiveis
	if (ListaDatasIndisponiveis.length > 0)
	{
		ListaDatasIndisponiveis = ";" + ListaDatasIndisponiveis + ";";

	    DiaI = padNmb(DiaDigitado, 2, "0");
    	MesI = padNmb(MesDigitado, 2, "0");
	    AnoI = padNmb(AnoDigitado, 4, "0");	
		strDataReserva = DiaI + "/" + MesI + "/" + AnoI;
		
		if (ListaDatasIndisponiveis.indexOf(";" + strDataReserva + ";") >= 0)
		{
			alert("Data Indisponível para serviço de reservas online, para esta data, por favor faça contato por telefone ou e-mail");
			return false;
		}
	}	
	
    nDia = 0;
    nMes = 0;
    nAno = 0;	

    nDia = strDataHojeMaisDoisDias.substr(0, 2);
    nMes = strDataHojeMaisDoisDias.substr(3, 2);
    nAno = strDataHojeMaisDoisDias.substr(6, 4);

    oDataHojeMaisDoisDias = new Date(nAno, nMes, nDia);

    if (dataVoo <= oDataHojeMaisDoisDias)
    {
		alert("As reservas devem ser feitas o mais tardar 48 horas antes do dia do vôo desejado. Reservas de última hora, ligar para 9843-9006.");
		d.txtMesPreferred.focus();
		return false;
    }

    //365 dias
    strDataHojeMaisDoisDias = addToDate(Dia + "/" + Mes + "/" + Ano, 365);

	nDia = strDataHojeMaisDoisDias.substr(0, 2);
    nMes = strDataHojeMaisDoisDias.substr(3, 2);
    nAno = strDataHojeMaisDoisDias.substr(6, 4);

    oDataHojeMaisDoisDias = new Date(nAno, nMes, nDia);

    if (dataVoo >= oDataHojeMaisDoisDias)
    {
		alert("Nosso serviço online só aceita reservas até um ano a partir de hoje.");
		d.txtMesPreferred.focus();
		return false;
    }

	if (d.txtHotel.value == "")
	{
		alert("Campo obrigatório: 'Nome do hotel ou local'");
		d.txtHotel.focus();
		return false;
	}				
			
	//email
	parte1 = d.txtEMail.value.indexOf("@");
	parte2 = d.txtEMail.value.indexOf(".");
	parte3 = d.txtEMail.value.length;
	if (!(parte1 >= 1 && parte2 >= 1 && parte3 >= 1))
	{
		alert("Campo inválido: 'E-Mail'");
		d.txtEMail.focus();
		return false;
	}

	//checks
	if (!d.radRead_EMail[0].checked && !d.radRead_EMail[1].checked) 
	{
		alert("Campo obrigatório: 'Leitura de e-mail no Rio'.");
		return false;
	}
	
	if (!d.radMode_Payment[0].checked && !d.radMode_Payment[1].checked) 
	{
		alert("Campo obrigatório: 'Modo de pagamento'");
		return false;
	}	
	
    return true;
}


function validaFormExpress()
{
	//requeridos
	d = document.formReservation;
	
	if (d.txtFirst_Name.value == "")
	{
		alert("Required field: Name.");
		d.txtFirst_Name.focus();
		return false;
	}
	
	//if (!(isInteger(d.txtNumber_Pessengers.value)))
	//{
		//alert("Required field: Number of flights.");
		//d.txtNumber_Pessengers.focus();
		//return false;
	//3}
	
	if ((d.txtGorjeta.value != "") && (!(IsNumeric(d.txtGorjeta.value))))
	{
		alert("Invalid field: Tip.");
		d.txtGorjeta.focus();
		return false;
	}		

	//if (d.txtNumber_Pessengers.value <= 0)
	//{
		//alert("Invalid field: Number of flights.");
		//d.txtNumber_Pessengers.focus();
		//return false;
	//}
	
	if (d.txtHotel.value == "")
	{
		alert("Required field: Address.");
		d.txtHotel.focus();
		return false;
	}				
			
	//email
	parte1 = d.txtEMail.value.indexOf("@");
	parte2 = d.txtEMail.value.indexOf(".");
	parte3 = d.txtEMail.value.length;
	if (!(parte1 >= 1 && parte2 >= 1 && parte3 >= 1))
	{
		alert("Invalid field: E-Mail.");
		d.txtEMail.focus();
		return false;
	}
	
    return true;
}

function validaFormExpressBR()
{
	//requeridos
	d = document.formReservation;
	
	if (d.txtFirst_Name.value == "")
	{
		alert("Campo obrigatório: Nome.");
		d.txtFirst_Name.focus();
		return false;
	}
	
	//if (!(isInteger(d.txtNumber_Pessengers.value)))
	//{
		//alert("Campo obrigatório: Número de vôos.");
		//d.txtNumber_Pessengers.focus();
		//return false;
	//}
	
	if ((d.txtGorjeta.value != "") && (!(IsNumericBR(d.txtGorjeta.value))))
	{
		alert("Campo inválido: Gorjeta.");
		d.txtGorjeta.focus();
		return false;
	}		

	//if (d.txtNumber_Pessengers.value <= 0)
	//{
		//alert("Campo inválido: Número de vôos.");
		//d.txtNumber_Pessengers.focus();
		//return false;
	//}
	
	if (d.txtHotel.value == "")
	{
		alert("Campo obrigatório: Endereço.");
		d.txtHotel.focus();
		return false;
	}				
			
	//email
	parte1 = d.txtEMail.value.indexOf("@");
	parte2 = d.txtEMail.value.indexOf(".");
	parte3 = d.txtEMail.value.length;
 if (!(parte1 >= 1 && parte2 >= 1 && parte3 >= 1))
	{
		alert("Campo inválido: E-Mail.");
		d.txtEMail.focus();
		return false;
	}
	
    return true;
}

function focaDiaArrival()
{
	if (document.formReservation.txtMesArrival.value.length == 2) 
	{ 
		document.formReservation.txtDiaArrival.focus(); 
	}		
}	

function focaAnoArrival()
{
	if (document.formReservation.txtDiaArrival.value.length == 2) 
	{ 
		document.formReservation.txtAnoArrival.focus(); 
	}		
}

function focaDiaDeparture()
{
	if (document.formReservation.txtMesDeparture.value.length == 2) 
	{ 
		document.formReservation.txtDiaDeparture.focus(); 
	}		
}	

function focaAnoDeparture()
{
	if (document.formReservation.txtDiaDeparture.value.length == 2) 
	{ 
		document.formReservation.txtAnoDeparture.focus(); 
	}		
}

function focaDiaPreferred()
{
	if (document.formReservation.txtMesPreferred.value.length == 2) 
	{ 
		document.formReservation.txtDiaPreferred.focus(); 
	}		
}	

function focaAnoPreferred()
{
	if (document.formReservation.txtDiaPreferred.value.length == 2) 
	{ 
		document.formReservation.txtAnoPreferred.focus(); 
	}		
}

function focaHorario()
{
	if (document.formReservation.txtAnoPreferred.value.length == 4)
	{
		document.formReservation.txtHoraPreferred.focus();
	}
}



function focaMinutoPreferred()
{

}

