function calculate()
{	
	var p = document.getElementById("balance").value;
	if ( p.length == 0 )
	{
		alert("Please enter your current credit card balance");
		return;
	}
	
	p = fixNumber(p);
	p = parseFloat(p);
	if ( isNaN(p) )
	{
		alert("Please enter the current balance as a number");
		return;
	}
	if ( p < 0 )
	{
		alert("Please enter a balance >= 0");
		return;
	}
	
	var rate = document.getElementById("rate").value;
	if ( rate.length == 0 )
	{
		alert("Please enter an interest rate");
		return;
	}
	rate = fixNumber(rate);
	rate = parseFloat(rate);
	if ( isNaN(rate) )
	{
		alert("Please enter the interest rate as a number");
		return;
	}
	if ( rate <= 0 || rate >= 35 )
	{
		alert("Please enter a interest rate greater than 0 and less than 35");
		return;
	}

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

	var x = document.getElementById("payment").value;
	if ( x.length == 0 )
	{
		alert("Please enter a down payment");
		return;
	}
	x = fixNumber(x);
	x = parseFloat(x);
	if ( isNaN(x) )
	{
		alert("Please enter the monthly payment as a number");
		return;
	}
	if ( x < 0 )
	{
		alert("Please enter a monthly payment >= 0");
		return;
	}
		
	// monthly rate:
	var m = rate / 1200;
	
	// you don't pay back what you spend
	x -= spending;
	if(x < 0) {
		alert("You are charging more on your card than you are paying back. With this spending pattern, you won't pay back your credit card debt.");
		return;
	}
	
	// make sure payment is not too small i.e. smaller than the balance is growing
	if(x <= (m * p)) 
	{
		alert("Your monthly payment of " + formatDollars(x+spending) + " is smaller than your monthly charges and financing cost of " + formatDollars(m * p + spending) + ". You will never pay off your debt!");
		return;
	}
	
	// calculate
	var nom = ( (-1 * p * m) / x ) + 1 ;
	var denom = 1 + m;
	var months = -1 * ( Math.log ( nom ) / Math.log ( denom )	);
	var interest = (months * x)- p;
	
	var resel = document.getElementById("results");

	var results = "With the information you have given, it will take <b>" + Math.ceil(months) + "</b> months to pay off your credit card balance.";

	results += "<p>You will pay a total of <b>" + formatDollars(interest) + "</b> in interest.";
	
	resel.innerHTML = results;
	resel.style.display = "block";
}
