
// start roll over functions
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//end roll over functions

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function verifyDelete(someURL){
 var where_to= confirm("Are you sure you want to delete this?");
 if (where_to== true)
 {
   window.location=someURL;
 }
 else
 {
  }
}

function formSubmit(whichForm){
 formSubmit = whichForm.submit()
}

function generic_pop_up(FileName)
{
	var scrWidth = screen.width
	var scrHeight = screen.height
	var getLeft
	var getTop
	var lgwinprop
	var winlgw = 261
	var winlgh = 358
	
	getLeft = (scrWidth - winlgw)/2
	getTop = (scrHeight - winlgh)/2
	
	lgwinprop = "'personalbar=no,toolbar=no,location=no,scrollbars=yes,width=" + winlgw + ",height=" + winlgh + ",left=" + getLeft + ",top=" + getTop + "'"
	
	newWindow = window.open(FileName, 'newWin', lgwinprop)
}

//Calculators
// 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;
}

function clearFields( form ) {
	form.income.value = "";
	form.down.value = "";
	form.debt.value = "";
	form.intrate.value = "";
	
	form.price_min.value = "";
	form.loan_amt_min.value = "";
	form.pmt_min.value = "";
	form.taxins_pmt_min.value = "";
	form.total_pmt_min.value = "";
	form.lmt_factor_min.value = "";

	form.price_max.value = "";
	form.loan_amt_max.value = "";
	form.pmt_max.value = "";
	form.taxins_pmt_max.value = "";
	form.total_pmt_max.value = "";
	form.lmt_factor_max.value = "";
}