// JavaScript Document
<!--
//==================================== Arquivo com todos Scripts usados no Simulador ================================
//EXECUTA RELOAD
function carrega(){ 
 eval("location='simform.asp?clienteid="+document.form.clienteid.value+"&id="+document.form.id.options[document.form.id.selectedIndex].value+"&sistema="+document.form.sistema.value+"&idade="+document.form.idade.value+"&investimento="+document.form.investimento.value+"&avaliacao="+document.form.avaliacao.value+"&finaciamento="+moeda2float(document.getElementById('v_aval').value)+moeda2float(document.getElementById('v_itbi').value)+moeda2float(document.getElementById('v_teo').value)+moeda2float(document.getElementById('v_reg').value)+moeda2float(document.getElementById('v_cert').value)+moeda2float(document.getElementById('v_outros').value)+moeda2float(document.form.finaciamento.value)+"&prestacoes="+document.form.prestacoes.value+"&indice="+document.form.indice.value+"&tipo="+document.form.tipoval.value+"&nome="+document.form.nome.value+"&cpf="+document.form.cpf.value+"'");
 //alert("location='simform.asp?clienteid="+document.form.clienteid.value+"&id="+document.form.id.options[document.form.id.selectedIndex].value+"&sistema="+document.form.sistema.value+"&idade="+document.form.idade.value+"&investimento="+document.form.investimento.value+"&avaliacao="+document.form.avaliacao.value+"&finaciamento="+financiamento+"&prestacoes="+document.form.prestacoes.value+"&indice="+document.form.indice.value+"&tipo="+document.form.tipoval.value+"&nome="+document.form.nome.value+"&cpf="+document.form.cpf.value+"'");

}

//LIMPA CAMPOS
function limpar(){   
	window.location.reload()
}
//
function currencyFormat(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; // Enter
	key = String.fromCharCode(whichCode); // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false; // Not a valid key
	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 replacer(entry) {
	num = "" + entry; // numorary holder	
	while (num.indexOf('.')>-1) {
		pos= num.indexOf('.');
		num = "" + (num.substring(0, pos) + '' + 
		num.substring((pos + 1), num.length));
	}	
	while (num.indexOf(',')>-1) {
		pos= num.indexOf(',');
		num = "" + (num.substring(0, pos) + '.' + 
		num.substring((pos + 1), num.length));
	}	
	return num.valueOf();
}


//====================================INÍCIO DAS VALIDAÇÕES DOS VALORES INSERIDOS NO FORMULARIO ===============================

//CALCULA VALOR DA AVALIAÇÃO NECESSÁRIA PARA DETERMINADO FINANCIAMENTO
function calculaaval(financ,perc,avalmin,avalmax,finacmin,finacmax){
	if(f_test_valfinacmin(finacmin)){	
		alert("O valor MÍNIMO para Financiamento é de "+reais(finacmin)+" !");
	}else{
		if(f_test_valfinacmax(finacmax)){	
					alert("O valor MÁXIMO para Financiamento é de "+reais(finacmax)+" !");
		}else{
			tmp=eval(financ)/eval(perc);
			if(f_test_valavalmin(avalmin)){tmp=avalmin}
			if(f_test_valavalmax(avalmax)){tmp=avalmax}
			alert("O valor de avalição necessário é de "+reais(tmp));	
		}
	}
}

//CALCULA PRAZO MAXIMO PARA DETERMINADA IDADE
function calcprazo(pmin,pmax,anos,meses,imax,idade) {
	if (idade=="") {
		alert("O campo Idade não foi preenchido !");
	}
	else
	{
	
		//calcula o prazo max
		idade=(eval(anos)*12)+eval(meses)
		prazomx=(imax*12)-idade

		//se o prazo for negativo seta para 0
		if (prazomx<0){ prazomx=0}
		
		//seta para 12 caso o prazo min seja nulo
		if (pmin>0) {
			prazomn=pmin
		} else {
			prazomn=12
		}

		if (prazomx>pmin) {
		//se prazo calculado for maior que o prazo max do produto... 
			if (prazomx>pmax) {
				alert("O prazo MÁXIMO para esse cliente é de "+pmax+" meses.");
			} else {
				alert("O prazo MÁXIMO para esse cliente é de "+prazomx+" meses.");
			}	
		} else {
			alert("Não é possível conceder financimento a este cliente!\nO prazo MÍNIMO para esse produto é de "+prazomn+" meses.");
		}	
	}	
}

//CALCULA IDADE COM MESES ATRAVÉS DA DATA DE NASCIMENTO
function calcage(dia,mes,ano) {
	if (dia!='' && mes!='' && ano!=''){
		thedate = new Date()
		mm = eval(mes)
		dd = eval(dia)
		yy = eval(ano)
		
		mm2 = thedate.getMonth() + 1
		dd2 = thedate.getDate()
		yy2 = thedate.getYear()
		
		if (yy2 < 1000) {
			yy2 = yy2 + 1900 
		}
		
		yourage = yy2 - yy
		
		
		if (mm2 < mm) {
		yourage = yourage - 1; 
		meses = (12-mm)+mm2
		}
		
		if (mm2 == mm) {
			meses = 0;
			if (dd2 < dd) {
			yourage = yourage - 1; }
		}
		
		if (mm2 > mm) {
		meses = mm2-mm
		}
		
		document.form.idade.value=yourage;
		document.form.meses.value=meses;
		
	}	
}

// VALIDAÇÃO DE CPF
function Cpf(valor){
block=false;
	if(valor.value==''){
		block=true;
		//alert('Por favor informe o CPF ou CNPJ');
	}
	else{
		pfValidaCPF_CGC(valor, 'CPF ou CNPJ inválido')
	}

}

function pfValidaCPF_CGC(msCPF_CGC, msMSG){
	if (!(checa(msCPF_CGC.value, msMSG))){
		return false;
	}
	else{ 
		return true;
	}
}

function checa(msCPF_CGC, msMSG){
	if ((msCPF_CGC.length != 14) && (msCPF_CGC.length !=11)){
		alert(msMSG);
		block=true;
		return false;
	}
  
	if ((!(modulo(msCPF_CGC.substring(0,msCPF_CGC.length - 2)).toString()+modulo(msCPF_CGC.substring(0,msCPF_CGC.length - 1)).toString() == msCPF_CGC.substring(msCPF_CGC.length - 2,msCPF_CGC.length))) && (modulo_cic(msCPF_CGC.substring(0,msCPF_CGC.length - 2)) + "" + modulo_cic(msCPF_CGC.substring(0,msCPF_CGC.length - 1)) != msCPF_CGC.substring(msCPF_CGC.length - 2,msCPF_CGC.length))){
		alert(msMSG);
		block=true;
		return false;
	}

}

function modulo(msCPF_CGC){
	soma=0;
	ind=2;
	
	for(pos=msCPF_CGC.length-1;pos>-1;pos=pos-1){
		soma = soma + (parseInt(msCPF_CGC.charAt(pos)) * ind);
		ind++;
	  
		if(msCPF_CGC.length>11){ 
			if(ind>9) ind=2; 
		}
	}
	
	resto = soma - (Math.floor(soma / 11) * 11);
		 
	if(resto < 2){ 
		return 0; 
	}
	else{ 
		return (11 - resto); 
	}
}

function modulo_cic(msCPF_CGC){
	soma=0;
	ind=2;

	for(pos=msCPF_CGC.length-1;pos>-1;pos=pos-1){
		 soma = soma + (parseInt(msCPF_CGC.charAt(pos)) * ind);
		 ind++;
		 
		 if(msCPF_CGC.length>11){	
			if(ind>9) ind=2; 
		 }
	}				
	
	resto = soma - (Math.floor(soma / 11) * 11);
		  
	if(resto < 2){	
		return 0; 
	}
	else{ 
		return 11 - resto;
	}
}



//====================================FIM DAS VALIDAÇÕES DOS VALORES INSERIDOS NO FORMULARIO ===============================

//==================================== INÍCIO DAS CONSISTÊNCIAS DO FINANCIAMENTO ================================

var retorno=true;

function f_blank(campo,txt){
	if (campo.value=="") {
		alert("O campo "+txt+" não foi preenchido !");
		campo.focus();
		retorno=false;
	}
}

function val_cidade(){
		var indexSelect = document.getElementById("cidadeid").selectedIndex;
		var valueSelected = form.cidadeid.options[indexSelect ].value;
		
		if (valueSelected=0){
			alert("A cidade não foi informada!");
			document.form.cidadeid.focus();
			retorno=false;
		}		
}

function f_prazomin(prazomin){
		if (document.form.prestacoes.value < prazomin){
			alert("O prazo MÍNIMO para contratação é de "+prazomin+" !");
			document.form.prestacoes.focus();
			retorno=false;
		}		
}		

function f_prazomax(prazomax){
		if (document.form.prestacoes.value > prazomax){
			alert("O prazo MÁXIMO de financiamento é de "+prazomax+" !");
			document.form.prestacoes.focus();
			retorno=false;
		}		
}

function f_valavalmin(avalmin){
		if (f_test_valavalmin(avalmin)){
			alert("O valor MÍNIMO para a avaliação é de "+reais(avalmin)+" !");
			document.form.avaliacao.focus();
			retorno=false;
		}		
}		

function f_test_valavalmin(avalmin){
	return (eval(replacer(document.form.avaliacao.value)) < eval(avalmin))
}

function f_valavalmax(avalmax){
		if (f_test_valavalmax(avalmax)){
			alert("O valor MÁXIMO para a avaliação é de "+reais(avalmax)+" !");
			document.form.avaliacao.focus();
			retorno=false;
		}		
}

function f_test_valavalmax(avalmax){
	return (eval(replacer(document.form.avaliacao.value)) > eval(avalmax))
}

function f_valfinacmin(financmin){
		if (f_test_valfinacmin(financmin)){
			alert("O valor MÍNIMO para Financiamento é de "+reais(financmin)+" !");
			document.form.finaciamento.focus();
			retorno=false;
		}		
}		

function f_test_valfinacmin(financmin){
	return (eval(replacer(moeda2float(document.getElementById('v_teo').value)+moeda2float(document.getElementById('v_outros').value)+moeda2float(document.form.finaciamento.value))) < eval(financmin))
}

function f_valfinacmax(financmax){
		if (f_test_valfinacmax(financmax)){
			alert("O valor MÁXIMO para Financianento é de "+reais(financmax)+" !");
			document.form.finaciamento.focus();
			retorno=false;
		}		
}	

function f_test_valfinacmax(financmax){
	return (eval(replacer(moeda2float(document.getElementById('v_teo').value)+moeda2float(document.getElementById('v_outros').value)+moeda2float(document.form.finaciamento.value))) > eval(financmax))
}

function f_financmin(financmin){
		if ((replacer(moeda2float(document.getElementById('v_teo').value)+moeda2float(document.getElementById('v_outros').value)+moeda2float(document.form.finaciamento.value)) / replacer(document.form.avaliacao.value)) < financmin){
			alert("Financiamento é INFERIOR ao limite MÍNIMO de "+financmin*100+"% da avaliação!");
			document.form.finaciamento.focus();
			retorno=false;
		}		
}		


function f_financmax(financmax){
		if ((replacer(moeda2float(document.getElementById('v_teo').value)+moeda2float(document.getElementById('v_outros').value)+moeda2float(document.form.finaciamento.value)) / replacer(document.form.avaliacao.value)) > financmax){
			alert("Financiamento ULTRAPASSA o limite MÁXIMO de "+financmax*100+"% da avaliação!");
			document.form.finaciamento.focus();
			retorno=false;
		}		
}		

function f_idademin(idademin){
		var hoje= new Date()
		if (((eval(document.form.idade.value)*12)+eval(document.form.prestacoes.value)) < eval(idademin*12)){
			if (idademin.indexOf(".") > 0){
				ponto=eval(idademin.indexOf("."));
				idade=idademin.substring(0,ponto)+' anos e';
				txt=' '+(eval(idademin.substring(eval(ponto+1),idademin.lenght))*1.2)+' meses';
				idadetxt=idade+txt;
			}else
			{
				idadetxt=idademin+' anos';
			}
			alert("A idade somada ao prazo ULTRAPASSA "+idadetxt+" !");
			document.form.prestacoes.focus();
			retorno=false;
		}		
}		

function f_idademax(idademax){
		var hoje= new Date()
		if (((eval(document.form.idade.value)*12)+eval(document.form.prestacoes.value)) > eval(idademax*12)){			
			if (idademax.indexOf(".") > 0){				
				ponto=eval(idademax.indexOf("."));
				idade=idademax.substring(0,ponto)+' anos e';
				txt=' '+(eval(idademax.substring(eval(ponto+1),idademax.lenght))*1.2)+' meses';
				idadetxt=idade+txt;
			}else{
				idadetxt=idademax+' anos';				
			}
			prazomx=(eval(idademax)-eval(document.form.idade.value))*12;
			alert("A idade somada ao prazo ULTRAPASSA "+idadetxt+" !\nO prazo MÁXIMO para este cliente é de "+prazomx+".");
			document.form.prestacoes.focus();
			retorno=false;
		}
}

function f_sist(sist){								
		if (document.form.sistema.value == sist){
			alert("Atualmente NÃO estamos financiando no sistema "+sist+" !");
			document.form.sistema.focus();
		}	
}

function autotab(original,destination){
	if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
		destination.focus()
}

//==================================== FIM DAS CONSISTÊNCIAS DO FINANCIAMENTO ================================				
-->