var aeroportos = [
[
// '0000000' - o 0 habilia e o 1 desabilita: ordem -> Seg, Ter, Qua, Qui, Sex, Sab, Dom
    'BH - Confins',
99, [
[1, '0000000'], // Brasília
[2, '0000000'], // Curitiba
[3, '0000000'], // Fortaleza
[4, '0000000'], // Natal
[6, '0000000'], // Recife
[7, '0000000'], // RIO - Galeão
[8, '0000000'], // RIO - Santos Dumiont
[9, '0000000'], // Salvador
[10, '0000000'] // SAO - Guarulhos
    ]
    ],

[
    'Brasília',
115, [
[0, '0000000'], // BH - Confins
[2, '0000000'], // Curitiba
[3, '0000000'], // Fortaleza
[4, '0000000'], // Natal
[5, '0000000'], // Porto Alegre
[6, '0000000'], // Recife
[8, '0000000'], // RIO - Santos Dumont
[9, '0000000'], // Salvador
[10, '0000000'] // SAO - Guarulhos
    ]
    ],
    
[
    'Curitiba',
81, [
[0, '0000000'], // BH - Confins
[1, '0000000'], // Brasília
[3, '0000000'], // Fortaleza
[4, '0000000'], // Natal
[5, '0000000'], // Porto Alegre
[6, '0000000'], // Recife
[7, '0000000'], // RIO - Galeão
[8, '0000000'], // RIO - Santos Dumont
[9, '0000000'], // Salvador
[10, '0000000'] // SAO - Guarulhos
    ]
    ],

[
    'Fortaleza',
83, [
[0, '0000000'], // BH - Confins
[1, '0000000'], // Brasília
[2, '0000000'], // Curitiba
[4, '0000000'], // Natal
[5, '0000000'], // Porto Alegre
[7, '0000000'], // RIO - Galeão
[8, '0000000'], // RIO - Santos Dumont
[10, '0000000'] // SAO - Guarulhos
    ]
    ],

[
    'Natal',
90, [
[0, '0000000'], // BH - Confins
[1, '0000000'], // Brasília
[2, '0000000'], // Curitiba
[3, '0000000'], // Fortaleza
[5, '0000000'], // Porto Alegre
[7, '0000000'], // RIO - Galeão
[10, '0000000'] // SAO - Guarulhos
    ]
    ],

[
    'Porto Alegre',
103, [
[1, '0000000'], // Brasília
[2, '0000000'], // Curitiba
[3, '0000000'], // Fortaleza
[4, '0000000'], // Natal
[6, '0000000'], // Recife
[7, '0000000'], // RIO - Galeão
[8, '0000000'], // RIO - Santos Dumont
[9, '0000000'], // Salvador
[10, '0000000'] // SAO - Guarulhos
    ]
    ],

[
    'Recife',
91, [
[0, '0000000'], // BH - Confins
[1, '0000000'], // Brasília
[2, '0000000'], // Curitiba
[5, '0000000'], // Porto Alegre
[7, '0000000'], // RIO - Galeão
[8, '0000000'], // RIO - Santos Dumont
[9, '0000000'], // Salvador
[10, '0000000'] // SAO - Guarulhos
    ]
    ],

[
    'RIO - Galeão',
84, [
[0, '0000000'], // BH - Confins
[2, '0000000'], // Curitiba
[3, '0000000'], // Fortaleza
[4, '0000000'], // Natal
[5, '0000000'], // Porto Alegre
[6, '0000000'], // Recife
[9, '0000000'], // Salvador
[10,'1111010'] // SAO - Guarulhos    
    ]
    ],
[
    'RIO - Santos Dumont',
92, [
[0, '0000000'], // BH - Confins
[1, '0000000'], // Brasília
[2, '0000000'], // Curitiba
[3, '0000000'], // Fortaleza
[5, '0000000'], // Porto Alegre
[6, '0000000'], // Recife
[9, '0000000'], // Salvador
[10, '0000000'] // SAO - Guarulhos
    ]
    ],    

[
    'Salvador',
100, [
[0, '0000000'], // BH - Confins
[1, '0000000'], // Brasília
[2, '0000000'], // Curitiba
[5, '0000000'], // Porto Alegre
[6, '0000000'], // Recife
[7, '0000000'], // RIO - Galeão
[8, '0000000'], // RIO - Santos Dumont
[10, '0000000'] // SAO - Guarulhos
    ]
    ],

[
    'SAO - Guarulhos',
85, [
[0, '0000000'], // BH - Confins
[1, '0000000'], // Brasília
[2, '0000000'], // Curitiba
[3, '0000000'], // Fortaleza
[4, '0000000'], // Natal
[5, '0000000'], // Porto Alegre
[6, '0000000'], // Recife
[7, '1111010'], // RIO - Galeão
[8, '0000000'], // RIO - Santos Dumont
[9, '0000000'] // Salvador
     ]
     ]
];

function checkForm()
{
	var valida = false;
	var myFrm = document.getElementById('FrmConsulta');
	var myOri = document.getElementById('Origem').value;
	var myDes = document.getElementById('Destino').value;
	var myPar = document.getElementById('beginDate').value;
	var myRet = document.getElementById('finalDate').value;
	var campos = [
		[myOri, 'Origem'],
		[myDes, 'Destino'],
		[myPar, 'Data da Partida'],
		[myRet, 'Data do Retorno']
	];
	var myLim = document.getElementById('tipo1').checked ? 0 : 1;
	var erro = '';
	for(i = 0; i < parseInt(campos.length - myLim); i++)
	{
		if(campos[i][0] == '' || campos[i][0] == null)
		{
			erro = erro + 'Campo ' + campos[i][1] + ' não preenchido\n';
		}
	}
	if(campos[2][0] == 'Selecione a data no calendário ao lado' || campos[2][0] == 'Selecione a data no calendário')
	{
		erro = erro + 'Campo ' + campos[2][1] + ' não preenchido\n';
	}
	if(myLim < 1)
	{
		if(campos[3][0] == 'Selecione a data no calendário ao lado' || campos[3][0] == 'Selecione a data no calendário')
		{
			erro = erro + 'Campo ' + campos[3][1] + ' não preenchido\n';
		}
		var datao = myPar.split('/');
		var datano = datao[2] + '' + datao[1] + '' + datao[0];
		var datad = myRet.split('/');
		var datand = datad[2] + '' + datad[1] + '' + datad[0];
		if(parseInt(datand) < parseInt(datano))
		{
			erro = erro + 'Data de partida maior que data de retorno\n';
		}
	}
	
	if(erro != '' && erro != null)
	{
		erro = 'Os seguintes erros foram encotrados:\n\n' + erro;
		alert(erro);		
	}
	else
	{
		if(myLim > 0)
		{
			document.getElementById('finalDate').value = '';
		}
		valida = true;
	}
	return valida;
}

function MostraDiv(state, id)
{
	document.getElementById('lstOrigem').style.display = 'none';
	document.getElementById('lstDestino').style.display = 'none';
	document.getElementById('cidorigem').innerHTML = '';
	document.getElementById('ciddestino').innerHTML = '';

	populaLista(id);

	id = id != 'origem' ? 'Destino' : 'Origem';
	var DivRef = document.getElementById('lst' + id);
	var IfrRef = document.getElementById('DivMestra');
	if(state)
	{
		DivRef.style.display = "block";
		IfrRef.style.width = DivRef.offsetWidth;
		IfrRef.style.height = DivRef.offsetHeight;
		IfrRef.style.top = DivRef.style.top;
		IfrRef.style.left = DivRef.style.left;
		IfrRef.style.marginLeft = DivRef.style.marginLeft;
		IfrRef.style.zIndex = DivRef.style.zIndex - 1;
		IfrRef.style.display = "block";
	}
	else
	{
		DivRef.style.display = "none";
		IfrRef.style.display = "none";
	}
}

function MostraDiv2(state, id)
{
	document.getElementById('boxTips').style.display = 'none';
	var DivRef = document.getElementById('boxTips');
	var IfrRef = document.getElementById('DivMestra');
	if(state)
	{
		DivRef.style.display = "block";
		IfrRef.style.width = DivRef.offsetWidth;
		IfrRef.style.height = DivRef.offsetHeight;
		IfrRef.style.top = DivRef.style.top;
		IfrRef.style.left = DivRef.style.left;
		IfrRef.style.marginLeft = DivRef.style.marginLeft;
		IfrRef.style.zIndex = DivRef.style.zIndex - 1;
		IfrRef.style.display = "block";
	}
	else
	{
		DivRef.style.display = "none";
		IfrRef.style.display = "none";
	}
}

function populaLista(id)
{
	var lista = id;
	var reves = id != 'origem' ? 'Origem' : 'Destino';
	if(document.getElementById(reves).value != '' && document.getElementById(reves).value != null)
	{
		local = document.getElementById(reves).value;
		for(i = 0; i < aeroportos.length; i++)
		{
			if(aeroportos[i][1] == local)
			{
				for(j = 0; j < aeroportos[i][2].length; j++)
				{
					var indice = aeroportos[i][2][j][0];
					var cidade = aeroportos[indice];
					document.getElementById('cid' + lista).innerHTML += '<li><a href="javascript:seleciona(' + indice + ', \'' + lista + '\')">' + cidade[0] + '</a></li>';
				}
				break;
			}
		}
	}
	else
	{
		var text = '';
		for(i = 0; i < aeroportos.length; i++)
		{
			text += '<li><a href="javascript:seleciona(' + i + ', \'' + lista + '\');">' + aeroportos[i][0] + '</a></li>';
		}
		document.getElementById('cid' + lista).innerHTML = text;
	}
}

function seleciona(indice, local)
{
	var cidade = aeroportos[indice][0];
	var campo = local != 'origem' ? 'Destino' : 'Origem';
	document.getElementById('wid' + local).innerHTML = cidade;
	trocaCor2(document.getElementById('wid' + local));
	//document.getElementById(local).value = cidade;
	document.getElementById(campo).value = aeroportos[indice][1];
	document.getElementById('beginDate').value = 'Selecione a data no calendário ao lado';
	document.getElementById('finalDate').value = 'Selecione a data no calendário ao lado';
	document.getElementById('beginDate').style.color = '#B9C3C5';
	document.getElementById('finalDate').style.color = '#B9C3C5';
	trocaData();
	hideMe();
	void(0);
}

function hideMe()
{
	document.getElementById('DivMestra').style.display = 'none';
	document.getElementById('lstOrigem').style.display = 'none';
	document.getElementById('lstDestino').style.display = 'none';
	document.getElementById('cidorigem').innerHTML = '';
	document.getElementById('ciddestino').innerHTML = '';
	void(0);
}

function trocaData()
{
	var origem = document.getElementById('Origem').value;
	var destino = document.getElementById('Destino').value;
	var iOrigem, iDestino, seqData;
	if(origem != '' && origem != null && destino != '' && destino != null)
	{
		for(i = 0; i < aeroportos.length; i++)
		{
			var comp = aeroportos[i][1];
			if(comp == origem)
			{
				iOrigem = i;
				var compd;
				for(j = 0; j < aeroportos[i][2].length; j++)
				{
					compd = aeroportos[i][2][j][0];
					if(aeroportos[compd][1] == destino)
					{
						seqData = aeroportos[i][2][j][1];
						document.getElementById('calData').value = seqData;
						disableDays();
						break;
					}
				}
				break;
			}
		}
	}
}

function disableDays()
{
	var ddata = [0, 0, 0, 0, 0, 0, 0];
	var datas = '' + document.getElementById('calData').value + '';
	for(i = 0; i < datas.length; i++)
	{
		ddata[i] = parseInt(datas.charAt(i));
	}
	var di = datePickerController.datePickers['beginDate'];
	var df = datePickerController.datePickers['finalDate'];
	if(!di || !df)
	{
		setTimeout("disableDays()", 200);
		return;
	}
	di.setDisabledDays(ddata);
	df.setDisabledDays(ddata);
}

function trocaCor(id)
{
	if(id.value != 'Selecione a data no calendário ao lado')
	{
		id.style.color = '#6C9D23';
	}
	else
	{
		id.style.color = '#B9C3C5';
	}
}

function trocaCor2(id)
{
	if(id.value != 'Clique para escolher a origem' || id.value != 'Clique para escolher o destino')
	{
		id.style.color = '#6C9D23';
	}
	else
	{
		id.style.color = '#B9C3C5';
	}
}

function mensagem()
{
	alert('Este serviço será disponibilizado em breve. Aguarde.\n\n');
	void(0);
}