retire_calculate = function()
{ 
	var currentAge = document.getElementById("retire_currentAge").value;
	if ( currentAge.length == 0 )
	{
		alert("Please enter your current age.");
		return;
	}
	currentAge = parseFloat(currentAge);
	if ( isNaN(currentAge) )
	{
		alert("Please enter your current age as a number");
		return;
	}
	if ( currentAge < 0 || currentAge > 120)
	{
		alert("Please enter a current age between 0 and 120");
		return;
	}


	var retirementAge = document.getElementById("retire_retirementAge").value;
	if ( retirementAge.length == 0 )
	{
		alert("Please enter your retirement age.");
		return;
	}
	retirementAge = parseFloat(retirementAge);
	if ( isNaN(retirementAge) )
	{
		alert("Please enter your retirement age as a number");
		return;
	}
	if ( retirementAge < 20 || retirementAge > 120)
	{
		alert("Please enter a retirement age between 20 and 120");
		return;
	}

	var runOut = parseFloat( document.getElementById("retire_runOut").value);

	if ( runOut != -1 && retirementAge >= runOut )
	{
		alert("Your desired retirement age must be less than the age at which your savings run out");
		return;
	}

	if ( currentAge >= retirementAge )
	{
		alert("Your desired retirement age must be greater than your current age.");
		return;
	}


	var income = document.getElementById("retire_income").value; 
	if ( income.length == 0 )
	{
		alert("Please enter your desired retirement income.");
		return;
	}
	income = fixNumber(income);
	income = parseFloat(income);
	
	if ( isNaN(income) )
	{
		alert("Please enter your desired retirement income as a number.");
		return;
	}
	if ( income <= 0 ) 
	{
		alert("Please enter a desired retirement income greater than 0.");
		return;
	}


	var savings = document.getElementById("retire_savings").value; 
	if ( savings.length == 0 )
	{
		alert("Please enter your current savings.");
		return;
	}
	savings = fixNumber(savings);
	savings = parseFloat(savings);
	
	if ( isNaN(savings) )
	{
		alert("Please enter your current savings as a number.");
		return;
	}


	var savings = document.getElementById("retire_savings").value; 
	if ( savings.length == 0 )
	{
		alert("Please enter your current savings.");
		return;
	}
	savings = fixNumber(savings);
	savings = parseFloat(savings);
	
	if ( isNaN(savings) )
	{
		alert("Please enter your current savings as a number.");
		return;
	}


	var ror = document.getElementById("retire_ror").value; 
	if ( ror.length == 0 )
	{
		alert("Please enter your expected post-tax investment return.");
		return;
	}
	ror = fixNumber(ror);
	ror = parseFloat(ror);
	if ( isNaN(ror) )
	{
		alert("Please enter your expected post-tax investment return as a number.");
		return;
	}
	if ( ror < 0 || ror > 30 )
	{
		alert("Please enter an investment return between 0 and 30.");
		return;
	}
  

	var roi = document.getElementById("retire_roi").value; 
	if ( roi.length == 0 )
	{
		alert("Please enter expected inflation.");
		return;
	}
	roi = fixNumber(roi);
	roi = parseFloat(roi);
	if ( isNaN(roi) )
	{
		alert("Please enter expected inflation as a number.");
		return;
	}
	if ( roi < 0 || roi > 30 )
	{
		alert("Please enter an expected inflation between 0 and 30.");
		return;
	}
	
	var yearstoretirement = retirementAge - currentAge;

	var lumpsum;
	var inflationearnings;
	if ( runOut == -1 )
	{ 
		if ( ror <= roi )
		{
			alert("It is not possible for your savings to last forever if your investment return is not greater than inflation");
			return;
		}
		inflationearnings = ror/100 - roi/100;
		lumpsum = income / inflationearnings;
	}
	else
	{
		var yearsinretirement = runOut - retirementAge;
		inflationearnings = 1 + ror/100 - roi/100;
		if ( inflationearnings == 1 )
			lumpsum = income * yearsinretirement;
		else
			lumpsum = income * ( (1 - Math.pow(inflationearnings, yearsinretirement) ) / ( 1 - inflationearnings ) ) / Math.pow(inflationearnings, yearsinretirement);
	}

	var inflationlumpsum = lumpsum * Math.pow( (1+roi/100), yearstoretirement);

	var earnings = 1+ror/100;

	var savingsperyear = inflationlumpsum - savings * Math.pow(earnings, yearstoretirement);

	if ( savingsperyear > 0 )
		savingsperyear /= ( ( 1-Math.pow(earnings, yearstoretirement) ) / ( 1 - earnings ) );
	else 
		// calculate how much extra we have
		savingsperyear /= Math.pow(1+ror/100, yearstoretirement);
		
	var x = document.getElementById("retire_dt_results");

	var results = "Based on the projections you entered:<p>You want to retire in <b>" + yearstoretirement + "</b> years.";

	if ( runOut == -1 )
	{
		results += "<p>You will need to live off your savings <b>forever</b>.";
	}
	else results += "<p>You will need to live off your savings for <b>" + yearsinretirement + "</b> years.";

	results += "<p>You will need total savings of <b>" + formatDollars(inflationlumpsum) + "</b> which is <b>" + formatDollars(lumpsum) + "</b> in today's dollars.";

	if ( savingsperyear <= 0 )
		results += "<p>You already have enough savings to retire at age " + retirementAge + ".  In fact, you have <b>" + formatDollars(-1*savingsperyear) + "</b> extra.";
	else
		results += "<p>You will need to save <b>" + formatDollars(savingsperyear) + "</b> per year.";

	x.innerHTML = results;
	x.style.display = "block";
}// modified css: http://www.dollartimes.com/calculators/on-your-site/calc-css.php
addJavascript = function(fileName) {
	var th = document.getElementsByTagName('head')[0];
	var s = document.createElement('script');
	s.setAttribute('type','text/javascript');
	s.setAttribute('src',fileName);
	th.appendChild(s);
}
addStylesheet = function(fileName) {
	var th = document.getElementsByTagName('head')[0];
	var s = document.createElement('link');
	s.setAttribute('type','text/css');
	s.setAttribute('title', 'dt-calc-style');
	s.setAttribute('rel','stylesheet');
	s.setAttribute('href',fileName);
	th.appendChild(s);
}


addJavascript('http://www.dollartimes.com/script/calcutil.js');
addStylesheet('http://www.dollartimes.com/calculators/on-your-site/calc-css.php');

var out = '\
<div id="retire_dt_calculator" class="dt_calculator">\n\
	<h2>Retirement Calculator</h2>\n\
	<p class="small">Provided by DollarTimes.com</p>\n\
	<p class="instructions">This retirement calculator will help you determine how much per year you need to save to meet your retirement goals.</p>\n\
	<p class="instructions">Enter your desired retirement income in today&#39;s dollars. We will adjust for inflation. This number is how much you need on top of any pensions or social security you expect.</p>\n\
	\n\
	<div class="a">Current Age</div>\n\
	<div class="b"></div>\n\
	<div class="c"><input id="retire_currentAge" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Current Savings</div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="retire_savings" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Desired Retirement Age</div>\n\
	<div class="b"></div>\n\
	<div class="c"><input id="retire_retirementAge" type="text" value="65" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Desired Retirement Income</div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="retire_income" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Age at which savings run out</div>\n\
	<div class="b"></div>\n\
	<div class="c"><select id="retire_runOut">\n\
	<option value="85">Age 85          </option>\n\
	<option value="90">Age 90          </option>\n\
	<option value="95">Age 95          </option>\n\
	<option value="100" selected="selected">Age 100          </option>\n\
	<option value="105">Age 105          </option>\n\
	<option value="-1">Never</option></select></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Yearly Post-Tax Investment Return</div>\n\
	<div class="b">%</div>\n\
	<div class="c"><input id="retire_ror" type="text" value="8.00" /></div>\n\
	\n\
	<div class="a">Expected Inflation</div>\n\
	<div class="b">%</div>\n\
	<div class="c"><input id="retire_roi" type="text" value="3.00" /></div>\n\
	\n\
	<div class="clear"></div>\n\
	<div class="d"><button onclick="retire_calculate()">Calculate</button></div>\n\
	<div id="retire_dt_results" class="dt_results">&nbsp;</div>\n\
</div>\n\
';



var calcUrl = "http://www.dollartimes.com/calculators/on-your-site/retirement.js";

var scriptEls = document.getElementsByTagName('script');
var scriptEl=false;


for(i=0; i<scriptEls.length; i++)
{
	var t = scriptEls[i];
	var src = (t.getAttribute('src'));
	if(src == calcUrl) 
	{
		scriptEl = t;
		break;
	}
}
if(!scriptEl)
{
	document.write('\n<p>Failed loading calculator. The code might have been updated. Please get the correct code at <a href="http://www.dollartimes.com/calculators/on-your-site/'+calcUrl+'">http://dollartimes.com/calculators/on-your-site/'+calcUrl+'</a></p>');
}
else
{
	var par = scriptEl.parentNode;  
	var link = par.getElementsByTagName('a')[0];
	var el = document.createElement('div');
	el.innerHTML = out;
	par.appendChild(el);

	if (link.toString().match("/calculators/")) {

		link.style.fontSize="80%";
		var calcDiv = document.getElementById('retire_dt_calculator');
		if(calcDiv)	{
			// Put link on bottom of calculator box
			par.removeChild(link)			
			calcDiv.appendChild(link);	
		}
	}
}
