// JavaScript Document
/***************************
(c) 2008 www.TUFaT.com
All Rights Reserved. Please
do not re-sell/re-distribute
this software.
***************************/
function CalcTaxDef(form)
{
  
  var StrOut='';
  StrOut = StrOut +  "<strong>Future Estimations</strong><br />";
  var abal = form.abal.value;
  var pymt = form.pymt.value;
  var time = form.time.value;
  var intr = form.intr.value;
  var intr2 = form.intr2.value;
  var infl = form.infl.value;
  var raiz = form.raiz.value;
  var wdrw = form.wdrw.value;
  
  StrOut = StrOut +  "<strong>Inputs</strong><table border=1 cellspacing=0 cellpadding=2>";
  StrOut = StrOut +  "<tr><td>Beginning Balance of Account<td>$ "+abal+"<br />"; 
  StrOut = StrOut +  "<tr><td>Initial monthly contribution <td>"+pymt+"<br />"; 
  StrOut = StrOut +  "<tr><td>Time until retirement<td> "+time+" years<br />"; 
  StrOut = StrOut +  "<tr><td>Estimated annual yield <td> "+intr+" % <br />"; 
  StrOut = StrOut +  "<tr><td>Estimated annual inflation <td> "+infl+" % <br />"; 
  StrOut = StrOut +  "<tr><td>Estimated annual contribution increase <td> "+raiz+" % <br />"; 
  StrOut = StrOut +  "<tr><td>Desired monthly withdrawal <td>$ "+wdrw+" (current dollars)<br />"; 
  StrOut = StrOut +  "</table><hr><br />";
  intr = intr/1200; 
  intr2 = intr2/1200; 
  raiz = raiz/100;
  infl = infl/100; 
  adj = 1; 
  StrOut = StrOut +  "<table border=1 cellspacing=0 cellpadding=2>";
  StrOut = StrOut +  "<tr><th>Year<th>Monthly Contrib<th>Actual Balance";
  StrOut = StrOut +  "<th>Current Dollar Balance<br />";
  for (i=0; i<time; i++)
  {
    for (j=0; j<12; j++)
    {
     abal = abal * (1 + eval(intr)) + eval(pymt); 
    }
    pymt = pymt * (1 + eval(raiz)); 
    adj = adj/(1 + infl); 
	pbal = abal * adj; 
    StrOut = StrOut + "<tr><td> "+(i+1)+" <td>$ "+formatNumberDec(pymt,2,1)+" <td>$ "+formatNumberDec(abal,2,1)+" <td>$ "+formatNumberDec(pbal,2,1)+"<br />";
  }
  
  awdr = wdrw/adj;
  StrOut = StrOut +  "</table><hr><p align=left><strong>At this time:</strong></p>";
  StrOut = StrOut + "<p align=left>Your last monthly contribution was: $ "+formatNumberDec(pymt,2,1)+"</p>";
  StrOut = StrOut + "<p align=left>Your current required withdrawal is: $ "+formatNumberDec(awdr,2,1)+"</p>";

  StrOut = StrOut +  "<p align=left><strong>Let's start taking money out and see how long it lasts</strong></p>";
  if (eval(intr2*1200) > 8)
  { 
  	StrOut = StrOut +  "<p align=left><b><blink>WARNING!</blink>That is a high yield to maintain during retirement </b></p><p align=left><strong>Your specified annual yield of "+formatNumberDec(intr2*1200,2,1)+" % may not be feasible if you regularly expect to withdraw funds from your account. These typically represent stock or more volatile bond funds which are quite unpredictable. You should consider estimated a lower yield once you are retirimg and withdrawing funds. </strong></p>";
  }
    

  StrOut = StrOut +  "<table border=1 cellspacing=0 cellpadding=2>"; 
  obal = abal;
  StrOut = StrOut +  "<tr><th>Year<th>Balance<th>Annual Withdrawal";
  yr=0;
  while(abal > 0)
  {
    yr++; 
	annw = 0;
    for (j=0; j<12; j++)
    {
		if (abal * (1 + eval(intr2)) > awdr)
		{ 
			abal = abal * (1 + eval(intr2)) - awdr; 
			annw = eval(annw) + eval(awdr); 
		} else { 
			annw = eval(annw) + eval(abal); 
			abal = 0; 
			j = 12; 
		}
    }
    StrOut = StrOut + "<tr><td> "+yr+" <td> $ "+formatNumberDec(abal,2,1)+" <td> $ "+formatNumberDec(annw,2,1)+"<br />";
    awdr = awdr * (1 + infl); 
    if (abal > (obal * (1 + infl)))
    {
      StrOut = StrOut +  "</table><strong>Congratulations! You are set for life!</strong><br />";
      StrOut = StrOut +  "Your balance is earning more than you are taking out<br />";
      abal = 0;
    }
    obal = abal
  }
  StrOut = StrOut +  "</table><br />";

document.getElementById("results").innerHTML=StrOut;
document.getElementById("results").style.display='block';
}
