/************************************************

   calculator2.js - Specific calculator2 routines
  
   Copyright 2003 Better-Mortgages.co.uk Ltd
   All rights reserved.
  
 ************************************************/

helpText = "Please enter all details required on the left hand form, including your Gross Annual Income. If you wish to include overtime payments, bonuses and or other irregular income from your work, please add only 50% of this irregular income to your normal salary. Choose to assess on single income or a joint income, as appropriate. Enter monetary values as whole pounds only. When you press the 'Calculate' button, the results will appear in the right hand box.\n\nWhen choosing an interest rate, remember that while there may be some mortgages offering low rates, the very lowest 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.";

function calcMortgage(form){
  var incTypeIdx = form.income_type.selectedIndex;
  var lenderAmount = "" + form.gross_income.value*form.income_type.options[incTypeIdx].value;
  var rateStrng = form.rateU.selectedIndex + "." + form.rateT.selectedIndex + form.rateH.selectedIndex;
  var theTerm = form.term.options[form.term.selectedIndex].value*12;
  var thePayment = "" + form.payment_target.value;
  var theRate = (rateStrng/100)/12;
  var repayId = form.repay_method.selectedIndex;
  var repayMethod = repayText[repayId];
  if (repayId == 0) var theMortgage = "" + Math.round(thePayment/(theRate)*100)/100;
  else if (repayId == 1)theMortgage = "" + Math.round(thePayment/(theRate*(1 - 1/(1-Math.pow((1+theRate),theTerm))))*100)/100;
  var incType; 
  (incTypeIdx == 0) ? incType = "Single" : incType = "Joint";
  var theResult = repayMethod + "Term = " + theTerm/12 + " years\n";
  theResult += "Interest Rate = " + rateStrng + "%\n";
  theResult += "For a monthly payment = £"  + insertCommas(thePayment) +"\n";
  theResult += "You can afford to borrow £" + insertCommas(theMortgage) +"\n\n";
  theResult += "Based on your Gross Income, a lender may be prepared to lend ";
  theResult += "up to £" + insertCommas(lenderAmount) + " for a " + incType + " income";
  form.results.value = theResult;
  return false;
}