/*shopping list object (ShoppingList())
 * 		methods:
 * 			ShoppingList.prototype.gettotalitems = getTotalNumberOfItems
 * 			ShoppingList.prototype.getList = getItemsList
 *  		ShoppingList.prototype.clearList = clearListItem
 * 			ShoppingList.prototype.savelist = saveListItems
 * 			ShoppingList.prototype.additem = addItemToList
 * 			ShoppingList.prototype.printlist = PrintDDCShoppingList;
 * 			ShoppingList.prototype.cleanwindows = cleanupShoppingListWindows;
 * 		functions - not exposed outside of the object (ie to pages)
 * 			writeDDCFoodsCookie(name, value, expires)
 * 			readDDCFoodsCookie(name)
 * 			createNewSiteWindow(windowURL, title)
 * 			closeSiteWindows()
 * 			sortListItems(listitems)
 */
function ShoppingList()
{
			// private array to store list of window opened by the site
			//		used to track windows opened within the site 
			// 			to ensure they are closed when the site is closed
			//		array not accessible from outside the object
	var privateOpenWindows = new Array();
	
	// constructor 	- run when new browser session is started -- new object created in the CatagoryFrame.php
	//				- creates a new cookie
	//						shopping list format:
	//							concatenated string containing product line items separated by *
	//								LineItem1*LineItem2*......
	//									each product line contains -
	//										product catagory:product item:product code:product description:product pack size:quantity
	//					page refreshes won't reset the cookie
	//					cookies deleted when browser is closed
	var test = null;
	test = readDDCFoodsCookie("totalitems");
	if (test == null)
	{
		if (writeDDCFoodsCookie("totallines", "0", "") == false){alert("Unable to setup shopping baskets.  Shopping baskets will not work correctly."); return;}
		if (writeDDCFoodsCookie("totalitems", "0", "") == false){alert("Unable to setup shopping baskets.  Shopping baskets will not work correctly."); return;}
		if (writeDDCFoodsCookie("listItems", "", "") == false){alert("unable to clear the shopping basket.  Shopping baskets will not work correctly."); return;}
	}
	
/*shopping list object METHODS
 * 		methods: - ACCESSIBLE FROM PAGES WITHIN THE SITE:
 * 			ShoppingList.prototype.gettotallines = getTotalNumberOfLineItems;
 * 			ShoppingList.prototype.gettotalitems = getTotalNumberOfItems
 * 			ShoppingList.prototype.getList = getItemsList
 *  		ShoppingList.prototype.clearList = clearListItem
 * 			ShoppingList.prototype.savelist = saveListItems
 * 			ShoppingList.prototype.additem = addItemToList
 * 			ShoppingList.prototype.printlist = PrintDDCShoppingList;
 * 			ShoppingList.prototype.cleanwindows = cleanupShoppingListWindows;
 * 
 * 		functions - not exposed outside of the object (ie to pages)
 * 			writeDDCFoodsCookie(name, value, expires)
 * 			readDDCFoodsCookie(name)
 * 			createNewSiteWindow(windowURL, title)
 * 			closeSiteWindows()
 * 			sortListItems(listitems)
 */
	
		// retrieves value of the product line items in the shopping list from the cookie
		//		returns the the value or null if fails
	function getTotalNumberOfLineItems()
	{
		return readDDCFoodsCookie("totallines");
	}
		// make getTotalNumberOfLineItems function visible to other pages as gettotallines
	ShoppingList.prototype.gettotallines = getTotalNumberOfLineItems;
	
		// retrieves value of the total items in the shopping list from the cookie
		//		returns the the value or null if fails
	function getTotalNumberOfItems()
	{
		return readDDCFoodsCookie("totalitems");
	}
		// make getTotalNumberOfItems function visible to other pages as gettotalitems
	ShoppingList.prototype.gettotalitems = getTotalNumberOfItems;
	
		//  retrieves shopping list items from the cookie
		//		separates them into individual items
		//		returns the array of items - List
		//		returns null if the list contains no items
	function getItemsList()
	{
		if (listItems == 0)
		{
			return null;
		}
		else
		{
			var List = new Array();
			var listItems = readDDCFoodsCookie("listItems");
				// concatenated string containing items separated by *
				//		each item contains -
				//			product catagory:product item:product code:product description:product pack size:quantity
			List = listItems.split("*");
			return List;
		}
	}
		// make getItemsList function visible to other pages as getList
	ShoppingList.prototype.getList = getItemsList;
	
	function clearListItems()
	{
		if (writeDDCFoodsCookie("totallines", "0", "") == false){alert("Unable to setup shopping baskets.  Shopping baskets will not work correctly."); return;}
		if (writeDDCFoodsCookie("totalitems", "0", "") == false){alert("Unable to setup shopping baskets.  Shopping baskets will not work correctly."); return;}
		if (writeDDCFoodsCookie("listItems", "", "") == false){alert("unable to clear the shopping basket.  Shopping baskets will not work correctly."); return;}
	}
		// make clearListItems function visible to other pages as clearList
	ShoppingList.prototype.clearList = clearListItems;
	
		//	saves the shopping list
		//		list passed in as an array of items
		//  	returns true/false
	function saveListItems(shoppingList)
	{
		var listItemString = "";
		var itemCounter = 0;
		var lineCounter = 0;
		var counter = 0;
		var tmpArray = new Array();
				// create list string
				//		items separated by "*"
				// 			if quantity = 0 don't add to the list
		while (counter <= shoppingList.length - 1)
		{
			tmpArray = null;
			tmpArray = shoppingList[counter].split(":");
			if (parseInt(tmpArray[5]) != 0)
			{
				if (listItemString == "")
				{
					listItemString = shoppingList[counter];
				}
				else
				{
					listItemString = listItemString + "*" + shoppingList[counter];
				}
				lineCounter++;
				itemCounter = itemCounter + parseInt(tmpArray[5]);
			}
			counter++;
		}
				// sort the list
		listItemString = sortListItems(listItemString);
				// write list
		if (writeDDCFoodsCookie("totallines", lineCounter, "") == false){alert("unable to clear the shopping basket.  Shopping baskets will not work correctly."); return;}
		if (writeDDCFoodsCookie("totalitems", itemCounter, "") == false){alert("unable to clear the shopping basket.  Shopping baskets will not work correctly."); return;}
		if (writeDDCFoodsCookie("listItems", listItemString, "") == false){alert("unable to clear the shopping basket.  Shopping baskets will not work correctly."); return;}
		return true;
	}
		// make saveListItems function visible to other pages as savelist
	ShoppingList.prototype.savelist = saveListItems;
		
	
		//  adds an new item to the shopping list
		//			returns true/false
	function addItemToList(item)
	{
		var shoppingListArray = new Array();
		var newItemArray = new Array();
		var currentitems = "";
		var quantity;
		
			// split new item into array (newItemArray) of fields
			//		product catagory:product item:product code:product description:product pack size:quantity
		newItemArray = item.split(":");
		
			// get current shopping list
		var shoppingListString = null;
		shoppingListString = readDDCFoodsCookie("listItems");
			// add item to the list string
		if (shoppingListString == null) 				// no items in current list
		{
				// update item list
			shoppingListArray[0] = item;
				//save the updated list  -- list as an array
			if (saveListItems(shoppingListArray) == false){alert("Unable to add to the shopping basket.  Shopping baskets will not work correctly."); return false;}
			
				// acknowledge message
				//		product catagory:product item:product code:product description:product pack size:quantity
			alert("The following product has been added to your shopping basket:" + '\n' + " " + '\n' +
					   "Product Description - " + newItemArray[3] + '\n' + 
					  "Product Code - " + newItemArray[2] + '\n' +
					  "Pack Size - " + newItemArray[4] + '\n' + " " + '\n' +
					  "Quantity added - " + newItemArray[5]);
		}
		else							// current list contains items
		{
					// split current list into array (list) of items
			shoppingListArray = shoppingListString.split("*");
			
					// check if the new item is already in the list
					//		product code used as the unique identifier
			var found = false;
			var tmpArray = new Array();
			var i = 0;
			while(i <= shoppingListArray.length -1)
			{
					// array containing list item details
					// 		item [i] in the list 
				tmpArray = null;
				tmpArray = shoppingListArray[i].split(":");
				if (tmpArray[2] == newItemArray[2])  			// item is already in the list
				{
					found = true;
					newItemArray[5] = parseInt(tmpArray[5]) + parseInt(newItemArray[5]);
					quantity = parseInt(newItemArray[5]);
					
						// build newitem string
					c = 0;
					while(c <= newItemArray.length -1)
					{
						if (c ==0)
						{
							newItem = newItemArray[c];
						}
						else
						{
							newItem = newItem + ":" + newItemArray[c];
						}
						c++;
					}
					
					shoppingListArray[i] = newItem;
					 	// if found don't need to look a remaining items
					 i = shoppingListArray.length;
				}
				i++;
			}
			
			if (found == false)
			{
				shoppingListString = shoppingListString + "*" + item;
				shoppingListArray = shoppingListString.split("*");
					//save the updated list  -- list as an array
				if (saveListItems(shoppingListArray) == false){alert("Unable to add to the shopping basket.  Shopping baskets will not work correctly."); return false;}
				
					// acknowledge message
					//		product catagory:product item:product code:product description:product pack size:quantity
				alert("The following product has been added to your shopping basket:" + '\n' + " " + '\n' +
					   "Product Description - " + newItemArray[3] + '\n' + 
					  "Product Code - " + newItemArray[2] + '\n' +
					  "Pack Size - " + newItemArray[4] + '\n' + " " + '\n' +
					  "Quantity added - " + newItemArray[5]);
			}
			else
			{
				var confirmBox=confirm("You already have " + tmpArray[5] + " of these in your shopping basket " + '\n' + " " + '\n' +
									   "This will make your total quantity in your shopping list to " + quantity + "?");
				if (confirmBox==true)
				{
						//save the updated list  -- list as an array
					if (saveListItems(shoppingListArray) == false){alert("Unable to add to the shopping basket.  Shopping baskets will not work correctly."); return false;}
					
						// acknowledge message
						//		product catagory:product item:product code:product description:product pack size:quantity
					alert("The following product has been added to your shopping basket:" + '\n' + " " + '\n' +
					   "Product Description - " + newItemArray[3] + '\n' + 
					  "Product Code - " + newItemArray[2] + '\n' +
					  "Pack Size - " + newItemArray[4] + '\n' + " " + '\n' +
					  "Updated Total Quantity - " + newItemArray[5]);
			   }
			}
		}  // if (listItems == null)
		return true;
	}
			// make addItemToList function visible to other pages as additem
	ShoppingList.prototype.additem = addItemToList;

	
	
		// print the current shopping list
	function PrintDDCShoppingList()
	{
				//call private function to create a new popup window
		var pr = createNewSiteWindow('PrintShoppingList.php', 'printShoppingList');
				// set focus to the new window & display print dialog box
		pr.focus();
		pr.print();
	}
			// make PrintDDCShoppingList function visible to other pages as printlist
	ShoppingList.prototype.printlist = PrintDDCShoppingList;
	
	
	
	function cleanupShoppingListWindows()
	{
		closeSiteWindows();
	}
		// make cleanupShoppingListWindows function visible to other pages as cleanwindows
	ShoppingList.prototype.cleanwindows = cleanupShoppingListWindows;
	
/*shopping list object FUNCTIONS)
 *
 * 		functions - not exposed outside of the object (ie to pages)
 * 			writeDDCFoodsCookie(name, value, expires)
 * 			readDDCFoodsCookie(name)
 * 			createNewSiteWindow(windowURL, title)
 * 			closeSiteWindows()
 * 			sortListItems(listitems)
 */
		// save a cookie
		//		saved as the name:value pair
	function writeDDCFoodsCookie(name, value, expires)
	{
		var test = null;
		test = document.cookie  = name+"="+value+expires+"; path=/";
		if (test == null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
		// read a cookie
		//		returns cookie value for the specified name
		//			if a blank name is supplied returns all cookies associated with the site
		//		returns null if:
		//			no cookie found or an error occurred
	function readDDCFoodsCookie(name)
	{
		var allCookies = document.cookie;
		if (name == "")
		{
			return document.cookie;
		}
		else
		{
			var allcookies = document.cookie;
			var pos = allcookies.indexOf(name+"=");
			if (pos == -1)
			{
				return null;
			}
			else
			{
				var start = pos + name.length + 1;
				var end =  allcookies.indexOf(";", start);
					if (end == -1)
					{
						end =  allcookies.length;
					}
				return allcookies.substring(start, end);
			}
		}
	}
			// create a new window
			//		store handle in the privateOpenWindows array
			//			note:  cannot pass private variable (privateOpenWindows[]) as a return
	function createNewSiteWindow(windowURL, title)
	{
		var p = window.open(windowURL, title, 'scrollbars=no,menubar=no,resizable=no,status=no,location=nn, height=400,width=400', false);
		privateOpenWindows[privateOpenWindows.length] = p;
		return p;
	}

		// close all windows opened within the site (handles stored in privateOpenWindows[])
	function closeSiteWindows()
	{
		var counter = 0;
		while (counter <= privateOpenWindows.length - 1)
		{
			if (privateOpenWindows[counter] != null)
			{
				privateOpenWindows[counter].close();
				privateOpenWindows[counter] = null;	
			}
			counter++;
		}
	}
	
		// sorts the list item string
		//		separates into individual items (*) into an array - list
		//		sorts the array items
		//		reassembles and returns the itemlist string
	function sortListItems(listitems)
	{
						//	note -- may have to replace spaces to get to sort across the whole string!!!!!
		
		var list = new Array();
		list = listitems.split("*");
		list.sort();
		listitems = "";
		var i=0;
		while (i <= list.length - 1)
		{
			if (i == 0)
			{
				listitems = list[i];
			}
			else
			{
				listitems = listitems + "*" + list[i];
			}
			
			i++;
		}
		return listitems;
	}
	
}// function ShoppingList()

/*	end of shopping list object declaration
 * 		methods: - ACCESSIBLE FROM PAGES WITHIN THE SITE:
 * 			ShoppingList.prototype.gettotallines = getTotalNumberOfLineItems;
 * 			ShoppingList.prototype.gettotalitems = getTotalNumberOfItems
 * 			ShoppingList.prototype.getList = getItemsList
 * * 		ShoppingList.prototype.clearList = clearListItem
 * 			ShoppingList.prototype.savelist = saveListItems
 * 			ShoppingList.prototype.additem = addItemToList
 * 			ShoppingList.prototype.printlist = PrintDDCShoppingList;
 * 			ShoppingList.prototype.cleanwindows = cleanupShoppingListWindows;
 * 		functions: - not exposed outside of the object (ie to pages)
 * 			writeDDCFoodsCookie(name, value, expires)
 * 			readDDCFoodsCookie(name)
 * 			createNewSiteWindow(windowURL, title)
 * 			closeSiteWindows()
 * 			sortListItems(listitems)
 */

//**********************    mouse event manager for shopping list buttons etc  ******************
//
// mouse events that invoke response -- 
//		case statement for each mouse event type 
//			case statement for each button type - if necessary
//
function shoppinglistMouseEventManager(event)
{
		//
		// retrieve the source object that created the event for the different browsers
		// srcElement - used by IE
		// target - used by other browsers
		//
	if (event.srcElement)
	{
		var mySourceElement = event.srcElement;
	} 
	else if (event.target)
	{
		var mySourceElement = event.target;
	}
	else
	{
		alert("Unable to detect mouse event source - probably an unsupported browser");
		return;
	}
			//
			// mouse events that invoke response -- 
			//		case statement for each mouse event
			//			case statements for element types that respond to the event
			// 				uses element id(buttonType) & name propetries to determine the element information
			// 					listimagebutton - buttons from HeaderFrame.php (show shopping list);
			//									- buttons from the ShoppingListFrame.php
			//									- buttons from ProductItem.php & SweetsProductItem.php(add to shopping list)
			//
//alert(event.type);
	var buttonType = mySourceElement.id;
	switch (event.type)
	{
		case "mouseover":
		{
			switch (buttonType)
			{
				case "listimagebutton":
				{
					mySourceElement.style.cursor = "pointer";
					mySourceElement.src = "Images/Buttons/" + mySourceElement.name + "Over.jpg";
					break;
				}
						//
						// add case statements for new element buttonTypes that respond to the mouseover event here 
						//
			}// switch (buttonType)
			break;
		}
		
		case "mouseout":
		{
			switch (buttonType)
			{
				case "listimagebutton":
				{
					mySourceElement.style.cursor = "default";
					mySourceElement.src = "Images/Buttons/"+ mySourceElement.name + ".jpg";
					break;
				}
						//
						// add case statements for new element buttonTypes that respond to the mouseout event here 
						//
			}// switch (buttonType)
			break;
		}
			
		case "mousedown":
		{
			switch (buttonType)
			{
				case "listimagebutton":
				{
					switch (mySourceElement.name)	
					{
						case "AddList":
						{
									// button name attribute contains the product item string
									//		product catagory:product item:product code:product description:product pack size:quantity
							var quantity = parent.frames['ProductFrame'].document.getElementById(mySourceElement.title).value;
							if (quantity == "")
							{
								alert("Unfortunately you haven't entered a valid quantity. Plese re-enter the quantity you require.");
								parent.frames['ProductFrame'].document.getElementById(mySourceElement.title).value = "0";
								return;
							}
							if(isNaN(quantity))
							{
								alert("Unfortunately you haven't entered a valid quantity. Plese re-enter the quantity you require.");
								parent.frames['ProductFrame'].document.getElementById(mySourceElement.title).value = "0";
								return;
							}
							
							quantity = parseInt(quantity);
							if(quantity <=0)
							{
								alert("Unfortunately you haven't told us how many you want.  Please set the quantity you require.");
								return;
							}
							parent.frames['CatagoryFrame'].mylist.additem(mySourceElement.title + ":" + quantity);
							top.focus();
							break;
						}	//case "addList":
						
						case "ShowList":
						{
							var shoppingList = new Array();
							var listLineCount =  parseInt(parent.frames['CatagoryFrame'].mylist.gettotallines());
							if (listLineCount == 0)
							{
								alert("Please add some products from the categories on the left to your shopping basket before you can view it");
							}
							else
							{
								//NOTE:
	        					//		THIS IS A STUPID WAY (PASSING AS A VARIABLE) OF DEALING WITH A CONSTANT VALUE BUT
	        					//			THE ONLY WAY ALL FUNCTIONS WORK IN DIFFERENT BROWSERS
								parent.frames['ProductFrame'].location = 'ShoppingListFrame.php?ProdC=productcode&ProdQ=productquantityvalue';
							}
							break;
						}	// case "showList":
						
						case "EmailList":
						{
							parent.frames['ProductFrame'].location = 'EmailShoppingListFrame.php';
							break;
						}
						
						case "UpdateList":
						{
								// retrieve current shopping list
							var savedList = new Array();
							savedList = parent.frames['CatagoryFrame'].mylist.getList();
								//	retrieve number of items in the list
							var listLineCount =  parent.frames['CatagoryFrame'].mylist.gettotallines();							
									//		loop through each item
									//			find product code & quantity
									//				use the unique element ids (productcode+item number & productquantityvalue+item number) to find the elements
									//			find product in the current saved list - use product code as unique identifier
									//				update the quantity value
									//				rebuild the list item string
									//				save back into the list
									//		save the list
									//		update the display		
							var counter = 1;
							var codeId = "";
							var quantityId = "";
							var quantity = 0;
							var code = "";
							var savedListCounter = 0;
							var tmparray = new Array();
							var tmpcounter = 0;
							var itemString = "";
							var found = false;
							
							while (counter <= listLineCount)
							{
										// find product code
										//		.document.getElementById(codeId).innerText  works for IE only
										//		.document.getElementById(codeId).textContent works for firefox and possibly other browsers
								codeId = "productcode" + counter;
								code = null;
								code =  parent.frames['ProductFrame'].document.getElementById(codeId).innerText;
								if (code == null)
								{
									code =  parent.frames['ProductFrame'].document.getElementById(codeId).textContent;
								}

									// remove special characters that appear on the front of the code string
									// 		otherwise match () doesn't work
								code = code.trim(tmparray[2] == code);
								
										// find corresponding quantity
							  	quantityId = "productquantityvalue" + counter;
								quantity =  parent.frames['ProductFrame'].document.getElementById(quantityId).value;
								if (quantity == "")
								{
									alert("Unfortunately you haven't entered a valid quantity for item " + counter + ". Please re-enter the quantity you require.");
									return;
								}
				
								if(isNaN(quantity))
								{
									alert("Unfortunately you haven't entered a valid quantity for item " + counter + ". Please re-enter the quantity you require.");
									return;
								}
								quantity = parseInt(quantity);
								
									// find entry corresponding to code in current saved list
								savedListCounter = 0;
								while (savedListCounter <= savedList.length - 1)
								{
									found = false;
									tmparray = null;
									tmparray = savedList[savedListCounter].split(":");
									if (tmparray[2] == code)
									{
										found = true;
											// update the quantity value
										tmparray[5] = quantity;
											// rebuild the string
										tmpcounter = 0;
										itemString = "";
											// rebuild the item string
										while (tmpcounter <= tmparray.length - 1)
										{
											if (tmpcounter == 0)
											{
												itemString = tmparray[tmpcounter];
											}
											else
											{
												itemString = itemString + ":" + tmparray[tmpcounter];
											}
											tmpcounter++;
										}
											// update the list item 
										savedList[savedListCounter] = itemString;
										
											// found don't need to test the other members
										savedListCounter = savedList.length;
									}
									
									savedListCounter++;
								}	//  while (savedListCounter <= savedList.length - 1)
								
								if(found == false)
								{
									alert("Unfortunately an error occured trying to match the items on display with those saved in the list.  The list probably won't be updated correctly");
								}
								
								counter++;
							}  // while (counter <= listItemCount)
							
									//save the updated list
							if (parent.frames['CatagoryFrame'].mylist.savelist(savedList) == false)
							{
								alert("Unable to Update the shopping list.  Shoppings lists will not work correctly.");
								parent.frames['ProductFrame'].location ='ProductFrame.php?ProdCat=Home&ProdId=XXX&BType=headerimagebutton';
							}
							else
							{
									// refresh the frame
									//		return to home page it the list now contains no items
								listItemCount =  parseInt(parent.frames['CatagoryFrame'].mylist.gettotalitems());
								if (listItemCount == 0)
								{
									parent.frames['ProductFrame'].location ='ProductFrame.php?ProdCat=Home&ProdId=XXX&BType=headerimagebutton';
								}
								else
								{
									//NOTE:
		        					//		THIS IS A STUPID WAY (PASSING AS A VARIABLE) OF DEALING WITH A CONSTANT VALUE BUT
		        					//			THE ONLY WAY ALL FUNCTIONS WORK IN DIFFERENT BROWSERS
									parent.frames['ProductFrame'].location = 'ShoppingListFrame.php?ProdC=productcode&ProdQ=productquantityvalue';
								}
								alert ("You have updated your basket, you can now print or email your new basket");
							}
							break;
						}	// case "updateList":
						

						case "DeleteItem":
							{
								// retrieve current shopping list
							var savedList = new Array();
							savedList = parent.frames['CatagoryFrame'].mylist.getList();
								//	retrieve number of items in the list
							var listLineCount =  parent.frames['CatagoryFrame'].mylist.gettotallines();							
									//		loop through each item
									//			find product code & quantity
									//				use the unique element ids (productcode+item number & productquantityvalue+item number) to find the elements
									//			find product in the current saved list - use product code as unique identifier
									//				update the quantity value
									//				rebuild the list item string
									//				save back into the list
									//		save the list
									//		update the display		
							var counter = 1;
							var codeId = "";
							var quantityId = "";
							var quantity = 0;
							var code = "";
							var savedListCounter = 0;
							var tmparray = new Array();
							var tmpcounter = 0;
							var itemString = "";
							var found = false;
							
							while (counter <= listLineCount)
							{
										// find product code
										//		.document.getElementById(codeId).innerText  works for IE only
										//		.document.getElementById(codeId).textContent works for firefox and possibly other browsers
								codeId = "productcode" + counter;
								code = null;
								code =  parent.frames['ProductFrame'].document.getElementById(codeId).innerText;
								if (code == null)
								{
									code =  parent.frames['ProductFrame'].document.getElementById(codeId).textContent;
								}

									// remove special characters that appear on the front of the code string
									// 		otherwise match () doesn't work
								code = code.trim(tmparray[2] == code);
								
										// find corresponding quantity
							  	quantityId = "productquantityvalue" + counter;
								quantity =  parent.frames['ProductFrame'].document.getElementById(quantityId).value;
								if (quantity == "")
								{
									alert("Unfortunately you haven't entered a valid quantity for item " + counter + ". Please re-enter the quantity you require.");
									return;
								}
				
								if(isNaN(quantity))
								{
									alert("Unfortunately you haven't entered a valid quantity for item " + counter + ". Please re-enter the quantity you require.");
									return;
								}
								quantity = parseInt(quantity);
								
									// find entry corresponding to code in current saved list
								savedListCounter = 0;
								while (savedListCounter <= savedList.length - 1)
								{
									found = false;
									tmparray = null;
									tmparray = savedList[savedListCounter].split(":");
									if (tmparray[2] == code)
									{
										found = true;
											// update the quantity value
										tmparray[5] = quantity;
											// rebuild the string
										tmpcounter = 0;
										itemString = "";
											// rebuild the item string
										while (tmpcounter <= tmparray.length - 1)
										{
											if (tmpcounter == 0)
											{
												itemString = tmparray[tmpcounter];
											}
											else
											{
												itemString = itemString + ":" + tmparray[tmpcounter];
											}
											tmpcounter++;
										}
											// update the list item 
										savedList[savedListCounter] = itemString;
										
											// found don't need to test the other members
										savedListCounter = savedList.length;
									}
									
									savedListCounter++;
								}	//  while (savedListCounter <= savedList.length - 1)
								
								if(found == false)
								{
									alert("Unfortunately an error occured trying to match the items on display with those saved in the list.  The list probably won't be updated correctly");
								}
								
								counter++;
							}  // while (counter <= listItemCount)
							
									//save the updated list
							if (parent.frames['CatagoryFrame'].mylist.savelist(savedList) == false)
							{
								alert("Unable to Delete item.  Shoppings lists will not work correctly.");
								parent.frames['ProductFrame'].location ='ProductFrame.php?ProdCat=Home&ProdId=XXX&BType=headerimagebutton';
							}
							else
							{
									// refresh the frame
									//		return to home page it the list now contains no items
								listItemCount =  parseInt(parent.frames['CatagoryFrame'].mylist.gettotalitems());
								if (listItemCount == 0)
								{
									parent.frames['ProductFrame'].location ='ProductFrame.php?ProdCat=Home&ProdId=XXX&BType=headerimagebutton';
								}
								else
								{
									//NOTE:
		        					//		THIS IS A STUPID WAY (PASSING AS A VARIABLE) OF DEALING WITH A CONSTANT VALUE BUT
		        					//			THE ONLY WAY ALL FUNCTIONS WORK IN DIFFERENT BROWSERS
									parent.frames['ProductFrame'].location = 'ShoppingListFrame.php?ProdC=productcode&ProdQ=productquantityvalue';
								}
								alert ("You have deleted the selected item and updated your basket, you can now print or email your new basket");
							}
							break;
						}	// case "DeleteItem":

		
						case "PrintList":
						{
								// call function to print the current shopping list
								//		opens list in a new window and prints from there
								//			ensures only shopping list is printed
							parent.frames['CatagoryFrame'].mylist.printlist();								
							break;
						}
						case "ClearList":
						{
								// move to home page as the list will be empty
							parent.frames['ProductFrame'].location ='ProductFrame.php?ProdCat=Home&ProdId=XXX&BType=headerimagebutton';
								// reset cookies
							parent.frames['CatagoryFrame'].mylist.clearList();	
							break;
						}
						
					}	//switch (mySourceElement.name)
					break;	
				}	//case "listimagebutton":
								//
								// add case statements for new element ID that resopnd
								// to the mousedown event here
								//	
			}	//switch (buttonType)
			
			break;
		}	// case "mousedown":
		
		case "focus":   // used in the quantity box only so don't need to distinguish between the form objects
		{
			mySourceElement.select();
		}	//case "onfocus":
				//
				// add case statements for new mouse events here
				//	
	}	//switch (event.type)
	
} //  function shoppinglistMouseEventManager(event)


	// function called before the site is closed
	//		called from beforeonUnload event attached to the CatagoryFrame.php
	//			used to cleanup
	//			activated when site is closed or pafe refreshed(F5) 
function siteclose()
{
	parent.frames['CatagoryFrame'].mylist.cleanwindows();
}



