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=";
var blk2a = "&quantity=";
var blk2q = 0;
var blk3  = "&item_name=";
var blk3n = "";
var bcode = "";
var blkcc = "";
var blkhn = "";  // item handling charges
var blksh = "";  // item shipping charges
var bmisc = "";  // stuff not supported anywhere else
var op1n  = "";  // option name and value
var op1v  = "";
var op2n  = "";
var op2v  = "";
var blk4  = "&amount=";
var blk4a = "6.66";
var imgx  = "dummy.jpg";  // place for cart thumbnail image
var winpar = "scrollbars,location,resizable,status,menubar,toolbar";
var cartwin;

var cmax  = 15;            // max items in cart
var cats  = 5;             // 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 thnd  = 0;      // item specific handling
var tshp  = 0;      // item specific shipping
var hand;           // cart totals
var ship;
var tpkg;
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 ader = 0;       // add a flat cost to an item
var adtx = "";      // text for flat add

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 ();  // amount breakpoint
var aamt = new Array ();  // price at breakpoint
var an   = 0;             // number of amt brkpts

var iqty = 0;             // minimum item quantity to order
var xqty = 1000000;       // maximum item quantity to order

var cdis = 0;             // percent coupon discount
var camt = 0;             // amount coupon discount
var coupval = "";         // coupon value user entered
var coupons = new Array ();  // coupon codes
var couponp = new Array ();  // percent discount from coupon
var coupona = new Array ();  // amount discount from coupon
var cval = "";            // recorded coupon value
var ctxt = "";            // coupon text
var coup = -1;            // cart discount coupon not active

var insu = -1;  // flag for insurance

var gwtot = 0;            // Gift wrapping total

// cart display options...
var on  = 1;       // on and off variables
var off = 0;

///////////////////////////////////////////////////////////////////////////////

function isNotValidQty(qty) {
    return qty == "" || qty < 1 || isNaN (qty);
}

function isNotValidQty_nullOk(qty) {
    return qty < 0 || isNaN (qty);
}

function checkInputs(form, coins) {
    var msg = '', sum = 0;
    for (var desc in coins) {
        if (isNotValidQty_nullOk(form[coins[desc].qtyId].value)) { 
            msg += 'The quantity of "' + desc + '" is not valid.\n'; 
        }
        else {
            sum += form[coins[desc].qtyId].value * 1;
        } //alert([desc, coins[desc].qtyId);
    }
    
    if (msg != '') {
        alert(msg);
        return false;
    }
    else if (sum == 0) {
        alert('Must order at least 1 coin.');
        return false;
    }
    else {
        return true;
    }

}

function getTotalOfACategory(category) {
    var cartTotal = 0.0;
    
    for (var i=1; i<cmax; i++) {      //check all cookies
        cook = new Cookie (document, "paycart" + i);  
        cook.load();                // get values
        if (cook.qty*1.0 > 0) {     
            for (var desc in category) {
                if (cook.des == desc) {
                    cartTotal = cartTotal + cook.qty*1.0;
                }
            }
        }
    }
    return cartTotal;
}

function updatePricesOfACategory(category, totalQty) {
    // Update the unit price of existed item
    for (var i=1; i<cmax; i++) {      //check all cookies
        cook = new Cookie (document, "paycart" + i);  
        cook.load();                // get values
        if (cook.qty*1.0 > 0) {     
            for (var desc in category) {
                if (cook.des == desc) {
                    cook.prc = category[desc].price(totalQty);
                    cook.store();
                }
            }
        }
    }
}

function ReadForm (form, coins) { //get form data for PayPal

    // Get total qty of coins already in cart
    var cartTotal = getTotalOfACategory(coins);

    // get form total qty
    var formTotalQty = 0;
    for (var desc in coins) { 
        formTotalQty += form[coins[desc].qtyId].value * 1; 
    }
    
    // Add up what already in cart
    var totalQty = cartTotal + formTotalQty; //getTotalQty(form); 
    
    // alert([cartTotal, getTotalQty(form), unitPrice(totalQty)]);
    for (var desc in coins) {
        if (!isNotValidQty(form[coins[desc].qtyId].value)) {
            blk3n = desc; // Set Description
            blk2q = form[coins[desc].qtyId].value; // Set Quantity (StoreCart will add to existed number)
            SetPrice (coins[desc].price(totalQty)); // Set Price
            SetWt (coins[desc].weight); // Set Weight
            SetImg (coins[desc].thumb); 
            StoreCart ();
        }
    }

    // Update the unit price of existed item
    updatePricesOfACategory(coins, totalQty);
    return true;
}

////////////////////////////////////////////////////////////
// The following one function and one variable vary for each item page

function unitPrice_DoubleDragonGeocoin(count) {
    if (count ==  1) return 6.50;
    if (count >=  2 && count <=  5) return 6.50;
    if (count >=  6 && count <= 10) return 6.50;
    if (count >= 11 ) return 6.50;
    return 0;
}

var coin_DoubleDragonGeocoin = { 
    'Double Dragon Geocoin - Two tones' : { 
        qtyId : 'qtyTwoTone', // change to the form input id
        thumb : 'CartImgs/2D_two_ton.gif',
        price : unitPrice_DoubleDragonGeocoin, // change to right function name
        weight: 2.2
    },
    'Double Dragon Geocoin - Antique Gold' : { 
        qtyId : 'qtyAntigueGold',      
        thumb : 'CartImgs/2D_ant_gold.gif',
        price : unitPrice_DoubleDragonGeocoin,
        weight: 2.2
    },
    'Double Dragon Geocoin - Antique Silver' : { 
        qtyId : 'qtyAntigueSilver',    
        thumb : 'CartImgs/2D_ant_sil.gif',
        price : unitPrice_DoubleDragonGeocoin,
        weight: 2.2
    },
    'Double Dragon Geocoin - Antique Copper' : { 
        qtyId : 'qtyAntigueCopper',    
        thumb : 'CartImgs/2D_ant_cop.gif',
        price : unitPrice_DoubleDragonGeocoin,
        weight: 2.2
    }
}; 

//////////////////////////////////////////////////////////////

function unitPrice_2DragCoin(count) {
    if (count ==  1) return 6.50;
    if (count >=  2 && count <=  5) return 6.50;
    if (count >=  6 && count <= 10) return 6.50;
    if (count >= 11 ) return 6.50;
    return 0;
}

var coin_2DragCoin = { 
    'Double Dragon Coin: Two-tones' : { 
        qtyId : 'qtyTwoTone', // change to the form input id
        thumb : 'CartImgs/2D_two_ton.gif',
        price : unitPrice_2DragCoin, // change to right function name
        weight: 1.40
    },
    'Double Dragon Coin: Antique Copper' : { 
        qtyId : 'qtyAntigueCopper',      
        thumb : 'CartImgs/2d_c_ac.gif',
        price : unitPrice_2DragCoin,
        weight: 1.40
    },
    'Double Dragon Coin: Antique Silver' : { 
        qtyId : 'qtyAntigueSilver',    
        thumb : 'CartImgs/2d_c_as.gif',
        price : unitPrice_2DragCoin,
        weight: 1.40
    },
    'Double Dragon Coin:  Black Nickel' : { 
        qtyId : 'qtyBlackNickel',    
        thumb : 'CartImgs/2d_c_bn.gif',
        price : unitPrice_2DragCoin,
        weight: 1.40
    },
    'Double Dragon Coin: Chromium' : { 
        qtyId : 'qtyChromium',    
        thumb : 'CartImgs/2d_c_ch.gif',
        price : unitPrice_2DragCoin,
        weight: 1.40
    }
}; 
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

function unitPrice_3DragCoin(count) {
    if (count ==  1) return 6.50;
    if (count >=  2 && count <=  5) return 6.50;
    if (count >=  6 && count <= 10) return 6.50;
    if (count >= 11 ) return 6.50;
    return 0;
}

var coin_3DragCoin = { 
    'Triple Dragon Coin: Two-tones' : { 
        qtyId : 'qty3Gtwotone', // change to the form input id
        thumb : 'CartImgs/3d_cart_tt.jpg',
        price : unitPrice_2DragCoin, // change to right function name
        weight: 1.50
    },
    'Triple Dragon Coin: Antique Gold' : { 
        qtyId : 'qty3GantGold',      
        thumb : 'CartImgs/3d_cart_ag.jpg',
        price : unitPrice_2DragCoin,
        weight: 1.50
    },
    'Triple Dragon Coin: Antique Copper' : { 
        qtyId : 'qty3GantCopp',    
        thumb : 'CartImgs/3d_cart_ac.jpg',
        price : unitPrice_2DragCoin,
        weight: 1.50
    },
    'Triple Dragon Coin:  Antique Silver' : { 
        qtyId : 'qty3GantSilver',    
        thumb : 'CartImgs/3d_cart_as.jpg',
        price : unitPrice_2DragCoin,
        weight: 1.50
    },
    'Triple Dragon Coin: Satin Silver' : { 
        qtyId : 'qty3GSatinSilver',    
        thumb : 'CartImgs/3d_cart_ss.jpg',
        price : unitPrice_2DragCoin,
        weight: 1.50
    }
}; 
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

function unitPrice_HKongGeo(count) {
    if (count ==  1) return 9.40;
    if (count >=  2 && count <=  5) return 9.05;
    if (count >=  6 && count <= 10) return 8.70;
    if (count >= 11 ) return 8.40;
    return 0;
}

var coin_HKongGeo = { 
    
    'Hong Kong Geocoin: Antique Copper' : { 
        qtyId : 'hk_g_ac',      
        thumb : 'CartImgs/hk_g_ac.gif',
        price : unitPrice_HKongGeo,
        weight: 2.2
    }
}; 

//////////////////////////////////////////////////////////////

function unitPrice_3DragonGeo(count) {
    if (count ==  1) return 9.40;
    if (count >=  2 && count <=  5) return 9.05;
    if (count >=  6 && count <= 10) return 8.75;
    if (count >= 11 ) return 8.40;
    return 0;
}

var coin_3Dragon = { 
    'Triple Dragon: Two-tones' : { 
        qtyId : '3d_g_tt', // change to the form input id
        thumb : 'CartImgs/3d_cart_tt.jpg',
        price : unitPrice_3DragonGeo, // change to right function name
        weight: 1.4
    },
    'Triple Dragon: Antigue Copper' : { 
        qtyId : '3d_g_ac',      
        thumb : 'CartImgs/3d_cart_ac.jpg',
        price : unitPrice_3DragonGeo,
        weight: 1.4
    },
    'Triple Dragon:: Antigue Silver' : { 
        qtyId : '3d_g_as',      
        thumb : 'CartImgs/3d_cart_as.jpg',
        price : unitPrice_3DragonGeo,
        weight: 1.4
    },
    'Triple Dragon: Black Nickel' : { 
        qtyId : '3d_g_bn',    
        thumb : 'CartImgs/3D_holding.gif',
        price : unitPrice_3DragonGeo,
        weight: 1.4
    },
    'Triple Dragon: Chromium' : { 
        qtyId : '3d_g_ch',    
        thumb : 'CartImgs/3D_holding.gif',
        price : unitPrice_3DragonGeo,
        weight: 1.4
    }
}; 
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

function unitPrice_HKongGeoSet(count) {
    if (count >= 1 ) return 45.00;
    return 0;
}

var coin_HKongGeoSet = { 
    'Hong Kong Geocoin Set' : { 
        qtyId : 'hk_g_s', // change to the form input id
        thumb : 'CartImgs/hk_g_s.gif',
        price : unitPrice_HKongGeoSet, // change to right function name
        weight: 11.5
    }
}; 

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

/*function unitPrice_3DragSet04(count) {
    if (count >= 1 ) return 36.00;
    return 0;
}

var coin_3DragSet04 = { 
    'Triple Dragon Geocoin (4) Set' : { 
        qtyId : '3d_g_s4', // change to the form input id
        thumb : 'CartImgs/3d_g_s4.jpg',
        price : unitPrice_3DragSet04, // change to right function name
        weight: 6.0
    }
}; */

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

function unitPrice_3DragSet02(count) {
    if (count >= 1 ) return 18.00;
    return 0;
}

var coin_3DragSet02 = { 
    'Triple Dragon Geocoin (2) Set' : { 
        qtyId : '3d_g_s2', // change to the form input id
        thumb : 'CartImgs/3d_g_s2.jpg',
        price : unitPrice_3DragSet02, // change to right function name
        weight: 4.0
    }
}; 

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

function unitPrice_3DragCoinSet(count) {
    if (count >= 1 ) return 30.00;
    return 0;
}

var coin_3DragCoinSet = { 
    'Triple Dragon Coin Set' : { 
        qtyId : '3drag_c_s', // change to the form input id
        thumb : 'CartImgs/3Drag_g_set.jpg',
        price : unitPrice_3DragCoinSet, // change to right function name
        weight: 7.5
    }
}; 

//////////////////////////////////////////////////////////////

function unitPrice_2DragonCoinSet(count) {
    if (count >= 1 ) return 24.00;
    return 0;
}

var coin_2DragonCoinSet = { 
    'Double Dragon Coin Set' : { 
        qtyId : '2d_c_s', // change to the form input id
        thumb : 'CartImgs/2d_c_s.gif',
        price : unitPrice_2DragonCoinSet, // change to right function name
        weight: 7.5
    }
}; 

//////////////////////////////////////////////////////////////

function unitPrice_3DragonGeoSet(count) {
    if (count >= 1 ) return 18.00;
    return 0;
}

var coin_3DragonGeoSet = { 
    'Triple Dragon Geiocoin Set' : { 
        qtyId : '3d_g_s', // change to the form input id
        thumb : 'CartImgs/3d_g_s.gif',
        price : unitPrice_3DragonGeoSet, // change to right function name
        weight: 7.5
    }
}; 

//////////////////////////////////////////////////////////////

function unitPrice_CoinStand(count) {
    if (count >= 1 ) return 1.25;
    return 0;
}

var coin_CoinStand = { 
    'Coin Stand (set of 2 ea.)' : { 
        qtyId : 'CoinStand', // change to the form input id
        thumb : 'CartImgs/stand.gif',
        price : unitPrice_CoinStand, // change to right function name
        weight: 0.40
    }
}; 

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

function unitPrice_CoinBox(count) {
	var unitPrice;
    unitPrice = 1.50;
    return unitPrice;
}

var coin_CoinBox = { 
    'Coin boxes (1.75 &#8221;): set of 2' : { 
        qtyId : 'coinbox175', // change to the form input id
        thumb : 'CartImgs/coinbox_cart_01.gif',
        price : unitPrice_CoinBox, // change to right function name
        weight: 0.45
    },
    'Coin boxes (2.00 &#8221;): set of 2' : { 
        qtyId : 'coinbox200', // change to the form input id
        thumb : 'CartImgs/coinbox_cart_01.gif',
        price : unitPrice_CoinBox, // change to right function name
        weight: 0.45
    }
};  

//////////////////////////////////////////////////////////////
// TripleDragonGeocoin
function unitPrice_TripleDragonGeocoin(count) {
    var unitPrice;
    if      (count >= 1 && count <= 5)  unitPrice = 8.95;
    else if (count >= 6 && count <= 10) unitPrice = 8.75;
    else if (count >= 11)               unitPrice = 8.45;
    return unitPrice;
}

var coin_3DragGeo = { 
    'Triple Dragon Geocoin: Two-tones' : { 
        qtyId : '3d_g_tt', // change to the form input id
        thumb : 'CartImgs/3d_cart_tt.jpg',
        price : unitPrice_TripleDragonGeocoin, // change to right function name
        weight: 1.45
    }
   // 'Triple Dragon Geocoin: Antique Gold' : { 
//        qtyId : '3d_g_ag', // change to the form input id
//        thumb : 'CartImgs/3d_cart_ag.jpg',
//        price : unitPrice_TripleDragonGeocoin, // change to right function name
//        weight: 1.45
//    },
//    'Triple Dragon Geocoin: Antique Copper' : { 
//        qtyId : '3d_g_ac', // change to the form input id
//        thumb : 'CartImgs/3d_cart_ac.jpg',
//        price : unitPrice_TripleDragonGeocoin, // change to right function name
//        weight: 1.45
//    },
//    'Triple Dragon Geocoin: Antique Silver' : { 
//        qtyId : '3d_g_as', // change to the form input id
//        thumb : 'CartImgs/3d_cart_as.jpg',
//        price : unitPrice_TripleDragonGeocoin, // change to right function name
//        weight: 1.45
//    }
  
}; 

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// 2007 Hong Kong Geocoin Set
//function unitPrice_2007HongKongGeocoinSet(count) {
//    var unitPrice;
//    unitPrice = 45.0;
//    return unitPrice;
//}
//
//var coin_3DragGeoSet = { 
//    'Triple Dragon Geocoin: Set' : { 
//        qtyId : '3d_g_set', // change to the form input id
//        thumb : 'CartImgs/3Drag_g_set.jpg',
//        price : unitPrice_2007HongKongGeocoinSet, // change to right function name
//        weight: 7.5
//    }
//}; 

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

function unitPrice_MiniSScombo(count) {
    var unitPrice;
    unitPrice = 33.75;
    return unitPrice;
}

var coin_MiniSScombo = { 
    'Mini Seashell Combo' : { 
        qtyId : 'MSS_combo', // change to the form input id
        thumb : 'CartImgs/mss_combo.gif',
        price : unitPrice_MiniSScombo, // change to right function name
        weight: 1.3
    }
}; 

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

function unitPrice_MiniSS5p(count) {
	var unitPrice;
    unitPrice = 23.00;
    return unitPrice;
}

var coin_MiniSS5p = { 
    'Dark Seashell: Pack of 5' : { 
        qtyId : 'MSS_d_5p', // change to the form input id
        thumb : 'CartImgs/mss_d.gif',
        price : unitPrice_MiniSS5p, // change to right function name
        weight: 0.8
    },
	
	'White Seashell: Pack of 5' : { 
        qtyId : 'MSS_w_5p', // change to the form input id
        thumb : 'CartImgs/mss_w.gif',
        price : unitPrice_MiniSS5p, // change to right function name
        weight: 0.8
    }
	
};  

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

function unitPrice_MiniSS2p(count) {
	var unitPrice;
    unitPrice = 9.25;
    return unitPrice;
}

var coin_MiniSS2p = { 
    'Dark Seashell: Pack of 2' : { 
        qtyId : 'MSS_d_2p', // change to the form input id
        thumb : 'CartImgs/mss_d.gif',
        price : unitPrice_MiniSS2p, // change to right function name
        weight: 0.3
    },
	
	'White Seashell: Pack of 2' : { 
        qtyId : 'MSS_w_2p', // change to the form input id
        thumb : 'CartImgs/mss_w.gif',
        price : unitPrice_MiniSS2p, // change to right function name
        weight: 0.3
    }
	
};  

//////////////////////////////////////////////////////////////
// Sundial Geocoin
//////////////////////////////////////////////////////////////

function unitPrice_SunGeo(count) {
    var unitPrice;
    if      (count >= 1 && count <= 5)  unitPrice = 9.50;
    else if (count >= 6 && count <= 10) unitPrice = 9.50;
    else if (count >= 11)               unitPrice = 9.50;
    return unitPrice;
}

var coin_SunGeo = { 
    'Sundial Geocoin: Two-tones' : { 
        qtyId : 'sd_g_tt', // change to the form input id
        thumb : 'CartImgs/sd_cart_tt.jpg',
        price : unitPrice_SunGeo, // change to right function name
        weight: 1.0
    },
    'Sundial Geocoin: Antique Gold' : { 
        qtyId : 'sd_g_ag', // change to the form input id
        thumb : 'CartImgs/sd_cart_ag.jpg',
        price : unitPrice_SunGeo, // change to right function name
        weight: 1.00
    }
//    'Sundial Geocoin: Antique Copper' : { 
//        qtyId : 'sd_g_ac', // change to the form input id
//        thumb : 'CartImgs/sd_cart_ac.jpg',
//        price : unitPrice_SunGeo, // change to right function name
//        weight: 1.00
//    }
  
}; 

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// Sundial Geocoin Set
//////////////////////////////////////////////////////////////

function unitPrice_SunGeoSet(count) {
    var unitPrice;
    unitPrice = 28.0;
    return unitPrice;
}

var coin_SunGeoSet = { 
    'Sundial Geocoin: Set' : { 
        qtyId : 'sd_g_set', // change to the form input id
        thumb : 'CartImgs/sd_g_set.jpg',
        price : unitPrice_SunGeoSet, // change to right function name
        weight: 3.0
    }
}; 

//////////////////////////////////////////////////////////////

// After create the coin info, make sure the variable is in the following list too.
var Categories = [
    coin_DoubleDragonGeocoin,
    coin_2DragCoin,
    coin_HKongGeo,
    coin_3Dragon,
    coin_HKongGeoSet,
    coin_2DragonCoinSet,
    coin_3DragonGeoSet,
    coin_CoinStand,
    coin_3DragGeo,
    coin_3DragGeoSet,
	coin_CoinBox,
	coin_MiniSS2p,
	coin_MiniSS5p,
	coin_MiniSScombo,
	coin_3DragCoin,
	coin_3DragCoinSet,
	coin_SunGeo,
	coin_3DragSet04,
	coin_SunGeoSet,
	coin_3DragSet02
];
/////////////////////////////////////////////////////////////////////////////

function AddDesc (strn) {  // add to current description
var c = "";
  if (strn.length < 1 || strn == " ") return;
  if (blk3n.length > 0) c = ", ";
  blk3n = blk3n + c + strn;
}

function AddFixed (amt, txt) {  // add flat charge to item
  ader = ader*1.0 + amt*1.0;
  if (txt.length > 0) adtx = adtx + ", " + txt;
}

function AddMisc (strn) {  // add extra stuff to PayPal params
var s = "&";
  if (strn.substring(0,1) == "&") s = "";
  bmisc = bmisc + s + strn;
}

function AddOpt1 (val) {  // add to the value in op1v
var c = "";
  if (val.length == 0 || val == " ") return;
  if (op1n.length == 0) op1n = "opt1";
  if (op1v.length > 0) c = ", ";
  op1v = op1v + c + val;
}

function AddOpt2 (val) {  // add to the value in op2v
var c = "";
  if (val.length == 0 || val == " ") return;
  if (op2n.length == 0) op2n = "opt2";
  if (op2v.length > 0) c = ", ";
  op2v = op2v + 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 AddWt (ozs) {  // set the weight of an item
  bozs = ozs*1.0 + bozs*1.0;
}

function CalcInsu (amt) {  // calculate insurance on an amount
//  it equals $1.50
//  return (1.50 + 0.02 * amt);
  return (1.50);
}

function CallView () { // call the local shopping cart view
  cartwin = window.open ("cart1.html", "local29", winpar);
//  cartwin.focus ();
}

function ChkCoup () {  // check for a discount coupon
var i;
  coup = -1;          // assume the worst
  coupons = root.xx_cval.split (",");
  couponp = root.xx_cdis.split (",");
  coupona = root.xx_camt.split (",");
  for (i=0; i<coupons.length; i++) {
    if (coupval == coupons[i]) {
      coup = 1;              // user hit the coupon value
      cdis = couponp[i];     // remember the discount percent
      camt = coupona[i];     // discount amount 
      cval = coupval;        // remember entered value
      if (cdis > 0) {
        alert ("Valid coupon number! \n\n" +
               cdis + "% discount now in effect.");
        ctxt = "COUPON-" + cdis + "%" + cval;
      }
      if (camt > 0) {
        alert ("Valid coupon number! \n\n" +
               "$" + camt + " discount now in effect.");
        ctxt = "COUPON-$" + camt + "-" + cval;
      }
      root.cval = cval;      // remember entered value
      root.coup = coup;
      root.cdis = cdis;
      root.camt = camt;
      root.ctxt = ctxt;
      root.xx_coup = off;    // suppress display
      root.store ();
      document.location.reload ();  // show the latest info...
      return;
    }
  }
  alert ("'" + coupval + "'  not a valid code!");
}

function ChkFlg (temp) {     // check for special flag chars
var j,tok,val;
var ary = new Array ();      // where we break down input
  ary = temp.split (" ");    // divide on spaces
  for (j=0; j<ary.length; j++) {
// first we do single character tokens...
    if (ary[j].length < 2) continue;
    tok = ary[j].substring (0,1); // first character
    val = ary[j].substring (1);   // get data
    if (tok == "@") SetPrice (parseFloat(val));
    if (tok == "+") AddPrice (parseFloat(val));
    if (tok == "%") AddPrcnt (parseFloat(val));
    if (tok == "#") {             // record weight
      AddWt (parseFloat(val));
      ary[j] = "";                // zap this array element
    }
// Now we do 3-character tokens...
    if (ary[j].length < 4) continue;
    tok = ary[j].substring (0,3); // first 3 chars
    val = ary[j].substring (3);   // get data
    if (tok == "cd=") {           // value for part number
      SetCode (val);
      ary[j] = "";                // clear it out
    }
  }
  return ary.join (" ");          // rebuild val with what's left
}

function ChkOpts () {  // check if user has selected everything
  if (tprc < root.xx_mamt || tqty < root.xx_mqty) {  // check minimums
    alert ("You have not yet met the minimum order requirements!");
    return false;
  } else
  if (root.xx_chkbx == on && root.chkby == off) {  // agree checkbox
    alert ('You must agree to terms!');
    return false;
  } else
//  if (root.xx_tax == on && ttxt.length == 0) {  // tax selection
//    alert ('Please select region for tax calculation');
//    return false;
//  } else
  if (root.xx_ship == on && root.sv0 == 0) {  // shipping selection
    alert ('Please select shipping method');
    return false;
  }
  return true;
}

function ClearAll () {  // wipe out the last entry
  blk2q = "";  // quantity
  blk3n = "";  // name
  blk4a = "0"; // price
  bcode = "";  // item number
  bmisc = "";  // extra data
  blkhn = "";  // item handling charges
  blksh = "";  // item shipping charges
  op1n  = "";  // clear options
  op1v  = "";
  op2n  = "";
  op2v  = "";
  imgx  = "dummy.jpg";
  ader  = 0;
  adtx  = "";
  bozs  = 0;
  dn    = 0;
  an    = 0;
  pcat  = 0;
  iqty  = 0;        // minimum item quantity
  xqty  = 1000000;  // max item quantity
}

function ClearCart () {     // zap all the cookies
var i;
  for (i=1; i<cmax; i++) {  // the data cookies
    ClearEntry (i);
  }
}

function getCategory(coinDesc, categories) {
    for (var i in categories)
        if (categories[i][coinDesc] != undefined)
            return categories[i];
    return null;
}

function ClearEntry (i) {  // knock out a specific entry
    cook = new Cookie (document, "paycart" + i);
    cook.load();
    
    var deletedCategory = getCategory(cook.des, Categories);
    InitData ();
    
    var totalQty = getTotalOfACategory(deletedCategory);
    updatePricesOfACategory(deletedCategory, totalQty);
    
    // Recalculate price
}

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';
    if (root.xx_cdmp == on)           // cookie diagnostic dump
        alert(cookie.length + " chars - " + cookie);
    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 + '='); // $name is paycart#
    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 GetTots (indx) {  // return cart totals
    root.load();             // get the cookie values
}

function InitData () {  // zap a data cookie
    cook.pcat = 0;
    cook.qty  = 0;
    cook.cod  = "";
    cook.ihnd = 0;
    cook.des  = "";
    cook.op1n = "";  // option name and value
    cook.op1v = "";
    cook.op2n = "";
    cook.op2v = "";
    cook.prc  = 0;
    cook.ozs  = 0;
    cook.imgx = "dummy.jpg";
    cook.ader = 0;   // add fixed amount
    cook.adtx = "";  // text for fixed amount
    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.cval  = cval;
    root.ctxt  = ctxt;
    root.cdis  = cdis;
    root.camt  = camt;
    root.chkby = off;
    root.gwtot = gwtot;
    root.stxt0 = "";
    root.insu  = insu;     // insurance flag

    root.qty   = 0;
    root.tot   = 0;

    root.xx_bn   = "";
    root.xx_can  = "";
    root.xx_cbt  = "";
    root.xx_cc   = off;
    root.xx_cdis = "";
    root.xx_camt = "";
    root.xx_cdmp = off;
    root.xx_chkbx= off;
    root.xx_coup = off;
    root.xx_cper = 0;
    root.xx_cur  = "USD";
    root.xx_dadd = on;
    root.xx_id   = "";
    root.xx_img  = off;
    root.xx_lc   = "US";
    root.xx_mamt = 0;
    root.xx_mqty = 0;
    root.xx_pc   = off;
    root.xx_prt  = on;
    root.xx_prtx = off;
    root.xx_ret  = "";
    root.xx_ship = off;
    root.xx_siz  = on;
    root.xx_sty  = "";
    root.xx_sum  = off;
    root.xx_tax  = off;
    root.xx_wt   = off;
    root.xx_wtent= "oz";
    root.xx_wtrup= 10;

    for (i=0; i<cats; i++) {
        root["hn" + i] = 0;         // qty-based handling brkpts
        root["qd" + i] = 0;         // qty-based percent dis brkpts
        root["sn" + i] = 0;         // qty-based shipping brkpts
        root["pv" + 1] = 0;         // cart-quantity charges
        root["ad" + i] = 0;         // amt-based percent dis brkpts
        root["sv" + i] = 0;         // amt-based shipping bkpts
        root["wv" + i] = 0;         // wgt-based shipping bkpts
    }
    root.store();                 // record it all..
}

function LoadTots (obj1) {          // get access to cart totals
    var i;
    for (i=0; i<obj1.length; i++) {   // Look in all FORMs
        if (obj1[i].ctot) obj1[i].ctot.value = "$" + Dollar (root.tot);
        if (obj1[i].cqty) obj1[i].cqty.value = root.qty;
    }
}

function PrintCart () {  // print out entire cart, and get cart totals
    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
    thnd = 0;                 // item specific handling
    tshp = 0;                 // item specific shipping

    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
        ctot["w" + i] = 0;      // weight 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

    hand = 0;  // line display totals
    ship = 0;
    tpkg = 1;
    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 category
        var a = ctot["a" + p]*1.0;             // amount this category
        var w = ctot["w" + p]*1.0;             // weight this category
        if (w > 0) w += 0.5;    // Add 0.5 oz for a "cat" which has items
        if (root.xx_wtent == "gm")             // do we fix it?
          w = Math.ceil(w / root.xx_wtrup) * root.xx_wtrup;
        else
          w = Math.ceil(w);
        ctot["w" + p] = w;                     // put back fixed version

        if (root["hn" + p] > 0) {
          tmp1 = root["hqty" + p].split(",");
          tmp2 = root["hamt" + p].split(",");
          for (i=root["hn" + p]-1; i>=0; i--) {  // run the table
            if (q >= tmp1[i]*1.0) {              // Check the quantity
              ha = tmp2[i]*1.0;                  // yep, set it
              break;                             // a-dios, mo-fo
            }
          }
        }
        ctot["h" + p] = ha;
        hand = hand + ha;

        if (root["sn" + p] > 0) {
          tmp1 = root["sqty" + p].split(",");
          tmp2 = root["samt" + p].split(",");
          for (i=root["sn" + p]-1; i>=0; i--) {  // run the table
            if (q >= tmp1[i]*1.0) {              // Check the price
              sh = tmp2[i]*1.0;                  // yep, set it
              break;
            }
          }
        }
        if ((sh == 0) && root["sv" + p] > 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;
            }
          }
        }
        if ((sh == 0) && root["pv" + p] > 0) { // only one shipping
          tmp1 = root["pval" + p].split(",");
          tmp2 = root["pper" + p].split(",");
          for (i=root["pv" + p]-1; i>=0; i--) {  // run the table (if one)
            if (a >= tmp1[i]*1.0) {      // Check the price
              sh = a * tmp2[i]/100;      // yep, set it
              break;
            }
          }
        }
        // Weight to money calculating?
        if ((sh == 0) && root["wv" + p] > 0) {  // only one shipping
          tmp1 = root["wval" + p].split(",");   // weight based shipping
          tmp2 = root["wchg" + p].split(",");
          for (i=root["wv" + p]-1; i>=0; i--) { // run the table (if one)
            if (w >= tmp1[i]*1.0) {     // Check the weight
              sh = tmp2[i]*1.0;         // yep, set it
              break;
            }
          }
        }
        ctot["s" + p] = sh;
        ship = ship + sh;

        if (root["qd" + p] > 0) {
          tmp1 = root["qqty" + p].split(",");  // qty based, %discount
          tmp2 = root["qamt" + p].split(",");
          for (i=root["qd" + p]-1; i>=0; i--) {
            if (q >= tmp1[i]*1.0) {
              qt = a * tmp2[i]/100.0;
              break;
            }
          }
        }

        if (root["ad" + p] > 0) {
          tmp1 = root["aprc" + p].split(",");  // amt based, %discount
          tmp2 = root["adis" + p].split(",");
          for (i=root["ad" + p]-1; i>=0; i--) {
            if (a >= tmp1[i]*1.0) {
              qt = a * tmp2[i]/100.0;
              break;
            }
          }
        }

        if (coup > 0) {
          qt = qt + a * cdis/100.0;  // percent discount
          qt = qt + q/tqty * camt;   // amount discount
        }
        ctot["d" + p] = qt;
        qtyd = qtyd + qt;
    }

    if (insu > 0) insx = CalcInsu (tprc - qtyd);

    hand = hand + thnd;
    ship = ship + tshp;

    ttax = Dollar ((tprc + hand + ship + gwtot*tqty - qtyd) * stax/100.0);
    gtot = Dollar (tprc + hand + ship + ttax*1.0 + insx
         - qtyd + gwtot*tqty);
    // record global totals for ref on other pages
    root.qty = tqty;
    root.tot = gtot;
    root.store ();   // save it off
}

function PrintItem (i) {  // make one entry in cart
var tm,t1,t2,wt,wtt;
  document.writeln ('<tr>');
  
  // Delete button
  document.writeln ('  <td>');
  document.writeln ('    <a class="RemoveButton"');
  document.writeln ('        onclick = "ClearEntry(', i, ');');
  document.writeln ('        document.location.reload();"><img src="../Items/delete.jpg"></a>');
  document.writeln ('  </td>');
 
  // Image TD
  document.writeln ('  <td align = "center">', '<img src = "', cook.imgx, '" border="0"');
  document.writeln ('  alt = "Image: Item" />', '</td>');
    
  // Description TD
  document.writeln ('  <td>',  cook.des + cook.adtx);
  if (cook.ihnd > 0) {  // item-specific handling charges
    document.writeln (', HND=' + Dollar (cook.ihnd));
    thnd = thnd + cook.ihnd*1.0;
  }
  if (cook.ishp > 0) {  // item-specific shipping charges
    document.writeln (', SHP=' + Dollar (cook.ishp));
    tshp = tshp + cook.ishp * cook.qty;
  }
  if (cook.op1n.length > 0) {  // see if we append stuff
    document.writeln (', ' +
      cook.op1n + ' ' + 
      cook.op1v + ' ' +
      cook.op2n + ' ' +
      cook.op2v);
  }
  document.writeln ('</td>');
  // End of Description TD
      
  // Qty TD
  document.writeln ('  <td class="number">', cook.qty, '</td>');
  
  // Price TD
  tm = Dollar (cook.prc);
  t1 = cook.qty*1.0;
  t2 = cook.pcat;
  document.writeln ('  <td class="number">',
    tm,'</td>');
  
  // Total TD
  tm = Dollar (t1*cook.prc + cook.ader*1.0);
  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 class="number">', tm,'</td>');

  // Weight TD
  //wt = cook.ozs*1.0 * cook.qty;
  wt = cook.ozs*cook.qty;
  tozs = tozs + wt;
  ctot["w" + t2] = ctot["w" + t2] + wt;
  wtt = wt.toFixed(2);
  wt = wtt;
//  if (root.xx_wt == on) {  // do we show weight?
//    document.writeln ('  <td>',
//      cook.ozs,'</td>');
//  
//  // Total weight TD
//    document.writeln ('    <td>',
//      wt,'</td>');
//  }

  document.writeln ('</tr>');
}

//function ReadForm (obj1, tst) { //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
//    if (obj.name == "tot" ||          // stkp some junk
//        obj.name == "cqty" ||
//        obj.name == "ctot") continue;
//    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
//      if (val == "@-1") continue;     // skip the crap
//      if (obj.name == "qty") {        // this is the quantity
//        qty = val;                    // get user input
//        if ((qty == "" || qty < 1 || isNaN (qty)) && tst) {  // test
//          alert ("Enter a valid integer quantity!");
//          return false;
//        }
//      } else {                        // this was not qty
//        val = 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;
//          val = 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;
//        val = ChkFlg (val);          // flag chars?
//        if (nam3 == "opt") {         // user says where to store
//          Where (val, nam4);         // stash it
//        } else if (val.length > 0) {
//          AddDesc (val);             // add to data
//        }
//      }
//    } else
//    if (obj.type == "text" ||
//        obj.type == "textarea") {  // user input fields
//      val = obj.value;             // get input
//      if (obj.name == "qty") {     // this is the quantity
//        qty = val;                 // get user input
//        if ((qty == "" || qty < 1 || isNaN (qty)) && tst) {  // 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 (tst == false && obj.type == "hidden" && obj.name == "initer") {
//      val = obj.value;
//      ChkFlg (val);
//    }
//  }  // end of loop

//  if (qty == 0) qty = 1;     // make sure we have something
//  if (qty < iqty && tst) {   // minimum items ordered?
//    alert ("You must order at least " + iqty + " of this item!");
//    return false;
//  }
//  if (qty > xqty && tst) {   // maximum items that can be ordered
//    alert ("You cannot order more than " + xqty + " of this item!");
//    return false;
//  }
//  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 (dis > 0) {             // there is an item discount, here
//    AddPrcnt (-dis);         // apply the discount
//    AddDesc ("DIS=" + dis + "%");  // mark it
//  }
//  if (obj1.tot) obj1.tot.value = "$" + blk4a;
//  if (tst) {
//    StoreCart ();
//    if (root.xx_dadd == off) {
//      alert("Item has been aded to cart!");
//      return false;            // hide cart display
//    }
//  }
//  return true;
//}

function SetAmtDC (pcat, a1, p1) {  // set cart amt dis breakpoints
var i,ad;
var aprc = new Array ();
var adis = new Array ();
  if (pcat >= cats) {          // check him out
    alert ("SetAmtDC cat arg too big!");
    return;
  }
  ad = 0;                      // count of breakpoints
  for (i=1; i<arguments.length; i=i+2) {
    aprc[ad] = arguments[i];   // price breakpoint
    adis[ad] = arguments[i+1]; // discount percent
    ad = ad + 1;               // number of bkpts
  }
  root["ad" + pcat] = ad;      // stash that bad boy off
  root["aprc" + pcat] = aprc;
  root["adis" + pcat] = adis;
  root.store();                // remember forever...
}

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 SetCartDC (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 ("SetCartDC 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 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 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 = cd;
}

function SetDesc (strn) {  // set the desc field
  blk3n = strn;
}

function SetFixed (amt, txt) {  // set flat charge to item
  ader = amt*1.0;
  if (txt.length > 0) adtx = ", " + txt;
  else adtx = "";
}

function SetHN (amt) {  // set item-specific handling charges
  blkhn = amt;    // set local variable
  AddPrice (amt); // add in the price
}

function SetID (strn) {  // set the PayPal ID of this user
  blk2b = strn;
}

function SetImg (strn) {  // set the image for the cart
  imgx = strn;
}

function SetOpt1 (nam, val) {  // set the value of 1st option
  op1n = nam;
  op1v = val;
}

function SetOpt2 (nam, val) {  // set the value of 2nd option
  op2n = nam;
  op2v = val;
}

function SetPcat (val) {
  if (val >= cats) {
    alert ("Only " + cats + " product categories allowed!\n\n" +
           "Correct your HTML!");
    return;
  }
  pcat = val;
}

function SetPerSH (pcat, a1, p1) {   // % of amt-based shipping brkpts
var i,pv;
var pval = new Array ();
var pper = new Array ();
  if (pcat >= cats) {          // check him out
    alert ("SetPerSH cat arg too big!");
    return;
  }
  pv = 0;                      // count of breakpoints
  for (i=1; i<arguments.length; i=i+2) {
    pval[pv] = arguments[i];   // price breakpoint
    pper[pv] = arguments[i+1]; // discount amount
    pv = pv + 1;               // number of bkpts
  }
  root["pv" + pcat]   = pv;    // stash that bad boy off
  root["pval" + pcat] = pval;
  root["pper" + pcat] = pper;
  root.store();                // remember forever...
}

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 SetQT (q) {           // set minimum item quantity
  iqty = q;
}

function SetQX (q) {           // set maximum item quantity
  xqty = q;
}

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 SetSH (amt) {  // set item specific shipping amount
  blksh = amt;    // set local variable
  AddPrice (amt); // add in the price
}

function SetWgtSH (pcat, w1, c1) {   // ship by weight bkpts
var i,wv;
var wval = new Array ();
var wchg = new Array ();
  if (pcat >= cats) {          // check him out
    alert ("SetWgtSH cat arg too big!");
    return;
  }
  wv = 0;                      // count of breakpoints
  for (i=1; i<arguments.length; i=i+2) {
    wval[wv] = arguments[i];   // price breakpoint
    wchg[wv] = arguments[i+1]; // discount amount
    wv = wv + 1;               // number of bkpts
  }
  root["wv" + pcat]   = wv;    // stash that bad boy off
  root["wval" + pcat] = wval;
  root["wchg" + pcat] = wchg; // alert([wv, wval, wchg]);
  root.store();                // remember forever...
}

function SetWt (ozs) {  // set the weight of an item
  bozs = ozs;
}

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.prc = blk4a;
        
        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.ihnd = blkhn;
      cook.ishp = blksh;
      cook.op1n = op1n;
      cook.op1v = op1v;
      cook.op2n = op2n;
      cook.op2v = op2v;
      cook.prc  = blk4a;
      cook.imgx = imgx;
      cook.ozs  = bozs;
      cook.ader = ader;
      cook.adtx = adtx;
      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 FORM
var inp = '<input type="hidden" name=';
var ary1 = new Array ();
var ary2 = new Array ();
var i,j,pc,sx;
var tot  = 0;               // price total
var frst = 1;               //  1st time thru marker
var str  = "";              // temp string
var disc = new Array ();    // price per item to subtract for discount
var tmps = root.stxt0+" "+ttxt+" "+ctxt;  // the shipping text

  if (bmisc.length > 0) {            // apply misc codes
    ary1 = bmisc.split ("&");        // individual commands
    for (i=0; i<ary1.length; i++) {  // process individual entries
      if (ary1(i).length > 0) {      //  but only if something there
        ary2 = ary1[i].split ("=");  // get parts
        document.writeln (inp, '"', ary2[0], // build line
          '" value="', ary2[1], '" />');
      }
    }
  }

  if (ttax > 0)             // do we have a calculated tax
    document.writeln (inp, '"tax_cart" value="', Dollar(ttax), '" />');
  for (i=0; i<cats; i++) {  // set item discounts
    disc[i] = ctot["d" + i] / ctot["a" + 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
      document.writeln (inp, '"quantity_', 
        j, '" value="', cook.qty, '" />');
      document.writeln (inp, '"item_name_', 
        j, '" value="', cook.des, cook.adtx, '" />');
      document.writeln (inp, '"amount_',
        j, '" value="', Dollar (cook.prc * (1.0 - disc[cook.pcat]) + cook.ader/cook.qty), '" />');
      if (gwtot > 0 && frst > 0) str = ', GIFT-WRAP';
      if (cook.op1n.length > 0) {    // 1st option present
        document.writeln (inp, '"on0_',
          j, '" value="', cook.op1n, '" />');
        document.writeln (inp, '"os0_',
          j, '" value="', cook.op1v + ', ' + tmps, '" />');
      } else if (tmps.length > 0) {
        document.writeln (inp, '"on0_',
          j, '" value="Shipping" />');
        document.writeln (inp, '"os0_',
          j, '" value="', tmps, str, '" />');
      }
      tmps = "";  // zap it
      str  = "";

      if (cook.op2n.length > 0) {    // 2nd option present
        document.writeln (inp, '"on1_',
          j, '" value="', cook.op2n, '" />');
        document.writeln (inp, '"os1_',
          j, '" value="', cook.op2v, '" />');
      }
      if (cook.cod != "")
        document.writeln (inp, '"item_number_',
          j, '" value="',cook.cod, '" />');
      if (frst > 0 && (hand > 0 || insx > 0 || gwtot > 0)) {
        document.writeln (inp, '"handling_',
          j,'" value="', Dollar (hand*1.0 + insx*1.0 + gwtot*tqty), '" />');
      }
      if (frst > 0 && ship > 0) 
        document.writeln (inp, '"shipping_',
          j, '" value="', Dollar (ship), '" />');
      else
        document.writeln (inp, '"shipping_',
          j, '" value="0" />');

      frst = 0;  // set to false - true only on 1st item
    }
  }
}

function Shipper (obj1) {  // use either amt or qty based shipping1
/* Note - when using something other than SetAmtSH you must include
SetAmtSH (0,0,0); within every position to kill shipping question. 
AND, every call used here must have a zero call under the worst area.*/
var pos;
  SetAmtSH (0);          // assume the worst
  SetCartSH (0);
  SetPerSH (0);
  SetWgtSH (0);
  insu = -1;
  pos = obj1.selectedIndex;  // option selected
  root.stxt0 = obj1.options[pos].text;
  if (pos == 1) {	// US
    SetAmtSH (0,0,0);
    //SetWgtSH (0, 1,1.25, 2,1.50, 3,1.75, 4,2.20, 5,2.50, 6,2.75, 7,3.00, 8,3.25, 9,3.50, 10,3.75, 11,4.00, 12,4.25, 13,4.50, 14,5.50, 28,7.25, 41,10.25, 48,12.05);
    //SetWgtSH (0, 1,1.25, 2,1.5, 3,1.75, 4,2.2, 5,2.5, 6,2.75, 7,3, 8,3.25, 9,3.5, 10,3.75, 11,4, 12,4.25, 13,4.5, 14,5.5, 27,7.25, 34,10.25, 43,10.75, 53,12.75, 66,15);
	//SetWgtSH (0, 1,1.2, 2,1.75, 3,1.85, 4,2.4, 5,2.6, 6,3.05, 7,3.45, 8,3.65, 9,3.8, 10,3.95, 11,4.1, 12,4.25, 13,4.4, 14,5.75, 24,7.25, 35,7.75, 39,8.25, 44,10.5, 56,12.75);
	//SetWgtSH (0,1,1.60,2,2.20,3,2.20,4,2.40,5,2.60,6,3.05,7,3.45,8,3.65,9,3.80,10,3.95,11,4.10,12,4.25,13,4.40,23,5.75,34,7.25,38,7.75,43,8.25,55,10.50,70,12.75);
	//SetWgtSH (0,1,1.60,2,2.20,3,2.40,4,2.60,5,2.80,6,3.05,7,3.45,8,3.65,9,3.80,10,3.95,11,4.10,12,4.25,13,4.40,14,5.95,24,7.00,34,7.50,35,7.85,36,12.00,56,15.00);
	SetWgtSH (0,1,1.60,2,2.20,3,2.40,4,2.60,5,2.80,6,3.05,7,3.45,8,3.65,9,3.80,10,3.95,11,4.10,12,4.25,13,4.40,14,5.95,24,7.00,33,12.50,53,15.00);
  } else
//  if (pos == 2) {    // US Insured
//   SetAmtSH (0,0,0);
//    SetWgtSH (0, 1,2.5, 2,2.5, 3,2.5, 4,2.85, 5,3.2, 6,3.55, 7,3.9, 8,4.25, 9,4.6, 10,4.95, 11,5.3, 12,5.65, 13,6, 14,6.35, 15,6.95, 24,7.95, 32,8.95, 48,9.95, 64,10.95, 80,11.95, 96,12.95);
//    insu = 1;
//  } else
  if (pos == 2) {    // Canada
    SetAmtSH (0,0,0);
    //SetWgtSH (0, 1,1.50, 2,1.75, 3,2.00, 4,2.75, 5,3.00, 7,3.25, 8,3.75, 9,4.75, 13,5.25, 17,5.75, 21,8.95, 30,10.90, 38,13.25, 41,16.50, 49,19.25, 57,22.50);
    //SetWgtSH (0, 1,1.5, 2,1.75, 3,2, 4,2.75, 5,3, 7,3.25, 8,3.75, 9,4.75, 13,5.25, 17,5.75, 19,8.95, 28,10.05, 34,13.5, 42,14.75, 51,17.5, 57,19.5, 66,22.75);
	//SetWgtSH (0, 1,1.5, 2,1.85, 3,2.1, 4,2.75, 5,3, 6,3.25, 7,3.5, 8,3.95, 9,4.5, 11,4.95, 13,5.5, 15,5.95, 17,6.5, 19,6.95, 21,7.5, 24,10.25, 34,12, 38,13.5, 42,15, 46,20.5, 57,25);
	//SetWgtSH (0,1,1.75,2,1.95,3,2.10,4,2.75,5,3.00,6,3.25,7,3.50,8,3.95,10,4.50,12,4.95,14,5.50,16,5.95,18,6.50,20,6.95,23,7.50,33,10.25,37,12.00,41,13.50,45,15.00,56,20.50,70,25.00);
	//SetWgtSH (0,1,1.80,2,2.00,3,2.20,4,2.75,5,3.00,6,3.30,7,3.75,8,4.10,9,5.50,13,6.50,19,7.75,24,10.00,31,13.00,42,16.00,48,28.50,60,30.00);
	 SetWgtSH (0,1,1.80,2,2.00,3,2.20,4,2.75,5,3.00,6,3.30,7,3.75,8,4.10,9,5.50,13,6.50,17,7.75,21,10.00,29,13.00,33,15.00,37,17.00,42,19.00,49,28.50,60,30.00);
  } else
  if (pos == 3) {    // Other International
    SetAmtSH (0,0,0);
  //SetWgtSH (0, 1,2.75, 2,3.00, 3,3.25, 4,4.25, 5,5.25, 9,6.25, 10,7.25, 15,8.95, 21,12.50, 32,14.95, 37,16.25, 41,25.25, 49,26.95, 57,30.25);
    //SetWgtSH (0, 1,2.75, 2,3.00, 3,3.25, 4,4.25, 5,5.25, 6,6.5,  11,7.25, 15,8.25, 19,9.0, 22,12.5, 32,14.95, 37,16.25, 41,25.25, 49,26.95, 57,30.25);
    //SetWgtSH (0, 1,2.75, 2,3, 3,3.25, 4,4.25, 5,5.25, 6,6.5, 14,8.25, 19,12.5, 28,13.95, 34,16.95, 42,18.95, 51,22.5, 57,24.5, 66,27);
	//SetWgtSH (0, 1,2.75, 2,3, 3,3.5, 4,4.5, 5,5.65, 6,6.5, 7,7.5, 8,8.45, 9,10.05, 13,13.25, 24,15, 34,16.5, 41,18.95, 46,24.5, 66,27);
	//SetWgtSH (0,1,2.75,2,3.00,3,3.50,4,4.50,5,5.65,6,6.50,7,7.50,8,8.45,12,10.05,23,13.25,33,15.00,40,16.50,45,18.95,65,24.50,70,27.00);
	//SetWgtSH (0,1,2.75,2,3.00,3,3.75,4,4.75,5,5.65,6,6.50,7,7.75,8,8.95,12,10.50,16,13.25,24,16.00,36,19.95,48, 28.50,60,30.00);
	 SetWgtSH (0,1,2.75,2,3.25,3,3.95,4,4.95,5,5.95,6,6.75,7,7.75,8,8.95,9,10.50,13,12.00,16,12.50,17,15.00,26,17.00,33,24.50,37,27.50,41,30.05,45,33.50,49,48.00,60,52.00);

  }   
  if (pos == 4) {    // Pick up
    SetAmtSH (0,0,0);
    SetWgtSH (0,0,0);
  } 
  root.insu = insu;
  root.store ();
  document.location.reload ();  // show the latest info...
}

function LoadIt () {
// executed at load time
  Cookie.prototype.store  = _Cookie_store;   // load proto methods
  Cookie.prototype.load   = _Cookie_load;
  Cookie.prototype.remove = _Cookie_remove;
  
  cook = new Cookie (document, "paycart1");
  cook.load();
  if (!cook.qty) {  //create the structure
    root = new Cookie (document, "paycart0");  // root cookie
    root.load();
    InitRoot ();
    for (var i=1; i<cmax; i++) {  // load or init data cookies
      cook = new Cookie (document, "paycart" + i);
      InitData ();              // create it
    }
  }
  root = new Cookie (document, "paycart0");  // root cookie
  root.load();
  stax  = root.stax;  // load defaults
  ttxt  = root.ttxt;
  coup  = root.coup;
  cdis  = root.cdis;
  camt  = root.camt;
  cval  = root.cval;
  ctxt  = root.ctxt;
  gwtot = root.gwtot;

  insu  = root.insu;
}

