// JavaScript Document
/***************************
(c) 2008 www.TUFaT.com
All Rights Reserved. Please
do not re-sell/re-distribute
this software.
***************************/
function CalcPrepay(form)
{
	
	var M = form.M.value;
	var P = form.P.value;
	var R = form.R.value;
	var L = form.L.value;
	var D = form.D.value;
	var Y = form.Y.value;
	var X = form.X.value;
	var S = form.S.value;
	
	var StrOut='';
	StrOut = StrOut + "<table border =2>";
	StrOut = StrOut + "<tr><td>Loan Balance<td>"+formatNumberDec(L,2,1)+"<br />";
	StrOut = StrOut + "<tr><td>Current Payment<td>"+formatNumberDec(P,2,1)+"<br />";
	StrOut = StrOut + "<tr><td>Additional Payment<td>"+formatNumberDec(M,2,1)+"<br />";
	StrOut = StrOut + "<tr><td>Loan Interest Rate<td>"+formatNumberDec(R,2,1)+" %<br />";
	StrOut = StrOut + "<tr><td>Loan Interest Deductibilty<td>"+D+"<br />";
	StrOut = StrOut + "<tr><td>Investment Return Rate<td>"+formatNumberDec(Y,2,1)+" %<br />";
	StrOut = StrOut + "<tr><td>Tax Bracket <td>"+formatNumberDec(X,2,1)+" %<br />";
	StrOut = StrOut +  "<tr><td>Investment Type<td>" +S;
	StrOut = StrOut +  "</table><hr><br />";
	StrOut = StrOut +  "<table border =2><tr>";
	StrOut = StrOut +  "<td colspan=4 align=center><b>PREPAYING</b>";
	StrOut = StrOut +  "<td colspan=4 align=center><b>INVESTING</b><tr>";
	StrOut = StrOut +  "<th align=center>Month<th align=center>Loan Bal (P)<th align=center>Int Paid (P)<th align=center>Tax Ded (P)";
	StrOut = StrOut +  "<th align=center>Loan Bal (I)<th align=center>Int Paid (I)<th align=center>Tax Ded (I)<th align=center>Inv Bal (I)<br />";
	Pbal = L; 
	Lbal = L; 
	mi = R/1200; 
	tf = X/100; 
	my = 1 + Y/1200; 
	tc = -1;
	m=0;
	Ptint=0;
	Ltint=0;
	Linv=0;
	Yyld=0;
	while(Pbal > (eval(P) + eval(M)) && m < 600)
	{
	  m++; 
	  Pint = Pbal * mi; 
	  Ptax = Pint * tf; 
	  Ptint = eval(Ptint) + eval(Pint);
	  Pbal = eval(Pbal) + eval(Pint) - eval(P) - eval(M);
	  Lint = Lbal * mi; 
	  Ltax = Lint * tf; 
	  Ltint = eval(Ltint) + eval(Lint);
	  if (D == "No") { 
		  Ltax = 0; 
		  Ptax = 0; 
	  }
	  Lbal = eval(Lbal) + eval(Lint) - eval(P);
	  Myld = Linv * Y/1200;
	  Linv = eval(Linv) + eval(Myld) + eval(M) + (eval(Ltax) - eval(Ptax));
	  if (S == "After-Tax")
	  {
		tc++; 
		Yyld = eval(Yyld) + eval(Myld);
		if (tc == 12)
		{
		  Ytax = Yyld * X/100;
	
		  Linv = Linv - Ytax; 
		  tc = 0; 
		  Yyld = 0;
		  StrOut = StrOut + "<tr><td><strong>TAX</strong><td>-<td>-<td>-<td>-<td>-<td>-<td><strong>"+formatNumberDec(Ytax,2,1)+"</strong><br />";
		}
	  }
	  StrOut = StrOut + "<tr><td>"+m+"<td>"+formatNumberDec(Pbal,2,1)+"<td>"+formatNumberDec(Pint,2,1)+"<td>"+formatNumberDec(Ptax,2,1);
	  StrOut = StrOut + "<td>"+formatNumberDec(Lbal,2,1)+"<td>"+formatNumberDec(Lint,2,1)+"<td>"+formatNumberDec(Ltax,2,1)+"<td>"+formatNumberDec(Linv,2,1);
	}
	lm = m; 
	cLbal = Lbal;
	while(Lbal > P)
	{
	  lm++;
	  Lint = Lbal * mi; 
	  Ltax = Lint * tf;
	  Ltint = eval(Ltint) + eval(Lint);
	  if (D == "No") { 
		  Ltax = 0; 
		  Ptax = 0; 
	  }
	  Lbal = eval(Lbal) + eval(Lint) - P;
	}
	
	StrOut = StrOut +  "</table><br />";
	StrOut = StrOut +  "<hr><strong>Summary</strong><table border=2><tr><td>-<th align=center>Prepay<th align=center>Invest<br />";
	StrOut = StrOut + "<tr><td>Total Interest ("+(m/12)+" yrs)<td>"+formatNumberDec(Ptint,2,1)+"<td>"+formatNumberDec(Ltint,2,1)+"<br />";
	StrOut = StrOut + "<tr><td>Loan Payback Time (Years)<td>"+m/12+"<td>"+lm/12+"<br />";
	StrOut = StrOut +  "</table><br />";
	
	StrOut = StrOut + "<strong>Investment - Loan Balance:  "+formatNumberDec((Linv - cLbal),2,1)+"</strong><br />";
	if (Linv - cLbal > 0)
	{
	  StrOut = StrOut +  "<h3>In your case it is wiser to invest than to prepay</h3><br />";
	}
	else
	{
	  StrOut = StrOut +  "<h3>In your case it is wiser to prepay than to invest</h3><br />";
	}
	
	StrOut = StrOut +  "</center><p><b>Assumptions:</b> This estimation assumes you will pay a fixed amount	every month to either your loan prepayment or to some type of after-tax	or tax deferred investment. If it is after-tax it will be more available to you, but every 12 months you will be forced to pay tax on your annual yield. This also assumes that you take the extra tax savings by NOT prepaying (prepaying lowers your deductible interest payments) and you put the tax savings into the investment as well. This also assumes that the top income tax rate will stay the same for a long time. This tax rate should also include your state income tax too if applicable.";
	
	document.getElementById("results").innerHTML=StrOut;
	document.getElementById("results").style.display='block';
}		
