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 ix   = 0;             // count of insurance brkpts
var icst = new Array ();  // Cost of order
var irat = new Array ();  // insurance charges

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 zn = 0;               // ZIP, zone entries index
var zzip = new Array ();  // ZIP entry
var zone = new Array ();  // corresponding zone for ZIP

var taxp = 0;             // tax percent for this order

var coup = false;         // discount coupon not active
var cdis = 0;             // amount of coupon discount (%)
var coupons = new Array ();  // array of valid coupon names
coupons[0] = "easy_plan";     // list as many as you want...
coupons[1] = "easy_plus";
coupons[2] = "personal_plan";
coupons[3] = "personal_plus";

// stuff for shipping support...
var zip;      // shipping zip code
var wte;      // total weight of order
var zne;      // shipping zone
var amt;      // priority shipping charge
var usps;     // flag for USPS priority shipping
var ups;      // flag for UPS ground, lower 48
var insu;     // flag for insurance
var itmw = 0; // wright of single item (ozs)

// these are the priority mail zone charge tables (1-8) to 40lbs.
var pz1  = new Array (0,  // USPS priority zone one
  3.85,  3.95,  4.75,  5.30,  5.85,  6.30,  6.80,  7.35,  7.90,  8.40,
  8.95,  9.50, 10.00, 10.55, 11.05, 11.60, 12.15, 12.65, 13.20, 13.75,
 14.25, 14.80, 15.30, 15.85, 16.40, 16.90, 17.45, 18.00, 18.50, 19.05,
 19.55, 20.10, 20.65, 21.15, 21.70, 22.25, 22.75, 23.30, 23.75, 24.25);
var pz2  = new Array (0,
  3.85,  3.95,  4.75,  5.30,  5.85,  6.30,  6.80,  7.35,  7.90,  8.40,
  8.95,  9.50, 10.00, 10.55, 11.05, 11.60, 12.15, 12.65, 13.20, 13.75,
 14.25, 14.80, 15.30, 15.85, 16.40, 16.90, 17.45, 18.00, 18.50, 19.05,
 19.55, 20.10, 20.65, 21.15, 21.70, 22.25, 22.75, 23.30, 23.75, 24.25);
var pz3  = new Array (0,
  3.85,  3.95,  4.75,  5.30,  5.85,  6.30,  6.80,  7.35,  7.90,  8.40,
  8.95,  9.50, 10.00, 10.55, 11.05, 11.60, 12.15, 12.65, 13.20, 13.75,
 14.25, 14.80, 15.30, 15.85, 16.40, 16.90, 17.45, 18.00, 18.50, 19.05,
 19.55, 20.10, 20.65, 21.15, 21.70, 22.25, 22.75, 23.30, 23.75, 24.25);
var pz4  = new Array (0, 
  3.85,  4.55,  6.05,  7.05,  8.00,  8.85,  9.80, 10.75, 11.70, 12.60,
 13.35, 14.05, 14.75, 15.45, 16.20, 16.90, 17.60, 18.30, 19.00, 19.75,
 20.45, 21.15, 21.85, 22.55, 23.30, 24.00, 24.70, 25.40, 26.15, 26.85,
 27.55, 28.25, 28.95, 29.70, 30.40, 31.10, 31.95, 32.65, 33.50, 34.30);
var pz5  = new Array (0,
  3.85,  4.90,  6.85,  8.05,  9.30,  9.90, 10.65, 11.45, 12.20, 13.00,
 13.75, 14.50, 15.30, 16.05, 16.85, 17.60, 18.35, 19.30, 20.20, 21.15,
 22.05, 22.95, 23.90, 24.85, 25.75, 26.60, 27.55, 28.50, 29.45, 30.35,
 31.20, 32.15, 33.10, 34.00, 34.95, 35.85, 36.80, 37.70, 38.65, 39.60);
var pz6  = new Array (0,
  3.85,  5.05,  7.15,  8.50,  9.85, 10.05, 11.00, 11.95, 12.90, 14.00,
 15.15, 16.30, 17.50, 18.60, 19.75, 20.85, 22.05, 23.15, 24.30, 25.35,
 26.55, 27.65, 28.80, 29.90, 31.10, 32.25, 33.35, 34.50, 35.60, 36.80,
 37.85, 39.00, 40.10, 41.25, 42.40, 43.55, 44.65, 45.85, 47.00, 48.10);
var pz7  = new Array (0,
  3.85,  5.40,  7.85,  9.45, 11.00, 11.80, 12.55, 13.80, 15.05, 16.30,
 17.55, 18.80, 20.05, 21.25, 22.50, 23.75, 25.00, 26.25, 27.50, 28.75,
 30.00, 31.20, 32.45, 33.70, 34.95, 36.20, 37.45, 38.70, 39.95, 41.20,
 42.40, 43.65, 44.90, 46.15, 47.40, 48.65, 49.90, 51.15, 52.40, 53.60);
var pz8  = new Array (0,
  3.85,  5.75,  8.55, 10.35, 12.15, 13.30, 14.55, 15.75, 17.50, 19.20,
 20.90, 22.65, 24.35, 26.05, 27.80, 29.50, 31.20, 32.95, 34.65, 36.40,
 38.10, 39.80, 41.55, 43.25, 44.95, 46.70, 48.40, 50.15, 51.85, 53.55,
 55.30, 57.00, 58.70, 60.45, 62.15, 63.85, 65.60, 67.30, 69.05, 70.75);
var pzx  = new Array (0,pz1,pz2,pz3,pz4,pz5,pz6,pz7,pz8);

// UPS ground, lower 48 charges to 35 lbs...
var uz1  = new Array (0     // ain't no zone 1 in UPS residential
 );
var uz2  = new Array (0,
  5.90,  6.00,  6.15,  6.30,  6.55,  6.70,  6.90,  7.15,  7.35,  7.55,
  7.75,  7.95,  8.15,  8.30,  8.50,  8.60,  8.70,  8.85,  9.00,  9.20,
  9.40,  9.60,  9.75, 10.00, 10.20, 10.40, 10.60, 10.80, 11.05, 11.25,
 11.45, 11.65, 11.85, 12.05, 12.25);
var uz3  = new Array (0,
  6.05,  6.30,  6.55,  6.75,  6.95,  7.15,  7.30,  7.50,  7.70,  7.85,
  8.05,  8.25,  8.50,  8.70,  8.95,  9.15,  9.45,  9.70, 10.00, 10.30,
 10.55, 10.85, 11.15, 11.40, 11.65, 11.90, 12.15, 12.40, 12.65, 12.90,
 13.20, 13.45, 13.70, 14.00, 14.25);
var uz4  = new Array (0,
  6.40,  6.80,  7.15,  7.45,  7.75,  7.90,  8.05,  8.25,  8.40,  8.55,
  8.70,  8.85,  9.00,  9.15,  9.30,  9.50,  9.75, 10.05, 10.35, 10.70,
 11.00, 11.35, 11.70, 12.00, 12.35, 12.70, 13.00, 13.35, 13.70, 14.05,
 14.40, 14.75, 15.10, 15.45, 15.80);
var uz5  = new Array (0,
  6.50,  6.95,  7.40,  7.75,  8.10,  8.35,  8.60,  8.75,  8.95,  9.15,
  9.40,  9.60,  9.80, 10.00, 10.20, 10.45, 10.85, 11.35, 11.80, 12.30,
 12.75, 13.20, 13.65, 14.05, 14.45, 14.85, 15.25, 15.65, 16.10, 16.55,
 16.95, 17.40, 17.85, 18.25, 18.70);
var uz6  = new Array (0,
  6.80,  7.40,  7.85,  8.25,  8.55,  8.80,  9.10,  9.40,  9.60,  9.95,
 10.35, 10.75, 11.20, 11.75, 12.30, 12.85, 13.45, 14.00, 14.60, 15.20,
 15.80, 16.35, 16.95, 17.55, 18.15, 18.70, 19.30, 19.90, 20.50, 21.05,
 21.65, 22.20, 22.80, 23.35, 23.95);
var uz7  = new Array (0,
  6.85,  7.55,  8.05,  8.45,  8.90,  9.20,  9.50,  9.90, 10.40, 11.05,
 11.75, 12.50, 13.20, 13.90, 14.60, 15.30, 16.00, 16.65, 17.35, 18.05,
 18.70, 19.40, 20.05, 20.75, 21.45, 22.10, 22.75, 23.40, 24.10, 24.80,
 25.50, 26.25, 26.95, 27.70, 28.40);
var uz8 = new Array (0,
  7.00,  7.95,  8.70,  9.20,  9.70, 10.00, 10.40, 11.05, 11.70, 12.40,
 13.20, 14.10, 14.95, 15.80, 16.70, 17.50, 18.35, 19.25, 20.10, 21.00,
 21.85, 22.75, 23.60, 24.45, 25.35, 26.20, 27.00, 27.90, 28.75, 29.60,
 30.45, 31.30, 32.20, 33.05, 33.90);
var uzx  = new Array (0,uz1,uz2,uz3,uz4,uz5,uz6,uz7,uz8);


function CalcCost (obj1) {  // given weight and ZIP, calc cost
var str = "(error)";
  wte = Math.floor ((itmw * obj1.quantity.value + 15.999) / 16.0);
  zip = prompt("Enter destination ZIP code -", "");
  zip = zip.substring(0, 3);    // 1st 3 chars of zip
  if (usps > 0) {   // user selected USPS Priority shipping
    SetUspsZ ();    // set up USPS Priority mail zones
    str = "USPS Shipping data"
  } else
  if (ups > 0) {    // user has selected UPS ground shipping
    SetUpsZ ();     // set up UPS ground shipping zones
    str = "UPS lower-48 shipping data";
  }
  for (var i=zn-1; i>=0; i--) { // run table backwards
    if (zip >= zzip[i]) {       // zip value
      zne = zone[i];            // corresponding zone
      break;                    // get out, now
    }
  }
  if (usps > 0) amt = pzx[zne][wte]; // USPS charges to that zone
  if (ups > 0)  amt = uzx[zne][wte]; // UPS charges     
  alert (str + "\n\n\n" +
         "Weight = " + wte + " lbs.\n\n\n" +
         "ZIP    = " + zip + "\n\n\n" +
         "Zone   = " + zne + "\n\n\n" +
         "Charge = " + Dollar (amt) );
}

function ChkCoup (val, amt) {  // check for a discount coupon
var i;
  coup = false;           // 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 = true;        // user hit the coupon value
      cdis = amt;         // remember the discount amt
      
      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;
  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, but no submit
var i,j,obj,bqty,bamt,btxt,tdis,thnd,tshp,ttax,val,pos;
  amt = 0;  // shipping amt we must init to make sure it works
  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 ("\n\n\n  Quantity not valid - forcing to 1.  \n\n\n");
    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 &&                     // coupon discount percent?
      cdis > 0) {
    bamt = bamt * (1.0 - cdis/100.0);   
    btxt = btxt + ", COUP=" + cdis + "%";
  }

  ttax = 0;
  if (taxp > 0 &&              // have a percent recorded?
      obj1.tax) {              // check for override
    ttax = bamt * taxp/100.0;  // yep - calculate it.
    obj1.tax.value = Dollar (ttax * bqty);
  }
 
  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
      break;                 // get out, now
    }
  }

  if (usps > 0) {  // use USPS priority mail
    SetUspsZ ();   // force USPS zone chart
    CalcCost (obj1);
  } else
  if (ups > 0) {
    SetUpsZ ();    // force UPS zone chart5
    CalcCost (obj1);
  }

  if (insu > 0) {  // apply insurance
    for (i=0; i<ix; i++) {
      if (bamt * bqty >= icst[i]) {
        amt = amt + irat[i];
        alert ("Insurance = " + Dollar (irat[i]));
        break;
      }
    }
  }
  obj1.shipping.value = Dollar (tshp*1.0 + amt*1.0);
  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 SetIN (c1, r1) {      // set insurance rates
var i;
  ix = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    icst[ix] = arguments[i];   // order cost bkpt
    irat[ix] = arguments[i+1]; // insurance rate
    ix = ix + 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 SetUspsZ () {  // set USPS zone chart in memory
  // built from USPS 81503 zone chart
  SetZone (000, 8,
    005, 7, 006, 8, 068, 7, 129, 8, 130, 7,
    242, 6, 243, 7, 250, 6, 254, 7, 255, 6,
    258, 7, 261, 6, 262, 7, 287, 6, 290, 7,
    300, 6, 304, 7, 305, 6, 308, 7, 310, 6,
    313, 7, 317, 8, 320, 7, 324, 6, 326, 7,
    350, 6, 375, 5, 376, 6, 380, 5, 382, 6,
    386, 5, 388, 6, 439, 7, 440, 6, 444, 7,
    448, 6, 500, 5, 530, 6, 540, 5, 541, 6,
    546, 5, 549, 6, 550, 5, 575, 4, 576, 5,
    577, 4, 580, 5, 590, 4, 592, 5, 593, 4,
    594, 5, 597, 4, 598, 5, 600, 6, 610, 5,
    617, 6, 620, 5, 624, 6, 625, 5, 669, 4,
    673, 5, 674, 4, 680, 5, 683, 4, 700, 6,
    706, 5, 707, 7, 710, 5, 730, 4, 733, 5,
    735, 4, 740, 5, 746, 4, 747, 5, 763, 4,
    764, 5, 768, 4, 770, 5, 790, 4, 800, 3,
    808, 2, 814, 1, 816, 2, 820, 3, 821, 4,
    822, 3, 824, 4, 829, 3, 832, 4, 835, 5, 
    836, 4, 838, 5, 840, 3, 850, 4, 859, 3,
    863, 4, 865, 3, 874, 2, 875, 3, 879, 4,
    884, 3, 885, 4, 894, 5, 898, 4, 900, 5,
    922, 4, 926, 5, 935, 4, 936, 5, 967, 8,
    970, 5, 979, 4, 980, 5, 995, 8, 998, 7);

  SetIN (.01,  1.30,  50.01,  2.20, 100.01,  3.20,  200.01,  4.20, 
      300.01,  5.20, 400.01,  6.20, 500.01,  7.20,  600.01,  8.20,
      700.01,  9.20, 800.01, 10.20, 900.01, 11.20, 1000.01, 12.20
  );
}

function SetUpsZ () {  // set UPS lower 48 ground Zones
  // built from UPS 81503 zone chart, lower 48
  SetZone (000, 8,
    078, 7, 079, 8, 080, 7, 084, 8, 085, 7,
    087, 8, 109, 7, 110, 8, 127, 7, 128, 8,
    130, 7, 289, 6, 290, 7, 300, 6, 304, 7,
    307, 6, 308, 7, 311, 6, 312, 7, 325, 6,
    326, 7, 330, 8, 335, 7, 349, 8, 350, 6,
    363, 7, 364, 6, 376, 7, 377, 6, 398, 7,
    399, 6, 439, 7, 448, 6, 457, 7, 458, 6,
    500, 5, 530, 6, 538, 5, 539, 6, 540, 5,
    541, 6, 546, 5, 549, 6, 550, 5, 575, 4,
    576, 5, 577, 4, 580, 5, 586, 4, 587, 5,
    590, 4, 592, 5, 593, 4, 595, 5, 596, 4,
    599, 5, 600, 6, 612, 5, 613, 6, 614, 5,
    617, 6, 620, 5, 624, 6, 625, 5, 628, 6,
    630, 5, 637, 6, 640, 5, 669, 4, 670, 5,
    674, 4, 680, 5, 688, 4, 700, 6, 710, 5,
    712, 6, 716, 5, 723, 6, 724, 5, 736, 4,
    737, 5, 738, 4, 740, 5, 776, 6, 778, 5,
    783, 6, 786, 5, 790, 4, 795, 5, 798, 4,
    800, 3, 812, 2, 820, 3, 821, 4, 822, 3,
    824, 4, 825, 3, 827, 4, 829, 3, 833, 4,
    835, 5, 836, 4, 838, 5, 840, 3, 845, 2,
    846, 3, 850, 4, 860, 3, 863, 4, 865, 3,
    878, 4, 900, 5, 922, 4, 926, 5, 935, 4,
    936, 5, 961, 4, 962, 5, 979, 4, 980, 5,
    995, 8
  );
}

function SetWT (ozs) {  // set item weight in ounces
  itmw = ozs;
}

function SetZone () {  // record zip/zone info into table
  zn = 0;                      // count of breakpoints
  for (var i=0; i<arguments.length; i=i+2) {
    zzip[zn] = arguments[i];   // zip code index
    zone[zn] = arguments[i+1]; // zone
    zn = zn + 1;               // number of bkpts
  }
}

function Shipper (obj1) {  // select shipping
var obj,pos;
  SetSH ();   //Leave this!! (NO SELECTION)
  usps = -1;  // clear it out
  ups  = -1;
  insu = -1;
  obj = obj1.shp;                // user-defined select
  pos = obj.selectedIndex;       // item selected
  stxt = obj.options[pos].text;  // text of selected item
  obj1.os0.value = stxt;         // stuff it into option fld
  if (pos == 1) {     //1st option
    SetSH (0, 0);                           // user code
    usps = 1;
    ups  = -1;
    insu = 1;
  } else
  if (pos == 2) {     // 2nd option
    SetSH (0, 0);                           // user code
    usps = 1;
    ups  = -1;
    insu = -1;
  } else
  if (pos == 3) {     // 3rd position
    SetSH (0, 0);                            // user code
    ups  = 1;
    usps = -1;
    insu = -1;
  } else
  if (pos == 4) {     // 4th option
    usps = -1;                               // user code
    ups  = -1;
    insu = -1;
    SetSH (1, 21, 100, 12, 200, 5, 300, 0);
  }
}
