function calculate(form) {

//Create new window
var win = window.open("", "win", "width=800,height=600,scrollbars, resizable");
win.document.open("text/html", "_blank");

win.document.write('<html><title>Executive Benefits Online Rate Calculator</title><link rel="stylesheet" href="/../css/calculator.css" type="text/css" media="screen" /><body>')

win.document.write('<p><a href = "javascript:window.close()">Close This Window</a></p>')

// (user-changeable) variables
var contribution = form.cashcontribution.value//= 165000
var age = form.age.value//= 52
var retirement_draw = form.retirementdraw.value// = 125000
var preretirement_tax_rate = form.preretirementtax.value// = 0.40
var postretirement_tax_rate = form.postretirementtax.value// = 0.40
var plan_investment_return = form.planreturn.value// = 0.05
var no_plan_investment_return = form.noplanreturn.value// = 0.10
var retirement_age = form.retirementage.value//62

win.document.write('<p>')
win.document.write('Annual Contribution = <b>' + contribution + '</b>')
win.document.write('</br>Annual Retirement Withdrawal = <b>' + retirement_draw + '</b>')
win.document.write('</br>Current Age = <b>' + age + '</b>')
win.document.write('</br>Retirement Age = <b>' + retirement_age + '</b>')
win.document.write('</br>Pre-retirement Tax Rate = <b>' + preretirement_tax_rate + '</b>')
win.document.write('</br>Post-retirement Tax Rate = <b>' + postretirement_tax_rate + '</b>')
win.document.write('</p>')

win.document.write('<table><tr><td width=50></td><td><table><tr><td height=10><font face="Verdana" size=2><b><u>No Retirement Plan</p></u></b>')
win.document.write('<p>Annual contribution is invested after tax. <br> Investment income is taxed.<br></p>') 
// + '<br>Pre-retirement Tax Rate = ' + preretirement_tax_rate + '<br>Post-retirement Tax Rate = ' + postretirement_tax_rate + '<br>Investment Return = 10%
win.document.write('<p>Investment Return = <b>' + no_plan_investment_return + '</b></p>')
win.document.write('<br></td></tr></table></td>')
win.document.write('<td width=65></td>') 
win.document.write('<td><table><tr><td height=10 ><b><font face="Verdana" size=2><u>Retirement With 412(i) Plan</p></u></b>') 
win.document.write('<p>Annual contribution is invested before tax. <br> Pay no taxes until retirement. <br>At normal retirement age, the 412(i) balance is <br>rolled into an IRA or profit sharing plan account.</p>') 
//Pre-retirement Tax Rate = 40%<br>Post-retirement Tax Rate = 40%
win.document.write('<p><br>412(i) Plan Investment Return = <b>' + plan_investment_return + '</b></p>')
win.document.write('<p>Defined Contribution Investment Return = <b>' + no_plan_investment_return + '</b></p>')

win.document.write('</td></tr></table></td></tr></table>')
//Create tabl
win.document.write("<table border=0 cellpadding=0 cellspacing=0 width='100%' bordercolor=#fff style='padding:0px;margin:0px;'>")
win.document.write('<tr><td><b>Year</td><td><b>Age</td><td><b>Annual<br>Cash<br>Cont.</td><td><b>Taxes</td><td><b>Fund</td><td><b>Before<br>Tax<br>Yield</td><td><b>Taxes</td><td><b>After<br>Tax<br>Yield</td><td><b>Annual<br>Draw</td><td><b>Annual<br>Cash<br>Cont.</td><td><b>Taxes</td><td><b>Fund</td><td><b>Before<br>Tax<br>Yield</td><td><b>Taxes</td><td><b>After<br>Tax<br>Yield</td><td><b>Before<br>Tax<br>Draw</td><td><b>Taxes</td><td><b>After<br>Tax<br>Draw</td></tr>')



var d = new Date()
var year = d.getFullYear()

//var contribution = form.cashcontribution.value//= 165000
//var age = form.age.value//= 52
//var retirement_draw = form.retirementdraw.value// = 125000
//var preretirement_tax_rate = form.preretirementtax.value// = 0.40
//var postretirement_tax_rate = form.postretirementtax.value// = 0.40
//var plan_investment_return = form.planreturn.value// = 0.05
//var no_plan_investment_return = form.noplanreturn.value// = 0.10
//var retirement_age = form.retirementage.value//62


//Do some error checking
if (contribution < 0)
	contribution = 0
if (age < 0)
	age = 0
if (retirement_draw < 0)
	retirement_draw = 0
if (preretirement_tax_rate < 0)
	preretirement_tax_rate = 0
if (postretirement_tax_rate < 0)
	postretirement_tax_rate = 0	
if (plan_investment_return < 0)
	plan_investment_return = 0
if (no_plan_investment_return < 0)
	plan_investment_return = 0
if (retirement_age < 0)
	retirement_age = 0		

//Pre retirement variables:
var before_tax_draw = 0
var taxes = 0
var after_tax_draw = 0
var no_plan_draw = 0

//Calculate variables based on user input
var cash_contribution = contribution-0
var no_plan_taxes = cash_contribution*preretirement_tax_rate
var contribution_after_taxes = cash_contribution-no_plan_taxes
var plan_before_tax_draw = retirement_draw / (1 - postretirement_tax_rate)
var plan_taxes = plan_before_tax_draw*postretirement_tax_rate

//Loop calculated variables
var no_plan_fund=0
var plan_fund=0
var no_plan_bty
var no_plan_aty = 0
var plan_yield = 0
var no_plan_yield_taxes=0
var no_plan_fund_temp=0
var plan_fund_temp=0

	do 
	{ 

		if (age == retirement_age)
			{
			cash_contribution = 0
			contribution_after_taxes = 0
			no_plan_taxes = 0
			no_plan_draw = retirement_draw
			before_tax_draw = plan_before_tax_draw
			taxes = plan_taxes
			after_tax_draw = retirement_draw
			plan_investment_return = no_plan_investment_return
			preretirement_tax_rate = postretirement_tax_rate
			}
	
		//Calculate NO PLAN year:
		no_plan_fund += contribution_after_taxes		
		no_plan_fund += no_plan_aty
		no_plan_fund_temp = no_plan_fund
		no_plan_fund -= no_plan_draw
		no_plan_bty = no_plan_fund*no_plan_investment_return
		no_plan_yield_taxes = no_plan_bty*preretirement_tax_rate
		no_plan_aty = no_plan_bty - no_plan_yield_taxes

		//Calculate PLAN year:
		plan_fund += cash_contribution
		plan_fund += plan_yield
		plan_fund_temp = plan_fund
		plan_fund -= before_tax_draw		
		plan_yield = plan_fund*plan_investment_return
		
		if (no_plan_fund_temp < no_plan_draw)
			no_plan_draw = no_plan_fund_temp 
		if (no_plan_fund < 0)
			no_plan_fund = 0
		if (no_plan_fund_temp < 0)
			no_plan_fund_temp = 0
		if (no_plan_bty < 0)
			no_plan_bty = 0
		if (no_plan_yield_taxes < 0)
			no_plan_yield_taxes = 0
		if (no_plan_aty < 0)
			no_plan_aty = 0
		if (plan_yield < 0)
			plan_yield = 0
		if (plan_fund_temp < before_tax_draw)
			{
			before_tax_draw = plan_fund_temp
			taxes = before_tax_draw*postretirement_tax_rate	
			after_tax_draw = before_tax_draw - taxes
			}

		//Print table
		win.document.write("<tr>")
		win.document.write('<td style="border-bottom:1px #666 solid;"><b>' + year + "</b></td>")
		win.document.write('<td style="border-bottom:1px #666 solid;"><b>' + age + "</b></td>")
		win.document.write('<td style="border-bottom:1px #666 solid;">' + cash_contribution + "</td>")
		win.document.write('<td style="border-bottom:1px #666 solid;">' + Math.round(no_plan_taxes) + "</td>")
		win.document.write('<td style="border-bottom:1px #666 solid;"><font color="red">' + Math.round(no_plan_fund_temp) + "</td>")
		win.document.write('<td style="border-bottom:1px #666 solid;">' + Math.round(no_plan_bty) + "</td>")
		win.document.write('<td style="border-bottom:1px #666 solid;">' + Math.round(no_plan_yield_taxes) + "</td>")
		win.document.write('<td style="border-bottom:1px #666 solid;">' + Math.round(no_plan_aty) + "</td>")
		win.document.write('<td style="border-bottom:1px #666 solid;">' + Math.round(no_plan_draw) + "</td>")
	
		//Plan section
		win.document.write('<td style="border-bottom:1px #666 solid;">' + cash_contribution + "</td>")
		win.document.write('<td style="border-bottom:1px #666 solid;">' + 0 + "</td>")
		win.document.write('<td style="border-bottom:1px #666 solid;"><font color="red">' + Math.round(plan_fund_temp) + "</td>")
		win.document.write('<td style="border-bottom:1px #666 solid;">' + Math.round(plan_yield) + "</td>")
		win.document.write('<td style="border-bottom:1px #666 solid;">' + 0 + "</td>")
		win.document.write('<td style="border-bottom:1px #666 solid;">' + Math.round(plan_yield) + "</td>")
		win.document.write('<td style="border-bottom:1px #666 solid;">' + Math.round(before_tax_draw) + "</td>")
		win.document.write('<td style="border-bottom:1px #666 solid;">' + Math.round(taxes) + "</td>")
		win.document.write('<td style="border-bottom:1px #666 solid;">' + Math.round(after_tax_draw) + "</td>")
		win.document.write("</tr>")

		year++
		age++
	}
	while ((age <= 120)&&(plan_fund>0))

	win.document.write('</table>')
	win.document.write('<br><a href = "javascript:window.close()"><p>Close This Window</p></a><br>')
	win.document.write('</body></html>')

win.document.close();
}