

function roundToPennies(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 Monthly(principle, years, apr)
{
   rate = apr / 12;
   payments = years * 12;
   return roundToPennies(principle * rate / (1 - (1 / Math.pow(1 + rate, payments))));
}



function MirasCalc(amount, apr)
{
miras_rate=10;

if (amount<30000)
	{
	return roundToPennies(miras_rate*amount*apr/1200);
	}
else
	{
	return roundToPennies(10*30000*apr/1200);
	}
}

function MonthlyInterest(amount, apr)
{
   return roundToPennies(amount * apr / 12);
}


function compute_borrowing(f) {

  income = eval(f.income.value)
  f.most_loan.value = income * 3
  f.some_loan.value = income * 4

  salary1 = eval(f.salary1.value)
  salary2 = eval(f.salary2.value)

  total_salaries = eval(salary1) + eval(salary2)

  f.most1_joint_loan.value = total_salaries * 2.5
  f.some1_joint_loan.value = total_salaries * 3

  f.most2_joint_loan.value = (salary1 * 3) + salary2
  f.some2_joint_loan.value = (salary1 * 4) + salary2

}

function compute_payments(f)
{
   if((f.mortgage_required.value.length != 0) &&
      (f.interest_rate.value.length != 0) &&
      (f.term.value.length != 0))
   {
      principle = eval(f.mortgage_required.value);
      apr = eval(f.interest_rate.value) / 100.0;
      years = eval(f.term.value);

      if(years == 0.0)
      {
         alert("You have no monthly payment, since the number of years is zero.");
      }
      else
      {
   payments = years * 12;
   monthlyInterest = apr / 12;
   monthlyInt = MonthlyInterest(principle, apr);
   monthlyPayment = Monthly(principle, years, apr);
   miras = MirasCalc(principle, apr);
   f.repayment.value = roundToPennies(monthlyPayment - miras);
   f.interest_only.value = roundToPennies(monthlyInt - miras);

      }
   }
   else
   {
      alert("You must fill in all the fields.");
   }
}



