// ARQUIVO DE SCRIPT DE IMOBILIARIA : 30/06/2010

var divConteudo = "conteudo_pagina";
var menuSelected, conteudoMenu; 
var negocio = "";
var imovel = "";
var AcaoNews = "inclui";

getPagina = onnavigate("pagina", Pagina);
getImoveis = onnavigate("buscaImovel", Imoveis);
getLancamentos = onnavigate("buscaLancamento", Lancamentos);
InserirImovel = onnavigate("detalhesImovel", ImovelInserir);

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;
}
//----------------------------------------------

// recebe a pagina a ser inserida no corpo do site (localizacao/contato)
function Pagina(pag){
	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" />';
    	}
	
	xmlHttp.open("GET",pag + ".asp",true);
	xmlHttp.send(null);
}

//funcao q realiza a consulta aos imoveis -----
function Imoveis(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' border='0'/>"
    	}
	
	url = "busca.asp"+querystring;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);			
}

function Lancamentos(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' border='0'/>"
    	}
	
	url = "buscaLancamento.asp"+querystring;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);			
}

function getNewsletter(email_obj, nome_obj)
{
	var email = document.getElementById(email_obj);
	var nome = document.getElementById(nome_obj);
	if ((email.value != "")&&(nome.value != ""))
	{
	
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
					alert(xmlHttp.responseText);
					email.value = "";
      				nome.value = "";
				}
    	}
	url = "insere_exclui_newsletter.asp?email="+email.value+"&acao="+AcaoNews+"&nome="+nome.value;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	}
	else
	{
		alert('Informe o seu nome e seu email!');
		nome.focus();
	}
}

//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);	
	}
}

//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+'&financiamento='+financiado.checked;
	}
	str += "&imovel="+imovel+"&negocio="+negocio;
	
	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{
						alert("Mensagem enviada com sucesso!");
						document.form.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;
        }
}

function createSubMenu(m, cM){
	var menu = document.getElementById(m);
	cMenu = document.getElementById(cM);
	
	if (menuSelected != undefined){
		menuSelected.style.height = "20px";
		conteudoMenu.style.visibility = "hidden";
	}
	
	cMenu.style.visibility = "visible";
	menu.style.height = "65px";
	menuSelected = menu;
	conteudoMenu = cMenu;
}

function tiraMenu(){
	if (menuSelected != undefined){
		menuSelected.style.height = "20px";
		conteudoMenu.style.visibility = "hidden";
	}		
}

function Redirecionar(){
	if(location.href.indexOf("[") == -1){
		Pagina('home');
	}
}

function ImovelInserir(idImovel){
	var imovel = new DetalhesImovel();
  	imovel.Inserir('conteudo_pagina',idImovel);
	imovel.Chat('http://bancodedados.imb.br/maimoveis/livezilla.php');
	imovel.DominioArquivos('maimoveispg.com.br');
}

var intervalo = window.setInterval(verificarCarregamento, 2500);		
function verificarCarregamento(){
	if(document.readyState == "complete"){
		Redirecionar();
		clearInterval(intervalo);
	}
}
