// Business email address - split up to fight spambots.
var business = 'erin' + '@' + 'timeforcake.com';		
// Paypal shopping cart URL.
var paypal_url = 'https://www.paypal.com/cart/';
// Currency
var currencyCode = 'USD';

var cartWin = null;
var winName = 'cartwindow';
var winProps = 'width=500,height=300,scrollbars,location=1,resizable,status';

function addToCart (qty,name,price,size,color,letter) {
	// Set URL for adding an item
	
	var cartUrl = paypal_url 
				+ 'add=1'
				+ '&business=' + escape(business)
				+ '&currency_code=' + escape(currencyCode)
				+ '&amount=' + escape(price);

	if (name != '') {
		cartUrl += '&item_name=' + escape(name);
	}
	if (color != '') {
		cartUrl += '&on0=' + escape("Color");
		cartUrl += '&os0=' + escape(color);
	}
	if (size != '') {
		cartUrl += '&on1=' + escape("Size");
		cartUrl += '&os1=' + escape(size);
	}
	if (qty != '' && qty != 0) {
		cartUrl += '&quantity=' + escape(qty);
	}
	// 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 name = '';
	var price = '';
	var size = '';
	var color = '';
	var quantity = 1;
	
	// Get the form values, if the form elements exist.
	// ID, name, and price should be hidden fields.
	if (form.item_name) {
		name = form.item_name.value;
	}
	if (form.item_price) {
		price = form.item_price.value;
	}
	// Size - select, radio, or text input.
	if (form.item_size) {
		size = getInputValue(form.item_size);
	}
	// Letter - select, radio, or text input.
	if (form.item_color) {
		color = getInputValue(form.item_color);
	}
	// Add this item to the cart.
	addToCart(quantity, name, price, size, color);
}


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 {
		return inputObj.value;
	}
	return null;
}


function showCart() {
	// 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();
	} 
}

// Kill the cart window when the page changes
function killCart() {
	cartWin.close();
	cartWin = null;
}
window.onunload = function() {
	killCart();
};