View Single Post
Old 02-15-2013, 07:01 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Calculator</title>
    
<style type="text/css">
.required { 
    font-weight: bold;
    color : red;
}
.info {
    width: 30pt;
}
</style>
</head>
<body>
<div id="content">
<form id="EmpForm">
    <label>Number of Employees:&nbsp;&nbsp;&nbsp;
        <input class="info" name="NumEmps" value="10" />&nbsp;
        <span class="required">*</span>
    </label><br />
    <label>Hourly Rate:&nbsp;&nbsp;&nbsp;$
        <input class="info" name="Hourly"  value="10"/>&nbsp;
        <span class="required">*</span>
    </label><br />
    <label>Markup Percentage:&nbsp;&nbsp;&nbsp;
        <input class="info" name="Markup" value="30"/>%&nbsp;
        <span class="required">*</span>
    </label><br />
    <label>Hours Per Week:&nbsp;&nbsp;&nbsp;
        <input class="info" name="Hours"  value="40" />
        <span class="required">*</span>
    </label><br />
    <br/>
    <label>Your Weekly Profit:&nbsp;&nbsp;&nbsp;$
        <input name="Profit" readonly="readonly" />
    </label><br />
</form>
</div>

<script type="text/javascript">
(
  function( )
  {
      var form = document.getElementById("EmpForm");
      var inps = form.getElementsByTagName("input");
      for ( var i = 0; i < inps.length; ++i )    
      {
          if ( inps[i].className == "info" )
          {
              inps[i].onchange = recalculate;
          }
      }

      function recalculate( )
      {
          var allOK = true;
          var result = 1;
          for ( var i = 0; i < inps.length; ++i )    
          {
              if ( inps[i].className == "info" )
              {
                  var inp = inps[i];
                  var val = inp.value.replace(/\s/g,"");
                  if ( val === "" || isNaN(val) )
                  {
                      allOK = false;
                  } else {
                      val = Number(val);
                      if ( inp.name == "Markup" ) { val = val * 0.01; }
                      result *= val;
                  }
              }
          }
          form.Profit.value = ( allOK ? result.toFixed(2) : "n/a" );
      }
      recalculate( );
  }
)();
</script>
</body>
</html>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
AlanP (02-15-2013)