/************************************************
   calculator3.js - Specific calculator3 routines
   Copyright 2003 Better-Mortgages.co.uk Ltd
   All rights reserved.
 ************************************************/

helpText = "Please enter all details for your Current Mortgage on the top left hand form, including a property value (even if only an estimate). Enter all details for the proposed New Mortgage on the top right hand form, including a property value. When you press the 'Calculate' button, the results will appear in the three boxes below. The left hand results box allows you to check that your current mortage details are correct. The right hand results box shows you what your New Mortgage payments might be. The large results box at the bottom shows the difference in monthly payments and total cost over the term. A positive difference shows a potential saving, while a negative difference shows that you may pay extra for the New Mortgage.\n\nWhen you choose an interest rate, remember that while there may be some mortgages offering lower rates than the Bank of England Base Rate, these rates tend to be short term offers, where the rate reverts to the lenders base rate in the first year or two. You also need to be aware that there are costs associated with the arrangement of any mortgage which will be over and above your monthly payments.";

alertLTV = "You are asking to borrow more than 100% of the property value. Very few mortgages of 100% loan to value (LTV) or greater are currently available. If you are looking to arrange a first mortgage or remortgage, the typical LTV is 95% or less.\n\nFor a Buy to Let mortgage you may not find a lender willing to lend more than 75%.\n\nThe more deposit you can offer, the lower will be your LTV, and the more likely you will be able to find a mortgage at favourable terms. You should also remember that when you buy or sell a property there will be other related costs and fees, and you need to take these into account in your overall calculations.\n\nIf you need a loan of 100% LTV please give us a call as we may still be able to help by structuring a group of loans to provide an equivalent 100% LTV facility, providing you can afford it.";
//alertLTV = "You are asking to borrow more than 100% of the property value. While mortgages of greater than 100% are available, they are rare. If you are looking to arrange a first mortgage or remortgage, the typical Loan to Value (LTV) is 85% - 90%. For a Buy to Let mortgage you will be unlikely to find a lender willing to lend more than 85%. The more deposit you can offer, the lower will be your LTV, and the more likely you will be able to find a mortgage at favourable terms. You should also remember that when you buy or sell a property there will be other related costs and fees, and you need to take these into account in your overall calculations.";

function amortise(principle, monthly_interest, monthly_payment){
  var i, balance, current_interest_payment, current_principle_payment;
  var total_interest = 0;
  var interest = monthly_interest - 1;
  for (i = principle; i > 0; i = balance) {
    current_interest_payment = i * interest;
    current_principle_payment = monthly_payment - current_interest_payment;
    balance = i - current_principle_payment;
    total_interest += current_interest_payment;
  }
return total_interest;
}

function resultsX(rp, mg, rt, mp, tp) {
  return rp +"Mortgage = £" + insertCommas(mg) + "\nInterest Rate = " + rt + "%\nMonthly repayment = £" + insertCommas(mp) + "\nTotal cost = £" + insertCommas(tp);
}

// c=current mortgage calcs, n=new mortgage calcs
function calcMortgage(form){
  var rateStrng_c = form.rateU_c.selectedIndex + "." + form.rateT_c.selectedIndex + form.rateH_c.selectedIndex;
  var theRate_c = (rateStrng_c/100)/12;
  var theTerm_c = form.term_c.options[form.term_c.selectedIndex].value*12;
  var theMortgage_c = 1.0*form.amount_c.value;
  var thePropertyValue_c = form.property_value_c.value;
  var theLTV_c = Math.round(theMortgage_c / thePropertyValue_c*100);
  var repayId_c = form.repay_method_c.selectedIndex;
  var repayMethod_c = repayText[repayId_c];
  if (theLTV_c > 100) alert(alertLTV);
  if (repayId_c == 0){  // Interest Only
    var theMonthlyPayment_c = paymentInt(theMortgage_c, theRate_c);
    var theTotalPayment_c = theMortgage_c + theMonthlyPayment_c * theTerm_c;
  }
  else if (repayId_c == 1){  // Capital Repayment
    var theMonthlyPayment_c = paymentCrm(theMortgage_c, theRate_c, theTerm_c);
    var theTotalPayment_c = theMortgage_c + amortise(theMortgage_c, (1+theRate_c), theMonthlyPayment_c);
  }
  var tMPc = theMonthlyPayment_c;
  var tTPc = theTotalPayment_c;
  theMonthlyPayment_c = "" + tMPc;
  theTotalPayment_c = "" + Math.round(tTPc*100)/100;
  theMortgage_c = "" + theMortgage_c;
  var theResult1 = resultsX(repayMethod_c, theMortgage_c, rateStrng_c, theMonthlyPayment_c, theTotalPayment_c);
  var rateStrng_n = form.rateU_n.selectedIndex + "." + form.rateT_n.selectedIndex + form.rateH_n.selectedIndex;
  var theRate_n = (rateStrng_n/100)/12;
  var theTerm_n = form.term_n.options[form.term_n.selectedIndex].value*12;
  var theMortgage_n = 1.0*form.amount_n.value;
  var thePropertyValue_n = form.property_value_n.value;
  var theLTV_n = Math.round(theMortgage_n / thePropertyValue_n*100);
  var repayId_n = form.repay_method_n.selectedIndex;
  var repayMethod_n = repayText[repayId_n];
  if (theLTV_n > 100) alert(alertLTV);
  if (repayId_n == 0){  // Interest Only
    var theMonthlyPayment_n = paymentInt(theMortgage_n, theRate_n);
    var theTotalPayment_n = theMortgage_n + theMonthlyPayment_n * theTerm_n;
  }
  else if (repayId_n == 1){  // Capital Repayment
    var theMonthlyPayment_n = paymentCrm(theMortgage_n, theRate_n, theTerm_n);
    var theTotalPayment_n = amortise(theMortgage_n, (1 + theRate_n), theMonthlyPayment_n) + theMortgage_n;
  }
  var tMPn = theMonthlyPayment_n;
  var tTPn = theTotalPayment_n;
  theMonthlyPayment_n = "" + tMPn;
  theTotalPayment_n = "" + Math.round(tTPn*100)/100;
  theMortgage_n = "" + theMortgage_n;
  var theResult2 = resultsX(repayMethod_n, theMortgage_n, rateStrng_n, theMonthlyPayment_n, theTotalPayment_n);
 
// Now define saving output for results3
  var theMonthlyDifference = difference(tMPc, tMPn);
  var moreOrLessPM = moreOrless(theMonthlyDifference);
  if (moreOrLessPM == " more") theMonthlyDifference = theMonthlyDifference.substring(1,theMonthlyDifference.length);
  var theTotalDifference = difference(tTPc, tTPn);
  var moreOrLessTot = moreOrless(theTotalDifference);
  if(moreOrLessTot == " more") theTotalDifference = theTotalDifference.substring(1,theTotalDifference.length);
  theResult3 = "Potential Savings\n-----------------\n"
  theResult3 += "         Current monthly payment = £" + insertCommas(theMonthlyPayment_c) + "\n";
  theResult3 += "             New monthly payment = £" + insertCommas(theMonthlyPayment_n) + "\n";
  theResult3 += "On your new mortgage you would pay £" + insertCommas(theMonthlyDifference) + moreOrLessPM + " per month.\n\n";
  theResult3 += "    Current total mortgage costs = £" + insertCommas(theTotalPayment_c) + "\n";
  theResult3 += "        New total mortgage costs = £" + insertCommas(theTotalPayment_n) + "\n";
  theResult3 += "On your new mortgage you would pay £" + insertCommas(theTotalDifference) + moreOrLessTot +" over the term.";
  form.results1.value = theResult1;
  form.results2.value = theResult2;
  form.results3.value = theResult3;
  return false;
}