/*
 *  mortgage calculation
 *
 */
 
/**
 *  Mortgage calculator object including default income multiples.
 */
function MortgageCalculator() {
	this.incomeMultipleSingle = 4.0;
	this.incomeMultipleDouble = 3.0;
}

/**
 *  Calculate the maximum mortgage that can be taken out by one or two applicants.
 *
 *  @param firstIncome - The income of the first applicant.  This should be the higher income if there is more than one applicant.
 *  @param secondIncome - The income of the second applicant.
 *
 *  @return Size of maximum mortgage that can be taken out.
 */
MortgageCalculator.prototype.maximumBorrowingAmount = function(firstIncome, secondIncome) {

	if(secondIncome) {
		return (firstIncome + secondIncome) * this.incomeMultipleDouble;
	} else {
		return firstIncome * this.incomeMultipleSingle;
	}
}

/**
 *  Calculate the monthly payments for an interest only mortgage.
 *
 *  @param mortgageAmount - The size of the mortgage.
 *  @param aprPercent - The apr amount as a percentage.
 *
 *  @return The monthly payments due on the mortgage.
 */
MortgageCalculator.prototype.monthlyPaymentInterestOnly = function(mortgageAmount, aprPercent) {

	return (((mortgageAmount * aprPercent) / 12) / 100);
}

/**
 *  Calculate the monthly payments for a repayment mortgage.
 *
 *  @param amount - The size of the mortgage.
 *  @param aprPercent - The apr amount as a percentage.
 *  @param termYears - The term of the mortgage in years.
 *
 *  @return The monthly repayments due on the mortgage.
 */
MortgageCalculator.prototype.monthlyPaymentRepayment = function(amount, aprPercent, termYears) {

	var apr = aprPercent / 100;
	var termMonths = termYears * 12;

	return (amount * (apr / 12)) / (1 - Math.pow(1 + (apr / 12), -termMonths));
}
   
/**
 *  Calculate the size of interest only mortgage possible for a given monthly payment.
 *
 *  @param monthlyPayment - The monthly payments that will be made.
 *  @param aprPercent - The apr amount as a percentage.
 *
 *  @return The size of interest only mortgage that may be available.
 */
MortgageCalculator.prototype.availableMortgageInterestOnly = function(monthlyPayment, aprPercent) {

	return (monthlyPayment * 100 * 12) / aprPercent;
}
  
/**
 *  Calculate the size of repayment mortgage possible for a given monthly payment.
 *
 *  @param monthlyPayment - The monthly payments that will be made.
 *  @param aprPercent - The apr amount as a percentage.
 *  @param termYears - The term of the mortgage in years.
 *
 *  @return The size of repayment mortgage that may be available.
 */
MortgageCalculator.prototype.availableMortgageRepayment = function(monthlyPayment, aprPercent, termYears) {

	var apr = aprPercent / 100;
	var termMonths = termYears * 12;

	return (monthlyPayment * (1 - Math.pow(1 + (apr / 12), -termMonths))) / (apr / 12);
}

/**
 *  Calculate the term of the repayment mortgage given other parameters.
 *
 *  @param monthlyPayment - The monthly payments that will be made.
 *  @param aprPercent - The apr amount as a percentage.
 *  @param mortgageAmount - The size of the mortgage.
 *
 *  @return The term over which the mortgage will be repaid.
 */
MortgageCalculator.prototype.termAsNumber = function(monthlyPayment, aprPercent, mortgageAmount) {

	var aprPerMonth = aprPercent / 1200;

	return (Math.log(monthlyPayment / (monthlyPayment - mortgageAmount * aprPerMonth)) / Math.log(1 + aprPerMonth)) / 12
}
