
function desformataValor(valor){
	// Limpa a string
	valor = valor.toString().replace(".","");
	valor = valor.toString().replace(".","");
	valor = valor.toString().replace(".","");
	valor = valor.toString().replace(",",".");
	return valor;
}

function valorFormatado(valor){

	var vr = valor;
	
	// Arredonda o valor e fixa em duas casas decimais
	vr = ((Math.round(vr*100))/100);  
	vr = vr.toFixed(2);
		
	// Limpa a string
	vr = vr.toString().replace( "/", "" );
	vr = vr.toString().replace( "/", "" );
	vr = vr.toString().replace( ",", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	
	// Salva o tamanho da string para a comparacao
	var tam = vr.length;
		
	if ( tam <= 2 ){
		return vr; 
	}
	// Retorna o valor formatado
	if ( (tam > 2) && (tam <= 5) ){
		return vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); 
	}
	if ( (tam >= 6) && (tam <= 8) ){
		return vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
	}
	if ( (tam >= 9) && (tam <= 11) ){
		return 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) ){
		return 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) ){
		return 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 financiamento(){
	var ace = document.getElementById('total_acessorios').value;
	var ent = document.getElementById('total_entrada').value;
	var val = document.getElementById('total_tabela').value;
	
	// Limpa a string
	ent = desformataValor(ent);
	
	// Faz o calculo do financiamento
	document.getElementById('24x').value = valorFormatado( ( ((((ace*1) + (val*1)) - (ent*1))) * 0.051091) );
	document.getElementById('36x').value = valorFormatado( ( ((((ace*1) + (val*1)) - (ent*1))) * 0.037368) );
 	//document.getElementById('42x').value = valorFormatado( ( ((((ace*1) + (val*1)) - (ent*1))) * 0.033506) );
	document.getElementById('48x').value = valorFormatado( ( ((((ace*1) + (val*1)) - (ent*1))) * 0.030644) );
	document.getElementById('60x').value = valorFormatado( ( ((((ace*1) + (val*1)) - (ent*1))) * 0.026717) );
	
}

function todosAcessorios() {

	// Pega todos os campos do formulário
    campos = document.form.elements;
	var total = 0;
	// Verifica se o checkbox "todos" esta checado
	if(document.form.todos.checked == true){
	    for (x=0; x<campos.length; x++){
	      if (campos[x].type == "checkbox"){
	      	campos[x].checked = true;
	      	total = ((total*1) + (campos[x].value*1));
	      }
		}
	}
	// Se o checkbox "todos" não tiver checado
	else{
		for (x=0; x<campos.length; x++){
	      if (campos[x].type == "checkbox"){
	      	campos[x].checked = false;
	      	total = 0,00;
	      }
		}
	}

	document.getElementById('total_acessorios').value = total;
	document.getElementById('valor_acessorios').value = valorFormatado(total)
}


function calculaAcessorios(id){
	
	var elemento 	= (document.getElementById(id).value * 1);
	var total 		= (document.getElementById('total_acessorios').value * 1);
	
	if(document.getElementById(id).checked == true){
		total = ((total + elemento) * 1);
	}
	
	else {
		total = ((total - elemento) * 1);
	}

	document.getElementById('total_acessorios').value = total;
	document.getElementById('valor_acessorios').value = valorFormatado(document.getElementById('total_acessorios').value);
}

function alterarVersao(valor){
	
	document.getElementById('total_tabela').value = valor;
	document.getElementById('valor_tabela').value = valorFormatado(document.getElementById('total_tabela').value);
	document.getElementById('total_tabelaConsorcio').value = valor;
	document.getElementById('valor_tabelaConsorcio').value = valorFormatado(document.getElementById('total_tabela').value);
	document.getElementById('valorVersao').value = valorFormatado(document.getElementById('total_tabela').value);
}

function porcentagemLance(valor){
	
	// Limpa a string
	var lan = 	ent = desformataValor(valor);

	// Calcula a porcentagem
	var res =  (lan*100) / (document.getElementById('total_tabela').value * 1.14);
	// Arredonda o valor e fixa em duas casas decimais
	res = ((Math.round(res*100))/100);  
	res = res.toFixed(2);
	
	document.getElementById('lance_percent').value = res; 
}

function consorcio(){
	
	var lan = document.getElementById('lance').value;
	var val = document.getElementById('total_tabela').value;
	
	// Limpa a string
	lan = desformataValor(lan);

	var v24x = desformataValor(document.getElementById('24x').value) * 24;
	var v36x = desformataValor(document.getElementById('36x').value) * 36;
	//var v42x = desformataValor(document.getElementById('42x').value) * 42;
	var v50x = desformataValor(document.getElementById('48x').value) * 48;
	var v60x = desformataValor(document.getElementById('60x').value) * 60;
	
	// Faz o calculo do consorcio
	
	// 25 vezes
	var vp25 = (((val * 0.11) + (val * 1)) / 25);
	var prazo1 = (((vp25 * 25) - lan) / vp25).toFixed(0);
	document.getElementById('25xc').value 	= valorFormatado( vp25 );
	document.getElementById('prazo1').value = prazo1 + "x";
	document.getElementById('25xce').value 	= valorFormatado( (v24x - (vp25 * prazo1)) /* * -1*/ );
	
	// 36 vezes
	var vp36 	= (((val * 0.11) + (val * 1)) / 36);
	var prazo2 	= (((vp36 * 36) - lan) / vp36).toFixed(0);
	document.getElementById('36xc').value = valorFormatado( vp36 );
	document.getElementById('prazo2').value =  prazo2 + "x";
	document.getElementById('36xce').value 	= valorFormatado( (v36x - (vp36 * prazo2)) /* * -1*/ );
	
	// 50 vezes
	var vp50 	= (((val * 0.13) + (val * 1)) / 50);
	var prazo3	= (((vp50 * 50) - lan) / vp50).toFixed(0);
	document.getElementById('50xc').value = valorFormatado( vp50 );
	document.getElementById('prazo3').value = prazo3 + "x";
	document.getElementById('50xce').value 	= valorFormatado( (v50x - (vp50 * prazo3)) /* * -1*/ );
	
	// 60 vezes
	var vp60 	= (((val * 0.13) + (val * 1)) / 60);
	var prazo4 	= (((vp60 * 60) - lan) / vp60).toFixed(0);
	document.getElementById('60xc').value = valorFormatado( vp60 );
	document.getElementById('prazo4').value = prazo4 + "x";
	document.getElementById('60xce').value 	= valorFormatado( (v60x - (vp60 * prazo4)) /* * -1*/ );
	
	// 70 vezes
	var vp70 	= (((val * 0.15) + (val * 1)) / 70);
	var prazo5 	= (((vp70 * 70) - lan) / vp70).toFixed(0);
	document.getElementById('70xc').value = valorFormatado( vp70 );
	document.getElementById('prazo5').value =  prazo5 + "x";
	document.getElementById('70xce').value 	= valorFormatado( (v60x - (vp70 * prazo5)) /* * -1*/ );

	// 80 vezes	
	var vp80 	= (((val * 0.17) + (val * 1)) / 80);
	var prazo6 	= (((vp80 * 80) - lan) / vp80).toFixed(0);
	document.getElementById('80xc').value = valorFormatado( vp80 );
	document.getElementById('prazo6').value = prazo6 + "x";
	document.getElementById('80xce').value 	= valorFormatado( (v60x - (vp80 * prazo6)) /* * -1*/ );
	
	// 90 vezes
	var vp90 	= (((val * 0.20) + (val * 1)) / 90);
	var prazo7 	= (((vp90 * 90) - lan) / vp90).toFixed(0);
	document.getElementById('90xc').value = valorFormatado( vp90 );
	document.getElementById('prazo7').value = prazo7 + "x";
	document.getElementById('90xce').value 	= valorFormatado( (v60x - (vp90 * prazo7)) /* * -1*/ );

}
