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 totalInterest = totalSavings - yearlySavings * years;


	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.  After " + years + " years, your savings will have grown to <b>" +  DT_F.formatDollars(totalSavings) + "</b>.<br><br>You will have earned " + DT_F.formatDollars(totalInterest) + " in interest.";
	results += "<br><br>" + table.join('');
	 
	var x = document.getElementById("dt_coffee_results");
	x.innerHTML = results;
	x.style.display = "block";
}