/// Instantiate the xmlHttp class
var xmlHttp;

function getXmlHttp()
{
	/// IE
	if(window.ActiveXObject)
	{
		xmlHttp	= new ActiveXObject("Microsoft.XMLHTTP");
	}
	/// Mozilla
	else if(window.XMLHttpRequest)
	{
		xmlHttp	= new XMLHttpRequest();
	}
	/// Not supported
	else
	{
		alert("Your browser does not support the product catalogue.");
	}
}

function toggleMenu(id, preloadId)
{
	if(preloadId == null)
	{
		preloadId	= "";
	}
	
	var parentId	= id.substring(0, id.lastIndexOf("_"));
	var menuItems	= document.getElementById("menuItems_"+parentId);

	if(menuItems != null)
	{
		var siblingIds	= menuItems.value.split(",");
		
		for(var i = 0; i < siblingIds.length; i++)
		{
			var fullId	= parentId+"_"+siblingIds[i];
			var menu 	= document.getElementById("menu_"+fullId);
			var	link	= document.getElementById("link_"+fullId);
			
			if(link != null)
			{
				if(fullId == id)
				{
					link.className	= "blueText";
				}
				else
				{
					link.className	= "";
				}
			}
			
			if(menu != null)
			{
				var menuContent	= document.getElementById("menuContent_"+fullId);
				
				/// Only load if it's the selected id and it's never been loaded
				if(menuContent.innerHTML == "" && id == fullId)
				{
					menuContent.innerHTML	= "Loading...";
					loadMenu(id, preloadId);
				}

				menu.style.display	= (fullId == id && menu.style.display != "")
					? ""
					: "none";
			}
		}
	}
}

function loadMenu(id, preloadId)
{
	getXmlHttp();
	
	/// Assign the event when a state changes
	xmlHttp.onreadystatechange	= xmlHttp_StateChanged;

	xmlHttp.open("GET", webRoot+"Controls/Catalogue.aspx?id="+id+"&preload="+preloadId, true);
	xmlHttp.send(null);	
}

function xmlHttp_StateChanged()
{
	var completed	= 4;
	
	if(xmlHttp.readyState == completed)
	{
		/// Regular expression to pickout
		var pattern	= /menuItems[^"]*/i;
		var id		= "";
		
		try
		{
			id	= xmlHttp.responseText.match(pattern).toString().replace("menuItems_", "");
			
			var subMenu	= document.getElementById("menuContent_"+id);
			subMenu.innerHTML	= xmlHttp.responseText;
		}
		catch(ex)
		{
			alert("Error loading data");
		}
	}
}

