dt_coffee_calculate = function()
{  

	var coffeeStore, coffeeHome, cupsPerWeek, years, rate;

	try
	{
		coffeeStore = DT_F.getFormFloat( "dt_coffee_shop", "Please enter the cost of shop-bought coffee", "Please enter the cost of shop-bought coffee as a number" );
		if ( coffeeStore <= 0 ) {
			throw("The cost of shop-bought coffee must be > 0");
		}
		coffeeHome = DT_F.getFormFloat( "dt_coffee_home", "Please enter the cost of homemade coffee", "Please enter a valid cost of homemade coffee" );
		if ( coffeeHome  < 0 ) {
			throw("The cost of homemade coffee must be >= 0");
		}
		cupsPerWeek = DT_F.getFormFloat( "dt_coffee_cpw", "Please enter the number of cups per week you currently drink", "Please enter a valid number of cups per week you currently drink" );
		if ( cupsPerWeek <= 0 ) {
			throw("The number of cups per week you drink must be > 0.  Otherwise you won't save money.");
		}
		years = DT_F.getFormInt( "dt_coffee_years", "Please enter the number of years to calculate", "Please enter the number of years to calculate as a number", "The number of years to calculate was rounded down to" );
		if ( years <= 0 ) {
			throw("Please enter a number of years > 0");
		}
		rate = DT_F.getFormFloat( "dt_coffee_rate", "Please enter an interest rate", "Please enter the interest rate as a number" );
		if ( rate < -100 ) {
			throw("Please enter an interest rate >= -100%");
		}
		if ( coffeeHome >= coffeeStore )
		{
			throw("Your homemade coffee is not cheaper than at the coffee shop. You won't save any money.");
			return;
		}
	}
	catch ( err )
	{
		alert(err);
		return;
	}

	var r = 1 + (rate / 100);
	var yearlySavings = ( coffeeStore - coffeeHome ) * ( cupsPerWeek * 50 );
	var totalSavings; 
	if ( rate == 0 ) {
		totalSavings = yearlySavings * years;
	}
	else {
		totalSavings = yearlySavings * ( 1 - Math.pow(r, years) ) / ( 1 - r ); 
	}

	var currAmount = 0;
	var tableYears = years;
	if ( tableYears > 10 ) {
		tableYears = 10;
	}
	var table = [];
	if ( years != tableYears ) {
		table.push("Here is how	your savings will grow over the first " + tableYears + " years:");
	} 
	else {
		table.push("Here is how your savings will grow over time:");
	}
	table.push("<br><table cellpadding='0' cellspacing='0'>");
	table.push("<tr><th>Year</th><th>Interest</th><th>Savings</th><th>Balance</th></tr>");
	table.push("<tr><td>start</td><td>$0</td><td>$0</td><td>$0</td></tr>");

	for ( var y=1; y<=tableYears; y++ )
	{
		var interest = ( currAmount * r ) - currAmount;
		currAmount = ( currAmount * r ) + yearlySavings;
		table.push("<tr><td>" + y + "</td><td>" + DT_F.formatDollars(interest) + "</td><td>" + DT_F.formatDollars(yearlySavings) + "</td><td>" + DT_F.formatDollars(currAmount) + "</td></tr>");
	}
	table.push("</table>");
	var results = "By reducing your coffee costs, you will save " + DT_F.formatDollars(yearlySavings) + " per year.<br><br>After " + years + " years, your savings will have grown to <b>" +  DT_F.formatDollars(totalSavings) + "</b>.";
	results += "<br><br>" + table.join('');
	 
	var x = document.getElementById("dt_coffee_results");
	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="coffee_dt_calculator" class="dt_calculator">\n\
	<h2>Coffee Savings Calculator</h2>\n\
	<p class="small">Provided by DollarTimes.com</p>\n\
	<p class="instructions">This will calculate how much you would save if you stopped buying coffee or tea in the coffeeshop, and instead made your own coffee (or quit drinking it altogether).</p>\n\
	<div class="a">Cost of Expensive Coffee</div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="coffee_cost" type="text" value="3.00" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Cost of Cheaper Coffee</div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="coffee_deduction" type="text" value="" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Cups per Week</div>\n\
	<div class="b"></div>\n\
	<div class="c"><input id="coffee_cpw" type="text" value="5" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Savings Interest Rate</div>\n\
	<div class="b">%</div>\n\
	<div class="c"><input id="coffee_rate" type="text" value="5.00"/></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Years in Savings</div>\n\
	<div class="b"></div>\n\
	<div class="c"><input id="coffee_years" type="text" value="5" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="d"><button onclick="coffee_calculate()">Calculate</button></div>\n\
	<div id="coffee_dt_results" class="dt_results">&nbsp;</div>\n\
</div>\n\
';



var calcUrl = "http://www.dollartimes.com/calculators/on-your-site/coffee-savings.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('coffee_dt_calculator');
		if(calcDiv)	{
			// Put link on bottom of calculator box
			par.removeChild(link)			
			calcDiv.appendChild(link);	
		}
	}
}
