var myCart = [{item_id: 0, item_name: 'Shopping Cart', item_price: 0}];

// ********************************************** //
// This function puts an item in the structure
// ********************************************** //
function buy(new_item_id, new_item_name, new_item_price)
	{
	// add the new item to the list
	myCart[myCart.length] = {item_id: new_item_id, item_name: new_item_name, item_price: new_item_price	};

	// update the shopping cart display
	updateDisplay();
	updateCookies();
	}

// ********************************************** //
// This function removes an item from the structure
// ********************************************** //
function remove()
	{
	var newCopy = Array();
	var newI = 1;
	var i = 1;

	// always copy the first element
	newCopy[0] = myCart[0];
	// cycle through each item in the list
	for (i=1; i<document.shop.cart.options.length; i++)
		{
		if (!document.shop.cart.options[i].selected) 
			{
			newCopy[newI] = myCart[i];
			newI++;
			}
		}
	myCart = null;
	myCart = newCopy;

	updateDisplay();
	updateCookies();
	}

// ********************************************** //
// Updates the data structure from the cookies
// ********************************************** //
function updateData()
	{
	var datalines = Array();

	// get the cookies out into a var
	cookieString = document.cookie;

	// split them up
	cookieSplit = cookieString.split('; ');
	cookieString = "";
	// cycle through each cookie to find the shop one.
	for (each in cookieSplit)
		{
		// find the right cookie
		if (cookieSplit[each].indexOf("shop_contents") != -1)
			{
			// reuse my variable heh.
			cookieString = unescape(cookieSplit[each]);
			}
		}
	// get rid of everything before the equals sign
	cookieSplit = cookieString.split("=");
	cookieString = cookieSplit[1];

	if ((cookieString != "undefined") && (cookieString != "") && cookieString)
		{
		datalines = cookieString.split("|");
		// check that we've got something, otherwise don't bother with the import
		if (datalines.length != 0)
			{
			// import the data into our structure
			var tupples = Array();
			for (items in datalines)
				{
				tupples = datalines[items].split(":");
				myCart[items] = {item_id: tupples[0], item_name: tupples[1], item_price: tupples[2]};
				}
			}
		}
	}
	
// ********************************************** //
// Updates the display from the data structure
// ********************************************** //
function updateDisplay()
	{
	// set our shop total to zero
	var cartTotal = 0;
	// make sure it's empty if it's empty
	document.shop.cart.options[0] = null;

	// cycle through putting items into the cart display
	for (i=0; i<myCart.length; i++)
		{
		// add the new item to the list
		if (i != 0)
			document.shop.cart.options[i] = new Option(myCart[i].item_name + ': ' + priceToString(myCart[i].item_price), myCart[i].item_id);
		else
			document.shop.cart.options[i] = new Option(myCart[i].item_name, myCart[i].item_id);
		// make sure we've got the right number of items
		document.shop.cart.options[i+1] = null;
		// add this item value to our total
		cartTotal += (100*myCart[i].item_price)/100;
		}
	document.shop.total.value = priceToString(cartTotal);
	}

// ********************************************** //
// Updates the cookies from the data structure
// ********************************************** //
function updateCookies()
	{
	var datalines = Array();
	
	// cycle through each object in the structure
	for (i=0; i<myCart.length; i++)
		{
		// add the next item to the cookies list
		datalines[i] = myCart[i].item_id + ":" + myCart[i].item_name + ":" + myCart[i].item_price;
		}
	// create the cookie
	document.cookie = "shop_contents=" + escape(datalines.join("|")) + "; path=/";
	}

// ********************************************** //
// Converts a price into a string
// ********************************************** //
function priceToString(a)
{
a = a * 50;
a = Math.round(a);
a = a / 50;
a += "";
if (a.indexOf(".") == -1) a += ".00";
if (a.indexOf(".") == (a.length - 2)) a+= "0";
a = a.substring(0,a.indexOf(".") + 3);
return("$" + a);
}

