var aqty = new Array ();  // amount qty breakpoint
var aamt = new Array ();  // amount to charge
var an   = 0;             // number of discount brkpts

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 stxt = "";            // shipping type text

var taxp = 0;             // tax percent for this order

var coup = -1;               // discount coupon not active
var cdis = 0;                // amount of coupon discount (%)
var coupons = new Array ();  // array of valid coupon names
coupons[0] = "coup1";        // list as many as you want...
coupons[1] = "coup2";

var on  = 1;
var off = 0;

var root = new Object ();
root.xx_ship = off;       // default is no shipping


function ChkCoup (val, amt) {  // check for a discount coupon
var i;
  coup = -1;              // assume the worst
  cdis = 0;
  if (amt == "" || isNaN (amt)) {
    alert ("\n\n\n   Coup arg not valid percent (01)  \n\n\n");
    return;
  }
  for (i=0; i<coupons.length; i++) {
    if (val == coupons[i]) {
      coup = 1;           // user hit the coupon value
      cdis = amt;         // remember the discount amt
      alert ("\n\n\n   Valid coupon code!  \n\n\n");
      return;
    }
  }
  alert ("\n\n\n  '" + val + "' not a valid code!  \n\n");
}

function ChkFlg (amt, temp) { // check for special flag char
var pos;
  pos  = temp.indexOf ("@");  // is there an initial value?  
  if (pos >= 0) amt = temp.substring (pos + 1)*1.0;
  pos  = temp.indexOf ("+");  // is there a price adjustment?  
  if (pos >= 0) amt = amt + temp.substring (pos + 1)*1.0;
  pos  = temp.indexOf ("%");  // is there a percent adjustment?  
  if (pos >= 0) amt = amt * (1.0 + temp.substring (pos + 1)/100.0);
  return amt;
}

function ClearAll () {
  sn = 0;  // reset shipping indicator
}

function Dollar (val) {  // force to valid dollar amount
var str,pos,rnd=0;
  if (val < 1) rnd = 1;
  str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;
}

function Process (obj1) {      // process the form, but no submit
var i,j,obj,bqty,bamt,btxt,tdis,thnd,tshp,ttax,val,pos;
  if (root.xx_ship == on) {  // is shipping on?
    if (sn == 0) {
      alert("Select shipping method!");
      return false;
    }
  }
  if (!obj1.quantity) {
    alert ("HTML must have quantity field!");
    return false;
  }
  if (!obj1.basedes) {
    alert ("HTML must have basedes field!");
    return false;
  }
  if (!obj1.baseamt) {
    alert ("HTML must have baseamt field!");
    return false;
  }
  btxt = obj1.basedes.value;          // reload desc
  bamt = obj1.baseamt.value*1.0;      //  and amount
  bqty = obj1.quantity.value;         // selected quantity
  if (isNaN(bqty) || bqty == "" || bqty < 1) {
    alert ("Quantity not valid - forcing to 1!");
    bqty = 1;
    obj1.quantity.value = "1";
  }
  bqty = bqty*1.0;                    // float that sucker

  for (i=0; i<obj1.length; i++) {     // run whole form
    obj = obj1.elements[i];           // ref particular element
    if (obj.name != "") continue;     // skip named elements
    if (obj.type == "select-one") {   // dropdowns
      pos = obj.selectedIndex;        // which option selected
      val = obj.options[pos].value;   // get selection
      bamt = ChkFlg (bamt, val);      // any flag chars
      btxt = btxt + ", " + val;
    } else
    if (obj.type == "select-multiple") { // one or more
      for (j=0; j<obj.options.length; j++) { // run all options
        if (obj.options[j].selected) {
          val = obj.options[j].value;
          bamt = ChkFlg (bamt, val);     // any flag chars
          btxt = btxt + ", " + val;
        }
      }
    } else
    if (obj.type == "checkbox") {     // checkboxes
      if (obj.checked) {              // just the selected ones
        val = obj.value;              // the value of it
        bamt = ChkFlg (bamt, val);    // any flag chars
        btxt = btxt + ", " + val;
      }
    } else
    if (obj.type == "text") {         // text
      if (obj.value.length > 0) {
        val = obj.value;              // the value of it
        btxt = btxt + ", " + val;
      }
    }
  }

  for (i=an-1; i>=0; i--) {       // qty amount?
    if (bqty >= aqty[i]) {        // qty brkpt
      bamt = aamt[i]*1.0;         // set amount from qty
      btxt = btxt + ", AMT=" + Dollar (bamt);
      break;                      // get out, now
    }
  }

  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
      btxt = btxt + ", QDIS=$" + Dollar (tdis);
      break;                      // get out, now
    }
  }

  if (coup > 0 &&                 // coupon discount percent?
      cdis > 0) {
    bamt = bamt * (1.0 - cdis/100.0);   
    btxt = btxt + ", COUP=" + cdis + "%";
  }

  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]*1.0;    // set shipping amount
      btxt = btxt + ", SHP:" + stxt;
      break;                 // get out, now
    }
  }

/* At this point everything has been calculated except tax.  
We need to figure out what we need to calculate tax on.  Handling 
is most often put into the tax calculation, but shipping is a toss-up.

In the following statement you may need to modify the Dollar amount
to include shipping - I have already included handling (thnd) - just
add tshp to include shipping in the tax calculation... */

  ttax = 0;
  if (taxp > 0 &&              // have a percent recorded?
      obj1.tax) {              // check for override
    ttax = taxp/100.0;         // yep - calculate it.
    obj1.tax.value = Dollar (ttax * bqty * (bamt + thnd));
  }

  obj1.shipping.value = Dollar (tshp);
  obj1.amount.value = Dollar (bamt);  // plug amount
  obj1.item_name.value = btxt;        // and descriptiuon
}

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 SetQA (q1, a1) {      // set qty amount breakpoints
var i;
  an = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    aqty[an] = arguments[i];   // quantity
    aamt[an] = arguments[i+1]; // amount
    an = an + 1;               // number of discount 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 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
}

function Shipper (obj1) {  // process user-selected shipping
var obj,pos;
  SetSH ();  // Don't delete this!  Shows bad selection was made.
  pos = obj1.selectedIndex;       // which item was selected?
  stxt = obj1.options[pos].text;  // user selection...
  switch (pos) {                  // item selected
    case 0:  // no selection made, return.
      break;
    case 1:  // West
      SetSH (1,2.00, 2,3.00, 5,4.00);  
      break;
    case 2:  // Mid
      SetSH (1,1.50, 2,1.90, 3,200, 5,2.50);
      break;
    case 3:  // East
      SetSH (1,2.00, 2,3.00, 5,4.00);  
      break;
    case 4:  // Can
      SetSH (1,3.00, 2,4.00, 5,7.00);
      break;
    case 5:  // Mex
      SetSH (1,2.50, 2,3.50, 5,6.00);
      break;
    default:
      SetSH (1, 99);  // outlandish, to detect.
      break;
  }
}