/****# DISCLAIMER OR WARRANTY 
 by IdealWebPresence, Inc - www.MortgageScript.com
 THIS SOFTWARE AND ACCOMPANYING DOCUMENTATION ARE PROVIDED "AS IS" AND
 WITHOUT WARRANTIES AS TO PERFORMANCE OF MERCHANTABILITY OR ANY OTHER
 WARRANTIES WHETHER EXPRESSED OR IMPLIED. BECAUSE OF THE VARIOUS HARDWARE
 AND SOFTWARE ENVIRONMENTS INTO WHICH THE FINANCIAL AND MORTGAGE CALCULATORS tm MAY BE USED, NO WARRANTY OF
 FITNESS FOR A PARTICULAR PURPOSE IS OFFERED.  THE USER MUST ASSUME THE
 ENTIRE RISK OF USING THIS PROGRAM. ANY LIABILITY OF IDEALWEBPRESENCE,INC WILL BE
 LIMITED EXCLUSIVELY TO PRODUCT REPLACEMENT OR REFUND OF PURCHASE PRICE.
 IN NO CASE SHALL IDEALWEBPRESENCE,INC BE LIABLE FOR ANY INCIDENTAL, SPECIAL OR
 CONSEQUENTIAL DAMAGES OR LOSS, INCLUDING, WITHOUT LIMITATION, LOST PROFITS
 OR THE INABILITY TO USE EQUIPMENT OR ACCESS DATA, WHETHER SUCH DAMAGES ARE
 BASED UPON A BREACH OF EXPRESS OR IMPLIED WARRANTIES, BREACH OF CONTRACT,
 NEGLIGENCE, STRICT TORT, OR ANY OTHER LEGAL THEORY. THIS IS TRUE EVEN IF
 IDEALWEBPRESENCE,INC IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. IN NO CASE WILL
 IDEALWEBPRESENCE,INC'S LIABILITY EXCEED THE AMOUNT OF THE LICENSE FEE ACTUALLY PAID
 BY LICENSEE TO IDEALWEBPRESENCE,INC. */
resultsFontFamily = 'Arial, Helvetica, sans-serif';
resultsFontSize = '12px';
resultsBackgroundColor = '#E1E1E1';

function round2(n)
{
   d = ((Math.round(n * 100)) / 100);
   return d;
}
function round3(n)
{
   d = ((Math.round(n * 1000)) / 1000);
   return d;
}
function round4(n)
{
   d = ((Math.round(n * 10000)) / 10000);
   return d;
}
function round5(n)
{
  d = ((Math.round(n * 100000)) / 100000);
  return d;
}
function round2S(n)
{
  d = "" + ((Math.round(n * 100)) / 100);

dec1 = d.substring(d.length-3, d.length-2);
dec2 = d.substring(d.length-2, d.length-1);

if (dec1 != '.') { 
if (dec2 == '.') d += "0";
else d += ".00";}

return d;
}
function round3S(n)
{
  d = "" + ((Math.round(n * 1000)) / 1000);

dec1 = d.substring(d.length-4, d.length-3);
dec2 = d.substring(d.length-3, d.length-2);
dec3 = d.substring(d.length-2, d.length-1);

if (dec1 != '.') 
	{ 
		if (dec2 == '.') d += "0";
		else if (dec3 == '.')
		{
		d += "00";
		}else d += ".000";
	}

return d;
}
function round4S(n)
{
  d = "" + ((Math.round(n * 10000)) / 10000);

dec1 = d.substring(d.length-5, d.length-4);
dec2 = d.substring(d.length-4, d.length-3);
dec3 = d.substring(d.length-3, d.length-2);
dec4 = d.substring(d.length-2, d.length-1);


if (dec1 != '.') 
	{ 
		if (dec2 == '.') d += "0";
		else if (dec3 == '.')
		{
		d += "00";
		}
		else if (dec4 == '.')
		{
		d += "000";
		}
		else d += ".0000";
	}

return d;
}
/**** Apr calculator starts here */

function show_apr()
{
	
	if((document.fm_apr.amt.value=="")||(document.fm_apr.intt.value=="")||(document.fm_apr.pts.value=="")||(document.fm_apr.clc.value=="")||(document.fm_apr.trm.value==""))
	{
		document.fm_apr.amt.value='200000';	
		document.fm_apr.intt.value='6.0';		
		document.fm_apr.pts.value='2.0';		
		document.fm_apr.clc.value='2400';		
		document.fm_apr.trm.value='30';		

		return false;
	}
	
		var amt = Number(document.fm_apr.amt.value);
		var intt = Number(document.fm_apr.intt.value);
		var pts = Number(document.fm_apr.pts.value);
		var clc = Number(document.fm_apr.clc.value);
		var trm = Number(document.fm_apr.trm.value);
		
		var result = document.getElementById('result');
		
		result.className='BlackText';
	    result.style.fontFamily = resultsFontFamily;
        result.style.fontSize = resultsFontSize;
	
		result.innerTEXT = "";
		
		result.innerHTML = "<p><br>";
		result.innerHTML += "<Center>";
 
		result.innerHTML += "Based on a \$"+round2S(amt)+" loan at  "+ round3S(intt)+"\% on a "+trm+" year term.\n"; 
		var mi = intt/1200; 
		var tt = trm * 12; 
		//var m2 = apr/1200;

		temp = (1.0 / Math.pow((1 + mi),tt));

		//temp2 = (1.0 / Math.pow((1 + m2),tt));
		if (temp < 1) 
		{ 
			paymt = amt * mi / (1 - temp); 
		}
		else 
		{ 
			paymt = amt / trm; 
		}
		//result.innerHTML += "<p>Monthly Principal & Interest Payment: \$"+round2S(paymt)+" \n";

		pta = amt * pts / 100; costs = pta + clc;
        result.innerHTML += "<p>Your total upfront costs are \$"+round2S(costs)+"\n"; 
		eamt = amt + costs;
		result.innerHTML += "("+round3S(pts)+"\% points=\$"+round2S(pta)+" and other costs of \$"+round2S(clc)+")\n";
		
		//result.innerHTML += "<p>The effective amount of your loan now is: \$"+round2S(eamt)+"\n";  
		epaymt = eamt * mi / (1 - temp); 
  

		m1 = mi; m2 = 2 * mi;
		for (j=0; j<25; j++)
		{
			mnew = (m1 + m2)/2;
			temp = (1.0 / Math.pow((1 + mnew),tt));
			epaymt2 = amt * mnew / (1 - temp); 
			if (epaymt2 < epaymt) { m1 = mnew; } else { m2 = mnew; }
			//printf("<li>%12.7f %12.7f\n",m1,m2); 
		}
		apr = (m1 + m2)*600;
		result.innerHTML += "<p>Using the figures above, your actual APR is  "+round4S(apr)+"\%\n";
		result.innerHTML += "</p>";  
	
}

/**** Apr calculator ends here */
/**************************************************************************************/
/**** duration calculator starts here */

function show_duration()
{
	
	if((document.fm_duration.amt.value=="")||(document.fm_duration.intt.value=="")||(document.fm_duration.ovl.value==""))
	{
		document.fm_duration.amt.value='200000';
		document.fm_duration.intt.value='6.5';
		document.fm_duration.ovl.value='20';
		return false;
	}
	
		var amt = Number(document.fm_duration.amt.value);
		var intt = Number(document.fm_duration.intt.value);
		var len = Number(document.fm_duration.len.value);
		var ovl = Number(document.fm_duration.ovl.value);
		var opt = document.fm_duration.opt.value;
		var out = document.fm_duration.out.value;
        var tabletext = '';
		var result = document.getElementById('result');
		
		result.className='BlackText';
	    result.style.fontFamily = resultsFontFamily;
        result.style.fontSize = resultsFontSize;
	
		result.innerTEXT = "";

		thou = amt/1000;
		//intt = ir1 + ir2;
		msg = "<div align=left class='BlackText'>Based on a \$"+amt+" loan at "+intt+"\% interest \n";
		msg += "for "+len+" years.\n";

		tt = len * 12; i = intt / 1200;
  
 		if (tt > 0)
		{ 
			if (i > 0) { paymt = amt * i / (1 - (Math.pow((1 + i),-tt))); }
			else { paymt = amt / tt; }
			msg += "<p>Your original principal & interest payment is: \$"+round2S(paymt)+"\n";
		}
		else
		{
			paymt = 0;
		}

		if (opt == "AMOUNT")
		{
			if (ovl < paymt)
			{
				msg += "<p>Sorry, you did not pay enough!\n";
 				msg += "<p>You entered \$"+round2S(ovl)+", You need \$"+round2S(paymt - ovl)+" more\n";
			}
			else
			{
				msg += "<p>Since you can afford to pay \$"+round2S(ovl)+" per month, \$"+round2S(ovl - paymt)+" will by applied to reduce your princpal balance each month. <p>\n";
				if (paymt > 0)
				{ 
					
				}
				if (ovl < (amt * i) && tt == 0)
				{
					msg += "<p>But that's less than the interest of \$"+round2S(amt * i)+","; 
				}
				eti = paymt * tt - amt;
			
				if (out == "FULL") 
				{ 
					tabletext = "<table border='1' width='400' style='font-size: "+resultsFontSize+"'>\n<tr><td>Month</td><td>Principal</td><td>Interest</td><td>Balance</td><td>Total Interest</td>\n"; 
				}
				m=0;ti=0;
				rowCounter = 0;
				while(amt > 0 && m < 1200)
				{
					ti = ti + amt * i; ci = amt * i; p = ovl - ci;
					amt = amt * (1 + i) - ovl; m++;
  					if (out == "FULL") 
					{ 
					    if(rowCounter % 2 == 0)
		                   textColor = "";
		                else
		                    textColor = " style='background:"+resultsBackgroundColor+"'";
				   
						tabletext +="<tr"+textColor+"><td>"+round2(m)+"</td><td align='right'>"+round2S(p)+"</td><td align='right'>"+round2S(ci)+"</td><td align='right'>"+round2S(amt)+"</td><td align='right'>"+round2S(ti)+"</td>\n"; 
					    rowCounter++;
					}
				}
				if (out == "FULL") 
				{ 
					tabletext +="</table><br>\n"; 
					
				}
				y = m/12;
				msg +="Your loan will be paid off in "+m+" months ("+round3(y)+" years)\n";
				if (tt > 0) 
				{ msg +="<li>Expected total interest : \$"+round2S(eti)+"\n"; }
				msg +="<li>Your total interest : \$"+round2S(ti)+"\n";
				if (tt > 0) 
				{
					msg +="<li>Total interest saved : \$"+round2S(eti - ti)+"\n";
				}
    
			}
		}
		else
		{
			if (ovl > len && tt > 0)
			{
				msg +="<p>Sorry, it can't take longer than the amortization";
				msg +="<p>That's called defaulting on your loan!";
			}
			else
			{
				dm = ovl * 12; dpt_l = paymt; dpt_h = 10 * paymt; m = 0;
				if (dpt_h == 0) { dpt_h = amt; }
				while(dpt_h > dpt_l + .01)
				{
					dpt = (dpt_l + dpt_h) / 2;
					m = 0; amtt = amt;
					while(amtt > 0 && m < 1200)
					{
						amtt = amtt * (1 + i) - dpt; m++;
					}
					if (m < dm) { dpt_h = dpt; } else { dpt_l = dpt; }
				}
				if (out == "FULL")
				{
											
					tabletext ="<table width='400' border='1' class='BlackText'>\n";
					tabletext +="<table border='1' width=400 style='font-size: "+resultsFontSize+"'>\n<td>Month</td><td>Principal</td><td>Interest</td><td>Balance</td><td>Total Interest</td>\n"; 
					
					amtt = amt;
 					ti=0;j=0;k=1;
			        rowCounter = 0;
					while((amtt > 0) && (j < 1200))
					{
						ci = amtt * i; 
						p = dpt - ci;
						amtt = amtt * (1 + i) - dpt; 
						
						j++; 
						ti = ti + ci;

						if(rowCounter % 2 == 0)
		                   textColor = "";
		                else
		                   textColor = " style='background:"+resultsBackgroundColor+"'";
						
						tabletext +="<tr"+textColor+"><td>"+k+"</td><td align='right'>"+round2S(p)+"</td><td align='right'>"+round2S(ci)+"</td><td align='right'>"+round2S(amtt)+"</td><td align='right'>"+round2S(ti)+"</td>\n";
						rowCounter++;
						k++;
					}
				
					tabletext +="</table>\n";
					

				}
				msg +="<p>Since you would like to pay off your loan in "+ovl+" years, you'll need to add \$"+round2S(dpt - paymt)+" to your current monthly payment. Your new monthly payment will be: \$"+round2S(dpt)+"\n";
				/msg +="<p> That's an extra \$"+round2S(dpt - paymt)+" a month\n"; */
				paid_interest = round2(ovl * 12 * dpt - amt);
				totl_interest = round2(len * 12 * paymt - amt);
				msg +="<p>Should you decide to prepay, you would save: <b>\$"+round2S(totl_interest - paid_interest)+"</b> in interest payments!\n";
			}
		}

		msg +="</td></tr></table>";
	
			result.innerTEXT = "";
			result.innerHTML = msg;
			result.innerHTML += tabletext;
			tabletext="";
			msg = "";
}

/**** duration calculator ends here */
/**************************************************************************************/
/**** Missing calculator starts here */

function show_missing()
{
	var result = document.getElementById('result');
	
	result.className='BlackText';
	result.style.fontFamily = resultsFontFamily;
    result.style.fontSize = resultsFontSize;
	
	result.innerTEXT = "";
	result.innerHTML = "";
	zeroes=0;
	which = '';
	for (i=0; i<=3; i++)
	{
		FV=document.fm_missing.elements[i].value;
		if(FV=="")
		{
			//find missing field
			which=i;
			zeroes++;
		}
	}
	/// asign form values

	LA = Number(document.fm_missing.elements[0].value); 
	MP = Number(document.fm_missing.elements[1].value); 
	AI = Number(document.fm_missing.elements[2].value); 
	NM = Number(document.fm_missing.elements[3].value);

	if (zeroes > 1)
	{
		//# Too many zeroes! I'll use defaults
		LA = 100000; AI=8.00; NM = 360; MP = 0; which = '1';
		
		document.fm_missing.elements[0].value='200000';
		which = '';
		document.fm_missing.elements[1].value='';

		document.fm_missing.elements[2].value='6.00'
		document.fm_missing.elements[3].value='360'
	}
	if (which == '1')
	{
		mi = AI/1200;
		MP = round2(LA * mi / (1 - Math.pow((1 + mi), - NM)));
		MP = round2S(MP);
		document.fm_missing.elements[1].value=MP;
	}

	if (which == '0')
	{
		mi = AI/1200;
		LA = round2(MP /( mi / (1 - Math.pow((1 + mi), - NM))));
		LA = round2S(LA);
		document.fm_missing.elements[0].value=LA;
	}

	if (which == '3')
	{
		mi = AI/1200;
		nm_lo = 0 ; nm_hi = 2000; mc=0;
		while((nm_lo < nm_hi + 0.01) && (mc < 500))
		{
			nm = (nm_lo + nm_hi) / 2;
			cmp = round2(LA * mi / (1 - Math.pow((1 + mi), - nm)));
			if (cmp > MP) 
			{ nm_lo = nm; } 
			else{ nm_hi = nm; }
			mc++;
		}
	  
		NM = round2(nm);
		if (NM > 1999)
		{
			needed_min = round2S(LA * mi);
			msg = "<p>You will never pay off your loan, the required minimum is \$ "+needed_min+" just to cover interest";
		
			result.innerTEXT = "";
			result.innerHTML = msg;
		
			NM = 0;
			document.fm_missing.elements[3].value=NM;
		}
		NM = round2S(NM);
		document.fm_missing.elements[3].value=NM;
	}

	if (which == '2')
	{
		if (MP * NM < LA)
		{
			msg = "<p>That payment is too low to ever pay off that loan";
			result.innerTEXT = "";
			result.innerHTML = msg;
			msg = "";
		}
		else
		{
			ai_lo = 0 ; ai_hi = 500; mc=0;
			while(ai_lo < ai_hi + 0.0001 && mc < 500)
			{
				ai = (ai_lo + ai_hi) / 2;
				mi = ai/1200;
				cmp = round2(LA * mi / (1 - Math.pow((1 + mi), - NM)));
				if (cmp < MP) { ai_lo = ai; } else { ai_hi = ai; }
				mc++;
			}
			AI = round5(ai);
			if (AI == 500)
			{
				result.innerHTML = "<p>If you paid that much that long, you're paying annual interest over 500%";
				AI = 0;
				document.fm_missing.elements[2].value=AI;
			}
			document.fm_missing.elements[2].value=AI;
		}
	}
	/* yrs = round3(NM/12);*/
}

/**** Missing calculator ends here *******/
/**************************************************************************************/
/**** Qulify calculator starts here */

function show_qualify()
{

	pi = Number(document.fm_qualify.pi.value);
	pt = Number(document.fm_qualify.pt.value);
	hi = Number(document.fm_qualify.hi.value);
	mi = Number(document.fm_qualify.mi.value);
	hv = Number(document.fm_qualify.hv.value);
	dp = Number(document.fm_qualify.dp.value);

	tpy = pi + mi + (pt + hi) / 12;
	lpy = pi + ((pt + hi) / 12) ;
	lpy = round2S(lpy);
	
	var result = document.getElementById('result');
	
	result.className='BlackText';
	result.style.fontFamily = resultsFontFamily;
    result.style.fontSize = resultsFontSize;
	
	result.innerTEXT = "";
	result.innerHTML = "<p><div align=left class='BlackText'>Your estimated monthly PITI payment is \$" + lpy;
	
	if (dp == 0)
	{
		if (hv > 0) { dp == hv * .2; } else { dp = 20000; }
	}
	else
	{
		if (hv == 0) { hv = dp * 5; }
	}
	if (hv == 0) { hv = 100000; }
	if (dp/hv < .20 && hv > 0)
	{
		// Impose PMI
		var pmi_r = .32; 
		if (dp/hv <= .15) { pmi_r = .52; }
		if (dp/hv <= .10) { pmi_r = .78; }
		if (dp/hv <= .05) { pmi_r = .90; }
		//if (dp/hv <= .15) { pmi_r = .32; }
		//if (dp/hv <= .10) { pmi_r = .52; }
		//if (dp/hv <= .05) { pmi_r = .78; }
		//if (dp/hv <= .03) { pmi_r = .90; }
		if (dp/hv >= 0.03)
		{
			pmi = (hv-dp) * pmi_r/1200;
			/*printf("<p>Total monthly PMI payment \$ %7.2f <br>( %4.2f %% a year on \$ %7.0f loan,<br> since your downpayment was %5.2f %%)", pmi, pmi_r, hv - dp, dp/hv*100);*/
		}
		else
		{
			result.innerHTML +=  "<p>With a down payment of \$ " +dp+ " on a purchase of \$ "+hv+"\n That's a downpayment of only "+ round3(dp/hv*100) +" \% !\n" ;
			//result.innerHTML +=  "That's a downpayment of only "+ round(dp/hv*100) +" \% !\n" ;
			result.innerHTML +=  "<p>Most lenders do prefer a down payment, but with decent credit you can find mortgage loans of 100% financing.  Contact JT to put you in touch with these lenders.\n";
			var doExit = true;   
		}
	}
	else { pmi = 0; }
    
	if (doExit!=true)
	{
		i28 = ((pi + pmi) * 12 + pt + hi ) / .28;
		i36 = ((pi + mi + pmi) * 12 + pt + hi ) / .36;
		if (i28 > i36) { req_inc  = i28; } else { req_inc = i36; }
	
		//("<p>28%% Qualifying income: \$ %9.2f ",i28);
		//("<p>36%% Qualifying income: \$ %9.2f ",i36);
	
		req_inc_s = round2S(req_inc);
	}
	if (doExit!=true) result.innerHTML +=  "<p><div align=left class='BlackText'>The minimum qualifying income needed is \$" + req_inc_s;

}

/**** Qualify calculator ends here *******/
/**************************************************************************************/
/**** balloon calculator starts here */

function show_balloon()
{

  	if((document.fm_balloon.bal.value=="")||(document.fm_balloon.intt.value==""))
	{
		document.fm_balloon.bal.value='200000';
		document.fm_balloon.intt.value='6.5';
		document.fm_balloon.out.value = 'Y';
		document.fm_balloon.fxd.value = '3'; 
		document.fm_balloon.prepay.value = '0';
		return false;
	}
    
	bal = Number(document.fm_balloon.bal.value);
	init = Number(document.fm_balloon.fxd.value);
	intt = Number(document.fm_balloon.intt.value);
	prepay = Number(document.fm_balloon.prepay.value);
	tbl = document.fm_balloon.out.value;
    
	var result = document.getElementById('result');
	
	result.className='BlackText';
	result.style.fontFamily = resultsFontFamily;
    result.style.fontSize = resultsFontSize;
	
	tt = init * 12; 
	ir = intt / 1200;
	temp = Math.pow((1 + ir), -360);
	paymt = (bal * ir) / (1 - temp);

	msg = "<p><div align=left class='BlackText'>Your monthly payment for the first "+round2S(init)+" years will be \$"+round2S(paymt);
	if (prepay > 0)
	{
		msg += "<p>Adding \$"+round2S(prepay)+" to your monthly payment equals \$"+round2S(paymt+prepay)+"\n";
	}
	msg += "<p>Amortization table for first "+init+" years.";
	if (tbl == 'Y') 
	{ 
		
		msg += "<table width=400 border=1 style='font-size: "+resultsFontSize+"'><tr><th>Year</th><th>Start Balance</th><th>Interest</th><th>Ending Balance</th></tr>\n"; 
	}
	else 
	{ 
		msg += "<pre width='400' style='font-family: "+resultsFontFamily+"'>\n    YR          Start Bal                  Int                End Bal  \n"; 
        msg += "     ==     ==========     ==========    =========     \n";
	}
	rowCounter=0;
	for (i=1; i<=init; i++)
	{
		sbal = bal; ti = 0;
		for (j=0; j<12; j++) //# each month
		{
			ci = bal * ir; bal += ci - paymt - prepay; ti += ci;
		}
		
		if(rowCounter % 2 == 0)
		    textColor = "";
		else
		    textColor = " style='background:"+resultsBackgroundColor+"'";
				   
		if (tbl == "Y")
		{
			msg += "<tr"+textColor+"><td>"+Math.round(i)+"</td><td align='right'>"+round2S(sbal)+"</td><td align='right'>"+round2S(ti)+"</td><td align='right'>"+round2S(bal)+"</td></tr>\n";
		}
		else
		{
			if (i == '1')
			{
				msg += "<span "+textColor+">     "+Math.round(i)+"          "+round2S(sbal)+"        "+round2S(ti)+"        "+round2S(bal)+"</span>\n";
			}
			else
			{
				msg += "<span "+textColor+">     "+Math.round(i)+"          "+round2S(sbal)+"        "+round2S(ti)+"        "+round2S(bal)+"</span>\n";
			}
		}
		rowCounter++;
	}
 
	if (tbl == "Y") { msg += "</table>\n"; }
	else { msg += "\n</pre></center>\n"; }

	/msg += "<p>After "+Math.round(init)+" Years Your Balance will be \$"+round2S(bal); */
	rest = 30 - init;

	msg += "<p>Now we'll examine what might happen over the next "+rest+" years\n";

	if (tbl == "Y")
	{	
		msg += "<table width=400 border=1 style='font-size: "+resultsFontSize+"'><tr><th>Rate</th><th>Payment</th><th>Total Interest</th>\n";
		msg += "<th>Rate</th><th>Payment</th><th>Total Interest</th></tr>\n";
	}
	else
	{
		msg += " <pre style='font-family: "+resultsFontFamily+"'>Rate          Payment            Tot Int          ";
		msg += "   Rate        Payment           Tot Int\n";
		msg += "======   ==========   =========  ";
		msg += "  ======   ==========   =========\n";
	}
	rowCounter=0;
	for (fi = 4; fi <= 8; fi += .25)
	{
		ir = fi/1200; fi2 = fi + 4;
		temp = Math.pow((1 + ir), -(rest * 12));
		np = (bal * ir) / (1 - temp);
		ti = (rest * 12 * np) - bal;

		ir2 = fi2/1200;
		temp = Math.pow((1 + ir2), -(rest * 12));
		np2 = (bal * ir2) / (1 - temp);
		ti2 = (rest * 12 * np2) - bal;
		
		if(rowCounter % 2 == 0)
		    textColor = "";
		else
		    textColor = " style='background:"+resultsBackgroundColor+"'";
		
		if (tbl == "Y") 
		{ 
			msg += "<tr"+textColor+"><td align='right'>"+round2S(fi)+"</td><td align='right'>"+round2S(np)+"</td><td align='right'>"+round2S(ti)+"</td>\n"; 
			msg += "<td align='right'>"+round2S(fi2)+"</td><td align='right'>"+round2S(np2)+"</td><td align='right'>"+round2S(ti2)+"</td></tr>\n";
		}
		else 
		{ 
			msg += "<span "+textColor+">   "+round2S(fi)+"        "+round2S(np)+"           "+round2S(ti)+"";
            msg += "       "+round2S(fi2)+"        "+round2S(np2)+"         "+round2S(ti2)+"</span>\n"; 
		}
		rowCounter++;
	}
	if (tbl == "Y")
	{ 
		msg += "</table>\n"; }
	else
	{ 
		msg += "\n</pre>\n"; 
	}
	msg += "<p>The table above shows 3 items; possible interest rate, monthly payment and total interest rate for the next "+rest+" years.";

	result.innerTEXT = "";
	result.innerHTML = msg;
	msg = "";
}
/**** balloon calculator ends here *******/
/**************************************************************************************/
/**** Million calculator starts here */

function show_million()
{
	yld = Number(document.fm_million.yld.value);
	inf = Number(document.fm_million.inf.value);
	fin = Number(document.fm_million.fin.value);
	amt = Number(document.fm_million.amt.value);
	dep = Number(document.fm_million.dep.value);
    
	var result = document.getElementById('result');
	result.className='BlackText';
	result.style.fontFamily = resultsFontFamily;
    result.style.fontSize = resultsFontSize;
	
	mi = yld/1200;
	//tt = yrs * py; 
	yrs = 0;
	msg = "";
	msg = "<p><div align=left class='BlackText'>Based on an annual yield of "+yld+"\%, ";
	msg += " monthly deposits of \$"+round2S(dep);
	msg += " and your initial investment of \$"+round2S(amt);
	//msg += "<p>You stated that your goal is to have \$"+round2S(fin);

	fact = 1; //#print "<PRE>\n";
	while(amt < fin && yrs < 100) 
	{
		yrs++;
		for (i=0; i<12; i++)
		{
			amt = amt * (1 + mi) + dep;
		}
		fact = fact * (1 + inf/100);
	}

	//#print "\n</PRE>\n";
	if (yrs < 100)
	{
		msg += "<p>You will reach your goal in approximately "+Math.round(yrs)+" years. Estimating a  "+inf+"\% annual inflation rate, your \$"+Math.round(fin)+" would be worth \$"+Math.round(fin/fact)+" in today's dollars.\n";
		msg += "\n";
	}
	else
	{
		msg += "<p>Sorry, even after 100 years, you will only have \$ "+Math.round(amt)+", worth<br>\$ "+Math.round(amt/fact)+" in today's dollars.\n";
	}

	msg += "</td></tr></table>";
	result.innerTEXT = "";
	result.innerHTML = msg;
	msg = "";
	
}
/**** Million calculator ends here *******/
/**************************************************************************************/
/**** rduration calculator starts here */

function show_rduration()
{
	if((document.fm_rduration.A.value=="")||(document.fm_rduration.I.value=="")||(document.fm_rduration.V.value=="")||(document.fm_rduration.F.value==""))
	{
		document.fm_rduration.A.value = '300000';
		document.fm_rduration.I.value = '7.5';
		document.fm_rduration.V.value = '25';
		document.fm_rduration.F.value = '2.0'; 
		document.fm_rduration.O.value = 'LENGTH';
		document.fm_rduration.X.value = 'SUMMARY';
		return false;
	}

	A = Number(document.fm_rduration.A.value);
	I = Number(document.fm_rduration.I.value);
	V = Number(document.fm_rduration.V.value);
	F = Number(document.fm_rduration.F.value);
	opt = document.fm_rduration.O.value;
	X = document.fm_rduration.X.value;
    
	var result = document.getElementById('result');
    
	result.className='BlackText';
	result.style.fontFamily = resultsFontFamily;
    result.style.fontSize = resultsFontSize;
	
	if (I > 100) { I = 100; }
	if (F > 10) { F = 10; }
	mi = I/1200;
	msg = "Based on an account balance of \$"+A+", interest rate of "+I+"\%\n";
	msg += "with payments inflated at a rate of "+F+"\% a year.\n";

	rowCounter = 0;
	if (opt == "AMOUNT")
	{
		
		msg += "<p>You've stated that you will need \$"+round2S(V)+" per month.\n";
		pmt = V; ym = -1;
		if (X == "FULL") 
		{ 
			msg += "<table width=450 border=1 style='font-size: "+resultsFontSize+"'>\n<tr><td>Month</td><td>Payment</td><td>Principal</td><td>Interest</td><td>Balance</td><td>Total Interest</td></tr>\n"; 
		}
		
		ci=0;ti=0;m=0;
		while(A > 0 && m < 1200)
		{
			ym++;
			if (ym == 12)
			{
				pmt = pmt * (1 + F/100); ym = 0;
			}
			ci = A * mi; 
			ti = ti + ci;
			p = pmt - ci;
			/*#if (p < 0)
			#{
				#msg += "</table>\n<p>You are earning more than you are withdrawing";
				#msg += "<br>Your money will last forever\n"; m = 1200;
			#}*/
			A = A + ci - pmt; m++;
  			if (X == "FULL" && m < 1200) 
			{ 																						
				
				if(rowCounter % 2 == 0)
		           textColor = "";
		        else
		           textColor = " style='background:"+resultsBackgroundColor+"'";
				   
				msg += "<tr"+textColor+"><td>"+Math.round(m)+"</td><td align='right'>"+round2S(pmt)+"</td><td align='right'>"+round2S(p)+"</td><td align='right'>"+round2S(ci)+"</td><td align='right'>"+round2S(A)+"</td><td align='right'>"+round2S(ti)+"</td></tr>\n"; 
			    rowCounter++;
			}
		}
		
		if (X == "FULL") { msg += "</table>\n"; }
		y = m/12;
		if (y<100)
		{
			msg += "With those monthly payments, your money would last approximately "+Math.round(m)+" months. ("+round3(y)+" years)\n";
 			//msg += "<li>Your total earned interest : \$ "+round2S(ti)+"\n";
		}
		else
		{
			msg += "<p>With those numbers your money would last over 100 year<br>";
			msg += "(i.e. essentially forever)\n";
		}
	}
	else
	{
		
		dm = V * 12; dpt_l = 0; dpt_h = A; m = 0;
		while(dpt_h > dpt_l + .01)
		{
			dpt_mid = (dpt_l + dpt_h) / 2; dpt = dpt_mid;
			m = 0; amtt = A; ym= -1;
			while(amtt > 0 && m < 1200)
			{
				amtt = amtt * (1 + mi) - dpt; m++;
				ym++;
				if (ym == 12)
				{
					dpt = dpt * (1 + F/100);
					ym = 0;
				}
			}
			//msg += "<li>$m $dpt_mid $dpt";
			if (m < dm) { dpt_h = dpt_mid; } else { dpt_l = dpt_mid; }
		}

		dpt_o = dpt_mid; //# Original
		dpt = dpt_o;
		msg += "<p> Your estimated monthly payment is \$"+round2S(dpt_o);

		if (X  == "FULL")
		{
			ym = -1;
			msg += "<table width=400 border=1 style='font-size: "+resultsFontSize+"'>\n";
			msg += "<tr><td>Month</td><td>Payment</td><td>Principal</td><td>Interest</td><td>Balance</td><td>Total Interest</td></tr>\n";
			amtt = A;
 			
			j=0;ti=0;
			while(amtt > 0 && j < 1200)
			{
				ym++;
				if (ym == 12)
				{
					dpt = dpt * (1 + F/100);
					ym = 0;

				}
				ci = amtt * mi; p = dpt - ci;
				amtt = amtt * (1 + mi) - dpt; j++; ti = ti + ci;
				
				if(rowCounter % 2 == 0)
		           textColor = "";
		        else
		           textColor = " style='background:"+resultsBackgroundColor+"'";
				  
				msg += "<tr"+textColor+"><td>"+Math.round(j)+"</td><td align='right'>"+round2S(dpt)+"</td><td align='right'>"+round2S(p)+"</td><td align='right'>"+round2S(ci)+"</td><td align='right'>"+round2S(amtt)+"</td><td align='right'>"+round2S(ti)+"</td></tr>\n";
			    rowCounter++;
			}
			msg += "</table>\n";
		}
        msg += " for "+V+" Years\n";
	}



	msg += "</td></tr></table>";

	result.innerTEXT = "";
	result.innerHTML = msg;
	msg = "";
	
}
/**** rduration calculator ends here *******/
/**************************************************************************************/
/**** Retire calculator starts here */

function show_retire()
{
	if(document.fm_retire.inc.value=="")
	{
		document.fm_retire.inc.value = '30000';
		document.fm_retire.unt.value = '15';
		document.fm_retire.yrs.value = '25';
		document.fm_retire.inf.value = '3.0'; 
		document.fm_retire.yld.value = '7.5';
		document.fm_retire.out.value = 'FULL';
		return false;
	}

	inc = Number(document.fm_retire.inc.value);
	unt = Number(document.fm_retire.unt.value);
	yrs = Number(document.fm_retire.yrs.value);
	inf = Number(document.fm_retire.inf.value);
	yld = Number(document.fm_retire.yld.value);
	out = document.fm_retire.out.value;
    
	var result = document.getElementById('result');
	result.className='BlackText';
	result.style.fontFamily = resultsFontFamily;
    result.style.fontSize = resultsFontSize;
	
	irat = 1 + inf/100; //# inflation ratio
	icf = round2(inc * (Math.pow(irat, unt)));// # future income
	
	
	if (out == "PLAIN")
	{
		msg = "<li>Required Income: \$"+inc+"  (Current Dollars)\n"; 
		msg += "<li>Required Income: \$"+round2S(icf)+"  (Future Dollars)\n"; 
		msg += "<li>Number of Years until retiring :  "+unt+" \n"; 
		msg += "<li>Number of Years after retiring :  "+yrs+" \n"; 
		msg += "<li>Annual Inflation : "+inf+"\%\n"; 
		msg += "<li>Annual Yield on Balance: "+yld+"\% \n"; 
	}
	else
	{ 
		msg = "<table width=400 border=1 align=center style='font-size: "+resultsFontSize+"'>\n";
		msg += "<tr><td>Required Annual Income (Current Dollars) </td><td> \$"+inc+" </td></tr>\n"; 
		msg += "<tr><td>Required Income (Future Dollars) </td><td> \$"+icf+" </td></tr>\n"; 
		msg += "<tr><td>Number of Years Until Retiring</td> <td>  "+unt+" </td></tr>\n"; 
		msg += "<tr><td>Number of Years After Retiring </td><td>  "+yrs+" </td></tr>\n"; 
		msg += "<tr><td>Annual Inflation (on Required Income) </td><td>"+inf+"\%</td></tr>\n"; 
		msg += "<tr><td>Annual Yield on Balance</td><td>"+yld+"\% </td></tr>\n"; 
		msg += "</table>\n";
	}
	msg += "\n";
    
	
	
	iy = yld/100; oinc = icf; 
	if (inf > 0) { icf = icf * (Math.pow(irat, yrs)); }
	
	var bal=0;
	for (i=0; i<yrs; i++)
	{
		if (inf > 0) 
		{ 
			icf = icf/ (1 + inf/100); 
		}
		bal = bal / (1 + iy) + icf;
	}
	
	msg += "<p><div align=left class='BlackText'>You will need \$"+round2S(bal)+"\n"; 
	icf = oinc;
	msg += "(\$"+round2S(bal / Math.pow ((1 + iy), unt))+" invested today)<center><p>\n";
	
	if (out == "FULL") 
	{ 
		msg += "<table width=400 border=1 style='font-size: "+resultsFontSize+"'>\n";
		msg += "<tr><th>Year</th><th>Beginning Balance</th><th>Withdraw</th><th>Interest</th><th>Ending Balance</th></tr>\n";
	}
	rowCounter = 0;
	for (i=1; i<=yrs; i++)
	{
		if(rowCounter % 2 == 0)
		   textColor = "";
		else
		   textColor = " style='background:"+resultsBackgroundColor+"'";
		
		bbal = bal;
		bal = bal - icf; earn = bal * yld/100; bal = bal + earn;
		if (out == "PLAIN")
		{
		   
			msg += "<li"+textColor+">Year "+Math.round(i)+" : Beg Balance "+round2S(bbal)+" Withdraw "+round2S(icf)+" Interest "+round2S(earn)+" End Balance "+round2S(bal)+"</li>\n";
		}
		else
		{
			msg += "<tr"+textColor+"><td> "+Math.round(i)+"  </td><td align='right'> "+round2S(bbal)+" </td><td align='right'> "+round2S(icf)+" </td><td align='right'> "+round2S(earn)+"</td> <td align='right'> "+round2S(bal)+"</td></tr>\n";
		}
		if (inf > 0) { icf = icf * (1 + inf/100); }
		
		rowCounter++;
	}
	
	if (out == "FULL") { msg += "</table>\n"; }

	//msg += "</td></tr></table>";

	result.innerTEXT = "";
	result.innerHTML = msg;
	msg = "";
	
}
/**** Retire calculator ends here *******/
/**************************************************************************************/
/**** Savings calculator starts here */

function show_savings()
{
	amt = Number(document.fm_savings.amt.value);
	dep = Number(document.fm_savings.dep.value);
	intt = Number(document.fm_savings.intt.value);
	yrs = Number(document.fm_savings.yrs.value);
	cmp = document.fm_savings.cmp.value;
	var result = document.getElementById('result');
	
	result.className='BlackText';
	result.style.fontFamily = resultsFontFamily;
    result.style.fontSize = resultsFontSize;
	
	py = 12;
	if (cmp == "Quarterly") { py = 4; }
	if (cmp == "Semiannually") { py = 2; }
	if (cmp == "Annually") { py = 1; }
	tt = yrs * py; 
	
	msg = "<p><div align=left class='BlackText'>Here's an estimate using the figures you entered.<p>Amortized over "+yrs+" years, ";
	msg += " including monthly payments of \$"+round2S(dep);
	msg += ", at "+round3(intt)+"\% compounded "+cmp;
	msg += ", with an initial savings balance of \$"+round2S(amt)+".";
	mi = intt / (py * 100); sav = amt; tcont = amt;
	msg += "<p><table width=400 border=1 align=center style='font-size: "+resultsFontSize+"'>\n<tr><th>Year</th><th>Month</th><th>Ending Balance</th>\n";
	msg += "<th>Average Balance</th></tr>";
	
	tsav=0;
	rowCounter = 0;
	for (i = 1; i <= yrs; i++)
	{
		for (j=0; j<12; j++)
		{
			//#sav = sav * (1 + mi) + dep;
			pnow = 0;
			if (py == 12) { sav = sav * (1 + mi); asav = sav; pnow = 1; }
			if (py == 4 && (j == 2 || j == 5 || j == 8 || j == 11)) 
			{ asav = tsav/3; sav += asav * mi; tsav = 0; pnow = 1; }
			if (py == 2 && (j == 5 || j == 11)) 
			{ asav = tsav/6; sav += asav * mi; tsav = 0; pnow = 1; }
			if (py == 1 && j == 11) 
			{ 
				asav = tsav/12; sav += asav * mi; tsav = 0; pnow = 1; 
			}
			sav = sav + dep; tcont += dep; tsav += sav;
			if (pnow == 1)
			{
			    if(rowCounter % 2 == 0)
		           textColor = "";
		        else
		           textColor = " style='background:"+resultsBackgroundColor+"'";
		   
				msg += "<tr"+textColor+"><td>"+Math.round(i)+"</td><td>"+Math.round(j+1)+"</td><td align='right'>"+round2S(sav)+"</td><td align='right'>"+round2S(asav)+"</td></tr>\n";
			    rowCounter++;
			}
		}
	}

	msg += "</table>\n<p>Initial Investment plus Monthly Contributions: \$"+round2S(tcont);
	msg += "<p>Total Interest Earned: \$"+round2S(sav-tcont);
	msg += "<p>Final Savings Balance: \$"+round2S(sav);
	msg += "";

	msg += "</td></tr></table>";

	result.innerTEXT = "";
	result.innerHTML = msg;
	msg = "";
	
}
/**** Savings calculator ends here *******/
/**************************************************************************************/
/**** Howmuch calculator starts here */

function show_howmuch()
{
	if((document.fm_howmuch.ai.value=="")||(document.fm_howmuch.md.value=="")||(document.fm_howmuch.dp.value=="")||(document.fm_howmuch.ir.value=="")||(document.fm_howmuch.yr.value==""))
	{
		document.fm_howmuch.ai.value = '40000';
		document.fm_howmuch.md.value = '250';
		document.fm_howmuch.dp.value = '20000';
		document.fm_howmuch.pt.value = '1'; 
		document.fm_howmuch.hi.value = '0.5';
		document.fm_howmuch.ir.value = '6.25';
		document.fm_howmuch.yr.value = '30'; 
		document.fm_howmuch.pr.value = '28';
		document.fm_howmuch.dr.value = '36';
		return false;
	}
	ai = Number(document.fm_howmuch.ai.value);
	md = Number(document.fm_howmuch.md.value);
	dp = Number(document.fm_howmuch.dp.value);
	pt = Number(document.fm_howmuch.pt.value);
	hi = Number(document.fm_howmuch.hi.value);
	ir = Number(document.fm_howmuch.ir.value);
	yr = Number(document.fm_howmuch.yr.value);
	pr = Number(document.fm_howmuch.pr.value);
	dr = Number(document.fm_howmuch.dr.value);
	
	var result = document.getElementById('result');
	result.className='BlackText';
	result.style.fontFamily = resultsFontFamily;
    result.style.fontSize = resultsFontSize;
	
	msg = "<table width=400 align=center style='font-size: "+resultsFontSize+"'><p>Here is an estimate of what you can afford.";

	r1 = pr; //#r1 = 28; 
	r2 = dr; //#r2 = 36; 
	mi = ai/12; c1 = mi * r1 / 100;
	c2 = (mi * r2 / 100) - md;
	if (c1 < c2)
	{
   		piti = c1;
	}
	else
	{
        piti = c2;
	}
	msg += "</td></tr>\n<tr><td colspan=2>\n";
 
	j = ir/1200; 
	amv = (1 - Math.pow((1 + j), (-yr * 12)))/j;
 
  

	piti_ov = hi + pt; 

	//dp = dp;

	msg += "</td></tr>\n<tr><td>";
	house = 1000; pmt = piti; loan = 1000;
	for (i=0; i<20; i++) 
	{
		ti = piti_ov/1200 * house;
		loan = (piti - ti) * amv;
		house = loan + dp;
	}

	dpp = dp/house*100; ltv = (house-dp)/house*100;
	msg += "<tr><td>Maximum House value: </td><td>\$"+Math.round(house)+"\n";
	msg += "<tr><td>Maximum Loan value:</td><td> \$"+Math.round(loan)+"\n";
	msg += "<tr><td>Monthly Principal and Interest:</td><td> \$"+round2S(loan/amv)+"\n";
	msg += "<tr><td>Monthly Property Tax and Insurance:</td><td> \$"+round2S(house*piti_ov/1200)+"\n";
	msg += "<tr><td>Down Payment</td><td> "+round2S(dpp)+" \%\n";
	msg += "<tr><td>Loan To Value Ratio (LTV)</td><td> "+round2S(ltv)+" \%\n";

    /*
	if (ltv>80)
	{
		msg += "<tr><td colspan=2><tr><td colspan=2><tr><td colspan=2><p>Since your down payment is less than 20%, you will be required to pay private mortgage insurance.(PMI)";
		house = 1000; pmt = piti; loan = 1000; 

		if (ltv>=85) { pmi = .52; }
		if (ltv>=90) { pmi = .78; }
		if (ltv>=95) { pmi = .90; }
		if (ltv>=97) { pmi = .5;	 DPTooLow='TRUE'; 
		}

		msg += " This will cost you an extra "+round2S(pmi)+" \% of your loan each year. Taking the PMI in to account, the maximum house you can afford is shown below.<tr><td colspan=2><tr><td colspan=2>";
	
		piti_ov += pmi;
		for (i=0; i<20; i++) 
		{
			ti = piti_ov/1200 * house;
			loan = (piti - ti) * amv;
			house = loan + dp;
		}
		dpp = dp/house*100;
		msg += "</td></tr>\n";
		msg += "<tr><td>Maximum House value:</td><td> \$"+Math.round(house)+"\n";
		msg += "<tr><td>Maximum Loan value:</td><td> \$"+Math.round(loan)+"\n";
		msg += "<tr><td>Monthly Principal and Interest:</td><td> \$"+round2S(loan/amv)+"\n";
		msg += "<tr><td>Monthly Property Tax and Insurance:</td><td> \$"+round2S(house*(piti_ov-0.5)/1200)+"\n";
		msg += "<tr><td>Monthly PMI:</td><td> \$ "+round2S(house*pmi/1200)+"\n";
		msg += "<tr><td>Down Payment</td><td> "+round2S(dpp)+" \%\n";
		msg += "</td></tr>\n";

	}


	if (dpp<3 && DPTooLow=='TRUE')
	{
		msg += "<tr><td colspan=2><tr><td colspan=2><p><p>Since your down payment is below 3%, you should consider applying for an FHA or VA insured loan. There may also be local funding programs that can help you out too. For more information on those programs you should contact a realtor or mortgage broker in your area.<p>";
	}		       


		msg += "<tr><td colspan=2><tr><td><p>The figures shown above do not include closing costs and any points your lender may charge.</td></tr></table>";
    */
		msg += "</td></tr></table>";

		result.innerTEXT = "";
		result.innerHTML = msg;
		msg = "";
	
}
/**** Howmuch calculator ends here *******/
/**************************************************************************************/
/**** Monthly calculator starts here */

function show_monthly()
{
	if((document.form.prin.value=="")||(document.form.prin.value<=0)||(document.form.intt.value=="")||(document.form.intt.value<=0)||(document.form.len.value=="")||(document.form.len.value<=0)||(document.form.tax.value=="")||(document.form.tax.value<=0)||(document.form.insure.value=="")||(document.form.insure.value<=0)||(document.form.startyr.value=="")||(document.form.startyr.value<=0))
	{
		document.form.prin.value = '200000';
		document.form.intt.value = '6.5';
		document.form.len.value = '30';
		document.form.tax.value = '2000'; 
		document.form.insure.value = '600';
		document.form.startmon.value = 'Jan';
		document.form.startday.value = '1'; 
		document.form.startyr.value = '2000';
		document.form.show_amort.value = 'No';
		return false;
	}

	prin = Number(document.form.prin.value);
	intt = Number(document.form.intt.value);
	len = Number(document.form.len.value);
	tax = Number(document.form.tax.value);
	insure = Number(document.form.insure.value);
	startmon = Number(document.form.startmon.value);
	startday = Number(document.form.startday.value);
	startyr = Number(document.form.startyr.value);
	show_amort = document.form.show_amort.value;

    var result = document.getElementById('result');
    
	result.className='BlackText';
	result.style.fontFamily = resultsFontFamily;
    result.style.fontSize = resultsFontSize;
	
	currency = "\$";

	//# Is the payment interval Biweekly or Monthly?

	d = new Date();
		
	month_day=d.getDay();
	month=d.getMonth();
	year=d.getFullYear();
	//IsDST

	//year += 1900;

	month++;
	startmonth = startmon;


	monthsleft = startmonth - month;
	yearsleft = startyr + len - year;
	if (monthsleft < 0) {monthsleft += 12;}
	if (startmonth < month) {yearsleft--;}
	if (yearsleft < 0) {yearsleft = "N/A"; monthsleft = "N/A";}

	/*# check and make sure that we got something entered for these
	# items, and that the value of the loan, interest, and loan term
	# is greater than 0.*****/



	//# Note: for biweekly, calculate monthly payment first, then halve it.
	freq = 12;

	monthly_interest = intt / (freq * 100);

	numpayments = len * freq;

	minusn = (numpayments * (-1));

	//# And heeeeeere's the formula to calculate the payment:
	pay = prin * (monthly_interest / (1 - Math.pow((1 + monthly_interest), (minusn))));
	payplus = pay + (tax / 12) + (insure / 12);

	//#biweekly payment
	//pay = pay / 2 if interval eq "Bi-Weekly";

	pay = round2(pay);
	payplus = round2S(payplus);

	//# Generate the HTML form to be printed out so someone can do another
	//# calculation immediately.

	realint = monthly_interest;

	//#realint = int / (26 * 100) if interval eq "Bi-Weekly";
	j = startmon;
	
	k = j;
	curyear = startyr;

	amort_html = "";
	amort_html += "You have "+yearsleft+" years "+monthsleft+" months left on your loan.<br><br></td></tr><tr><td colspan=2><table width=400 border=1 align=center style='font-size: "+resultsFontSize+"'><tr><th>Month</th><th>Principal Paid</th>	       <th>Interest Paid</th><th>Loan Balance</th></tr>";

bal=1;totalint=0;startmon2="";
	rowCounter=0;
	for (i = prin; i > 0; i = bal)
	{
		if (k == 13) {k = 1; curyear++;}
		if (k == 1) {startmon2 = "Jan";}
		if (k == 2) {startmon2 = "Feb";}
		if (k == 3) {startmon2 = "Mar";}
		if (k == 4) {startmon2 = "Apr";}
		if (k == 5) {startmon2 = "May";}
		if (k == 6) {startmon2 = "Jun";}
		if (k == 7) {startmon2 = "Jul";}
		if (k == 8) {startmon2 = "Aug";}
		if (k == 9) {startmon2 = "Sep";}
		if (k == 10) {startmon2 = "Oct";}
		if (k == 11) {startmon2 = "Nov";}
		if (k == 12) {startmon2 = "Dec";}
		
		curint = i * realint;
		p_curint = round2(curint);
		curprin = pay - curint;
		p_curprin = round2(curprin);
		bal = i - curprin;
		p_bal = round2(bal);
		////////printbal = &commify(p_bal);
		printbal = p_bal;
		totalint += curint;
		
		if (bal > 0)
		{
			if(rowCounter % 2 == 0)
		           textColor = "";
		    else
		           textColor = " style='background:"+resultsBackgroundColor+"'";
			amort_html += "<tr"+textColor+"><td>"+startmon2+" "+curyear+"</td><td align='right'>"+currency+round2S(p_curprin)+"</td><td align='right'>"+currency+round2S(p_curint)+"</td><td align='right'>"+currency+round2S(printbal)+"</td></tr>\n" ;
		    rowCounter++;
		}
		k++;
    }


	amort_html += "</table></td></tr>";

	//# Add some commas...
	prin = round2(prin);
	//prin = &commify("prin");

	totalint = round2S(totalint);
	//totalint = &commify("totalint");


	html = "<table width=400 border=0 align=center style='font-size: "+resultsFontSize+"'><tr valign=top><td>Your estimated monthly PITI payment is "+currency+payplus+"<p>(PITI=Principal, Interest, Taxes and Insurance)<p>";

	loan = "<tr valign=top><td>Total interest paid over life of loan is "+currency+totalint+"<br><br>";

	msg = html;
	if(show_amort == "Yes")
	{
		msg += loan;
		msg += amort_html ;
	}



	if(show_amort == "Yes") { vis = "visible"; }
	if(show_amort == "No") { vis = "hidden"; }



		result.innerTEXT = "";
		result.innerHTML = msg;
		msg = "";
	
}
/**** Monthly calculator ends here *******/
/**************************************************************************************/
/**** Autoloan calculator starts here */

function show_autoloan()
{
	
	if((document.fm_autoloan.pur.value=="")||(document.fm_autoloan.irt.value=="")||(document.fm_autoloan.cdp.value=="")||(document.fm_autoloan.tia.value==""))
	{
		document.fm_autoloan.pur.value = '20000';
		document.fm_autoloan.irt.value = '6.5';
		document.fm_autoloan.cdp.value = '2000';
		document.fm_autoloan.tia.value = '4000'; 
		document.fm_autoloan.trm.value = '5';
		document.fm_autoloan.pay.value = "";

		return false;
	}

	pur = Number(document.fm_autoloan.pur.value);
	irt = Number(document.fm_autoloan.irt.value);
	cdp = Number(document.fm_autoloan.cdp.value);
	tia = Number(document.fm_autoloan.tia.value);
	trm = Number(document.fm_autoloan.trm.value);
	
	//#find missing form field and set default values
		
	pay = pur-cdp-tia;
	//#pay = (pay+pay*(query{'irt'}/100)*query{'trm'})/(query{'trm'}*12);

	irt = 	irt/(100*12);
	pay = (pay)*irt/(1-power(1+irt,-trm*12));
		
	pay = round2S(pay);
	if (trm) 
	{
		month = trm;
	}
	document.fm_autoloan.pay.value = pay;
    
}
function power(no,po)
{

		if (po == 0) 
		{
			return 0;
		}
		mi= 1;
		if (po < 0) 
		{
			po *=-1;
			mi = -1;
		}
		result=1;
		for(i=1;i<=po;i++)
		{
			result *= no;
		}
		if (mi == -1) 
		{
			result = 1/result;
		}

		return result;
}
/**** Autoloan calculator ends here *******/
/**************************************************************************************/
/**** Lease calculator starts here */

function show_lease()
{
	
	msr = Number(document.fm_lease.msr.value);
	neg = Number(document.fm_lease.neg.value);
	dwn = Number(document.fm_lease.dwn.value);
	lln = Number(document.fm_lease.lln.value);
	intt = Number(document.fm_lease.intt.value);
	res = Number(document.fm_lease.res.value);
	tax = Number(document.fm_lease.tax.value);

    var result = document.getElementById('result');
	
	result.className='BlackText';
	result.style.fontFamily = resultsFontFamily;
    result.style.fontSize = resultsFontSize;
	
	msg = "<align=left>Automotive Lease Final Numbers<br>";
   
    
	msg += "<table cellpadding=2 border=1 width=420 align=center style='font-size: "+resultsFontSize+"'>\n"; 
	msg += "<tr><td>Capitalized Cost: </td><td align='right'>\$ "+neg+"</td><td> (Negoiated Price)</td></tr>";
	
	lp = neg - dwn; mf = intt/2400;
	
	msg += "<tr><td>Lease Price: </td><td align='right'> \$ "+round2S(lp)+" </td><td>(after \$"+dwn+" down payment)</td></tr>\n";
	
	msg += "<tr><td>Residual Value: </td><td align='right'> \$ "+round2S(res)+" </td><td>(after "+lln+" months)</td>\n";
	
	df = ( lp - res ) / lln;
	
	msg += "<tr><td>Depreciation Fee: </td><td align='right'>\$ "+round2S(df)+"</td><td>of lease payment</td>";
	lf = ( lp + res) * mf;
	msg += "<tr><td>Lease Fee: </td><td align='right'>\$ "+round2S(lf)+" </td><td>of lease payment</td></tr>";
	mp = df + lf;
	//("<tr><td>Monthly Payment : </td><td>\$ %6.2f </td><td> without tax</td></tr>",mp);

	//# utax = (tax/100) * (lp - res);
	utax = (tax/100) * mp;
	msg += "<tr><td>Usage Tax: </td><td align='right'>\$ "+round2S(utax)+" </td><td>with tax</td></tr>";
	msg += "<tr><td>Monthly Payment: </td><td align='right'><b>\$ "+round2S(mp+utax)+"</b></td><td>&nbsp;</td></tr></table>";
  
	result.innerTEXT = "";
	result.innerHTML = msg;
	msg = "";
    
}

/**** Lease calculator ends here *******/
/**************************************************************************************/
/**** Biweekly calculator starts here */

function show_biweekly()
{
	if((document.fm_biweekly.prin.value=="")||(document.fm_biweekly.intt.value=="")||(document.fm_biweekly.term.value==""))
	{
		document.fm_biweekly.prin.value = '20000';
		document.fm_biweekly.intt.value = '6.5';
		document.fm_biweekly.term.value = '2000';
		document.fm_biweekly.full.value = 'no';

		return false;
	}

	prin = Number(document.fm_biweekly.prin.value);
	intt = Number(document.fm_biweekly.intt.value);
	term = Number(document.fm_biweekly.term.value);
	full = document.fm_biweekly.full.value;
    
	var result = document.getElementById('result');
	
	result.className='BlackText';
	result.style.fontFamily = resultsFontFamily;
    result.style.fontSize = resultsFontSize;
	
	tt = term * 12; ir = intt / 1200; 

	bir = intt / 2600; 

	temp = (1.0 / Math.pow((1 + ir), tt));
	paymt = prin * ir / (1 - temp);
	tint = paymt * tt - prin; 
	aint = tint / term /12;
	bal = prin; yrint = 0; yrprin = 0;
	bbal = prin; byrint = 0; byrprin = 0;

	content = "";
	
	rowCounter=0;
	for (i = 1; i <= term; i++ )
	{
		for (j = 1; j <= 12; j++) 
		{	
			mint = ir * bal; bal = bal - paymt + mint;
			yrint = yrint + mint; mprin = paymt - mint;
			yrprin = yrprin + mprin;
		}

		for (j = 1; j <= 26; j++) 
		{	
  			bmint = bir * bbal;
			bbal = bbal - (paymt/2) + bmint;
			byrint = byrint + bmint; bmprin = (paymt/2) - bmint;
			byrprin = byrprin + bmprin;
		}		

		if (bbal < 0) { bbal = 0; }
	  
		if (full == 'yes') 
		{
		    if(rowCounter % 2 == 0)
		        textColor = "";
		    else
		        textColor = " style='background:"+resultsBackgroundColor+"'";
			content += "<tr"+textColor+"><td>"+Math.round(i)+" </td><td align='right'> \$ "+round2S(bal)+" </td> <td align='right'>\$ "+round2S(bbal)+"</td></tr>"; 
			rowCounter++;
		}
	}

	msg = "<td colspan='3' div align=left style='font-size: "+resultsFontSize+"'><b>Mortgage Payment Comparison</b></div></table><table width='400' border='1'><table width='400' border='1'  style='font-size: "+resultsFontSize+"'>";
	
	bpaymt = paymt/2;

	btint = byrint;
	baint =btint/ (term*26);
	int_sa = tint-btint;
	
	msg += "<tr><td><center>Regular Payment<center></td><td><center>Biweekly Payment<center></td></tr>";
	
	msg += "<tr><td>Payment: \$"+round2S(paymt)+"</td><td>Payment: \$"+round2S(bpaymt)+"</td>\n<tr class='BlackText'><td>Total Interest: \$"+round2S(tint)+"</td><td>Total Interest: \$"+round2S(btint)+"</td>\n<tr class='BlackText'><td>Interest Savings: <b>\$0.00</b></td><td>Interest Savings: <b>\$"+round2S(int_sa)+"</b></tr></table>\n";

	bpaymt = paymt/2;
	btint = byrint;
	baint =btint/ (term*26);
	int_sa = tint-btint;

	msg += "</td></tr>";
	if (full == 'yes') 
	{
		msg += "<br><tr><td colspan=3 div align=left><b>Yearly Mortgage Amortization Comparison</b></td></tr>\n<tr><td colspan='3'><table width='393' border='1' cellspacing='0' cellpadding='0' style='font-size: "+resultsFontSize+"'><tr><td><div align='center'>Year</div></td><td><div align='center'>Standard</div></td><td><div align='center'>Biweekly</div></td></tr>";
		msg += content;			
	}

	result.innerTEXT = "";
	result.innerHTML = msg;
	msg = "";
    
}

/**** Biweekly calculator ends here *******/