// Variable to define standard mortgage numbers.
//
var stdPropTax = 0.0175;
var stdIns = 0.0060;
var stdPurchAprCC = 1700.0;
var stdRefiAprCC = 1700.0;
var stdNonAprCC = 425.0;
var stdPrice = 417000.0;
var stdDP = 20;
var stdMaint = 0.025;
var stdRealtorComm = 0.06;
var stdFrontRatio = 0.31;
var stdBackRatio = 0.45;
var stdDiscountRate = 3.0;
var maxConvLoan = 417000.0;
var maxCashOutLTV = 0.8;
var titleDisc = new Array(0.4,0.4,0.35,0.3,0.25,0.2,0.15);

// Variable used for info popup.
var InfoPop = new PopupWindow('param_info', 'info_text');

// Callback function to display help info about parameters.
//
function infoCB(paramName, tab, colNum)
{
  // The anchor name for the popup uses the paramName.
  anchor = paramName + tab + '_anchor';

  // Set the offset based on the column in which the parameter appears.
  InfoPop.offsetX = (colNum == 2) ? -200 : -60;

  // The input is html for the param_info div.
  InfoPop.populate(ParamInfo[paramName]);
  InfoPop.showPopup(anchor);
}

// Callback function fired when user clicks Close button in info popup.
//
function closeInfoCB()
{
  InfoPop.hidePopup();
  return(false);
}

// Functions to calculate the down payment amount or percentage depending
// on the value of the other.
//
function calcDPPer(tab, secFlag)
{
  // Set the input parameters based on the existence of a second lien.
  var dp_amt_name = (secFlag) ? 'dp2$' : 'dp$';
  var dp_per_name = (secFlag) ? 'dp2%' : 'dp%';

  // Strip the price of any commas.
  price = stripCommas(document.getElementById('price' + tab).value);

  // Look for a down payment amount input.
  // If not found, look for a balance input.
  var dp_per;
  var e = document.getElementById(dp_amt_name + tab);
  if (! e) {
    var bal = stripCommas(document.getElementById('balance' + tab).value);

    if ((bal != '') && (price != '')) {
      document.getElementById(dp_per_name + tab).value = (100*(price-bal)/price).toFixed(2);
    }
  }
  else {
    // Strip the down payment amount of any commas.
    var dp = stripCommas(e.value);

    if ((dp != '') && (price != '')) {
      document.getElementById(dp_per_name + tab).value = (100*dp/price).toFixed(2);
    }
  }
}

function calcDPAmount(tab, secFlag)
{
  // Set the input parameters based on the existence of a second lien.
  var dp_amt_name = (secFlag) ? 'dp2$' : 'dp$';
  var dp_per_name = (secFlag) ? 'dp2%' : 'dp%';

  // Strip the price off any commas.
  price = stripCommas(document.getElementById('price' + tab).value);

  var dp = document.getElementById(dp_per_name + tab).value;
  if ((dp != '') && (price != '')) {
    document.getElementById(dp_amt_name + tab).value = addCommas(Math.round(dp*price/100));
  }
}

// Functions to bracket the value of the down payment.
// The percentage cannot be greater than 100%, and the amount cannot
// be greater than the price.
//
function validateDPAmount(e, tab)
{
  // Grab the value for use later.
  // ValidateNumber may add commas.
  var val = e.val/1;

  if (validateAmount(e, 0)) {
    var price = document.getElementById('price' + tab).value;
    if (val > price/1) {
      alert('The down payment cannot be greater than the price.');
      e.focus();
      e.select();
      return(false);
    }
  }
  return(true);
}

function validateDPPer(e, tab)
{
  if (validateNumber(e, -1)) {
    if (e.value/1 > 100) {
      alert('The down payment cannot be greater than than 100%.');
      e.focus();
      e.select();
      return(false);
    }
  }
  return(true);
}

// Helper function to reset the className of the result elements.
//
function changeResultClass(oldName, newName, parent)
{
  var parent_e = document.getElementById(parent);
  if (parent_e) {
    var e_array = getElementsByClass(oldName, parent_e, null);
    for (var i = 0; i < e_array.length; i ++) {
      e_array[i].className = newName;
    }
  }
}

// This is the help info.
//
var ParamInfo = new Array();
ParamInfo['price'] = 'This is the purchase price of the home.';
ParamInfo['amount'] = 'This is the 1st lien loan amount.';
ParamInfo['residency'] = 'Lenders assign greater risk to a mortgage on a home that you do not plan to use as a residence and, thus, charge a higher interest rate.';
ParamInfo['rate'] = 'This is the stated interest rate on the mortgage note.';
ParamInfo['term'] = 'This is the period of time after which the mortgage is due. For the purposes of this calculator, this also is the length of time over which the mortgage amortizes. Thus, the mortgage is paid off at the end of the term.';
ParamInfo['dp'] = "This is the part of the home's purchase price paid in cash up front, reducing the amount of your mortgage.";
ParamInfo['income'] = 'This is your yearly gross income before deductions (for taxes, benefit programs, etc.). If you are paid weekly, multiply the pay rate on your last pay stub by 52. If you are paid bi-weekly, multiple by 26. If you are paid monthly, multiple by 12. Include bonuses and commissions if you have received them for the last two years.';
ParamInfo['auto'] = 'This is the total of your monthly auto loan payments.';
ParamInfo['cc'] = 'This is the total of your monthly credit card payments. If you pay more than the minimum monthly payment on any of your credit cards, use the minimum monthly payment for those cards.';
ParamInfo['loan'] = 'This is the total of your monthly payments for any other loans. This could include student loans, other mortgages, loans against your retirement accounts, and personal loans.';
ParamInfo['balance'] = 'This is the balance on your existing mortgage.';
ParamInfo['value'] = 'This is the estimated value of your home.';
ParamInfo['old_payment'] = 'This is the current payment of the mortgage you are refinancing.';
ParamInfo['ti_flag'] = "Choose \"Yes\" if you currently pay your home's property taxes and insurance as part of your mortgage payment. You also will need to enter amounts for your property taxes and insurance.";
ParamInfo['close_year'] = "This is the year you closed the existing mortgage on your home.";
ParamInfo['prop_tax'] = "This is the yearly amount you pay for property taxes. If you don't know the amount and do not have a tax bill from a prior year, check your county appraisal district's Web site. Most allow you to search for a property's current tax information. As a last resort, estimate your taxes as " + (100*stdPropTax).toFixed(2) + "% of the property's value.";
ParamInfo['ins'] = "This is the yearly amount you pay for property insurance. If you don't know the amount, estimate it using " + (100*stdIns).toFixed(2) + "% of the property's value.";
ParamInfo['app_rate'] = "This is the yearly rate at which you expect the home to increase in value.";
ParamInfo['rent'] = "This is the amount you pay for rent.";
ParamInfo['inf_rate'] = "This is the yearly rate at which you expect your rent to increase.";
ParamInfo['inv_rate'] = "This is the yearly rate you expect to receive on investment savings.";
ParamInfo['inc_tax'] = "This is your income tax rate."
ParamInfo['move'] = "This is the number of years you expect to live at the same location (whether as a home owner or renter).";
ParamInfo['extra_month'] = "You will pay this additional amount each month, which will be applied against the outstanding principal balance of the loan.";
ParamInfo['extra_year'] = "You will pay this additional amount each year (every 12 months), which will be applied against the outstanding principal balance of the loan.";
ParamInfo['extra_one'] = "You will pay this additional amount once on the specified date, which will be applied against the outstanding principal balance of the loan.";
