
<!-- hide script from older browsers

// some constants and default (average) values
var loan_life = 30;			// years to pay off the loan
var frequency = 12;			// how many payments a year
var avg_tax_rate = 0.015;	// the average yearly % for property tax
var avg_pmi = 80;			// the average PMI (personal mortgage insurance) amount
var pmi_threshold = 0.20;	// the down_pmt/value ratio that triggers the PMI charge

var periods = loan_life * frequency;	// the number of periods (and payments) in the loan


function CalculatePayment ( form )
{
	var good, payment, taxins, pmi, total, dp_ratio;

	good = VerifyInputs( form );
	if ( !good ) return;

	payment = GetMortgagePayment( form );
	form.loanpayment.value = AddThousandsCommasDecimal( payment );

	taxins = GetPeriodicTaxesInsurance( form );
	form.taxinsurance.value = AddThousandsCommasDecimal( taxins );

	// calculate the down payment to home price ratio and see if PMI kicks in
	dp_ratio = form.down.value / form.price.value;
	if ( dp_ratio < pmi_threshold ) {
		form.pmi.value = avg_pmi;
		pmi = avg_pmi;
	} else {
		form.pmi.value = 0;
		pmi = 0;
	}

	total = payment + taxins + pmi;
	form.total.value = AddThousandsCommasDecimal( total );
}

function VerifyInputs ( form )
{
	var val;

	val = form.price.value;
	if ( val == null || val == "" || isNaN( val ) || val <= 0 ) {
		alert( "You have to enter a valid, positive price for the home!" );
		form.price.focus();
		return false;
	}

	val = form.intrate.value;
	if ( val == null || val == "" || isNaN( val ) || val <= 0 ) {
		alert( "You have to enter a valid, positive interest rate!" );
		form.intrate.focus();
		return false;
	}

	return true;
}

function GetMortgagePayment ( form )
{
	var intrate, period_intrate, disc_factor, payment, principal;

	intrate = parseFloat( form.intrate.value );
	principal = form.price.value - form.down.value;

	period_intrate = intrate / (frequency * 100);
	disc_factor = ( Math.pow( 1 + period_intrate, periods ) - 1 )
								/
		( period_intrate *  Math.pow( 1 + period_intrate, periods ) )
	;

	payment = principal / disc_factor;

	return payment;
}

function GetPeriodicTaxesInsurance ( form )
{
	var str_taxes, str_ins, yearly_taxes, yearly_ins, periodic_txins;

	str_taxes = form.taxes.value;
	str_ins = form.insurance.value;
	if ( str_taxes == "" ) {
		yearly_taxes = 0;
	} else {
		yearly_taxes = parseFloat( str_taxes );
	}
	if ( str_ins == "" ) {
		yearly_ins = 0;
	} else {
		yearly_ins = parseFloat( str_ins );
	}
	periodic_txins = (yearly_taxes + yearly_ins) / frequency;

	return periodic_txins;
}

function process_price ( form )
{
	var price, tax;

	process_input( form.price );
	price = parseFloat( form.price.value );
	if ( isNaN( price ) ) return;

	// calculate the default tax for the home
	tax = price * avg_tax_rate;
	form.taxes.value = round2( tax );
}

function process_input ( field )
{
	strip_number( field );
}

function strip_number ( field )
{
	var original_number = field.value;
	var stripped_number = "";
	var parsed_number;

	for ( var i=0; i<original_number.length; i++ )
	{
		var digit = original_number.charAt(i);
		if ( digit == '.' || !( digit < "0" || digit > "9" ) )
		{
			stripped_number = stripped_number + digit;
		}
	}

	// this will clean up more than one decimals
	if ( stripped_number )
	{
		parsed_number = parseFloat( stripped_number );
		if ( isNaN( parsed_number ) )
			field.value = "";
		else
			field.value = parsed_number;
	}
	else {
		field.value = "";
	}
}

function AddThousandsCommas( number )
{
	var T='', S = String(number), L = S.length-1, C, j;

	for ( j=0; j<=L; j++ ) {
		T += C = S.charAt(j);
		if ( (j<L) && ((L-j)%3 == 0) && (C!='-') )
			T+=',';
	}
	return T;
}

function AddThousandsCommasDecimal( number )
{
	var number, whole, decimal, dec_str, formatted_number;

	if ( isNaN( number ) ) return number;

	// get the whole part
	whole = Math.floor( number );

	// get the decimal up to two places and remove "0." in the beginning
	decimal = number - whole;
	if ( decimal != 0 )
	{
		decimal = round2( decimal );
		dec_str = String( decimal );
		dec_str = "." + dec_str.substr( dec_str.indexOf(".") + 1 );
	} else {
		dec_str = "";
	}

	// merge the commas-formatted whole with the decimal
	formatted_number = AddThousandsCommas( whole ) + dec_str;

	return formatted_number;
}

function round2( number )
{
	if ( isNaN( number ) ) return number;

	return Math.round(number * 100) / 100;
}

// end hiding from old browsers -->
														
