
/************** vrai leasing **************/
$(function() {
	$('#v_step2').hide(); 
	$('#v_step3').hide(); 

	$('#v_step1 .next').click(function() {
		if ($("#v1").validate().form()) {
		
			$('#v_step1').hide();
			$('#v_step2').show();
			
			v_calcul_valeur_residuelle();
		}
	});
	
	$('#v_step2 .next').click(function() {
		if ($("#v2").validate().form()) {
			$('#v_step2').hide();
			$('#v_step3').show();
			
			v_calcul_mensualites();
		}
	});

	$('#v_step2 .back').click(function() {
		$('#v_step2').hide(); 
		$('#v_step1').show(); 
	});
	
	$('#v_step3 .back').click(function() {
		$('#v_step3').hide(); 
		$('#v_step2').show(); 
	});	

	$('#v1').validate({
		errorPlacement: function(error, element) {
			error.prependTo(element.parent());
		},			
		rules: { 
			v_prix_cat: { required: true, number:true },
			v_rabais: { required: true, number:true }
    },
		messages: {
			v_prix_cat: { required: "ce champ est requis", number: "ce champ doit être numérique" },
			v_rabais: { required: "ce champ est requis", number: "ce champ doit être numérique" }
		}
	});
	
	$('#v2').validate({
		errorPlacement: function(error, element) {
			error.prependTo(element.parent());
		},			
		rules: { 
			v_valeur_residuelle: { required: true, number:true },
			v_taux_leasing: { required: true, number:true }
    },
		messages: {
			v_valeur_residuelle: { required: "ce champ est requis", number: "ce champ doit être numérique" },
			v_taux_leasing: { required: "ce champ est requis", number: "ce champ doit être numérique" }
		}
	});


});

function v_calcul_valeur_residuelle() {
	prix_cat = parseFloat($('#v_prix_cat').val());
	rabais =  parseFloat($('#v_rabais').val());
	duree = parseFloat($('#v_duree').val());
	km = parseFloat($('#v_km').val());

	tab_val = new Array(4);
	tab_val[12] = new Array(4);
  tab_val[12][10000] = 64;
  tab_val[12][15000] = 63;
  tab_val[12][20000] = 62;
  tab_val[12][25000] = 60;

	tab_val[24] = new Array(4);
  tab_val[24][10000] = 58;
  tab_val[24][15000] = 56;
  tab_val[24][20000] = 53;
  tab_val[24][25000] = 50;

	tab_val[36] = new Array(4);
  tab_val[36][10000] = 51;
  tab_val[36][15000] = 49;
  tab_val[36][20000] = 45;
  tab_val[36][25000] = 42;

	tab_val[48] = new Array(4);
  tab_val[48][10000] = 46;
  tab_val[48][15000] = 43;
  tab_val[48][20000] = 39;
  tab_val[48][25000] = 35;

	prix_net = prix_cat - (prix_cat * rabais/100);
	valeur_residuelle = prix_net*tab_val[duree][km]/100;
	$('#v_valeur_residuelle').val(valeur_residuelle);
}

function v_calcul_mensualites() {
	valeur_residuelle = parseFloat($('#v_valeur_residuelle').val());
	taux_leasing = parseFloat($('#v_taux_leasing').val());
	x1 = prix_net - valeur_residuelle;
	x1 = x1 * (taux_leasing/100) * 2;
	x1 = prix_net - valeur_residuelle + x1;
	cout_par_mois = x1*1.076/duree;
	cout_par_km = cout_par_mois*duree/km;
	if (cout_par_km < 1) {
		cout_par_km = Math.round(cout_par_km*100.0) + " ct";
	} else {
		cout_par_km = cout_par_km.toFixed(2) + " fr.";
	}
	v_display_results();
}

function v_display_results() {
	$('#v_r1').text(prix_cat.toFixed(2));
	$('#v_r2').text(rabais.toFixed(2) + "%");
	$('#v_r3').text(valeur_residuelle.toFixed(2));
	$('#v_r4').text(taux_leasing.toFixed(2) + "%");
	$('#v_r5').text(duree + ' mois');
	$('#v_r6').text(km + " KM");
	$('#v_cout_par_mois').text(Math.round(cout_par_mois));
	$('#v_cout_par_km').text(cout_par_km);	
}

/************** vrai leasing max **************/
$(function() {
	$('#vm_step2').hide();

	$('#vm_step1 .next').click(function() {
		if ($("#vm1").validate().form()) {
	  	$('#vm_step1').hide();
	  	$('#vm_step2').show();
	  	
	  	vm_calcul_prix_max();
	  }
	});
	
	$('#vm_step2 .back').click(function() {
		$('#vm_step2').hide();
		$('#vm_step1').show();
	});
	
	$('#vm1').validate({
		errorPlacement: function(error, element) {
			error.prependTo(element.parent());
		},			
		rules: { 
			vm_montant_mensuel: { required: true, number:true },
			vm_taux_leasing: { required: true, number:true },
			vm_rabais: { required: true, number:true }
    },
		messages: {
			vm_montant_mensuel: { required: "ce champ est requis", number: "ce champ doit être numérique" },
			vm_taux_leasing: { required: "ce champ est requis", number: "ce champ doit être numérique" },
			vm_rabais: { required: "ce champ est requis", number: "ce champ doit être numérique" }
		}
	});
});

function vm_calcul_valeur_residuelle() {
	tab_val = new Array(4);
	tab_val[12] = new Array(4);
  tab_val[12][10000] = 64;
  tab_val[12][15000] = 63;
  tab_val[12][20000] = 62;
  tab_val[12][25000] = 60;

	tab_val[24] = new Array(4);
  tab_val[24][10000] = 58;
  tab_val[24][15000] = 56;
  tab_val[24][20000] = 53;
  tab_val[24][25000] = 50;

	tab_val[36] = new Array(4);
  tab_val[36][10000] = 51;
  tab_val[36][15000] = 49;
  tab_val[36][20000] = 45;
  tab_val[36][25000] = 42;

	tab_val[48] = new Array(4);
  tab_val[48][10000] = 46;
  tab_val[48][15000] = 43;
  tab_val[48][20000] = 39;
  tab_val[48][25000] = 35;

	pourcent_residuel = tab_val[duree][km];
	valeur_residuelle = prix_net*tab_val[duree][km]/100;
	//$('#vm_valeur_residuelle').val(valeur_residuelle);
}

function vm_calcul_prix_max() {
	rabais =  parseFloat($('#vm_rabais').val());
	duree = parseFloat($('#vm_duree').val());
	km = parseFloat($('#vm_km').val());
	montant_mensuel = parseFloat($('#vm_montant_mensuel').val());
	taux_leasing = parseFloat($('#vm_taux_leasing').val());

	x1 = montant_mensuel * duree;
	x2 = 1.076 * (1+2*taux_leasing/100);
	prix_net = (x1/x2)/0.9;
	vm_calcul_valeur_residuelle();
	prix_max_catalog = prix_net * (1+rabais/100);

	cout_par_km = montant_mensuel*duree/km;
	if (cout_par_km < 1) {
		cout_par_km = Math.round(montant_mensuel*100.0) + " ct";
	} else {
		cout_par_km = cout_par_km.toFixed(2);
	}
	vm_display_results();
}

function vm_display_results() {
	$('#vm_r1').text(montant_mensuel.toFixed(2));
	$('#vm_r2').text(duree + ' mois');
	$('#vm_r3').text(km + " KM");
	$('#vm_r4').text(taux_leasing.toFixed(2) + "%");
	$('#vm_r5').text(rabais.toFixed(2) + "%");
	$('#vm_r6').text(pourcent_residuel.toFixed(2) + " % du prix net");
	$('#vm_prix_net').text(Math.round(prix_net));
	$('#vm_prix_max_catalog').text(Math.round(prix_max_catalog));
}

/************** faux leasing **************/
$(function() {
	$('#f_step2').hide(); 
	$('#f_step3').hide(); 

	$('#f_step1 .next').click(function() {
		if ($("#f1").validate().form()) {
			$('#f_step1').hide();
			$('#f_step2').show();
		
			f_calcul_valeur_residuelle();
		}
	});
	
	$('#f_step2 .next').click(function() {
		if ($("#f2").validate().form()) {
			$('#f_step2').hide();
			$('#f_step3').show();
		
			f_calcul_mensualites();
		}
	});
	
	$('#f_step2 .back').click(function() {
		$('#f_step2').hide(); 
		$('#f_step1').show(); 
	});
	
	$('#f_step3 .back').click(function() {
		$('#f_step3').hide(); 
		$('#f_step2').show(); 
	});	

	$('#f1').validate({
		errorPlacement: function(error, element) {
			error.prependTo(element.parent());
		},			
		rules: { 
			f_prix_cat: { required: true, number:true },
			f_rabais: { required: true, number:true }
    },
		messages: {
			f_prix_cat: { required: "ce champ est requis", number: "ce champ doit être numérique" },
			f_rabais: { required: "ce champ est requis", number: "ce champ doit être numérique" }
		}
	});
	$('#f2').validate({
		errorPlacement: function(error, element) {
			error.prependTo(element.parent());
		},			
		rules: { 
			f_valeur_residuelle: { required: true, number:true },
			f_taux_leasing: { required: true, number:true }
    },
		messages: {
			f_valeur_residuelle: { required: "ce champ est requis", number: "ce champ doit être numérique" },
			f_taux_leasing: { required: "ce champ est requis", number: "ce champ doit être numérique" }
		}
	});
});

function f_calcul_valeur_residuelle() {
	prix_cat = parseFloat($('#f_prix_cat').val());
	rabais =  parseFloat($('#f_rabais').val());
	duree = parseFloat($('#f_duree').val());
	km = parseFloat($('#f_km').val());

	prix_net = prix_cat - (prix_cat * rabais/100);
	valeur_residuelle = prix_net*.1;
	$('#f_valeur_residuelle').val(valeur_residuelle);
}

function f_calcul_mensualites() {
	valeur_residuelle = parseFloat($('#f_valeur_residuelle').val());
	taux_leasing = parseFloat($('#f_taux_leasing').val());
	x1 = prix_net - valeur_residuelle;
	x1 = x1 * (taux_leasing/100) * 2;
	x1 = prix_net - valeur_residuelle + x1;
	cout_par_mois = x1*1.076/duree;
	cout_par_km = cout_par_mois*duree/km;
	if (cout_par_km < 1) {
		cout_par_km = Math.round(cout_par_km*100) + " ct";
	} else {
		cout_par_km = cout_par_km.toFixed(2) + " fr.";
	}
	f_display_results();
}

function f_display_results() {
	$('#f_r1').text(prix_cat.toFixed(2));
	$('#f_r2').text(rabais.toFixed(2) + "%");
	$('#f_r3').text(valeur_residuelle.toFixed(2));
	$('#f_r4').text(taux_leasing.toFixed(2) + "%");
	$('#f_r5').text(duree + ' mois');
	$('#f_r6').text(km + " KM");
	$('#f_cout_par_mois').text(Math.round(cout_par_mois));
	$('#f_cout_par_km').text(cout_par_km);
	$('#f_valeur_residuelle_2').text(Math.round(valeur_residuelle));
}

/************** faux leasing max **************/
$(function() {
	$('#fm_step2').hide();  

	$('#fm_step1 .next').click(function() {
		if ($("#fm1").validate().form()) {
			$('#fm_step1').hide();
			$('#fm_step2').show();
			
			fm_calcul_prix_max();
		}
	});
	
	$('#fm_step2 .back').click(function() {
		$('#fm_step2').hide();
		$('#fm_step1').show();
	});	
	$('#fm1').validate({
		errorPlacement: function(error, element) {
			error.prependTo(element.parent());
		},			
		rules: { 
			fm_montant_mensuel: { required: true, number:true },
			fm_taux_leasing: { required: true, number:true },
			fm_rabais: { required: true, number:true }
    },
		messages: {
			fm_montant_mensuel: { required: "ce champ est requis", number: "ce champ doit être numérique" },
			fm_taux_leasing: { required: "ce champ est requis", number: "ce champ doit être numérique" },
			fm_rabais: { required: "ce champ est requis", number: "ce champ doit être numérique" }
		}
	});
});


function fm_calcul_valeur_residuelle() {
	tab_val = new Array(4);
	tab_val[12] = new Array(4);
  tab_val[12][10000] = 64;
  tab_val[12][15000] = 63;
  tab_val[12][20000] = 62;
  tab_val[12][25000] = 60;

	tab_val[24] = new Array(4);
  tab_val[24][10000] = 58;
  tab_val[24][15000] = 56;
  tab_val[24][20000] = 53;
  tab_val[24][25000] = 50;

	tab_val[36] = new Array(4);
  tab_val[36][10000] = 51;
  tab_val[36][15000] = 49;
  tab_val[36][20000] = 45;
  tab_val[36][25000] = 42;

	tab_val[48] = new Array(4);
  tab_val[48][10000] = 46;
  tab_val[48][15000] = 43;
  tab_val[48][20000] = 39;
  tab_val[48][25000] = 35;

	prix_net = prix_cat - (prix_cat * rabais/100);
	valeur_residuelle = prix_net*tab_val[duree][km]/100;
	//$('#fm_valeur_residuelle').val(valeur_residuelle);
}

function fm_calcul_prix_max() {
	rabais =  parseFloat($('#fm_rabais').val());
	duree = parseFloat($('#fm_duree').val());
	km = parseFloat($('#fm_km').val());
	montant_mensuel = parseFloat($('#fm_montant_mensuel').val());
	taux_leasing = parseFloat($('#fm_taux_leasing').val());

	x1 = montant_mensuel * duree;
	x2 = 1.076 * (1+2*taux_leasing/100);
	prix_net = (x1/x2)/0.9;
	valeur_residuelle = prix_net*.1;
	prix_max_catalog = prix_net * (1+rabais/100);

	cout_par_km = montant_mensuel*duree/km;
	if (cout_par_km < 1) {
		cout_par_km = Math.round(montant_mensuel*100.0) + " ct";
	} else {
		cout_par_km = cout_par_km.toFixed(2);
	}
	fm_display_results();
}

function fm_display_results() {
	$('#fm_r1').text(montant_mensuel.toFixed(2));
	$('#fm_r2').text(duree + ' mois');
	$('#fm_r3').text(km + " KM");
	$('#fm_r4').text(taux_leasing.toFixed(2) + "%");
	$('#fm_r5').text(rabais.toFixed(2) + "%");
	$('#fm_r6').text(valeur_residuelle.toFixed(2));
	$('#fm_prix_net').text(Math.round(prix_net));
	$('#fm_prix_max_catalog').text(Math.round(prix_max_catalog));
	$('#fm_valeur_residuelle').text(Math.round(valeur_residuelle));

}