var order = new Object();  // object to store order items in
var root  = new Object();  // selection criteria

var sqty = new Array ();  // shipping qty breakpoints
var samt = new Array ();  // amount charged
var sn   = 0;             // number of shipping brkpts

var on    = true;
var off   = false;
var cntr  = 0;             // items in object
var opts  = 5;             // number of options to allow
var shpr  = 0; 			   // what to charge for shipping;
var stxt  = "";            // shipping text
var spos  = -1;            // shipping position selector
var tpos  = -1;            // tax position selector
var tamt=0,tqty=0,twgt=0,wgt=0;  // totals
var tshp = 0;

root.shp  = on;    // shipping selection line
// place for user-specific options
root.xx_can  = ""; // place for PayPal cancel return path
root.xx_cur  = "AUD"; // enter default currency code (or null)
root.xx_id   = "dirk@dirkbertels.net"; // PayPal ID
root.xx_img  = ""; // image URL
root.xx_lc   = "AU"; // enter default country code (or null)
root.xx_ret  = ""; // place for PayPal return path
root.xx_sty  = ""; // place for PayPal page style
root.xx_xtra = "&rm=0"; // place for other PayPal commands

// calc root stuff. Called from HTML
// sets shipping position selector to 0
function CalcRoot () 
{  
  if (root.shp) spos=0; 
}

 // display totals on the page
function DispTots () 
{ 
var d;
  tshp = 0;
  d = document.orderf;
  d.sub.value = Dollar (tamt);
  d.wgt.value = twgt;
  
  if(root.shp)
  {
  	for(i=sn-1; i>=0; i--)
	{
		if(tqty >= sqty[i])
		{
			tshp = samt[i] * 1.0;			
			break;
		}		
	}
	d.shp.value = tshp;
  }
  d.tot.value = tamt + tshp;
} 

// force to valid dollar amount
function Dollar (val) 
{     
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 (isNaN (qty)) 
  {      // test entry
    alert ("That is not a valid number!  Try again.");
    return;
  }

  document.orderf["prc" + nr].value = Dollar (qty * amt);
  document.orderf["tgm" + 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
  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;
  
  // calc totals we might use
  for (i in order) 
  {             
    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 grams
  }

  DispTots ();                         // calc totals
}

// send the cart to PayPal
function SendCart () 
{  
	var frst = true;  // 1st pass thru items.
	var winpar = "width=710,height=390,scrollbars," +
             "location,resizable,status";
	
/*		 
	// sandbox	
	var strn = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_cart" +
             "&upload=1" +
             "&business=" + root.xx_id + root.xx_xtra;
*/			 	
	
	//true paypal site
	var strn   = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart" +
           "&upload=1" +
           "&business=" + root.xx_id + root.xx_xtra;
 
/*	
	// test php site
	var strn = "http://dirkbertels.net/verify.php?cmd=_cart" +
             "&upload=1" +
             "&business=" + root.xx_id + root.xx_xtra;
*/      
	var i,j=0,des;
	
  	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;
  	if (tpos > 0) 
		strn = strn + "&tax_cart=" + Dollar (0);
	
	// send all valid data	
  	for (i in order) 
  	{
		if (order[i].qty > 0) 
		{
      		j = j + 1;
      		des = order[i].des;
			
			// put in descriptions for 1st item
      		if (j == 1) 
	  		{  
        		des = des;
				// display shipping value
  				if (spos >= 0) // there is some shipping activity
				{
					if (frst) // first time thru
					{
						strn = strn + "&shipping_" + j + "=" + tshp;
					}
					else // every other time thru
					{
						strn = strn + "&shipping_" + j + "= 0";
					}
				}
  			}
      		strn = strn + "&item_name_"    + j + "=" + escape (des) +
                    "&item_number_"  + j + "=" + i +
                    "&quantity_"     + j + "=" + order[i].qty +
                    "&amount_"       + j + "=" + order[i].amt;
		}
	}

  	frst = false; 
  	if (j > 0) window.open (strn, "paypal", winpar);
}

//NEW
function SetSH (q1, s1) 		// fill appropriate arrays
{   
var i;
  sn = 0;                      // count of breakpoints
  for (i=0; i<arguments.length; i=i+2) 
  {
    sqty[sn] = arguments[i];   // price breakpoint
    samt[sn] = arguments[i+1]; // shipping charge
    sn = sn + 1;               // number of bkpts
  }
  DispTots();
}

// process user-selected shipping
function Shipper (obj1) 
{  
  SetSH ();  						// Don't delete this!  Shows bad selection was made.
  spos = obj1.selectedIndex;       	// which item was selected?
  stxt = obj1.options[spos].text;  	// user selection...
  
  switch (spos) 
  {                  // item selected
    case 0:  // no selection made, return.
      break;
    case 1:  // Will Pick Up
      SetSH (1, 0.00, 2, 0.00, 3, 0.00, 4, 0.00);  
      break;
    case 2:  // Australia
      SetSH (1, 2.50, 2, 4.00, 3, 5.00, 4, 6.00);
      break;
    case 3:  // Asia
      SetSH (1, 4.20, 2, 5.50, 3, 9.00, 4, 10.50);  
      break;
    case 4:  // All other countries
      SetSH (1, 6.00, 2, 8.00, 3, 14.00, 4, 16.00);
      break;
    default:
      SetSH (1, 99);  // outlandish, to detect.
      break;
  }
  DispTots ();
}

