// JavaScript Document

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

window.onload=function(){
	MM_preloadImages('imagens/loader.gif');	
}

function mostra_foto(Y, X, Z){
	//alert(Y);
	var nova = Y.split('\\');
	var nova_barra = '';
	for (i=0; i< nova.length; i++){
		nova_barra += nova[i]+'\\';
	}

	var novo_split = nova_barra.substr(0, Y.length);

   window.document.getElementById(X).innerHTML='<img src="file:///'+novo_split+'" width=105>';
}
function kurole_cadastro(x){	
	
	if (x.nome.value.length < 2 ){
		alert('Preencha o campo nome!');
		x.nome.focus();
		return false;
	}
	if (x.prop_cep.value.length < 8 ){
		alert('Preencha o CEP do proprietário!');
		x.prop_cep.focus();
		return false;
	}
	if (x.prop_cidade.value == "" ){
		alert('Preencha a CIDADE do proprietário!');
		x.prop_cidade.focus();
		return false;
	}
	if (x.prop_estado.value == "" ){
		alert('Preencha o ESTADO do proprietário!');
		x.prop_estado.focus();
		return false;
	}
	
	if (x.prop_bairro.value == "" ){
		alert('Preencha o BAIRRO do proprietário!');
		x.prop_bairro.focus();
		return false;
	}
	if (x.prop_numero.value == "" ){
		alert('Preencha o NÚMERO do proprietário!');
		x.prop_numero.focus();
		return false;
	}
	
	if ((!x.locacao.checked) && (!x.venda.checked)){
		alert('O imóvel é para locaçao ou venda?');
		return false;
	}
	if ((x.valor_venda.value == '') && (x.valor_aluguel.value == '')){
		alert('É obrigatório colocar um valor para operaçao escolhida!');
		return false;
	}
	
	if (x.cep.value.length < 8 ){
		alert('Preencha o CEP do imóvel!');
		x.cep.focus();
		return false;
	}
	if (x.cidade_estado.value == "" ){
		alert('Preencha a CIDADE do imóvel!');
		x.cidade_estado.focus();
		return false;
	}
	if (x.estado.value == "" ){
		alert('Preencha o ESTADO do imóvel!');
		x.estado.focus();
		return false;
	}
	
	if (x.bairro.value == "" ){
		alert('Preencha o BAIRRO do imóvel!');
		x.bairro.focus();
		return false;
	}
	if (x.numero.value == "" ){
		alert('Preencha o NÚMERO do imóvel!');
		x.numero.focus();
		return false;
	}
	window.document.getElementById('bt_enviar').innerHTML = '<img src="imagens/loader.gif"><br><font color=green><b>enviando...</b></font>';
}

function Limpar(valor, validos) {
	// retira caracteres invalidos da string
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++) {
	aux = validos.indexOf(valor.substring(i, i+1));
	if (aux>=0) {
	result += aux;
	}
	}
	return result;
}

function Formata_VALOR(campo,tammax,teclapres,decimal) {
	
	var tecla = teclapres.keyCode;

	var vr = Limpar(campo.value,"0123456789");
	var tam = vr.length;
	var dec=decimal
	
	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
	
	if (tecla == 8 )
	{ tam = tam - 1 ; }
	
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
	
	if ( tam <= dec )
	{ campo.value = vr ; }

	if ( (tam > dec) && (tam <= 5) ){
	campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 6) && (tam <= 8) ){
	campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
	}
	
	if ( (tam >= 9) && (tam <= 11) ){
		campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 12) && (tam <= 14) ){
		campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 15) && (tam <= 17) ){
		campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}
	}	

}

function mostra_esconde_form(id1, id2){
	if(document.getElementById(id1).style.display == 'none' || document.getElementById(id1).style.display == ''){
		document.getElementById(id1).style.display = 'block';
		if(id2 != ""){
			document.getElementById(id2).style.display = 'none';
		}
	}else{
		document.getElementById(id1).style.display = 'none';
		if(id2 != ""){
			document.getElementById(id2).style.display = 'none';
		}
	}
}


function selecionaAbas(x, id1, id2, id3){
	var opcao1 		= document.getElementById(id1);
	var opcao2 		= document.getElementById(id2);
	var opcao3 		= document.getElementById(id3);
	var pesquisa	= document.getElementById('pesquisa');
	
	if(x == 0){
		pesquisa.style.backgroundImage= "url(imagens/background_pesquisa.png)";
	}else if(x == 1){
		pesquisa.style.backgroundImage = "url(imagens/background_pesquisa2.png)";
	}else{
		pesquisa.style.backgroundImage = "url(imagens/background_pesquisa3.png)";
	}
	
	//alert(pesquisa.style.backgroundImage);
	
/*
	alert(opcao1.id);
	alert(opcao1.style.display);
*/


	if(opcao1.style.display == "none"){
		
		opcao1.style.display = "block";
		opcao2.style.display = "none";
		opcao3.style.display = "none";
	}


/*
	alert(opcao1.id);
	alert(opcao1.style.display);
*/

}


	function kurole(x){
		/* if (x.cnpj_numero.value == ''){
			window.alert('O campo CNPJ é obrigatório!');	
			return false;
		}
		
		if (!isCpfCnpj(x.cnpj_numero.value)){
			window.alert('O campo CNPJ está inválido!');	
			return false;
		}
		
		*/
		
		if (x.nome.value == ''){
			window.alert('O campo Nome é obrigatório!');	
			return false;
		}
		
		
		if (x.cpf.value == ''){
			window.alert('O campo CPF é obrigatório!');	
			return false;
		}
		
		if (!isCpfCnpj(x.cpf.value)){
			window.alert('O campo CPF está inválido!');	
			return false;
		}
	
		if (x.rg.value == ''){
			window.alert('O RG é obrigatório!');	
			return false;
		}
		
		if (x.rg_expedicao.value == ''){
			window.alert('O campo Data de Expediçao é obrigatório!');	
			return false;
		}
		
		if (x.endereco.value == ''){
			window.alert('O campo Endereco é obrigatório!');	
			return false;
		}
		
		if (x.numero.value == ''){
			window.alert('O campo Número é obrigatório!');	
			return false;
		}
		
		
		if (x.cidade.value == ''){
			window.alert('O campo Cidade é obrigatório!');	
			return false;
		}
		
		if (x.estado.value == ''){
			window.alert('O Estado é obrigatório!');	
			return false;
		}
		
		if (x.bairro.value == ''){
			window.alert('O campo Bairro é obrigatório!');	
			return false;
		}
		
		
		if (x.numero.value == ''){
			window.alert('O campo Número é obrigatório!');	
			return false;
		}
		
		
		if (x.cep.value == ''){
			window.alert('O campo CEP é obrigatório!');	
			return false;
		}
		
		if (x.fone.value == ''){
			window.alert('O campo Fone é obrigatório!');	
			return false;
		}
		
		if (x.data_nascimento.value == ''){
			window.alert('O campo data nascimento é obrigatório!');	
			return false;
		}
		
		
	}
	
	

var NUM_DIGITOS_CPF  = 11;
var NUM_DIGITOS_CNPJ = 14;
var NUM_DGT_CNPJ_BASE = 8;


/**
 * Adiciona método lpad() a classe String.
 * Preenche a String a esquerda com o caractere fornecido,
 * até que ela atinja o tamanho especificado.
 */
String.prototype.lpad = function(pSize, pCharPad)
{
	var str = this;
	var dif = pSize - str.length;
	var ch = String(pCharPad).charAt(0);
	for (; dif>0; dif--) str = ch + str;
	return (str);
} //String.lpad


/**
 * Adiciona método trim() a classe String.
 * Elimina brancos no início e fim da String.
 */
String.prototype.trim = function()
{
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
} //String.trim


/**
 * Elimina caracteres de formataçao e zeros a esquerda da string
 * de número fornecida.
 * @param String pNum
 *      String de número fornecida para ser desformatada.
 * @return String de número desformatada.
 */
function unformatNumber(pNum)
{
	return String(pNum).replace(/\D/g, "").replace(/^0+/, "");
} //unformatNumber


/**
 * Formata a string fornecida como CNPJ ou CPF, adicionando zeros
 * a esquerda se necessário e caracteres separadores, conforme solicitado.
 * @param String pCpfCnpj
 *      String fornecida para ser formatada.
 * @param boolean pUseSepar
 *      Indica se devem ser usados caracteres separadores (. - /).
 * @param boolean pIsCnpj
 *      Indica se a string fornecida é um CNPJ.
 *      Caso contrário, é CPF. Default = false (CPF).
 * @return String de CPF ou CNPJ devidamente formatada.
 */
function formatCpfCnpj(pCpfCnpj, pUseSepar, pIsCnpj)
{
	if (pIsCnpj==null) pIsCnpj = false;
	if (pUseSepar==null) pUseSepar = true;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var numero = unformatNumber(pCpfCnpj);

	numero = numero.lpad(maxDigitos, '0');
	if (!pUseSepar) return numero;

	if (pIsCnpj)
	{
		reCnpj = /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/;
		numero = numero.replace(reCnpj, "$1.$2.$3/$4-$5");
	}
	else
	{
		reCpf  = /(\d{3})(\d{3})(\d{3})(\d{2})$/;
		numero = numero.replace(reCpf, "$1.$2.$3-$4");
	}
	return numero;
} //formatCpfCnpj


/**
 * Calcula os 2 dígitos verificadores para o número-efetivo pEfetivo de
 * CNPJ (12 dígitos) ou CPF (9 dígitos) fornecido. pIsCnpj é booleano e
 * informa se o número-efetivo fornecido é CNPJ (default = false).
 * @param String pEfetivo
 *      String do número-efetivo (SEM dígitos verificadores) de CNPJ ou CPF.
 * @param boolean pIsCnpj
 *      Indica se a string fornecida é de um CNPJ.
 *      Caso contrário, é CPF. Default = false (CPF).
 * @return String com os dois dígitos verificadores.
 */
function dvCpfCnpj(pEfetivo, pIsCnpj)
{
	if (pIsCnpj==null) pIsCnpj = false;
	var i, j, k, soma, dv;
	var cicloPeso = pIsCnpj? NUM_DGT_CNPJ_BASE: NUM_DIGITOS_CPF;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var calculado = formatCpfCnpj(pEfetivo, false, pIsCnpj);
	calculado = calculado.substring(2, maxDigitos);
	var result = "";

	for (j = 1; j <= 2; j++)
	{
		k = 2;
		soma = 0;
		for (i = calculado.length-1; i >= 0; i--)
		{
			soma += (calculado.charAt(i) - '0') * k;
			k = (k-1) % cicloPeso + 2;
		}
		dv = 11 - soma % 11;
		if (dv > 9) dv = 0;
		calculado += dv;
		result += dv
	}

	return result;
} //dvCpfCnpj


/**
 * Testa se a String pCpf fornecida é um CPF válido.
 * Qualquer formataçao que nao seja algarismos é desconsiderada.
 * @param String pCpf
 *      String fornecida para ser testada.
 * @return <code>true</code> se a String fornecida for um CPF válido.
 */
function isCpf(pCpf)
{
	var numero = formatCpfCnpj(pCpf, false, false);
	var base = numero.substring(0, numero.length - 2);
	var digitos = dvCpfCnpj(base, false);
	var algUnico, i;

	// Valida dígitos verificadores
	if (numero != base + digitos) return false;

	/* Nao serao considerados válidos os seguintes CPF:
	 * 000.000.000-00, 111.111.111-11, 222.222.222-22, 333.333.333-33, 444.444.444-44,
	 * 555.555.555-55, 666.666.666-66, 777.777.777-77, 888.888.888-88, 999.999.999-99.
	 */
	algUnico = true;
	for (i=1; i<NUM_DIGITOS_CPF; i++)
	{
		algUnico = algUnico && (numero.charAt(i-1) == numero.charAt(i));
	}
	return (!algUnico);
} //isCpf


/**
 * Testa se a String pCnpj fornecida é um CNPJ válido.
 * Qualquer formataçao que nao seja algarismos é desconsiderada.
 * @param String pCnpj
 *      String fornecida para ser testada.
 * @return <code>true</code> se a String fornecida for um CNPJ válido.
 */
function isCnpj(pCnpj)
{
	var numero = formatCpfCnpj(pCnpj, false, true);
	var base = numero.substring(0, NUM_DGT_CNPJ_BASE);
	var ordem = numero.substring(NUM_DGT_CNPJ_BASE, 12);
	var digitos = dvCpfCnpj(base + ordem, true);
	var algUnico;

	// Valida dígitos verificadores
	if (numero != base + ordem + digitos) return false;

	/* Nao serao considerados válidos os CNPJ com os seguintes números BÁSICOS:
	 * 11.111.111, 22.222.222, 33.333.333, 44.444.444, 55.555.555,
	 * 66.666.666, 77.777.777, 88.888.888, 99.999.999.
	 */
	algUnico = numero.charAt(0) != '0';
	for (i=1; i<NUM_DGT_CNPJ_BASE; i++)
	{
		algUnico = algUnico && (numero.charAt(i-1) == numero.charAt(i));
	}
	if (algUnico) return false;

	/* Nao será considerado válido CNPJ com número de ORDEM igual a 0000.
	 * Nao será considerado válido CNPJ com número de ORDEM maior do que 0300
	 * e com as tres primeiras posiçoes do número BÁSICO com 000 (zeros).
	 * Esta crítica nao será feita quando o no BÁSICO do CNPJ for igual a 00.000.000.
	 */
	if (ordem == "0000") return false;
	return (base == "00000000"
		|| parseInt(ordem, 10) <= 300 || base.substring(0, 3) != "000");
} //isCnpj


/**
 * Testa se a String pCpfCnpj fornecida é um CPF ou CNPJ válido.
 * Se a String tiver uma quantidade de dígitos igual ou inferior
 * a 11, valida como CPF. Se for maior que 11, valida como CNPJ.
 * Qualquer formataçao que nao seja algarismos é desconsiderada.
 * @param String pCpfCnpj
 *      String fornecida para ser testada.
 * @return <code>true</code> se a String fornecida for um CPF ou CNPJ válido.
 */
function isCpfCnpj(pCpfCnpj)
{
	var numero = pCpfCnpj.replace(/\D/g, "");
	if (numero.length > NUM_DIGITOS_CPF)
		return isCnpj(pCpfCnpj)
	else
		return isCpf(pCpfCnpj);
} //isCpfCnpj


function opacityOver(id){
	document.getElementById(id).style.opacity 		= "0.60";
	document.getElementById(id).style.filter		= "alpha(opacity=60)";	
}

function opacityOut(id){
	document.getElementById(id).style.opacity 		= "1";
	document.getElementById(id).style.filter		= "alpha(opacity=100)";	
}

function none_block(id1, id2){
	var el1 = document.getElementById(id1);
	var el2 = document.getElementById(id2);
	if(el1.style.display == 'none' || el1.style.display == ''){
		el1.style.display = 'block';
		el2.style.display = 'none';
	}
}


function muda_estilo_selecionado(id1, id2){
	var el1 = document.getElementById(id1);
	var el2 = document.getElementById(id2);

	if(el1.style.display == 'block'){
		el1.style.color = '#777';
		el2.style.color = '#023265';
	}else{
		el1.style.color = '#023265';
		el2.style.color = '#777';
	}
}


function menuHover(el, acao){
	var img 		= el.firstChild;
	var srcSplit	= img.src.split('/');
	var nomeImg		= srcSplit[srcSplit.length - 1].split('.')[0];
	var extensaoImg	= srcSplit[srcSplit.length - 1].split('.')[1];
	
	if(acao == 'over'){
		var src		= img.src.replace(nomeImg, nomeImg+'_hover');
		
		img.setAttribute('src', src);	
	}else{
		var src		= img.src.replace('_hover', '');

		img.setAttribute('src', src);
	}
	//alert(src);
}

/*################################ Adicionado dia 13/01/2012 por Renan ################################*/

function FormataValor2(campo,tammax,teclapres) {

    var tecla = teclapres.keyCode;
    var vr = campo.value;
    vr = vr.replace( "/", "" );
    vr = vr.replace( "/", "" );
    vr = vr.replace( ",", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    vr = vr.replace( ".", "" );
    tam = vr.length;

    if (tam < tammax && tecla != 8){ tam = vr.length + 1; }

    if (tecla == 8 ){    tam = tam - 1; }
        
    if ( tecla == 8 || (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105) ){
        if ( tam <= 2 ){
             campo.value = vr; }
         tam = tam - 1;
         if ( (tam > 2) && (tam <= 5) ){
             campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }
         if ( (tam >= 6) && (tam <= 8) ){
             campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
         if ( (tam >= 9) && (tam <= 11) ){
             campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
         if ( (tam >= 12) && (tam <= 14) ){
             campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
         if ( (tam >= 15) && (tam <= 17) ){
             campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );}
    }
}
