title: Stuck on a simple calculation with JavaScript
body: <p>I (total Javascript Noob) am trying to write a javascript to create a weight and balance calculator for general aviation aircraft.</p>
  
  <p>However I am getting stuck along the way.
  The user is supposed to enter weight information and the calculator should then calculate the airplanes Moment and or weight as well as sum up the corresponding fields.</p>
  
  <p>Unfortunately I can't get the script to calculate the users inputs. Does anyone have a suggestion?</p>
  
  <p>My HTML is: (JS further down below)</p>
  
  <pre><code>&lt;form id="form" onsubmit="return false;"&gt;
  &lt;table&gt;
      &lt;tr&gt;
          &lt;th&gt;Station&lt;/th&gt;
          &lt;th&gt;Weight&lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
          &lt;th&gt;Arm&lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
          &lt;th&gt;Moment&lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Empty Weight&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(weight);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;kg&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(acarm);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;kg-m&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(moment);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;m&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Pilot&lt;/td&gt;
          &lt;td&gt;
              &lt;input type="text" value="0" onkeypress="return isNumberKey(event)" onblur="onBlur(this)" onfocus="onFocus(this)" id="Pilot" /&gt;
          &lt;/td&gt;
          &lt;td&gt;kg&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(afseat);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;kg-m&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(pimom);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;m&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;CoPilot&lt;/td&gt;
          &lt;td&gt;
              &lt;input type="text" value="0" onkeypress="return isNumberKey(event)" onblur="onBlur(this)" onfocus="onFocus(this)" id="CoPilot" /&gt;
          &lt;/td&gt;
          &lt;td&gt;kg&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(afseat);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;kg-m&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(cpmom);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;m&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Pax 1&lt;/td&gt;
          &lt;td&gt;
              &lt;input type="text" value="0" onkeypress="return isNumberKey(event)" onblur="onBlur(this)" onfocus="onFocus(this)" id="Pax1" /&gt;
          &lt;/td&gt;
          &lt;td&gt;kg&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(arseat);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;kg-m&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(p1mom);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;m&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Pax 2&lt;/td&gt;
          &lt;td&gt;
              &lt;input type="text" value="0" onkeypress="return isNumberKey(event)" onblur="onBlur(this)" onfocus="onFocus(this)" id="Pax2" /&gt;
          &lt;/td&gt;
          &lt;td&gt;kg&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(arseat);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;kg-m&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(p2mom);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;m&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Fuel&lt;/td&gt;
          &lt;td&gt;
              &lt;input type="text" value="0" onkeypress="return isNumberKey(event)" onblur="onBlur(this)" onfocus="onFocus(this)" id="Fuel" /&gt;
          &lt;/td&gt;
          &lt;td&gt;l&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(abag);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;kg-m&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(bamom);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;m&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Baggage&lt;/td&gt;
          &lt;td&gt;
              &lt;input type="text" value="0" onkeypress="return isNumberKey(event)" onblur="onBlur(this)" onfocus="onFocus(this)" id="Baggage" /&gt;
          &lt;/td&gt;
          &lt;td&gt;kg&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(abag);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;kg-m&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(bamom);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;m&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Total&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(totweight);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;kg&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(totarm);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;td&gt;kg-m&lt;/td&gt;
          &lt;td&gt;
              &lt;script type="text/javascript"&gt;
                  document.write(totmom);
              &lt;/script&gt;
          &lt;/td&gt;
          &lt;http://jsfiddle.net/#savetd&gt;m&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;/td&gt;
          &lt;td&gt;
              &lt;input type="submit" onclick="weightandbalance();" /&gt;
          &lt;/td&gt;
          &lt;td&gt;&lt;/td&gt;
          &lt;td&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p></p>
  
  <p>And here is the JS:</p>
  
  <pre><code>     // Basic Aircraft Set Up
      var weight = 627.9; // Basic Empty Weight in kg
      var moment = 201.22; // Moment in m-kg
      var acarm = Math.round((moment / weight) * 10000) / 10000;
  
      var mtow = 1002; // Maximum Take Off Weight in kg
      var afseat = 0.41; // Front Seat Arm in m
      var arseat = 1.19; // Rear Seat Arm in m
      var abag = 1.9; // Baggage Area Arm in m
      var afuel = 1.12; // Fuel Tank Arm in m
  
  
  function weightandbalance() {
  
      var Pilot = parseFloat(document.getElementById("Pilot").value);
      var CoPilot = document.getElementById("CoPilot").value;
      var Pax1 = document.getElementById("Pax1").value;
      var Pax2 = document.getElementById("Pax2").value;
      var Baggage = document.getElementById("Baggage").value;
      var Fuel = document.getElementById("Fuel").value;
  
      // Perform Calculations
      var pimom = Pilot * afseat;
      var cpmom = CoPilot.value * afseat;
      var p1mom = Pax1.value * arseat;
      var p2mom = Pax2.value * arseat;
      var bamom = Baggage.value * abag;
      var fumom = Fuel.value * afuel * 0.71;
  
      var totweight = weight + Pilot + CoPilot + Pax1 + Pax2 + Baggage + Fuel;
      var totmom = moment + pimom + cpmom + p1mom + p2mom + bamom + fumom;
      var totarm = Math.round((totmom / weight) * 10000) / 10000;
  
  
  
  }
  
  // Allow only numbers
  function isNumberKey(evt) {
      var charCode = (evt.which) ? evt.which : event.keyCode;
      if (charCode != 46 &amp;&amp; charCode &gt; 31 &amp;&amp; (charCode &lt; 48 || charCode &gt; 57)) return false;
  
      return true;
  }
  
  
  //Default Values    
  function onBlur(el) {
      if (el.value == '') {
          el.value = el.defaultValue;
      }
  }
  
  function onFocus(el) {
      if (el.value == el.defaultValue) {
          el.value = '';
      }
  }
  </code></pre>