// ARQUIVO DE SCRIPT -- VANDERLEI 13-04-2009

var divConteudo = "conteudo_pagina";
var menuSelected, conteudoMenu; 
var negocio = "";
var imovel = "";
var AcaoNews = "inclui";

function AbreJanela(url, width, height, nome, scrollbars) {
	var top; var left;
	top = ( (screen.height/2) - (height/1.55) )
	left = ( (screen.width/2) - (width/2) )
	window.open(url, nome,'width='+width+',height='+height+',scrollbars='+scrollbars+',toolbar=no,location=no,status=no,menubar=no,resizable=no,left='+left+',top='+top);
}

// funcao que cria objeto do ajax ----------------------
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
//----------------------------------------------


// inseri a pagina principal do site -------------
function getPrincipal(){
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
      			}
			else
				document.getElementById(divConteudo).innerHTML = '<img src="imagens/load.gif" />';
    	}
	
	url = "home.asp";

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);		
}

function getParceiro(querystring){
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
      			}
    	}
	
	if (querystring == undefined)
	{
		url = "busca_parceiro.asp";
	}
	else
	{
		url = "busca_parceiro.asp"+querystring;
	}

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);		
}

// recebe a pagina a ser inserida no corpo do site (localizacao/contato)
function getPagina(pag){
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
					//document.location = "#"+divConteudo;
      			}
			else
				document.getElementById(divConteudo).innerHTML = '<img src="imagens/load.gif" />';
    	}
	
	xmlHttp.open("GET",pag + ".asp",true);
	xmlHttp.send(null);			
}

//funcao q realiza a consulta aos imoveis -----
function getImoveis(querystring){
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
      			}
			else
				document.getElementById(divConteudo).innerHTML = '<img src="imagens/load.gif" />';
    	}
	
	url = "busca.asp"+querystring;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);			
}

function getImoveisbyDorms(querystring,opcao){
	opcaovalor = document.getElementById(opcao);
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
      			}
			else
				document.getElementById(divConteudo).innerHTML = '<img src="imagens/load.gif" />';
    	}
	
	url = "busca.asp"+querystring+opcaovalor.value;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);			
}

function getIntermediaria(querystring){
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
      			}
			else
				document.getElementById(divConteudo).innerHTML = '<img src="imagens/load.gif" />';
    	}
	
	url = "intermediaria.asp"+querystring;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function getLancamentos(querystring){
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
      			}
			else
				document.getElementById(divConteudo).innerHTML = '<img src="imagens/load.gif" />';
    	}
	
	url = "buscaLancamento.asp"+querystring;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);			
}

function getNewsletter(email_obj)
{
	var email = document.getElementById(email_obj);
	if (email.value != "")
	{
	
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
					alert(xmlHttp.responseText);
					email.value = "";
      			}
    	}
	url = "insere_exclui_newsletter.asp?email="+email.value+"&acao="+AcaoNews;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	}
	else
	{
		alert('Informe o Email!');
		email.focus();
	}
}

function getNoticias(querystring){
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
      			}
			else
				document.getElementById(divConteudo).innerHTML = '<img src="imagens/load.gif" />';
    	}
	
	url = "ver_noticia.asp"+querystring;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);			
}

function getNoticiaPagina(querystring){
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
      			}
			else
				document.getElementById(divConteudo).innerHTML = '<img src="imagens/load.gif" />';
    	}
	
	url = "noticias.asp"+querystring;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);			
}

//busca pela referencia do imovel --------
function buscaRef(ref){
	var r = document.getElementById(ref);
	if (r.value == ""){
		alert("Informe a referencia do imovel!");
		r.focus();
	}
	else{
		getImoveis("?ref="+r.value);
		r.value = "";
	}
}

//pesquisa do menu busca imovel --------------------------

function setNegocio(vl){
	negocio = vl;
}

function setFinalidade(vl){
	imovel = vl;
}

function setAcao(vl){
	AcaoNews = vl;
}

function getBuscaImovel(form){
	with (form){
		var str = '?tipo='+tipo.value+'&dorm='+dorm.value+'&cidade='+cidade.value+'&precoate='+precoate.value+'&bairro='+bairro.value+'&finalidade='+imovel;
	}			
	
	str += "&negocio="+negocio+"&imovel="+imovel;
	getImoveis(str);
}

//pesquisa quando eh selecionado um item do submenu -----
function pesSubMenu(dorms){
	getImoveis('?tipo='+tipoImovel+'&dorm='+dorms);	
}

// envia a mensagem da pagina de contato ---------------
function sendMSG(form){
	with(form){
		if (v_nome.value == ""){
			alert('Informe o nome!');
			v_nome.focus();
		}
		else{
			if (! checkMail(v_email)){
				alert('email incorreto!');
				v_email.focus();
			}
			else{
				if (v_mensagem.value == ""){
					alert('Informe a mensagem!');
					v_mensagem.focus();
				}else{
						submit();	
				}		
			}
		}	
	}   
}

//valida email ------------
function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){ 
                    return true; 
                }
    }else{
        return false;
        }
}

// funcao criada como solução ao problema de passagem de espaços via metodo post e para retirar possiveis caracteres invalidos  para o metodo 
function cCaracEsp(valor){
	if (valor.length > 0){
		for (var i = 1;i <= valor.length; i++){
			valor = valor.replace("&","");
			valor = valor.replace(" ","*space*");
		};	
	}
	return valor;	
}

function createSubMenu(menu, cM){
	cMenu = document.getElementById(cM);
	
	if (menuSelected != undefined){
		menuSelected.style.height = "18px";
		conteudoMenu.style.visibility = "hidden";
	}
	
	cMenu.style.visibility = "visible";
	menu.style.height = "105px";
	menuSelected = menu;
	conteudoMenu = cMenu;
}

function tiraMenu(){
	if (menuSelected != undefined){
		menuSelected.style.height = "18px";
		conteudoMenu.style.visibility = "hidden";
	}		
}

function clearEdt(edit){
	edit.value = "";	
}

function insereLabel(edit){
	edit.value = "Digite a Referência";	
}

function PressRef(e){
	if(EnterPress(e))
		buscaRef('refImovel')
}

function EnterPress(e){
	if (navigator.appName == "Microsoft Internet Explorer" )
		if(event.keyCode==13)
			return true;
		else
			return false;	
	else 
		if(e.charCode == 13)
			return true;
		else
			return false;
}

function selecao(obj){
	obj.style.color = "#4b2813";
}

function tiraSelecao(obj){
	obj.style.color = "#ede0c2";	
}

// FUNCAO PARA BUSCAR BAIRROS DA CIDADE ESCOLHIDA

function getBairros(vl){
	
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById("divBairro").innerHTML = xmlHttp.responseText;
      			}
    	}
	
	url = "lstBairros.asp?id="+vl;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);		
}

window.setTimeout(function () {
			getPrincipal()
}, 100)	