// JavaScript Document
var blk1  = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart";
var blk1a = "&add=1";
var blk1d = "&display=1";
var blk2  = "&business=";
var blk2a = "&quantity=";
var blk2q = "";
var blk3  = "&item_name=";
var blk3n = "";
var blk4  = "&amount=";
var blk4a = "";
var bcode = "";  // item number
var blkcc = "&currency_code=USD";
var op1n  = "";  // option name and value
var op1v  = "";
var op2n  = "";
var op2v  = "";
var winpar = "width=600,height=400,scrollbars," +
             "location,resizable,status";

var dqty = new Array ();  // quantity breakpoint
var damt = new Array ();  // percent discount
var dn   = 0;             // number of discount brkpts

var coup = false;         // discount coupon not active
var coupdis = 0;          // amount of coupon discount
var coupval = "";         // coupon value user entered
var coupons = new Array ();
coupons[0] = "coup1";
coupons[1] = "coup2";

function AddDesc (strn) {  // add to current description
var c;
  if (blk3n.length > 0) c = ",+"; else c = "";
  blk3n = blk3n + escape (c + Coder (strn));
}

function AddOpt1 (val) {  // add to the value in op1v
var c;
  if (op1n.length == 0) op1n = "opt1";
  if (op1v.length > 0) c = ", "; else c = "";
  op1v = op1v + escape (c + Coder (val));
}

function AddOpt2 (val) {  // add to the value in op2v
var c;
  if (op1n.length == 0) {  // fill 1st field 1st
    alert ("\n\n  Error - filling opt1 first!  \n\n");
    AddOpt1 (val);
    return;
  }
  if (op2n.length == 0) op2n = "opt2";
  if (op2v.length > 0) c = ", "; else c = "";
  op2v = op2v + escape (c + Coder (val));
}

function AddPrcnt (strn) {  // add a percent to the price
  SetPrice (blk4a * (1.0 + strn/100.0));  // add the percent
}

function AddPrice (strn) {  // add to current price
var pos;
  strn = escape (blk4a*1.0 + strn*1.0 + 0.005);
  pos = strn.indexOf ("."); // find decimal point
  if (pos > 0) strn = strn.substring (0, pos + 3);  // lop off extra
  blk4a = strn;             // stash new price
}

function CallPay () { // call the PayPal shopping cart
var strn;             // where to build the PayPal string
  strn = blk1 + blk1a + blk2 + blk2a + blk2q + 
         blk3 + blk3n + blk4 + blk4a + blkcc + bcode +
         op1n + op1v + op2n + op2v;
  ClearAll ();        // clear all the data
  window.open (strn, "paypal", winpar);
}

function CallView () { // call the PayPal shopping cart view
  window.open (blk1 + blk1d + blk2, "paypal", winpar);
}

function ChkCoup (amt) {  // check for a discount coupon
var i;
  coup = false;       // assume the worst
  val = coupval.toLowerCase ();
  for (i=0; i<coupons.length; i++) {
    if (val == coupons[i]) {
      coup = true;    // user hit the coupon value
      coupdis = amt;  // remember the discount amt
      alert ("\n\nValid coupon number  \n\n");
      return;
    }
  }
  alert ("\n\n'" + val + "'  not a valid code!  \n\n");
}

function ChkFlg (temp) {     // check for special flag char
  pos  = temp.indexOf ("@"); // is there a initial value?  
  if (pos > 0) SetPrice (temp.substring (pos + 1));
  pos  = temp.indexOf ("+"); // is there a price adjustment?  
  if (pos > 0) AddPrice (temp.substring (pos + 1));
  pos  = temp.indexOf ("%"); // is there a percent adjustment?  
  if (pos > 0) AddPrcnt (temp.substring (pos + 1));
}

function ClearAll () {  // wipe out the last entry
  blk2q = "";  // quantity
  blk3n = "";  // name
  blk4a = "";  // price
  bcode = "";  // item number
  op1n = "";   // clear options
  op1v = "";
  op2n = "";
  op2v = "";
}

function Coder (strn) {     //replace spaces with "+"
var aray = new Array ();    // we need array support stuff
  aray = strn.split (" ");  // delete all spaces
  return aray.join ("+");   // replace with "+"
}

function SetCode (cd) { // set product code
  bcode = "&item_number=" + escape (Coder (cd));
}

function SetDesc (strn) {  // set the desc field
  blk3n = escape (Coder (strn));
  
}

function SetOpt1 (nam, val) {  // set the value of 1st option
  op1n = "&on0=" + escape (Coder (nam));
  op1v = "&os0=" + escape (Coder (val));
}

function SetOpt2 (nam, val) {  // set the value of 2nd option
  if (op1n == "") {
    alert ("\n\n  Fill option1 first!  \n\n");
    return;
  }
  op2n = "&on1=" + escape (Coder (nam));
  op2v = "&os1=" + escape (Coder (val));
}

function SetPrice (strn) {  // set the current price
  blk4a = 0;
  AddPrice (strn);
}

function SetQtyD (q1, d1) {  // set qty discount breakpoints
var i;
  dn = 0;
  for (i=0; i<arguments.length; i=i+2) {
    dqty[dn] = arguments[i];
    damt[dn] = arguments[i+1];
    dn = dn + 1;  // number of discount bkpts
  }
}

// site specific code

function ReadForm (obj1) { // get form data for PayPal
var i,j,obj,temp,pos,val,nam3,nam4;
var qty  = 0;              // default value
var dis  = 0;              // quantity discount
var acnt = 0;              // textarea count (for storage
  for (i=0; i<obj1.length; i++) {     // run whole form
    obj = obj1.elements[i];           // ref particular element
    nam3 = obj.name.substring (0, 3); // 3-char name (maybe)
    nam4 = obj.name.substring (3, 4); // where to store it
    if (obj.type == "select-one") {   // dropdowns
      pos = obj.selectedIndex;        // which option selected
      val = obj.options[pos].value;   // get selection
      ChkFlg (val);                   // check for flag char
      if (nam3 == "opt") {            // user says where to store
        Where (val, nam4);            // stash it
      } else {
        AddDesc (val);                // add to data
      }
    } else
    if (obj.type == "select-multiple") {     // one or more
      for (j=0; j<obj.options.length; j++) { // run all options
        if (obj.options[j].selected) {
          val = obj.options[j].value;
          ChkFlg (val);                   // flag chars?
          if (nam3 == "opt") {            // user says where to store
            Where (val, nam4);            // stash it
          } else {
            AddDesc (val);                // add to data
          }
        }
      }
    } else
    if (obj.type == "checkbox" ||    // boxes
        obj.type == "radio") {
      if (obj.checked) {             // was selected
        val = obj.value;
        ChkFlg (val);                // flag chars?
        if (nam3 == "opt") {         // user says where to store
          Where (val, nam4);         // stash it
        } else {
          AddDesc (val);             // add to data
        }
      }
    } else
    if (obj.type == "text") {  // user input fields
      val = obj.value;         // get input
      if (qty == 0) {          // this is 1st time
        qty = val;             // get user input
        if (qty == "" || qty < 1 || isNaN (qty)) {  // test
          alert ("\n\n  Enter a valid integer quantity!  \n\n");
          return;
        }
      } else {
        if (nam3 == "opt") {   // user says where to store
          Where (val, nam4);   // stash it
        } else {
          AddDesc (val);       // add to data
        }
      }
    } else
    if (obj.type == "textarea" &&  // textarea stuff (beware)
        obj.value.length > 0) {
      acnt = acnt + 1;            // areas encountered
      if (acnt > 2) {  // too many areas
        alert ("\n\n  Only 2 textareas may be input!  \n\n");
        return;
      }
      if (acnt == 1) {  // something there, check size
        if (obj.value.length + op1v.length > 200) {
          alert ("\n\n  Too many textarea1 characters!  \n\n");
          return;
        }
        if (op1n.length == 0) {     // nothing in 1st field
          SetOpt1 ("TextArea 1", obj.value);
        } else
          AddOpt1 (obj.value);
      } else
      if (acnt == 2) {
        if (obj.value.length + op2v.length > 200) {
          alert ("\n\n  Too many textarea2 characters!  \n\n");
          return;
        }
        if (op2n.length == 0) {     // nothing in second field
          SetOpt2 ("TextArea 2", obj.value);
        } else
          AddOpt2 (obj.value);
      }
    }
  }
  if (qty == 0) qty = 1; // make sure we have something
  blk2q = qty;           // record for posterity

  dis = 0;                   // any qty discounts?
  for (i=dn-1; i>=0; i--) {  // run backwards
    if (qty >= dqty[i]) {    // qty brkpt
      dis = damt[i];         // set qty amount
      break;                 // get out, now
    }
  }
  if (coup) {                // any valid coupons?
    dis = dis*1.0 + coupdis*1.0;
    AddDesc ("COUP");        // mark it
  }
  if (dis > 0) {             // there is an item discount, here
    AddPrcnt (-dis);         // apply the discount
    AddDesc ("QTY=" + qty + ",+DIS=" + dis + "%.");  // mark it
  }
}

function Where (val, loc) {  // store val at opt[loc]
  if (loc < 1 || loc > 2) {
    alert ("\n\nYour name is wrong - storing in OPT1!  \n\n");
    AddOpt1 (val);
    return;
  }
  if (loc == 1) AddOpt1 (val);
  else          AddOpt2 (val);
}


// site specific code...

function GetArgs () { // get the calling query string data
/* This function parses comma-separated name=value argument pairs
from the query string of the URL. It stores the pairs in properties
of an object which is returned to the caller. From David Flanagan's
"JavaScript" book (O'Reilly), example 13-5.
*/
var args = new Object (); // where to put the data
var i,pos,argname,value;
var query = location.search.substring(1); // get query string
var pairs = query.split (",");     // break at comma
  for (i=0; i<pairs.length; i++) {
    pos = pairs[i].indexOf ("=");  // look for name=value
    if (pos == -1) continue;       // skip if not found
    argname = pairs[i].substring (0, pos); // get name
    value = pairs[i].substring (pos + 1);  // get value
    args[argname] = value;         // store as property
  }
return args; // return object
}

// executed immediately after script loads
var args = GetArgs (); // see what user passed
SetDesc ("");
if (args.iname)  {SetDesc (args.iname);} // load 'em up

//SetPrice (0);
//if (args.iamt)   {SetPrice (args.iamt);}
