
/**
 * This calculator provides methods to give a rough estimate of the
 * required closing costs given certain loan parameters.
 */

//constants

var TAXSERVICEFEE;
var FEETOCHARGE;
var CREDITREPORTFEE;
var APPRAISALFEE;
var FLOODCERTFEE;
var DEFAULTRATE;
var DEFAULTLOANAMT;
var DEFAULTTERM;
var DEFAULTINTERESTDAYS;
 
//note:  calculate method is NOT overloaded

function EstimatedClosingCosts() {

	var returnObject = new Object();
	returnObject.className = "EstimatedClosingCosts";
	returnObject.calculate = EstimatedClosingCostsCalculate;

	return returnObject;

}

/**
 * The calculate() method takes the loan parameters and determines 
 * estimated closing costs.
 * 
 * @param loanAmt loan amount in dollars
 * @param loanTerm loan term in months
 * @param interestRate annual interest rate of loan
 * @param programPoints discount points
 * @param downPaymentAmt down payment amount in dollars
 * @param closingDate closing date
 *
 * @return the total estimated closing costs in dollars
 * @throws MortgageFamilyException if the default values cannot be
 * loaded, or if any of the loan parameters contain invalid values
 */

function EstimatedClosingCostsCalculate(loanAmt, loanTerm, interestRate, programPoints, downPaymentAmt, closingDate) {

	TAXSERVICEFEE = 75;
	//change this amount to 250 from 375
	FEETOCHARGE = 250;
	CREDITREPORTFEE = 20.7;
	APPRAISALFEE = 300;
	FLOODCERTFEE = 15.5;
	DEFAULTRATE = 0.08;
	DEFAULTLOANAMT = 100000;
	DEFAULTTERM = 360;
	DEFAULTINTERESTDAYS = 15;


	if (loanAmt == null) {
		loanAmt = new Number(DEFAULTLOANAMT); // Default
	}

	if (loanTerm == null) {
		loanTerm = new Number (DEFAULTTERM); // default (in months)
	}


	if (downPaymentAmt == null) {
		downPaymentAmt = new NUmber(0);
	}

	if (interestRate == null) {
		interestRate = new Number(DEFAULTRATE);            
	}

	var tempPMICalc = new PMICalculator();
	var monthlyMI = tempPMICalc.calculate (loanAmt,	downPaymentAmt);
	var MIFee = 2*monthlyMI;

	var today = new Date();

	var interestDays = DEFAULTINTERESTDAYS;
	if (closingDate != null) {            
		interestDays = today.getDate();
	}
	var prePaidInterest = interestRate/365 * loanAmt * interestDays;        

	// determine origination fee
	var originationFee = 0;
		if (programPoints == null) {
			programPoints = new Number(0);        
		} else if (programPoints >= 0.01) {
			originationFee = 0.01 * loanAmt;
		}

	// determine discount points
	var discountPoints = 0;        
	if (programPoints > .01) {
		discountPoints = (programPoints - 0.01) * loanAmt;
	} else if (programPoints > 0 && programPoints < 0.01) {
		discountPoints = programPoints * loanAmt;
	}

	var paidByLender = 0;
	if (programPoints < 0.0) {
		paidByLender = programPoints * loanAmt;
	}

	return new Number(TAXSERVICEFEE + FEETOCHARGE + CREDITREPORTFEE + APPRAISALFEE + FLOODCERTFEE + paidByLender + discountPoints + originationFee + prePaidInterest + MIFee);
}
