jQuery.noConflict();
jQuery(document).ready(function(){
	
	jQuery("select[@name='cidade']").change(function(){
		carregaNossasLojas();
	});

	jQuery("#btnPesquisarNossasLojas").click(function(){
		jQuery.post(
			URL+'ajax',{
				cidade: jQuery("select[@name='cidade']").val(),
				bairro: jQuery("select[@name='bairro']").val(),
				tipo: 'pesquisarLojas'
			},
			function(response){
				document.getElementById('NLBusca').style.display = 'block';
				jQuery("#divListaBusca").html(response);
				window.location = '#NLBusca';
			}
		)
	});
	
	jQuery('#bairro').change(function(){
		if(jQuery("#endereco").val() != undefined)
			carregaEnderecos();
	});
	
	jQuery('#endereco').change(function(){
		carregaLoja();
	});
	
	
	jQuery("#cnpj").blur(function(){
		jQuery.post(
			URL+'ajax',{
				cnpj: jQuery("#cnpj").val(),
				tipo: 'pesquisaCnpj'
			},
			function(response){
				if(response == 'yes'){
					alert("CNPJ já cadastrado em nosso sistema.");
					jQuery("#cnpj").focus();
				}
			}
		)
	});
	
	jQuery("#cpf").blur(function(){
		if(validaCpf(jQuery("#cpf").val())){
			jQuery.post(
				URL+'ajax',{
					cpf: jQuery("#cpf").val(),
					tipo: 'pesquisaCpf'
				},
				function(response){
					if(response == 'yes'){
						alert("CPF já cadastrado em nosso sistema.");
						jQuery("#cpf").focus();
					}
				}
			)
		}else{
			alert("CPF não é válido.");
			jQuery("#cpf").focus();
		}
	});
	

	jQuery("#btnCadastrarLogin").click(function(){
		var error = '';
		if(jQuery("#cpfLogin").val().length == 0){
			error += 'Favor preencher o seu cpf.\n';
		}
		if(!validaCpf(jQuery("#cpfLogin").val())){
			error += 'CPF não é válido.\n';
		}
		if(jQuery("#cep").val().length < 1){
			error += 'Favor preencher o seu CEP.\n';
		}
		jQuery.post(
			URL+'ajax',{
				cpf: jQuery("#cpfLogin").val(),
				tipo: 'pesquisaCpf'
			},
			function(response){
				if(response == 'yes'){
					error = "CPF já cadastrado em nosso sistema.";
				}
				if(error.length > 1)
					alert(error);
				else
					jQuery('#formCadastrar').submit();
			}
		)
	})
	jQuery("#email").blur(function(){
		if(MODO == 'INCLUSAO' || (MODO != jQuery("#email").val() && MODO != 'INCLUSAO')){
			jQuery.post(
				URL+'ajax',{
					email: jQuery("#email").val(),
					tipo: 'validaEmail'
				},
				function(response){
					if(response == 'no' && jQuery("#email").val().length >= 10){
						alert("E-mail já cadastrado, favor informar outro e-mail, ou logar com seu email e senha para alterar dados do seu curriculo.");
						jQuery("#email").focus();
					}
				}
			)
		}
	})


	jQuery("#finalizarTrabalhe").click(function(){
		var error = "";
		var nome = jQuery("input[@name='nome']").val();
		var estadoCivil = jQuery("#estadoCivil").val();
		var sexo = jQuery("#sexo").val();
		var email = jQuery("input[@name='email']").val();
		var confEmail = jQuery("input[@name='confEmail']").val();
		var senha = jQuery("input[@name='senha']").val();
		var editSenha = jQuery("input[@name='editSenha']").val();
		var confSenha = jQuery("input[@name='confSenha']").val();
		var cep = jQuery("input[@name='cep']").val();
		var logradouro = jQuery("input[@name='logradouro']").val();
		var numero = jQuery("input[@name='numero']").val();
		var bairro = jQuery("input[@name='bairro']").val();
		var cidade = jQuery("input[@name='cidade']").val();
		var estado = jQuery("select[@name='estado']").val();
		var dddDadosPessoais = jQuery("input[@name='dddDadosPessoais']").val();
		var foneDadosPessoais = jQuery("input[@name='foneDadosPessoais']").val();
		var cargo = jQuery('#cargo').val();
		var area = jQuery("#area").val();
		var pretensao = document.getElementById('pretensao').value;
		var rgNumero = jQuery("input[@name='rgNumero']").val();
		var rgOrgao = jQuery("input[@name='rgOrgao']").val();
		var diaNasc = jQuery("select[@name='diaNascimento']").val();
		var mesNasc = jQuery("select[@name='mesNascimento']").val();
		var anoNasc = jQuery("select[@name='anoNascimento']").val();
		
		/*Validação dos campos de formação ensino fundamental*/
		var situacao0 		= jQuery("select[@name='situacao[0]']").val();
		var mesInicioCurso0 = jQuery("input[@name='mesInicioCurso[0]']").val();
		var anoInicioCurso0 = jQuery("input[@name='anoInicioCurso[0]']").val();
		var mesFimCurso0 	= jQuery("input[@name='mesFimCurso[0]']").val();
		var anoFimCurso0 	= jQuery("input[@name='anoFimCurso[0]']").val();
		/*var curso0 	= jQuery("input[@name='curso[0]']").val();
		var especializacao0	= jQuery("input[@name='especializacao[0]']").val();*/
		var instituicao0	= jQuery("input[@name='instituicao[0]']").val();
		
		/*Validação dos campos de formação ensino medio*/
		var situacao1 		= jQuery("select[@name='situacao[1]']").val();
		var mesInicioCurso1 = jQuery("input[@name='mesInicioCurso[1]']").val();
		var anoInicioCurso1 = jQuery("input[@name='anoInicioCurso[1]']").val();
		var mesFimCurso1 	= jQuery("input[@name='mesFimCurso[1]']").val();
		var anoFimCurso1 	= jQuery("input[@name='anoFimCurso[1]']").val();
		var curso1 	= jQuery("input[@name='curso[1]']").val();
		/*var especializacao1	= jQuery("input[@name='especializacao[1]']").val();*/
		var instituicao1	= jQuery("input[@name='instituicao[1]']").val();
		
		/*Validação dos campos de formação ensino superior*/
		var situacao2 		= jQuery("select[@name='situacao[2]']").val();
		var mesInicioCurso2 = jQuery("input[@name='mesInicioCurso[2]']").val();
		var anoInicioCurso2 = jQuery("input[@name='anoInicioCurso[2]']").val();
		var mesFimCurso2 	= jQuery("input[@name='mesFimCurso[2]']").val();
		var anoFimCurso2 	= jQuery("input[@name='anoFimCurso[2]']").val();
		var curso2 	= jQuery("input[@name='curso[2]']").val();
		var especializacao2	= jQuery("input[@name='especializacao[2]']").val();
		var instituicao2	= jQuery("input[@name='instituicao[2]']").val();
		
		var foco = null;
		
		if(nome.length <= 0){
			error += "Favor informar o seu nome.\n";
			if(foco === null)
			{
				foco = 'nome';
				window.location='#dadosPessoais';
			}
		}
		if(estadoCivil.length < 2){
			error += "Favor selecionar seu estado civil.\n";
			if(foco === null)
				foco = 'estadoCivil';
		}
		if(sexo.length < 1){
			error += "Favor selecionar seu sexo.\n";
			if(foco === null)
			{
				foco = 'sexo';
				window.location='#dadosPessoais';
			}
		}
		if(email.length <= 0 || !jcv_checkEmail(email)){
			error += "E-mail inválido.\n";
			if(foco === null)
			{
				foco = 'email';
				window.location='#dadosPessoais';
			}

		}
		if(email != confEmail){
			error += "E-mail e confirmação de email não conferem.\n";
			if(foco === null)
			{
				foco = 'email';
				window.location='#dadosPessoais';
			}

		}
		if((senha.length <= 0 || confSenha.length <= 0) && editSenha == 'no' ){
			error += "Senha e Confirmação de Senha devem ser informadas.\n";
			if(foco === null)
			{
				foco = 'senha';
					window.location='#dadosPessoais';
			}

		}
		if(senha != confSenha){
			error += "Senha e Confirmação de Senha não conferem.\n";
			if(foco === null)
			{
				foco = 'senha';
				window.location='#dadosPessoais';
			}
		}
		if(cep.length != 9 || cep == '_____-___'){
			error += "Favor informar o CEP.\n";
			if(foco === null)
			{
				foco = 'cep';
				window.location='#dadosPessoais';
			}

		}
		if(logradouro.length < 1){
			error += "Favor informar o logradouro.\n";
			if(foco === null)
			{
				foco = 'logradouro';
				window.location='#dadosPessoais';
			}
				
		}
		if(numero.length < 1){
			error += "Favor informar o numero.\n";
			if(foco === null)
			{
				foco = 'numero';
				window.location='#dadosPessoais';
			}

		}
		if(bairro.length < 1){
			error += "Favor informar o bairro.\n";
			if(foco === null)
			{
				foco = 'bairro';
				window.location='#dadosPessoais';
			}
		}
		if(cidade.length < 1){
			error += "Favor informar o cidade.\n";
			if(foco === null)
			{
				foco = 'cidade';
				window.location='#dadosPessoais';
			}

		}
		if(estado == null || estado.length != 2){
			error += "Favor selecionar um estado.\n";
			if(foco === null)
			{
				foco = 'estado';
				window.location='#dadosPessoais';
			}
		}
		if(dddDadosPessoais.length != 2){
			error += "Favor informar o DDD do seu telefone.\n";
			if(foco === null)
				foco = 'dddDadosPessoais';
		}
		if(dddDadosPessoais < 10){
			error += "DDD do seu telefone é inválido.\n";
			if(foco === null)
			{
				foco = 'dddDadosPessoais';
				window.location='#dadosPessoais';
			}
		}
		if(foneDadosPessoais.length != 9){
			error += "Favor informar o seu telefone.\n";
			if(foco === null)
			{
				foco = 'foneDadosPessoais';
				window.location='#dadosPessoais';
			}
		}
		if(cargo == 0){
			error += "Favor selecionar um cargo\n";
			if(foco === null)
			{
				foco = 'cargo';
				window.location='#infoIniciais';
			}
		}
		if(area == 0){
			error += "Favor selecionar uma àrea\n";
			if(foco === null)
			{
				foco = 'area';
				window.location='#infoIniciais';
			}
		}
		if(pretensao <= 0){
			error += "Favor informar sua pretenção salarial.\n";
			if(foco === null)
			{
				foco = 'pretensao';
				window.location='#infoIniciais';
			}
		}
		if(rgNumero.length <= 0){
			error += "Favor informar o numero do seu RG.\n";
			if(foco === null)
			{
				foco = 'rgNumero';
				window.location='#documentos';
			}
		}
		if(rgOrgao.length <= 0){
			error += "Favor informar o órgão expeditor do seu RG.\n";
			if(foco === null)
			{
				foco = 'rgOrgao';
				window.location='#documentos';
			}
				
		}
		if(diaNasc.length != 2 || mesNasc.length != 2 || anoNasc.length != 4){
			error += "Favor informar a sua data de nascimento.\n";
			if(foco === null)
			{
				foco = 'diaNascimento';
				window.location='#documentos';
			}
		}
		
		/*if(situacao0 == "")
			error += "Favor informar a situação da formação no ensino fundamental.\n";
		if(mesInicioCurso0.length <= 0 || mesInicioCurso0 == "MM")
			error += "Favor informar o mes de inicio da formação no ensino fundamental.\n";
		if(anoInicioCurso0.length <= 0 || anoInicioCurso0 == "AAAA")
			error += "Favor informar o ano de inicio da formação no ensino fundamental.\n";
		if(mesFimCurso0.length <= 0 || mesFimCurso0 == "MM")
			error += "Favor informar o mes de término da formação no ensino fundamental.\n";
		if(anoFimCurso0.length <= 0 || anoFimCurso0 == "AAAA")
			error += "Favor informar o ano de término da formação no ensino fundamental.\n";
		if(instituicao0.length <= 0)
			error += "Favor informar a instituição da formação no ensino fundamental.\n";
		
		if(situacao1 == "")
			error += "Favor informar a situação da formação no ensino médio.\n";
		if(mesInicioCurso1.length <= 0 || mesInicioCurso1 == "MM")
			error += "Favor informar o mes de inicio da formação no ensino médio.\n";
		if(anoInicioCurso1.length <= 0 || anoInicioCurso1 == "AAAA")
			error += "Favor informar o ano de inicio da formação no ensino médio.\n";
		if(mesFimCurso1.length <= 0 || mesFimCurso1 == "MM")
			error += "Favor informar o mes de término da formação no ensino médio.\n";
		if(anoFimCurso1.length <= 0 || anoFimCurso1 == "AAAA")
			error += "Favor informar o ano de término da formação no ensino médio.\n";
		if(curso1.length <= 0)
			error += "Favor informar o curso da formação no ensino médio.\n";
		if(instituicao1.length <= 0)
			error += "Favor informar a instituição da formação no ensino médio.\n";
		
		if(situacao2 == "")
			error += "Favor informar a situação da formação no ensino superior.\n";
		if(mesInicioCurso2.length <= 0 || mesInicioCurso2 == "MM")
			error += "Favor informar o mes de inicio da formação no ensino superior.\n";
		if(anoInicioCurso2.length <= 0 || anoInicioCurso2 == "AAAA")
			error += "Favor informar o ano de inicio da formação no ensino superior.\n";
		if(mesFimCurso2.length <= 0 || mesFimCurso2 == "MM")
			error += "Favor informar o mes de término da formação no ensino superior.\n";
		if(anoFimCurso2.length <= 0 || anoFimCurso2 == "AAAA")
			error += "Favor informar o ano de término da formação no ensino superior.\n";
		if(curso2.length <= 0)
			error += "Favor informar o curso da formação no ensino superior.\n";
		if(especializacao2.length <= 0)
			error += "Favor informar a especialização da formação no ensino superior.\n";
		if(instituicao2.length <= 0)
			error += "Favor informar a instituição da formação no ensino superior.\n";*/
		
		var stSituacao = 0;
		var stDate = 0;
		var stDataFim = 0;
		for(i=0; i < formacao+1; i++)
		{
			if(jQuery("input[@name='inserir_"+ i +"']").val() == 1){
				if(jQuery("select[@name='escolaridade["+ i +"]']").val().length <= 0 && stSituacao == 0){
					error += "Favor informar o Grau de Ensino das formações.\n";
					stSituacao = 1;
					if(foco === null)
					{
						foco = 'escolaridade['+ i +']';
						window.location='#formacoesCurriculo';
					}
				}
				if(jQuery("select[@name='situacao["+ i +"]']").val().length <= 0 && stSituacao == 0){
					error += "Favor informar a situação das formações.\n";
					stSituacao = 1;
					if(foco === null)
					{
						foco = 'situacao['+ i +']';
						window.location='#formacoesCurriculo';
					}
				}
				if(jQuery("select[@name='mesInicioCurso["+ i +"]']").val() == "MÉS" || jQuery("select[@name='anoInicioCurso["+ i +"]']").val() == 'ANO'){
					error += "Favor informar a data de início da sua formação.\n";
					if(foco === null)
					{
						foco = 'mesInicioCurso['+ i +']';
						window.location='#formacoesCurriculo';
					}
				}
				if(jQuery("select[@name='mesFimCurso["+ i +"]']").val() == "MÉS" || jQuery("select[@name='anoFimCurso["+ i +"]']").val() == 'ANO'){
					if(i == 0 || i == 1 || (jQuery("select[@name='situacao["+ i +"]']").val() == 'completo' && jQuery("select[@name='escolaridade["+ i +"]']").val() == 'Superior')){
						error += "Favor informar a data final da sua formação.\n";
						if(foco === null)
						{
							foco = 'mesFimCurso['+ i +']';
							window.location='#formacoesCurriculo';
						}
					}
				}
				if(verifyDate(null, jQuery("select[@name='mesInicioCurso["+ i +"]']").val(), jQuery("select[@name='anoInicioCurso["+ i +"]']").val()) && stDate == 0){
					error += "A data de início das formações deve ser menor que a data atual.\n";
					stDate = 1;
					if(foco === null)
					{
						foco = 'mesInicioCurso['+ i +']';
						window.location='#formacoesCurriculo';
					}
				}
				if(((jQuery("select[@name='anoInicioCurso["+ i +"]']").val()+""+jQuery("select[@name='mesInicioCurso["+ i +"]']").val()) - (jQuery("select[@name='anoFimCurso["+ i +"]']").val()+""+jQuery("select[@name='mesFimCurso["+ i +"]']").val())) > 0 && stDataFim == 0){
					error += "A data de início das formações deve ser menor que a data final.\n";
					stDataFim = 1;
					if(foco === null)
					{
						foco = 'mesInicioCurso['+ i +']';
						window.location='#formacoesCurriculo';
					}
				}
				if(jQuery("input[@name='escolaridade["+ i +"]']").val() == 'Superior' && jQuery("input[@name='curso["+ i +"]']").val().length <= 0){
					error += "Favor informar o curso da formação no ensino superior.\n";
					if(foco === null)
					{
						foco = 'escolaridade['+ i +']';
						window.location='#formacoesCurriculo';
					}
				}
				if(jQuery("input[@name='instituicao["+ i +"]']").val().length <= 0){
					error += "Favor informar a instituição da sua formação.\n";
					if(foco === null)
					{
						foco = 'instituicao['+ i +']';
						window.location='#formacoesCurriculo';
					}
				}
			}
		}

		var stDate = 0;
		var stDataFim = 0;
		for(i=0; i < experiencia+1; i++){
			if(jQuery("#experienciaInserir_"+ i +"").val() == 1){
				if(jQuery("input[@name='nomeEmpresa["+ i +"]']").val().length <= 0){
					error += "Favor informar o nome da empresa em que trabalhou.\n";
					if(foco === null)
					{
						foco = 'nomeEmpresa['+ i +']';
						window.location='#novaExperiencia	';
					}
				}
				
				if(jQuery("input[@name='dddEmpresa["+ i +"]']").val().length <= 0){
					error += "Favor informar o DDD da empresa em que trabalhou.\n";
					if(foco === null)
					{
						foco = 'dddEmpresa['+ i +']';
						window.location='#novaExperiencia	';
					}
				}
				if(jQuery("input[@name='foneEmpresa["+ i +"]']").val().length <= 0){
					error += "Favor informar o telefone da empresa em que trabalhou.\n";
					if(foco === null)
					{
						foco = 'foneEmpresa['+ i +']';
						window.location='#novaExperiencia	';
					}
				}
				if(jQuery("input[@name='cargoEmpresa["+ i +"]']").val().length <= 0){
					error += "Favor informar o cargo atribuido na empresa em que trabalhou.\n";
					if(foco === null)
					{
						foco = 'cargoEmpresa['+ i +']';
						window.location='#novaExperiencia	';
					}
				}
				if(jQuery("input[@name='remuneracao["+ i +"]']").val().length <= 0 || jQuery("input[@name='remuneracao["+ i +"]']").val() < 1){
					error += "Favor informar a sua remuneração na(s) empresa(s) informadas(s).\n";
					if(foco === null)
					{
						foco = 'remuneracao['+ i +']';
						window.location='#novaExperiencia	';
					}
				}
				if(jQuery("select[@name='mesInicioEmpresa["+ i +"]']").val() == "MÉS" || jQuery("select[@name='anoInicioEmpresa["+ i +"]']").val() == 'ANO'){
					error += "Favor informar a data de início das suas atividades na empresa.\n";
					if(foco === null)
					{
						foco = 'mesInicioEmpresa['+ i +']';
						window.location='#novaExperiencia	';
					}
				}
				if(verifyDate(null, jQuery("select[@name='mesInicioEmpresa["+ i +"]']").val(), jQuery("select[@name='anoInicioEmpresa["+ i +"]']").val()) && stDate == 0){
					error += "A data de início na empresa deve ser menor que a data atual.\n";
					stDate = 1;
					if(foco === null)
					{
						foco = 'mesInicioEmpresa['+ i +']';
						window.location='#novaExperiencia	';
					}
				}
				if(((jQuery("select[@name='anoInicioEmpresa["+ i +"]']").val()+""+jQuery("select[@name='mesInicioEmpresa["+ i +"]']").val())-(jQuery("select[@name='anoFimEmpresa["+ i +"]']").val()+""+jQuery("select[@name='mesFimEmpresa["+ i +"]']").val())) > 0 && stDataFim == 0){
					error += "A data de início na empresa deve ser menor que a data final.\n";
					stDataFim = 1;
					if(foco === null)
					{
						foco = 'anoInicioEmpresa['+ i +']';
						window.location='#novaExperiencia	';
					}
				}
				if(jQuery("textarea[@name='atividade["+ i +"]']").val().length < 200){
					error += "Favor informar um descrição das atividade desenvolvidas na empresa em que trabalhou. No mínimo 200 caracteres.\n";
					if(foco === null)
					{
						foco = 'atividade['+ i +']';
						window.location='#novaExperiencia	';
					}
				}
			}
		}

		if(error.length > 0){
			alert(error);
			document.getElementById(foco).focus();
		}else{
			document.getElementById('frmTrabalheConosco').submit();
		}
	});
})

	function carregaEnderecos(){
		jQuery.post(
			URL+'ajax',{
				cidade: jQuery("select[@name='cidade']").val(),
				bairro: jQuery("select[@name='bairro']").val(),
				tipo: 'pesquisarEndereco'
			},
			function(response){
				if(response.length > 0){
					jQuery("select[@name='endereco']").find('option').remove().end().append(response)
					document.getElementById("endereco").options[0].selected = true;
				}
				else
				{
					jQuery("select[@name='endereco']").find('option').remove().end().append("<option value='0'>Nenhum endereço localizado</option>")
					document.getElementById("endereco").options[0].selected = true;
				}
				carregaLoja();
			}
		)
	}
	
	function carregaLoja(){
		jQuery.post(
			URL+'ajax',{
				cidade: jQuery("select[@name='cidade']").val(),
				bairro: jQuery("select[@name='bairro']").val(),
				idLoja: jQuery("select[@name='endereco']").val(),
				tipo: 'getLoja'
			},
			function(response){
				jQuery("#spanLoja").html(response);
				jQuery("#loja").val(response);
			}
		)
	};
	
	function carregaNossasLojas(){
		jQuery.post(
			URL+'ajax',{
				cidade: jQuery("select[@name='cidade']").val(),
				tipo: 'cidade'
			},
			function(response){
				jQuery("select[@name='bairro']").find('option').remove().end().append(response)
				document.getElementById('bairro').options[0].selected = true;
				//if(jQuery("#endereco").val() != undefined){
					carregaEnderecos();
				//}
			}
		)
	}

	function loadFormacoes(){
		var esc_0 = document.getElementById("escolaridade_0");
		var esc_1 = document.getElementById("escolaridade_1");

		//var esc_2 = document.getElementById("escolaridade_2");
		
		for(i=esc_0.options.length;i>=0;i--){
			esc_0.remove(i);
		}
		for(i=esc_1.options.length;i>=0;i--){
			esc_1.remove(i);
		}
		//for(i=esc_2.options.length;i>=0;i--){
		//		esc_2.remove(i);
		//}
		
		var fundamental=document.createElement('option');
		fundamental.text= 'Fundamental';
		fundamental.value='Fundamental';
		try{
	    	esc_0.add( fundamental , null);
	    }catch(ex){
	    	esc_0.add( fundamental );
	    }

	    var medio=document.createElement('option');
		medio.text= 'Médio';
		medio.value='Medio';
		try{
	    	esc_1.add( medio , null);
	    }catch(ex){
	    	esc_1.add( medio );
	    }
	    
	    
	    //document.getElementById('mesInicioCurso[0]').selectedIndex = 0;
	    //document.getElementById('anoInicioCurso[0]').selectedIndex = 0;
	    //document.getElementById('mesFimCurso[0]').selectedIndex = 0;
	    //document.getElementById('anoFimCurso[0]').selectedIndex = 0;
	    /*var superior=document.createElement('option');
		superior.text= 'Superior';
		superior.value='Superior';
		try{
	    	esc_2.add( superior , null);
	    }catch(ex){
	    	esc_2.add( superior );
	    }
	    
	    faddingDiv('divFadding', 60, 1, 0);
	    faddingDiv('divFaddingImg', 60, 1, 0);*/
		
	}
	
	function formataValor(campo) {
		campo.value = filtraCampo(campo);
		vr = campo.value;
		tam = vr.length;
	
		if ( tam <= 2 ){ 
	 		campo.value = vr ; }
	 	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 <= 18) ){
	 		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 ) ;}
	 		
	}
	
	// limpa todos os caracteres especiais do campo solicitado
	function filtraCampo(campo){
		var s = "";
		var cp = "";
		vr = campo.value;
		tam = vr.length;
		for (i = 0; i < tam ; i++) {  
			if (vr.substring(i,i + 1) != "/" && vr.substring(i,i + 1) != "-" && vr.substring(i,i + 1) != "."  && vr.substring(i,i + 1) != "," ){
			 	s = s + vr.substring(i,i + 1);}
		}
		campo.value = s;
		return cp = campo.value
	}

	function mascaraTelefone(obj, e)
	{
		var tecla=(window.event)?event.keyCode:e.which;
		
	    if((tecla > 47 && tecla < 58))
		{
			var separador = '-';
			if (obj.value.length == 4)
				obj.value = obj.value + separador;		
		}
		else
		{
    		if ((tecla != 8) && (tecla != 0))
				return false;
    		else
				return true;
    	}

	}
	

	function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
		var sep = 0;
		var key = '';
		var i = j = 0;
		var len = len2 = 0;
		var strCheck = '0123456789';
		var aux = aux2 = '';
		//alert(("Seu navegador é " + navigator.appName + " || " + navigator.appVersion ));
		if(navigator.appName == 'Microsoft Internet Explorer')
			var whichCode = e.keyCode;
		else
			var whichCode = e.which;
			
		if (whichCode == 13 || whichCode == 8 || e.keyCode == 9)
			return true;

		key = String.fromCharCode(whichCode); // Valor para o código da Chave

		if (strCheck.indexOf(key) == -1)
			return false; // Chave inválida

		len = objTextBox.value.length;

		for(i = 0; i < len; i++)
			if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal))
				break;

		aux = '';

		for(; i < len; i++)
			if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) 
				aux += objTextBox.value.charAt(i);

		aux += key;
		len = aux.length;

		if (len == 0) objTextBox.value = '';

		if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;

		if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;

		if (len > 2 && len <= 8) {
			aux2 = '';
			for (j = 0, i = len - 3; i >= 0; i--) {
				if (j == 3) {
					aux2 += SeparadorMilesimo;
					j = 0;
				}
				aux2 += aux.charAt(i);
				j++;
			}
			objTextBox.value = '';
			len2 = aux2.length;

			for (i = len2 - 1; i >= 0; i--)
				objTextBox.value += aux2.charAt(i);

			objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
		}
		return false;
	}
