/* --------------------------------------------------- */
/* name:    order.js                                   */
/* purpose: order form calculations                    */
/* --------------------------------------------------- */
function itemTotal(i) { 
  var q = 0;
  var p = 0;
  var t = 0;
  with (document.orderForm) {
    q = eval("parseInt(qty" + i + ".value)");
    p = eval("parseFloat(price" + i + ".value,10)");
    t = q * p; 
    if (!isNaN(t)) { 
      eval("total" + i + ".value = t");
      eval("total" + i + ".value = formatCurrency(total" + i + ".value)");
    }
  }
  subTotal();
}
/* --------------------------------------------------- */
function subTotal() { 
  var n = 0;
  var t = 0;
  with (document.orderForm) {
    for (var i=1; i<=8; i++) {
      n = eval("parseFloat(total" + i + ".value,10)");
      if (!isNaN(n)) { t += n; }
    }
    subtotal.value = t;
    subtotal.value = formatCurrency(subtotal.value);
  }
  orderTotal();
}
/* --------------------------------------------------- */
function orderTotal() { 
  var n = 0;
  var t = 0;
  with (document.orderForm) {
    n = parseFloat(subtotal.value,10);
    if (!isNaN(n)) { t += n; }
    n = parseFloat(shipping.value,10);
    if (!isNaN(n)) { t += n; }
    n = parseFloat(insurance.value,10);
    if (!isNaN(n)) { t += n; }
    n = parseFloat(giftbox.value,10);
    if (!isNaN(n)) { t += n; }
    ordertotal.value = t;
    ordertotal.value = formatCurrency(ordertotal.value);
  }
}
/* --------------------------------------------------- */
function formatCurrency(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if (isNaN(num)) { num = "0"; }
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num % 100;
  num = Math.floor(num/100).toString();
  if (cents < 10) { cents = "0" + cents; }
  for (var i=0; i<Math.floor((num.length-(1+i))/3); i++) {
    num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
  }
  return (((sign)?'':'-') + num + '.' + cents);
}

