// JavaScript Document
// Global variables
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 tamt=0,tqty=0;  // totals



// place for user-specific options
root.xx_can  = "https://www.bridgeyouth.org/galaregistration/2010Gala.php"; // place for PayPal cancel return path
root.xx_cur  = "USD"; // enter default currency code (or null)
//root.xx_id   = "testing@danicoenterprises.com"; // PayPal ID
root.xx_id   = "onlinepayments@bridgeyouth.org";  // PayPal ID *******<<<<<<<<<<<<<<<<
root.xx_img  = "https://www.bridgeyouth.org/images/bridgeheader.jpg"; // image URL
root.xx_lc   = "US"; // enter default country code (or null)
root.xx_ret  = "https://www.bridgeyouth.org/galaregistration/2010GalaRegistrationConfirmation.php"; // place for PayPal return path
root.xx_sty  = "BridgeFrame"; // place for PayPal page style
root.xx_xtra = "&rm=1"; // place for other PayPal commands

function DispTots () 
{  // display totals on the page
var tmp,d;
  d = document.apForm;
//  d.sub.value = Dollar (tamt);
//   
//  d.tot.value = Dollar (tamt);
  d.RegTotal.value = Dollar (tamt);
}

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) {  // get all ordered items
var i,nr,val,qty,pos;
var op = new Array ();    // accumulate options here
  tamt=0,tqty=0;   // zero totals
  nr = id.substring (2);  // get number part of ID
  qty = document.apForm["qty" + nr].value;  // get qty
 // alert("in getorder, nr="+nr);
  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.apForm["op" + i + nr]) 
	{  
	
      val = document.apForm["op" + i + nr].value;  // get option
	  //alert ("val= "+val);
      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.apForm["prc" + nr].value = Dollar (qty * amt);
 

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;

 //alert("des= "+des);

  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
  }
  //alert("calling form DispTots from GetOrder");
   DispTots ();                         // calc totals
}

function GetDonation (id, des) {  // 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 = 1;
  
 // alert("GetDonation:nr= "+nr);
  
  amt = document.apForm["amt" + nr].value;
	//alert("GetDonation:amt= "+amt);
  document.apForm["prc" + nr].value = Dollar (qty * amt);
  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
  
    for (i=1; i<=opts; i++) 
  {     // see if any options
    if (document.apForm["op" + i + nr]) 
	{  
	
      val = document.apForm["op" + i + nr].value;  // get option
	 // alert ("val= "+val);
      op[i] = val;
     }
    else op[i] = "";
 
  document.apForm["prc" + nr].value = Dollar (qty * amt);
 
    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;
  }
  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
  }
  DispTots ();                         // calc totals
}

function SendCart () {  // send the cart to PayPal
var frst = true;  // 1st pass thru items.
var winpar = "scrollbars,location,resizable,status";
var strn   = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart" +
             "&upload=1" +
             "&business=" + root.xx_id + root.xx_xtra;
var i,j=0,des;
//"https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_cart"

// set root variables
  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;

//alert("in sendcart, strn= "+strn);



//alert("about to enter the for loop");

for (i in order) {  // send all valid data
    if (order[i].qty > 0) {
      j = j + 1;
      des = order[i].des;
      strn = strn + "&item_name_"    + j + "=" + escape (des) +
                    "&item_number_"  + j + "=" + i +
                    "&quantity_"     + j + "=" + order[i].qty +
                    "&amount_"       + j + "=" + order[i].amt;
      frst = false;
	  //alert("in sendcart forloop, strn= "+strn);
    }
  }
  if (j > 0) window.open (strn, "paypal", winpar);
}

function checkLen(x,y)
{
if (y.length==x.maxLength)
  {
  var next=x.tabIndex;
  if (next<document.getElementById("apForm").length)
    {
    document.getElementById("apForm").elements[next].focus();
    }
  }
}
function showInfo(iName)
{
	var winpar = "width=200,height=100,left=400,top=400,resizable,scrollbars";
	var strn   = null;
	
	if (iName == 'General') 
	{
		strn="https://www.bridgeyouth.org/2010Gala/2010GalaFundDesc.htm#General";
		window.open(strn,"Donation - General Contribution",winpar);
	}
	if (iName == 'Computer') 
	{
		strn="https://www.bridgeyouth.org/2010Gala/2010GalaFundDesc.htm#Computer";
		window.open(strn,"Donation - Computer System",winpar);
	}
	if (iName == 'Bridges') 
	{
		strn="https://www.bridgeyouth.org/2010Gala/2010GalaFundDesc.htm#Bridges";
		window.open(strn,"Donation - Building Bridges",winpar);
	}
	if (iName == 'Cunneen') 
	{
		strn="https://www.bridgeyouth.org/2010Gala/2010GalaFundDesc.htm#Cunneen";
		window.open(strn,"Donation - Peter D. Cunneen Memorial Fund",winpar);
	}
	if (iName == 'Honor') 
	{
		strn="https://www.bridgeyouth.org/2010Gala/2010GalaFundDesc.htm#Honor";
		window.open(strn,"Donation - In Honor/Memory of",winpar);
	}
	if (iName == 'Staff') 
	{
		strn="https://www.bridgeyouth.org/2010Gala/2010GalaTixDesc.htm#Staff";
		window.open(strn,"Staff Tickets",winpar);
	}
	if (iName == 'Volunteer') 
	{
		strn="https://www.bridgeyouth.org/2010Gala/2010GalaTixDesc.htm#Volunteer";
		window.open(strn,"Volunteer Tickets",winpar);
	}
}
