var order = new Object();  // object to store order items in
var root  = new Object();  // selection criteria
var guest = new Object();
var on    = true;
var off   = false;
var cntr  = 0;             // items in object
var opts  = 5;             // number of options to allow
var shpr		   // what to charge for shipping;
var stxt  = "";            // shipping text
var spos  = -1;            // shipping position selector
var ttax  = 0;             // percent for taxes
var ttxt  = "0";           // tax text
var tpos  = -1;            // tax position selector
var tamt=0,tqty=0,thnd=0;  // totals
var nme
var nmeList
var nmetest
var nmedefault
var nr
var din
var dinpos
var tbl
var phn

// place for user-specific options
root.xx_can  = ""; // place for PayPal cancel return path
root.xx_cur  = ""; // enter default currency code (or null)
root.xx_id   = ""; // PayPal ID
root.xx_img  = ""; // image URL
root.xx_lc   = ""; // 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 = ""; // place for other PayPal commands
root.xx_shpr = "";
root.xx_shpr2 = "";


function ValidatePhn ()
{
phn = document.orderf["phnContact"].value;
if (phn.length < 13)
{
alert ("Please enter a valid phone number.\n" +
	   "Format: (xxx)xxx-xxxx.\n" +
	   "This will help us contact you if we have any questions.");
return;
}
tblType = document.orderf["op1Tbl1"].value;
if (tblType == 0) {
	   alert ("Please Select a Table Type.");
	   document.orderf["op1Tbl1"].focus();
	   return;
}
BuildGList();
}

function EnableCheckout ()
{
document.orderf["submit"].disabled = false;
document.orderf["submit"].value   = "Checkout";
}

function DispTots () {  // display totals on the page
var tmp,d;
  d = document.orderf;
  d.sub.value = Dollar (tamt);
  d.tot.value = Dollar (tamt + (shpr*cntr));
}

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,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 = 1;  // get qty
   nme = document.orderf["nme" + nr].value;
   nmedefault = "TypeNameHere"
   tblpos = document.orderf["op1" + nr].value;
   //tbl = document.orderf["op2" + nr].value;
   s = document.orderf["nme" + nr].value;
	filteredValues = " ";     // Characters stripped out
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {  // Search through string and append to unfiltered values to returnString.
	var c = s.charAt(i);
	if (filteredValues.indexOf(c) == -1) returnString += c;
	}
	nmetest = returnString;
   if (nmetest == nmedefault)
   	{
		alert ("Please enter Guest's name.");
		document.orderf["op1" + nr].value = 0;
		document.orderf["nme"+ nr].select();
   		return;
   	}
	
   if (tblpos == 0) {
	   alert ("Please Select a Table Type.");
	   return;
   }
   else if (tblpos == 1) {
	   des = "Reg 10Top Table - ";
	   amt = 1250.00;
   }
   else if (tblpos == 2) {
	   des = "Vip 10Top Table - ";
	   amt = 1750.00;
   }
   else if (tblpos == 3) {
	   des = "Reg 12Top Table - ";
	   amt = 1500.00;
   }
   else if (tblpos == 4) {
	   des = "VIP 12Top Table - ";
	   amt = 2050.00;
   }   
   if (isNaN (qty))           // test entry
   { 
    alert ("That is not a valid number!  Try again.");
    return;
   }
   if (qty > 1)           // test entry
   { 
    alert ("Sorry, we can only process one table at a time.");
    return;
   }
   /*s = document.orderf["op2" + nr].value;
	filteredValues = " ";     // Characters stripped out
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {  // Search through string and append to unfiltered values to returnString.
	var c = s.charAt(i);
	if (filteredValues.indexOf(c) == -1) returnString += c;
	}
	tbltest = returnString;
   if (tbltest == "Whowouldyouliketobeseatedwith?")
   	{
   	tbl = "None."
   	}*/
   
  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 + nme;           // 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
  }
  shpr  = root.xx_shpr/cntr;
  DispTots ();                         // calc totals
}

function BuildGList ()
{  
var i = 1;
var val,qty,pos;
var op = new Array ();    // accumulate options here
   nmeList = ""; // clear list
   while (i < 11) {
   gstnme = document.orderf["nmeGuest" + i].value;
   dinpos = document.orderf["op1Guest" + i].value;
   if (gstnme == "Type Name Here") gstnme = "No Name";
   if (dinpos == 0) din = "N/S";
   else if (dinpos == 1) din = "Combo";
   else if (dinpos == 2) din = "Pasta";
   //else if (dinpos == 3) din = "Pasta";
   if (i == 1) nmeList = gstnme + "-" + din;
   else if (i > 1) nmeList = nmeList + "; " + gstnme + "-" + din;
   i = i + 1;
   }
  SendCart ();    
}
	 
function SendCart () 
{  // send the cart to PayPal
document.orderf["submit"].disabled = true;
document.orderf["submit"].value   = "Please Reset";
var frst = true;  // 1st pass thru items.
var winpar = "width=820,height=760,scrollbars," +
             "location,resizable,status";
var strn   = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart" +
             "&upload=1" +
             "&business=" + root.xx_id + root.xx_xtra+
             "&shipping=" + root.xx_shpr;
var i,j=0,des,nme;
  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;
		info = nmeList;
      		if (j == 1) 
      		{  // put in descriptions for 1st item
        	des = des + " - Contact Number: " + phn;
       		strn = strn + "&item_name_"    + j + "=" + escape (des) +
                    "&quantity_"     + j + "=" + order[i].qty +
                    "&amount_"       + j + "=" + order[i].amt +
                    "&on0_"			 + j + "=" + "Guest List" +
                    "&os0_"			 + j + "=" + escape (info) +
                    "&shipping_"     + j + "=" + shpr;
      		}
	  		else(j > 1)
	  		{
	  		des = des;
       		strn = strn + "&item_name_"    + j + "=" + escape (des) +
                    "&quantity_"     + j + "=" + order[i].qty +
                    "&amount_"       + j + "=" + order[i].amt +
                    "&on0_"			 + j + "=" + "Guest List" +
                    "&os0_"			 + j + "=" + escape (info) +
                    "&shipping_"     + j + "=" + shpr;
      		}
      	  }
      	}
      	
      	frst = false;
    
  	if (j > 0) window.open (strn, "paypal", winpar);
}