// ARQUIVO DE SCRIPT -- VANDERLEI 13-04-2009

var divConteudo = "conteudo_pagina";
var menuSelected, conteudoMenu; 
var negocio = "";
var TLancamento = "TituloLancamentos";
var finalidade = "1";
var negocio = "2";

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;
					document.getElementById(TLancamento).style.visibility = "visible";
      			}
			else
				document.getElementById(divConteudo).innerHTML = '<img src="imagens/load.gif" />';	
    	}
	
	url = "home.asp";

	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.getElementById(TLancamento).style.visibility = "hidden";
      			}
			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;
				document.getElementById(TLancamento).style.visibility = "hidden";
			}
			else
				document.getElementById(divConteudo).innerHTML = '<img src="imagens/load.gif" />';
    	}
	
	url = "busca.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);	
	}
}

//pesquisa do menu busca imovel --------------------------

function setNegocio(vl){
	negocio = vl;
}

function getNegocio(){
	return negocio;
}

function setFinalidade(vl){
	finalidade = vl;
}

function getFinalidade(){
	return finalidade;
}


function getBuscaImovel(form){
	var vl = document.getElementById('opBuscaLancamento');
			
	with (form){
		var str = '?tipo='+tipo.value+'&dorm='+dorm.value+'&cidade='+cidade.value+'&precoate='+precoate.value+"&bairro="+bairro.value;	
	}
		
	str += "&negocio="+getNegocio()+"&finalidade="+getFinalidade();
	
	if(vl.checked)
		str += "&lancamento="+vl.value; 
	//alert(str)
	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.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_telefone=";
							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)+"&v_cel="+cCaracEsp(v_cel.value)+"&v_nextel="+cCaracEsp(v_nextel.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(menu, cM){
	cMenu = document.getElementById(cM);
	
	if (menuSelected != undefined){
		menuSelected.style.height = "20px";
		conteudoMenu.style.visibility = "hidden";
	}
	
	cMenu.style.visibility = "visible";
	menu.style.height = "95px";
	menuSelected = menu;
	conteudoMenu = cMenu;
}

function tiraMenu(){
	if (menuSelected != undefined){
		menuSelected.style.height = "20px";
		conteudoMenu.style.visibility = "hidden";
	}		
}

function clearEdt(edit){
	edit.value = "";	
}

function insereLabel(edit){
	edit.value = "Digite a Referência";	
}

//newsletter ----------------------

var AcaoNews = "inclui";

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 setAcao(vl){
	AcaoNews = vl;
}

//---------------------------------

function validarEmail(mail){        
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);        
	return er.test(mail);
}

function addFavoritos(){
	title = "Pimentel Imóveis";
	url = "http://pimentelimoveis.com.br"
    if (window.sidebar) 
		window.sidebar.addPanel(title, url,"");
    else 
		if(window.opera && window.print){
        	var mbm = document.createElement('a');
        	mbm.setAttribute('rel','sidebar');
        	mbm.setAttribute('href',url);
        	mbm.setAttribute('title',title);
        	mbm.click();
    	}else 
			if(document.all){window.external.AddFavorite(url, title);}
		else{alert('Seu navegador nao tem suporte para esta acao!')}
}

function PressRef(e){
	if(EnterPress(e))
		buscaRef('Ref')
}

function EnterPress(e){
	if (navigator.appName == "Microsoft Internet Explorer" )
		if(event.keyCode==13)
			return true;
		else
			return false;	
	else 
		if(e.charCode == 0)
			return true;
		else
			return false;				
}

window.setTimeout(function () {
			getPrincipal()	
}, 100)	

