<script language="JavaScript">
<!-- 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 min_pti = 0.28;
var max_pti = 0.33;
var min_dti = 0.36;
var max_dti = 0.38;

var periods = loan_life * frequency;	// the number of periods (and payments) in the loan


function Calculate ( form )
{
	var good, periodic_income, periodic_debt, intrate, down, taxins, min_total_pmt,
		max_total_pmt, min_pmt, max_pmt, temp, min_limit, max_limit, payment,
		min_loan, max_loan, min_price, max_price, min_total, max_total;

	good = VerifyInputs( form );
	if ( !good ) return;

	// read some of the values from the form
	periodic_income = form.income.value / frequency;
	periodic_debt = parseFloat( form.debt.value );
	down = parseFloat( form.down.value );
	if ( isNaN( down ) ) { down = 0; }
	intrate = parseFloat( form.intrate.value );


	// calculate the periodic taxes and insurance
	taxins = GetPeriodicTaxesInsurance( form );

	// determine the lower level (minimum) for the max monthly payment as a bank would determine it
	min_total_pmt = periodic_income * min_pti;
	temp = (periodic_income * min_dti) - periodic_debt;
	if ( temp < min_total_pmt ) {
		min_limit = "debt";
		min_total_pmt = temp;
	} else {
		min_limit = "income";
	}

	// determine the upper level (maximum) for the max monthly payment as a bank would determine it
	max_total_pmt = periodic_income * max_pti;
	temp = (periodic_income * max_dti) - periodic_debt;
	if ( temp < max_total_pmt ) {
		max_limit = "debt";
		max_total_pmt = temp;
	} else {
		max_limit = "income";
	}

	// When we calculate the loan the bank can give, we subtract the taxes and insurance
	// from the maximum payment because the bank doesn't give you a big enough loan to
	// cover your insurance and taxes even though they are included while the bank calculates
	// the maximum payment you can afford.

	// get the lower maximum (here called "minimum") loan the bank will give
	min_pmt = min_total_pmt - taxins;
	min_loan = GetMortgageLoan( intrate, min_pmt );
	min_price = min_loan + down;

	// get the upper maximum (here called "maximum") loan the bank will give
	max_pmt = max_total_pmt - taxins;
	max_loan = GetMortgageLoan( intrate, max_pmt );
	max_price = max_loan + down;

	// display the results on the form, all at once
	form.price_min.value = AddThousandsCommasDecimal( min_price );
	form.loan_amt_min.value = AddThousandsCommasDecimal( min_loan );
	form.pmt_min.value = AddThousandsCommasDecimal( min_pmt );
	form.taxins_pmt_min.value = AddThousandsCommasDecimal( taxins );
	form.total_pmt_min.value = AddThousandsCommasDecimal( min_total_pmt );
	form.lmt_factor_min.value = min_limit;

	form.price_max.value = AddThousandsCommasDecimal( max_price );
	form.loan_amt_max.value = AddThousandsCommasDecimal( max_loan );
	form.pmt_max.value = AddThousandsCommasDecimal( max_pmt );
	form.taxins_pmt_max.value = AddThousandsCommasDecimal( taxins );
	form.total_pmt_max.value = AddThousandsCommasDecimal( max_total_pmt );
	form.lmt_factor_max.value = max_limit;
}

function VerifyInputs ( form )
{
	var val;

	val = form.income.value;
	if ( val == null || val == "" || isNaN( val ) || val <= 0 ) {
		alert( "You have to enter a valid, positive income!" );
		form.income.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 GetMortgageLoan ( intrate, payment )
{
	var period_intrate, disc_factor, principal;

	// get the interest rate for one period (e.g. month)
	period_intrate = intrate / (frequency * 100);

	// calculate the discount factor
	disc_factor = ( Math.pow( 1 + period_intrate, periods ) - 1 )
								/
		( period_intrate *  Math.pow( 1 + period_intrate, periods ) )
	;

	principal = payment * disc_factor;

	return principal;
}

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_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 );
		if ( dec_str.length == 1 ) { dec_str = dec_str + "0"; }
		dec_str = "." + dec_str;
	} 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 -->
														</script>
