var aguardando = false;	// indica se há uma consulta em processamento
var caixa = 1

window.onload = function() {
	var novaCamada;
	novaCamada = document.createElement('div');
	novaCamada.className = 'sugestoes';
	novaCamada.setAttribute('id','divPopup');
	novaCamada.setAttribute('style','font-size: 10px; z-index: 10;');
	

	// cria a tabela que ficará dentro da camada
	var tabela = document.createElement('table');
	tabela.setAttribute('id','tabela');
	//tabela.setAttribute('bgcolor','#FFFFFF');
	tabela.setAttribute('border','0');
	tabela.setAttribute('cellspacing','0');
	tabela.setAttribute('cellpadding','0');
	tabela.setAttribute('style','font-size: 10px; z-index: 10;');
	novaCamada.appendChild(tabela);
	document.body.appendChild(novaCamada);
	Location();
}

// ativa o programa servidor que retorna as sugestões
function Sugestoes(indice) 
{
	var palavras;

		if (indice == 1)
		{
			caixa = 1;
			palavras = document.getElementById('cidade').value;


			if(!aguardando) 
			{
				//Aviso(true);
				var url="includes/localizacidades.asp?value="+encodeURIComponent(palavras);
				requisicaohttp("GET",url,true);;
			}
		}
		else if (indice == 2)
		{
			caixa = 2;

			if (document.getElementById('cidade').value != '' && document.getElementById('cidade').value != 'Local (cidade)')
			{
				palavras = "cidade=" + encodeURIComponent(document.getElementById('cidade').value) + '&value=' + encodeURIComponent(document.getElementById('hotelName').value)
			}
			else
			{
				palavras = "value=" + encodeURIComponent(document.getElementById('hotelName').value)
			}

			if(!aguardando) 
			{
				//Aviso(true);
				var url="includes/localizahoteis.asp?"+palavras;
				requisicaohttp("GET",url,true);
			}
		}
		else 
		{
			LimparSugestoes();
		}
}

// cria a lista de sugestões na tabela HTML
function MostrarSugestoes(dados) {
	LimparSugestoes();
	document.getElementById('divPopup').style.height = '200px';

		if (dados.length > 0)
		{
			var linha, celula, texto;
			var tabela = document.getElementById('tabela');
			var sugestao = eval(dados);
			var num = sugestao.length;
			if(num>0) {

				PosicionarCamada(num);
				for(var i=0; i<num; i++) {
					linha = tabela.insertRow(tabela.rows.length);
					celula = linha.insertCell(0);
					celula.onmouseover = function() {this.className='comCor';};
					celula.onmouseout = function() {this.className='semCor';};
					celula.onclick = function() {PreencherCaixa(this);};
					celula.setAttribute('border','0');
					celula.setAttribute('style','font-size: 12px; padding: 6px 0 6px 0;');
					celula.innerHTML = sugestao[i];
				}
			}

			//Aviso(false);
		}
		else
			LimparSugestoes();
	
}

function PreencherCaixa(valor) {
	var caixaTexto; 

		if (caixa == 1)
		{
			caixaTexto = document.getElementById('cidade'); 
		}
		else if (caixa == 2)
		{
			caixaTexto = document.getElementById('hotelName'); 
		}

	caixaTexto.value = valor.firstChild.nodeValue;
	LimparSugestoes();
}

function LimparSugestoes() {
	var camada =  document.getElementById('divPopup');
	camada.style.border = '0';
	var tabela = document.getElementById('tabela');
	while(tabela.hasChildNodes())
		tabela.removeChild(tabela.childNodes[0]);

	camada.style.height = '0px';
}

// posiciona a camada de sugestões logo abaixo da caixa de texto
function PosicionarCamada(i) {
	
	var camada =  document.getElementById('divPopup');
	var tabela =  document.getElementById('tabela');

		if (i > 8)
		{
			camada.setAttribute('style','height: 200px; overflow-y: auto;');
		}
		else
		{
			//var altura = 32 * i;
			camada.setAttribute('style','overflow-y: auto;');
		}
//alert(altura);
	var caixaTexto; 

		if (caixa == 1)
		{
			caixaTexto = document.getElementById('cidade'); 
		}
		else if (caixa == 2)
		{
			caixaTexto = document.getElementById('hotelName'); 
		}

	var largura = caixaTexto.offsetWidth;
	var esq = CalcularPos(caixaTexto,'offsetLeft');
	var cima = CalcularPos(caixaTexto,'offsetTop') + caixaTexto.offsetHeight;
	
	camada.style.border = '#000000 1px solid';
	camada.style.left = esq + 'px';
	camada.style.top = cima + 'px';
	tabela.style.width = largura + 'px';

		if (i > 8)
		{
			largura = largura + 18;
		}

	camada.style.width = largura + 'px';
}

// função auxiliar para calcular a posição (offset) do campo
function CalcularPos(campo, atributo) {
	var posicao = 0;
	while(campo) {
		posicao += campo[atributo];
		campo = campo.offsetParent;
	}
	return posicao;
}

// exibe ou oculta o aviso "Processando..."
function Aviso(exibir) {
	aguardando = exibir;
	var aviso =  document.getElementById('aviso'); 
	if(exibir)
		aviso.style.display='block';
	else
		aviso.style.display='none';
}

// trata a resposta do servidor
function trataDados(){
	var resposta = ajax.responseText;
	//Aviso(false);
	MostrarSugestoes(resposta);
}

