
function getNumberValue(elemName, editedField) {
  var elemValue = document.getElementById(elemName).value;
  if (elemValue == "") {
    elemValue = 0;
  } else if (isNaN(elemValue)) {
    alert("Please enter a valid number for " + editedField);
    document.getElementById(elemName).value = "";
    return 0;
  }
  return elemValue;
}


function computeTotal(elemName1, elemName2, elemNameTotal, editedField) {
  var elemValue1 = getNumberValue(elemName1, editedField);
  var elemValue2 = getNumberValue(elemName2, editedField);

  document.getElementById(elemNameTotal).value = parseFloat(elemValue1) + parseFloat(elemValue2);
}

function computeTotals(editedField) {
  computeTotal("income", "income2", "income_total", editedField);
  computeTotal("overtime", "overtime2", "overtime_total", editedField);
  computeTotal("dividends", "dividends2", "dividends_total", editedField);
  computeTotal("other_income", "other_income2", "other_income_total", editedField);

  document.getElementById("total_monthly_income").value = 
    parseFloat(document.getElementById("income_total").value) +
    parseFloat(document.getElementById("overtime_total").value) +
    parseFloat(document.getElementById("dividends_total").value) +
    parseFloat(document.getElementById("other_income_total").value);

  computeTotal("car", "car2", "car_total", editedField);
  computeTotal("school", "school2", "school_total", editedField);
  computeTotal("credit", "credit2", "credit_total", editedField);
  computeTotal("other_payments", "other_payments2", "other_payments_total", editedField);

  document.getElementById("total_monthly_payments").value = 
    parseFloat(document.getElementById("car_total").value) +
    parseFloat(document.getElementById("school_total").value) +
    parseFloat(document.getElementById("credit_total").value) +
    parseFloat(document.getElementById("other_payments_total").value);

}

function computeHousingCosts() {
  var mortgageValue = getNumberValue("mortgage", "Mortgage payment");
  var taxesValue = getNumberValue("taxes",  "Real estate taxes");
  var insuranceValue = getNumberValue("insurance", "Insurance");

  document.getElementById("housing_total").value =
    parseFloat(mortgageValue) +
    parseFloat(taxesValue) +
    parseFloat(insuranceValue);
}

function calculateRatios() {
  var monthlyIncome = document.getElementById("total_monthly_income").value;
  if (monthlyIncome == "" || monthlyIncome == 0) {
    alert("Please enter your expected housing expenses.");
    return;
  }

  var expenseRatio = document.getElementById("housing_total").value / monthlyIncome;
  expenseRatio = Math.round(expenseRatio * 100);

  var debtRatio = document.getElementById("total_monthly_payments").value / monthlyIncome;
  debtRatio = Math.round(debtRatio * 100);

  document.getElementById("expense_ratio").innerHTML = 
    "<b>Your front-end ratio is <font color='red'>" + expenseRatio + "</font></b>";
  document.getElementById("debt_ratio").innerHTML =
    "<b>Your back-end ratio is <font color='red'>" + debtRatio + "</font></b>";
}

