var hqty = new Array ();  // handling qty breakpoints
var hamt = new Array ();  // amount charged
var hn   = 0;             // number of handling brkpts

var qqty = new Array ();  // quantity discount breakpoints
var qamt = new Array ();  // amount of discount
var qd   = 0;             // number of qty breakpoints

var sqty = new Array ();  // shipping qty breakpoints
var samt = new Array ();  // amount charged
var sn   = 0;             // number of shipping brkpts

var shpm = 1;             // shipping multiplier
var taxp = 0;             // tax percent for this order


function ClearAll () {    // put everything back
  shpm = 1;
  taxp = 0;
}

function Dollar (val) {   // force to valid dollar amount
var str,pos;
  str = escape (val*1.0 + 0.005001);  // float, round, back to char
  pos = str.indexOf (".");            // find decimal
  if (pos > 0) str = str.substring (0, pos + 3);  // lop
  return str;
}

function Process (obj1) {      // process the form and submit
var i,obj,bqty,bamt,tdis,thnd,tshp,ttax,val,pos,temp;
  obj1.item_name.value = obj1.basedesc.value;  // reload
  bamt = obj1.baseamt.value*1.0;
  bqty = obj1.quantity.value*1.0;     // selected quantity
temp = bamt;

  for (i=0; i<obj1.length; i++) {     // run whole form
    obj = obj1.elements[i];           // ref particular element
    if (obj.type == "select-one" &&   // dropdowns
        obj.name == "") {             // ignore named ones
      pos = obj.selectedIndex;        // which option selected
      val = obj.options[pos].value;   // get selection
      pos = val.indexOf ("@");        // is there a base price
      if (pos > 0) bamt = val.substring (pos + 1)*1.0;
      pos = val.indexOf ("+");        // a price adjustment?  
      if (pos > 0) bamt = bamt + val.substring (pos + 1)*1.0;
      obj1.item_name.value = obj1.item_name.value + ", " + val;
    } 
  }

  tdis = 0;                       // qty discounts
  for (i=qd-1; i>=0; i--) {       // run backwards
    if (bqty >= qqty[i]) {        // qty brkpt
      tdis = qamt[i]*1.0;
      bamt = bamt - tdis;         // Deduct from item price
      obj1.item_name.value = obj1.item_name.value + 
            ", QDIS=" + Dollar (tdis);
      break;                      // get out, now
    }
  }

  ttax = 0;
  if (taxp > 0 &&              // have a percent recorded?
      obj1.tax) {              // check for override
    ttax = bamt * taxp/100.0;  // yep - calculate it.
    obj1.item_name.value = obj1.item_name.value +
      ", TAX=" + taxp + "%";
  }
 
  thnd = 0;                  // handling charges
  for (i=hn-1; i>=0; i--) {  // run backwards
    if (bqty >= hqty[i]) {   // qty brkpt
      thnd = hamt[i];        // set hand amount
      obj1.handling.value = Dollar (thnd);
      break;                 // get out, now
    }
  }

  tshp = 0;                  // shipping charges
  for (i=sn-1; i>=0; i--) {  // run backwards
    if (bqty >= sqty[i]) {   // qty brkpt
      tshp = samt[i];        // set shipping amount
      obj1.shipping.value = Dollar (tshp * shpm);
      break;                 // get out, now
    }
  }

obj1.amount.value = temp;

if (obj1.quantity.value == "" || obj1.quantity.value == " " || obj1.quantity.value == "  ") { 
alert ("You forgot to enter how many you want."); return false; } 
if (obj1.quantity.value == 0) { alert ("It must be greater than zero."); return false; } 
if (!Number(obj1.quantity.value)) { alert ("You need to enter a number."); return false; } 
if (obj1.quantity.value.charAt(0) == '.' || obj1.quantity.value.charAt(1) == '.') { alert ("No decimal point allowed."); return false; }
if (obj1.quantity.value < 0) { alert ("You need to enter a positive number."); return false; }
if (obj1.quantity.value > 20) { alert ("Sorry. The limit is 20."); return false; }

// added code to eliminate zero quantity

}

function SetHN (q1, h1) {      // set qty handling breakpoints
var i;
  hn = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    hqty[hn] = arguments[i];   // qty breakpoint
    hamt[hn] = arguments[i+1]; // handling charge
    hn = hn + 1;               // number of bkpts
  }
}

function SetQD (q1, d1) {      // set qty discount breakpoints
var i;
  qd = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    qqty[qd] = arguments[i];   // qty breakpoint
    qamt[qd] = arguments[i+1]; // discount amount
    qd = qd + 1;               // number of bkpts
  }
}

function SetSH (q1, s1) {      // set shipping breakpoints
var i;
  sn = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    sqty[sn] = arguments[i];   // price breakpoint
    samt[sn] = arguments[i+1]; // shipping charge
    sn = sn + 1;               // number of bkpts
  }
}

function SetSM (obj1) {  // set shipping multiplier (from chart)
var pos;
  pos  = obj1.selectedIndex;           // which one selected
  shpm = obj1.options[pos].value*1.0;  // float it
}

function SetTX (obj1) {  // Set tax percent for this order
var pos;
  pos  = obj1.selectedIndex;           // which item selected
  taxp = obj1.options[pos].value*1.0;  // float it
}
