function calculate()
{  
	var price = document.getElementById("purchaseprice").value;
	if ( price.length == 0 )
	{
		alert("Please enter the purchase price");
		return;
	}
	price = fixNumber(price);
	price = parseFloat(price);
	if ( isNaN(price) )
	{
		alert("Please enter the  purchase price as a number");
		return;
	}
	if ( price < 0 )
	{
		alert("Please enter a purchase price >= 0");
		return;
	}


	var rate = document.getElementById("rate").value;
	if ( rate.length == 0 )
	{
		alert("Please enter a normal interest rate");
		return;
	}
	rate = fixNumber(rate);
	rate = parseFloat(rate);
	if ( isNaN(rate) )
	{
		alert("Please enter the normal interest rate as a number");
		return;
	}
	if ( rate < 0 || rate >= 35 )
	{
		alert("Please enter a normal interest rate >= 0 and less than 35");
		return;
	}




	var lowrate = document.getElementById("lowrate").value;
	if ( lowrate.length == 0 )
	{
		alert("Please enter the low interest rate offer");
		return;
	}
	lowrate = fixNumber(lowrate);
	lowrate = parseFloat(lowrate);
	if ( isNaN(lowrate) )
	{
		alert("Please enter the low interest rate offer as a number");
		return;
	}
	if ( lowrate < 0 || lowrate >= 35 )
	{
		alert("Please enter the low interest rate offer >= 0 and less than 35");
		return;
	}

		


	var months = document.getElementById("duration").value;
	if ( months.length == 0 )
	{
		alert("Please enter the # of months you loan will last");
		return;
	}
	months = fixNumber(months);
	months = parseFloat(months);
	if ( isNaN(months) )
	{
		alert("Please enter the # of months as a number");
		return;
	}
	if ( months <= 0 || months > 120 )
	{
		alert("Please enter a # of months between 1 and 120");
		return;
	}

	var downpayment = document.getElementById("down").value;
	if ( downpayment.length == 0 )
	{
		alert("Please enter a down payment");
		return;
	}
	downpayment = fixNumber(downpayment);
	downpayment = parseFloat(downpayment);
	if ( isNaN(downpayment) )
	{
		alert("Please enter the down payment as a number");
		return;
	}
	if ( downpayment < 0 )
	{
		alert("Please enter a down payment >= 0");
		return;
	}



	var rebate = document.getElementById("rebate").value;
	if ( rebate.length == 0 )
	{
		alert("Please enter a rebate amount");
		return;
	}
	rebate = fixNumber(rebate);
	rebate = parseFloat(rebate);
	if ( isNaN(rebate) )
	{
		alert("Please enter the rebate amount as a number");
		return;
	}
	if ( rebate < 0 )
	{
		alert("Please enter a rebate >= 0");
		return;
	}


	if ( downpayment + rebate >= price )
	{
		alert("Your down payment plus rebate exceeds the car price, so you won't need a loan.");
		return;
	}




	var loan1 = price-downpayment;
	var loan2 = price-downpayment-rebate;

	var amount1 = calcPayment(loan1, months, lowrate);
	var amount2 = calcPayment(loan2, months, rate);
	
	var x = document.getElementById("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.";
	
	res += "<p>With the low rate, your monthly payment will be " + formatDollars(amount1) + ", totalling " + formatDollars(amount1*months) + " over the course of the loan.";
	res += "<p>With the rebate, your monthly payment will be " + formatDollars(amount2) + ", totalling " + formatDollars(amount2*months) + " over the course of the loan.";

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

function calcPayment(loanamount, months, rate)
{
	var mrate = rate / 1200;
	var payment;

	if ( rate == 0 )
	{
		payment = loanamount / months;
	}
	else
	{
		var factor = Math.pow(1 + mrate, months );
		payment = loanamount * ( mrate / (1 - 1/factor) );
	}
	return payment;
}


