var order = new Object();  // object to store order items in
var cntr  = 0;             // items in object
var opts  = 5;             // number of options to allow
var shpr  = function (wt) {return 0;} // what to charge for shipping;
var stxt  = "";            // shipping text
var spos  = 0;             // shipping position selector
var tamt=0,tqty=0,twgt=0,wgt=0;  // totals

function DispTots () {  // display totals on the page
  document.orderf.sub.value = "Sub = " + Dollar (tamt);
  document.orderf.wgt.value = "Lbs = " + twgt;
  document.orderf.shp.value = "Shp = " + Dollar (shpr (twgt));
  document.orderf.tot.value = "Tot = " + Dollar (tamt + shpr (twgt));
}

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;   // zero totals
  nr = id.substring (2);  // get number part of ID
  qty = document.orderf["qty" + nr].value;  // get qty. if I'm testing code and it doesn't work properly then "" should be "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
var winpar = "width=710,height=390,scrollbars," +
             "location,resizable,status";
var strn   = "https://www.paypal.com/cgi-bin/webscr" +
             "?cmd=_cart" +
             "&upload=1" +
             "&business=forestwalker333@hotmail.com" +
             "&handling_cart=" + Dollar (shpr (twgt)) +
             "&currency_code=USD" +
             "&lc=US";
var i,j=0,des;
  for (i in order) {  // send all valid data
    if (order[i].qty > 0) {
      j = j + 1;
      des = order[i].des;
      if (j == 1) des = des + ", SHP=" + stxt;
      strn = strn + "&item_name_"   + j + "=" + escape (des) +
                    "&item_number_" + j + "=" + i +
                    "&quantity_"    + j + "=" + order[i].qty +
                    "&amount_"      + j + "=" + order[i].amt +
                    "&shipping_"    + j + "=0";
    }
  }
  if (j > 0) window.open (strn, "paypal", winpar);
}

function SetShp (obj1) {  // set shipping on user selection
  shpr = function (wt) {return 0;}  // zap it
  spos = obj1.selectedIndex;  // which option selected
  stxt = obj1.options[spos].text;
  // set various shipping functions.
  if (spos == 1) shpr = function (wt) {return 5.95;}
  if (spos == 2) shpr = function (wt) {return 11.90;}
  if (spos == 3) shpr = function (wt) {return 6.95;}
  if (spos == 4) shpr = function (wt) {return 13.90;}
  if (spos == 5) shpr = function (wt) {return 9.95;}
  if (spos == 6) shpr = function (wt) {return 19.90;}
  if (spos == 7) shpr = function (wt) {return 10.95;}
  if (spos == 8) shpr = function (wt) {return 21.90;}
  if (spos == 9) shpr = function (wt) {return 12.95;}
  if (spos == 10) shpr = function (wt) {return 25.90;}
  if (spos == 11) shpr = function (wt) {return 14.95;}
  if (spos == 12) shpr = function (wt) {return 29.90;}
  if (spos == 13) shpr = function (wt) {return 16.95;}
  if (spos == 14) shpr = function (wt) {return 33.90;}
  if (spos == 15) shpr = function (wt) {return 19.95;}
  if (spos == 16) shpr = function (wt) {return 39.90;}
  DispTots ();
}

