
function MM_openBrWindow(theURL,winName,features) {

  window.open(theURL,winName,features);

}

function closeit() {
	window.close();
}

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 Trim(s) {
    return s.replace(/(^\s+)|(\s+$)/g, "");
}

function computeField(input) {
	input.value = Trim(input.value);
    if (input.value != null && input.value.length != 0)
	input.value = "" + eval(input.value);
	computeForm(input.form);
}

function computeForm(form) {
	if ((form.years.value == null || form.years.value.length == 0) || (form.interest.value == null || form.interest.value.length == 0) || (form.principal.value == null || form.principal.value.length ==0)) {
  	   form.payment.value = "";
  	   form.weeklypayment.value = "";
	   return;
    }

    if (!checkNumber(form.years, 1, 30, "# of years") ||
        !checkNumber(form.interest, .01, 99, "Interest") ||
        !checkNumber(form.principal, 100, 10000000, "Principal")) {
    	   form.payment.value = "Invalid";
    	   form.weeklypayment.value = "Invalid";
    	   return;
    }

  var i = form.interest.value;
    i = i / 100.0;
    i /= 12;

    var pow = 1;
    var months = form.years.value * 12;
    for (var j = 0; j < months; j++)
    	pow = pow * (1 + i);
	thisInt = (form.principal.value * pow * i) / (pow - 1)
	var monthly_payment = Math.round(thisInt*Math.pow(10,2))/Math.pow(10,2);
	form.payment.value = monthly_payment;
	var weekly_payment = (monthly_payment * 12) / 52;
	form.weeklypayment.value = Math.round(weekly_payment*Math.pow(10,2))/Math.pow(10,2);
}


function clearForm(form) {
	form.years.value = "";
	form.interest.value = "";
	form.principal.value = "";
}

