dt_rebate_calculate = function()
{
	var price, down, rate, months;
	try
	{
		price = DT_F.getFormFloat( "dt_rebate_price", "Please enter the purchase price", "Please enter the purchase price as a number" );
		if ( price < 0 ) {
			throw("Please enter a purchase price >= 0");
		}
		down = DT_F.getFormFloat( "dt_rebate_down", "Please enter the down payment", "Please enter the down payment as a number" );	
		if ( down < 0 ) {
			throw("Please enter a down payment >= 0");
		}
		cash = DT_F.getFormFloat( "dt_rebate_cash", "Please enter the cash rebate offer", "Please enter the cash rebate offer as a number" );	
		if ( cash < 0 ) {
			throw("Please enter a cash rebate offer >= 0");
		}
		if ( down + cash > price ) {
			throw("Your down payment plus rebate exceeds the purchase price.  Therefore, you won't need a loan.");
		}
		normal = DT_F.getFormFloat( "dt_rebate_normal", "Please enter the normal interest rate ", "Please enter the normal interest rate as a number" );
		if ( normal < 0 || normal > 40 ) {
			throw("Please enter a normal interest rate between 0% and 40%");
		}
		low = DT_F.getFormFloat( "dt_rebate_low", "Please enter the low interest rate offer", "Please enter the low interest rate offer as a number" );
		if ( low < 0 || low > 40 ) {
			throw("Please enter a low interest rate offer between 0% and 40%");
		}
		months = DT_F.getFormInt( "dt_rebate_months", "Please enter the term of your loan, in months", "Please enter the term of your loan, in months", "The loan term was rounded to" );
		if ( months <= 0 || months > 360 ) {
			throw("Please enter an a loan term between 1 and 360 months");
		}
		if ( low > normal )	{
			throw("The low interest rate should be lower than the normal interest rate.");
		}
	}
	catch ( err )
	{
		alert(err);
		return;
	}

	var loan1 = price-down;
	var loan2 = price-down-cash;

	var amount1 = DT_F.calcPayment(loan1, months, low);
	var amount2 = DT_F.calcPayment(loan2, months, normal);
	
	var x = document.getElementById("dt_rebate_results");

	var res;
	if ( amount1 == amount2 ) {
		res = "Both the rebate and the low interest rate will give you the same monthly payment.";
	}
	else if ( amount1 < amount2 ) {
		res = "You will pay less with the <b>Low Rate</b> offer.";
	}
	else if ( amount2 < amount1 ) {
		res = "You will pay less with the <b>Rebate</b> offer.";
	}
	
	var table = [];
	table.push("<br><table cellpadding='0' cellspacing='0'>");
	table.push("<tr><th>Offer</th><th>Monthly Payment</th><th>Total Payments</th></tr>");
	table.push("<tr><td>Low-Rate</td><td>" + DT_F.formatDollars(amount1) + "</td><td>" + DT_F.formatDollars(amount1*months) + "</td></tr>");
	table.push("<tr><td>Rebate</td><td>" + DT_F.formatDollars(amount2) + "</td><td>" + DT_F.formatDollars(amount2*months) + "</td></tr>");
	table.push("</table>");

	res += "<br>" + table.join('');

	x.innerHTML = res;
	x.style.display = "block";
};
