//---------------------------------------------------------
// JavaScript Function Library for PayPal Shopping Carts
// Version 0.3
//
// Copyright 2004-05, Mike Brittain (www.embimedia.com)
// Permission is granted to use this free of charge on
// your web site as long as all instructions, credits
// and URLs are maintained in the comments of this script.
//
// Disclaimer:
// Use this software at your own risk.  No guarantee is made
// that this script works properly, or is configured to 
// handle all possible setups with the PayPal online
// payment service.  In other words, it's up to you to
// test the script and your own web site to make sure that
// your shopping cart is functioning properly.
// 
//
// This library of functions is helpful for setting up a 
// free online shopping cart using the PayPal online payment
// services.  See www.paypal.com for more info about
// how to use their shopping cart.
// 
// The functions will allow for adding a product to a 
// shopping cart either by link or by an HTML form.
//
// -- LINK USAGE --
// The link supplies options for item_id, name, quantity, 
// cost, size, color, and 2 shipping parameters.  These 
// options are passed to the PayPal shopping cart which 
// is opened in a new pop-up window.
//
// USAGE:
// To add a product:
//   <a href="#" onclick="addToCart('1', '00231',
//          'Custom T-Shirt','12.00','XXL','Red','7.00','2.50');
//           return false;">Custom T-Shirt</a>
//
// Each parameter should be passed an argument, even if you
// do not have any data to pass.  In other words, if you
// don't have a value, pass the empty string:
//
//   <a href="#" onclick="addToCart('1', '00231',
//          'One-size Hat','28.00','','','','');
//           return false;">One-size Hat</a>
//
// To show the current shopping cart:
//   <a href="#" onclick="showCart();return false;">
//     View My Shopping Cart
//   </a>
//
//
// FORM USAGE:
// Additionally, an HTML form may be used to add items to 
// your PayPal cart.  The form must use an onSubmit handler
// of "handleCartItem()".  That function then reads values
// from the HTML form, and sends them to a PayPal shopping 
// cart displayed in a new browser window.
//
//
//---------------------------------------------------------


//---------------------------------------------------------
// SET THESE CONFIGURATION VARIABLES ACCORDING TO YOUR 
// OWN BUSINESS AND SHOPPING CART.
// 
// Business email address as registered with PayPal - split 
// up into segments in order to help fight spambots.
var business = 'kjgemstones' + '@' + 'comcast.net';    
// Your business currency.
var currencyCode = 'USD';
//
//---------------------------------------------------------

// Paypal shopping cart URL.
var paypal_url = 'https://www.paypal.com/cart/';

// Pop-up window properties.
var cartWin = null;
var winName = 'cartwindow';
var winProps = 'width=700,height=400,scrollbars,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);
}

function handleCartItem (form) 
// 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.
{
  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);
}

function showCart() 
// Open a pop-up window with the PayPal 
// shopping cart displayed.
{
  // Set URL for viewing the cart
  var viewUrl = paypal_url + 'display=1' 
        + '&business=' + escape(business);
  // Show the cart
  openCartWin(viewUrl);
}

function openCartWin (loadUrl) {
  // Does window exist?
  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();
  } 
}

function killCart() 
// Kill the cart window when the page changes.
{
  cartWin.close();
  cartWin = null;
}
window.onunload = function() {
  killCart();
};

function getInputValue (inputObj) 
// 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.
{
  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;
}

function getRadioValue (radioObj) 
// Return the selected value from a group of radio buttons.
// radioObj should be a radio button object.
{
  for (var i=0; i < radioObj.length; i++) {
    if (radioObj[i].checked) {
      return radioObj[i].value;
    }
  }
  return null;
}