// all-in-one script march 2008
  
  var expenses; // year one annual expenses
  var savings; // year one savings
  var fixed_income;
  var variable_income;
  var years_before; // number years before retirement
  var rate_inflation = 3;
  var rate_return = 6;
  var amount;
  var future_expenses;
  var future_savings;
  var monthly_invest;
  var result;
  var rate;
  var years;
  var nest_egg;
  var balance10;
  var balance20;
  var balance = new Array();
  var start_balance;
  var need;
  var withdraw;
  var future_savings_withdraw;
  var change_nest_egg;
 
 
  function CurrencyFormatted(amount)
  {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
   }
// end of function CurrencyFormatted()

    function CommaFormatted(amount)
    {
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
    }
// end of function CommaFormatted()
  
	function formatCurrency(strValue)
    {
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
    } 

  function power(rate,years)
  {
  var add_one = 1 + rate;
  var pow = Math.pow(add_one,years);
  return pow;
  } 
  
  
  function calculate(form)
  {
  expenses = form.expenses.value;
  savings = form.savings.value;
  fixed_income = form.fixed_income.value;
  variable_income = form.variable_income.value;
  years_before = form.years_before.value;
 
  
  years = years_before;
  rate = rate_inflation / 100;
  
  
  future_expenses = expenses*(power(rate,years));
 
  rate = rate_return / 100;
   
  future_savings = savings*(power(rate,years));
  future_savings_withdraw = future_savings*rate;
  
   
  amount = 1*future_expenses - 1*future_savings_withdraw - 1*fixed_income - 1*variable_income;
  
  if (amount > 0.0) 
  {
 
  nest_egg = amount / (rate_return/100);

  
  
  rate = rate_return / 100;
  
  rate = rate / 12;
  
  years = years_before*12;

  monthly_invest = (nest_egg*rate)/((power(rate,years)-1));
  
  //nest_egg = 1*nest_egg + 1*future_savings;
      
  nest_egg = Math.round(100*nest_egg)/100;
  result = CurrencyFormatted(nest_egg);
  form.nest_egg.value = CommaFormatted(result);
  result = formatCurrency(nest_egg);
  form.nest_egg.value = result;
  
    
  monthly_invest = Math.round(100*monthly_invest)/100;
  result = CurrencyFormatted(monthly_invest);
  form.monthly_invest.value = CommaFormatted(result);
  result = formatCurrency(monthly_invest);
  form.monthly_invest.value = result;
  } 
  else
  {
  nest_egg = 0.0
  monthly_invest = 0.0
  form.nest_egg.value = nest_egg;
  form.monthly_invest.value = monthly_invest;
  }
  // compute balances after 10 and 20 years
  for (var i = 0; i < 20; i++)
     {
	 balance[i] = 0;
	 }
	 start_balance = nest_egg + future_savings;
	 balance[0] = start_balance;
	 
  for (var i = 0; i < 20; i++)
     {
	 change_nest_egg = balance[i]*(rate_return/100);
	
	 variable_income = variable_income*(1 + rate_inflation/100);
	 if(i !== 0) // do not adjust first year expenses
	 {
	 future_expenses = future_expenses*(1 + rate_inflation/100);
	 }
	 
	 withdraw = future_expenses - fixed_income - variable_income;
	 
	 if(withdraw < 0)
	 {
	 withdraw = 0;
	 }
	 balance[i + 1] = balance[i] + change_nest_egg - withdraw;
	 
	 	 
	 if (balance[i] <= 0)
	 {
	 balance[i] = 0;
	 }
	 
	 }
	 
  var result = CurrencyFormatted(start_balance);
  result = CommaFormatted(result);
  var result = formatCurrency(result);
  form.start_balance.value = result;
  	 
  var result = CurrencyFormatted(balance[10]);
  result = CommaFormatted(result);
  var result = formatCurrency(result);
  form.balance10.value = result;
  
  var result = CurrencyFormatted(balance[20]);
  result = CommaFormatted(result);
  var result = formatCurrency(result);
  form.balance20.value = result;	 
  
  
  
  }     
// Copyright ©Richard A. Howard 2003-2008