dt_cig_calculate = function()
{  

	var costPerPack, packsPerDay, years, rate;

	try
	{
		costPerPack = DT_F.getFormFloat( "dt_cig_costperpack", "Please enter the cost of a pack of cigarettes", "Please enter a valid cost of a pack of cigarettes" );
		if ( costPerPack <= 0 ) {
			throw("The cost of a pack of cigarettes must be > 0");
		}
		packsPerDay = DT_F.getFormFloat( "dt_cig_packsperday", "Please enter the number of packs you smoke per day", "Please enter a valid number of packs per day" );
		if ( packsPerDay  < 0 ) {
			throw("The number of packs per day must be >= 0");
		}
		years = DT_F.getFormInt( "dt_cig_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_cig_rate", "Please enter an interest rate", "Please enter the interest rate as a number" );
		if ( rate < -100 ) {
			throw("Please enter an interest rate >= -100%");
		}
	}
	catch ( err )
	{
		alert(err);
		return;
	}

	var r = 1 + (rate / 100);
	var yearlySavings = ( costPerPack * packsPerDay * 365 );
	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 quitting smoking, 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_cig_results");
	x.innerHTML = results;
	x.style.display = "block";
}