//---------------------------------------------------------
	
// Pop-up window properties.
var cartWin = null;
var winName = 'cartwindow';
var winProps = 'width=800,height=410,scrollbars,location,resizable,status';
	
//---------------------------------------------------------
	
function addToCart (qty, item_id, name, price, size,
                    color, shipping, shipping2)
{
  // Set URL for adding an item
  var cartUrl = paypal_url
        + 'add=1'
        + '&business=' + escape(business)
        + '&currency_code=' + escape(currencyCode)
        + '&amount=' + escape(price);
  if (item_id != '') {
    cartUrl += '&item_number=' + escape(item_id);
  }
  if (name != '') {
    cartUrl += '&item_name=' + escape(name);
  }
  if (size != '') {
    cartUrl += '&on0=' + escape("Size");
    cartUrl += '&os0=' + escape(size);
  }
  if (color != '') {
    cartUrl += '&on1=' + escape("Color");
    cartUrl += '&os1=' + escape(color);
  }
  if (qty != '' && qty != 0) {
    cartUrl += '&quantity=' + escape(qty);
    cartUrl += '&undefined_quantity=1';
  }
  if (shipping != '') {
    cartUrl += '&shipping=' + escape(shipping);
  }
  if (shipping2 != '') {
    cartUrl += '&shipping2=' + escape(shipping2);
  }
  // Add the item
  openCartWin(cartUrl);
}
	
/**
 * Pick up values from within an HTML form used for a
 * PayPal shopping cart item and add the item to a cart.
 * Supports select options for size and color.
 */
function handleCartItem (form)
{
  var item_id = '';
  var name = '';
  var price = '';
  var size = '';
  var color = '';
  var shipping = '';
  var shipping2 = '';
  var quantity = 1;
	
  // Get the form values, if the form elements
  // exist.  ID, name, shipping and price should
  // be hidden text fields.
  if (form.item_id) {
    item_id = form.item_id.value;
  }
  if (form.item_name) {
    name = form.item_name.value;
  }
  if (form.item_price) {
    price = form.item_price.value;
  }
  if (form.shipping) {
    shipping = form.shipping.value;
  }
  if (form.shipping2) {
    shipping2 = form.shipping2.value;
  }
  // Size, color and quantity may be setup as
  // select, radio, or text input fields in your
  // form.
  if (form.item_size) {
    size = getInputValue(form.item_size);
  }
  if (form.item_color) {
    color = getInputValue(form.item_color);
  }
  if (form.item_quantity) {
    quantity = getInputValue(form.item_quantity);
  }
	
  // Add this item to the cart.
  addToCart(quantity, item_id, name, price, size,
            color, shipping, shipping2);
}
	
/**
 * Open a pop-up window showing the PayPal shopping cart.
 */
function showCart()
{
  // Set URL for viewing the cart
  var viewUrl = paypal_url + 'display=1'
        + '&business=' + escape(business);
  // Show the cart
  openCartWin(viewUrl);
}
	
/**
 * Check to see whether the cart window exists.
 */
function openCartWin (loadUrl) {
  if (!cartWin || cartWin.closed) {
    // No - Open new window
    cartWin = window.open(loadUrl,winName,winProps);
  } else {
    // Yes - Focus existing window and load new URL
    cartWin.location = loadUrl;
    cartWin.focus();
  }
}
	
/**
 * Kill the cart window when the page changes.
 */
function killCart()
{
 if (cartWin && !cartWin.closed) {
  cartWin.close();
  cartWin = null;
 }
}

window.onbeforeunload = function() {
  killCart();
};

/**
 * Return the current value (or selected value) of the
 * input field... The field can be of type radio, select,
 * input or textarea. Return null if value is not found.
 */
function getInputValue (inputObj)
{
  if (inputObj.type == 'select-one') {  // select box
    return inputObj.options[inputObj.selectedIndex].value;
  } else if (inputObj.length) {  // radio buttons
    return getRadioValue(inputObj);
  } else {
    return inputObj.value;
  }
  return null;
}
	
/**
 * Return the selected value from a group of radio
 * buttons. 'radioObj' should be a radio button object.
 */
function getRadioValue (radioObj)
{
  for (var i=0; i < radioObj.length; i++) {
    if (radioObj[i].checked) {
      return radioObj[i].value;
    }
  }
  return null;
}
	

