var cart = {};
var products = {};

function addToCart() {
	var itemid = arguments[0]
	var productname = arguments[1];
	var price = arguments[2];
	var shipping = arguments[3]
	var quantity = ((arguments.length<5)?1:arguments[4]);

	var nvPairs = [
		 'itemid='+encodeURIComponent(itemid)
		,'productname='+encodeURIComponent(productname)
		,'price='+encodeURIComponent(price)
		,'shipping='+encodeURIComponent(shipping)
		,'quantity='+encodeURIComponent(quantity)
	];
	if (arguments.length == 6) {
		nvPairs.push('mode='+encodeURIComponent(arguments[5]));
	}

	var url = '/store/cartentry.php';
	var querystring = nvPairs.join(String.fromCharCode(38))

	window.location = url+'?'+querystring;

	return(false);
}

function calculateAndAddToCart(item,qty) {
	//Does this item exist in the cart?  Are we adding it or re-setting it?
	for (cartItem in cart) {
		if (cartItem == item.id) {
			qty += parseInt(cart[cartItem].quantity);
		}
	}
	addToCart(item.id, item.name, item.calculatePriceEach(qty), item.calculateShippingEach(qty), qty, 'set');
}


function addMultiToCart() {
	var items = arguments[0];
	// var productname = arguments[1];
	// var price = arguments[2];
	// var shipping = arguments[3]
	// var quantity = ((arguments.length<5)?1:arguments[4]);
	var nvPairs = [];
	for (it in items) {
		nvPairs.push('itemid=' + encodeURIComponent(items[it].itemid));
		nvPairs.push('productname=' + encodeURIComponent(items[it].productname));
		nvPairs.push('price=' + encodeURIComponent(items[it].price));
		nvPairs.push('shipping=' + encodeURIComponent(items[it].shipping));
		nvPairs.push('quantity=' + encodeURIComponent(items[it].quantity));
	}

	nvPairs.push('mode=set');

	var url = '/store/cartentry.php';
	var querystring = nvPairs.join(String.fromCharCode(38));

	window.location = url + '?' + querystring;
	
	return(false);

}

function calculateAndAddMultiToCart(items,qty) { 
	var itemArgs = [];

	for (it in items) 
	{
		itemArgs.push({
			 'itemid':items[it].id
			,'productname':items[it].name
			,'price':items[it].calculatePriceEach(qty[it])
			,'shipping':items[it].calculateShippingEach(qty[it])
			,'quantity':qty[it]
		});
	}
	
	addMultiToCart(itemArgs);
}

