function AbreImovel() {

Codigo = document.getElementById("codigo").value;

if (Codigo == "")
alert("Por favor, informe o código do imóvel");
else
popupDetalhe('../login/index_detalhes.asp?codigo='+Codigo,'','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes','622','582','true');

return false;

}

// abre os detalhes do imóvel
function Detalhes(url) {
  if (url == "")
  alert("Erro...");
  else
  popupDetalhe(url,'','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes','680','582','true');
}

// abre os detalhes do imóvel
function PesquisarImovel(url) {
  if (url == ""){
  	alert("Erro...");
  }else{
	document.getElementById( "selecionado" ).value = url.substring(url.indexOf('?imovel=')+8);  
  	popupDetalhe(url,'','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes','660','450','true');
  }
}

function InserirCodigoImovel(cod, cidade, bairro, area, tipo, quartos, preco) {
  var xxx = window.opener.document.getElementById( "selecionado" ).value;
  window.opener.document.getElementById( 'cod' + xxx ).value =cod;
  window.opener.document.getElementById( 'codigo' + xxx ).innerHTML =cod;
  window.opener.document.getElementById( 'cidade' + xxx ).innerHTML =cidade;
  window.opener.document.getElementById( 'bairro' + xxx ).innerHTML =bairro;
  window.opener.document.getElementById( 'area' + xxx ).innerHTML =area;
  window.opener.document.getElementById( 'tipo' + xxx ).innerHTML =tipo;
  window.opener.document.getElementById( 'quartos' + xxx ).innerHTML =quartos;
  window.opener.document.getElementById( 'preco' + xxx ).innerHTML =preco;
  window.close();
}

//===========================================================================
// FUNÇÕES GENÉRICAS
//===========================================================================
function valida_mail(valor) {
prim = valor.indexOf("@")
if(prim < 1) return false;
if(valor.indexOf("@",prim + 1) != -1) return false;
if(valor.indexOf(".") < 1) return false;
if(valor.indexOf("zipmeil.com") > 0) return false;
if(valor.indexOf("hotmeil.com") > 0) return false;
if(valor.indexOf(".@") > 0) return false;
if(valor.indexOf("@.") > 0) return false;
if(valor.indexOf(".com.br.") > 0) return false;
if(valor.indexOf("/") > 0) return false;
if(valor.indexOf("[") > 0) return false;
if(valor.indexOf("]") > 0) return false;
if(valor.indexOf("(") > 0) return false;
if(valor.indexOf(")") > 0) return false;
if(valor.indexOf("..") > 0) return false;
if(valor.indexOf(",") > 0) return false;
return true;

}

function popupDetalhe(theURL,winName,features, myWidth, myHeight, isCenter) {
    if(window.screen)if(isCenter)if(isCenter=="true"){
	   var myLeft = (screen.width-myWidth)/2;
	   var myTop = (screen.height-myHeight)/2;
	   features+=(features!='')?',':'';
	   features+=',left='+myLeft+',top='+myTop;
    }
    window.open(theURL,winName,features+((features!='')?',':'')+'width='+myWidth+',height='+myHeight);
}

function valida_cpf(elemento) {

AuxCPFCGC = elemento.value;
AuxCPFCGC = AuxCPFCGC.replace(".", "");
AuxCPFCGC = AuxCPFCGC.replace(".", "");
AuxCPFCGC = AuxCPFCGC.replace("-", "");

var checkOK = "0123456789";

 if (elemento.value == "" || elemento.value == null || AuxCPFCGC.length != 11 )
  {
    alert("CPF deve conter 11 dígitos!");
    elemento.focus();
    return(false);
  }

  for (i = 1; i <= AuxCPFCGC.length ; i++) 
  {
    if (AuxCPFCGC.substring(i-1,i) == " ")
    {
      elemento.focus();	  
      alert("O campo CPF não pode conter espaços!");
      return(false);
    }
  }

var checkStr = AuxCPFCGC;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("O CPF deve conter apenas números.");
    elemento.focus();
    return(false);
  }
 
    if (!CPF_OK(AuxCPFCGC))
    {
      elemento.focus();
      alert("Número de CPF testado e entendido como inválido!");
      return(false);
    }
return(true);

}


function CPF_OK(Numero_CPF) 
{
  var Parcela;
  var Quociente;
  var Resto;
  var Soma;
  var Fator;
  var I;
  var C1;
  var C2;
  var dv1;
  var dv2;
    
  //Verificação dos dois digitos finais em relação ao número completo
  C1 = parseInt(Numero_CPF.substring(9, 10));  //10º caracter = primeiro dígito verificador
  C2 = parseInt(Numero_CPF.substring(10, 11)); //11º caracter = segundo dígito verificador
    
  //Verificação do primeiro dígito (C1)
  Soma = 0;
  Parcela = 0;
  Fator = 0;
	
  for(I=1; I<=9; I++)
  {
    Fator = I + 1;

    Parcela = Fator * parseInt(Numero_CPF.substring(9 - I, 9 - I + 1));
    Soma = Soma + Parcela;
  } //fechando o "for".
    
  dv1 = (Soma % 11); 
  dv1 = 11 - dv1;

  if(dv1 >9 )
  {
    dv1 = 0;
  }
    
  if(C1 != dv1)
  {
    return false;
  }
    
  //Verificação do segundo dígito (C2)
  Soma = 0;
  Parcela = 0;
  Fator = 0;

  for(I=1; I<=10; I++)
  {
    Fator = I + 1;
  
    Parcela = Fator * parseInt(Numero_CPF.substring(10 - I, 10 - I + 1));
    Soma = Soma + Parcela;
  }
	 
  dv2 = (Soma % 11);
  dv2 = 11 - dv2;
  
  if(dv2 > 9)
  {
    dv2 = 0;
  }

  if(C2 != dv2)
  {
    return false;
  }
return true;
}

function troca_virgula( num ) {
	var aux = "";
    for ( var i = 0; i < num.length ; i++ ) {
    	if ( num.substring(i, i+1) == "," )
			aux += '.';
		else if ( num.substring(i, i+1) != "." )
			aux += num.substring(i, i+1);
    }
    return aux;
}


function validaCep(cep) {
	if (cep != "" && cep.length == 9) {
	    url   = "http://www.bhsol.com.br/scripts/netimoveis/cep_xml.php?cep=" + cep.replace("-","");
	    tipo = "mostraDadosCep";
	    loadXMLDoc(url);
	}
}

function check_cep(form) {
	
	if(form.cep.value==""){
		alert("Por favor, digite o CEP do imóvel.");
		form.cep.focus();
	}
	else if ( valida_cep_tracao(form.cep.value) ){
		alert("Por favor, digite um CEP válido e no formato '99999-999'.");
		form.cep.focus();
	}
	else {
		window.open('checar_cep.asp?form='+form.name+'&cep='+form.cep.value,'checa_cep','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=468,height=300')
	}
}
