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=support%40blaufuss.org";
var blk2a = "&quantity=";
var blk2q = 0;
var blk3  = "&item_name=";
var blk3n = "";
var bcode = "";
var btax  = "";
var blkcc = "&currency_code=USD";
var bhand = "";
var bozs  = 0;
var op1n  = "";  // option name and value
var op1v  = "";
var op2n  = "";
var op2v  = "";
var blk4  = "&amount=";
var blk4a = "6.66";
var winpar = "width=640,height=390,scrollbars," +
             "location,resizable,status";

var cmax = 10;      // max items in cart
var ozip = "90274"; // originating zip code
var stax = 0;       // regional (state) tax percent
var ttax = 0;       // total tax for page
var tqty;           // the total quantity of item ordered
var qtyd;           // amount of the discount
var cook;           // place to put cookie object
var tprc;           // total price of cart
var tlbs;           // total lbs of cart  
var tozs;           // total ozs of cart
var hndl;           // handling charges
var insr;           // insurance charges
var ship;           // shipping charges
var carr;           // carrier of shipment
var meth;           // method of shipment
var gtot;           // order grand total--actually just ship+hndl+insr+ttax

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

var sprc = new Array ();  // S/H price breakpoints
var samt = new Array ();  // amount charged
var sn   = 0;             // number of S&H brkpts

var qval = new Array ();  // QD breakpoints
var qamt = new Array ();  // amount of discount (changed to percent)
var qtyp = 0;             // updated percent discount
var qd   = 0;             // number of qty breakpoints

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

var selectedOptionValue = null;  // used in Changed fxn
var selectedOptionText = null;   // used in Changed fxn

function openWin(linkTo) {
 winOutside = window.open(linkTo,"Outside", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=450,height=550,left=10,top=10")
}
function openWin2(linkTo) {
 winOutside = window.open(linkTo,"Outside", "toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,width=640,height=550,left=10,top=10")
}

function populateForm() {
// used on a reload()
// use these variables to set form
	if (ship == 8.00) {     // Within USA--USPS Priority Mail: $8.00
	document.formCart.chooseShip.selectedIndex = "1"
	}
	if (ship == 25.00) {    // Within USA--FedEx Overnight: $25.00
	document.formCart.chooseShip.selectedIndex = "2"
	}
	if (ship == 45.00) {    // International--Airmail: $55.00
	document.formCart.chooseShip.selectedIndex = "3"
	}
	if (ship == 65.00) {    // International--Express: $65.00
	document.formCart.chooseShip.selectedIndex = "4"
	}
	if (stax == 0) {
	document.formCart.CAtax[1].checked = true;
	document.formCart.CAtax[0].checked = false;
	}
	if (stax == 8.25) {
	document.formCart.CAtax[1].checked = false;
	document.formCart.CAtax[0].checked = true;
	}
}

function Shipper (obj1) {  // use either amt or qty based shipping
var obj,pos;
  obj = obj1.elements[0];
  pos = obj.selectedIndex;
  root.stxt0 = obj.options[pos].text;
  SetAmtSH (0, 0, 0); // leave alone!!
  if (pos == 1) {  //1st option
    SetAmtSH (0, 1, 7);                    // user code  $7.00
  } else
  if (pos == 2) {  // 2nd option
    SetAmtSH (0, 1, 25);                   // user code $25.00
  } else
  if (pos == 3) {  // 3rd option
   SetAmtSH (0, 1, 55);                   // user code $55.00
  }
 if (pos == 4) {  // 4th option
    SetAmtSH (0, 1, 65);                   // user code $65.00
  }
  window.opener.document.location.reload ();
//  self.close ();
}

function AddDesc (strn) {  // add to current description
var c;
  if (blk3n.length > 0) c = ", "; else 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 = ", "; else c = "";
  op1v = op1v + escape (c + val);
}

function AddOpt2 (val) {  // add to the value in op2v
var c;
  if (op1n.length == 0) {  // fill 1st field 1st
    alert ("\n\n  Error - filling opt1 first!  \n\n");
    AddOpt1 (val);
    return;
  }
  if (op2n.length == 0) op2n = "opt2";
  if (op2v.length > 0) c = ", "; else 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
var pos;
  strn = escape (blk4a*1.0 + strn*1.0 + 0.005);
  pos = strn.indexOf ("."); // find decimal point
  if (pos > 0) strn = strn.substring (0, pos + 3);  // lop off extra
  blk4a = strn;             // stash new price
}

function CallPay () { // call the PayPal shopping cart
var strn;
  if (tprc == 0) {
  alert('You have no items in your shopping cart\n\nPress Continue Shopping to add an item');
  return;
  }
  if (ship == 0) {
  alert('You must first choose a shipping method');
  return;
  }
  strn = blk1 + blk1u + blk2 + blkcc + Xbild ();
  ClearAll ();
  window.open (strn, "paypal", winpar);
}

function CallView () { // call the local shopping cart view
  windowName = window.open ("cart.html", "local9", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=500,height=420,left=10,top=10");
  windowName.focus();
}

// function ChkCoup (amt) {  // check for a discount coupon
// var i;
//   coup = false;       // assume the worst
//   val = coupval.toLowerCase ();
//   for (i=0; i<coupons.length; i++) {
//     if (val == coupons[i]) {
//       coup = true;    // user hit the coupon value
//       cdis = amt;     // remember the discount amt
//       cook = new Cookie (document, "paycart0");  // record
//       cook.load ();
//       cook.coup = coup;
//       cook.cdis = cdis;
//       cook.store ();
//       alert ("\n\nValid coupon number  \n\n");
//       return;
//    }
//   }
//   alert ("\n\n'" + val + "'  not a valid code!  \n\n");
// }

function Changed(what) {
	selectedOptionValue = what.options[selectedIndex].value;
	selectedOptionText = what.options[selectedIndex].text;
}

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
  bhand = "0"; // item handling
  btax  = "0"; // item tax override
  bozs  = "0";
  op1n  = "";  // clear options
  op1v  = "";
  op2n  = "";
  op2v  = "";
  dn    = 0;
}

function ClearCart () {  // zap all the cookies
var i;
  cook = new Cookie (document, "paycart0");  // root cookie
  cook.load();
  InitRoot ();
  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);
  //alert ("paycart: " + i + "\n\n")
  InitData ();
  //
  //alert ("Totals: " + tprc + "\n\n")
}

function Cookie(document, name, hours, path, domain, secure) {
/* special cookie prototype functions
 document: The Document object that the cookie is stored for. Required.
 name:     A string that specifies a name for the cookie. Required.
 hours:    An optional number, the number of hours from now
            that the cookie should expire.
 path:     An optional string for the cookie path attribute.
 domain:   An optional string for the cookie domain attribute.
 secure:   An optional Bool, if true, requests a secure cookie.
*/
  this.$document = document;
  this.$name = name;
  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]);

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

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;
  str = escape (val*1.0 + 0.0050000001);
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (0, pos + 3);
  return str;
}

function DollCents (val) { // set the amount in hundredths only
var str,pos;
  str = escape (val*1.000000001);
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (0, pos + 3);
  return str;
}


function GoFedEx () {
var q1 = "http://www.fedex.com/cgi-bin/rate-tables";
var q2 = "?origin-country=United+States";
var q3 = "&origin-zip=";
var q4 = "&destination-zip=";
var q5 = "&submitregzip=View+by+Zone";
var dzip = "";
  dzip = prompt ("Enter destination ZIP code.", "");
  window.open (q1 + q2 + q3 + ozip + q4 + dzip + q5,
               "locfed9", winpar);
}

function GoUPS () {
var q1 = "http://wwwapps.ups.com/QCCWebApp/controller";
var q2 = "?action=packageinfo" +
         "&iso_language=en" +
         "&iso_country=US" +
         "&origcountry=US"; 
var q3 = "&origpostal=";
var q4 = "&destcountry=US" +
         "&destpostal=";
var q5 = "&destResidential=YES" +
         "&packages=1";
var dzip = "";
  dzip = prompt ("Enter destination ZIP code.", "");
  window.open (q1 + q2 + q3 + ozip + q4 + dzip + q5,
               "locups9", winpar);
}

function GoUSPS () {
var q1 = "http://postcalc.usps.gov/packagelanding.asp";
var q2 = "?MT=2";
var q3 = "&OZ=";
var q4 = "&DZ=";
var q5 = "&P=1&O=3&image1.x=22&image1.y=10";
var dzip = "";
  dzip = prompt ("Enter destination ZIP code.", "");
  window.open (q1 + q2 + q3 + ozip + q4 + dzip + q5,
               "locusps9", winpar);
}

function InitData () {  // zap a data cookie
  cook.qty = 0;
  cook.cod = "";
  cook.tax = "";
  cook.des = "";
  cook.op1n = "";  // option name and value
  cook.op1v = "";
  cook.op2n = "";
  cook.op2v = "";
  cook.prc = 0;
  cook.lbs = 0;
  cook.ozs = 0;
  cook.store();    // stash it off
}

function InitRoot () {  // zap the root cookie
  cook.hndl = "0";
  cook.insr = "0";
  cook.ship = "0";
  cook.stax = stax;
  cook.carr = "";
  cook.meth = "";
  cook.ozip = ozip;  // originating zip code
//  cook.coup = coup;
  cook.cdis = cdis;
  cook.sn   = sn;
  cook.sprc = sprc;
  cook.samt = samt;
  cook.qd   = qd;
  cook.qval = qval;
  cook.qamt = qamt;
  cook.store();
}

function MakeInt (val) {  // force to whole integer
var str,pos;
  str = escape (val);
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (0, pos);
  return str;
}

function PrintCart () {  // print out entire cart
var i,wt;
  tprc = 0;             // total price of cart--subtotal
  tlbs = 0;             // total lbs of cart  
  tozs = 0;             // total ozs of cart
  gtot = 0;             // Grand total
  ttax = 0;             // total cart tax
  tqty = 0;             // total qty of items

  ReadRoot ();
  for (i=1; i<cmax; i++) {
    cook = new Cookie (document, "paycart" + i);
    cook.load();
    if (cook.qty > 0) {
      PrintItem (i);
    }
  }

  tprc = Dollar (tprc);  // subtotal
  
  wt   = tlbs;
  tlbs = MakeInt (wt / 16.0);
  tozs = MakeInt (wt - (tlbs * 16));


  //  hndl = 1.50;                   // cart-wide s/h?
         
  for (i=sn-1; i>=0; i--) {        // run the table (if one)
    if (tprc*1.0 >= sprc[i]*1.0) { // Check the price
      if (tqty > 0) {
      }
      //hndl = samt[i];              // yep, set it
      break;                       // a-dios, mo-fo
    }
  }
  SetHndl (hndl);

  qtyd = 0;                          // cart-wide qty discount
  for (i=qd-1; i>=0; i--) {
    if (tqty >= qval[i]*1.0) {
      //qtyd = qamt[i];
      qtyp = qamt[i];                      // discount in percent
      qtyd = Dollar ((qtyp/100)*tprc);     // discount in dollars
      break;
    }
  }
  stax = cook.stax;
  ttax = Dollar ((tprc-qtyd) * stax/100.0);
  gtot = Dollar (tprc*1.0 + hndl*1.0 + insr*1.0 +
                 ship*1.0 + ttax*1.0 - qtyd*1.0);
}

function PrintItem (i) {  // make one entry in cart
var wt,lb,oz,tm;
  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>');
  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);
  document.writeln ('  <td>',
    tm,'</td>');
  tm = Dollar (cook.qty*cook.prc);
  tprc = tprc*1.0 + tm*1.0;
  tqty = tqty + cook.qty*1.0;
  document.writeln ('  <td>',
    tm,'</td>');
  //wt = (cook.lbs*16.0 + cook.ozs*1.0) * cook.qty;
  //lb = MakeInt (wt / 16);
  //oz = MakeInt (wt - (lb * 16));
  //tlbs = tlbs*1.0 + wt;
  //document.writeln ('  <td>',
  //  lb,'</td>');
  //document.writeln ('    <td>',
  //  oz,'</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
var acnt = 0;              // textarea count (for storage
  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) || (qty.indexOf (".")>0)) {  // test
          alert ("\n\n  Please use only integers to specify quantity!  \n\n");
          return;
        }
      } else {
        if (nam3 == "opt") {   // user says where to store
          Where (val, nam4);   // stash it
        } else {
          AddDesc (val);       // add to data
        }
      }
    } else
    if (obj.type == "textarea" &&  // textarea stuff (beware)
        obj.value.length > 0) {
      acnt = acnt + 1;            // areas encountered
      if (acnt > 2) {  // too many areas
        alert ("\n\n  Only 2 textareas may be input!  \n\n");
        return;
      }
      if (acnt == 1) {  // something there, check size
        if (obj.value.length + op1v.length > 200) {
          alert ("\n\n  Too many textarea1 characters!  \n\n");
          return;
        }
        if (op1n.length == 0) {     // nothing in 1st field
          SetOpt1 ("TextArea 1", obj.value);
        } else
          AddOpt1 (obj.value);
      } else
      if (acnt == 2) {
        if (obj.value.length + op2v.length > 200) {
          alert ("\n\n  Too many textarea2 characters!  \n\n");
          return;
        }
        if (op2n.length == 0) {     // nothing in second field
          SetOpt2 ("TextArea 2", obj.value);
        } else
          AddOpt2 (obj.value);
      }
    }
  }
  if (qty == 0) qty = 1; // make sure we have something
  blk2q = qty;           // record for posterity

  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) {                // 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 ("Discount=" + dis + "%.");  // mark it
  }
  StoreCart ();
}

function SetCarr (obj) {  // set carrier
var pick;
  cook = new Cookie (document, "paycart0");
  cook.load();
  pick = obj.selectedIndex;   // which option selected
  cook.carr = obj.options[pick].text;  // isolate it
  cook.store();
}

function SetCartQD (q1, c1) {  // set cart qty dis breakpoints
var i;
  cook = new Cookie (document, "paycart0");
  cook.load();
  qd = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    qval[qd] = arguments[i];   // price breakpoint
    qamt[qd] = arguments[i+1]; // discount amount
    qd = qd + 1;               // number of bkpts
  }
  cook.qd = qd;                // stash that bad boy off
  cook.qval = qval;
  cook.qamt = qamt;
  cook.store();                // remember forever...
}

function SetCartSH (q1, c1) {  // set cart S&H breakpoints
var i;
  cook = new Cookie (document, "paycart0");
  cook.load();
  sn = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) {
    sprc[sn] = arguments[i];   // price breakpoint
    samt[sn] = arguments[i+1]; // handling charge
    sn = sn + 1;               // number of bkpts
  }
  cook.sn = sn;                // stash that bad boy off
  cook.sprc = sprc;
  cook.samt = samt;
  cook.store();                // remember forever...
}

function SetCartTX (val) {    // set cart tax value
//var i,pos;
  cook = new Cookie (document, "paycart0");
  cook.load();
  //pos  = obj1.selectedIndex;   // which item selected
  //stax = obj1.options[pos].value;
  //cook.stax = stax;
  // add this line instead...
  cook.stax = val;
  cook.store();                // remember forever...
  //location.reload();
}

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

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

function SetHand (val) { // set handling charge per item
  bhand = Dollar (val);
}

function SetHndl (val) {  // set handling cart-wide
  cook = new Cookie (document, "paycart0");
  cook.load();
  cook.hndl = Dollar (val);
  cook.store();
}

function SetInsr (obj) {  // set insurance
  cook = new Cookie (document, "paycart0");
  cook.load();
  cook.insr = Dollar (obj.value);
  cook.store();
}

function SetMeth (obj) {      // set shipping method
var pick;
  pick = obj.options[obj.selectedIndex].text;  // isolate which option selected
  if (obj.options[obj.selectedIndex].text == "Choose shipping method") { //user has unset shipping
	alert('You must choose a shipping method');
	}
  cook = new Cookie (document, "paycart0");
  cook.load();
  cook.meth = pick;  // isolate which option selected
  cook.store();
}

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 SetPrice (strn) {  // set the current price
  blk4a = 0;
  AddPrice (strn);
}

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 SetShip (obj) {  // set shipping charges
  cook = new Cookie (document, "paycart0");
  cook.load();
  cook.ship = Dollar (obj.value);
  cook.store();
}

function SetTax (val) {  // tax override
  btax = Dollar (val);
}

function SetWt (lb, oz) {            // set item weight
  bozs = escape (lb*16.0 + oz*1.0);  // always save ounces
}

function StoreCart () {  // store data in local cart
var i,lb,oz;
  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.qty = blk2q;
      cook.cod = bcode;
      cook.hnd = bhand;
      cook.tax = btax;
      cook.des = blk3n;
      cook.op1n = op1n;
      cook.op1v = op1v;
      cook.op2n = op2n;
      cook.op2v = op2v;
      cook.prc = blk4a;
      lb = MakeInt (bozs*1.0 / 16.0);
      oz = MakeInt (bozs*1.0 - lb * 16.0);
      cook.lbs = lb;
      cook.ozs = oz;
      cook.store();
      ClearAll ();
      if (i == cmax - 1)
        alert ("\n Warning - your shopping cart is almost full! \n\n Please delete redudant items \n on your shopping cart. \n\n");
      return;      
    }
  }
  alert ("\n Your shopping cart is full! \n\n Please delete redundant \n items before proceeding.\n\n");
}

function Where (val, loc) {  // store val at opt[loc]
  if (loc < 1 || loc > 2) {
    alert ("\n\n  Your name is wrong - storing in OPT1!  \n\n");
    AddOpt1 (val);
    return;
  }
  if (loc == 1) AddOpt1 (val);
  else          AddOpt2 (val);
}

function Xbild () {  // build the PayPal string
var i,j;
var tot = 0;   // price total
var str = "";  // string to PayPal
// original code subtracts same discount from each item
// modified code will multiply discount to each item
// original code is here...
//var disc = 0;  // price per item to subtract for discount
//  if (qtyd*1.0 > 0) {  //Set to apply qty discounts
//   disc = qtyd/tqty;  // discount per item
//  }
// modified code is here...
 var qtypd = 1;  // fraction to multiply to price for discount (1 - fraction discount)
  if (qtyp*1.0 > 0) {  //Set to apply qty discounts
   qtypd = (1 - (qtyp/100));  // 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;
      // original code subtracted the same discount from each item...
      //str = str + "&amount_"      + j + "=" + 
      //             Dollar (cook.prc*1.0 - disc);
      // modified code multiplies a percentage discount to each item...
      str = str + "&amount_"      + j + "=" + 
                   Dollar (cook.prc*qtypd);
      if (cook.op1n.length > 0) {    // 1st option present
        str = str + "&on0_"       + j + "=" + cook.op1n;
        str = str + "&os0_"       + j + "=" + cook.op1v;
        if (cook.op2n.length > 0) {  // 2nd option present
          str = str + "&on1_"     + j + "=" + cook.op2n;
          str = str + "&os1_"     + j + "=" + cook.op2v;
        }
      }
      if (cook.cod != "")
        str = str + "&item_number_" + j + "=" + cook.cod;
      //if (cook.tax != "")
      //  str = str + "&tax_"         + j + "=" + cook.tax;
      //if (cook.hnd != "")
      //  str = str + "&handling_"    + j + "=" + cook.hnd;
    }    
  }
  gtot = Dollar (hndl*1.0 + insr*1.0 + ship*1.0 + ttax*1.0);
  if (gtot == 0) return str;  // nothing more to report

  j = j + 1;                  // more to report
  str = str + "&quantity_"    + j + "=1";
  str = str + "&item_name_"   + j + "=" + 
escape (
"Shipping: "    + ship +  // these are the add-ons
//" = "           + carr + 
" "             + meth +
//", Handling: "  + hndl +
//", Insurance: " + insr +
", Tax: "       + ttax);
  str = str + "&amount_"      + j + "=" + gtot;
  return str;
}

function ReadRoot () {
  cook = new Cookie (document, "paycart0");  // special root cookie
  if (!cook.load () ||
      !cook.ozip) {
    InitRoot ();                  // gotta create it
  } else {                        // load up globals
    hndl = cook.hndl;
    insr = cook.insr;
    ship = cook.ship;
    stax = cook.stax;
    carr = cook.carr;
    meth = cook.meth;
 //   coup = cook.coup;
    cdis = cook.cdis;
    sn   = cook.sn;
    sprc = cook.sprc.split(",");
    samt = cook.samt.split(",");
    qd   = cook.qd;
    qval = cook.qval.split(",");
    qamt = cook.qamt.split(",");
  }
}

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

ReadRoot ();
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
  }
}
