// Basic Functions

//----------------------------------------------------------------------------------------------------------
function validadorBusca() {
	campoText=false;
	palavraBusca = document.getElementById("busca").value;
	if(palavraBusca.length > 0) {
		campoText=true;
	}

	campoUfCidade=false;
	if(document.getElementById("uf")) {
		if(document.getElementById("uf").value != "") {
			campoUfCidade=true;
		}
	}
	
	if(!campoText && !campoUfCidade && !habilitaSubmit()) {
		alert("Informe a palavra-chave ou selecione um ou mais Ramos de Atividade ou escolha um UF/Cidade no botão Pesquisa Avançada!!");
		return false;
	} else {
		return true;
	}
}

//----------------------------------------------------------------------------------------------------------
function formBuscaAvancada(paramOrAtv,uf,cidade,pessoa){
	http.open("GET", "include/ajax_form_busca.php?nothing=false&uf=" + uf + "&cidade=" + cidade + "&pessoa=" + pessoa + "&" + paramOrAtv, true);
	http.onreadystatechange =  handleHttpResponse_formBuscaAvancada;
	http.send(null);
}

function handleHttpResponse_formBuscaAvancada()
{
  if (http.readyState == 4) {
	fechaLayerWaiting();
    results = http.responseText;
	tamanho=results.length;
	if(results != "ERRO") {
		document.getElementById('formbuscaAvancada').innerHTML = results.substring(0,tamanho-4);
		document.getElementById('qt_itens').value = results.substring(tamanho-3);
	}
  } else {
	  posicionaLayerWaiting();
  }
}


//----------------------------------------------------------------------------------------------------------
function desviaBuscaAvancada(Uf,Cidade,Telefone,TelefoneAntigo,Pessoa,somenteWWW,somenteEmail,somenteTelefone,andOr,fraseExata,bairro){
	campoBusca = document.getElementById("busca").value;
	campoLogin = document.getElementById("login").value;
	document.location = "pesquisa_avancada.php?login=" + campoLogin + "&busca=" + campoBusca + "&uf=" + Uf  + "&cidade=" + Cidade  + "&telefone=" + Telefone  + "&telefone_antigo=" + TelefoneAntigo + "&pessoa=" + Pessoa + "&somente_www=" + somenteWWW + "&somente_email=" + somenteEmail + "&somente_telefone=" + somenteTelefone + "&and_or=" + andOr + "&frase_exata=" + fraseExata + "&bairro=" + bairro;
}

//----------------------------------------------------------------------------------------------------------
CampoX='';
IndiceX=0;
function exibeBusca(IdEmpresa,Chave,Campo,Indice,Busca){
	taVazio=document.getElementById(Campo).innerHTML=="&nbsp;"
	escondeBusca();
	if(taVazio) {
		CampoX=Campo;
		IndiceX=Indice;
		http.open("GET", "include/ajax_pesquisa_detalhe.php?login="+Login+"&id_empresa=" + IdEmpresa + "&chave=" + Chave + "&busca=" + Busca, true);
		http.onreadystatechange =  handleHttpResponse_exibeBusca;
		http.send(null);
	}
	return true;
}

function handleHttpResponse_exibeBusca()
{
  if (http.readyState == 4) {
    results = http.responseText;
	fechaLayerWaiting();
	document.getElementById(CampoX).innerHTML = results;
	document.getElementById('tabelaRegistro_'+IndiceX).style.border = "solid 2px #01CC00";
	document.getElementById('button_'+IndiceX).innerHTML = "<img src='img/seta_fecha.gif' border='0' align='right'>";
  } else {
	  posicionaLayerWaiting();
  }
}

function escondeBusca(){
	for(i=0; i<numLinhas; i++) {
		document.getElementById('reg_'+i).innerHTML = "&nbsp;";
		document.getElementById('button_'+i).innerHTML = "<img src='img/seta_abre.gif' border='0' align='right'>";
		document.getElementById('tabelaRegistro_'+i).style.border = "solid 2px #000095";
	}
}

function formEmail(Email,Empresa) {
	// Escurece a layer principal
	document.getElementById('layerVeu').style.visibility= "visible";
	// Limpa os campos
	document.getElementById('email_empresa').value=Email;
	document.getElementById('campo_nome').value='';
	document.getElementById('campo_email').value='';
	document.getElementById('campo_telefone').value='';
	document.getElementById('campo_texto').value='';
	document.getElementById('layerEmailNomeEmpresa').innerHTML=Empresa;

	var arrayPageSize = getPageSize();
	document.getElementById('layerVeu').style.height=arrayPageSize[1]+"px";
	posicionaLayerEmail();
	document.getElementById('layerEmail').style.visibility='visible';
	return true;	
}

function posicionaLayerEmail() {
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var layerWidth = eval(document.getElementById('layerEmail').style.width.substr(0,document.getElementById('layerEmail').style.width.length-2));
	var layerHeight = eval(document.getElementById('layerEmail').style.height.substr(0,document.getElementById('layerEmail').style.height.length-2));
	posicaoX = (document.body.scrollWidth / 2) - (layerWidth / 2); 
	posicaoY = ((arrayPageSize[3]/2)+arrayPageScroll[1]) - (layerHeight / 2); 
	document.getElementById('layerEmail').style.left = posicaoX + "px";
	document.getElementById('layerEmail').style.top = posicaoY  + "px";
}

function scrollFlags() {
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
			
	msg="Height: "+document.body.scrollHeight;
	msg+="\nWidth: "+document.body.scrollWidth;
	msg+="\nLeft: "+document.body.scrollLeft;
	msg+="\nTop: "+document.body.scrollTop;

	for(i=0; i<arrayPageSize.length; i++) {
		msg+="\narrayPageSize: "+arrayPageSize[i];
	}
	for(i=0; i<arrayPageScroll.length; i++) {
		msg+="\narrayPageScroll: "+arrayPageScroll[i];
	}
	msg+="\nLayerVeu Size: "+document.getElementById('layerVeu').style.height;
	alert(msg);
	return true;
}
		
//----------------------------------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}



