
function Calculator() {

	this.DEPOSIT = 0;		// set the calculator to use different equations for Loan and Deposit.
	this.LOAN = 1;
	
	this.calcType = this.DEPOSIT;  // default the calculator type to Deposit(savings).
	
	
	this.interestPerPeriod = 0.0;
	this.paymentsPerPeriod = 0.0;
	this.numOfPeriods = 0;
	this.futureValue = 0.0;
	this.presentValue = 0.0;

}

Calculator.prototype.setInterestPerPeriod = function (interest) {
	this.interestPerPeriod = parseFloat(interest);
}

Calculator.prototype.setPaymentsPerPeriod = function (payment) {
	this.paymentsPerPeriod = parseFloat(payment);
}

Calculator.prototype.setNumOfPeriods = function (numOfPeriods) {
	this.numOfPeriods = parseInt(numOfPeriods);
}
Calculator.prototype.setFutureValue = function (FV) {
	this.futureValue = parseFloat(FV);
}

Calculator.prototype.setPresentValue = function (PV) {
	this.presentValue = parseFloat(PV);
}

Calculator.prototype.getFutureValue = function() {

	var numerator;
	var denom;
	
	if(this.calcType == this.DEPOSIT) {
		var FV_principal = this.presentValue * (Math.pow((1 + this.interestPerPeriod), this.numOfPeriods));
		var FV_annuity;
		
		if(this.interestPerPeriod == 0) {
			FV_annuity = this.paymentsPerPeriod * this.numOfPeriods;
		}
		else {
			FV_annuity = this.paymentsPerPeriod * (1 + this.interestPerPeriod) * (((Math.pow((1 + this.interestPerPeriod), this.numOfPeriods)) -1) / this.interestPerPeriod);
		}
	
		this.futureValue =  Math.round((FV_principal + FV_annuity) * 100)/100;	
	}
	else if (this.calcType == this.LOAN)  {
		if(this.interestPerPeriod == 0 ) {
			this.futureValue = this.paymentsPerPeriod * this.numOfPeriods + this.presentValue;
		}
		else {
			numerator =  - this.paymentsPerPeriod * (1 - Math.pow((1 + this.interestPerPeriod),  this.numOfPeriods));
			denom = this.interestPerPeriod * Math.pow((1 + this.interestPerPeriod),  this.numOfPeriods);
			
			this.futureValue = (numerator / denom) + this.presentValue;
			
		}
	
		this.futureValue =  Math.round(this.futureValue * 100)/100;	
	}
	
	return this.futureValue;
}

Calculator.prototype.getPaymentsPerPeriod = function () {

	var numerator;
	var denom;

	if(this.calcType == this.DEPOSIT) {
	
		if(this.interestPerPeriod == 0 ) {
			numerator = this.futureValue - this.presentValue;
			denom = this.numOfPeriods;
		}
		else {
			numerator = this.interestPerPeriod * (this.futureValue - this.presentValue * (Math.pow((1 + this.interestPerPeriod), this.numOfPeriods)));
	
			denom =  (Math.pow((1 + this.interestPerPeriod), this.numOfPeriods + 1)) - 1 - this.interestPerPeriod;
		}
	
		this.paymentsPerPeriod = Math.round(100* numerator / denom) / 100;
	}
	else if (this.calcType == this.LOAN)  {

		if(this.interestPerPeriod == 0 ) {
			numerator = this.futureValue - this.presentValue;
			denom = this.numOfPeriods;
		}
		else {
			
			numerator =  - (this.interestPerPeriod * (this.futureValue - this.presentValue)  *  (Math.pow((1 + this.interestPerPeriod), this.numOfPeriods)));
	
			denom =  1 - (Math.pow((1 + this.interestPerPeriod), this.numOfPeriods));
		}
	
		this.paymentsPerPeriod = Math.round(100* numerator / denom) / 100;	
		
		
	}
	return this.paymentsPerPeriod;
}

Calculator.prototype.getNumOfPeriods = function (numOfPeriods) {
	
	var numerator;
	var denom;

	if(this.calcType == this.DEPOSIT) {

		if(this.interestPerPeriod == 0 ) {
			numerator = this.futureValue - this.presentValue;
			denom = this.paymentsPerPeriod;
		}
		else {	
			numerator = Math.log((this.futureValue * this.interestPerPeriod  +  this.paymentsPerPeriod * (1 + this.interestPerPeriod)) / (this.presentValue + this.paymentsPerPeriod * (1 + this.interestPerPeriod))); 
			denom = Math.log(1 + this.interestPerPeriod);
		}
	
		this.numOfPeriods = Math.round(100* numerator / denom) / 100;
	}
	else if (this.calcType == this.LOAN)  {
	
		if(this.interestPerPeriod == 0 ) {
			numerator = this.futureValue - this.presentValue;
			denom = this.paymentsPerPeriod;
		}
		else {	
			numerator = Math.log(this.paymentsPerPeriod / (this.paymentsPerPeriod - ((this.futureValue - this.presentValue) *  this.interestPerPeriod ))); 
			denom = Math.log(1 + this.interestPerPeriod);
		}
	
		this.numOfPeriods = Math.round(100* numerator / denom) / 100;	
		
	}
	return this.numOfPeriods;
}

