﻿// JScript File

// Digital Motorwork's JavaScript Payment Calculator
			// with thanks to Netscape Communications, Inc.
			function checkNumber(input, min, max, msg) {
				msg = msg + " field has invalid data: " + input.value;
				var str = input.value;
				for (var i = 0; i < str.length; i++) {
					var ch = str.substring(i, i + 1)
					if ((ch < "0" || "9" < ch) && ch != '.') {
						alert(msg);
						return false;
					}
				}
				var num = 0 + str
				if (num < min || max < num) {
					alert(msg + " not in range [" + min + ".." + max + "]");
					return false;
				}
				input.value = str;
				return true;
			}
			function computeField(input) {
				if (input.value != null && input.value.length != 0)
					input.value = "" + eval(input.value);
				compute(input.form);
			}
			function compute(form) {
				var Price = form.txtPrice.value;
				if (!checkNumber(form.txtDownPayment, 0, Price, "Down Payment")) {
					form.txtMonthlyPayment.value = "Invalid";
					return;
				}
				Price = Price - form.txtDownPayment.value;
				var Interest = form.txtInterest.value;
					var i = Interest;
				if (i > 1.0)
					{
					i = i / 100.0;
					form.txtInterest.value = i * 100;
					}
				i /= 12;

				var pow = 1;
				for (var j = 0; j < form.cboTerms.options[form.cboTerms.options.selectedIndex].value; j++)
					pow = pow * (1 + i);
				form.txtMonthlyPayment.value = Math.round((Price * pow * i) / (pow - 1))
			}
			function clearForm(form) {
				form.txtMonthlyPayment.value = "";
			}
