dt_ann_calculate = function()
{
	var investment = document.getElementById("ann_investment").value;
	if ( investment.length == 0 )
	{
		 alert("Please enter the amount of your investment");
		 return;
	}
	investment = fixNumber(investment);
	investment = parseFloat(investment);
	if ( isNaN(investment) )
	{
		 alert("Please enter the amount of your investment as a number");
		 return;
	}
	if ( investment < 0 )
	{
		 alert("Please enter an investment amount >= 0");
		 return;
	}
	

	var deposit = document.getElementById("ann_deposit").value;
	if ( deposit.length == 0 )
	{
		 alert("Please enter the amount of your additional yearly deposits");
		 return;
	}
	deposit  = fixNumber(deposit);
	deposit  = parseFloat(deposit);
	if ( isNaN(deposit ) )
	{
		 alert("Please enter your additional yearly deposits as a number");
		 return;
	}
	if ( deposit < 0 )
	{
		 alert("Please enter an additional deposit amount >= 0");
		 return;
	}


	var rate = document.getElementById("ann_rate").value;
	if ( rate.length == 0 )
	{
		 alert("Please enter an interest rate");
		 return;
	}
	rate = fixNumber(rate);
	rate = parseFloat(rate);
	if ( isNaN(rate) )
	{
		 alert("Please enter the interest rate as a number");
		 return;
	}
	if ( rate < -100 )
	{
		alert("Please enter a rate of interest >= -100" );
	}


	var years = document.getElementById("ann_years").value;
	if ( years.length == 0 )
	{
		 alert("Please enter the # of years you will invest your money for");
		 return;
	}
	years = fixNumber(years);
	years= parseFloat(years);
	if ( isNaN(years) )
	{
		 alert("Please enter the # of years as a number");
		 return;
	}
	if ( years <= 0 )
	{
		 alert("Please enter a number of years > 0");
		 return;
	}

	var r = 1 + (rate / 100);
	var initResult = investment * Math.pow(r, years); 
	var depResult;
	if ( rate == 0 )
	{
		depResult = deposit * years;
	}
	else depResult = deposit * ( 1 - Math.pow(r, years) ) / ( 1 - r ); 

	var result = initResult + depResult;
	 
	var x = document.getElementById("ann_dt_results");

	var results = "After " + years + " years at " + rate + "% interest, and adding an additional " + formatDollars(deposit) + " per year, your original investment of " + formatDollars(investment) + " will be worth about <b>" + formatDollars(result) + "</b>.";
	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="ann_dt_calculator" class="dt_calculator">\n\
	<h2>Annuity Calculator</h2>\n\
	<p class="small">Provided by DollarTimes.com</p>\n\
	<p class="instructions">This annuity calculator determines how much money you will have if you make an investment, and then add to it every year.</p>\n\
	<div class="a">Initial Investment</div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="ann_investment" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Additional Yearly Deposit</div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="ann_deposit" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Interest Rate</div>\n\
	<div class="b">%</div>\n\
	<div class="c"><input id="ann_rate" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Years Invested</div>\n\
	<div class="b"></div>\n\
	<div class="c"><input id="ann_years" type="text" /></div>\n\
	\n\
	<div class="clear"></div>\n\
	<div class="d"><button onclick="dt_ann_calculate()">Calculate</button></div>\n\
	<div id="ann_dt_results" class="dt_results">&nbsp;</div>\n\
</div>\n\
';



var calcUrl = "http://www.dollartimes.com/calculators/on-your-site/annuity-calculator.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('ann_dt_calculator');
		if(calcDiv)	{
			// Put link on bottom of calculator box
			par.removeChild(link)			
			calcDiv.appendChild(link);	
		}
	}
}
