﻿//	Copyright 2008 Idexis SARL; droits d'utilisation concédés pour le site ASSUR-TV

/*
var ir = .05;			//	interest rate
var np = 10;			//	number of periods
var pv = 77217.35;		//	present value
var pmt = 10000;		//	future value
var fv = 125778.93;		//	regular payment
*/

function vpm(taux, npm, va, type, franchise)
{
	//	fin période		: type = 0
	//	début période	: type = 1
	//	vpm et va sont en principe < 0 s'agissant de décaissements
	//	le taux est mensuel
	//	exemple : vpm(.05 / 12, 20 * 12, 550000, 1))
	
	//	formule mens*somme(j=1 à n) de 1/(1+I)^j=K

	if (npm <= 0) return 0;
	if (type == undefined)
		type = 0;
	if (franchise == undefined)
		franchise = 0;
	if (franchise != 0)
		va = vc(taux, franchise, 0, -va);

	var temp = - 1 + 1 / Math.pow(1 + taux, npm);
	if (temp == 0) 
		temp = -va / npm;
	else
		temp = 	va * taux / temp;
	if (type == 0)
		return temp;
	else
		return temp / (1 + taux);
};
	
function informit_required_deposits(ir, np, fv)
{
	//	http://www.informit.com/articles/article.aspx?p=23230&seqNum=1
	//	Calculating Required Investment Deposits
	return fv * ir/ (Math.pow(1 + ir, np) - 1)
}

function getRar(taux, npm, va, per)
{
	//	http://www.ecogesam.ac-aix-marseille.fr/Resped/Gestion/mathfi/mathfin1.html#ei8
	return va * (Math.pow(1 + taux, per) - 1) / (Math.pow(1 + taux, npm) - 1)
};

function getCTC(taux, npm, va)
{
	//	cût du crédit (intérêts)
	return -npm * vpm(taux, npm, va) - va;
};

//	informit_vc(ir, np, pmt) == -vc(ir, np, pmt, 0);

function informit_vc(ir, np, pmt)
{
	//	http://www.informit.com/articles/article.aspx?p=23230&seqNum=4
	//	Calculating the Future Value of an Investment
	return pmt * (Math.pow(1 + ir, np) - 1) / ir
};

function vc(taux, npm, vpm, va, type)
{
	//	fin période		: type = 0
	//	début période	: type = 1
	//	vpm et va sont en principe < 0 s'agissant de décaissements
	//	le taux est mensuel

	if (type == undefined)
		type = 0;
	if (va == undefined)
		va = 0;
	if (vpm == undefined)
		vpm = 0;
	if (npm < 0)
		npm = 0;

	var capital = -va;
	for (var i = 1; i <= npm; i++)
	{
		if (type == 1)
			capital -= vpm;
		capital += taux * capital;
		if (type == 0)
			capital -= vpm;
	};
	return capital;
};

//	informit_vpm(ir, np, pv) == - vpm(ir, np, pv)

function informit_vpm(ir, np, pv)
{
	//	http://www.informit.com/articles/article.aspx?p=23230&seqNum=1
	//	Calculating Loan or Mortgage Payments
	return (pv * ir) / (1 - Math.pow(1 + ir, -np))
};

function getVaVpm(taux, npm, capital)
{
	//	http://fr.wikipedia.org/wiki/Suite_g%C3%A9om%C3%A9trique
	return capital * (1 - (1 + taux)) / (1 - Math.pow(1 + taux, npm));
};

function getVaCapital(taux, npm, capital)
{
	//	http://fr.wikipedia.org/wiki/Suite_g%C3%A9om%C3%A9trique
	return capital / (Math.pow(1 + taux, npm));
};

function va(taux, npm, vpm)
{
	//	http://www.informit.com/articles/article.aspx?p=23230&seqNum=3
	//	Calculating a Loan Balance
	if (taux == 0)
		return npm * vpm;
	else
		return vpm * (1 - Math.pow((1 + taux), -npm)) / taux;
};

function npm(taux, vpm, va)
{
	taux += 1;
	var capital = va;
	var year = 0;
	while ((capital * taux - vpm) > 0)
	{
		capital *= taux;
		capital -= vpm;
		year += 1;
	};
	year += capital * taux / vpm;
	return year;
};
