// Funções Javascript para validação dos dados do Amadeus
var browser;

function achaBrowser() {
	var browserName = navigator.appName;
	browser = new String(browserName);

	erro_txt = new String;
	var erro=0;
	d = new Date();
	datasist= new String(d);

	data=new String();
	diaform=new String(valorCombo(document.theform.D_Day));
	
	if (diaform.substring(0,1).indexOf("0")>=0) diaform=diaform.substring(1,2);

	messist = new String();

	if (datasist.substring(9,10).indexOf(" ")>=0) // Dia da data é um numero ate 9
		data = datasist.substring(8,9);

	if (datasist.substring(9,10).indexOf(" ")<0) { // Dia da data é > que 9
		if (datasist.substring(8,9).indexOf("1")>=0) data = "1"+datasist.substring(9,10);
		if (datasist.substring(8,9).indexOf("2")>=0) data = "2"+datasist.substring(9,10);
		if (datasist.substring(8,9).indexOf("3")>=0) data = "3"+datasist.substring(9,10);
	}

	if (datasist.substring(3,7).indexOf("Jan")>=0) messist="01";
	else if (datasist.substring(3,7).indexOf("Feb")>=0) messist="02";
	else if (datasist.substring(3,7).indexOf("Mar")>=0) messist="03";
	else if (datasist.substring(3,7).indexOf("Apr")>=0) messist="04";
	else if (datasist.substring(3,7).indexOf("May")>=0) messist="05";
	else if (datasist.substring(3,7).indexOf("Jun")>=0) messist="06";
	else if (datasist.substring(3,7).indexOf("Jul")>=0) messist="07";
	else if (datasist.substring(3,7).indexOf("Aug")>=0) messist="08";
	else if (datasist.substring(3,7).indexOf("Sep")>=0) messist="09";
	else if (datasist.substring(3,7).indexOf("Oct")>=0) messist="10";
	else if (datasist.substring(3,7).indexOf("Nov")>=0) messist="11";
	else if (datasist.substring(3,7).indexOf("Dec")>=0) messist="12";
	
	dm=new String(valorCombo(document.theform.D_Month));

	var ano_form = parseInt(dm.substring(2,4));
	var dia_sistema = parseInt(data);		
	var dia_form = parseInt(diaform);
	var mes_sistema = parseInt(messist);
	ano_teste = new String (d.getYear());
	var ano_atual = parseInt(ano_teste.substring(2,4));

	mes_form = dm.substring(4,7)
	
	// Verificacao dos dados do tipo de passageiro (adulto, crianca, crianca de colo)
	var i = 0;
	var Sobra = 9;
	var NAdult = parseInt(valorCombo(document.theform.NumAdultos));
	var NCrianca = parseInt(valorCombo(document.theform.NumCriancas));
	var NBebe = parseInt(valorCombo(document.theform.NumBebes));

	for (i = 1; i <= 4; i++) {
		eval("document.theform.PaxType" + i + ".value = '000'");
		eval("document.theform.PaxType" + i + "_Infant.value = 'N'");
	}
	for (i = 1; i <= NAdult; i++) {
		eval("document.theform.PaxType" + i + ".value = 'ADT'");
	}
	Sobra = (Sobra  - NAdult);
	if (NCrianca > Sobra) {
		alert("O nosso sistema de reservas so atende a solicitacao de no maximo 9 pessoas, acompanhadas por criancas de 0 a 1 ano.");
		erro=3;
	}
	for (i = (NAdult + 1); i <= (NCrianca + NAdult); i++) {
		eval("document.theform.PaxType" + i + ".value = 'CHD'");
	}
	if (NBebe > NAdult) {
		alert("Toda crianca menor de 1 ano deve estar acompanhada de um adulto.");
		erro=3;
	}
	for (i = 1; i <= NBebe; i++) {
		eval("document.theform.PaxType" + i + "_Infant.value = 'Y'");
	}
// FIM DO CHECK PARA CRIANCAS E BEBES
	if (document.theform.D_City.value!="null" && document.theform.D_Month.value!=="null") {  
		if ( mes_form < mes_sistema && ano_atual > ano_form) {
			alert("Data de ida anterior a data atual");
			erro=2;
		} else {
			if ( (mes_form == mes_sistema) && (dia_form < dia_sistema) && (ano_atual > ano_form) ) {
				alert("Data de ida anterior a data atual");
				erro=2;
			}
		}
	}	
	if (document.theform.D_City.value=="" && document.theform.D_City2.value=="") {  
		erro_txt += "Cidade de origem\n";
		erro=1;
	}
	if (document.theform.D_City.value=="" && document.theform.D_City2.value!="") {
		document.theform.D_City.value=document.theform.D_City2.value;
	}
	if (document.theform.A_City.value=="" && document.theform.A_City2.value!="") {
		document.theform.A_City.value=document.theform.A_City2.value;
	}
	if (document.theform.A_City.value=="" && document.theform.A_City2.value=="") {  
		erro_txt += "Cidade de destino\n";
		erro=1;
	}
	if (valorCombo(document.theform.D_Day)== "null") {
		erro_txt += "Dia da saída\n";
		erro=1;
	}
	if (valorCombo(document.theform.D_Month)== "null") {
		erro_txt += "Mês e ano da saída\n";
		erro=1;
	}
	if (valorCombo(document.theform.R_Day)=="null" && valorCombo(document.theform.R_Month)=="null") {
		document.theform.TripType.value = "1";
	}
	if (valorCombo(document.theform.R_Day)=="null" && valorCombo(document.theform.R_Month)!="null") {
		erro_txt += "Dia do retorno\n";
		erro=1;
	}
	if (valorCombo(document.theform.R_Day)!="null" && valorCombo(document.theform.R_Month)=="null") {
		erro_txt += "Mês de retorno\n";
		erro=1;
	}		
	if (valorCombo(document.theform.R_Month) < valorCombo(document.theform.D_Month)) { 
		erro_txt += "Mês e ano do retorno menor que o mês e ano da saída\n";
		erro=1;
	}
	if (valorCombo(document.theform.R_Month) == valorCombo(document.theform.D_Month)) { 
		if (valorCombo(document.theform.R_Day) < valorCombo(document.theform.D_Day)) {
			erro_txt += "Dia do retorno menor que o dia da saída\n";
			erro=1;
		}
	}
	if (erro==1) {
		alert("Favor preencher corretamente os seguintes campos:\n\n"+erro_txt);
	} else {
		if (erro!=2 && erro!=3) {
			if (document.theform.A_City.value.toLowerCase().indexOf("sdu")>=0 && document.theform.D_City.value.toLowerCase().indexOf("cgh")>=0
			||	document.theform.A_City.value.toLowerCase().indexOf("cgh")>=0 && document.theform.D_City.value.toLowerCase().indexOf("sdu")>=0
			||	document.theform.A_City2.value.toLowerCase().indexOf("ongonhas")>=0 && document.theform.D_City2.value.toLowerCase().indexOf("umont")>=0
			||	document.theform.A_City2.value.toLowerCase().indexOf("umont")>=0 && document.theform.D_City2.value.toLowerCase().indexOf("ongonhas")>=0
			||	document.theform.A_City2.value.toLowerCase()=="rio de janeiro"
			&&	(document.theform.D_City2.value.toLowerCase()=="são paulo" 
				|| document.theform.D_City2.value.toLowerCase()=="sao paulo" )
			||	document.theform.D_City2.value.toLowerCase()=="rio de janeiro" 
			&&	(document.theform.A_City2.value.toLowerCase()=="são paulo" 
				|| document.theform.A_City2.value.toLowerCase()=="sao paulo" )
			)
				window.open("outraseponte.htm","outras","toolbar=no,location=no,directories=no,menubar=no,scrollbars=no,status=no,resizable=yes,WIDTH=400,HEIGHT=200, topt=0,left=0");
			else {
				if (erro!=3) {
					window.open("http://res.amadeus.net/pl/casablanca/br/AAGETAVAIL.hot","amadeus","toolbar=yes,location=no,directories=no,menubar=no,scrollbars=yes,status=yes,resizable=no,WIDTH=680,HEIGHT=430, topt=0,left=0");
					return true;
					//document.theform.submit();
				}
			}
		}
	}
	return false;
}

function valorCombo(combo) {
	if (combo.options) {
		return combo.options[combo.selectedIndex].value;
	} else {
		return combo.value;
	}
}

function abreTimeTable() {
	window.open("http://www.casablanca.tur.br/panrotas.asp","time","width=630,height=300,scrollbars,menubar,resizable,left=80,screenX=80,top=110,screenY=110");
}

