// Shopping cart routines

// Clear all the check boxes in the menu
function InitCheckboxes()
{
}

// Create the menu
function WriteProducts(Sandbox)
{
  var lclCheckBoxNameRegExp = new RegExp('##CheckBoxName##');
  var lclCheckBoxIDRegExp = new RegExp('##CheckBoxID##');
  var lclProductPrefix;

// Set the PayPal URL depending on whether Sandbox or live site
	var PayPalURL;
	if (Sandbox)
		PayPalURL = "https://www.sandbox.paypal.com/cgi-bin/webscr";
	else
		PayPalURL = "https://www.paypal.com/cgi-bin/webscr";

// For whatever reason, it's necessary to embed a table within a table for the BuyNow
// button that follows to render below the menu items.  Without this extra encapsulating
// table, the button renders to the right of the menu.
	document.write('<table border="0"><tr><td>');
	
	document.write(MenuPrefix);
  for (var i=0; i < Products.length; i++)
  {
    lclProductPrefix = ProductPrefix.replace(lclCheckBoxNameRegExp, "CheckBox"+i);
    lclProductPrefix = lclProductPrefix.replace(lclCheckBoxIDRegExp, "CheckBox"+i);
    lclProductPrefix = lclProductPrefix.replace(lclCheckBoxIDRegExp, "CheckBox"+i);
    document.write (lclProductPrefix);
    document.write (ProductNumberPrefix);
    document.write (Products[i][0]);
    document.write (ProductNumberPostfix);
    document.write (ProductNamePrefix);
    document.write (Products[i][1]);
    document.write (ProductNamePostfix);
    document.write (ProductPricePrefix);
    document.write ('$' + Products[i][2]);
    document.write (ProductPricePostfix);
    document.write (ProductPostfix);

// Clear checkbox
    var CheckBoxID = "CheckBox"+i;
    var box = document.getElementById(CheckBoxID);
    box.checked = false;
  }
	document.write(MenuPostfix);

// Close the encsapsulating table
	document.write('</td></tr></table>');

// Write the form for the BuyNow button
// Table forces items subsequent to the button to render below it, not along side it
	document.write('<br>');
	document.write('<table border="0"><tr><td>');
	document.write (' <form id="frmProducts" method="post" onSubmit="return GeneratePayPalCode();" action="' + PayPalURL + '" name="frmProducts">');
	document.write (' <input src="https://www.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" type="image" name="submit" value="Submit" align="left">');
	document.write ('</form>');
	document.write('</td></tr></table>');
}

function ProductClickRoutine(CheckBoxID)
{
  var box = document.getElementById(CheckBoxID);
  
// Find out which product was checked
  for (var i=0; i < Products.length; i++) 
	{
		var TestThisOne = "CheckBox" + i;
		if (TestThisOne == CheckBoxID)
		{
			break;
		}
  }

// Update the checked status in the Products array
  if (box.checked)
  {
    Products[i][Products[i].length-2] = true;
  }
  else
  {
    Products[i][Products[i].length-2] = false;
  }
}

// ====================================
// Generate PayPal code for items which are checked in the form "frmProducts"
// ====================================

function GeneratePayPalCode()
{
  var newElement;		// Use this var to create elements (i.e. fields) for the form to pass to Paypal
  var item_ct = 0;		// Number of line items in the order
  var cart_total_amount = 0;	// Order total amount
  var item_number = new Array();
  var item_name = new Array();
  var item_quantity = new Array();
  var item_amount = new Array();

// ====================================
// Check if nothing is checked and do not proceed if so.
// IE presents a problem in the following situation ....
// 1. User selects one or more items and goes to PayPal
// 2. Uses browser back button to return to this page.
// 3. Clicks the submit button without changing product selection.
// In this case, IE leaves the boxes checked in the screen, but their "checked" property is false.
// This means we see no boxes checked internally and therefore send an empty cart to PayPal.
// Solution is to cancel submit and issue a warning.
// ====================================

  var cart_empty = true;

	for (var i=0; i < Products.length; i++) 
  {
    if (Products[i][Products[i].length-2])  // is this product checked?
		{
			cart_empty = false;
			break;
		}
  }
	
  if (cart_empty)
  {
    InitCheckboxes();			// start fresh
    alert ("There are no items selected.\nPlease check your items and try again.\nThank you.");
    return false;					// this cancels the submit
  }

// ====================================
// cmd = "_cart"
// ====================================

  newElement = document.createElement('input');
  newElement.type="hidden";
  newElement.name="cmd";
  newElement.value="_cart";
  newElement.id="PP_cmd";
  document.frmProducts.appendChild(newElement);

// ====================================
// Paypal vendor account
// ====================================
  newElement = document.createElement('input');
  newElement.type="hidden";
  newElement.name="business";
  newElement.value=PayPalAcct;
  newElement.id="PP_business";
  document.frmProducts.appendChild(newElement);

// ====================================
// return URL for user upon completion
// ====================================
	if (PayPalReturnURL != "")
	{
		newElement = document.createElement('input');
		newElement.type="hidden";
		newElement.name="return";
		newElement.value=PayPalReturnURL;
		newElement.id="PP_return";
		document.frmProducts.appendChild(newElement);
	}

// ====================================
// cancel_return URL if user wants to cancel
// ====================================
	if (PayPalCancelURL != "")
	{
		newElement = document.createElement('input');
		newElement.type="hidden";
		newElement.name="cancel_return";
		newElement.value=PayPalCancelURL;
		newElement.id="PP_cancel_return";
		document.frmProducts.appendChild(newElement);
	}

// ====================================
// IPN URL
// ====================================
	if (PayPalIPNURL != "")
	{
		newElement = document.createElement('input');
		newElement.type="hidden";
		newElement.name="notify_url";
		newElement.value=PayPalIPNURL;
		newElement.id="PP_IPN";
		document.frmProducts.appendChild(newElement);
	}

// ====================================
// Variable passed thru to IPN
// ====================================
	if (PayPalPassThruValue != "")
	{
		newElement = document.createElement('input');
		newElement.type="hidden";
		newElement.name="custom";
		newElement.value=PayPalPassThruValue;
		newElement.id="PP_Custom";
		document.frmProducts.appendChild(newElement);
	}

// ====================================
// "upload" var is the trigger to Paypal that we're passing entire shopping cart
// ====================================
  newElement = document.createElement('input');
  newElement.type="hidden";
  newElement.name="upload";
  newElement.value="1";
  newElement.id="upload";
  document.frmProducts.appendChild(newElement);

// ====================================
// Build arrays corresponding to line items
// ====================================
  for (var i=0; i < Products.length; i++) 
  {
    if (Products[i][Products[i].length-2])  // is this product checked?
		{
			item_ct++;
			item_number[item_ct] = Products[i][0];
			item_name[item_ct] = Products[i][1];
			item_quantity[item_ct] = 1;
			item_amount[item_ct] = Products[i][2];
			cart_total_amount += item_amount[item_ct];
		}
  }

// ====================================
// Convert line items array elements to actual Paypal variables
// Each item contains (I from 1 to N)
//   item_number_I
//   item_name_I
//   quantity_I
//   amount_I
// ====================================
  for (i=1; i <= item_ct; i++)
  {
    newElement = document.createElement('input');
    newElement.type="hidden";
    newElement.name="item_number_" + i
    newElement.value=item_number[i];
    newElement.id="item_number_" + i;
    document.frmProducts.appendChild(newElement);

    newElement = document.createElement('input');
    newElement.type="hidden";
    newElement.name="item_name_" + i
    newElement.value=item_name[i];
    newElement.id="item_name_" + i;
    document.frmProducts.appendChild(newElement);

    newElement = document.createElement('input');
    newElement.type="hidden";
    newElement.name="quantity_" + i;
    newElement.value=item_quantity[i];
    newElement.id="quantity_" + i;
    document.frmProducts.appendChild(newElement);

    newElement = document.createElement('input');
    newElement.type="hidden";
    newElement.name="amount_" + i;
    newElement.value=item_amount[i];
    newElement.id="amount_" + i;
    document.frmProducts.appendChild(newElement);

    newElement = document.createElement('input');
    newElement.type="hidden";
    newElement.name="tax_" + i;
    newElement.value="0";
    newElement.id="tax_" + i;
    document.frmProducts.appendChild(newElement);
  }
}
