function calculate() {

	var obj = document.loan;
	var msgText = "";
	
	if (parseFloat(obj.principal.value) != obj.principal.value) {
		msgText = "Please enter a valid vehicle price.";		
	}
	else {
		if (parseFloat(obj.principal.value) > 10000000 || parseFloat(obj.principal.value) < 1) {
			msgText = "Please enter a vehicle price between $1.00 and $10,000,000.";
		}
	}
	
	if (parseFloat(obj.down.value) != obj.down.value) {
		msgText = msgText + "\nPlease enter a valid down payment.";		
	}
	else {
		if (parseFloat(obj.down.value) >= parseFloat(obj.principal.value) || parseFloat(obj.down.value) < 0) {
			msgText = msgText + "\nPlease enter a down payment between $0 and $" + obj.principal.value;
		}
	}
	
	if (parseFloat(obj.interest.value) != obj.interest.value) {
		msgText = msgText + "\nPlease enter a valid interest rate.";		
	}
	else {
		if (parseFloat(obj.interest.value) > 100 || parseFloat(obj.interest.value) < 0) {
			msgText = msgText + "\nPlease enter a valid interest rate.";
		}
	}
		
	if (msgText != "") {
		alert(msgText);
		return false;
	}
	
	var i = obj.interest.value;
	
	if (i >= 1.0) {
	
		i = (i / 100.0);
	}
	
	else if (i > 0 && i < 1) {
	
		i = (i / 10.0);
	}
	
	if (i > 0) {
	
		i /= 12;
		var pow = 1;
	
		for (var j = 0; j < (obj.years[obj.years.selectedIndex].value*12); j++) {
			pow = pow * (1 + i);
		}
		
		var money = "" + .01* Math.round(100*((obj.principal.value-obj.down.value) * pow * i) / (pow - 1));
		
	}
	
	else {
	
		var money = "" + (obj.principal.value-obj.down.value)/(obj.years[obj.years.selectedIndex].value*12);
	
	}
		
		
	var total = "" + (money * (obj.years[obj.years.selectedIndex].value*12));
	
	var dec = "";
	var dollars = "";
	var cents = "";
	
	dec = money.indexOf(".");
	
	if (dec != -1) {
		dollars = money.substring(0,dec); 
		cents = money.substring(dec+1,dec+3);
		cents = (cents.length < 2) ? cents + "0" : cents;
		money = formatAmt(dollars, cents);
	}
	else {
		money = formatAmt(money, "00");
	}
	
	dec = total.indexOf(".");
	
	if (dec != -1) {
		dollars = total.substring(0,dec);
		cents = total.substring(dec+1,dec+3);
		cents = (cents.length < 2) ? cents + "0" : cents;
		total = formatAmt(dollars, cents);
	}
	else {
		total = formatAmt(total, "00");
	}

	obj.monthly.value = money;
	obj.total.value = total;
	
}

function clearForm() {

	var obj = document.loan;

	obj.years.selectedIndex = 0;
	obj.interest.value = "";
	obj.principal.value = "";
	obj.monthly.value = "";
	obj.total.value = "";

}

function formatAmt(dollars, cents) {

	var dollarString = "";
	var counter = 1;
	
	for (i=dollars.length; i>=1; i--) {
	
		dollarString = dollars.charAt(i-1) + dollarString;
		
		if (counter % 3 == 0 && counter < dollars.length) {
		
			dollarString = "," + dollarString;
		
		}
		
		counter++;
	
	}
	
	return "$ " + dollarString + "." + cents;
	
}

function removeCommas(obj) {

	obj.value = obj.value.replace(/\,/g, '');
	
}

