// Copyright (c) 2001 D3 New Media Interactive Savings Calculator.

function calculate(obj) {

	
	a=obj.monthly.value;
	b=obj.interest.value;
	c=obj.term.value;
	f=Math.abs(obj.initial.value);

  i=eval((b/100)/12);
  n=eval(c*12);
  v=eval(1/(1+i));

  z=Math.pow((1+i),n);
  w=Math.pow(v,n);

  totala=eval((f+a*((1-w)/i)*(1+i))*z);
  totalamt = eval(f+(a*n));
  totalint = eval(totala - totalamt);

  obj.totalone.value=rounding(totala);
  obj.totalamt.value=rounding(totalamt);
  obj.totalint.value=rounding(totalint);

}


// Copyright (c) 2001 D3 New Media Interactive Borrowing Calculator.

  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 - 2);
	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(0.32 * TotalIncome);
	Monthly = eval(Dsr - TotalExpense);

	TotalAmount = pv(Rate, Term.value, -Monthly);
	TotalAmount = rounding(TotalAmount);
	Monthly = rounding(Monthly);
	if (TotalAmount > 0) {
		window.alert("You can borrow $" + TotalAmount + " and your monthly repayments will be $" + Monthly + "");
	}
	else {
		window.alert("Your income is currently insufficient to meet proposed loan repayments.");
	}
	}
}

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