// Simulateur de pret immobilier



// Taux par defaut		

var tauxDefauts = new Array(4.7, 4.7, 4.7, 4.7, 4.7, 4.7, 4.7, 4.7, 4.7, 4.7, 4.8, 4.8, 4.9, 4.9, 4.9, 4.9, 5.05, 5.05, 5.05, 5.05, 5.05, 5.05, 5.15, 5.15, 5.15, 5.15, 5.15, 5.40, 5.40, 5.40);



/* ############################# */

/*   Function Version   	     */

/* ############################# */



// Calcul le montant de l'echeance mensuelle en fonction du taux annuel, du montant emprunte et de la duree en annee

// ex : calculEcheance(0.05, 145000, 25) -> pret à 5% sur 25 ans de 145 000€ retourne une echeance mensuelle de 847 euros

function calculEcheance(taux, montant, duree) {

	// Calcul du taux periodique et du nombre d'echeance

	var tauxPer = taux / 12;

	var nbEcheance = duree * 12;

	

	// Calcul de l'echeance

	var ech = ( montant * tauxPer ) / ( 1 - Math.pow((1 + tauxPer), -nbEcheance) );

	

	return ech;

}







/* ############################# */

/*   Object Oriented Version     */

/* ############################# */



function Pret(montant) {

	this.montant = montant;

	this.taux = 0;

	this.duree = 0;

	this.echeance = 0;

}



Pret.prototype.montant;

Pret.prototype.taux;

Pret.prototype.duree;

Pret.prototype.echeance;

Pret.prototype.tauxPeriodique;

Pret.prototype.nombreEcheances;



Pret.prototype.setMontant = function(valeur) { this.montant = valeur; }

Pret.prototype.setTaux = function(valeur) { this.taux = valeur; }

Pret.prototype.setDuree = function(valeur) { this.duree = valeur; }

Pret.prototype.setEcheance = function(valeur) { this.echeance = valeur; }



Pret.prototype.getMontant = function() { return this.montant; }

Pret.prototype.getTaux = function() { return this.taux; }

Pret.prototype.getDuree = function() { return this.duree; }

Pret.prototype.getEcheance = function() { return this.echeance; }



Pret.prototype.getTauxPeriodique = function() { return this.tauxPeriodique; }

Pret.prototype.getNombreEcheances = function() { return this.nombreEcheances; }



Pret.prototype.calculEcheance = function() {

	// Calcul du taux periodique et du nombre d'echeance

	var tauxPer = this.taux / 12;

	var nbEcheance = this.duree * 12;

	

	// Calcul de l'echeance

	//var ech = ( this.montant * tauxPer * Math.pow( (1 + tauxPer ), nbEcheance ) )  / ( Math.pow( ( 1 + tauxPer), nbEcheance ) - 1 );

	var ech = ( this.montant * tauxPer ) / ( 1 - Math.pow((1 + tauxPer), -nbEcheance) )

	

	this.echeance = Math.round(ech);

	this.tauxPeriodique = tauxPer;

	this.nombreEcheances = nbEcheance;

}