// JavaScript Document

  function select_item(name, value) {
        this.name = name;
        this.value = value;
    }

    function get_selection(select_object) {
        contents = new select_item();
        for(var i=0;i<select_object.options.length;i++)
           if(select_object.options[i].selected == true) {
                contents.name = select_object.options[i].text;
                contents.value = select_object.options[i].value;
            }
        return contents;
    }

function compute(obj) {
        Income=obj.Income.value;
        OtherIncome=obj.OtherIncome.value;
        CarExpense= obj.CarExpense.value;
        CreditExpense=obj.CreditExpense.value;
        OtherExpense=obj.OtherExpense.value;
        Term = get_selection(obj.Term);
        Interest=obj.Interest.value;
        Rate = -(-Interest - 0);
        if (OtherIncome == "") {
          OtherIncome = 0;
        }
        if (CarExpense == "") {
          CarExpense = 0;
        }
        if (CreditExpense == "") {
          CreditExpense = 0;
        }
        if (OtherExpense == "") {
          OtherExpense = 0;
        }
        if (Income == "") {
          window.alert("You must enter your income!");
        }
        else {
        TotalIncome = -(-Income - OtherIncome);
        TotalExpense = -(-CarExpense - CreditExpense - OtherExpense);
        Dsr = eval(1 * TotalIncome);
        Monthly = eval(Dsr - TotalExpense);

        TotalAmount = pv(Rate, Term.value, -Monthly);
        TotalAmount = rounding(TotalAmount);
        Monthly = rounding(Monthly);
        if (TotalAmount > 0) {
		obj.TotalAmount.value = TotalAmount;
		obj.Monthly.value = Monthly;
}
        else {
                window.alert("Your income is insufficient to qualify for a home loan.");
        }
        }
}

function pv(rate, nper, pmt) {
nper = parseFloat(nper);
pmt = parseFloat(pmt);
fv = 0
per = 12;
rate = eval((rate)/(per * 100));

if (( pmt == 0 ) || ( nper == 0 )) {
        alert("Why do you want to test me with zeros?");
        return(0);
}

if ( rate == 0 ) // Interest rate is 0
{
        pv_value = -(fv + (pmt * nper));
}
else {
        x = Math.pow(1 + rate, -nper);
        y = Math.pow(1 + rate, nper);
        pv_value = - ( x * ( fv * rate - pmt + y * pmt )) / rate;
        return(pv_value);
}
}

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);
}

// stop hiding -->
