/*
** Project: Picky Noo
**
** File:    banquetbuilder.js
** Author:  NJW James
** Date:    12.03.2004
**	
** Copyright (c) NJW James 2004
*/
var aaMenus = new Array();
var aMyMenu = new Array();

var oCurrentMenu     = null;
var sCurrentMenuCode = "";
var oCurrentItem     = null;

// Define submenus.

var oSubmenuO = new Object();
var oSubmenuS = new Object();
var oSubmenuM = new Object();
var oSubmenuV = new Object();

function onLoadBanquetBuilder()
{
	// Populate the menu drop-list. First add an item containing "--Please Select--".
	var oOption = new Option();
		
	oOption.value = "0";
	oOption.text  = "--Please Select--";

	// Add the option to the menu select object.
	document.all.selMenu.options[0] = oOption;
	
	var oMenusXml = new ActiveXObject("Microsoft.XMLDOM");

	oMenusXml.async = false;
	oMenusXml.load("getmenusxml.aspx");

	var aMenus = oMenusXml.getElementsByTagName("menu");

	for (nIndex = 0; nIndex < (aMenus.length); ++nIndex)
	{
		var oThisMenu = aMenus[nIndex];
		var sMenuCode = oThisMenu.getAttribute("code");

		var oMenuName = oThisMenu.firstChild;
		var sMenuName = oMenuName.nodeValue;
		
		oOption = new Option();
		
		oOption.value = sMenuCode;
		oOption.text  = sMenuName;

		// Add the option to the select object.
		document.all.selMenu.options[document.all.selMenu.options.length] = oOption;
	}
}

function onChangeMenu()
{
	// Handler for change of selection in the menu drop list.
	var oXMLDOM = new ActiveXObject("Microsoft.XMLDOM");

	oXMLDOM.async = false;

	// Get the index of the selected menu.	
	var nSelMenuInd = document.all.selMenu.selectedIndex;
	
	// Clear menu items list and item description box.		
	document.all.selMenuItem.options.length = 0;
	document.all.tdDescription.innerText = "";

	if (nSelMenuInd != 0)
	{
		// Menu selected. Check whether this menu is cached. First, get the menu code for the selected menu.		
		var oSelMenuOption =
			document.all.selMenu.options[nSelMenuInd];
			
		sCurrentMenuCode = oSelMenuOption.value;
		
		if (aaMenus[sCurrentMenuCode] == null)
		{
			// Need to get the menu from the server. Create a menu array to hold the menu items.
			var aaMenu = new Array();		
			var sURL = "getmenuxml.aspx?menu=";
	
			sURL += sCurrentMenuCode;

			var bResp = oXMLDOM.load(sURL);
			var aItems = oXMLDOM.getElementsByTagName("item");

			for (nIndex = 0; nIndex < (aItems.length); ++nIndex)
			{
				var oThisElement = aItems.item(nIndex);
				var sItemCode = oThisElement.getAttribute("code");
				var sItemName = oThisElement.getAttribute("name");
				var sItemDesc = oThisElement.getAttribute("desc");

				// Create a menu item object and initialise it. We store the menu code, and item code, name and
				// description on the object, and key it in its parent object on the code.
				var oMenuItem = new Object();
				
				oMenuItem.sMenuCode = sCurrentMenuCode;
				oMenuItem.sItemCode = sItemCode;
				oMenuItem.sItemName = sItemName;
				oMenuItem.sItemDesc = sItemDesc;
				
				// Add this item to the menu object.
				aaMenu[sItemCode] = oMenuItem;
			}
	
			// Finally, add the new menu object to the menus array.
			aaMenus[sCurrentMenuCode] = aaMenu;
		}
		
		oCurrentMenu = aaMenus[sCurrentMenuCode];

		// Now we can populate the menu items drop-list. First, add an item containing "--Please Select--".
		var oOption = new Option();
		
		oOption.value = "0";
		oOption.text  = "--Please Select--";

		// Add the option to the select object.
		document.all.selMenuItem.options[0] = oOption;

		// Now add the actual menu items.
		for (oThisKey in oCurrentMenu)
		{
			var oThisItem = oCurrentMenu[oThisKey];
			oOption = new Option();
			
			oOption.text = oThisItem.sItemCode;
			oOption.text += ". ";
			oOption.text += oThisItem.sItemName;
			
			oOption.value = oThisItem.sItemCode;
			
			// Add the option to the select object.
			
			document.all.selMenuItem.options[
				document.all.selMenuItem.options.length] = oOption;
		}
		
		// Switch item selection to "--Please Select--".		
		document.all.selMenuItem.options[0].selected = true;
	}
};

function onChangeMenuItem()
{
	var nSelInd = document.all.selMenuItem.selectedIndex;

	if (nSelInd != 0)
	{
		// Get the description for the selected menu item and place it in the description box. First get the
		// index of the selected menu item.
		var nItemInd = document.all.selMenuItem.selectedIndex;

		// Get the item code for the selected item.		
		var sSelItemCode =
			document.all.selMenuItem.options[nItemInd].value;

		// Store the currently selected menu item.		
		oCurrentItem = oCurrentMenu[sSelItemCode];
		
		// Now get the description of the selected menu item.
		var sDesc = oCurrentItem.sItemDesc;

		// Finally, store the item description in the table.		
		document.all.tdDescription.innerText = sDesc;
	}
	else
	{
		// User has selected the PS option. Just clear the description box and set the current item to null.
		document.all.tdDescription.innerText = "";
		oCurrentItem = null;
	}
};

function onAdd()
{
	// Determine which submenu this item belongs to.	
	var oSubmenu;

	switch (oCurrentItem.sMenuCode)
	{
		case "O":
			oSubmenu = oSubmenuO;
			break;
		case "S":
			oSubmenu = oSubmenuS;
			break;
		case "V":
			oSubmenu = oSubmenuV;
			break;
		default:
			oSubmenu = oSubmenuM;
			break;
	}

	// Check that the item being added does not already exist in the relevant submenu.
	var sNewItemCode = oCurrentItem.sItemCode;

	if (oSubmenu[sNewItemCode] != null)
	{
		// This item already exists.		
		alert("You already have this item in your menu!");
		return;
	}
	
	// The item does not exist already, so add it.
	oSubmenu[sNewItemCode] = oCurrentItem;

	// Rebuild the My Menu HTML.	
	buildMyMenu();
}

function buildMyMenu()
{
	var nItemsO = 0;
	var nItemsS = 0;
	var nItemsV = 0;
	var nItemsM = 0;
	
	var nItemNum = 0;

	// Delete all the existing rows of the My Menu table except the header row (row 0).
	for (n = (document.all.tblMyMenu.rows.length - 1); n > 0; n--)
	{
		document.all.tblMyMenu.deleteRow(n);
	}

	// Add items from Soup submenu.	
	for (oThisItemCode in oSubmenuO)
	{
		var oThisItem = oSubmenuO[oThisItemCode];
		
		if (oThisItem != null)
		{
			addItem(oThisItem, ++nItemNum, "O");
			++nItemsO;
		}
	}
	
	// Add items from Starter submenu.
	for (oThisItemCode in oSubmenuS)
	{
		var oThisItem = oSubmenuS[oThisItemCode];
		
		if (oThisItem != null)
		{
			addItem(oThisItem, ++nItemNum, "S");
			++nItemsS;
		}
	}
	
	// Add items from Vegetable submenu.
	for (oThisItemCode in oSubmenuV)
	{
		var oThisItem = oSubmenuV[oThisItemCode];
		
		if (oThisItem != null)
		{
			addItem(oThisItem, ++nItemNum, "M");
			++nItemsV;
		}
	}
	
	// Add items from Meat submenu.
	for (oThisItemCode in oSubmenuM)
	{
		var oThisItem = oSubmenuM[oThisItemCode];
		
		if (oThisItem != null)
		{
			addItem(oThisItem, ++nItemNum, "M");
			++nItemsM;
		}
	}
	
	// Check menu validity if basic course counts are above the minima.
	if ((nItemsO + nItemsS + nItemsV + nItemsM) < 8)
		return;
	
	var sMsg = "";
	
	if (nItemsO > 1)
	{
		sMsg +=
			"You have included more than one Soup course.\n";
	}
	
	if (nItemsS > 4)
	{
		sMsg +=
			"You have included more than four Starter courses.\n";
	}

	if (nItemsV == 0)
	{
		sMsg +=
			"You have not included a Vegetable course.\n";
	}

	if (nItemsV > 1)
	{
		sMsg +=
			"You have included more than one Vegetable course.\n";
	}

	if (nItemsM < 3)
	{
		sMsg +=
			"You have not included three Meat courses.\n";
	}
	else if (nItemsM > 3)
	{
		sMsg +=
			"You have included more than three Meat courses.\n";
	}

	// If all is well so far, check that a Soup is present if there are only 3 starters.
	if (sMsg == "")
	{
		if ((nItemsS == 3) && (nItemsO == 0))
		{
			sMsg =
				"You have included only 3 Starter courses. " +
				"Please add an item from the Soups or "      +
				"Starters menus.";
		}
	}
	else
	{
		sMsg =
			"Your menu is not properly balanced. Please make " +
			"adjustments as indicated below:\n\n"              +
			sMsg;
	}
	
	// Finally, output the message if there is one.
	if (sMsg != "")
		alert(sMsg);
}
	

function addItem(oItem, nItemNum, sMenuType)
{
	// Add a new row to the My Menu table containing the details of the selected item.
	var trRow = document.all.tblMyMenu.insertRow();
	
	var tdCourse = trRow.insertCell();
	tdCourse.innerText =
		nItemNum + " (" + sMenuType + ")";
	tdCourse.setAttribute("align", "center");
		
	var tdItem = trRow.insertCell();
	tdItem.innerText = oItem.sItemName;

	var tdDesc = trRow.insertCell();
	tdDesc.innerText = oItem.sItemDesc;
	
	var tdDelete = trRow.insertCell();
	
	// Build up HTML for Delete button.
	var sDelHTML = '<input type="button" value="Delete" ';
	sDelHTML += 'onClick="onDelete()" id="';
	sDelHTML += oItem.sItemCode;
	sDelHTML += '">';
	
	tdDelete.innerHTML = sDelHTML;
};

function onDelete()
{
	// Get the click event object.
	var eClickEvent = this.event;
	var btnDelButton = eClickEvent.srcElement;
	var sItemCode = btnDelButton.id;

	// The id attribute of the Delete buttons is the item code of the item, which is the key against which the
	// item is stored in the submenu hash.
	
	// Find the submenu containing the item, and remove the item from it.
	if (oSubmenuO[sItemCode] != null)
	{
		oSubmenuO[sItemCode] = null;
	}
	else if (oSubmenuS[sItemCode] != null)
	{
		oSubmenuS[sItemCode] = null;
	}
	else if (oSubmenuV[sItemCode] != null)
	{
		oSubmenuV[sItemCode] = null;
	}
	else if (oSubmenuM[sItemCode] != null)
	{
		oSubmenuM[sItemCode] = null;
	}
	
	// Finally, rebuild the My Menu HTML.	
	buildMyMenu();
};

