// PayPal Shopping Cart JS
// Modified by AIS Internet Solutions for use on Core Awareness

// 2.02.07 Added handling function
// 2.19.07 Added no_shipping function
// 2.19.07 Added tax function
// 9.04.07 Switched size to weight
// 9.04.08 Switched color to weight_unit



//---------------------------------------------------------
// 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 = 'info' + '@' + 'coreawareness.com';
// 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=900,height=400,scrollbars,location,resizable,status';
	
//---------------------------------------------------------
	
function addToCart (qty, item_id, name, price, weight, weight_unit, shipping, handling, no_shipping, tax)
{
  // 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 (weight != '') {
    cartUrl += '&weight=' + escape(weight);
  }
  if (weight_unit != '') {
    cartUrl += '&weight_unit=' + escape(weight_unit);
  }
  if (qty != '' && qty != 0) {
    cartUrl += '&quantity=' + escape(qty);
    cartUrl += '&undefined_quantity=1';
  }
  if (shipping != '') {
    cartUrl += '&shipping=' + escape(shipping);
  }
  if (handling != '') {
    cartUrl += '&handling=' + escape(handling);
	}
  if (no_shipping != '') {
    cartUrl += '&no_shipping=' + escape(no_shipping);
	}
  if (tax != '') {
    cartUrl += '&tax=' + escape(tax);
  }
  // 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 weight = '';
  var weight_unit = '';
  var shipping = '';
  var handling = '';
  var no_shipping ='';
  var tax ='';
  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.handling) {
    handling = form.handling.value;
	}
  if (form.no_shipping) {
    no_shipping = form.no_shipping.value;
  }
    if (form.tax) {
    tax = form.tax.value;
  }
  if (form.weight) {
    weight = form.weight.value;
  }
  if (form.weight_unit) {
    weight_unit = form.weight_unit.value;
  }
  if (form.item_quantity) {
    quantity = getInputValue(form.item_quantity);
  }
	
  // Add this item to the cart.
  addToCart(quantity, item_id, name, price, weight, weight_unit, shipping, handling, no_shipping, tax);
}
	
/**
 * 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()
{
  cartWin.close();
  cartWin = null;
}
window.onunload = 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;
}
	

