var order = new Object();  // object to store order items 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  = function () {return 0;} // what to charge for shipping;
var stxt  = "";            // shipping text
var spos  = -1;            // shipping position selector
var ttax  = 0;             // percent for taxes
var ttxt  = "0";           // tax text
var tpos  = -1;            // tax position selector
var tamt=0,tqty=0,twgt=0,wgt=0,thnd=0;  // totals

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  = off;   // 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   = ""; // 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


function ShpCheck () { // user can't select a shipping method until a qty has been selected
	if ( (order['01D0405']==undefined) && (order['02GL1206']==undefined) ){
	alert("Please select a quantity for a book first before selecting a Shipping Method. \n Thank you.");
	document.orderf.qty01D0405.focus();
	document.orderf.prc01D0405.focus();
	document.orderf.qty01D0405.focus();
	}
}

function CalcRoot () {  // calc root stuff
  if (root.shp) spos=0; 
  if (root.tax) tpos=0;

  if (cntr == 0) order = new Object (); // zap object
  cntr = cntr + 1;               // bump counter so no zap next time
}

function DispTots () {  // display totals on the page
var tmp,d;
  d = document.orderf;
  d.sub.value = Dollar (tamt);
  d.wgt.value = twgt;
  if (root.shp) d.shp.value = Dollar (shpr ());
  	tmp = tamt * 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 (twgt) + tmp + thnd);
  d.tot.value = Dollar (tamt + shpr() );
  //alert ( "Sub: "+d.sub.value +"\n"+"Shipping: "+Dollar( shpr())+"\nTotal: "+d.tot.value );
}

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);
  if (pos == 0) str = '0' + str;
  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;   // zero totals
  nr = id;  // 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;           // load up values
  order[id].amt = Dollar (amt);
  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;  // total amount
    tqty = tqty + qty;                 // total quantity
    twgt = twgt + order[i].wgt * qty;  // total ounces
  }
  twgt = Math.floor ((twgt + 15.99)/16.0);  // get pounds
 
  DispTots ();                         // calc totals
}

function SendCart () {  // send the cart to PayPal
xx1="payp";
xx2="al@merl";
xx3="publications.com";
var frst = true;  // 1st pass thru items.
var winpar = "width=710,height=390,scrollbars," +
             "location,resizable,status";
var strn   = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart" +
             "&upload=1" +
             "&business=" + xx1 + xx2 +xx3 + 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 (tpos > 0) strn = strn + "&tax_cart=" + Dollar (tamt*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 + ", SHP=" + stxt;
        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 SetShp (obj) {  // set shipping on user selection
  shpr = function () {return 0;}  // zap it
  spos = obj.selectedIndex;  // which option selected
  stxt = obj.options[spos].text;

  // set various shipping functions.
 


  if (-1 == spos) shpr = function () { return 0.00 } // null state
 
  else if (spos == 0) shpr = function () { // if set to US "Standard" Priority rate

	   // Priority Mail Rates
		var p_rates = new Array()
		p_rates[0]= new Array(0   ,1.7 ,2.6 ,4.6 ,4.6 ,4.6 ,4.6 ,4.6 ,4.6 ,8.9 ,8.9 ,8.9 ,8.9 ,8.9 ,8.9 ,8.9 ), 
		p_rates[1]= new Array(4.6 ,4.6 ,4.6 ,4.6 ,4.6 ,6.2 ,6.2 ,6.2 ,8.3 ,8.3 ,8.3 ,8.3 ,10.2,10.2,10.2,10.2),
		p_rates[2]= new Array(6   ,6.2 ,6.2 ,8.3 ,8.3 ,10.2,10.2,10.2,10.2,11.9,11.9,11.9,11.9,13  ,13  ,13  ), 
		p_rates[3]= new Array(8.9 ,8.9 ,8.9 ,8.9 ,8.9 ,8.9 ,11.9,12.9,12.9,12.9,12.9,13.9,13.9,13.9,13.9,14.9),
		p_rates[4]= new Array(8.9 ,8.9 ,8.9 ,8.9 ,8.9 ,12.9,13.9,13.9,13.9,14.9,14.9,14.9,14.9,15.9,15.9,15.9),
		p_rates[5]= new Array(13.9,13.9,13.9,13.9,14.9,14.9,14.9,14.9,15.9,15.9,15.9,15.9,16.9,16.9,16.9,16.9),
		p_rates[6]= new Array(14.9,14.9,15.9,15.9,15.9,15.9,16.9,16.9,16.9,16.9,17.9,17.9,17.9,17.9,18.9,18.9),
		p_rates[7]= new Array(16.9,16.9,16.9,16.9,17.9,17.9,17.9,17.9,18.9,18.9,18.9,18.9,20  ,20  ,20  ,20  ),
		p_rates[8]= new Array(17.9,17.9,18.9,18.9,18.9,18.9,20  ,20  ,20  ,20  ,20.9,20.9,20.9,20.9,21.5,21.5),
		p_rates[9]= new Array(18.9,20  ,20  ,20  ,20  ,20.9,20.9,20.9,20.9,21.5,21.5,21.5,21.5,22  ,22  ,22  ), 
		p_rates[10]=new Array(20.9,20.9,20.9,21.5,21.5,21.5,21.5,22  ,22  ,22  ,22  ,22.5,22.5,22.5,22.5,23  ), 
		p_rates[11]=new Array(21.5,22  ,22  ,22  ,22  ,22.5,22.5,22.5,22.5,23  ,23  ,23  ,23  ,23.5,23.5,23.5),
		p_rates[12]=new Array(22.5,22.5,22.5,22.5,23  ,23  ,23  ,23  ,23.5,23.5,23.5,23.5,23.5,23.9,23.9,23.9),
		p_rates[13]=new Array(23  ,23  ,23.5,23.5,23.5,23.5,23.9,23.9,23.9,23.9,24.3,24.3,24.3,24.3,24.9,24.9),
		p_rates[14]=new Array(23.9,23.9,23.9,23.9,24.3,24.3,24.3,24.3,24.9,24.9,24.9,24.9,25.3,25.3,25.3,25.3),
		p_rates[15]=new Array(24.3,24.3,24.9,24.9,24.9,24.9,25.3,25.3,25.3,25.3,25.8,25.8,25.8,25.8,26.2,26.2)
	  //
	  return p_rates[order['01D0405'].qty][order['02GL1206'].qty];
  }
  else if (spos == 1) shpr = function () { // if set to US Media Mail rate
		var m_rates = new Array()
		m_rates	[0]= new Array(0,2.1,2.1,2.1,2.1,2.1,2.1,2.5,2.5,2.5,2.8,2.8,2.8,3.2,3.2,3.2), 
		m_rates	[1]= new Array(2.5,2.5,2.5,2.5,2.5,2.5,2.5,2.8,2.8,2.8,2.8,3.2,3.2,3.2,3.2,3.5),
		m_rates	[2]= new Array(3,3,3,3,3.2,3.2,3.2,3.2,3.5,3.5,3.5,3.5,3.8,3.8,3.8,3.8 ), 
		m_rates	[3]= new Array(3.5,3.5,3.5,3.5,3.8,3.8,3.8,3.8,4.2,4.2,4.2,4.2,4.5,4.5,4.5,4.5),
		m_rates	[4]= new Array(4,4,4,4,4.2,4.2,4.2,4.2,4.5,4.5,4.5,4.5,4.8,4.8,4.8,4.8),
		m_rates	[5]= new Array(4.5,4.5,4.5,4.5,4.8,4.8,4.8,4.8,5,5,5,5,5.2,5.2,5.2,5.2),
		m_rates	[6]= new Array(5,5,5,5,5.2,5.2,5.2,5.2,5.5,5.5,5.5,5.5,5.8,5.8,5.8,5.8),
		m_rates	[7]= new Array(5.5,5.5,5.5,5.5,5.8,5.8,5.8,5.8,6,6,6,6,6.2,6.2,6.2,6.2 ),
		m_rates	[8]= new Array(6,6,6,6,6.2,6.2,6.2,6.2,6.5,6.5,6.5,6.5,6.8,6.8,6.8,6.8),
		m_rates	[9]= new Array(16.2,6.2,6.2,6.2,6.5,6.5,6.5,6.5,6.8,6.8,6.8,6.8,7,7,7,7 ), 
		m_rates	[10]=new Array(6.5,6.5,6.5,6.5,6.8,6.8,6.8,6.8,7,7,7,7,7.2,7.2,7.2,7.2  ), 
		m_rates	[11]=new Array(7,7,7,7,7.2,7.2,7.2,7.2,7.5,7.5,7.5,7.5,8,8,8,8),
		m_rates	[12]=new Array(7.5,7.5,7.5,7.5,8,8,8,8,8.2,8.2,8.2,8.2,8.5,8.5,8.5,8.5),
		m_rates	[13]=new Array(8,8,8,8,8.2,8.2,8.2,8.2,8.5,8.5,8.5,8.5,9,9,9,9),
		m_rates	[14]=new Array(8.5,8.5,8.5,8.5,9,9,9,9,9.2,9.2,9.2,9.2,9.5,9.5,9.5,9.5),
		m_rates	[15]=new Array(9,9,9,9,9.2,9.2,9.2,9.2,9.5,9.5,9.5,9.5,10,10,10,10)
	  //
	  return m_rates[order['01D0405'].qty][order['02GL1206'].qty];
  }


  else if (spos == 2) shpr = function () {    // Canadian Ground Rate
	  //
		var c_rates = new Array()
		c_rates[0]= new Array(0,1.7,3,3.8,4.25,4.25,7.5,7.5,7.5,8.85,10,10.8,11.65,12.7,13.6,14.6), 
		c_rates[1]= new Array(9,9,9,9,9,11,12,13,14,15,16,17,18,20,21,22),
		c_rates[2]= new Array(9,11,12,13,14,15,16,17,18,20,21,22,23,24,25,26), 
		c_rates[3]= new Array(20,20.5,21,21.5,22,22.5,23,23.5,24,24.5,25,25.5,26,26.5,27,27.5),
		c_rates[4]= new Array(21,21.5,22,22.5,23,23.5,24,24.5,25,25.5,26,26.5,27,27.5,28,28.5),
		c_rates[5]= new Array(24,24.5,25,25.5,26,26.5,27,27.5,28,28.5,29,29.5,30,30.5,31,31.5),
		c_rates[6]= new Array(25,25.5,26,26.5,27,27.5,28,28.5,29,29.5,30,30.5,31,31.5,32,32.5),
		c_rates[7]= new Array(28,28.5,29,29.5,30,30.5,31,31.5,32,32.5,33,33.5,34,34.5,35,35.5),
		c_rates[8]= new Array(29,29.5,30,30.5,31,31.5,32,32.5,33,33.5,34,34.5,35,35.5,36,36.5),
		c_rates[9]= new Array(31,31.5,32,32.5,33,33.5,34,34.5,35,35.5,36,36.5,37,37.5,38,38.5), 
		c_rates[10]=new Array(33,33.5,34,34.5,35,35.5,36,36.5,37,37.5,38,38.5,39,39.5,40,40.5), 
		c_rates[11]=new Array(35,35.5,36,36.5,37,37.5,38,38.5,39,39.5,40,40.5,41,41.5,42,42.5),
		c_rates[12]=new Array(38,38.5,39,39.5,40,40.5,41,41.5,42,42.5,43,43.5,44,44.5,45,45.5),
		c_rates[13]=new Array(39,39.5,40,40.5,41,41.5,42,42.5,43,43.5,44,44.5,45,45.5,46,46.5),
		c_rates[14]=new Array(42,42.5,43,43.5,44,44.5,45,45.5,46,46.5,47,47.5,48,48.5,49,49.5),
		c_rates[15]=new Array(43,43.5,44,44.5,45,45.5,46,46.5,47,47.5,48,48.5,49,49.5,50,50.5)
	  //
	  return c_rates[order['01D0405'].qty][order['02GL1206'].qty];
  }
  else  return 0.00;


//  else if (spos == 3) shpr = function () {    // Global Priority Flat Rate
//	  if ( (order['01D0405'].qty == 0) && (order['02GL1206'].qty == 0) ) return 0.00;  // both back to zero
//      else if ( (order['01D0405'].qty > 0) && (order['02GL1206'].qty > 0) ) { //both dictionary and glossay ordered
//		     if (order['01D0405'].qty ==  1) {
//				if (order['02GL1206'].qty < 6 ) return (9.00);
//				else if (3 <= order['02GL1206'].qty  &&  order['02GL1206'].qty <= 10 ) return (18.00);
//		}
//		else if (order['01D0405'].qty ==  2) {
//				if (order['02GL1206'].qty < 2 ) return (9.00);
//				else return (0.00);
//		}		
//	  }	
//	  else  return 0.00;
//  }

  //alert("Hit else\nshpr() = " + shpr());
  //alert("shpr() = "+shpr() );

  DispTots ();
}

function SetTax (obj) {
  tpos = obj.selectedIndex;  // which option selected
  ttxt = obj.options[tpos].text;
  ttax = obj.options[tpos].value*1.0;
  DispTots ();
}