function validaCadastro(tipo) {
	if (document.getElementById("finalidadeCadastro").value == "") {
		alert("Selecione a finalidade do cadastro");
		document.getElementById("finalidadeCadastro").focus();
		return false;
	}
	if (!(validaCpf_Cnpj("Cadastrar"))){
		document.getElementById("cpf_cnpj").focus();
		return false;
	}
	if (document.getElementById("nome").value == "") {
		alert("Preencha o campo Nome");
		document.getElementById("nome").focus();
		return false;
	}
	if (document.getElementById("email").value == "") {
		alert("Preencha o campo Email");
		document.getElementById("email").focus();
		return false;
	}
	if (!checkMail(document.getElementById("email").value)) {
    	alert("Email inválido");
    	document.getElementById("email").focus();
    	return false;
	}
	if (tipo == "cad") {
		if (document.getElementById("senha").value == "" || document.getElementById("senha2").value == "") {
			alert("Preencha o campo senha");
			document.getElementById("senha").focus();
			return false;
		}
		else {
			if (document.getElementById("senha").value != document.getElementById("senha2").value) {
				alert("As senhas não conferem");
				document.getElementById("senha").focus();
				return false;
			}
		}
	}
	else {
		if (document.getElementById("senha").value != document.getElementById("senha2").value) {
			alert("As senhas não conferem");
			document.getElementById("senha").focus();
			return false;
		}
	}
	if (validaNumero(document.getElementById("docIdentidade").value)) {
		if (document.getElementById("docIdentidade").value == "") {
			alert("Preencha o campo Doc. Identidade");
			document.getElementById("docIdentidade").focus();
			return false;
		}
	}
	else{
		document.getElementById("docIdentidade").focus();
		return false;
	}
        if (document.getElementById("orgaoEmissor").value == "") {
		alert("Selecione o orgao emissor");
		document.getElementById("orgaoEmissor").focus();
		return false;
	}
        if (document.getElementById("nacionalidade").value == "") {
		alert("Preencha o campo Nacionalidade");
		document.getElementById("nacionalidade").focus();
		return false;
	}
	if (document.getElementById("estCivil").value == "") {
		alert("Preencha o campo Est. Civil");
		document.getElementById("estCivil").focus();
		return false;
	}
	if (document.getElementById("profissao").value == "") {
		alert("Preencha o campo Profissao");
		document.getElementById("profissao").focus();
		return false;
	}
	if (document.getElementById("endereco").value == "") {
		alert("Preencha o campo Endereco");
		document.getElementById("endereco").focus();
		return false;
	}
	if (document.getElementById("complemento").value == "") {
		alert("Preencha o campo Complemento");
		document.getElementById("complemento").focus();
		return false;
	}
	if (validaNumero(document.getElementById("cep").value)) {
		if (document.getElementById("cep").value == "") {
			alert("Preencha o campo cep");
			document.getElementById("cep").focus();
			return false;
		}
	}
	else{
		document.getElementById("cep").focus();
		return false;
	}
	if (document.getElementById("estado").value == "") {
		alert("Selecione o Estado");
		document.getElementById("estado").focus();
		return false;
	}
	if (document.getElementById("cidade").value == "") {
		alert("Preencha o campo Cidade");
		document.getElementById("cidade").focus();
		return false;
	}
	if (document.getElementById("pais").value == "") {
		alert("Preencha o campo Pais");
		document.getElementById("pais").focus();
		return false;
	}
	if (document.getElementById("dataNascimento").value == "") {
		alert("Preencha o campo Data Nascimento");
		document.getElementById("dataNascimento").focus();
		return false;
	}
	if (!(validaData("dataNascimento"))){
		document.getElementById("dataNascimento").focus();
		return false;
	}
	if (document.getElementById("dddResidencial").value == "") {
		alert("Preencha o campo DDD Residencial");
		document.getElementById("dddResidencial").focus();
		return false;
	}
	if (document.getElementById("telResidencial").value == "") {
		alert("Preencha o campo Tel Residencial");
		document.getElementById("telResidencial").focus();
		return false;
	}
	
	document.getElementById("formUsuario").submit();
}

function validaImovelPasso1() {
	if (document.getElementById("finalidadeImovel").value == "") {
		alert("Selecione a Finalidade do Imovel");
		document.getElementById("finalidadeImovel").focus();
		return false;
	}
	if (document.getElementById("endereco").value == "") {
		alert("Preencha o campo Endereco");
		document.getElementById("endereco").focus();
		return false;
	}
	if (document.getElementById("cidade").value == "") {
		alert("Preencha o campo Cidade");
		document.getElementById("cidade").focus();
		return false;
	}
	if (validaNumero(document.getElementById("cep").value)) {
		if (document.getElementById("cep").value == "") {
			alert("Preencha o campo cep");
			document.getElementById("cep").focus();
			return false;
		}
	}
	else{
		document.getElementById("cep").focus();
		return false;
	}
	if (document.getElementById("estado").value == "") {
		alert("Selecione o Estado");
		document.getElementById("estado").focus();
		return false;
	}
	if (document.getElementById("iptuInscricao").value == "") {
		alert("Preencha o campo IPTU Inscricao");
		document.getElementById("iptuInscricao").focus();
		return false;
	}
	if (document.getElementById("descImovel").value == "") {
		alert("Preencha o campo Descrição do Imovel");
		document.getElementById("descImovel").focus();
		return false;
	}
	if (document.getElementById("descImovel").value.length > 800) {
		alert("O campo Descrição do Imovel permite até 800 caracteres");
		document.getElementById("descImovel").focus();
		return false;
	}
}

function validaImovelPasso2() {
	if (document.getElementById("destinacaoUso").value == "") {
		alert("Selecione a Destinacao/uso");
		document.getElementById("destinacaoUso").focus();
		return false;
	}
	if (document.getElementById("tipoImovel").value == "") {
		alert("Selecione o Tipo de Imovel");
		document.getElementById("tipoImovel").focus();
		return false;
	}
	if (document.getElementById("tipoUnidade").value == "") {
		alert("Selecione o Tipo de Unidade");
		document.getElementById("tipoUnidade").focus();
		return false;
	}
	if (document.getElementById("numeroSalas").value == "") {
		alert("Preencha o campo Nº Salas");
		document.getElementById("numeroSalas").focus();
		return false;
	}
	if (document.getElementById("numeroQuartos").value == "") {
		alert("Preencha o campo Nº Quartos");
		document.getElementById("numeroQuartos").focus();
		return false;
	}
	if (document.getElementById("numeroBanheiros").value == "") {
		alert("Preencha o campo Nº Banheiros");
		document.getElementById("numeroBanheiros").focus();
		return false;
	}
	if (document.getElementById("numeroCozinhas").value == "") {
		alert("Preencha o campo Nº Cozinhas");
		document.getElementById("numeroCozinhas").focus();
		return false;
	}
	if (document.getElementById("numeroCozinhas").value == "") {
		alert("Preencha o campo Nº Cozinhas");
		document.getElementById("numeroCozinhas").focus();
		return false;
	}
	if (document.getElementById("numeroDependencias").value == "") {
		alert("Preencha o campo Nº Dependencias");
		document.getElementById("numeroDependencias").focus();
		return false;
	}
	if (document.getElementById("numeroVagas").value == "") {
		alert("Preencha o campo Nº Vagas");
		document.getElementById("numeroVagas").focus();
		return false;
	}
	if (document.getElementById("numeroVagas").value == "") {
		alert("Preencha o campo Area util/construida");
		document.getElementById("areaUtilConstruida").focus();
		return false;
	}
	if (document.getElementById("numeroVagas").value == "") {
		alert("Preencha o campo Area do Terreno");
		document.getElementById("areaTerreno").focus();
		return false;
	}
	if (document.getElementById("areaUtilConstruida").value == "") {
		alert("Preencha o campo Area útil/construida");
		document.getElementById("areaUtilConstruida").focus();
		return false;
	}
	if (document.getElementById("areaTerreno").value == "") {
		alert("Preencha o campo Area do Terreno");
		document.getElementById("areaTerreno").focus();
		return false;
	}
	if (document.getElementById("valorVenda").value == "") {
		alert("Preencha o campo Valor da Venda");
		document.getElementById("valorVenda").focus();
		return false;
	}
	if (document.getElementById("valorLocacao").value == "") {
		alert("Preencha o campo Valor de Locacao");
		document.getElementById("valorLocacao").focus();
		return false;
	}
	if (document.getElementById("valorCondominio").value == "") {
		alert("Preencha o campo Valor de Condominio");
		document.getElementById("valorCondominio").focus();
		return false;
	}
	if (document.getElementById("valorIptuAnual").value == "") {
		alert("Preencha o campo Valor de Condominio");
		document.getElementById("valorIptuAnual").focus();
		return false;
	}
	if (document.getElementById("situacaoImovel").value == "") {
		alert("Selecione a Situacao do Imovel");
		document.getElementById("situacaoImovel").focus();
		return false;
	}
	if (document.getElementById("posicao").value == "") {
		alert("Selecione a Posicao");
		document.getElementById("posicao").focus();
		return false;
	}
}

function validaImovelPasso3() {
	if (document.getElementById("frentePredioMt").value == "") {
		alert("Preencha o campo Frente do predio");
		document.getElementById("frentePredioMt").focus();
		return false;
	}
	if (document.getElementById("numeroBlocos").value == "") {
		alert("Preencha o campo Nº de blocos");
		document.getElementById("numeroBlocos").focus();
		return false;
	}
	if (document.getElementById("numeroPisosBloco").value == "") {
		alert("Preencha o campo Nº de pisos p/ bloco");
		document.getElementById("numeroPisosBloco").focus();
		return false;
	}
	if (document.getElementById("unidadesPiso").value == "") {
		alert("Preencha o campo Unidades p/ piso");
		document.getElementById("unidadesPiso").focus();
		return false;
	}
	if (document.getElementById("elevadoresBloco").value == "") {
		alert("Preencha o campo Elevadores p/ bloco");
		document.getElementById("elevadoresBloco").focus();
		return false;
	}
	if (document.getElementById("flagGaragemBloco").value == "") {
		alert("Selecione o campo Garagem p/ bloco");
		document.getElementById("flagGaragemBloco").focus();
		return false;
	}
	if (document.getElementById("flagCoberturaBloco").value == "") {
		alert("Selecione o campo Cobertura p/ bloco");
		document.getElementById("flagCoberturaBloco").focus();
		return false;
	}
	if (document.getElementById("flagPiscinas").value == "") {
		alert("Selecione o campo Piscinas");
		document.getElementById("flagPiscinas").focus();
		return false;
	}
	if (document.getElementById("flagHidroginastica").value == "") {
		alert("Selecione o campo Hidroginastica");
		document.getElementById("flagHidroginastica").focus();
		return false;
	}
	if (document.getElementById("flagSalaoFestas").value == "") {
		alert("Selecione o campo Hidroginastica");
		document.getElementById("flagSalaoFestas").focus();
		return false;
	}
}

function validaImovelPasso4() {
}

function validaImovelPasso5() {
	if (document.getElementById("outrasCaracteristicas").value.length > 800) {
		alert("O campo Outras caracteristicas permite até 800 caracteres");
		document.getElementById("outrasCaracteristicas").focus();
		return false;
	}
}

function validaLogin() {
	if (document.getElementById("emailLogin").value == "") {
		alert("Preencha o campo Login");
		document.getElementById("emailLogin").focus();
		return false;
	}
	if (document.getElementById("senhaLogin").value == "") {
		alert("Preencha o campo Senha");
		document.getElementById("senhaLogin").focus();
		return false;
	}
}

function validaNumero(numero){
	if (isNaN(numero)) {
		alert("Formato inválido, digite apenas numeros");
		return false;
	}
	return true;
}

function validaCpf_Cnpj(action) {
	if (document.getElementById("cpf_cnpj").value == "") {
		if ( document.getElementById("flagCpfCnpj").checked ) { //pessoa fisica
			alert("Preencha o campo CPF");
		}
		else {
			alert("Preencha o campo CNPJ");
		}
		document.getElementById("cpf_cnpj").focus();
		return false;
	}
	if (validaNumero(document.getElementById("cpf_cnpj").value)) {
		if ( document.getElementById("flagCpfCnpj").checked ) { //pessoa fisica
			if (valida_cpf(document.getElementById("cpf_cnpj").value)) {
				if (action == "Validar") {
					alert("CPF valido");
				}
				return true;
			}
			else {
				alert("CPF invalido, por favor, corriga o numero fornecido");
				document.getElementById("cpf_cnpj").focus();
				return false;
			}
		}
		else {
			if (valida_cnpj(document.getElementById("cpf_cnpj").value)) {
				if (action == "Validar") {
					alert("CNPJ válido");
				}
				return true;
			}
			else {
				alert("CNPJ invalido, por favor, corriga o numero fornecido");
				document.getElementById("cpf_cnpj").focus();
				return false;
			}
		}
	}
}

function valida_cnpj(cnpj) {
    var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
    digitos_iguais = 1;
    if (cnpj.length < 14 && cnpj.length < 15)
        return false;
    for (i = 0; i < cnpj.length - 1; i++)
        if (cnpj.charAt(i) != cnpj.charAt(i + 1)) {
        	digitos_iguais = 0;
	        break;
        }
    if (!digitos_iguais) {
    	tamanho = cnpj.length - 2
        numeros = cnpj.substring(0,tamanho);
        digitos = cnpj.substring(tamanho);
        soma = 0;
        pos = tamanho - 7;
        for (i = tamanho; i >= 1; i--) {
			soma += numeros.charAt(tamanho - i) * pos--;
			if (pos < 2)
				pos = 9;
		}
        resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
        if (resultado != digitos.charAt(0))
        	return false;
        tamanho = tamanho + 1;
        numeros = cnpj.substring(0,tamanho);
        soma = 0;
        pos = tamanho - 7;
        for (i = tamanho; i >= 1; i--) {
			soma += numeros.charAt(tamanho - i) * pos--;
			if (pos < 2)
				pos = 9;
		}
        resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
        if (resultado != digitos.charAt(1))
        	return false;
        return true;
	}
    else
		return false;
}

function valida_cpf(cpf) {
    var numeros, digitos, soma, i, resultado, digitos_iguais;
    digitos_iguais = 1;
    if (cpf.length < 11)
        return false;
    for (i = 0; i < cpf.length - 1; i++)
        if (cpf.charAt(i) != cpf.charAt(i + 1)) {
			digitos_iguais = 0;
			break;
		}
    if (!digitos_iguais) {
		numeros = cpf.substring(0,9);
		digitos = cpf.substring(9);
		soma = 0;
        for (i = 10; i > 1; i--)
			soma += numeros.charAt(10 - i) * i;
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(0))
			return false;
		numeros = cpf.substring(0,10);
		soma = 0;
		for (i = 11; i > 1; i--)
			soma += numeros.charAt(11 - i) * i;
        resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
        if (resultado != digitos.charAt(1))
			return false;
        return true;
        }
    else
		return false;
}

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;
	}
}

function formataDataOnKeyDown(valor){
	var pass = valor.value;
	var expr = /[0123456789]/;

	for(i=0; i<pass.length; i++) {
	 var lchar = valor.value.charAt(i);
	 var nchar = valor.value.charAt(i+1);
	
	 if(i==0) {
		 if ((lchar.search(expr) != 0) || (lchar>3)) {
			 valor.value = "";
		 }
	 } else if(i==1) {
			if(lchar.search(expr) != 0) {
				var tst1 = valor.value.substring(0,(i));
				valor.value = tst1;
				continue;
			}
			if ((nchar != '/') && (nchar != '')) {
				var tst1 = valor.value.substring(0, (i)+1);
				
				if(nchar.search(expr) != 0)
					var tst2 = valor.value.substring(i+2, pass.length);
				else
					var tst2 = valor.value.substring(i+1, pass.length);
		
					valor.value = tst1 + '/' + tst2;
			}
		} else if (i==4) {
				if( lchar.search(expr) != 0) {
					var tst1 = valor.value.substring(0, (i));
					valor.value = tst1;
					continue;
				}
				if ((nchar != '/') && (nchar != '')){
					var tst1 = valor.value.substring(0, (i)+1);
					
					if(nchar.search(expr) != 0)
						var tst2 = valor.value.substring(i+2, pass.length);
					else
						var tst2 = valor.value.substring(i+1, pass.length);

					valor.value = tst1 + '/' + tst2;
				}
		}
		if (i>=6) {
			if (lchar.search(expr) != 0) {
				var tst1 = valor.value.substring(0, (i));
				valor.value = tst1;
			}
		}
	}
	if (pass.length>10)
		valor.value = valor.value.substring(0, 10);
		
	return true;
}

function validaData(data){
	array_mes = ["02","04","06","09","11"];
	erro = "Data Inválida"
	form = document.getElementById(data);
	dia = form.value.charAt(0)+form.value.charAt(1);
	mes = form.value.charAt(3)+form.value.charAt(4);
	year = form.value.charAt(6)+form.value.charAt(7)+form.value.charAt(8)+form.value.charAt(9); 
	texto = form.value;

	if(parseInt(texto.substr(6,10)) % 4==0)
		ano = "bissexto";
	else
		ano = "";

	if (dia<1 || dia>31 || mes<1 || mes>12) {
		alert(erro);
		return false;
	}

	for(x=0; x<array_mes.length; x++){
		if(mes == array_mes[x] && dia>30){
			alert(erro);
			return false;
		}
	}
	 
	if (ano!="bissexto" && dia>28 && mes==2){
		alert(erro);
		return false;
	}

	if (ano=="bissexto" && dia>29 && mes==2){
		alert(erro);
		return false;
	}

	return true;
}

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;
}

//Formata número tipo moeda usando o evento onKeyDown
function Formata(campo,tammax,teclapres,decimal) {
	var tecla = teclapres.keyCode;
	vr = Limpar(campo.value,"0123456789");
	tam = vr.length;
	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 );
		}
	}
}

var numbers = '1234567890';
var barra 	= '/';
function alpha(e,allow) {
    var k;
    
    //delete key & "."
    //if ( e.keyCode == 46 ) return true
    
    var charCode = (e.which) ? e.which : e.keyCode;
    if (charCode == 9 || charCode == 8 || charCode == 13)
        return true;

    k=document.all?parseInt(e.keyCode): parseInt(e.which);
    return (allow.indexOf(String.fromCharCode(k))!=-1);
}

function enviando() {
	Enviando.innerHTML = "Enviando ...";
}

function validaCampos() {
	if (document.getElementById("nome").value == "") {
		alert("Preencha o campo Nome");
		document.getElementById("nome").focus();
		return false;
	}
	if (document.getElementById("email").value == "") {
		alert("Preencha o campo Email");
		document.getElementById("email").focus();
		return false;
	}
	if (!checkMail(document.getElementById("email").value)) {
    	alert("Email inválido");
    	document.getElementById("email").focus();
    	return false;
	}
        if (!(validaCpf_Cnpj("Cadastrar"))){
		document.getElementById("cpf_cnpj").focus();
		return false;
	}
	if (validaNumero(document.getElementById("docIdentidade").value)) {
		if (document.getElementById("docIdentidade").value == "") {
			alert("Preencha o campo Doc. Identidade");
			document.getElementById("docIdentidade").focus();
			return false;
		}
	}
	else{
		document.getElementById("docIdentidade").focus();
		return false;
	}
	if (document.getElementById("nacionalidade").value == "") {
		alert("Preencha o campo Nacionalidade");
		document.getElementById("nacionalidade").focus();
		return false;
	}
	if (document.getElementById("estCivil").value == "") {
		alert("Preencha o campo Est. Civil");
		document.getElementById("estCivil").focus();
		return false;
	}
	if (document.getElementById("profissao").value == "") {
		alert("Preencha o campo Profissao");
		document.getElementById("profissao").focus();
		return false;
	}
	if (document.getElementById("endereco").value == "") {
		alert("Preencha o campo Endereco");
		document.getElementById("endereco").focus();
		return false;
	}
	if (document.getElementById("complemento").value == "") {
		alert("Preencha o campo Complemento");
		document.getElementById("complemento").focus();
		return false;
	}
	if (validaNumero(document.getElementById("cep").value)) {
		if (document.getElementById("cep").value == "") {
			alert("Preencha o campo cep");
			document.getElementById("cep").focus();
			return false;
		}
	}
	else{
		document.getElementById("cep").focus();
		return false;
	}
	if (document.getElementById("estado").value == "") {
		alert("Selecione o Estado");
		document.getElementById("estado").focus();
		return false;
	}
	if (document.getElementById("cidade").value == "") {
		alert("Preencha o campo Cidade");
		document.getElementById("cidade").focus();
		return false;
	}
	if (document.getElementById("pais").value == "") {
		alert("Preencha o campo Pais");
		document.getElementById("pais").focus();
		return false;
	}
	if (document.getElementById("dataNascimento").value == "") {
		alert("Preencha o campo Data Nascimento");
		document.getElementById("dataNascimento").focus();
		return false;
	}
	if (!(validaData("dataNascimento"))){
		document.getElementById("dataNascimento").focus();
		return false;
	}
	if (document.getElementById("dddResidencial").value == "") {
		alert("Preencha o campo DDD Residencial");
		document.getElementById("dddResidencial").focus();
		return false;
	}
	if (document.getElementById("telResidencial").value == "") {
		alert("Preencha o campo Tel Residencial");
		document.getElementById("telResidencial").focus();
		return false;
	}
	if (document.getElementById("msg").value.length > 800) {
		alert("O campo menssagem permite ate 800 caracteres");
		document.getElementById("msg").focus();
		return false;
	}
}