// Variable to record the width of an option column.
var OptionWidth = 0;
var DivWidth = 0;

// Variables to keep tracks of the options displayed.
var CurrentOption;
var OptionsString = ',1';

// Variable to record whether tab results are stale.
//
var ResultsDisplayed = false;
var curResultsArray = new Array();

// Callback function when the user changes the transaction type.
//
function chooseTypeCB(e)
{
  var type = e.value;
  var num_opt = document.getElementById('num_options').value;

  // If results have been calculated or the user has added additional
  // options, make the user confirm changing the type.
  if (((ResultsDisplayed) &&
       (! confirm('The results you have calculated will be cleared.'))) ||
      ((num_opt > 1) &&
       (! confirm('The additional options you have added will be deleted\nand the results will be cleared.')))) {
    // Reset the type.
    e.value = document.getElementById('start_type').value;
    return;
  }
  else {
    // Reload the page passing the desired type.
    window.open("/cgi-bin/LoanCompare.pl?type=" + type, "_self");
  }
}

// Callback function when user checks/unchecks second lien.
//
function checkSecLien(e, opt)
{
  // Make the second lien controls editable or not.
  var dis_val = (e.checked) ? false : true;
  var cname1 = (dis_val) ? 'number_noedit' : 'number';
  var term_e = document.getElementById('termsec' + opt);
  var dp1_e = document.getElementById('dp2$' + opt);
  var dp2_e = document.getElementById('dp2%' + opt);
  var rate_e = document.getElementById('ratesec' + opt);
  term_e.disabled = dis_val;
  dp1_e.readOnly = dis_val;
  dp1_e.className = cname1;
  dp2_e.readOnly = dis_val;
  dp2_e.className = cname1;
  rate_e.readOnly = dis_val;
  rate_e.className = cname1;

  // If the user hides the second lien, reset the loan amount to zero
  // and reset the first lien loan amount.
  if (! e.checked) {
    dp1_e.value = 0;
    dp2_e.value = 0;

    // Set the hidden loan amount input.
    var amount = calcLoanAmount(opt);
    document.getElementById('_amount' + opt).value = amount;
  }

  // Call a perl function to get the second lien rate if it hasn't
  // already been set.
  var rate_inp = 'ratesec' + opt;
  if ((! rate_e.value) ||
      (rate_e.value == 0)) {
    getSecRate([], [rate_inp]);
  }

  // Also, call the perl function to reset the max LTV and CLTV.
  getMaxLtvCB(opt)
}

// Validation function for the cash-out input.
//
function validateCashOut(e, opt)
{
  if (validateAmount(e, 0)) {
    // The sum of the balance and cash-out cannot result in an LTV
    // greater than maxCashOutLTV.
    var price = stripCommas(document.getElementById('price' + opt).value);
    var bal = stripCommas(document.getElementById('balance' + opt).value)/1;
    var cash = stripCommas(document.getElementById('cash' + opt).value)/1;

    if (((bal+cash)/price) > maxCashOutLTV) {
      alert('By state law your new loan amount cannot be more than ' + (maxCashOutLTV*100) + "% of your home value.");

      e.focus();
      e.select();
      return(false);
    }
  }

  return(true);
}

// Callback function when the user changes the loan program.
//
function setProgram(e, opt)
{
  var prog = e.value;

  var occ_e = document.getElementById('residency' + opt);
  var term_e = document.getElementById('term' + opt);

  // For all government programs, hide all occupancies except primary.
  if ((prog == 'USDA') || (prog == 'FHA') || (prog == 'VA') || (prog == 'TVLB')) {
    // Get the option elements associated with occupancy.
    // Hide those with class equal to 'conv'.
    // Reset the value if a hidden option is selected.
    var opt_array = occ_e.options;
    for (var i = 0; i < opt_array.length; i ++) {
      if (opt_array[i].className == 'conv') {
        opt_array[i].style.display = 'none';

        if (i == occ_e.selectedIndex) {
          occ_e.selectedIndex = -1;
        }
      }
    }

    // This is a USDA, FHA, or VA program.  Hide the 10, 20, and 25-year terms.
    if ((prog == 'USDA') || (prog == 'FHA') || (prog == 'VA')) {
      // Get the option elements associated with loan term.
      // Hide those with class equal to 'conv'.
      // Reset the value if a hidden option is selected.
      var opt_array = term_e.options;
      for (var i = 0; i < opt_array.length; i++) {
        if (opt_array[i].className == 'conv') {
          opt_array[i].style.display = 'none';

          if (i == term_e.selectedIndex) {
            term_e.selectedIndex = -1;
          }
        }
      }

      // This is a USDA program.  Hide the 15-year loan term.
      if (prog == 'USDA') {
        // Get the option elements associated with loan term.
        // Hide those with class equal to 'usda'.
        // Reset the value if a hidden option is selected.
        var opt_array = term_e.options;
        for (var i = 0; i < opt_array.length; i++) {
          if (opt_array[i].className == 'usda') {
            opt_array[i].style.display = 'none';

            if (i == term_e.selectedIndex) {
              term_e.selectedIndex = -1;
            }
          }
        }
      }
      else {
        // Get the option elements associated with loan term.
        // Make visible those with class equal to 'usda'.
        var opt_array = term_e.options;
        for (var i = 0; i < opt_array.length; i++) {
          if (opt_array[i].className == 'usda') {
            opt_array[i].style.display = 'block';
          }
        }
      }
    }
  }

  // For conventional, show all occupancies.
  else {
    // Get the option elements associated with occupancy.
    // Make visible those with class equal to 'conv'.
    var opt_array = occ_e.options;
    for (var i = 0; i < opt_array.length; i++) {
      if (opt_array[i].className == 'conv') {
        opt_array[i].style.display = 'block';
      }
    }
  }

  // For conventional or TVLB, show all loan terms.
  if ((prog == 'Conv') || (prog == 'TVLB')) {
    // Get the option elements associated with loan term.
    // Make visible those with class equal to 'conv'.
    var opt_array = term_e.options;
    for (var i = 0; i < opt_array.length; i++) {
      if ((opt_array[i].className == 'conv') ||
          (opt_array[i].className == 'usda')) {
        opt_array[i].style.display = 'block';
      }
    }
  }
}

// Callback function to get the max LTV allowed for the program and
// other parameters entered.
//
function getMaxLtvCB(opt)
{
  // Get the inputs fields based on the selected tab.
  var prog_inp = 'prog' + opt;
  var res_inp = 'residency' + opt;
  var sec_inp = 'sec_flag' + opt;

  CurrentOption = opt;

  // Get the max LTV and CLTV from the perl function.
  getMaxLTV(['type', prog_inp, res_inp, sec_inp], [handleCheckMaxLtvCB]);
}

// Callback function to check that the user requested LTV is less than
// the max LTV allowed for the program and other parameters entered.
//
function checkMaxLtvCB(opt)
{
  // If we don't have the max LTV yet, get it now.
  var max_ltv = document.getElementById('_max_ltv' + opt).value;
  var max_cltv = document.getElementById('_max_cltv' + opt).value;
  if (! max_ltv) {
    getMaxLtvCB(opt);
  }
  else {
    CurrentOption = opt;
    handleCheckMaxLtvCB(max_ltv, max_cltv);
  }
}

// Handler function for checkMaxLtvCB.
//
function handleCheckMaxLtvCB()
{
  var max_ltv = arguments[0];
  var max_cltv = arguments[1];

  // I have to set the hidden inputs because I may have called this function
  // from the check function rather than the get function.
  document.getElementById('_max_ltv' + CurrentOption).value = max_ltv;
  document.getElementById('_max_cltv' + CurrentOption).value = max_cltv;

  // Make sure we have a down payment to compare.
  var dp = document.getElementById('dp%' + CurrentOption).value;
  var dp2 = document.getElementById('dp2%' + CurrentOption).value;
  if (dp2 > 0) {
    if (((100-(dp+dp2))/100) > max_cltv) {
      var max_per = max_cltv*100;
      alert("The sum of the first and second loan amounts cannot be\ngreater than " + max_per + '% of the value of the property.');
    }
  }
  if (dp) {
    if (((100-dp)/100) > max_ltv) {
      var max_per = max_ltv*100;
      alert('The loan amount cannot be greater than ' + max_per + '% of the value of the property.');
    }
  }
}

// Callback function to get a suggested interest rate.
// I have the pass the result back to a handler function because the
// rate is displayed in an input element.
//
function getRateCB(opt)
{
  // Get the inputs fields based on the selected tab.
  var prog_inp = '_prog' + opt;
  var term_inp = 'term' + opt;
  var res_inp = 'residency' + opt;
  var dp_inp = 'dp%' + opt;
  var dp2_inp = 'dp2%' + opt;
  var price_inp = 'price' + opt;
  var cout_inp = '_cash_flag' + opt;

  // I must have all the required parameters to get the rate.
  if (checkInputsNotNull('Calculator' + opt, 'number rate', 1)) {
    document.body.style.cursor = 'wait';

    // Strip any commas out of the price.
    price = stripCommas(document.getElementById(price_inp).value);

    // Set the hidden loan amount input.
    var amount = calcLoanAmount(opt);
    document.getElementById('_amount' + opt).value = amount;

    // Set the hidden program field based on the loan amount.
    var prog = document.getElementById('prog' + opt).value;
    if (prog == 'Conv') {
      document.getElementById(prog_inp).value = (amount > maxConvLoan) ? 'Jumbo' : 'Conforming';
    } else {
      document.getElementById(prog_inp).value = prog;
    }

    CurrentOption = opt;

    // Pass the term and residency to the perl function.
    getBrokerRate([prog_inp, term_inp, res_inp, dp_inp, dp2_inp, cout_inp], [handleGetRateCB]);
  }
}

// Handler function for the getRateCB.
//
function handleGetRateCB()
{
  var rate = arguments[0];
  document.getElementById('rate' + CurrentOption).value = (rate/1).toFixed(3);

  document.body.style.cursor = 'default';
}

// Helper function to calculate the loan amount.
//
function calcLoanAmount(opt)
{
  var amount;

  // Calculate the loan amount.
  var type = document.getElementById('type').value;
  if (type == 1) {
    amount = stripCommas(document.getElementById('price' + opt).value)-stripCommas(document.getElementById('dp$' + opt).value);
  }
  else if (type == 2) {
    amount = stripCommas(document.getElementById('balance' + opt).value);
  }
  else {
    amount = stripCommas(document.getElementById('balance' + opt).value)/1+stripCommas(document.getElementById('cash' + opt).value)/1;
  }

  // Subtract any second lien for a purchase.
  if (type == 1) {
    var sec_amt = stripCommas(document.getElementById('dp2$' + opt).value);
    if (sec_amt > 0) {
      amount -= sec_amt
    }
  }

  return(amount);
}

// Callback function to handle changes to the second lien amount.
//
function calcSecLoanAmount(opt)
{
  // Recompute the first lien loan amount.
  var amount = calcLoanAmount(opt);
  document.getElementById('_amount' + opt).value = amount;
  
}

// Callback function to add option column.
//
function addOptionCB()
{
  document.body.style.cursor = 'wait';

  // Call perl function using ajax to add a new option.
  addOption(['num_options','type'], [handleAddOptionCB]);
}

// Handler for addOptionCB.
//
function handleAddOptionCB()
{
  var html = arguments[0];

  // Increment the number of options.
  var num_opt = document.getElementById('num_options').value;
  var next_opt = (num_opt/1)+1;
  document.getElementById('num_options').value = next_opt;

  // For the first option added, record the width.
  if (OptionWidth == 0) {
    // Add the new option div to the main div.
    // If I append to the compare div innerHTML, it wipes out existing inputs.
    var new_div = document.createElement('div');
    new_div.innerHTML = html;
    document.getElementById('compare').appendChild(new_div);

    // Get the width of a new option.
    DivWidth = document.getElementById('option1').offsetWidth;
    OptionWidth = document.getElementById('option2').offsetWidth;
    DivWidth += OptionWidth;
  }

  // If I already have the widths, resize the div first.
  // Then, append the html.
  else {
    // Add the new option width.
    var last_width = DivWidth;
    DivWidth += OptionWidth;

    // Once I add the new option, the width will exceed the div width.
    var add_width;
    if (DivWidth > 600) {
      if (last_width <= 600) {
        add_width = DivWidth-600;
      }

      // We already have expanded the width.
      else {
        add_width = OptionWidth;
      }

      var div_e = document.getElementById('main');
      div_e.style.width = div_e.offsetWidth+add_width;
    }

    // Add the new option div to the main div.
    // If I append to the compare div innerHTML, it wipes out existing inputs.
    var new_div = document.createElement('div');
    new_div.innerHTML = html;
    document.getElementById('compare').appendChild(new_div);
  }

  // Get the most recent option from the options string.
  // Add the new option to the options string.
  var last_opt = OptionsString.substring(OptionsString.lastIndexOf(',')+1);
  OptionsString += ',' + next_opt;

  // Copy some of the input parameters from the most recent option to the
  // new option.  Have to check whether I'm dealing with a purchase or refi.
  var inp_e = document.getElementById('balance' + last_opt);
  if (inp_e.value != '') {
    // For a refi, copy loan balance and close year.
    document.getElementById('balance' + next_opt).value = inp_e.value;
    document.getElementById('close_year' + next_opt).value = document.getElementById('close_year' + last_opt).value;
  }

  // Always copy the price, residency, and move date.
  // For a refi, the price is the home value.
  document.getElementById('price' + next_opt).value = document.getElementById('price' + last_opt).value;
  document.getElementById('residency' + next_opt).value = document.getElementById('residency' + last_opt).value;
  document.getElementById('move' + next_opt).value = document.getElementById('move' + last_opt).value;

  // Set focus to the first input in the new option.
  document.getElementById('price' + next_opt).focus();

  document.body.style.cursor = 'default';
}

// Callback function when the user clicks the Calc button.
//
function calcOptionCB(opt)
{
  // Check that we have all out inputs.
  if (checkInputsNotNull('Calculator' + opt, 'number')) {
    document.body.style.cursor = 'wait';

    // Get the inputs fields based on the selected tab.
    var prog_inp = '_prog' + opt;
    var term_inp = 'term' + opt;
    var res_inp = 'residency' + opt;
    var dp_inp = 'dp%' + opt;
    var price_inp = '_price' + opt;
    var cout_inp = 'cash' + opt;
    var la_inp = '_amount' + opt;
    var rate_inp = 'rate' + opt;
    var cyear_inp = 'close_year' + opt;
    var move_inp = 'move' + opt;
    var cc_inp = 'cc_flag' + opt;
    var term2_inp = 'termsec' + opt;
    var la2_inp = '_sec_amount' + opt;
    var dp2_inp = 'dp2%' + opt;
    var rate2_inp = 'ratesec' + opt;

    // For a purchase transaction, the balance is the loan amount.
    var type = document.getElementById('type').value;
    var bal_inp = (type == 1) ? la_inp : '_balance' + opt;

    // Set the hidden inputs.
    document.getElementById(price_inp).value = stripCommas(document.getElementById('price' + opt).value);
    document.getElementById(la2_inp).value = (type == 3) ? 0 : stripCommas(document.getElementById('dp2$' + opt).value);
    if (type != 1) {
      document.getElementById(bal_inp).value = stripCommas(document.getElementById('balance' + opt).value)
    }

    CurrentOption = opt;

    // Call perl function using ajax to calculate the results.
    calcOption(['type', prog_inp, term_inp, res_inp, la_inp, dp_inp, rate_inp, cyear_inp, price_inp, bal_inp, move_inp, cc_inp, term2_inp, la2_inp, dp2_inp, rate2_inp], [handleCalcOptionCB]);
  }
}

// Handler for calcOptionCB.
//
function handleCalcOptionCB()
{
  var la = arguments[0];
  var cc = arguments[1];
  var umi = arguments[2];
  var pi = arguments[3];
  var pi2 = arguments[4];
  var mi = arguments[5];
  var payment = arguments[6];
  var apr = arguments[7];
  var eff_apr = arguments[8];
  var fc = arguments[9];

  // Put the results in the results elements.
  document.getElementById('r_amount' + CurrentOption).value = la;
  document.getElementById('r_cc' + CurrentOption).value = cc;
  document.getElementById('r_mi' + CurrentOption).innerHTML = mi;
  document.getElementById('r_payment' + CurrentOption).value = payment;
  document.getElementById('r_apr' + CurrentOption).value = apr;
  document.getElementById('r_fc' + CurrentOption).value = fc;
  document.getElementById('r_eff_apr' + CurrentOption).value = eff_apr;

  if (pi2) {
    document.getElementById('r_pandi' + CurrentOption).innerHTML = pi + ' / ' + pi2;
  } else {
    document.getElementById('r_pandi' + CurrentOption).innerHTML = pi;
  }

  // These need just a little more work.
  if (umi) {
    document.getElementById('r_umi' + CurrentOption).innerHTML = 'Includes ' + umi;
  }

  var move = document.getElementById('move' + CurrentOption).value;
  document.getElementById('r_move' + CurrentOption).innerHTML = "After " + move + ((move > 1) ? ' years' : 'year');

  // Make the display appear current.
  changeResultClass('result changed', 'result', 'option' + CurrentOption);
  curResultsArray[CurrentOption] = true;
  ResultsDisplayed = true;

  // Scroll the window down to the results.
//  document.getElementById('calc_button' + CurrentOption).scrollIntoView(true);

  document.body.style.cursor = 'default';
}

// Helper function to reset the className of the result elements.
//
function staleResults(tab)
{
  // If the results already are stale, just return.
  if (curResultsArray[tab]) {
    changeResultClass('result', 'result changed', 'option' + tab);
    curResultsArray[tab] = false;
  }
}

// Callback function when the user wants to delete an option.
//
function deleteOptionCB(opt)
{
  // Don't let the user delete the first option.
  if (opt == 1) {
    alert('You cannot delete the first option.');
  }
  else {
    // Remove the div element from the compare div.
    // Otherwise, I will find it when the user tries to print.
    var div_e = document.getElementById('option' + opt);
    div_e.parentNode.removeChild(div_e);

    // Hide the specified option. - removing option instead.
//    div_e.style.display = 'none';

    // Shrink the width of the div.
    var last_width = DivWidth;
    DivWidth -= OptionWidth;

    // Once I add the new option, the width will exceed the div width.
    var sub_width;
    if (last_width > 600) {
      if (DivWidth <= 600) {
        sub_width = last_width-600;
      }

      // Removing the option won't bring me under 600.
      else {
        sub_width = OptionWidth;
      }
      div_e = document.getElementById('main');
      div_e.style.width = div_e.offsetWidth-sub_width;
    }

    // Remove the option from the OptionsString.
    var opt_array = OptionsString.split(',' + opt)
    OptionsString = opt_array[0] + opt_array[1];
  }
}

// Callback function to create a printable version of the options.
//
function printOptions()
{
  // I have to split the options because I only can show 3 per page.
  // Use css to put a page break after each table of 3.
  // Loop over the options.  For each option, find all the inputs in
  // the form.  Match the input id/name to the td id is the printable
  // version.  Protect against option #'s that don't exist because
  // they have been removed.
}

// Help info specific to the loan compare tool.
//
ParamInfo['prog'] = "Choose the type of loan program you want to compare. The available programs depend on the transaction type.";
ParamInfo['cc_loan'] = "Choose 'Yes' if you want to include the closing costs in the new loan, which will increase your loan amount. Note that closing costs may be included up to the loan-to-value limit for the loan program.";
ParamInfo['payment'] = "This is the new monthly payment. It includes mortgage insurance, if applicable, but it does not include property taxes and insurance.";
ParamInfo['fc'] = "This is the amount of total finance charges you will pay during the years you plan to own the property. Finance charges include mortgage interest, mortgage insurance, and some closing costs.";
ParamInfo['eff_apr'] = "This is the effective annual percentage rate for the loan that takes into account when you plan to sell the property. If you plan to sell prior to end of the loan term, the 'effective APR' is greater than the 'APR' because the calculation must amortize the closing costs over a shorter period of time. If you have a 1st and 2nd lien, the effective APR is \"blended\" or averaged based on the relative loan amounts.";
ParamInfo['cash'] = "This is an estimate of the total amount due at closing. It includes your down payment and closing costs.";
ParamInfo['apr'] = "This is the effective interest rate the loan would have if you account for costs associated with securing the loan, such as closing costs and mortgage insurance. If you have a 1st and 2nd lien, the APR is \"blended\" or averaged based on the relative loan amounts. You can find more information about the APR and how it is calculated by searching for 'APR' in our <a class=\"navy\" target=\"_blank\" href=\"/cgi-bin/search/SearchLibrary.pl\">Library</a>.";

