// Mortgage Calculator
function GetPayment() {
  var termMonths, monthlyInterest, loanAmount, payment;

  // Get input values
  termMonths = parseFloat(document.getElementById('numTerm').value);
  monthlyInterest = parseFloat(document.getElementById('numInterest').value);
  loanAmount = parseFloat(removeCurrency(document.getElementById('numAmt').value));

  // Convert inputted year into months
  termMonths = termMonths*12;

  // See if interest rate was in percent or decimal form, and calculate monthly interest accordingly.
  if (monthlyInterest > 1) {
    monthlyInterest = ((monthlyInterest)/100)/12;
  } else {
    monthlyInterest = (monthlyInterest)/12;
  }

  // Check required fields and numerics. If all good, calculate.
  if ( (!isNaN(termMonths)) && (!isNaN(monthlyInterest)) &&(!isNaN(loanAmount)) ) {
    payment = FormatCurrency( (loanAmount * Math.pow(1+monthlyInterest,termMonths) * monthlyInterest) / (Math.pow(1+monthlyInterest,termMonths) - 1) );
  } else {
    payment = "";
  }

  document.getElementById('numPayment').value = payment;
}


// Affordability Calculator
function GetAffordability() {
  // Get input values
  var numInterest = parseFloat(document.getElementById('numInterest').value);
  var numTerm = parseFloat(document.getElementById('numTerm').value);
  var numTaxes = parseFloat(removeCurrency(document.getElementById('numTaxes').value));
  var numInsurance = parseFloat(removeCurrency(document.getElementById('numInsurance').value));
  var numAssessments = parseFloat(removeCurrency(document.getElementById('numAssessments').value));
  var numIncome = parseFloat(removeCurrency(document.getElementById('numIncome').value));
  var numDebt = parseFloat(removeCurrency(document.getElementById('numDebt').value));

  // See if interest rate was in percent or decimal form, and get monthly interest
  var numMonthlyInterest = (numInterest > 1) ? ((numInterest/100)/12) : (numInterest/12);

  // Get monthly payment per $1,000 borrowed
  var numPayment;
  numPayment = (1000 * Math.pow(1+numMonthlyInterest,numTerm*12) * numMonthlyInterest) / (Math.pow(1+numMonthlyInterest,numTerm*12) - 1);

  // Check required fields and numerics. If all good, calculate.
  if ( (!isNaN(numInterest)) && (!isNaN(numTerm)) && (!isNaN(numTaxes)) && (!isNaN(numInsurance)) && (!isNaN(numIncome)) ) {

    // Make sure assessments and debt are numeric for calculations (zero out if necessary)
    numAssessments = (isNaN(numAssessments)) ? 0 : numAssessments;
    numDebt = (isNaN(numDebt)) ? 0 : numDebt;
    // Calculate monthly expenses (ASSUME $40/month PMI)
    var numExpenses = ((numTaxes + numInsurance)/12) + numAssessments + 40;

    // Calculate values for 33/38 ratio
    var num38Ratio = (((numIncome/12)*.38) - numDebt - numExpenses)*1000/numPayment;
    var num33Ratio = (((numIncome/12)*.33) - numExpenses)*1000/numPayment;
  
    // Determine which is lower, and assign to final field
    document.getElementById('numResult').value = (num38Ratio < num33Ratio) ? FormatCurrency2(num38Ratio) : FormatCurrency2(num33Ratio);

  } else {
    document.getElementById('numResult').value = "";
  }


}








// HELPER FUNCTIONS

function removeCurrency( strValue ) {
/************************************************
DESCRIPTION: Removes currency formatting from
  source string.

PARAMETERS:
  strValue - Source string from which currency formatting
     will be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /\(/;
  var strMinus = '';

  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}


function FormatCurrency(n) {
  pennies = n * 100;
  pennies = Math.round(pennies);
  strPennies = "" + pennies;
  len = strPennies.length;
  return "$" + strPennies.substring(0, len - 2) + "." + strPennies.substring(len - 2, len);
}

function FormatCurrency2(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
  num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
  cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  num = num.substring(0,num.length-(4*i+3))+','+
  num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + '$' + num + '.' + cents);
}


