var blk1  = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart";
var blk1a = "&add=1";
var blk1d = "&display=1";
var blk1u = "&upload=1";
var blk2  = "&business=georgia%40bestloghomes.com";
var blk2a = "&quantity=";
var blk2q = 0;
var blk3  = "&item_name=";
var blk3n = "";
var bcode = "";
var blkcc = "&currency_code=USD";
var op1n  = "";  // option name and value
var op1v  = "";
var op2n  = "";
var op2v  = "";
var blk4  = "&amount=";
var blk4a = "6.66";
var blk6  = "&return=http://www.bestlogdecor.com/order-complete.htm";
var blk7  = "&cancel_return=http://www.bestlogdecor.com/order-cancelled.htm";
var winpar = "width=430,height=430,scrollbars," +
             "location,resizable,status";
var winpar2 = "width=700,height=500,scrollbars," +
             "location,resizable,status";			 

var cmax  = 12;            // max items in cart
var cats  = 3;             // max number of categories allowed
var pcat  = 0;             // item product category
var ctot  = new Object (); // amt & qty totals by pcat

var stax  = 0;      // regional (state) tax percent
var ttxt  = "";     // tax text.
var ttax  = 0;
var hand;           // cart totals
var ship;
var qtyd;
var tqty = 0;       // the total quantity of items ordered
var cook;           // place for item cookie object
var tprc = 0;       // total price of items in cart
var tozs = 0;       // total ounces of order
var bozs = 0;       // subtotal for item
var gtot;           // order grand total
var insx = 0;       // insurance total

var dqty = new Array ();  // item discount qty breakpoint
var damt = new Array ();  // percent discount
var dn   = 0;             // number of discount brkpts

var aqty = new Array ();  // item quantity amt breakpoint
var aamt = new Array ();  // price at breakpoint
var an   = 0;             // number of amt brkpts

var zn;                   // count of sip/zone pairs
var zzip = new Array ();  // zip code
var zone = new Array ();  // zone for that zip

var cdis = 0;             // amount of coupon discount
var coupval = "";         // coupon value user entered
var coupons = new Array ("coup1", "coup2");
var coup = -1;            // cart discount coupon not active

// stuff for shipping support...
var zip;        // shipping zip code
var wte;        // total weight of order
var zne = 0;    // shipping zone
var amt;        // priority shipping charge
var usps = -1;  // flag for USPS priority shipping
var ups  = -1;  // flag for UPS ground, lower 48
var insu = -1;  // flag for insurance
var itmw = 0;   // weight of single item (ozs)

// cart display options...
var on  = 1;       // on and off variables
var off = 0;
 

function AddDesc (strn) {  // add to current description
var c = "";
  if (blk3n.length > 0) c = ", ";
  blk3n = blk3n + escape (c + strn);
}

function AddOpt1 (val) {  // add to the value in op1v
var c = "";
  if (op1n.length == 0) op1n = "opt1";
  if (op1v.length > 0) c = ", ";
  op1v = op1v + escape (c + val);
}

function AddOpt2 (val) {  // add to the value in op2v
var c = "";
  if (op2n.length == 0) op2n = "opt2";
  if (op2v.length > 0) c = ", ";
  op2v = op2v + escape (c + val);
}

function AddPrcnt (strn) {    // add a percent to the price
  SetPrice (blk4a * (1.0 + strn/100.0));  // add the percent
}

function AddPrice (strn) {  // add to current price
  blk4a = Dollar (blk4a*1.0 + strn*1.0);
}

function CalcInsu (amt) {  // calculate insurance on an amount
//  it equals $2.20 + $1.00 per hundred
if (amt == 0) return 0;
if (amt <= 50) return 1.30;
return (2.20 + Math.floor (amt / 100.0));
}

function CallPay () { // call the PayPal shopping cart
var strn;
  strn = blk1 + blk1u + blk2 + blkcc + blk6 + blk7 + Xbild ();
  ClearAll ();
  window.open (strn, "paypal", winpar2);
}

function CallView () { // call the local shopping cart view
  window.open ("carty.htm", "local29", winpar);
}

function ChkCoup (amt) {  // check for a discount coupon
var i;
  coup = -1;          // assume the worst
  for (i=0; i<coupons.length; i++) {
    if (coupval == coupons[i]) {
      coup = 1;       // user hit the coupon value
      cdis = amt;     // remember the discount amt
      root.coup = coup;
      root.cdis = cdis;
      root.store ();
      alert ("Valid coupon number!");
      return;
    }
  }
  alert ("'" + val + "'  not a valid code!");
}

function ChkFlg (temp) {     // check for special flag char
var pos;
  pos  = temp.indexOf ("@"); // is there a initial value?  
  if (pos > 0) SetPrice (temp.substring (pos + 1));
  pos  = temp.indexOf ("+"); // is there a price adjustment?  
  if (pos > 0) AddPrice (temp.substring (pos + 1));
  pos  = temp.indexOf ("%"); // is there a percent adjustment?  
  if (pos > 0) AddPrcnt (temp.substring (pos + 1));
}

function ClearAll () {  // wipe out the last entry
  blk2q = "";  // quantity
  blk3n = "";  // name
  blk4a = "0"; // price
  bcode = "";  // item number
  op1n  = "";  // clear options
  op1v  = "";
  op2n  = "";
  op2v  = "";
  bozs  = 0;
  dn    = 0;
  an    = 0;
  pcat  = 0;
}

function ClearCart () {     // zap all the cookies
var i;
  for (i=1; i<cmax; i++) {  // the data cookies
    ClearEntry (i);
  }
}

function ClearEntry (i) {  // knock out a specific entry
  cook = new Cookie (document, "paycart" + i);
  InitData ();
}

function Cookie(document, name, hours, path, domain, secure) {
  this.$document = document;  // required
  this.$name = name;          // required
  if (hours) this.$expiration = 
      new Date((new Date()).getTime() + hours * 3600000);
  else this.$expiration = null;
//  if (path)   this.$path   = path;   else this.$path   = null;
this.$path = "/";  // force, for now
  if (domain) this.$domain = domain; else this.$domain = null;
  if (secure) this.$secure = true;   else this.$secure = false;
}

function _Cookie_store() {  // store method of cookie object
var cookieval = "";  // clear actual cookie value
  for(var prop in this) {  // Ignore "$" properties, and methods
    if ((prop.charAt(0) == '$') || 
        ((typeof this[prop]) == 'function')) 
        continue;
    if (cookieval != "") cookieval += '&';
    cookieval += prop + ':' + escape(this[prop]);
  }

  var cookie = this.$name + '=' + cookieval;
  if (this.$expiration)
      cookie += '; expires=' + this.$expiration.toGMTString();
  if (this.$path) cookie += '; path=' + this.$path;
  if (this.$domain) cookie += '; domain=' + this.$domain;
  if (this.$secure) cookie += '; secure';

  this.$document.cookie = cookie;  // store with magic property
}

function _Cookie_load() {  // cookie load function
var i;
var allcookies = this.$document.cookie;
  if (allcookies == "") return false;

  // Now extract just the named cookie from that list.
  var start = allcookies.indexOf(this.$name + '=');
  if (start == -1) return false;   // Cookie not defined for this page.
  start += this.$name.length + 1;  // Skip name and equals sign.
  var end = allcookies.indexOf(';', start);
  if (end == -1) end = allcookies.length;
  var cookieval = allcookies.substring(start, end);

  var a = cookieval.split('&');  // array of name/value pairs.
  for(i=0; i < a.length; i++)  // Break each pair into an array.
    a[i] = a[i].split(':');

  for(i=0; i<a.length; i++)
    this[a[i][0]] = unescape(a[i][1]);

  return true;     // We're done, so return the success code.
}

function _Cookie_remove() {  // the remove method
var cookie;
  cookie = this.$name + '=';
  if (this.$path) cookie += '; path=' + this.$path;
  if (this.$domain) cookie += '; domain=' + this.$domain;
  cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

  this.$document.cookie = cookie;  // magic store
}

function Dollar (val) {  // force to valid dollar amount
var str,pos,rnd=0;
  if (val < .995) rnd = 1;  // for old Netscape browsers
  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 InitData () {  // zap a data cookie

  cook.pcat = 0;
  cook.qty  = 0;
  cook.cod  = "";
  cook.des  = "";
  cook.op1n = "";  // option name and value
  cook.op1v = "";
  cook.op2n = "";
  cook.op2v = "";
  cook.prc  = 0;
  cook.ozs  = 0;
  cook.store();    // stash it off
}

function InitRoot () {  // Build the base root cookie
var i;
  root.stax = stax;     // stuff all base values
  root.ttxt = ttxt;
  root.coup = coup;
  root.cdis = cdis;
  root.usps = usps;     // USPS flag
  root.ups  = ups;      // UPS flag
  root.insu = insu;     // insurance flag
  root.zne  = zne;      // destination zone
  root.xx_tax  = on;    // init cart display options
  root.xx_ship = on;
  root.xx_sum  = on;
  root.xx_pc   = on;
  root.xx_xx   = on;
  root.xx_prt  = on;

  for (i=0; i<cats; i++) {
    root["hn" + i]   = 0;                // number of handling brkpts
    root["hqty" + i] = new Array ();  // cart hndl qty breakpoints
    root["hamt" + i] = new Array ();  // amount charged

    root["qd" + i]   = 0;              // number of qty breakpoints
    root["qqty" + i] = new Array ();  // cart QD breakpoints
    root["qamt" + i] = new Array ();  // amount of discount

    root["sn" + i]   = 0;              // number of shipping brkpts
    root["sqty" + i] = new Array ();  // cart ship qty breakpoints
    root["samt" + i] = new Array ();  // amount charged
    root["stxt" + i] = "";             // text describing shipping

    root["sv" + i]   = 0;              // number of bkpts
    root["sval" + i] = new Array ();  // cart ship val breakpoints
    root["schg" + i] = new Array ();  // amount charged
  }
  root.store();
}

function PrintCart () {  // print out entire cart
var i,wt,p,tmp1,tmp2,s;
  tprc = 0;                 // total price of cart
  tozs = 0;                 // total ounces of order
  gtot = 0;                 // Grand total
  ttax = 0;                 // total cart tax
  tqty = 0;                 // total qty of items
  insx = 0;                 // insurance amount

  for (i=0; i<cats; i++) {  // init pcat totals
    ctot["a" + i] = 0;      // amount in this cat
    ctot["q" + i] = 0;      // quantity in this cat
    ctot["h" + i] = 0;      // handling in this cat
    ctot["s" + i] = 0;      // shipping in this cat
    ctot["d" + i] = 0;      // discount in this cat
  }
  for (i=1; i<cmax; i++) {  // print all entries
    cook = new Cookie (document, "paycart" + i);
    cook.load();
    if (cook.qty > 0) {
      PrintItem (i);
    }
  }

  tprc = Dollar (tprc)*1.0;             // round, float
  ttax = Dollar (tprc * stax/100.0);    // do tax
  gtot = Dollar (tprc + ttax*1.0)*1.0;  // round, float

  hand = 0;  // line display totals
  ship = 0;
  qtyd = 0;   

  for (p=0; p<cats; p++) {                 // run product categories
    sh  = 0;                               // local pcat tots
    ha  = 0;
    qt  = 0;

    var q = ctot["q" + p]*1.0;             // quantity this catagory
    var a = ctot["a" + p]*1.0;             // amount this category
  
    tmp1 = root["hqty" + p].split(",");
    tmp2 = root["hamt" + p].split(",");
    for (i=root["hn" + p]-1; i>=0; i--) {  // run the table (if one)
      if (q >= tmp1[i]*1.0) {              // Check the price
        ha = tmp2[i]*1.0;                  // yep, set it
        break;                             // a-dios, mo-fo
      }
    }
    ctot["h" + p] = ha;
    gtot = gtot + ha;
    hand = hand + ha;

    tmp1 = root["sqty" + p].split(",");
    tmp2 = root["samt" + p].split(",");
    for (i=root["sn" + p]-1; i>=0; i--) {  // run the table (if one)
      if (q >= tmp1[i]*1.0) {              // Check the price
        sh = tmp2[i]*1.0;                  // yep, set it
        break;
      }
    }
    if (sh == 0) {                         // one or the other
     tmp1 = root["sval" + p].split(",");
     tmp2 = root["schg" + p].split(",");
     for (i=root["sv" + p]-1; i>=0; i--) {  // run the table (if one)
       if (a >= tmp1[i]*1.0) {  // Check the price
         sh = tmp2[i]*1.0;      // yep, set it
         break;
       }
     }
    }
    ctot["s" + p] = sh;
    gtot = gtot + sh;
    ship = ship + sh;

    tmp1 = root["qqty" + p].split(",");
    tmp2 = root["qamt" + p].split(",");
    for (i=root["qd" + p]-1; i>=0; i--) {
      if (q >= tmp1[i]*1.0) {
        qt = tmp2[i]*1.0;
        break;
      }
    }
    ctot["d" + p] = qt;
    gtot = gtot - qt;
    qtyd = qtyd + qt;
  }
  s = 0;  // for special stuff...
  wte = Math.floor ((tozs + 15.999) / 16.0);
  if (usps > 0) s = pzx[zne][wte];  // USPS shipping
  if (ups  > 0) s = uzx[zne][wte];  // UPS shipping
  ship = ship + s;  // only if wt shipping set
  if (insu > 0) insx = CalcInsu (tprc);

  gtot = Dollar (gtot + s + insx);  // round, and string
}

function PrintItem (i) {  // make one entry in cart
var tm,t1,t2,wt;
  document.writeln ('<tr valign = "top"');
  document.writeln ('    align = "right">');
  document.writeln ('  <td>');
  document.writeln ('    <input type  = "checkbox" ');
  document.writeln ('           value = "BOX1"');
  document.writeln ('           onclick = "ClearEntry (',
    i, ');');
  document.writeln ('                      location.reload ();" />');
  document.writeln ('  </td>');
  document.writeln ('  <td align = "right">',
    cook.qty,'</td>');
  if (root.xx_pc == on) {
    document.writeln ('  <td align = "right">',
      cook.pcat,'</td>');
  }
//  document.writeln ('  <td align = "left">',
//    cook.cod,'</td>');
  document.writeln ('  <td align = "left">',
    unescape (cook.des  + ' ' + 
              cook.op1n + ' ' + 
              cook.op1v + ' ' +
              cook.op2n + ' ' +
              cook.op2v),'</td>');
  tm = Dollar (cook.prc);
  t1 = cook.qty*1.0;
  t2 = cook.pcat;
  document.writeln ('  <td>',
    tm,'</td>');
  tm = Dollar (t1*cook.prc);
  tprc = tprc*1.0 + tm*1.0;
  tqty = tqty + t1;
  ctot["a" + t2] = ctot["a" + t2] + tm*1.0;
  ctot["q" + t2] = ctot["q" + t2] + t1;
  document.writeln ('  <td>',
    tm,'</td>');

  wt = cook.ozs*1.0 * cook.qty;
  tozs = tozs + wt;
  document.writeln ('  <td>',
    cook.ozs,'</td>');
  document.writeln ('    <td>',
    wt,'</td>');
  document.writeln ('</tr>');
}

function ReadForm (obj1) { //get form data for PayPal
var i,j,obj,temp,pos,val,nam3,nam4;
var qty  = 0;              // default value
var dis  = 0;              // quantity discount
  for (i=0; i<obj1.length; i++) {     // run whole form
    obj = obj1.elements[i];           // ref particular element
    nam3 = obj.name.substring (0, 3); // 3-char name (maybe)
    nam4 = obj.name.substring (3, 4); // where to store it
    if (obj.type == "select-one") {   // dropdowns
      pos = obj.selectedIndex;        // which option selected
      val = obj.options[pos].value;   // get selection
      ChkFlg (val);                   // check for flag char
      if (nam3 == "opt") {            // user says where to store
        Where (val, nam4);            // stash it
      } else {
        AddDesc (val);                // add to data
      }
    } 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;
          ChkFlg (val);                   // flag chars?
          if (nam3 == "opt") {            // user says where to store
            Where (val, nam4);            // stash it
          } else {
            AddDesc (val);                // add to data
          }
        }
      }
    } else
    if (obj.type == "checkbox" ||    // boxes
        obj.type == "radio") {
      if (obj.checked) {             // was selected
        val = obj.value;
        ChkFlg (val);                // flag chars?
        if (nam3 == "opt") {         // user says where to store
          Where (val, nam4);         // stash it
        } else {
          AddDesc (val);             // add to data
        }
      }
    } else
    if (obj.type == "text") {  // user input fields
      val = obj.value;         // get input
      if (qty == 0) {          // this is 1st time
        qty = val;             // get user input
        if (qty == "" || qty < 1 || isNaN (qty)) {  // test
          alert ("Enter a valid integer quantity!");
          return false;
        }
      } else {
        if (nam3 == "opt") { // user says where to store
          Where (val, nam4); // stash it
        } else {
          AddDesc (val);     // add to data
        }
      }
    }
  }
  if (qty == 0) qty = 1;     // make sure we have something
  blk2q = qty;               // record for posterity

  for (i=an-1; i>=0; i--) {  // qty amount?
    if (qty >= aqty[i]) {    // qty brkpt
      SetPrice (aamt[i]);    // force it
      AddDesc ("AMT");
      break;                 // get out, now
    }
  }

  dis = 0;                   // any qty discounts?
  for (i=dn-1; i>=0; i--) {  // run backwards
    if (qty >= dqty[i]) {    // qty brkpt
      dis = damt[i];         // set qty amount
      break;                 // get out, now
    }
  }
  if (coup > 0) {            // any valid coupons?
    dis = dis*1.0 + cdis*1.0;
    AddDesc ("COUP");        // mark it
  }
  if (dis > 0) {             // there is an item discount, here
    AddPrcnt (-dis);         // apply the discount
    AddDesc ("DIS=" + dis + "%");  // mark it
  }
  StoreCart ();
  return true;
}

function SetAmtSH (pcat, a1, c1) {   // set cart amt shp brkpts
var i,sv;
var sval = new Array ();
var schg = new Array ();
  if (pcat >= cats) {          // check him out
    alert ("SetAmtSH cat arg too big!");
    return;
  }
  sv = 0;                      // count of breakpoints
  for (i=1; i<arguments.length; i=i+2) {
    sval[sv] = arguments[i];   // price breakpoint
    schg[sv] = arguments[i+1]; // discount amount
    sv = sv + 1;               // number of bkpts
  }
  root["sv" + pcat]   = sv;    // stash that bad boy off
  root["sval" + pcat] = sval;
  root["schg" + pcat] = schg;
  root.store();                // remember forever...
}

function SetCartHN (pcat, q1, c1) {  // set cart hand breakpoints
var i,hn;
var hamt = new Array ();
var hqty = new Array ();
  if (pcat >= cats) {          // check him out
    alert ("SetCartHN cat arg too big!");
    return;
  }
  hn = 0;                      // count of breakpoints
  for (i=1; i<arguments.length; i=i+2) {
    hqty[hn] = arguments[i];   // price breakpoint
    hamt[hn] = arguments[i+1]; // discount amount
    hn = hn + 1;               // number of bkpts
  }
  root["hn" + pcat] = hn;      // stash that bad boy off
  root["hqty" + pcat] = hqty;
  root["hamt" + pcat] = hamt;
  root.store();                // remember forever...
}

function SetCartQD (pcat, q1, c1) {  // set cart qty dis breakpoints
var i,qd;
var qqty = new Array ();
var qamt = new Array ();
  if (pcat >= cats) {          // check him out
    alert ("SetCartQD cat arg too big!");
    return;
  }
  qd = 0;                      // count of breakpoints
  for (i=1; i<arguments.length; i=i+2) {
    qqty[qd] = arguments[i];   // price breakpoint
    qamt[qd] = arguments[i+1]; // discount amount
    qd = qd + 1;               // number of bkpts
  }
  root["qd" + pcat] = qd;      // stash that bad boy off
  root["qqty" + pcat] = qqty;
  root["qamt" + pcat] = qamt;
  root.store();                // remember forever...
}

function SetCartSH (pcat, q1, c1) {  // set cart shp qty breakpoints
var i,sn;
var sqty = new Array ();
var samt = new Array ();
  if (pcat >= cats) {          // check him out
    alert ("SetCartSH cat arg too big!");
    return;
  }
  sn = 0;                      // count of breakpoints
  for (i=1; i<arguments.length; i=i+2) {
    sqty[sn] = arguments[i];   // price breakpoint
    samt[sn] = arguments[i+1]; // handling charge
    sn = sn + 1;               // number of bkpts
  }
  root["sn" + pcat] = sn;      // stash that bad boy off
  root["sqty" + pcat] = sqty;
  root["samt" + pcat] = samt;
  root.store();                // remember forever...
}

function SetCartTX (obj1) {    // set cart tax value
var i,pos;
  pos  = obj1.selectedIndex;   // which item selected
  stax = obj1.options[pos].value;
  ttxt = obj1.options[pos].text;
  root.stax = stax;
  root.ttxt = ttxt;
  root.store();                // remember forever...
  document.location.reload ();  // show the latest info...
}

function SetCode (cd) { // set product code
  bcode = escape (cd);
}

function SetDesc (strn) {  // set the desc field
  blk3n = escape (strn);
}

function SetOpt1 (nam, val) {  // set the value of 1st option
  op1n = escape (nam);
  op1v = escape (val);
}

function SetOpt2 (nam, val) {  // set the value of 2nd option
  op2n = escape (nam);
  op2v = escape (val);
}

function SetPcat (val) {
  if (val >= cats) {
    alert ("Only " + cats + " product categories allowed!\n\n" +
            "Correct your HTML!");
    return;
  }
  pcat = val;
}

function SetPrice (strn) {  // set the current price
  blk4a = 0;
  AddPrice (strn);
}

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 SetQtyD (q1, d1) {    // set item qty discount breakpoints
var i;
  dn = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    dqty[dn] = arguments[i];   // quantity
    damt[dn] = arguments[i+1]; // percent
    dn = dn + 1;               // number of discount bkpts
  }
}

function SetWt (ozs) {  // set the weight of an item
  bozs = ozs;
}

function SetZone (zi1, zn1) {  // record zip/zone info into table
var i;
  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 StoreCart () {  // store data in local cart
var i;
  for (i=1; i<cmax; i++) {      //check for dup entry
    cook = new Cookie (document, "paycart" + i);  // run
    cook.load();                // get values
    if (cook.qty*1.0 > 0) {     // check it out - combine dups
      if (cook.cod == bcode &&  // check everything!!!
          cook.des == blk3n &&
          cook.prc*1.0 == blk4a*1.0 &&
          cook.op1n == op1n &&
          cook.op1v == op1v &&
          cook.op2n == op2n &&
          cook.op2v == op2v) {
        cook.qty = cook.qty*1.0 + blk2q*1.0;  // combine
        cook.store();
        ClearAll ();
        return;                 // th-th-thats all, folks
      }
    }
  }
  for (i=1; i<cmax; i++) {  // find empty entry
    cook = new Cookie (document, "paycart" + i);
    cook.load();
    if (cook.qty*1.0 == 0 ||
        isNaN (cook.qty)) {
      cook.pcat = pcat
      cook.qty  = blk2q;
      cook.cod  = bcode;
      cook.des  = blk3n;
      cook.op1n = op1n;
      cook.op1v = op1v;
      cook.op2n = op2n;
      cook.op2v = op2v;
      cook.prc  = blk4a;
      cook.ozs  = bozs;
      cook.store();
      ClearAll ();
      if (i == cmax - 1)
        alert ("Warning - that was your last cookie!");
      return;      
    }
  }
  alert ("Error 1 - out of cookies!");
}

function Where (val, loc) {  // store val at opt[loc]
  if (loc == 1) AddOpt1 (val);
  else          AddOpt2 (val);
}

function Xbild () {         // build the PayPal string
var i,j;
var tot  = 0;               // price total
var frst = 1;               //  1st time thru marker
var str  = "";              // string to PayPal
var disc = new Array ();    // price per item to subtract for discount
var tmps = root.stxt0+" "+ttxt;  // the shipping text (temp, for now)

  for (i=0; i<cats; i++) {  // set item discounts
    disc[i] = ctot["d" + i] / ctot["q" + i];  // discount per item
  }
  j = 0;       // starting suffix
  for (i=1; i<cmax; i++) {  // run all the data cookies
    cook = new Cookie (document, "paycart" + i);
    cook.load();         // get contents
    if (cook.qty > 0) {  // something here
      j = j + 1;         // bump suffix
      str = str + "&quantity_"    + j + "=" + cook.qty;
      str = str + "&item_name_"   + j + "=" + cook.des;
      str = str + "&amount_"      + j + "=" + 
                   Dollar (cook.prc*1.0 - disc[cook.pcat]);
      if (cook.op1n.length > 0) {    // 1st option present
        str = str + "&on0_"       + j + "=" + escape (cook.op1n);
        str = str + "&os0_"       + j + "=" + 
          escape (cook.op1v + ", " + tmps);
      } else if (tmps.length > 0 && ship > 0) {
        str = str + "&on0_"       + j + "=Shipping";
        str = str + "&os0_"       + j + "=" + escape (tmps);
      }
      tmps = "";  // zap it
      if (cook.op2n.length > 0) {    // 2nd option present
        str = str + "&on1_"       + j + "=" + escape (cook.op2n);
        str = str + "&os1_"       + j + "=" + escape (cook.op2v);
      }
      if (cook.cod != "")
        str = str + "&item_number_" + j + "=" + cook.cod;
      if (frst > 0 &&
          (hand*1.0 > 0 ||
          ttax*1.0 > 0 ||
          insx > 0)) {
        str = str + "&handling_"  + j + "=" + 
          Dollar (hand*1.0 + ttax*1.0 + insx*1.0);
      }
      if (frst > 0) {
        str = str + "&shipping_"  + j + "=" + Dollar (ship);
      } else {
        str = str + "&shipping_"  + j + "=0.00";
      }
      frst = 0;
    }    
  }
  return str;
}

// UPS residential 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 () {  // given weight and ZIP, calc cost
  wte = Math.floor ((tozs + 15.999) / 16.0);
  if (ups > 0) {    // user has selected UPS ground shipping
    SetUpsZ ();     // set up UPS ground shipping zones
  }
  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 (ups  > 0) amt = uzx[zne][wte]; // UPS charges     
  root.stxt0 = root.stxt0 + ", zone " + zne;
  root.zne = zne;
  root.usps = usps;
  root.ups  = ups;
  root.insu = insu;
  root.store ();
}

function SetUpsZ () {  // set UPS lower 48 ground Zones
  // built from UPS 35124 zone chart, lower 48
  SetZone (000, 5,
      17, 6,  27, 5,  30, 6,  34, 5,  35, 6, 
      36, 5,  37, 6,  51, 5,  54, 6,  57, 5, 
	  58, 6,  60, 5, 129, 6, 130, 5, 150, 4,  
	 157, 5, 160, 4, 161, 5, 162, 4, 163, 5, 
	 215, 4, 216, 5, 224, 4, 233, 5, 238, 4, 
	 255, 4, 279, 5, 280, 4, 287, 3, 290, 4, 
	 293, 3, 294, 4, 296, 3, 297, 4, 298, 3, 
	 299, 4, 300, 2, 304, 3, 307, 2, 308, 3, 
	 311, 2, 312, 3, 313, 4, 315, 3, 318, 2, 
	 320, 4, 323, 3, 326, 4, 330, 5, 334, 4, 
	 350, 2, 363, 3, 367, 2, 370, 3, 373, 2, 
	 375, 3, 384, 2, 385, 3, 388, 2, 389, 3, 
	 393, 2, 394, 3, 397, 2, 398, 3, 399, 2,  
	 300, 3, 403, 4, 407, 3, 410, 4, 417, 3, 
	 430, 4, 471, 3, 472, 4, 476, 3, 478, 4, 
	 484, 5, 490, 4, 493, 5, 500, 5, 522, 4, 
	 530, 5, 567, 6, 570, 5, 575, 6, 590, 7, 
	 592, 6, 594, 7, 600, 4, 629, 3, 630, 4, 
	 637, 3, 640, 4, 664, 5, 667, 4, 669, 5, 
	 673, 4, 674, 5, 693, 6, 700, 4, 704, 3, 
	 705, 4, 723, 3, 726, 4, 730, 5, 734, 4, 
	 735, 5, 740, 4, 746, 5, 747, 4, 763, 5, 
	 764, 4, 765, 5, 770, 4, 779, 5, 798, 6, 
	 800, 6, 821, 7, 822, 6, 832, 7, 838, 8,
	 840, 7, 845, 6, 846, 7, 855, 6, 860, 7, 
	 865, 6, 881, 5, 882, 6, 884, 5, 885, 6, 
	 889, 7, 894, 8, 898, 7, 900, 7, 930, 8, 
	 935, 7, 936, 8, 979, 7, 980, 8, 994, 7,
	 995, 8);
}

function Shipper (obj1) {  // use either amt or qty based shipping
var pos;
  SetAmtSH (0);  // assume the worst
  SetCartSH (0);
  pos = obj1.selectedIndex;
  root.stxt0 = obj1.options[pos].text;
  SetAmtSH (0,0,0);  // LEAVE ALONE!!
  if (pos == 1) {    //1st option
    usps = -1;
    ups  = 1;
    insu = 1;
    zip = prompt("Enter destination ZIP...", "").substring (0, 3);
    if (isNaN (zip)) {  // keep them honest
      alert ("You must enter a valid ZIP code!");
      return;
    }
    CalcCost ();
  } else
  if (pos == 2) {    // 2nd option
    usps = -1;
    usp  = 1;
    insu = -1;
    zip = prompt("Enter destination ZIP...", "").substring (0, 3);
    if (isNaN (zip)) {  // keep them honest
      alert ("You must enter a valid ZIP code!");
      return;
    }
    CalcCost ();
  }
  document.location.reload ();  // show the latest info...
}

// executed at load time
new Cookie (document, "xcxc");  // (backward compatibility)
Cookie.prototype.store  = _Cookie_store;   // load proto methods
Cookie.prototype.load   = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;

var root = new Cookie (document, "paycart0");  // special root cookie
  if (!root.load () ||
      !root.stax) {
    InitRoot ();                  // gotta create it
  } else {                        // load up globals
    stax  = root.stax;
    ttxt  = root.ttxt;
    coup  = root.coup;
    cdis  = root.cdis;
    usps  = root.usps;
    ups   = root.ups;
    insu  = root.insu;
    zne   = root.zne;
  }

for (var i=1; i<cmax; i++) {  // load or init data cookies
  cook = new Cookie (document, "paycart" + i);
  if (!cook.load () ||
      !cook.des) {
    InitData ();              // create it
  }
}

