Banner_GV = 1;
Banner_SP = 1;
Total_GV = 6;
Total_SP = 2;

window.onerror = LimpaErro;

function LimpaErro() {
   return true;	
}

function bloquearTeclas(e) {
   var key;
   var keychar;
   var reg;
   if (window.event) {
      key = e.keyCode;
   } else if(e.which) {
      key = e.which;
   } else {
      return true;
   }

   keychar = String.fromCharCode(key);
   reg = /\d/;
   return reg.test(keychar);
}

function ProximoBanner(Local,Proximo) {
   if (Local == "sp") {
      Banner = document.getElementById("banner_sp").innerHTML;	   
      if (Banner_SP == Total_SP) {
  	     Banner_SP = 0;
	  }
	  Banner_SP++;
      document.getElementById("banner_sp").innerHTML = "<img src=\"images/sp.gif\" width=\"159\" height=\"21\" alt=\"\" border=\"0\" /><br /><object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\" width=\"441\" height=\"202\"><param name=\"movie\" value=\"flash/sp/" + Proximo + ".swf\" /><param name=\"quality\" value=\"high\" /><param name=\"wmode\" value=\"transparent\" /><embed src=\"flash/sp/" + Proximo + ".swf\" wmode=\"transparent\" quality=\"high\" pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"441\" height=\"202\"></embed></object>";		
   } else {
      Banner = document.getElementById("banner_gv").innerHTML;	   
      if (Banner_GV == Total_GV) {
  	     Banner_GV = 0;
	  }
	  Banner_GV++;
      document.getElementById("banner_gv").innerHTML = "<img src=\"images/granja.gif\" width=\"441\" height=\"21\" alt=\"\" border=\"0\" /><br /><object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\" width=\"441\" height=\"202\"><param name=\"movie\" value=\"flash/gv/" + Proximo + ".swf\" /><param name=\"quality\" value=\"high\" /><param name=\"wmode\" value=\"transparent\" /><embed src=\"flash/gv/" + Proximo + ".swf\" wmode=\"transparent\" quality=\"high\" pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"441\" height=\"202\"></embed></object>";		   }
   //alert(Banner.substring(Banner.indexOf("src"),10));
}

function Verifica(Objeto,Acao) {
   if ((Objeto.value.length == 0) && (Acao == "blur")) {	
      Objeto.value = "Código:";
   } else if ((Objeto.value.length > 0) && (Acao == "blur")) {	
      //Objeto.value = Objeto.value + "0000000000";
	  //while (Objeto.value.indexOf("-") >= 0) {
      //   Objeto.value = Objeto.value.replace("-","");
	  //}
	  //while (Objeto.value.indexOf(".") >= 0) {
      //   Objeto.value = Objeto.value.replace(".","");		  
	  //}
      //Objeto.value = Objeto.value.substring(0,2) + "-" + Objeto.value.substring(2,5) + "." + Objeto.value.substring(5,8) + "-" + Objeto.value.substring(8,9);
   } else if ((Objeto.value == "Código:") && (Acao == "focus")) {	
      Objeto.value = "";
   }
}

function Buscar(Local) {
   if (Local == "sp") {
      formulario = document.frmSP;   
	  document.frmSP.bairro_sp.name = "bairro_sp[]";
   } else {
      formulario = document.frmGranja;
      if (document.frmGranja.bairro_granja != undefined) {
        document.frmGranja.bairro_granja.name = "bairro_granja[]";
      }
   }
   formulario.submit();
}

function Buscar2() {
   document.frmInterna.submit();
}

function Buscar3() {
   if ((document.frmReferencia.referencia_busca.value.length == 0) || (document.frmReferencia.referencia_busca.value == "Código:")) {	   
	   alert("Digite o Código de Referência!");
	   document.frmReferencia.referencia_busca.focus();
	   return;
   }
   document.frmReferencia.submit();
}

function Buscar4() {
   if (document.frmEncontre.local.selectedIndex == 0) {
	   alert("Escolha o Local!");
	   document.frmEncontre.local.focus();
	   return;
   }
   document.frmEncontre.submit();   
}

function Buscar5() {
   if ((document.frmEncontre2.referencia_busca.value.length == 0) || (document.frmEncontre2.referencia_busca.value == "Código:")) {
	   alert("Digite o Código de Referência!");
	   document.frmEncontre2.referencia_busca.focus();
	   return;
   }
   document.frmEncontre2.submit();
}

function ExibeFotoPrincipal(Camada, Acao, Foto) {
    if (Acao == "over") {
        Camada.style.cursor = "pointer";
    } else {
        Camada.style.cursor = "default";
    }
}

function LinkSaibaMais(URL,Camada,Acao,Foto) {
   	if (Acao == "over") {
   	    Camada.style.cursor = "pointer";
   	    document.getElementById("foto_atual").src = Foto;
	} else if (Acao == "out") {
	    Camada.style.cursor = "default";
	    document.getElementById("foto_atual").src = Foto;
	} else {
	   if (URL.substring(0,19) == "empreendimentos.asp") {
		  location.href = URL;   
	   } else {
	      JanelaLink = window.open(URL);
	      JanelaLink.focus();
	   }
	}
}

function MudaCor(Objeto,Acao) {
   if (Acao == "over") {
      Objeto.className = "busca2_saibamais";   
   } else {
      Objeto.className = "busca_saibamais";   
   }
}

function MudaCor2(Objeto,Acao) {
   if (Acao == "over") {
      Objeto.className = "registro2_lancamento";   
   } else {
      Objeto.className = "registro_lancamento";   
   }
}

function MudaCor3(Objeto,Acao) {
   if (Acao == "over") {
      Objeto.className = "panel2";   
	  if (Objeto.style.borderColor.toUpperCase() == "#DBDCDD") {
		  Objeto.style.borderColor = "#cccccc";
	  }
	  if (Objeto.style.borderColor.toUpperCase() == "RGB(234, 235, 235) RGB(234, 235, 235) RGB(234, 235, 235) RGB(234, 235, 235)") {
		  Objeto.style.borderColor = "#cccccc";
	  }
   } else {
	  if (Objeto.style.borderColor.toUpperCase() == "#CCCCCC") {
		  Objeto.style.borderColor = "#DBDCDD";
	  }	  
	  if (Objeto.style.borderColor.toUpperCase() == "RGB(204, 204, 204) RGB(204, 204, 204) RGB(204, 204, 204) RGB(204, 204, 204)") {
		  Objeto.style.borderColor = "#DBDCDD";
	  }
      Objeto.className = "panel";   
   }
}

function Indicar(Referencia,Local) {
   JanelaIndique = window.open("indique.php?ref=" + Referencia + "&local=" + Local,"JanelaIndique","width=500,height=400,left=10,top=10");
   JanelaIndique.focus();
}

function EnviarIndicacao() {
 if (document.frmIndique.Nome.value.length == 0) {
	alert("Preencha o seu Nome!");
	document.frmIndique.Nome.focus();
	return;
 }
 if (document.frmIndique.Email.value.length == 0) {
	alert("Preencha o seu eMail!");
	document.frmIndique.Email.focus();
	return;			
 }
 AchouArroba = document.frmIndique.Email.value.indexOf("@");
 AchouPonto = document.frmIndique.Email.value.indexOf(".");
 if (AchouArroba <= 0 || AchouPonto <= 0) {
	alert("O seu eMail está no formato inválido. Verifique!");
	document.frmIndique.Email.focus();
	return;					 
 }		 
 for (x = 0; x < 1; x++) {
	if (document.frmIndique.NomeAmigo[x].value.length == 0) {
	   alert("Preencha o Nome do seu Amigo!");
	   document.frmIndique.NomeAmigo[x].focus();
	   return;			
	}
	if (document.frmIndique.EmailAmigo[x].value.length == 0) {
	   alert("Preencha o e-Mail do seu Amigo!");
	   document.frmIndique.EmailAmigo[x].focus();
	   return;			
	}
	AchouArroba = document.frmIndique.EmailAmigo[x].value.indexOf("@");
	AchouPonto = document.frmIndique.EmailAmigo[x].value.indexOf(".");
	if (AchouArroba <= 0 || AchouPonto <= 0) {
	   alert("O eMail do seu Amigo está no formato inválido. Verifique!");
	   document.frmIndique.EmailAmigo[x].focus();
	   return;					 
	}		 
 }
 document.frmIndique.submit();
}

function CarregaPermutas() {
   JanelaPermutas = window.open("permuta.php","JanelaPermutas","width=550,height=500,left=10,top=10");
   JanelaPermutas.focus();
}

function CarregaAreas() {
   JanelaAreas = window.open("areas.php","JanelaAreas","width=550,height=500,left=10,top=10");
   JanelaAreas.focus();
}

function ajaxInit() {
   var req;
   try {
      req = new ActiveXObject("Microsoft.XMLHTTP");
   } catch(e) {
      try {
         req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(ex) {
         try {
            req = new XMLHttpRequest();
         } catch(exc) {
            alert("Esse browser não tem recursos para uso do Ajax");
            req = null;
         }
      }
   }
   return req;
}

function VerificaCEP(CEP) {
   ValorCEP = CEP[0].value + CEP[1].value;
   ajax2 = ajaxInit();
   resultado = "";
   if (ValorCEP.length > 0) {
      document.getElementById("div_cep1").style.display = "block";
      document.getElementById("div_cep2").style.display = "block";   
   } else {
	  alert("Digite o CEP!");
	  document.frmCadastre.CEP[0].focus();
	  return;
   }
   if (ajax2) {
      ajax2.open("GET", "cep.php?cep=" + ValorCEP, true);
      ajax2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      ajax2.onreadystatechange = function() {
         if(ajax2.readyState == 4) {
            if(ajax2.status == 200) {
               resultado = unescape(ajax2.responseText);
			   if (resultado.length > 0) {
			      splitResultado = resultado.split("&");
				  if (splitResultado[1] == "resultado=1") {
					 UF = splitResultado[3].replace("uf=","");
					 while (UF.indexOf("+") >= 0) {
						UF = UF.replace("+"," ");
					 }
					 Cidade = splitResultado[4].replace("cidade=","");
					 while (Cidade.indexOf("+") >= 0) {
						Cidade = Cidade.replace("+"," ");
					 }					 
 					 Bairro = splitResultado[5].replace("bairro=","");
					 while (Bairro.indexOf("+") >= 0) {
						Bairro = Bairro.replace("+"," ");
					 }					 
					 TipoLogradouro = splitResultado[6].replace("tipo_logradouro=","");
					 while (TipoLogradouro.indexOf("+") >= 0) {
						TipoLogradouro = TipoLogradouro.replace("+"," ");
					 }					 
					 Logradouro = splitResultado[7].replace("logradouro=","");
					 while (Logradouro.indexOf("+") >= 0) {
						Logradouro = Logradouro.replace("+"," ");
					 }		
					 document.frmCadastre.Estado.value = UF;
					 document.frmCadastre.Cidade.value = Cidade;
					 document.frmCadastre.Bairro.value = Bairro;
					 document.frmCadastre.Endereco.value = TipoLogradouro + " " + Logradouro;
 				     document.getElementById("div_cep1").style.display = "none";
				     document.getElementById("div_cep2").style.display = "none";   					 
				  } else {
 				     document.getElementById("div_cep1").style.display = "none";
				     document.getElementById("div_cep2").style.display = "none";   					 
					 document.frmCadastre.Estado.value = "";
					 document.frmCadastre.Cidade.value = "";
					 document.frmCadastre.Bairro.value = "";
					 document.frmCadastre.Endereco.value = "";
					 alert("CEP Inválido!");
					 document.frmCadastre.CEP[0].focus();
				  }
			   }
            }
         }      
      }
      ajax2.send(null);
   }     
}

function VerificaCEPProprietario(CEPProprietario) {
   ValorCEP = CEPProprietario[0].value + CEPProprietario[1].value;
   ajax3 = ajaxInit();
   resultado = "";
   if (ValorCEP.length > 0) {
      document.getElementById("div_cep3").style.display = "block";
      document.getElementById("div_cep4").style.display = "block";   
   } else {
	  alert("Digite o CEP!");
	  document.frmCadastre.CEPProprietario[0].focus();
	  return;
   }
   if (ajax3) {
      ajax3.open("GET", "cep.php?cep=" + ValorCEP, true);
      ajax3.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      ajax3.onreadystatechange = function() {
         if(ajax3.readyState == 4) {
            if(ajax3.status == 200) {
               resultado = unescape(ajax3.responseText);
			   if (resultado.length > 0) {
			      splitResultado = resultado.split("&");
				  if (splitResultado[1] == "resultado=1") {
					 UF = splitResultado[3].replace("uf=","");
					 while (UF.indexOf("+") >= 0) {
						UF = UF.replace("+"," ");
					 }
					 Cidade = splitResultado[4].replace("cidade=","");
					 while (Cidade.indexOf("+") >= 0) {
						Cidade = Cidade.replace("+"," ");
					 }					 
 					 Bairro = splitResultado[5].replace("bairro=","");
					 while (Bairro.indexOf("+") >= 0) {
						Bairro = Bairro.replace("+"," ");
					 }					 
					 TipoLogradouro = splitResultado[6].replace("tipo_logradouro=","");
					 while (TipoLogradouro.indexOf("+") >= 0) {
						TipoLogradouro = TipoLogradouro.replace("+"," ");
					 }					 
					 Logradouro = splitResultado[7].replace("logradouro=","");
					 while (Logradouro.indexOf("+") >= 0) {
						Logradouro = Logradouro.replace("+"," ");
					 }		
					 document.frmCadastre.EstadoProprietario.value = UF;
					 document.frmCadastre.CidadeProprietario.value = Cidade;
					 document.frmCadastre.BairroProprietario.value = Bairro;
					 document.frmCadastre.EnderecoProprietario.value = TipoLogradouro + " " + Logradouro;
 				     document.getElementById("div_cep3").style.display = "none";
				     document.getElementById("div_cep4").style.display = "none";   					 
				  } else {
 				     document.getElementById("div_cep3").style.display = "none";
				     document.getElementById("div_cep4").style.display = "none";   					 
					 document.frmCadastre.EstadoProprietario.value = "";
					 document.frmCadastre.CidadeProprietario.value = "";
					 document.frmCadastre.BairroProprietario.value = "";
					 document.frmCadastre.EnderecoProprietario.value = "";
					 alert("CEP Inválido!");
					 document.frmCadastre.CEPProprietario[0].focus();
				  }
			   }
            }
         }      
      }
      ajax3.send(null);
   }     
}

function SelecionaDIV(Valor) {
   document.frmEncontre.local.value = Valor;
   if (Valor == "gv") { 
      document.getElementById("div_granja").style.display = "block";
      document.getElementById("div_sp").style.display = "none";
      document.getElementById("local_sp").src = "images/sp_1.png";
      document.getElementById("local_gv").src = "images/granja_2.png";
   } else if (Valor == "sp") { 
      document.getElementById("div_sp").style.display = "block"; 
	  document.getElementById("div_granja").style.display = "none";
	  document.getElementById("local_sp").src = "images/sp_2.png";
	  document.getElementById("local_gv").src = "images/granja_1.png";

} else {
      document.getElementById("div_sp").style.display = "none"; 
	  document.getElementById("div_granja").style.display = "none"; 	   
   }
}

function AbrirLista() {
   if (document.getElementById("lista").style.display == "block") {
      document.getElementById("lista").style.display = "none";
      document.getElementById("lista_sombra").style.display = "none";	  
	  return;
   } 
   if (document.getElementById("lista").style.display == "none") {
      document.getElementById("lista").style.display = "block";
      document.getElementById("lista_sombra").style.display = "block";	  	  
	  return;	   
   } 
}

function FecharLista() {
   parent.document.getElementById("lista").style.display = "none";
   parent.document.getElementById("lista_sombra").style.display = "none";	  
}

function AdicionarLista(ref) {
   document.getElementById("lista_imoveis").src = "lista_adicionar.php?ref=" + ref;
}

function ExibeMensagem(status) {
   if (status == "over") {
      document.getElementById("tooltip").style.display = "block";	   
   } else {
      document.getElementById("tooltip").style.display = "none";	   
   }
}

function Midias(Opcao) {
   if (Opcao == "S") {
	   document.frmCadastre.AutorizaPromocao[3].checked = false;
   } else {
	   document.frmCadastre.AutorizaPromocao[0].checked = false;
	   document.frmCadastre.AutorizaPromocao[1].checked = false;
	   document.frmCadastre.AutorizaPromocao[2].checked = false;
   }
}

function FecharJanela() {
    document.getElementById("boxAviso").style.display = "none";
    document.getElementById("boxAvisoSombra").style.display = "none";
    document.getElementById("fundoPreto").style.display = "none";
    document.getElementById("URL_Endereco").src = "about:blank";
}

function Mapa(Opcao) {
    document.getElementById("boxAviso").style.display = "block";
    document.getElementById("boxAvisoSombra").style.display = "block";
    document.getElementById("fundoPreto").style.display = "block";
    document.getElementById("URL_Endereco").src = "mapa.php?local=" + Opcao;	
}

function ajaxInit() {
   var req;
   try {
      req = new ActiveXObject("Microsoft.XMLHTTP");
   } catch(e) {
      try {
         req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(ex) {
         try {
            req = new XMLHttpRequest();
         } catch(exc) {
            alert("Esse browser não tem recursos para uso do Ajax");
            req = null;
         }
      }
   }
   return req;
}

function PopulaBairros(Utilizacao, Local) {
   document.getElementById("tipo4_granja_div").style.display = "none";
   if (document.getElementById("bairro_granja") != undefined) {
       document.getElementById("bairro_granja").style.display = "none";
   }
   if (Local == "GV") {
      TotalGranja = document.frmGranja.bairro_granja.length;
      for (x = TotalGranja; x >= 0; x--) {
         document.frmGranja.bairro_granja.remove(x);
      }
	  if ((Utilizacao == "2") || (Utilizacao == "3")) {
	     document.getElementById("loading_granja2").style.display = "block";
	     document.getElementById("loading_granja").style.display = "none";		 
	  } else {
	     document.getElementById("loading_granja").style.display = "block";	  		  
	     document.getElementById("loading_granja2").style.display = "none";		 
	  }
      document.frmGranja.bairro_granja.selectedIndex = 0;	  	  
	  document.frmGranja.bairro_granja.style.display = "none";
   } else {
      TotalSP = document.frmSP.bairro_sp.length;
      for (x = TotalSP; x >= 0; x--) {
         document.frmSP.bairro_sp.remove(x);
      }
	  document.getElementById("loading_sp").style.display = "block";	  
      document.frmSP.bairro_sp.selectedIndex = 0;	  
	  document.frmSP.bairro_sp.style.display = "none";	  
   }
   timerID = setTimeout("PopulaBairros2('" + Utilizacao + "','" + Local + "')",1000);
}

function PopulaBairros2(Utilizacao,Local) {
   ajax = ajaxInit();
   resultado = "";
   if (ajax) {
      ajax.open("GET", "popula_bairros.php?local=" + Local + "&utilizacao=" + Utilizacao, true);
      ajax.setRequestHeader("Content-Type", "text/html; charset=iso-8859-1");
      ajax.onreadystatechange = function() {
         if(ajax.readyState == 4) {
            if(ajax.status == 200) {
               resultado = ajax.responseText;
			   if (resultado.length > 0) {
			      splitResultado = resultado.split("|");
				  xGV = 0;
				  xSP = 0;
				  for (x = 0; x < (splitResultado.length-1); x++) {
				     BairroAtual = splitResultado[x].split("&");
				     NomeBairro = BairroAtual[0];
					 LocalBairro = BairroAtual[1];
					 if (LocalBairro == "GV") {
						if ((NomeBairro == "Bairro:") || (NomeBairro == "Condomínio Fechado:")) {
					       document.frmGranja.bairro_granja[xGV] = new Option(NomeBairro, "");
						} else {
                           document.frmGranja.bairro_granja[xGV] = new Option(NomeBairro, NomeBairro);							
						}
						xGV++;
					 } else {
					    if (xSP == 1) {
					    //   NomeBairro = NomeBairro.substring(2,NomeBairro.length);
					    }
						if (NomeBairro.charCodeAt(0) == 13) {
						   NomeBairro = NomeBairro.substring(2,NomeBairro.length);	
						}
						if ((NomeBairro == "Bairro:") || (NomeBairro == "Condomínio Fechado:")) {
					       document.frmSP.bairro_sp[xSP] = new Option(NomeBairro, "");
						} else {
                           document.frmSP.bairro_sp[xSP] = new Option(NomeBairro, NomeBairro);							
						}
						xSP++;
					 }
				  }
				  if (Local == "GV") {
				  	  document.getElementById("loading_granja").style.display = "none";
					  document.getElementById("loading_granja2").style.display = "none";
                      document.frmGranja.bairro_granja.selectedIndex = 0;	  	  					  
	                  document.frmGranja.bairro_granja.style.display = "block";
				  } else {
				  	  document.getElementById("loading_sp").style.display = "none";
                      document.frmSP.bairro_sp.selectedIndex = 0;	  	  					  
	                  document.frmSP.bairro_sp.style.display = "block";
				  }
			   }
            }
         }      
      }
      ajax.send(null);
   }  
   
}

function VerificaLocal(LocalAtual) {
    if (LocalAtual.length == 0) {
        LocalAtual = "sp";
    }
    if (LocalAtual == "sp") {
        document.frmEncontre.local.selectedIndex = 0;
        document.getElementById("div_granja").display = "none";
        document.getElementById("div_sp").display = "block";
   } else {
        document.frmEncontre.local.selectedIndex = 1;
        document.getElementById("div_granja").display = "block";
        document.getElementById("div_sp").display = "none";
   }
   document.frmGranja.bairro_granja.selectedIndex = 0;
   document.frmSP.bairro_sp.selectedIndex = 0;   

   document.frmGranja.tipo_granja.selectedIndex = 0;
   document.frmSP.tipo_sp.selectedIndex = 0;   

   document.frmGranja.tipo2_granja.selectedIndex = 0;
   document.frmSP.tipo2_sp.selectedIndex = 0;   

   document.frmGranja.tipo3_granja.selectedIndex = 0;
   document.frmSP.tipo3_sp.selectedIndex = 0;   

   document.frmGranja.negocio_granja.selectedIndex = 0;
   document.frmSP.negocio_sp.selectedIndex = 0;   

   document.frmGranja.valor_granja.selectedIndex = 0;
   document.frmSP.valor_sp.selectedIndex = 0;   

   document.frmGranja.valor2_granja.selectedIndex = 0;
   document.frmSP.valor2_sp.selectedIndex = 0;   

   document.frmGranja.dormitorios_granja.selectedIndex = 0;
   document.frmSP.dormitorios_sp.selectedIndex = 0;

   if (LocalAtual == "sp") {
       document.frmSP.estilo_sp[0].click();          
   } else {
       document.frmGranja.estilo_granja[0].click();       
   }


}

function HabilitaCondominios(Indice) {
    if (Indice == 4) {
        document.getElementById("tipo4_granja_div").style.display = "block";
        if (document.getElementById("bairro_granja") != undefined) {
            document.getElementById("bairro_granja").style.display = "block";
            document.frmGranja.bairro_granja.selectedIndex = 0;
        }
    } else {
        document.getElementById("tipo4_granja_div").style.display = "none";
        if (document.getElementById("bairro_granja") != undefined) {
            document.getElementById("bairro_granja").style.display = "none";
        }
    }
}

function SelecionaBairros(Objeto) {
   TotalSelecionado = 0;
   Todos = false;   
   if (Objeto.options[0].selected) {
      Todos = true;   
   }
   for (x = 0; x < Objeto.options.length; x++) {
	  if ((Todos) && (x > 0)) {
	     Objeto.options[x].selected = false;
	  }
   }
}