// Popup Script
<!-- Begin
function NewWindow1(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
//  End -->

//netscape bug fix for layers
<!--
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->


browser = navigator.appName;
ie = "Microsoft Internet Explorer";
netscape = "Netscape";

function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); 
  return x;
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}

function SetFormValue(formStr,field,value) {
	
	form=(MM_findObj(formStr))
	
	if (form) {
		if (null != (form.elements[field])) {
			if ((browser == netscape)&&(form.elements[field].type=='select-one')){
				sellength=form.elements[field].length;
				for (i=0; i< sellength; i++) {
					if (form.elements[field].options[i].value==value)
						form.elements[field].options[i].selected=true;
				}
			} 
			else 
				form.elements[field].value=value;
		}		
	}
}

function SubmitForm(formStr) {
	form=(MM_findObj(formStr))
	if(form) form.submit();
}


//**************************************************************
//**************************************************************
// AFFORDING HOUSE CALCULATOR CODE-->

// 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 CalculateAffordHouse ( 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_inputAffordHouse ( field )
{
	strip_numberAffordHouse( field );
}

function strip_numberAffordHouse ( 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 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 AFFORDING HOUSE CALCULATOR CODE-->



//**************************************************************
//**************************************************************
// NET WORTH CALCULATOR CODE-->

function CalculateNetWorth( form, displayNetWorth )
{
	var f = form;
	
	var stocks, bonds, cash, f401k, annuities,
		residence, vacationhome, valuables, otherinvest, totalassets,
		mortgage, homeloan, studentloan, credcards, otherdebt, totaldebt,
		networth;

	stocks = f.stocks.value - 0;
	bonds = f.bonds.value - 0;
	cash = f.cash.value - 0;
	f401k = f.f401k.value - 0;
	annuities = f.annuities.value - 0;
	residence = f.residence.value - 0;
	vacationhome = f.vacationhome.value - 0;
	valuables = f.valuables.value - 0;
	otherinvest = f.otherinvest.value - 0;

	mortgage = f.mortgage.value - 0;
	homeloan = f.homeloan.value - 0;
	studentloan = f.studentloan.value - 0;
	credcards = f.credcards.value - 0;
	otherdebt = f.otherdebt.value - 0;

	totalassets = stocks + bonds + cash + f401k + annuities + residence + vacationhome + valuables + otherinvest;
	totaldebt = mortgage + homeloan + studentloan + credcards + otherdebt;
	networth = totalassets - totaldebt;
	
	f.totalassets.value = AddThousandsCommas( Math.round( totalassets ) );
	f.totaldebt.value = AddThousandsCommas( Math.round( totaldebt ) );
	if ( displayNetWorth ) {
		f.networth.value = AddThousandsCommas( Math.round( networth ) );
	}
}

function process_inputNetWorth ( field )
{
	strip_numberNetWorth( field );
	rejectNegatives( field );
	CalculateNetWorth( field.form, false );
}

function strip_numberNetWorth ( field )
{
	var original_number = field.value;
	var stripped_number = "";

	for ( var i=0; i<original_number.length; i++ )
	{
		var digit = original_number.charAt(i);
		if ( digit == '.' || digit == '-' || !( digit < "0" || digit > "9" ) )
		{
			stripped_number = stripped_number + digit;
		}
	}

	if ( stripped_number )
		field.value = stripped_number;
	else
		field.value = "";
}

//**************************************************************
//**************************************************************
// END NET WORTH CALCULATOR CODE-->



//**************************************************************
//**************************************************************
// END BUDGET CALCULATOR CODE-->
function CalculateBudget( form, section)
{
	var f = form;
	
	if (section=='income'){
		var salary, rent, child, dividends, pension, totalincome;
		salary = f.salary.value - 0;
		rent = f.rent.value - 0;
		child = f.child.value - 0;
		dividends = f.dividends.value - 0;
		pension = f.pension.value - 0;
		totalincome = salary + rent + child + dividends + pension;
		f.totalincome.value = AddThousandsCommas( Math.round( totalincome ) );
	}
	else if (section=='housingdebt'){
		var mortgage, loans, creditcard, child, autoloans, other, totalhousingdebt, totalincome;
		strip_numberBudget( BudgetIncome.totalincome );
		totalincome = BudgetIncome.totalincome.value - 0;
		BudgetIncome.totalincome.value = AddThousandsCommas( Math.round( totalincome ) );

		mortgage = f.mortgage.value - 0;
		loans = f.loans.value - 0;
		creditcard = f.creditcard.value - 0;
		child = f.child.value - 0;
		autoloans = f.autoloans.value - 0;
		other = f.other.value - 0;

		totalhousingdebt = mortgage + loans + creditcard + child + autoloans + other;
		f.totalhousingdebt.value = AddThousandsCommas( Math.round( totalhousingdebt ) );
		housingdebtpercent = (totalhousingdebt/totalincome)*100;
		housingdebtpercent = Math.round(housingdebtpercent);

		writit(housingdebtpercent+'%',"housingdebtpercentage");
	}
	else if (section=='taxes'){
		var federal, fica, state, medicare, property, totaltaxes, totalincome;
		strip_numberBudget( BudgetIncome.totalincome );
		totalincome = BudgetIncome.totalincome.value - 0;
		BudgetIncome.totalincome.value = AddThousandsCommas( Math.round( totalincome ) );
		
		federal = f.federal.value - 0;
		fica = f.fica.value - 0;
		state = f.state.value - 0;
		medicare = f.medicare.value - 0;
		property = f.property.value - 0;

		totaltaxes = federal + fica + state + medicare + property;
		f.totaltaxes.value = AddThousandsCommas( Math.round( totaltaxes ) );
		taxespercent = (totaltaxes/totalincome)*100;
		taxespercent = Math.round(taxespercent);
		writit(taxespercent+'%',"taxespercentage");
	}
	else if (section=='insurance'){
		var life, auto, health, home, disability, other, totalinsurance, totalincome;
		strip_numberBudget( BudgetIncome.totalincome );
		totalincome = BudgetIncome.totalincome.value - 0;
		BudgetIncome.totalincome.value = AddThousandsCommas( Math.round( totalincome ) );
		
		life = f.life.value - 0;
		auto = f.auto.value - 0;
		health = f.health.value - 0;
		disability = f.disability.value - 0;
		other = f.other.value - 0;

		totalinsurance = life + auto + health + disability + other;
		f.totalinsurance.value = AddThousandsCommas( Math.round( totalinsurance ) );
		insurancepercent = (totalinsurance/totalincome)*100;
		insurancepercent = Math.round(insurancepercent);
		writit(insurancepercent+'%',"insurancepercentage");
	}
	else if (section=='savings'){
		var f1k, stock, ira, college, mutualfunds, other, totalsavings, totalincome;
		strip_numberBudget( BudgetIncome.totalincome );
		totalincome = BudgetIncome.totalincome.value - 0;
		BudgetIncome.totalincome.value = AddThousandsCommas( Math.round( totalincome ) );
		
		f1k = f.f1k.value - 0;
		stock = f.stock.value - 0;
		ira = f.ira.value - 0;
		college = f.college.value - 0;
		other = f.other.value - 0;

		totalsavings = f1k + stock + ira + college + other;
		f.totalsavings.value = AddThousandsCommas( Math.round( totalsavings ) );
		savingspercent = (totalsavings/totalincome)*100;
		savingspercent = Math.round(savingspercent);
		writit(savingspercent+'%',"savingspercentage");
	}
	else if (section=='livingexpenses'){
		var food, personalcare, clothing, doctor, fuel, entertainment, water, reading, phone, club, cable, daycare, gas, tuition, parking, other, totallivingexpenses, totalincome;
		strip_numberBudget( BudgetIncome.totalincome );
		totalincome = BudgetIncome.totalincome.value - 0;
		BudgetIncome.totalincome.value = AddThousandsCommas( Math.round( totalincome ) );
		
		food = f.food.value - 0;
		personalcare = f.personalcare.value - 0;
		clothing = f.clothing.value - 0;
		doctor = f.doctor.value - 0;
		fuel = f.fuel.value - 0;
		entertainment = f.entertainment.value - 0;
		water = f.water.value - 0;
		reading = f.reading.value - 0;
		phone = f.phone.value - 0;
		club = f.club.value - 0;
		cable = f.cable.value - 0;
		daycare = f.daycare.value - 0;
		gas = f.gas.value - 0;
		tuition = f.tuition.value - 0;
		parking = f.parking.value - 0;
		other = f.other.value - 0;

		totallivingexpenses = food + personalcare + clothing + doctor + fuel + entertainment + water + reading + phone + club + cable + daycare + gas + tuition + parking + other;
		f.totallivingexpenses.value = AddThousandsCommas( Math.round( totallivingexpenses ) );
		livingexpensespercent = (totallivingexpenses/totalincome)*100;
		livingexpensespercent = Math.round(livingexpensespercent);
		writit(livingexpensespercent+'%',"livingexpensespercentage");
	}

}

function process_inputBudget ( field , section )
{
	strip_numberBudget( field );
	rejectNegatives( field );
	CalculateBudget( field.form, section );
}

function strip_numberBudget ( field )
{
	var original_number = field.value;
	var stripped_number = "";

	for ( var i=0; i<original_number.length; i++ )
	{
		var digit = original_number.charAt(i);
		if ( digit == '.' || digit == '-' || !( digit < "0" || digit > "9" ) )
		{
			stripped_number = stripped_number + digit;
		}
	}

	if ( stripped_number )
		field.value = stripped_number;
	else
		field.value = "";
}
//**************************************************************
//**************************************************************
// END BUDGET CALCULATOR CODE-->



//COMMON CALCULATOR FUNCTIONS
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 rejectNegatives ( field )
{
	if ( field.value < 0 ) {
		alert( "Please, enter a positive number!" );
		field.value = "";
		field.focus();
	}
}
//END COMMON CALCULATOR FUNCTIONS


function writit(text,id)
{
	if (document.getElementById)
	{
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	}
	else if (document.all)
	{
		x = document.all[id];
		x.innerHTML = text;
	}
	else if (document.layers)
	{
		x = document.layers[id];
		text2 = '<P>' + text + '</P>';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}

//popup
function NewWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}


