title: How do I get this javascript to calculate the two listener fields as the price?
body: <p>I have two fields that have user entered data, RoomCount &amp; contractedNights. I can't figure out where I am going wrong. I have added my listeners, and tried a bunch of different means to get those to fields to multiply, and display the result with a minimum of factor of 60 (math.max60) in the calculatePrice function. Can someone please help me see/learn what I am doing wrong? THank you.</p>
  
  <pre><code>var listenerFieldIDs = {
    "roomCountID": "item4_text_1",
    "contractedNightsID": "item8_text_1"
  };
  
  var impactFields = ["item12_text_1", "item1_text_1", "item16_text_1", "item18_text_1", "item20_text_1"];
  var estimatedBottleSize = 1.5,
    occupancyRate = (60 / 100),
    collectionDuration = 365,
    soapOuncesRecoverable = 0.63,
    bottleOuncesRecoverable = 0.47,
    lbConversion = 0.0626;
  
  var $ = function (id) {
    return document.getElementById(id);
  }
  
  var updateFormField = function (id, amount) {
    $(id).value = amount;
  }
  
  var updateForm = function (roomCount, contractedNights) {
  
  
    updateFormField(impactFields[0], addCommas(calculateLbsOfSoap(roomCount).toFixed(2)));
    updateFormField(impactFields[1], addCommas(calculateLbsOfBottles(roomCount).toFixed(2)));
    updateFormField(impactFields[2], addCommas(calculateBarsOfSoap(roomCount).toFixed(0)));
    updateFormField(impactFields[3], addCommas(calculateBottles(roomCount).toFixed(0)));
    updateFormField(impactFields[4], ("$" + addCommas(calculatePrice(checkCount(roomCount), contractedNights).toFixed(2))));
  }
  
  var listenForNumbers = function (event) {
    updateForm($(listenerFieldIDs["roomCountID"]).value, $(listenerFieldIDs["contractedNightsID"]).value);
  }
  
  var calculateLbsOfSoap = function (rmCnt) {
    return checkCount(rmCnt) ? 0 : ((soapOuncesRecoverable * lbConversion) * (rmCnt * occupancyRate) * collectionDuration);
  }
  
  var calculateLbsOfBottles = function (rmCnt) {
    return checkCount(rmCnt) ? 0 : ((bottleOuncesRecoverable * lbConversion) * (rmCnt * occupancyRate) * collectionDuration);
  }
  
  var calculateBarsOfSoap = function (rmCnt) {
    return checkCount(rmCnt) ? 0 : ((calculateLbsOfSoap(rmCnt) * 16) / 3);
  }
  
  var calculateBottles = function (rmCnt) {
    return checkCount(rmCnt) ? 0 : (((calculateLbsOfBottles(rmCnt) * 16) / estimatedBottleSize) * (2 / 3));
  }
  
  var calculatePrice = function (rmCnt, contractedNights) {
    return checkCount(rmCnt, contractedNights) ? 0 : (calculatePrice(rmCnt) * (contractedNights));
  }
  
  
  var checkCount = function (count) {
    return (count &lt; 0 || count == 0) ? true : false;
  }
  
  var addCommas = function (num) {
  
    var result = "",
      numString = "",
      numOfDigits = 0;
  
    numString = num.toString();
    numOfDigits = numString.indexOf(".");
  
    if (numOfDigits == -1) {
      numOfDigits = numString.length;
    }
  
    if (numOfDigits &lt; 4) {
      result = num;
    } else {
  
      var extraDigits = numOfDigits % 3;
      var count = extraDigits;
  
      if (count != 0) {
        result = numString.substring(0, extraDigits) + ",";
      }
  
      while (count &lt; numOfDigits) {
        /             
              if(numOfDigits - count == 3){
                  break;
              }
  
              result = result + numString.substring(count,count + 3) + ",";
              count = count + 3;          }
  
          result = result + numString.substring(count,count + 3) + numString.substring(numOfDigits);
  
  }
  
  return result;
  }
  
  var stripCommas = function(formattedNumber){
  
  var strippedFormat = ""; 
  var pulledNumbers = formattedNumber.match(/ [0 - 9\.] / );
  
      for (var i = 0; i &lt; pulledNumbers.length; i++) {
        strippedFormat = strippedFormat + pulledNumbers[i];
      }
  
      return parseFloat(strippedFormat);
  
    }
  
    var initializeRealTimeCalcToForm = function () { //Used to load the needed functionality to the form.
  
  
      if (window.attachEvent) {
        $(listenerFieldIDs["roomCountID"]).attachEvent("onkeydown", listenForNumbers, false);
        $(listenerFieldIDs["roomCountID"]).attachEvent("onkeyup", listenForNumbers, false);
        $(listenerFieldIDs["roomCountID"]).attachEvent("onkeypress", listenForNumbers, false);
        $(listenerFieldIDs["roomCountID"]).attachEvent("onchange", listenForNumbers, false);
        $(listenerFieldIDs["contractedNightsID"]).attachEvent("onkeydown", listenForNumbers, false);
        $(listenerFieldIDs["contractedNightsID"]).attachEvent("onkeyup", listenForNumbers, false);
        $(listenerFieldIDs["contractedNightsID"]).attachEvent("onkeypress", listenForNumbers, false);
        $(listenerFieldIDs["contractedNightsID"]).attachEvent("onchange", listenForNumbers, false);
      } else {
  
        $(listenerFieldIDs["roomCountID"]).addEventListener("keydown", listenForNumbers, false);
        $(listenerFieldIDs["roomCountID"]).addEventListener("keyup", listenForNumbers, false);
        $(listenerFieldIDs["roomCountID"]).addEventListener("keypress", listenForNumbers, false);
        $(listenerFieldIDs["roomCountID"]).addEventListener("change", listenForNumbers, false);
        $(listenerFieldIDs["contractedNightsID"]).addEventListener("keydown", listenForNumbers, false);
        $(listenerFieldIDs["contractedNightsID"]).addEventListener("keyup", listenForNumbers, false);
        $(listenerFieldIDs["contractedNightsID"]).addEventListener("keypress", listenForNumbers, false);
        $(listenerFieldIDs["contractedNightsID"]).addEventListener("change", listenForNumbers, false);
      }
  
    }
  
    window.onload = function () { //This will kick start the process to install the functionality onto your form.
      initializeRealTimeCalcToForm();
    }
  </code></pre>