/* Support construction of dynamic PayPal args. This script is used in robes.htm */
var blk1  = "https://www.paypal.com/cgi-bin/webscr" +
            "?cmd=_cart";
var blk1a = "&add=1";
var blk1d = "&display=1";
var blk2  = "&business=saulhaffner%40jpus.org";
var blk2a = "&quantity=";
var blk2q = "";
var blk3  = "&item_name=";
var blk3n = "Test";
var opt0a = "&on0=Test";
var opt0b = "&os0=";
var opt0v = "test";
var opt1a = "&on1=Test";
var opt1b = "&os1=";
var opt1v = "test";
var blk4  = "&amount=";
var blk4a = "6.66";
var blk5  = "&image_url=http%3a//" + 
     "jpus.org/images/kristina/logosm.gif"
var winpar = "width=600,height=400,scrollbars," +
             "location," +  // some users delete this
             "resizable,status";

function AddBoth (strn1, strn2, arg) {  // add to both fields
  AddStyle (strn1, "0");  // add to description
  AddPrice (strn2, arg);  // add to price
}

function AddPrice (strn, arg) {  // add to current price
var r1,r2,pos;
  r1 = blk4a * 1.0 + 0.005; // float 'em
  r2 = strn * 1.0;
  strn = escape (r1 + r2);  // add and put back to string
  pos = strn.indexOf ("."); // find decimal point
  blk4a = strn.substring (0, pos + 3);  // lop off extra
  if (arg != "0") CallPay ();
}

function AddStyle (strn, arg) {  // add to current description
  blk3n = blk3n + "%2C%20" + escape (strn);
  if (arg != "0") CallPay ();
}

function CallPay () { // call the PayPal shopping cart
  window.open (blk1 + blk1a + // open the cart window
               blk2 + blk2a + blk2q + 
//               opt0a + opt0b + opt0v +  // option 0 field
//               opt1a + opt1b + opt1v +  // option 1 field
               blk3 + blk3n + blk4 + blk4a + blk5,
               "paypal",
               winpar);
}

function CallView () { // call the PayPal shopping cart view
  window.open (blk1 + blk1d +  // open the PayPal cart window
               blk2,
               "paypal",
               winpar);
}

function SetBoth (strn1, strn2, arg) {  // set desc and value
  SetDesc  (strn1);
  SetPrice (strn2);
  if (arg != "0") CallPay ();
}

function SetDesc (strn) {  // set the desc field
  blk3n = escape (strn);
}

function SetPrice (strn) {  // set the current price
  blk4a = "0.00";
  AddPrice (strn, "0");
}

// site specific code

function MakeBuy (obj1) {  //get form data for PayPal
var i,j,obj,temp,pick,pos;
var aray = new Array ();
var qty = 1;
var n = 0;
  for (i=0; i<obj1.length; i++) {    // run whole form
    obj = obj1.elements[i];  // ref particular element
    if (obj.type == "select-one" ||  // dropdowns
        obj.type == "select-multiple") {
      for (j=0; j<obj.options.length; j++) {  // run all options
        if (obj.options[j].selected) {
          aray[n] = obj.options[j].value;
          n = n + 1;
        }
      }
    } else
    if (obj.type == "checkbox" ||    // boxes
        obj.type == "radio" ||
        obj.type == "button") {
      if (obj.checked) {
        aray[n] = obj.value;
        n = n + 1;
      }
    } else
    if (obj.type == "text") {  // user input fields
      if (qty == 0) {
        qty = obj.value;
       
      } else {
      aray[n] = obj.value;
      n = n + 1;
      }
    }
  }

  // run array and test for prices...
  SetPrice (6.66);  // just to make sure it works
  for (i=0; i<n; i++) {
    temp = aray[i];  // isolate it
    pos  = temp.indexOf ("@$"); // is there a initial value?  
    if (pos > 0) {SetPrice (temp.substring (pos + 2));}

    pos  = temp.indexOf ("+$"); // is there an adjustment?  
    if (pos > 0) {AddPrice (temp.substring (pos + 2), "0");}
  }

  if (qty == 0) qty = 1;
  blk2q = qty;

  SetDesc (aray.join (", "));  // build desc
  CallPay ();  // put into cart
}
