/*===========================================================================*/
// 20090416 AK added focus to control on div expand
// 20090414 AK new abstraction and code
/*===========================================================================*/
var blnCalculator = 0;
function flipCalculator() {
	if (blnCalculator == 0) {
		hideLayer("nocalculator");
		showLayer("calculator");
		blnCalculator = 1;
		
		objControl = new getObj('txtPurchasePrice').obj;
		objControl.focus();
	} else {
		hideLayer("calculator")
		showLayer("nocalculator")
		blnCalculator = 0;
	};
};

var P0 = 0;
function doCalcAmount(){
	var objControl = null;
	var purchasePrice = 0;
	var downPayment = 0;
	objControl = new getObj('txtPurchasePrice').obj;
	purchasePrice = isNaN(objControl.value)?0:objControl.value;
	objControl = new getObj('txtDownPayment').obj;
	downPayment = isNaN(objControl.value)?0:objControl.value;
	objControl = new getObj('txtMortgageAmount').obj;
	P0 = purchasePrice - downPayment;
	objControl.value = P0;
};

function doCalcMonthlyPayment() {
	// http://en.wikipedia.org/wiki/Fixed_rate_mortgage#Monthly_payment_formula
	// The fixed monthly payment for a fixed rate mortgage is the amount paid by the borrower
	// every month that ensures that the loan is paid off in full with interest at the end of
	// its term. This monthly payment c depends upon the monthly interest rate r (expressed as
	// a fraction, not a percentage, i.e., divide the quoted yearly nominal percentage rate by
	// 100 and by 12 to obtain the monthly interest rate), the number of monthly payments N
	// called the loan's term, and the amount borrowed P0 known as the loan's principal;
	// rearranging the formula for the present value of an ordinary annuity we get the formula
	// for c:
	//              c = (r / (1 - (1 + r) - N))P0 
	// For example, for a home loan for $200,000 with a fixed yearly nominal interest rate of
	// 6.5% for 30 years, the principal is P0 = 200000, the monthly interest rate is
	// r = 6.5 / 100 / 12, the number of monthly payments is N = 30 * 12 = 360, the fixed
	// monthly payment equals $1264.14. This formula is provided using the financial function
	// PMT in a spreadsheet such as Excel. In the example, the monthly payment is obtained by
	// entering either of the these formulas:
	//	=PMT(6.5/100/12,30*12,200000) 
	//  =((6.5/100/12)/(1-(1+6.5/100/12)^(-30*12)))*200000 
	//  = 1264.14 
	
	if (isValidData()) {
		P0 = isNaN(P0)?0:P0;
		objControl = new getObj('ddlRate').obj;
		I = objControl.value;
		objControl = new getObj('ddlTerm').obj;
		L = objControl.value;
		r = I / (12 * 100);
		N = L * 12;
		c = P0 * ( r / (1 - Math.pow((1 + r), -N)));
		objControl = new getObj('txtMonthlyPayment').obj;
		objControl.value = Math.round(c * 100) / 100;
	};
};

function isValidData() {
	var isValid = true;
	var objControl = null;
	var message = '';

	objControl = new getObj('ddlTerm').obj;
	if (objControl.value == '') {
		isValid = false;
		if (message != '') {
			message += '\n';
		};
		message += 'Please select a mortgage term.';
	};

	objControl = new getObj('ddlRate').obj;
	if (trim(objControl.value) == '') {
		isValid = false;
		if (message != '') {
			message += '\n';
		};
		message += 'Please select a mortgage rate.';
	};

	if (!isValid) {
		alert(message);
		return false;
	} else {
		return true;
	};
};

