function checkNumber(input, min, max, msg) {

    msg = msg + " field has invalid data: " + input.value;

    var str = input.value;
    for (var z = 0; z < str.length; z++) {
         var ch = str.substring(z, z + 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 markInvalid(form) {
     form.mopmt.value = "Invalid";
}


function addInt(form ,Increment) {
     if (!checkNumber(form.interest, .001, 99, "Interest Rate") ) {
         markInvalid(form)
         return true;
     }
     if (Increment==0) return false
     var x = eval(form.interest.value)
     x = x + Increment
     if ( x < 0 ) x = 1
     form.interest.value = x
     computeForm(form)
     return false
}


function addExtra(form,Increment) {
     if (!checkNumber(form.extra, 1, 999999, "Extra repayments") ) {
         markInvalid(form)
         return true;
     }
     if (Increment==0) return false
     var x = eval(form.extra.value)
     x = x + Increment
     if (x < 0) x = 0
     form.extra.value = x
     computeForm(form)
     return false
}

function addTerm(form,Increment) {
     if (!checkNumber(form.term, 10, 30, "Loan term") ) {
         markInvalid(form)
         return true;
     }
     if (Increment==0) return false
     var x = eval(form.term.value)
     x = x + Increment
     if (x < 10) x = 10
     if (x > 30) x = 30
     form.term.value = x
     computeForm(form)
     return false
}

function addExtra(form,Increment) {
     if (!checkNumber(form.extra, 0, 9999, "Extra Repayment") ) {
         markInvalid(form)
         return true;
     }
     if (Increment==0) return false
     var x = eval(form.extra.value)
     x = x + Increment
     if (x < 0) x = 0
     form.extra.value = x
     computeForm(form)
     return false
}
function addPrinc(form,Increment) {
     if (!checkNumber(form.principal, 1, 999999, "Loan Amount") ) {
         markInvalid(form)
         return true;
     }
     if (Increment==0) return false
     var x = eval(form.principal.value)
     x = x + Increment
     if (x < 0 ) x = 0
     form.principal.value = x
     computeForm(form)
     return false
}

function calcPMT(principal,interest,term) {
     var T= term.value
     if (T < 31) T*=12
     var i = interest.value;
     if (i < 1.0) {
         i = i * 100.0;
         interest.value = i;
     }
     if (i >= 1.0) {
        i = i / 100.0;
     }
     i /= 12;
     var pow = 1;
     for (var j = 0; j < T; j++)
          pow = pow * (1 + i);

     return Math.round((principal.value * pow * i) / (pow - 1) *100)/100
}

function computeForm(form) {

     if (addInt(form,0)){
         markInvalid(form)
         return;
     }

     if (addPrinc(form,0)){
         markInvalid(form)
         return;
     }

     if (addTerm(form,0)){
         markInvalid(form)
         return;
     }

     if (addExtra(form,0)){
          markInvalid(form)
          return;
     }


     var pmt = calcPMT(form.principal,form.interest,form.term)

     form.mopmt.value = pmt
     if (eval(form.extra.value)==0) {
         form.savetermy.value=form.term.value
         form.savetermm.value="0"
         form.saveamt.value="0"
         return;
     }

     var pv = eval(form.principal.value)
     var term = eval(form.term.value * 12)
     var totpmts = pmt * term
     var int1 = totpmts - pv
     var r = eval(form.interest.value)
     if ( r > 1 ) r/=100
     r/=12
     pmt+=eval(form.extra.value)
     var int2 =0
     for (t=1;t<=term;t++) {
          var i = Math.round((pv*r)*100)/100  // calc interest due
          int2+=i          // record total interest paid
          pv+=i            // add the interest due to the balance
          pv-=pmt          // deduct the payment
          if (pv <= 0 )  {
             break;
             }
     }

     form.saveamt.value = Math.round((int1-int2)*100) /100
     if (t > 12) {
        var m = t % 12
        t-=m
        var y= t / 12
        form.savetermy.value= y
        form.savetermm.value= m
     }
     else {
        form.savetermy.value=""
        form.savetermm.value= t
     }

}

function clearForm(form) {
     form.principal.value = "100000";
     form.term.value = "30";
     form.interest.value="8.0"
     form.extra.value="0"
     form.savetermy.value=""
     form.savetermm.value=""
     form.saveamt.value="0"
}
