function validaCampos(qual){
	if(qual == "3"){
		with (document.frm_busca){
			if(ls_cidade.value == '0'){
				alert('Por favor selecione uma cidade.');
				ls_cidade.focus();
				return false;
			}else{
				submit();
				return true;
			}
		}
	}else if(qual == "2"){
		with (document.frm_busca_rapida){
			if(ls_opcao.value == '0'){
				alert('Por favor selecione a opção de negócio.');
				ls_opcao.focus();
				return false;
			}else{
				submit();
				return true;
			}
		}
	}else if(qual == "1"){
		with (document.frm_contato){
			if(txt_nome.value == ""){
				alert('Por favor informe seu nome');
				txt_nome.focus();
				return false;
			}
// 			if(txt_email.value == ""){
// 				alert('Por favor informe seu e-mail');
// 				txt_email.focus();
// 				return false;
// 			}else if (!validaEmail(txt_email, "frm_contato", 2)){
// 				txt_email.focus();
// 				txt_email.select();
// 				return false;
// 			}
			if(txt_cidade.value == ""){
				alert('Por favor informe sua cidade');
				txt_cidade.focus();
				return false;
			}
			if(ls_uf.value == "0"){
				alert('Por favor informe seu estado');
				ls_uf.focus();
				return false;
			}
			if(txt_fone.value == ""){
				alert('Por favor informe seu telefone');
				txt_fone.focus();
				return false;
			}
			if(ls_assunto.value == "0"){
				alert('Por favor selecione o assunto');
				ls_assunto.focus();
				return false;
			}
			if(txt_mensagem.value == ""){
				alert('Por favor digite uma mensagem');
				txt_mensagem.focus();
				return false;
			}else{
				submit();
				return true;
			}
		}
	}
}

//valida CPF
function validaCpf(cpf){ 
	var i;
	cpf = cpf.substr(0,3) + cpf.substr(4,3) + cpf.substr(8,3) + cpf.substr(12,2);
	var c = cpf.substr(0,9); 
	var dv = cpf.substr(9,2); 
	var d1 = 0; 
	for (i = 0; i < 9; i++) { 
		d1 += c.charAt(i)*(10-i); 
	} 
	if (d1 == 0){
// 		alert("CPF Invalido") 
		return false; 
	} 
	d1 = 11 - (d1 % 11); 
	if (d1 > 9) d1 = 0; 
	if (dv.charAt(0) != d1){ 
// 		alert("CPF Invalido") 
		return false;  
	}
	d1 *= 2; 
	for (i = 0; i < 9; i++){ 
		d1 += c.charAt(i)*(11-i); 
	} 
	d1 = 11 - (d1 % 11); 
	if (d1 > 9) d1 = 0; 
	if (dv.charAt(1) != d1){ 
// 		alert("CPF Invalido")
		return false; 
	} 
	return true; 
} 

function criaLista(p, l, op){
// 	if(l == "bairro"){
// 		with(document.frm_busca){
// 			for(var i = 0; i < ls_bairro.options.length; i++){
// 				ls_bairro.options[i] = '';
// 			}
// 		}
// 	}
	window.open('cria_lista.php?listar=' + l + '&opcao=' + op + '&p=' + p, 'ifr_executa');
// 	if(l == "bairro"){
// 		window.open('cria_lista.php?listar=tipo_imovel_cidade&opcao=' + op + '&p=' + p, 'ifr_executa');
// 	}
}

function limpaBtsAvanca(){
	for(i = 1; i < 4; i++){
		var temBotao = document.getElementsByName('btn_avanca_' + i);
		if(temBotao[0] != undefined){
			botao = eval('btn_avanca_' + i);
			botao.style.display = 'none';
		}
	}
}

function limpaBtsVoltar(){
	for(i = 0; i < 3; i++){
		var temBotao = document.getElementsByName('btn_volta_' + i);
		if(temBotao[0] != undefined){
			botao = eval('btn_volta_' + i);
			botao.style.display = 'none';
		}
	}
}

function avancaFoto(botao, p, btP, alt, larg){
	mostraFoto = "fig_imovel/imovel.php?";
	mostraOk = false;
	limpaBtsAvanca();
	limpaBtsVoltar();
	
	var temBotao = document.getElementsByName(btP);
	if(temBotao[0] != undefined){
		btProximo = eval(btP);
		mostraOk = true;
	}
	proxima = mostraFoto + p;
	document.frm_guarda_foto.foto_real.value = p;
	
	document.frm_guarda_foto.alt_real.value = alt;
	document.frm_guarda_foto.larg_real.value = larg;
	
	document.img_imovel.src = proxima;
	if(mostraOk){btProximo.style.display = '';}
}

function voltaFoto(botao, a, btA, alt, larg){
	mostraFoto = "fig_imovel/imovel.php?";
	limpaBtsVoltar();
	limpaBtsAvanca();
	if(btA != "btn_volta_-1"){btAnterior = eval(btA);}
	anterior = mostraFoto + a;
	document.frm_guarda_foto.foto_real.value = a;
	
	document.frm_guarda_foto.alt_real.value = alt;
	document.frm_guarda_foto.larg_real.value = larg;
	
	document.img_imovel.src = anterior;
	if(btA != "btn_volta_-1"){btAnterior.style.display = '';}
}

function mostraImagem(a,l,pg){
	if(pg.substr(0,8) != "figuras/") pg = "fig_imovel/" + pg;
	if(a > 480) a = 480
	if(l > 600) l = 600
	nova_janela = window.open('','mostra_imagem','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=no,menubar=no,width=' +l+ ',height=' + a);
	nova_janela.document.writeln('<HTML><HEAD><TITLE></TITLE></HEAD><BODY style="margin=0px;text-align=center;"><IMG style="cursor=hand;" src="'+pg+'" onClick="window.close()" alt="Clique na imagem para fechar"></BODY></HTML>');
	nova_janela.document.close();
}

function validaEmail(email, formu, tipo){
	var sMail = new String(email.value)
	var index = sMail.indexOf("@");
	if (index > 0){
		var pindex = sMail.indexOf(".",index);
		if ((pindex > index+1) && (sMail.length > pindex+1)){
			result = true;
		}else{
			result = false;
		}
	}else{
		result = false;
	}
	if(!result){
		alert('EMAIL inexistente, por favor insira um e-mail válido.');
		if(tipo == 1){
			formu.elements[0].focus();
			formu.elements[0].select();
		}
	}else{
		if(tipo == 1){
			formu.submit();
		}else{
			return result;
		}
	}
}

// Valida endereço de e-mail
function validaEmailSimples(email){
	var resultado = false
	var emailStr = new String(email)
	var index = emailStr.indexOf("@");
	if (index > 0){
		var indice = emailStr.indexOf(".",index);
		if ((indice > index+1) && (emailStr.length > indice+1)){
			resultado = true;
		}
	}
	return resultado;
}

function ajustarFone(input, evento){
	var tecla = evento.keyCode;
	valor = input.value;
	valor = valor.replace( "(", "" );
	valor = valor.replace( ")", "" );
	valor = valor.replace( " ", "" );
	valor = valor.replace( "-", "" );
	tamanhoMaximo = 10;
	tamanho = valor.length;
    if(!soNumero(evento)){
	    return false;
	} else { 
		if (tamanho < tamanhoMaximo && tecla != 8) {
		tamanho = valor.length + 1 ;
		}
		if (tecla == 8 ) {
			tamanho = tamanho - 1 ;
		}
		if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
			if ( tamanho <= 4 ) { 
		 		input.value = valor ;
			}
		 	if ( (tamanho > 4) && (tamanho <= 8) ) {
		 		input.value = valor.substr(0,tamanho-4) + '-' + valor.substr( tamanho - 4, tamanho ) ;
			}
		 	if ( (tamanho >= 9) && (tamanho <= 10) ) {
				input.value = '(' + valor.substr(0,2) + ') ' + valor.substr(2,tamanho-6) + '-' + valor.substr(tamanho-4,tamanho) ;			
			}
		}
	}
	return true;
}

// Ajuste para moeda (R$)
function ajustarGrana(thisone){
	var wd
	wd="w"
	var tempnum=thisone.value
	for (i=0;i<tempnum.length;i++){
		if (tempnum.charAt(i)==","){
			wd="d"
			break
		}
	}
	if (wd=="w")
		thisone.value=tempnum+",00"
	else{
		if (tempnum.charAt(tempnum.length-2)==","){
	 		thisone.value=tempnum+"0"
		}else if (tempnum.charAt(tempnum.length-1)==","){
	 		thisone.value=tempnum+"00"
		}
	}
}

//Formata CPF / CNPJ
function ajustarCpf(input, evento){
	tecla = evento.keyCode;
    if(!soNumero(evento)){
	    return false;
	}else{
		if((tecla<48)||(tecla>57)){
			evento.returnValue =false;
			return false;
		}else{
			if ((input.value.length == 3)||(input.value.length == 7)) {
				input.value = input.value + "." ;
			}
			if (input.value.length == 11) {
				input.value = input.value + "-" ;
			}
			if(input.value.length == 15){
				input.value = input.value.replace('.','');
				input.value = input.value.replace('.','');
				input.value = input.value.replace('-','');
				input.value = input.value.substr(0, 2) + '.' + input.value.substr(2, 3) + '.' + input.value.substr(5, 3) + '/' + input.value.substr(8, 4);
				input.value = input.value + "-" ;
			}
		}
	}
	return true;
}

//Formata CEP
function ajustarCep(input, evento){
    if(!soNumero(evento)){
	    return false;
	} else { 
		if (input.value.length == 5) {
		   input.value = input.value + "-";
		}
	}
	return true;
}

//Completa a data com as barras "/"
function ajustarData(input, evento){
    if(!soNumero(evento)){
	    return false;
	} else { 
		if ((input.value.length == 2)||(input.value.length == 5)) {
		   input.value = input.value + "/" ;
		   }
	}
	return true;
}

function soNumero(evento){
	var BACKSPACE=  8; 
	var TAB= 9;
    var DEL=  46; 
    var FRENTE=  39; 
    var TRAS=  37; 
    var tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
    
    if (( tecla == BACKSPACE )||(tecla == DEL)||(tecla == FRENTE)||(tecla == TRAS)||(tecla == TAB)){
         return true; 
	}
    if ( tecla == 13 )     return false; 

	if ((tecla >= 48 && tecla <= 57) || (tecla == 44)) {
		return true;
	}else{
		evento.returnValue = false; 
		return false;
	}
}

function FormataReais(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) 
		return true;
	key = String.fromCharCode(whichCode);  // Valor para o código da Chave
	if (strCheck.indexOf(key) == -1) 
		return false;  // Chave inválida
	len = fld.value.length;
	for(i = 0; i < len; i++)
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
		aux2 = '';
		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		fld.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
			fld.value += aux2.charAt(i);
		fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}

function acrescentaConjuge(opcao){
	if(opcao == '1'){
		document.getElementById('caixa_conjuge').style.display = '';
	}else{
		document.getElementById('caixa_conjuge').style.display = 'none';
	}
}

function validaDadosConj(i){
	with(parent.document.frm_cadastro){
		if(ls_regime.value == '0'){
			campos[i] = 'ls_regime';
			nomesCampos[i] = 'Regime';
			i++;
		}
		if(st_nome_conj.value == ''){
			campos[i] = 'st_nome_conj';
			nomesCampos[i] = 'Nome conj.';
			i++;
		}
		if(st_cpf_conj.value == ''){
			campos[i] = 'st_cpf_conj';
			nomesCampos[i] = 'CPF conj.';
			i++;
		}
		if(st_rg_conj.value == ''){
			campos[i] = 'st_rg_conj';
			nomesCampos[i] = 'Rg conj.';
			i++;
		}
		if(st_nacionalid_conj.value == ''){
			campos[i] = 'st_nacionalid_conj';
			nomesCampos[i] = 'Nacionalidade';
			i++;
		}
		if(dt_dt_nasc_conj.value == ''){
			campos[i] = 'dt_dt_nasc_conj';
			nomesCampos[i] = 'Dt. nasc. conj.';
			i++;
		}
	}
	return i;
}
