var order = new Object();  // object to store order items in
var root  = new Object();  // selection criteria
var cntr  = 0;             // items in object
var opts  = 5;             // number of options to allow
var tamt=0,tqty=0,thnd=0;  // totals
var tship=0,tbook=0;

// place for user-specific options
root.xx_can  = "http://releeco.com/ppcancel.php";   // place for PayPal cancel return path
root.xx_cur  = "USD";// enter default currency code (or null)
root.xx_id   = "orders@releeco.com";  // PayPal ID
root.xx_img  = "";   // place for image url
root.xx_lc   = "US"; // enter default country code (or null)
root.xx_ret  = "http://releeco.com/ppsuccess.php";   // place for PayPal return path
root.xx_sty  = "";   // place for PayPal page style
root.xx_xtra = "&rm=0"; // place for other PayPal commands

function DispTots () {  // display totals on the page
  if (tamt == 0) {
  document.getElementById("tot").innerHTML = "$ 0.00";
  document.getElementById("ship").innerHTML = "$ 0.00";
  document.getElementById("totl").innerHTML = "$ 0.00";
  } else if (tbook == 1) {
  document.getElementById("tot").innerHTML = "$ " +  Dollar (tamt);
  	if (tqty == 4) {
	tship = 8.95;
	} else if (tqty <= 5){
	 tship=3.95 * tqty;
	 } else {
	 tship = 8.95;
	 }
	
  document.getElementById("ship").innerHTML = "$ " +  Dollar (tship);
  document.getElementById("totl").innerHTML = "$ " +  Dollar (tamt + tship);
  
  }  else {
  document.getElementById("tot").innerHTML = "$ " +  Dollar (tamt);
    if (tamt <= 30.00){
	 tship=8.95;
	} else if (tamt <= 100.00) {
	 tship=8.95;
	 } else {
	 tship = 8.95;
	 }
	
  document.getElementById("ship").innerHTML = "$ " +  Dollar (tship);
  document.getElementById("totl").innerHTML = "$ " +  Dollar (tamt + tship);
  }
}

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);
  if (str ==  ".00") str="";
  return str;               // return valid string
}

function GetOrder (id, pn, 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.orderf["qty" + nr].value;  // get qty
  if (isNaN (qty)) {      // test entry
    alert ("That is not a valid number!  Try again.");
	document.orderf["qty" + nr].value = "";
	qty=0;
	document.getElementById("prc" + nr).innerHTML = Dollar (qty * amt);
    return;
  }
  qty = Math.round(qty);
  if (qty == 0) {
  	document.orderf["qty" + nr].value = "";
  } else {
   	document.orderf["qty" + nr].value = qty;
  }
  document.getElementById("prc" + nr).innerHTML = Dollar (qty * amt);
//  document.orderf["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
  order[id].des = des;           // load up values
  order[id].amt = Dollar (amt);
  order[id].qty = qty;
  order[id].ref = nr;
  order[id].pnum = pn;
  tbook = 0;
  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
	if (nr ==  '81') {
	  tbook = tbook - 1;
	}
	if (nr ==  '82') {
	  tbook = tbook - 1;
	}
	if (nr ==  '83') {
	  tbook = tbook - 1;  
	}
	if (nr ==  '84') {
	  tbook = tbook - 1; 
	}
	if (nr ==  '85') {
	  tbook = tbook - 1;
	}
	if (nr ==  '86') {
	  tbook = tbook - 1; 
	}
	if (nr ==  '87') {
	  tbook = tbook - 1; 
	}
	if (nr ==  '88') {
	  tbook = tbook - 1;
	}
	if (nr ==  '888') {
	  tbook = tbook - 1;
	}
	if (nr ==  '889') {
	  tbook = tbook + 1;  
	     
	 } else if (qty > 0) {tbook = tbook + 2;}
  }
  DispTots ();                         // calc totals
}

function ClearCart() { // Clear the cart and reset the form
var i,nr;
	if (cntr == 0) return;
	for (i in order) {
	  nr = order[i].ref;
	  document.orderf["qty" + nr].value = "";
	  document.getElementById("prc" + nr).innerHTML =  "";
	}
	document.getElementById("tot").innerHTML = "$ 0.00";
	document.getElementById("ship").innerHTML = "$ 0.00";
	document.getElementById("totl").innerHTML = "$ 0.00";
	cntr = 0;
	tbook = 0;
}

function SendCart () {  // send the cart to PayPal
var frst = true;  // 1st pass thru items.
if (cntr == 0) {
  alert ("You have not selected any items to order!");
  return;
}
// window.open(href,'actionWindow','width=740,height=500,status=yes,toolbars=yes,menubar=no,location=no,scrollbars=yes,resizable=yes');
var winpar = "width=710,height=390,scrollbars=yes,location=no,resizable=yes,status=yes,location=no,menubar=no,toolbars=no";
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;
  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;	 
  for (i in order) {  // send all valid data
    if (order[i].qty > 0) {
      j = j + 1;
      des = order[i].des;
      if (j == 1) {  // put in descriptions for 1st item
        strn = strn + "&handling_cart=" + Dollar (thnd);
      }
      strn = strn + "&item_name_"    + j + "=" + escape (des) +
                    "&item_number_"  + j + "=" + order[i].pnum +
                    "&quantity_"     + j + "=" + order[i].qty +
                    "&amount_"       + j + "=" + order[i].amt;
      strn = strn + "&shipping_" + j + "=" + Dollar(tship) + "&shipping2_" + j + "=0.00";
	  tship = 0;
    }
  }
  if (j > 0) window.open (strn, "paypal", winpar);
  window.document.location.replace("processing.php");
}