var TitleDisc = new Array(0.4,0.4,0.35,0.3,0.25,0.2,0.15);

var FeeTable = new Array(229,233,235,239,243,246,250,254,257,260,262,266,270,274,277,281,285,287,290,293,298,301,305,308,312,315,318,321,325,328,332,335,339,342,345,348,352,355,359,362,366,369,373,376,379,383,386,390,393,397,400,404,407,410,413,417,421,425,427,431,434,438,440,445,448,452,454,458,461,465,469,472,475,479,481,485,489,493,496,499,503,506,508,512,516,520,523,527,530,533,536,539,544,547,550,554,558,560,564,567,571,575,578,581,585,589,591,594,598,602,605,608,612,617,620,621,625,629,632,635,640,644,647,649,652,656,660,663,667,671,674,676,680,683,687,690,694,698,702,703,707,711,715,717,721,725,729,731,734,739,742,745,748,752,756,759,762,766,770,772,775,779,783,787,789,793,797,801,802,806,811,814,816,820,824,828,830,834,838,841,843);

// Function to calculate the title commitment fee for the lender's policy.
//
function getLenderTitleFees(ownFee)
{
  // The lender's policy is $100 if issued simultaneously with the owner's
  // policy plus the following endorsements:
  // T-36 - $25.00
  // Tax Deletion - $20.00
  // Not yet Due & Payable - $5.00
  // T-17 - $25.00
  // T-19 is approximately 5% of the cost of the Owners Title Policy
  // T-19.2 - $50.00
  return(225.0 + 0.05*ownFee);
}

// Function to calculate the title commitment fee for the owner's policy.
//
function getTitleFees(amt, bal, years, coFlag)
{
  // The loan amount is 100,000 or less, so use the table.
  var base;
  if (amt <= 100000) {
    var ind = getTableIndex(amt);
    base = FeeTable[ind];
  }

  // The loan amount is between 100,001 and 1,000,000.
  else if (amt <= 1000000) {
    base = 843+0.00534*(amt-100000);
  }

  // The loan amount is between 1,000,001 and 5,000,000.
  else if (amt <= 5000000) {
    base = 5649+0.00439*(amt-1000000);
  }

  // The loan amount is between 5,000,001 and 15,000,000.
  else if (amt <= 15000000) {
    base = 23209+0.00362*(amt-5000000);
  }

  // The loan amount is between 15,000,001 and 25,000,000.
  else if (amt <= 25000000) {
    base = 59409+0.00257*(amt-15000000);
  }

  // The loan amount exceeds 25,000,000.
  else {
    base = 85109+0.00154*(amt-25000000);
  }

  // For cash-outs, we need to account for the endorsements.
  var scale = (coFlag == 0) ? 1.0 : 1.25;

  // The borrower gets a discount based on the number of years since the
  // existing loan closed.
  var disc = (years > 6) ? 1.0 : (1-TitleDisc[years]);

  // The fee takes into account the current loan balance (if any) and
  // the new loan amount.
  var fee = ((bal/amt)*disc*base)+(((amt-bal)/amt)*base)*scale;

  return(fee);
}

//
// Helper function to calculate the index for the title fees table.
//
function getTableIndex(amt)
{
  var ind = (amt < 10000) ? 0 : (amt-10000)/500;
  return(Math.ceil(ind));
}
