Order Form Calculations

function calculate(fld, price) { // fld will equal pw1 or pe1
     var dir = fld.name.charAt(1); // used to determine whether it is the East or West Form
     var num = fld.name.charAt(2); // what Item number is it
     var quant = fld.options[fld.selectedIndex].value; // how many of the items did the User choose
     var subtotal = eval(quant * price);
     // the eval converts a string to an Object property - in this case the value
     // dir = East or West & num = Item number
     // with this information we can post the result to appropriate Form &
     // to the appropriate Field on the Form
     eval('document.order.t' + dir + num).value = fix(subtotal);
     var total = 0;
     // this loop sums the totals for each of the Items to give us the Grand Total for that Form
     for (i = 1; i < 8; i++) {
          // does that particular Item have a "total" - in other words did the User choose this Item
          var itemTotal = eval('document.order.t' + dir + i).value;
          // total is a running sum of the Form's "subtotals"
          if (parseFloat(itemTotal) > 0) total += parseFloat(itemTotal);
     }
    // prior to sticking in the Grand Total into the Total Field we need to "dollarize" the number
     eval('document.order.total' + dir ).value = fix(total);
}

// a number like 6.6 should ultimately read as $6.60 and not $6.6
// a number like 6.6275 should ultimately read as $6.63
//
the fix(total) function takes care of these particular problems
function fix(total) { // ie, total == 6.6275
     var dollars = Math.floor(total); // dollars = 6

     // browsers sometimes have rounding errors - 662.75 - 600 = 62.75
     var cents = (total * 100) - (dollars * 100); 

     cents = Math.round(cents);  // 63

     if (cents < 10) cents = "0" + cents;
     // .998 will become 1.00 so we need to increment the dollar value by one
     if (cents == 100) dollars++;
     // if cents equal 0 then dollars equal total or total + 1 incase of .998
     if (dollars == total || dollars == Math.floor(total) + 1) cents = "00";

     total = dollars + "." + cents; // 6.63
     return total;
}