// Coggins shopping cart 2005;
var blk1  = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart";
var blk1a = "&add=1";
var blk1d = "&display=1";
var blk2  = "&business=rcoggins@cox.net";
var blk2a = "&quantity=";
var blk2q = "";
var blk3  = "&item_name=";
var blk3n = "Test";
var blk4  = "&amount=";
var blk4a = "6.66";
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

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 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 aray = new Array ();   // where to build description
var qty  = 0;              // default value
var n    = 0;              // index for array entry
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
      if (nam3 == "opt") {            // user says where to store
        Where (val, nam4);            // stash it
      } else {
        aray[n] = val; // save it
        n = n + 1;     // bump counter
      }
    } 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;
          if (nam3 == "opt") {            // user says where to store
            Where (val, nam4);            // stash it
          } else {
          aray[n] = val;
          n = n + 1;
          }
        }
      }
    } else
    if (obj.type == "checkbox" ||    // boxes
        obj.type == "radio") {
      if (obj.checked) {             // was selected
        val = obj.value;
        if (nam3 == "opt") {         // user says where to store
          Where (val, nam4);         // stash it
        } else {
          aray[n] = val;             // record it
          n = n + 1;
        }
      }
    } else
    if (obj.type == "text") {  // user input fields
      if (qty == 0) {          // this is 1st time
        qty = obj.value;       // get user input
        if (qty == "" || qty < 1 || isNaN (qty)) {  // test
          alert ("\n\n  Enter a valid integer quantity!  \n\n");
          return;
        }
      } else {
        val = obj.value;
        if (nam3 == "opt") {   // user says where to store
          Where (val, nam4);   // stash it
        } else {
          aray[n] = val;       // else just add to desc field
          n = n + 1;
        }
      }
    } 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 ("Group", 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);
      }
    }
  }

  for (i=0; i<n; i++) {  // run whole array for prices
    temp = aray[i];      // isolate it for testing
    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));
  }

  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
    }
  }

  AddDesc (Coder (aray.join (",+")));  // build desc
  if (dis > 0) {             // there is an active 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\n  Your name is wrong - storing in OPT1!  \n\n");
    AddOpt1 (val);
    return;
  }
  if (loc == 1) AddOpt1 (val);
  else          AddOpt2 (val);
}
