// Given:   P = principal (eg: 150000)
//          I = annual interest rate (eg: 7.75)
//          L = length of loan in years (eg: 25)
//          F = frequency of payments (monthly = 12, weekly = 52, etc)
//
// Returns: monthly payment amount (eg: 1120.98)

function MonthlyPayment( P, I, L, F )
{
   var J = Math.pow( 1 + I / 200, 1 / (F / 2) );

   var payment = P * (J - 1) / (1 - Math.pow( J, L * -F ));

   return Math.round( payment * 100 ) / 100;
}

// Given:   P = principal (eg: 150000)
//          I = annual interest rate (eg: 7.75)
//          N = number of monthly payments made (eg: 60)
//          F = frequency of payments (monthly = 12, weekly = 52, etc)
//          PMT = amount of monthly payment
//
// Returns: balance remaining

function BalanceRemaining( P, I, N, F, PMT )
{
   var J = Math.pow( 1 + I / 200, 1 / (F / 2) ) - 1;

   var balance = (P * Math.pow( 1 + J, N ) - PMT * (Math.pow( 1 + J, N ) - 1) / J);

   return Math.round( balance * 100 ) / 100;
}

// Given:   FV = final value (eg: 0)
//          I = annual interest rate (eg: 7.75)
//          N = number of monthly payments made (eg: 60)
//          F = frequency of payments (monthly = 12, weekly = 52, etc)
//          PMT = amount of monthly payment
//
// Returns: present value

function PV( FV, I, N, F, PMT )
{
   var J = Math.pow( 1 + I / 200, 1 / (F / 2) ) - 1;

   var PV = Math.pow( 1 + J, -N ) * (FV * J - PMT + Math.pow( 1 + J, N ) * PMT) / J;

   return Math.round( PV * 100 ) / 100;
}

function CalcPayments()
{
   var principal	= document.formpmts.principal.value;
   var years		= 25;
   var frequency;
   var rate;
   var start;
   var end;

   start = document.formpmts.irate.options[document.formpmts.irate.selectedIndex].text.indexOf( "@" ) + 1;
   end = document.formpmts.irate.options[document.formpmts.irate.selectedIndex].text.indexOf( "%" );
   rate = parseFloat( document.formpmts.irate.options[document.formpmts.irate.selectedIndex].text.substring( start, end ) );


   if( document.formpmts.freq.options[document.formpmts.freq.selectedIndex].text == "weekly" )
      frequency = 52;
   else if( document.formpmts.freq.options[document.formpmts.freq.selectedIndex].text == "bi-weekly" )
      frequency = 26;
   else if( document.formpmts.freq.options[document.formpmts.freq.selectedIndex].text == "semi-monthly" )
      frequency = 24;
   else
      frequency = 12;

   document.formpmts.payment.value = MonthlyPayment( principal, rate, years, frequency );
}

function Calc5YearSavings()
{
   var principal	= document.form5y.principal.value;
   var years		= 25;
   var numpayments	= 60;
   var frequency	= 12;
   var pmt;

   pmt = MonthlyPayment( principal, b_fiveYear, years, frequency );
   document.form5y.payment.value = pmt;

   document.form5y.b_balance.value = BalanceRemaining( principal, b_fiveYear, numpayments, frequency, pmt );

   document.form5y.balance.value = BalanceRemaining( principal, fiveYear, numpayments, frequency, pmt );
   document.form5y.savings.value = Math.round( (document.form5y.b_balance.value - document.form5y.balance.value) * 100 ) / 100;
}

function Calc10YearSavings()
{
   var principal	= document.form10y.principal.value;
   var years		= 25;
   var numpayments	= 120;
   var frequency	= 12;
   var pmt;

   pmt = MonthlyPayment( principal, b_tenYear, years, frequency );
   document.form10y.payment.value = pmt;

   document.form10y.b_balance.value = BalanceRemaining( principal, b_tenYear, numpayments, frequency, pmt );

   document.form10y.balance.value = BalanceRemaining( principal, tenYear, numpayments, frequency, pmt );
   document.form10y.savings.value = Math.round( (document.form10y.b_balance.value - document.form10y.balance.value) * 100 ) / 100;
}

function DownPayment( P )
{
   var principal	= parseFloat( P );
   var downpayment;

   if( principal > 250000 )
      downpayment = principal * .1;
   else
      downpayment = principal * .05;

   return Math.round( downpayment * 100 ) / 100;
}

function RequiredDownPayment( P )
{
   var principal	= parseFloat( P );
   var downpayment;

   if( principal > 237500 )
      downpayment = principal / 9;
   else
      downpayment = principal / 19;

   return Math.round( downpayment * 100 ) / 100;
}
