// JavaScript Document
/***************************
(c) 2008 www.TUFaT.com
All Rights Reserved. Please
do not re-sell/re-distribute
this software.
***************************/
function CalcRoth(form)
{
	var CB = form.CB.value;
	var CA = form.CA.value;
	var AC = form.AC.value;
	var RR = form.RR.value;
	var RA = form.RA.value;
	var IR = form.IR.value;
	var RY = form.RY.value;
	var IN = form.IN.value;
	var RI = form.RI.value;
	var CR = form.CR.value;
	var StrOut='<font color="#FF0000">Result</font><br /><br />';
	var is='';
	var leg='';
	if (CA > 0)
	{
	  nyrs = RA - CA;
	  roth_ac = AC * (1 - CR/100);
	  if (roth_ac  > 4000) { 
		StrOut = StrOut + "<strong>The maximum after-tax annual contribution to a Roth IRA is \4000 for couples (\2000 if single)</strong>";
		roth_ac = 4000; 
	  }
	  if (AC > 4000)
	  {
		StrOut = StrOut + "<strong>The maximum before-tax annual contribution to an IRA is $4000 for couples ($ 2000 if single)</strong>";
		AC = 4000;
	  }
	  inv_yld = IR/100;
	  ret_yld = RY/100;
	  inf_yld = IN/100;
	  req_inc = RI;
	  StrOut=StrOut+"<strong>After-tax Roth contributions: $ "+formatNumberDec(roth_ac,2,1)+"</strong>";
	  trad = CB; 
	  roth = trad;
	  roth_tax = CB *  CR/100;
	  roth_taxed = CB - roth_tax;
	  for (i=0; i<nyrs; i++) 
	  { 
		 req_inc = req_inc * (1 + eval(inf_yld));
		 trad = eval(trad * (1 + eval(inv_yld))) + eval(AC); 
		 roth = eval(roth * (1 + eval(inv_yld))) + eval(roth_ac);
		 roth_taxed = eval(roth_taxed * (1 + eval(inv_yld))) + eval(roth_ac);
	  }
	  roth_taxed_s = roth_taxed;
	  trad_s = trad;
	  roth_s = roth;
	  trad_wd_s = trad* (1-RR/100);
	  req_inc_s = req_inc;
	  tc = 100;
	  tt = 100 * (1-RR/100);
	  rt = 100*roth_taxed/trad;
	  ru = 100*roth/trad;
	StrOut=StrOut+"<table border=0 cellspacing=8 align=center><tr><td valign=bottom align=center><img src=images/dotb.gif width=10 height="+tc+">&nbsp;<img src=images/dotr.gif width=10 height="+tt+" >";
	StrOut=StrOut+"<br /><strong>Traditional</strong><td valign=bottom align=center><img src=images/dotb.gif width=10 height="+rt+" >&nbsp;<img src=images/dotr.gif width=10 height="+rt+" >";
	StrOut=StrOut+"<br /><strong>Roth Taxed</strong><td valign=bottom align=center><img src=images/dotb.gif width=10 height="+ru+" >&nbsp;<img src=images/dotr.gif width=10 height="+ru+" >";
	StrOut=StrOut+"<br /><strong>Roth Untaxed</strong></table>";
	StrOut=StrOut+"<table border=1 width=100% cellpadding=2 cellspacing=0 align=center><tr><th align=center>Plan<th align=center>Extra 1998 Tax** <th align=center><img src=images/dotb.gif width=10 height=10><br />Balance at Age RA";
	StrOut=StrOut+"<th align=center><img src=images/dotr.gif width=10 height=10><br />Total After-Tax Retirement Withdrawals";
	StrOut=StrOut+"<tr><td align=center>Traditional IRA<td align=center>0<td align=center>"+formatNumberDec(trad_s,2,1)+"<td align=center>"+formatNumberDec(trad_wd_s,2,1)+" *";
	StrOut=StrOut+"<tr><td align=center>Roth IRA - pay tax from rollover<td align=center>"+formatNumberDec(roth_tax,2,1)+"<td align=center>"+formatNumberDec(roth_taxed_s,2,1)+"<td align=center>"+formatNumberDec(roth_taxed_s,2,1)+"";
	StrOut=StrOut+"<tr><td align=center>Roth IRA - untaxed**<td align=center>"+formatNumberDec(roth_tax,2,1)+"<td align=center>"+formatNumberDec(roth_s,2,1)+"<td align=center>"+formatNumberDec(roth_s,2,1)+"	</table>";
	StrOut=StrOut+"* = subject to withdrawal minimums starting at age 70 1/2.<br />** = untaxed option means you pay the tax out of pocket instead of from the rollover proceeds. If the rollover is in 1998 you have <b>four</b> years to pay the taxes.";
	StrOut=StrOut+"<P><strong>You will need "+formatNumberDec(req_inc_s,2,1)+" as income when you are "+RA+" </strong><table border=1 width=100% cellpadding=2 cellspacing=0 >";
	StrOut=StrOut+"<tr><th align=center>Age<th align=center>Withdrawal<th align=center>Balance (Roth)<br /><img src=images/dotr.gif width=10 height=10><th align=center>Balance (Roth**)<br /><img src=images/dotg.gif width=10 height=10><th align=center>Balance (Trad)<br /><img src=images/dotb.gif width=10 height=10>";
	
	var let = Array(16,15.3,14.6,13.9,13.2,12.5,11.9,11.2,10.6,10.0,9.5,8.9,8.4,7.9,7.4,6.9,6.5,6.1,5.7,5.3,5.0,4.7,4.4,4.1,3.9,3.7,3.4,3.0,2.8,2.7,2.5,2.3,2.1,1.9,1.8,1.6,1.4,1.3,1.1,1.0);
	
	tax_ratio = 1 + RR/100;
	base200 = trad/50;
	for (a = RA; a<91; a++)
	{
	  req_inc_tax = req_inc * tax_ratio;
	  if (a >= 70 && let[a - 70] > 0)
	  {
		if (req_inc_tax * let[a -70] < trad) 
		{ 
			pen = "***"; 
			leg = 1; 
			trad = trad - trad/let[a -70]; 
		} else { 
			pen = ""; 
			trad = trad - req_inc_tax; 
		}	  
	  } else { 
		  pen = ""; 
		  trad = trad - req_inc_tax; 
	  }
	  roth = roth - req_inc;
	  roth_taxed = roth_taxed - req_inc;
	  StrOut=StrOut+"<tr><td align=center>"+a+"<td align=center>"+formatNumberDec(req_inc,2,1)+"<td align=center>"+formatNumberDec(roth_taxed,2,1)+"<td align=center>"+formatNumberDec(roth,2,1)+"<td align=center>"+formatNumberDec(trad,2,1)+" "+pen;
	  req_inc = req_inc * (1 + eval(inf_yld));
	  tv = trad/base200;
	  rtv = roth_taxed/base200;
	  ruv = roth/base200;
	  is = is + "<img src=images/dotr.gif width=5 height="+rtv+" >";
	  is = is + "<img src=images/dotg.gif width=5 height="+ruv+" >";
	  is = is + "<img src=images/dotb.gif width=5 height="+tv+" >";
	  roth = roth * (1 + ret_yld);
	  trad = trad * (1 + ret_yld);
	  roth_taxed = roth_taxed * (1 + ret_yld);
	  
	}
	StrOut = StrOut + "</table><p align=center>"+is+"</p>";
	if (leg == 1)
	{
	  StrOut = StrOut + "<hr>*** = You must withdraw more from a traditional IRA because you have not withdrawn the minimum";
	}
}
document.getElementById("results").innerHTML=StrOut;
document.getElementById("results").style.display='block';
}
