
//global data and objects

var term,amt,rate,rate_pct,pmt;

// intrinsic function

function power(baseval,factor)
{
var idx, result;
	
	result = baseval;
	for ( idx = 1 ; idx < factor ; idx++ )
		result = result * baseval;
	return result;

} // end power function

function format_number(prefix,innum,decpoints,postfix)  
{
var intpart, fractpart, idx, outstr, instr = innum + "";
    
    idx = instr.indexOf(".");
    if (idx != (-1)) {
		intpart = instr.substring(0,idx);
		fractpart = instr.substring(idx+1,idx+3);
		for (idx = 0; idx < (decpoints - fractpart.length); idx++)
			fractpart += "0";
	}
	else { 
		intpart = instr;
		fractpart = "";
		for (idx = 0; idx < decpoints; idx++)
			fractpart += "0";
	}

	if (decpoints > 0) {
		outstr = prefix + intpart + "." + fractpart + postfix;
	}
	else {
		outstr = prefix + intpart + postfix;
	}	

	return outstr;

} // end format_number function

function init()
{
    term = 360.0;
    amt  = 100000.0;
    rate = 8.000;
    rate_pct = rate/100.0;
    pmt = ((-(rate_pct/12)*(0+(power((1+(rate_pct/12)),term))*(-amt)))/(-1+power((1+(rate_pct/12)),term))) + 0.009;

	document.loan_info.term.selectedIndex = 29;  //30 years
	document.loan_info.amt.value  = format_number("",amt,0,"");
	document.loan_info.rate.selectedIndex = 56;  // 8.000%
	document.loan_info.pmt.value  = format_number("",pmt,2,"");
    
}

function update_loan()
{
    term = (document.loan_info.term.selectedIndex + 1) * 12;
    amt  = document.loan_info.amt.value;
    rate = (document.loan_info.rate.selectedIndex + 8) * 0.125;
    rate_pct = rate/100.0;
    pmt = ((-(rate_pct/12)*(0+(power((1+(rate_pct/12)),term))*(-amt)))/(-1+power((1+(rate_pct/12)),term))) + 0.009;

	document.loan_info.pmt.value  = format_number("",pmt,2,"");
}	
