// JavaScript Document

function compute(obj) {
        x=obj.LoanAmount.value
        y=obj.Rate.value
        z = obj.Term.value

    if ((x=="") || (y=="") || (z=="")) {
        alert("You have not entered a value in all three fields");
    }

    else {
        month = eval(pmt(x,(y*.01)/12,z*12));
        fnight = eval(pmt(x,(y*.01)/26,z*26));
        week = eval(pmt(x,(y*.01)/52,z*52));
        obj.Monthly.value = rounding(month)
    }
}

function pmt(ppl,rate,mths) {

        var x=eval((ppl*rate)/(eval(1-Math.pow(eval(rate+1),eval(0-mths)))))
        return x
}

function rounding(n)
{
        pennies = n * 100 ;
        pennies = Math.round(pennies) ;
        strPennies = "" + pennies ;
        len = strPennies.length ;
        return strPennies.substring(0, len - 2) + "." + strPennies.substring((len - 2), len);
}

function nper(obj) {
        fv = 0;
        origterm = parseFloat(obj.Term.value);
        origpay = parseFloat(obj.Monthly.value);
        extpay = parseFloat(obj.Extra.value);
        pv = parseFloat(obj.LoanAmount.value);
        per = 12;
        pv = -pv;
        pay = origpay + extpay;
        rate = obj.Rate.value;

   if (obj.Extra.value == "") {

        alert("Please enter how much extra you would like to pay off your loan!");
   }
   else {
   rate = eval((rate)/(per * 100));
   if ( rate == 0 )    // Interest rate is 0
   {
         nper_value = - (fv + pv)/pay; 
   }
   else 
   {
         nper_value = Math.log((-fv * rate + pay)/(pay+ rate * pv))/ Math.log(1 + rate);
   }
        new_nper =  rounding(nper_value);
        orig_total_amt = origpay * origterm * 12;
        new_total_amt = pay * new_nper;
        save = orig_total_amt - new_total_amt;
        obj.NewTerm.value = rounding(nper_value/12);
        obj.Saving.value = Math.round(save);
   }
}