// JavaScript Document

    // JavaScript Loan Calculator
    // by Paul Colton
    // based on a demo by Netscape Communications, Inc.

    function checkNumber(input, min, max, msg) {
        msg = msg + " field has invalid data: " +  input.value;
        var str = input.value;
//		alert(str);
        for (var i = 0; i < str.length; i++) {
            var ch = str.substring(i, i + 1)
			if (ch == ",") {
				alert(msg + ". Please remove all commas from your submitted values.");
				return false;
			}
            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 editField(form) {
		
	}
 
 	function computeField1(input) {
//        alert("1 " + input.value);
		
//		if (input.value != null && input.value.length != 0) 
//            input.value = "" + eval(input.value);
//         alert("4 " + input.value);
       computeForm1(input.form);
    }
 
     function computeForm1(form) {
        if ((form.averagebalance.value == null || form.averagebalance.value.length == 0) ||
            (form.checkswritten.value == null || form.checkswritten.value.length == 0) ||
            (form.checksdeposited.value == null || form.checksdeposited.value.length == 0)) {
            return;
        }
//        alert("2 " + form.averagebalance.value);
//        alert("3 " + form.averagebalance.value.length);

        if (!checkNumber(form.averagebalance, 1, 99999999.99, "Average Balance") ||
            !checkNumber(form.checkswritten, 1, 10000000, "Checks Written") ||
            !checkNumber(form.checksdeposited, 1, 10000000, "Checks Deposited")) {
            form.estimatedcost.value = "";
            return;
        }
	 }

 
 	function computeField(input) {
        if (input.value != null && input.value.length != 0)
            input.value = "" + eval(input.value);
        computeForm(input.form);
    }

    function computeForm(form) {
        if ((form.averagebalance.value == null || form.averagebalance.value.length == 0) ||
            (form.checkswritten.value == null || form.checkswritten.value.length == 0) ||
            (form.checksdeposited.value == null || form.checksdeposited.value.length == 0)) {
            return;
        }

        if (!checkNumber(form.averagebalance, 1, 99999999.99, "Average Balance") ||
            !checkNumber(form.checkswritten, 1, 10000000, "Checks Written") ||
            !checkNumber(form.checksdeposited, 1, 10000000, "Checks Deposited")) {
            form.estimatedcost.value = "";
            return;
        }
		
        var avgbal = form.averagebalance.value;
		var chkswrtn = form.checkswritten.value;
		var chksdep = form.checksdeposited.value;
		var estcharge = 0;
		
		if (chkswrtn < 51) {
			chkswrtn = 0;
		}
		else {
			chkswrtn = chkswrtn - 50;
		}

		if (chksdep < 51) {
			chksdep = 0;
		}
		else {
			chksdep = chksdep - 50;
		}
		
		estcharge = (6.00 + (chkswrtn * 0.14) + (chksdep * 0.09) - ((avgbal * 0.005) / 12));
         if (estcharge < 0) {
            estcharge = 0;
        }
 	   estcharge = Math.round( Math.round( estcharge * Math.pow( 10, 2 + 1 ) ) / Math.pow( 10, 1 ) ) / Math.pow(10,2);

        form.estimatedcost.value = "$" + estcharge;

		if (form.averagebalance.value > 135000 || form.payrolldirectdeposit[1].checked || form.checkscanner[1].checked) {
			var confirmboxmsg = "Based on the information you supplied, we would request that you contact us to obtain specific pricing.  Clicking OK will open a small form that you can fill out and send to us.";
			var answer = window.confirm(confirmboxmsg)
			form.estimatedcost.value = "";
			if (answer) {
				window.open('/irequest/rfmi.asp?requesttype=treasurymanagement','RequestForMorInformation','scrollbars=0,location=0,resizable=0,status=0,toolbar=0,menubar=0,width=735px,height=790px');
			}
		}
   }

    function clearForm(form) {
        form.averagebalance.value = "0";
        form.checkswritten.value = "1";
        form.checksdeposited.value = "1";
		form.estimatedcost.value = "";
    }

