﻿/*
- Function fcnAmountPerPeriod
-
- This function calculates the payment amount per period throughout a loan.
-
- Parameters:
- a_intInterestRate = The interest rate in decimal form.
- a_intCompoundingPeriods = The number of times interest is applied to the loan.
- a_intPrincipal = The initial amount of the loan.
- a_intTerms = The total amount of terms the loan is set for.
-
- Returns:
- intAmountPerPeriod = The amount per period.
*/
function fcnAmountPerPeriod(a_intInterestRate, a_intCompoundingPeriods, a_intPrincipal, a_intTerms) {
  //Declare all the variables used.
  var intAnnualInterestRate
  var intNominalInterestRate
  var intAmountPerPeriod
  var intTotalPeriods

  //Compute the Annual Interest Rate.
  intAnnualInterestRate = Math.pow((1 + a_intInterestRate / a_intCompoundingPeriods), a_intCompoundingPeriods) - 1;

  //Compute the Nominal Interest Rate.
  intNominalInterestRate = Math.pow((1 + intAnnualInterestRate), 1 / a_intCompoundingPeriods) - 1;

  //Compute the Amount Per Period.
  intAmountPerPeriod = a_intPrincipal * intNominalInterestRate * Math.pow((1 + intNominalInterestRate), a_intTerms) / (Math.pow((1 + intNominalInterestRate), a_intTerms) - 1);

  //Return the Monthly Payment.
  return (intAmountPerPeriod);
}

function DisplayRates() {
  if (ValidateMortgage()) {
    DisplayValues();
    ShowStep4();
    document['aspnetForm']['ctl00$cphMainContent$Calculated'].value = 1;
    document['aspnetForm']['ResetBaseline'].value = document['aspnetForm']['Baseline'].selectedIndex;
  }
  else {
    document['aspnetForm']['Baseline'].selectedIndex = document['aspnetForm']['ResetBaseline'].value;
  }
}

function DisplayRatesFirst() {
  if (ValidateMortgage()) {
      document['aspnetForm']['ctl00$cphMainContent$Calculated'].value = 1;
    this.form.submit();
  }
  else {
    document['aspnetForm']['Baseline'].selectedIndex = 0;
  }
}


function ShowStep4() {
  var isIE = document.all;
  var isIE6 = isIE && document.implementation;
  var isIE5 = isIE && window.print && !isIE6;
  var isIEDOM2 = isIE5 || isIE6;
  var isIE4 = isIE && !isIEDOM2 && navigator.cookieEnabled;
  var isIE4 = document.all && !(isIE5 || isIE6) && navigator.cookieEnabled;
  var isNS4 = document.layers;

  if (isIE4) {
    if (document.aspnetForm.MonthlyPmt5.value == "N/A (2)")
      document.all.savings.note2.display = "";
  }
  else if (isNS4) {

  }
  else {
    if (document.aspnetForm.MonthlyPmt5.value == "N/A (2)")
      document.getElementById("note2").style.display = "";
  }
}


/*
- Function DisplayValues()
-
- This function is called by the calculator to
- ** retrieve the Baseline Rate from memory
- ** calculate monthly payments for all FICO ranges
- ** calculate savings on monthly payments between baseline and other ranges
- ** calculate interest over life of loan for each FICO range
- ** calculate total amount saved over life of loan between baseline and other ranges
- ** display these calculations in the appropriate fields
*/
function DisplayValues() {

    var intCurrentLoan = document['aspnetForm']['ctl00$cphMainContent$ddlLoanTypes'].options.selectedIndex;
  var intCRange = document['aspnetForm']['Baseline'].options[document['aspnetForm']['Baseline'].options.selectedIndex].value;
  var intTRange;

  if (intCRange == "")
    intCRange = 5;

  for (intTRange = 0; intTRange < 6; intTRange++) {
    var dblCRate = aryRates[intCRange];
    var dblTRate = aryRates[intTRange];
    var intPrinc = document['aspnetForm']['ctl00_cphMainContent_txtLoanPrincipal'].value;
    var intMonths = aryRates[6];

    if (dblCRate > 0) //rate available
    {
      var intCPmt = fcnAmountPerPeriod(dblCRate / 100, 12, intPrinc, intMonths);
      var intInterestCurrent = (intCPmt * intMonths) - intPrinc;
    }
    if (intMonths == null) {
      document['aspnetForm']['MonthlyPmt' + intTRange].value = "N/A (2)";
      document['aspnetForm']['InterestOverLoan' + intTRange].value = "N/A (2)";
    }
    else {
      if (dblTRate <= 0) //rate unavailable
      {
        document['aspnetForm']['MonthlyPmt' + intTRange].value = "N/A (1)";
        document['aspnetForm']['InterestOverLoan' + intTRange].value = "N/A (1)";
      }
      else {
        var intTPmt = fcnAmountPerPeriod(dblTRate / 100, 12, intPrinc, intMonths);
        document['aspnetForm']['MonthlyPmt' + intTRange].value = "$" + formatCommas(Math.round(intTPmt), true, 2);
        var intInterestTarget = (intTPmt * intMonths) - intPrinc;
        document['aspnetForm']['InterestOverLoan' + intTRange].value = "$" + formatCommas(Math.round(intInterestTarget), true, 2);
      }
    }
    //alert(intSavingsOnMonthly);
    //if either rate is unavailable
    if (document['aspnetForm']['ctl00$cphMainContent$ddlRegion'].options[document['aspnetForm']['ctl00$cphMainContent$ddlRegion'].selectedIndex].value == "TX" && document['aspnetForm']['ctl00$cphMainContent$ddlLoanTypes'].options[document['aspnetForm']['ctl00$cphMainContent$ddlLoanTypes'].selectedIndex].value == "HELOC") {
      if (intCRange > intTRange) {
        document['aspnetForm']['SavingsOnMonthly' + intTRange].value = "This loan product is not offered in the chosen state. (3)";
      }
      else {
        if (intCRange < intTRange) {
          document['aspnetForm']['SavingsOnMonthly' + (intTRange - 1)].value = "This loan product is not offered in the chosen state. (3)";
        }
      }
    }
    else {
      if ((dblCRate <= 0) || (dblTRate <= 0)) {
        if (dblCRate <= 0) {
          if (dblTRate > 0) {
            document['aspnetForm']['SavingsOnMonthly' + intTRange].value = "If your score changes to " + document['aspnetForm']['range' + intTRange].value + ", you would get the loan";
          }
          else {
            if (intCRange > intTRange) {
              document['aspnetForm']['SavingsOnMonthly' + intTRange].value = "If your score changes to " + document['aspnetForm']['range' + intTRange].value + ", you would not get the loan (1)";
            }
            else {
              if (intCRange < intTRange) {
                document['aspnetForm']['SavingsOnMonthly' + (intTRange - 1)].value = "If your score changes to " + document['aspnetForm']['range' + intTRange].value + ", you would not get the loan (1)";
              }
            }
          }
        }
        else //dblTRate <= 0 and dblCRate > 0
        {
          if (intCRange > intTRange)
            document['aspnetForm']['SavingsOnMonthly' + intTRange].value = "If your score changes to " + document['aspnetForm']['range' + intTRange].value + ", you would not get the loan (1)";
          else
            document['aspnetForm']['SavingsOnMonthly' + (intTRange - 1)].value = "If your score changes to " + document['aspnetForm']['range' + intTRange].value + ", you would not get the loan (1)";
        }
      }
      else {
        if (intMonths == null) {
          if (intCRange > intTRange) {
            if (dblTRate > 0) {
              document['aspnetForm']['SavingsOnMonthly' + intTRange].value = "If your score changes to " + document['aspnetForm']['range' + intTRange].value + ", you would get the loan";
            }
            else {
              document['aspnetForm']['SavingsOnMonthly' + intTRange].value = "If your score changes to " + document['aspnetForm']['range' + intTRange].value + ", you would not get the loan (1)";
            }
          }
          else {
            if (intCRange < intTRange) {
              if (dblTRate > 0) {
                document['aspnetForm']['SavingsOnMonthly' + (intTRange - 1)].value = "If your score changes to " + document['aspnetForm']['range' + intTRange].value + ", you would get the loan";
              }
              else {
                document['aspnetForm']['SavingsOnMonthly' + (intTRange - 1)].value = "If your score changes to " + document['aspnetForm']['range' + intTRange].value + ", you would not get the loan (1)";
              }
            }
          }
        }
        else {
          var intSavingsOnMonthly = intCPmt - intTPmt;
          var intSavingsOverLoan = (intCPmt * intMonths) - (intTPmt * intMonths);
          if (intSavingsOnMonthly > 0) {
            document['aspnetForm']['SavingsOnMonthly' + intTRange].value = "If your score changes to " + document['aspnetForm']['range' + intTRange].value + ", you could save an extra $" + formatCommas(Math.abs(Math.round(intSavingsOverLoan)), true, 2);
          }
          else {
            if (intSavingsOnMonthly < 0) {
              document['aspnetForm']['SavingsOnMonthly' + (intTRange - 1)].value = "If your score changes to " + document['aspnetForm']['range' + intTRange].value + ", you could pay an extra $" + formatCommas(Math.abs(Math.round(intSavingsOverLoan)), true, 2);
            }
          }
        }
      }
    }

  }
}

// Function ValidateMortgage()
//
// This function is called every time the principal is changed, makes sure the principal is a numeric value
//
function ValidateMortgage() {
    mv = new String(document['aspnetForm']['ctl00_cphMainContent_txtLoanPrincipal'].value);
  var i = 0;

  //check to make sure the string isn't empty
  if (mv.length == 0) {
    alert("Please enter a valid Loan Principal.");
    return false;
  }

  //check for leading dollar sign ($)
  if (mv.charCodeAt(0) == 36)
    mv = mv.substr(1, mv.length - 1);

  //remove commas
  for (i = 0; i < mv.length; i++) {
    var intCode = mv.charCodeAt(i);
    if (intCode == 44) {
      if (i < mv.length - 1) {
        var temp1 = mv.substr(0, i);
        var temp2 = mv.substr(i + 1, mv.length - (i + 1));
        mv = temp1.concat(temp2);
        i--; //character has been removed, so next char is now in current position.
      }
      else //comma is at the end of the string
        mv = mv.substr(0, i);
    }
    else //make sure this char is a digit (charCodes 48-57)
    {
      if ((intCode < 48) || (intCode > 57)) {
        alert("Please enter a valid Loan Principal.");
        document['aspnetForm']['ctl00_cphMainContent_txtLoanPrincipal'].value = "";
        return false;
      }
    }
  }
  document['aspnetForm']['ctl00_cphMainContent_txtLoanPrincipal'].value = mv;
  return true;
}

//taken from http://www.tneoh.zoneit.com/javascript/js_func.html#formatDecimal 
function formatDecimal(argvalue, addzero, decimaln) {
  var numOfDecimal = (decimaln == null) ? 2 : decimaln;
  var number = 1;
  number = Math.pow(10, numOfDecimal);
  argvalue = Math.round(parseFloat(argvalue) * number) / number;
  argvalue = "" + argvalue;
  if (argvalue.indexOf(".") == 0)
    argvalue = "0" + argvalue;
  if (addzero == true) {
    if (argvalue.indexOf(".") == -1)
      argvalue = argvalue + ".";
    while ((argvalue.indexOf(".") + 1) > (argvalue.length - numOfDecimal))
      argvalue = argvalue + "0";
  }
  return argvalue;
}

function formatCommas(argvalue) {
  var strWholeNumPart = "" + argvalue;
  var strDecimalPart = "";
  var intDecimalLocation = strWholeNumPart.indexOf(".");
  var strSign = "";
  var i = 3;
  var temp1 = "";
  var temp2 = "";
  //if this is not a whole number, separate into parts, delimited by the decimal
  if (intDecimalLocation >= 0) {
    strDecimalPart = strWholeNumPart.substr(intDecimalLocation, (strWholeNumPart.length - intDecimalLocation));
    strWholeNumPart = strWholeNumPart.substr(0, intDecimalLocation);
  }
  //remove sign
  if ((strWholeNumPart.length > 0) && (strWholeNumPart.charAt(0) == '-')) {
    strSign = "-";
    strWholeNumPart = strWholeNumPart.substr(1, strWholeNumPart.length - 1);
  }
  //build new string with commas
  for (i = strWholeNumPart.length; i > 0; i -= 3) {
    if (i <= 3) {
      temp1 = strWholeNumPart.substr(0, i);
    }
    else {
      temp1 = "," + strWholeNumPart.substr(i - 3, 3);
    }
    temp2 = temp1 + temp2;
  }
  return strSign + temp2 + "" + strDecimalPart;
}
