// JavaScript Document
var aguardando = false;
window.onload = function()
{
	Start();
}
function criarPopup()
{
	var novaCamada = document.createElement('div');
	novaCamada.className = 'sugestoes';
	novaCamada.setAttribute('id','divPopup');
	
	var tabela = document.createElement('table');
	tabela.setAttribute('id','tabela');
	tabela.setAttribute('bgcolor','#E8FFFF');
	tabela.setAttribute('border','0');
	tabela.setAttribute('cellspacing','0');
	tabela.setAttribute('cellpadding','0');	
	novaCamada.appendChild(tabela);
	document.body.appendChild(novaCamada);
	
}
function Sugestoes()
{
	var palavras = document.getElementById('palavras');
	if (palavras.value.length > 0)
	{
		if (!aguardando)
		{
			var url = "sugestoes.php?palavras="+encodeURIComponent(palavras.value);
			requisicaoHTTP("GET",url,true);
			
		}
	}
	else
		LimparSugestoes();
}
function MostrarSugestoes(dados)
{
	LimparSugestoes();
	var linha, celula, texto;
	var tabela = document.getElementById('tabela');
	var num = dados.getElementsByTagName('nome').length;
	if (num>0)
	{
		PosicionarCamada();
		for (var i=0; i < num; i++)
		{
			var sugestao = dados.getElementsByTagName('nome')[i].firstChild.data;
			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.innerHTML = sugestao;
		}
	}
	
}

function PreencherCaixa(valor)
{
	var caixaTexto = document.getElementById('palavras');
	caixaTexto.value = valor.firstChild.nodeValue;
	LimparSugestoes();
}

function LimparSugestoes()
{
	var camada = document.getElementById('divPopup');
	camada.style.border = '0';
	camada.style.width = '0';
	var tabela = document.getElementById('tabela');
	while (tabela.hasChildNodes())
	{
		tabela.removeChild(tabela.childNodes[0]);
	}
}

function PosicionarCamada()
{
	var camada = document.getElementById('divPopup');
	var tabela = document.getElementById('tabela');
	var caixaTexto = document.getElementById('palavras');
	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';
	camada.style.width = largura  + 'px';
	tabela.style.width = largura - 20 + 'px';
	
}

function CalcularPos(campo, atributo)
{
	var posicao = 0;
	while (campo)
	{
		posicao += campo[atributo];
		campo = campo.offsetParent;
	}
	return posicao;
}

/*function Aviso(exibir)
{
	aguardando = exibir;
	var aviso = document.getElementById('aviso');
	if (exibir)
		aviso.style.visibility = 'visible';
	else
		aviso.style.visibility = 'hidden';
}*/
function trataDados()
{
	var resposta = ajax.responseXML;
	//Aviso(false);
	MostrarSugestoes(resposta);
	
}
function Sugestoes2()
{
	var palavras = document.getElementById('palavras');
	if (palavras.value.length > 0)
	{
		if (!aguardando)
		{
			var url = "sugestoes2.php?palavras="+encodeURIComponent(palavras.value);
			requisicaoHTTP("GET",url,true);
			//Aviso(true)
		}
	}
	else
		LimparSugestoes();
}
///////////////////////////////////
/*function criarPopup2()
{
	var novaCamada = document.createElement('div');
	novaCamada.className = 'sugestoes';
	novaCamada.setAttribute('id','divPopup');
	
	var tabela = document.createElement('table');
	tabela.setAttribute('id','tabela');
	tabela.setAttribute('bgcolor','#E8FFFF');
	tabela.setAttribute('border','0');
	tabela.setAttribute('cellspacing','0');
	tabela.setAttribute('cellpadding','0');	
	novaCamada.appendChild(tabela);
	document.body.appendChild(novaCamada);
	
}
function Sugestoes2()
{
	var palavras2 = document.getElementById('palavras2');
	if (palavras2.value.length > 0)
	{
		if (!aguardando)
		{
			var url = "sugestoes2.php?palavras="+encodeURIComponent(palavras2.value);
			requisicaoHTTP2("GET",url,true);
			//Aviso(true)
		}
	}
	else
		LimparSugestoes2();
}
function MostrarSugestoes2(dados)
{
	LimparSugestoes2();
	var linha, celula, texto;
	var tabela = document.getElementById('tabela');
	var num = dados.getElementsByTagName('nome').length;
	if (num>0)
	{
		PosicionarCamada2();
		for (var i=0; i < num; i++)
		{
			var sugestao = dados.getElementsByTagName('nome')[i].firstChild.data;
			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.innerHTML = sugestao;
		}
	}
	
}

function PreencherCaixa2(valor)
{
	var caixaTexto = document.getElementById('palavras2');
	caixaTexto.value = valor.firstChild.nodeValue;
	LimparSugestoe2();
}

function LimparSugestoes2()
{
	var camada = document.getElementById('divPopup');
	camada.style.border = '0';
	camada.style.width = '0';
	var tabela = document.getElementById('tabela');
	while (tabela.hasChildNodes())
	{
		tabela.removeChild(tabela.childNodes[0]);
	}
}

function PosicionarCamada2()
{
	var camada = document.getElementById('divPopup');
	var tabela = document.getElementById('tabela');
	var caixaTexto = document.getElementById('palavras2');
	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';
	camada.style.width = largura  + 'px';
	tabela.style.width = largura - 20 + 'px';
	
}

function CalcularPos2(campo, atributo)
{
	var posicao = 0;
	while (campo)
	{
		posicao += campo[atributo];
		campo = campo.offsetParent;
	}
	return posicao;
}

/*function Aviso(exibir)
{
	aguardando = exibir;
	var aviso = document.getElementById('aviso');
	if (exibir)
		aviso.style.visibility = 'visible';
	else
		aviso.style.visibility = 'hidden';
}
function trataDados2()
{
	var resposta = ajax.responseXML;
	//Aviso(false);
	MostrarSugestoes2(resposta);
	
}*/