function checkit() {
  update();
  var txtw = "";
  var txtq = "";
  var txtp = 0;
  var x = 0;
  var z = 0;

  for (x=0;x<=productName.length-1;x=x+1) {
    if (itemNumber[x]=="heading") {
      z=z-1;
    } else {
      if(document.forms[0].productQty[z].value>0) {
        txtw = txtw + productName[x] + ",";
        txtq = txtq + document.forms[0].productQty[z].value + ",";
        txtp = txtp + priceWeb[x] + ",";
      }
    }
    z=z+1;
  }
  document.forms[0].wineSelected.value = txtw;
  document.forms[0].wineQty.value = txtq;
  document.forms[0].winePrice.value = txtp;
  var ok = true;
  var t = document.forms['myForm'].freight.selectedIndex;

  var oBottle = document.forms['myForm'].totalBottles.value;
  var oCase = (oBottle*1)/6;

  if (parseInt(oCase, 10) != oCase) {
    alert ("Wine must be purchased in multiples of 6 bottles, please go back and adjust your order!");
    ok = false;
    return;
  }

  if (document.forms['myForm'].freight[t].value==1) {
    alert ("Please select a Freight destination!");
    ok = false;
    document.forms[0].freight.focus();
    return;
  }
  if (document.forms[0].wineQty.value==0) {
    alert ("No products Selected!");
    ok = false;
    return;
  }
  if (ok) {
    document.forms[0].submit();
  }
}


function reset() {
  z=0;
  for (x=0;x<=productName.length-1;x=x+1) {
    if (itemNumber[x]=="heading") {
      z=z-1;
    } else {
      document.forms[0].productQty[z].value="";
    }
    z=z+1;
  }
  document.forms[0].totalBottles.value = "";
  document.forms[0].grandTotal.value = "";

  update();
}


function update(id) {
  var totBottles = 0;
  var totCost = 0;
  var x = 0 ;
  var z = 0 ;
  var numb = 0;
  var dc = 0 ;
  var r = 0;

  for (x=0;x<=productName.length-1;x=x+1) {
    if (itemNumber[x]=="heading") {
      z=z-1;
    } else {
      numb = parseInt(document.forms[0].productQty[z].value);
      multiplier = itemMultiplier[x];

      if (isNaN(numb)) {
        numb = 0;
      }
      if(numb>0) {
        totCost+=(priceWeb[x]*numb);
      }
      totBottles = totBottles + (numb * multiplier);
    }
    z=z+1;
  }
  // number of cases to apply discount
  if (totBottles>=500) {
    totCost = totCost * .95;
  }
  document.forms[0].totalBottles.value = totBottles;
  document.forms[0].grandTotal.value = formatnumb(totCost);

}


function formatnumb (mytotal) {
  tot = mytotal;
  var dot = String(tot).indexOf(".");
  if (dot==-1) {
    tot = tot + ".00";
  }
  if (dot>-1) {
    var y = (String(tot).length);
    var ss = String(tot).substring(dot+1,y);
    if (ss.length==1) {
      tot = tot + "0";
    }
    if (ss.length>2) {
      tot = String(tot).substring(0,dot+3);
    }
  }
  tot="$"+tot
  return(tot);
}
