function calculate(resultId) {

	var amount = checkValue(document.calculator.amount, 1);

	var length=document.calculator.length.options[document.calculator.length.selectedIndex].text	

	var rate =document.calculator.rate.options[document.calculator.rate.selectedIndex].text	

	var deposit = 0;

	var residual = checkValue(document.calculator.residual, 0);

	var error = false;

	if (amount == false || length == false || rate == false) {

		error = true;

	}

	if (error == false) {

		var rateNew = rate/1200;

		var months = length*12;

		var topRight = Math.pow((1+rateNew), months);

		var top = ((amount - deposit)*rateNew)*topRight;

		top = top - (residual*rateNew);

		var bottom = topRight - 1;

		var total = top/bottom;

		total = Math.round(total);

		document.getElementById(resultId).innerHTML = "$"+total;

		} else {

		document.getElementById(resultId).innerHTML = "0";

	}

}







function checkValue(field, zero) {

var amount;

	amount = field.value;

	amount = amount.replace("$", "");

	amount = amount.replace(",", "");

	amount = amount.replace(" ", "");

	amount = amount.replace("%", "");

	amount = amount = parseFloat(amount);

	if (zero == 1) {

		if (amount == 0 || isNaN(amount)) {

			field.value = 0;

			return false;

		} else {

			field.value = amount;

			return amount;

			

		}

	} else { 

		if (amount == 0 || isNaN(amount)) {

			field.value = 0;

			return 0;

		} else {

			field.value = amount;

			return amount;

			

		}

	}

}