var order = new Object(); // object to store order in
var root  = new Object();  // selection criteria
var on    = true;
var off   = false;
var cntr    = 0;           // items in object
var opts    = 5;           // number of options to allow
var shpr    = 0;           // what to charge for shipping;
var stxt    = "";  // shipping text
var usrzip  = 0;           // user input of zip code
var spos    = -1;          // position in the shipping select
var zne     = 0;           // zone calc'ed from user zip
var zip     = "";          // user input of zip code
var ttax  = 0;             // percent for taxes
var ttxt  = "0";           // tax text
var tpos  = -1;            // tax position selector
var tamt=0,tqty=0,twgt=0,thnd=0;  // totals

var minqty = "10";			// set minimum quantity

handchg = 5.00	 // handling charge
root.hamt = 0;     // amount, below which, handling charge applies
root.hand = 0;     // handling charge for orders less than hamt
root.shp  = on;    // shipping selection line
root.tax  = on;    // tax selection line
// place for user-specific options
root.xx_can  = ""; // place for PayPal cancel return path
root.xx_cur  = ""; // enter default currency code (or null)
root.xx_id   = "ed@muellerartcover.com"; // PayPal ID
root.xx_img  = ""; // image URL
root.xx_lc   = ""; // enter default country code (or null)
root.xx_ret  = ""; // place for PayPal return path
root.xx_sty  = ""; // place for PayPal page style
root.xx_xtra = ""; // place for other PayPal commands

var zn = 0;                // ZIP, zone entries index
var zzip = new Array ();   // ZIP entry
var zone = new Array ();   // corresponding zone for ZIP

// USPS priority mail zone charge tables to 40lbs. 6 Jan 2003
var pz1  = new Array (0,  // USPS priority zone one
  3.85,  3.95,  4.75,  5.30,  5.85,  6.30,  6.80,  7.35,  7.90,  8.40,
  8.95,  9.50, 10.00, 10.55, 11.05, 11.60, 12.15, 12.65, 13.20, 13.75,
 14.25, 14.80, 15.30, 15.85, 16.40, 16.90, 17.45, 18.00, 18.50, 19.05,
 19.55, 20.10, 20.65, 21.15, 21.70, 22.25, 22.75, 23.30, 23.75, 24.25);
var pz2  = new Array (0,
  3.85,  3.95,  4.75,  5.30,  5.85,  6.30,  6.80,  7.35,  7.90,  8.40,
  8.95,  9.50, 10.00, 10.55, 11.05, 11.60, 12.15, 12.65, 13.20, 13.75,
 14.25, 14.80, 15.30, 15.85, 16.40, 16.90, 17.45, 18.00, 18.50, 19.05,
 19.55, 20.10, 20.65, 21.15, 21.70, 22.25, 22.75, 23.30, 23.75, 24.25);
var pz3  = new Array (0,
  3.85,  3.95,  4.75,  5.30,  5.85,  6.30,  6.80,  7.35,  7.90,  8.40,
  8.95,  9.50, 10.00, 10.55, 11.05, 11.60, 12.15, 12.65, 13.20, 13.75,
 14.25, 14.80, 15.30, 15.85, 16.40, 16.90, 17.45, 18.00, 18.50, 19.05,
 19.55, 20.10, 20.65, 21.15, 21.70, 22.25, 22.75, 23.30, 23.75, 24.25);
var pz4  = new Array (0, 
  3.85,  4.55,  6.05,  7.05,  8.00,  8.85,  9.80, 10.75, 11.70, 12.60,
 13.35, 14.05, 14.75, 15.45, 16.20, 16.90, 17.60, 18.30, 19.00, 19.75,
 20.45, 21.15, 21.85, 22.55, 23.30, 24.00, 24.70, 25.40, 26.15, 26.85,
 27.55, 28.25, 28.95, 29.70, 30.40, 31.10, 31.95, 32.65, 33.50, 34.30);
var pz5  = new Array (0,
  3.85,  4.90,  6.85,  8.05,  9.30,  9.90, 10.65, 11.45, 12.20, 13.00,
 13.75, 14.50, 15.30, 16.05, 16.85, 17.60, 18.35, 19.30, 20.20, 21.15,
 22.05, 22.95, 23.90, 24.85, 25.75, 26.60, 27.55, 28.50, 29.45, 30.35,
 31.20, 32.15, 33.10, 34.00, 34.95, 35.85, 36.80, 37.70, 38.65, 39.60);
var pz6  = new Array (0,
  3.85,  5.05,  7.15,  8.50,  9.85, 10.05, 11.00, 11.95, 12.90, 14.00,
 15.15, 16.30, 17.50, 18.60, 19.75, 20.85, 22.05, 23.15, 24.30, 25.35,
 26.55, 27.65, 28.80, 29.90, 31.10, 32.25, 33.35, 34.50, 35.60, 36.80,
 37.85, 39.00, 40.10, 41.25, 42.40, 43.55, 44.65, 45.85, 47.00, 48.10);
var pz7  = new Array (0,
  3.85,  5.40,  7.85,  9.45, 11.00, 11.80, 12.55, 13.80, 15.05, 16.30,
 17.55, 18.80, 20.05, 21.25, 22.50, 23.75, 25.00, 26.25, 27.50, 28.75,
 30.00, 31.20, 32.45, 33.70, 34.95, 36.20, 37.45, 38.70, 39.95, 41.20,
 42.40, 43.65, 44.90, 46.15, 47.40, 48.65, 49.90, 51.15, 52.40, 53.60);
var pz8  = new Array (0,
  3.85,  5.75,  8.55, 10.35, 12.15, 13.30, 14.55, 15.75, 17.50, 19.20,
 20.90, 22.65, 24.35, 26.05, 27.80, 29.50, 31.20, 32.95, 34.65, 36.40,
 38.10, 39.80, 41.55, 43.25, 44.95, 46.70, 48.40, 50.15, 51.85, 53.55,
 55.30, 57.00, 58.70, 60.45, 62.15, 63.85, 65.60, 67.30, 69.05, 70.75);

// UPS residential ground, lower 48 charges to 35 lbs. November 9,2006
var uz1 = new Array (0,7.70, 7.80, 8.15, 8.39, 8.60, 8.83, 9.07, 9.28, 9.53, 9.69, 9.90, 9.94, 10.07, 10.21, 10.42, 10.60, 10.81, 11.00, 11.24, 11.47, 11.69, 11.94, 12.17, 12.39, 12.61, 12.87, 13.08, 13.19, 13.41, 13.62, 13.83);
var uz2 = new Array (0,7.70, 7.80, 8.15, 8.39, 8.60, 8.83, 9.07, 9.28, 9.53, 9.69, 9.90, 9.94, 10.07, 10.21, 10.42, 10.60, 10.81, 11.00, 11.24, 11.47, 11.69, 11.94, 12.17, 12.39, 12.61, 12.87, 13.08, 13.19, 13.41, 13.62, 13.83);
var uz3 = new Array (0,8.04, 8.30, 8.50, 8.72, 8.93, 8.97, 9.32, 9.58, 9.83, 10.08, 10.35, 10.50, 10.79, 11.09, 11.40, 11.72, 12.03, 12.33, 12.64, 12.92, 13.23, 13.50, 13.75, 14.03, 14.29, 14.60, 14.87, 15.15, 15.44, 15.75, 16.07);
var uz4 = new Array (0,9.03, 9.16, 9.34, 9.53, 9.70, 9.90, 10.05, 10.23, 10.39, 10.56, 10.71, 10.85, 11.13, 11.44, 11.80, 12.15, 12.49, 12.87, 13.23, 13.61, 13.99, 14.34, 14.69, 15.08, 15.46, 15.86, 16.23, 16.60, 17.00, 17.36, 17.75);
var uz5 = new Array (0,9.32, 9.56, 9.83, 10.02, 10.23, 10.47, 10.71, 10.95, 11.16, 11.37, 11.59, 11.80, 12.21, 12.74, 13.24, 13.78, 14.28, 14.76, 15.23, 15.67, 16.09, 16.53, 16.97, 17.42, 17.88, 18.35, 18.80, 19.28, 19.77, 20.23, 20.69);
var uz6 = new Array (0,9.83, 10.09, 10.40, 10.70, 10.93, 11.31, 11.72, 12.18, 12.68, 13.29, 13.90, 14.36, 15.01, 15.64, 16.27, 16.91, 17.53, 18.17, 18.79, 19.43, 20.06, 20.71, 21.34, 21.97, 22.60, 23.24, 23.86, 24.49, 25.09, 25.72, 26.33);
var uz7 = new Array (0,10.16, 10.50, 10.85, 11.28, 11.80, 12.52, 13.29, 14.06, 14.84, 15.61, 16.39, 17.01, 17.75, 18.48, 19.22, 19.96, 20.69, 21.43, 22.15, 22.90, 23.66, 24.35, 25.03, 25.73, 26.50, 27.29, 28.06, 28.81, 29.60, 30.37, 31.15);
var uz8 = new Array (0,11.05, 11.37, 11.80, 12.50, 13.20, 13.99, 14.85, 15.82, 16.76, 17.71, 18.65, 19.40, 20.33, 21.27, 22.22, 23.17, 24.11, 25.03, 25.98, 26.92, 27.86, 28.76, 29.65, 30.59, 31.50, 32.44, 33.36, 34.31, 35.24, 36.16, 37.10);
var uzx  = new Array (0,uz1,uz2,uz3,uz4,uz5,uz6,uz7,uz8);

function CalcCost (wte) {  // wt & ZIP, calc cost
  zip = usrzip.substring(0, 3); // 1st 3 chars of zip
  for (var i=zn-1; i>=0; i--) { // run table backwards
    if (zip >= zzip[i]) {       // zip value
      zne = zone[i];            // corresponding zone
	  if (zip >= 430 && zip < 460) ttax = 8.0 * 1.0;
      break;                    // get out, now
    }
  }
  //if (wte >= uzx[zne].length) { // see if the weight is OK
	if (twgt >= 25) {
    
	//alert ("Your weight is beyond the shipping table!");

	boxfw = 25;				// full box weight
	boxtempn = twgt/boxfw;		// number of boxes
	boxpw = twgt % boxfw;		// partial box number
	//boxpw = boxpn * boxfw;		// partial box weight

	boxfn = Math.floor(boxtempn);	// full boxes
		
	boxfshpr = uzx[zne][boxfw];		// shipping for one full box
	tempshpr = boxfshpr * boxfn;		// shipping for one full boxe times number of boxes

	boxpshpr = uzx[zne][boxpw];
	shpr = tempshpr + boxpshpr + handchg;
	//alert ("got this far");


    return;
  }
  shpr = uzx[zne][wte] + handchg;         // charges to that zone
}


function CalcRoot () {  // calc root stuff
  if (root.shp) spos=0; 
  if (root.tax) tpos=0;
}

function CalcShip (wte) {  // see what the shipping charges are
  if (!root.shp) return;
  if (usrzip == 0) {       // entered his ZIP yet?
    usrzip = prompt ("Enter destination ZIP code...", "");
    if (isNaN(usrzip) || usrzip.length != 5) {
      alert ("Not a valid ZIP - try again...");
      return false;
    }
  }
  CalcCost (wte);
}

function DispTots () {  // display totals on the page
var tmp,d;
  d = document.orderf;
  CalcShip (twgt);
  //CalcSalesTax ();
  d.sub.value = Dollar (tamt);
  d.wgt.value = twgt;
  if (root.shp) d.shp.value = Dollar (shpr);
  tmp = (tamt + shpr) * ttax/100;
  if (root.tax) d.tax.value = Dollar (tmp);
  thnd = 0;                    // assume the worst;
  if (d.hnd) d.hnd.value = "0.00";
  if (tamt*1.0 < root.hamt) {  // calculate handling charge
      thnd = root.hand;        // figure it out
      if (d.hnd) d.hnd.value = Dollar (thnd);
  }
  d.tot.value = Dollar (tamt + shpr + tmp + thnd);
  if (d.zne) d.zne.value = "Zone = " + zne;
}

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 (".");  // should be one, but OK if not
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;               // return valid string
}

function GetOrder (id, des, amt, wgt) {  // get all ordered items
var i,nr,val,qty,pos;
var op = new Array ();    // accumulate options here
  tamt=0,tqty=0,twgt=0;   // clear totals
  nr = id.substring (2);  // get number part of ID
  qty = document.orderf["qty" + nr].value;  // get qty

  if (isNaN (qty)) {      // test entry
    alert ("That is not a valid number!  Try again.");
    return;
  }
  for (i=1; i<=opts; i++) {     // see if any options
    if (document.orderf["op" + i + nr]) {
      val = document.orderf["op" + i + nr].value;  // get option
      op[i] = val;
      pos  = val.indexOf ("+"); // price increment?
      if (pos > 0) amt = amt + val.substring (pos + 1)*1.0;
      pos  = val.indexOf ("%"); // percent change?
      if (pos > 0) amt = amt + (amt * val.substring (pos + 1)/100.0);
    }
    else op[i] = "";
  }
  document.orderf["prc" + nr].value = Dollar (qty * amt);
  document.orderf["toz" + nr].value = qty * wgt;
  if (cntr == 0) order = new Object (); // zap object
  cntr = cntr + 1;               // bump counter so no zap next time
  order[id] = new Object ();     // create new entry
  for (i=1; i<=opts; i++)        // load options
    if (op[i] != "") des = des + ", OP" + i + "=" + op[i];
  order[id].des = des;
  order[id].amt = Dollar (amt);  // load base stuff
  order[id].qty = qty;
  if (wgt) order[id].wgt = wgt;
    else order[id].wgt = 0;
  for (i in order) {             // calc totals we might use
    qty = order[i].qty*1.0;
    tamt = tamt + order[i].amt * qty;
    tqty = tqty + qty;
    twgt = twgt + order[i].wgt * qty;
  }
  twgt = Math.floor ((twgt + 15.99)/16.0);  // get pounds
// here is where to add shipping and other stuff based on cart totals
  
  DispTots ();
}


function CalcSalesTax () {

		 if (userzip > 42999 && userzip < 46000) ttax = tamt * .008;
	}
		 
function SendCart () {  // send the cart to PayPal


var frst = true;  // 1st pass thru items.
var winpar = "width=1024,height=768,scrollbars," +
             "location,resizable,status";


var strn   = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart" +
             "&upload=1" +
             "&business=" + root.xx_id + root.xx_xtra;
var i,j=0,des;
  if (root.xx_cur.length > 0)
    strn = strn + "&currency_code=" + root.xx_cur;
  if (root.xx_lc.length > 0)
    strn = strn + "&lc=" + root.xx_lc;
  if (root.xx_can.length > 0)
    strn = strn + "&cancel_return=" + root.xx_can;
  if (root.xx_ret.length > 0)
    strn = strn + "&return=" + root.xx_ret;
  if (root.xx_sty.length > 0)
    strn = strn + "&page_style=" + root.xx_sty;
  if (root.xx_img.length > 0)
    strn = strn + "&image_url=" + root.xx_img;
  if (ttax > 0) strn = strn + "&tax_cart=" + Dollar ((tamt +shpr) * ttax/100);
  for (i in order) {  // send all valid data
    if (order[i].qty > 0) {
      j = j + 1;
      des = order[i].des;
      if (j == 1) {  // put in descriptions for 1st item
        des = des + ", " + stxt + ", Zone=" + zne;
        des = des + ", TAX=" + ttxt;
        strn = strn + "&handling_cart=" + Dollar (thnd);
      }
      strn = strn + "&item_name_"    + j + "=" + escape (des) +
                    "&item_number_"  + j + "=" + i +
                    "&quantity_"     + j + "=" + order[i].qty +
                    "&amount_"       + j + "=" + order[i].amt;
      if (spos >= 0) {  // there is some shipping activity
        if (frst) {     // first time thru
          strn = strn + "&shipping_" + j + "=" + Dollar (shpr);
        } else {        // every other time thru
          strn = strn + "&shipping_" + j + "=0";
        }
      }
      frst = false;
    } 
  }
  if (j > 0) window.open (strn, "paypal", winpar);
}

function SetZone () {  // record zip/zone info into table
  zn = 0;                      // count of breakpoints
  for (var i=0; i<arguments.length; i=i+2) {
    zzip[zn] = arguments[i];   // zip code index
    zone[zn] = arguments[i+1]; // zone
    zn = zn + 1;               // number of bkpts
  }
}

function Shipper (obj1) {  // select shipping method
  spos = 1;
  //spos = obj1.selectedIndex;
  //if (spos == 1) {  // UPS ground
    
    uzx  = new Array (0,uz1,uz2,uz3,uz4,uz5,uz6,uz7,uz8);
    stxt = "UPS Ground";
    DispTots ();
  //}
  if (spos == 2) {  // USPS priority
    uzx  = new Array (0,pz1,pz2,pz3,pz4,pz5,pz6,pz7,pz8);
    stxt = "USPS Ground";
    DispTots ();
  }
}

function SetTax (obj) {
  //tpos = obj.selectedIndex;  // which option selected
  //ttxt = obj.options[tpos].text;
  //ttax = obj.options[tpos].value*1.0;
  ttax = 10 * 1.0;
  DispTots ();
}

// Must be executed at load time to build the table.
// build from 44149 zone chart
SetZone (441, 2,  // executed at load time
  4, 4,  10, 4,  42, 5,  50, 4, 100, 4,
130, 3, 133, 4, 140, 3, 147, 2, 148, 3,
150, 2, 159, 3, 160, 2, 166, 3, 167, 2,
168, 3, 180, 4, 182, 3, 183, 4, 184, 3,
189, 4, 200, 3, 214, 4, 215, 3, 216, 4,
217, 3, 218, 4, 220, 3, 230, 4, 240, 3,
260, 2, 262, 3, 263, 2, 266, 3, 270, 4,
299, 5, 300, 4, 313, 5, 330, 6, 334, 5,
341, 5, 350, 4, 354, 5, 355, 4, 360, 5,
362, 4, 363, 5, 368, 4, 369, 5, 370, 4,
387, 5, 388, 4, 389, 5, 399, 4, 400, 3,
407, 4, 410, 3, 420, 4, 425, 3, 430, 2,
450, 3, 453, 2, 459, 3, 475, 4, 479, 3,
480, 2, 488, 3, 492, 2, 493, 3, 497, 4,
500, 5, 504, 4, 505, 5, 506, 4, 508, 5,
520, 4, 540, 5, 541, 4, 550, 5, 559, 4,
560, 5, 577, 6, 580, 5, 586, 6, 594, 7,
600, 3, 602, 4, 603, 3, 610, 4, 640, 5,
650, 4, 653, 5, 654, 4, 656, 5, 677, 6,
678, 5, 679, 6, 680, 5, 693, 6, 700, 5,
723, 4, 725, 5, 733, 6, 734, 5, 739, 6,
740, 5, 760, 6, 762, 5, 763, 6, 798, 7,
800, 6, 815, 7, 816, 6, 821, 7, 822, 6,
829, 7, 854, 8, 855, 7, 870, 6, 873, 7,
875, 6, 878, 7, 881, 6, 885, 7, 889, 8,
893, 7, 894, 8, 898, 7, 900, 8, 970, 8,
979, 7, 980, 8, 990, 7, 993, 8, 994, 7
)
