// ARQUIVO DE SCRIPT -- VANDERLEI 13-04-2009

var divConteudo = "conteudo_pagina";

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;
}
//----------------------------------------------

// funcao para mostrar o submenu do site, recebe como parametro a posicao vertical do submenu e o tipo = qual eh o item do submenu selecionado
function showSubMenu(pos,tipo){
	tipoImovel = tipo;
	var menuEscolhido = document.getElementById("submenu");
	menuEscolhido.style.top = pos+"px";
	setVisibility("visible");
}

//tira e coloca a visibilidade do submenu
function setVisibility(op){
	var menuEscolhido = document.getElementById("submenu");	
	menuEscolhido.style.visibility = op; 
}

// 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);		
}

// 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;
      			}
    	}
	
	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;
      			}
    	}
	
	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 getBuscaImovel(form){
	with (form){
		var str = '?tipo='+tipo.value+'&dorm='+dorm.value+'&cidade='+cidade.value+'&bairro='+bairro.value+'&precoate='+precoate.value+"&negocio="+tipoNegocio.value+"&finalidade="+finalidade.value;
		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_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;	
}


window.setTimeout(function () {
			getPrincipal()	
}, 100)	