/**
 * This calculator is used to determine the monthly Principal and Interest
 * (P and I) payment based on certain loan parameters.
 */
function MonthlyPayment() {

	var returnObject = new Object();

	returnObject.calculate = MonthlyPaymentCalculate;
	returnObject.className = "MonthlyPayment";
	
	return returnObject;

}

/**
 * The calculate method takes the loan parameters and returns the
 * monthly principal and interest (PI) payment in dollars.
 *
 * @param loanTerm  loan term (in months)
 * @param intRate interest rate
 * @param loanAmt loan amount
 *
 * @return monthly P+I payment in dollars
 * @throws MortgageFamilyException if the loan parmeters are invalid
 */
function MonthlyPaymentCalculate(loanTerm, intRate, loanAmt) {

	var periodicInt = 0.0;
	var monthlyPI = 0.0;


	if ((intRate > 0) && (intRate < 1)) { /* Entered as a fraction */
		periodicInt = (intRate/12);
	} else { 
		/* Entered as a whole number percentage */
		periodicInt = (intRate/12)/100;
	}

	/* Actually calculate the P&I payment */
	monthlyPI = loanAmt * ((periodicInt * (Math.pow((1 + periodicInt),loanTerm))) / (Math.pow((1 + periodicInt),loanTerm) - 1));

	return (monthlyPI);
}

