// ARQUIVO DE SCRIPT -- VANDERLEI 13-04-2009

var divConteudo = "conteudo_pagina";
var menuSelected, conteudoMenu; 
var negocio = "";
var imovel = "";
var AcaoNews = "inclui";
var flag_pos = false;


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;
}
//----------------------------------------------


// inseri a pagina principal do site -------------
function getPrincipal(){
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
      			}
    	}
	
	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 Pagina(pag){
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
      			}
    	}
	
	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='images/loader.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='images/loader.gif' border='0'/>"
				}
    	}
	
	url = "buscaLancamento.asp"+querystring;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);			
}

function getObras(querystring){
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
      			}
			else
				{
					document.getElementById(divConteudo).innerHTML = "<img src='images/loader.gif' border='0'/>"
				}
    	}
	
	url = "buscaObra.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;
      			}
    	}
	
	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;
      			}
    	}
	
	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);
	solucao = "ambos"
	
	referencia = r.value;
	
	for(var i=1;i<r.value.length;i++)
	{
		if (r.value.charAt(i) == "-")
		{
			if (r.value.charAt(i+1)=="0")
			{
				referencia = r.value.substr(0,i);
				solucao = "so_banco1";
			}
			else
			{
				referencia = r.value.substr(0,i);
				solucao = "so_banco2";
			}
		}
	}
	
	if (r.value == ""){
		alert("Informe a referencia do imovel!");
		r.focus();
	}
	else{
		getImoveis("?ref="+referencia+"&solucao="+solucao);	
	}
}

//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;
	}
	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{
						xmlHttp=GetXmlHttpObject();
						xmlHttp.onreadystatechange=function()
							{
								if(xmlHttp.readyState==4)
									{
										alert("Mensagem enviada com sucesso!");
										document.form.submit();
										//document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
										document.getElementById(divConteudo).innerHTML = getPrincipal();
									}
							}
							
						with(form){						
							dados = "v_nome=" + cCaracEsp(v_nome.value) + "&v_email="+ cCaracEsp(v_email.value) + "&ddd="+ cCaracEsp(v_ddd.value) + "&v_telelefone=";
							dados += + cCaracEsp(v_telefone.value)+"&v_cidade="+ cCaracEsp(v_cidade.value) +"&v_uf="+ cCaracEsp(v_uf.value) +"&v_pais=";
							dados += cCaracEsp(v_pais.value) + "&v_ppc="+ cCaracEsp(v_ppc.value) +"&v_mhpc="+cCaracEsp(v_mhpc_1.value) + "&v_mensagem=";
							dados += cCaracEsp(v_mensagem.value);						
						}
					
								
						xmlHttp.open("POST","contato_enviar.asp",true);
						xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	// Setando Content-type
						xmlHttp.setRequestHeader("Content-length", dados.length); // Comprimento do conteúdo=comprimento dos dados a enviar
						xmlHttp.send(dados);	
				}		
			}
		}	
	}   
}

//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(m, cM){
	var menu = document.getElementById(m);
	cMenu = document.getElementById(cM);
	
	if (menuSelected != undefined){
		menuSelected.style.height = "18px";
		conteudoMenu.style.visibility = "hidden";
	}
	
	cMenu.style.visibility = "visible";
	menu.style.height = "85px";
	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 solicita_cadastro(querystring){
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
					if (document.getElementById(divConteudo).innerHTML == 'Foi' )
					{
						alert('Solicitação enviada! Aguarde nosso contato.');
						getPagina('login_tabela');
					}
      			}
    	}
	
	url = "solicita_cadastro.asp"+querystring;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function getEscolha(){
	var id_edif = document.getElementById("combolancamento").value;
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
      			}
    	}
	
	if (opcao == "revenda")
	{
		imprimir("admin/tabela_revenda.asp");
	}
	else
	{
		if (opcao == "venda")
			if (id_edif != 0)
				imprimir("admin/venda_print.asp?idEdificio="+id_edif);
			else
				alert('Selecione um edifício!');
		else
			alert('Selecione um tipo de tabela!');
	}
	

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);			
}

function lembra_senha(){
	var area = document.getElementById('esq_senha');
	area.style.display = 'block';
}


function validaForm_login(){
		
	    var email = document.getElementById('email_cadastro');
		var senha = document.getElementById('senha_cadastro');
		if ((email != undefined) && ( senha != undefined))
			{
				if(email.value == ""){alert("Por favor, digite o email cadastrado.");  return false;}
				if(senha.value == ""){alert("Por favor, digite sua senha.");  return false;}
			}
		return true;
	}
	
	function validaForm_senha(){
		
		var senha = domcument.getElementById('senha_email');
		if(senha.value == ""){alert("Digite seu e-mail de cadastro, para que sua senha seja enviada para ele."); 
		return false;}
		return true;
	}
	
	function validaForm_cadastro(){
		
		var creci = document.getElementById('imobiliaria_creci');
		var nome = document.getElementById('imobiliaria_nome');
		var email = document.getElementById('imobiliaria_email');
		if(creci.value == ""){alert("Por favor, digite seu Creci.");  return false;}
		if(nome.value == ""){alert("Por favor, digite o nome da imobiliaria.");  return false;}
		if(email.value == ""){alert("Por favor, digite o e-mail para o qual será enviada a senha.");  return false;}
		return true;
	}

function pega_senha(querystring){
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
      			{
      				document.getElementById(divConteudo).innerHTML = xmlHttp.responseText;
					if (document.getElementById(divConteudo).innerHTML == 'Foi' )
					{
						alert('Um Email foi enviado contendo sua senha de acesso!');
						getPagina('login_tabela');
					}
					else
					{
						alert('O Email informado não está cadastrado!');
						getPagina('login_tabela');
					}
      			}
    	}
	
	url = "pega_senha.asp"+querystring;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function ImovelInserir(idImovel){
	var imovel = new DetalhesImovel();
  	imovel.Inserir('conteudo_pagina',idImovel);
	imovel.Chat('chat.estrelaimoveispraia.bancodedados.imb.br/chat/livezilla.php');
	imovel.DominioArquivos('www.apartamentopraiagrande.imb.br');
}

function recarrega(){
	window.location.reload();
}

function Redirecionar(){
	if(location.href.indexOf("[") == -1)
		Pagina('home');
}

var intervalo = window.setInterval(verificarCarregamento, 2500);		
function verificarCarregamento(){
	if(document.readyState == "complete"){
		Redirecionar();
		clearInterval(intervalo);
	}
}