/************************************************
   calculator4.js - Specific calculator4 routines
   Copyright 2003 Better-Mortgages.co.uk Ltd
   All rights reserved.
 ************************************************/

helpText = "Please enter all details required on the form including estimates for refurbishment and furnishings if appropriate. You should have researched your likely rental income and management fees (usually 10-15% of rental). You should also estimate a value for maintenance of the property each year. When you press the 'Calculate' button, the results will appear in the 'results' box below.\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.\n\nMonthly profit is calculated as the rental income minus all monthly costs (including a pro rata portion of the mainenance costs).\nGross Yield is calculated as annual rental income divided by the property price.";

function stampDuty(price) {  // Inland Revenue data // last updated 24/03/2010
  var mult = 0.0;
  if (price >= 1000001) mult = 0.05;
  else if (price >= 500001) mult =  0.04;     
  else if (price >= 250001) mult =  0.03;  
  return price * mult;
}

function LRFees(price) {  // Landregistry website last updated 24/03/2010
  var lreg = 50.00;
  if (price >= 1000001) lreg = 920.00;
  else if (price >= 500001) lreg = 550.00;
  else if (price >= 200001) lreg = 280.00;
  else if (price >= 100001) lreg = 200.00;
  else if (price >= 80001) lreg = 130.00;
  else if (price >= 50001) lreg = 80.00;
  return lreg;
}

function solicitorFees() { 
// Average of eConveyancer fees
return 750;  // ******* check *********
}

function calcMortgage(form){
  var rateStrng = form.rateU.selectedIndex + "." + form.rateT.selectedIndex + form.rateH.selectedIndex;
  var theRate = eval((rateStrng/100)/12);
  var theTerm = eval(form.term.options[form.term.selectedIndex].value*12);
  var thePropertyValue = form.property_value.value;
  var theMortgage = Math.round(thePropertyValue * 0.85); // 85% LTV max
  var theDeposit = eval(thePropertyValue - theMortgage);
  var totalCost = eval(thePropertyValue) + stampDuty(thePropertyValue) + LRFees(thePropertyValue) + solicitorFees() + eval(form.refurb.value) + eval(form.appliances.value);
  var outLay = totalCost - theMortgage;
  var repayId = form.repay_method.selectedIndex;
  if (repayId == 0){  // Interest Only
    var thePayment = paymentInt(theMortgage, theRate);
    var theType = "n Interest Only Mortgage";
  } else if (repayId == 1){  // Capital Repayment
    var thePayment = paymentCrm(theMortgage, theRate, theTerm);
    var theType = " Capital Repayment Mortgage";
  }
  var theFees = thePayment + eval(form.mgmt_fees.value) + eval(form.property_maint.value)/12;
  var theProfit = "" + eval(form.rental_income.value) - theFees;
  var theYield = "" + ((form.rental_income.value * 12)/thePropertyValue);  
  var theRefurb = "" + form.refurb.value;
  var theAppliances = "" + form.appliances.value;
  var theRent = "" + form.rental_income.value;
  var rentMessage = ""
  if (theRent < thePayment*1.3) {
    rentMessage = " Most lenders require rent at least\n";
    rentMessage += "                            130% times the mortgage payment";
  }
  var theResult = "Purchase Costs\n--------------\n";
  theResult += "        Maximum Loan to Value (85%) = £" + insertCommas("" + theMortgage);
  theResult += "\n                      Deposit (15%) = £" + insertCommas("" + theDeposit);
  theResult += "\n                         Stamp Duty = £" + insertCommas("" + stampDuty(thePropertyValue));
  theResult += "\n                 Land Registry Fees = £" + insertCommas("" + LRFees(thePropertyValue));
  theResult += "\n          Estimated Solicitors Fees = £" + insertCommas("" + solicitorFees());
  theResult += "\n       Estimated Refurbishment cost = £" + insertCommas(theRefurb);
  theResult += "\nEstimated Appliance/Furnishing cost = £" + insertCommas(theAppliances);
  theResult += "\n                                      ------------";
  theResult += "\n       Total Estimated Initial Cost = £" + insertCommas("" + totalCost);
  theResult += "\n      Your Estimated Initial Outlay = £" + insertCommas("" + outLay);
  theResult += "\n\nOngoing Monthly Costs\n---------------------\n";
  theResult += "         Rental Income = £" + theRent + rentMessage;
  theResult += "\n      Mortgage Payment = £" + insertCommas("" + thePayment) + " for a" + theType;
  theResult += "\n          Monthly fees = £" + Math.round((theFees - thePayment)*100)/100;
  theResult += "\n        Monthly Profit = £" + Math.round(theProfit*100)/100 + " (before tax)";
  theResult += "\nGross investment Yield = " + Math.round(theYield*100*100)/100 + "%";
  form.results.value = theResult;
  return false;
}
