if (typeof iconCol == "undefined") iconCol = "(-)";
if (typeof iconExp == "undefined") iconExp = "(+)";

// Expands/collapses the specified menu item
function toggleDisplay(id, action) {
	var div=document.getElementById(id);
	var sign=document.getElementById(id+"sign");
		
	if (div && sign) {
		if (typeof action == 'undefined' || action.length < 1) action = sign.title;
		// Added for backwards compatibility ('-' and '+' are actions parameters in v1, and some non-standard menus use them still)
		else if (action.indexOf("-") > -1) action = "Collapse";
		else if (action.indexOf("+") > -1) action = "Expand";
	
		if (action == "Collapse") { // Collapsing the list
			sign.innerHTML = iconExp;
			sign.title = "Expand";
			div.style.display = "none";
			}
		else { // Expanding the list
			sign.innerHTML = iconCol;
			sign.title = "Collapse";
			div.style.display = "";
			}
		
		/* document.cookie = "menuState=null; ; path=/"; */
		}
	}

// Expands/collapses an item and its children
function toggleChildren(id, action) {
	var div=document.getElementById(id);
	var nodetype = 1;
	var tagname = 'DIV';
	if (typeof action == 'undefined') action = "";
	
	if (div && div.childNodes.length > 0)
		for (var i = 0; i < div.childNodes.length; i++) 
			if (div.childNodes[i].nodeType == nodetype && div.childNodes[i].nodeName == tagname) {
				toggleDisplay(div.childNodes[i].id, action);
				toggleChildren(div.childNodes[i].id, action);
				}
	}

// Appends an entry to a list using the "," delimiter
function addToList(list,value) { 
	if (list.length == 0) list = value;
	else if (value.length > 0) list = list + ',' + value;
	
	return list;
	}

// Read through the cookie and expand specified menu items, if any
function readCookies(id) {
	var idx = document.URL.indexOf('?');
	var ca = document.cookie.split('; ');
	var foundcookie = false;

	if (idx != -1) ca = ca.concat(document.URL.substring(idx+1,document.URL.length).split('&'));
	
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		if (c.indexOf(id) == 0) {
			foundcookie = true;
			toggleChildren(id,'Collapse');
			var idsToExpand = c.substring(c.indexOf('=')+1).split(',');
			for (var j = 0; j < idsToExpand.length;j++) 
				toggleDisplay(idsToExpand[j],'Expand');
			}		
		}

	return foundcookie;
	}

function returnExpandedIds(id) {
	var sign=document.getElementById(id+"sign");
	var div=document.getElementById(id);
	var nodetype = 1;
	var tagname = 'DIV';
	var expandedIds = '';
	
	if (div.childNodes.length > 0)
		for (var i = 0; i < div.childNodes.length; i++) 
			if (div.childNodes[i].nodeType == nodetype && div.childNodes[i].nodeName == tagname)
				expandedIds = addToList(expandedIds,returnExpandedIds(div.childNodes[i].id));
	if (div.style.display.indexOf('none') == -1) expandedIds = addToList(expandedIds,id);
	
	return expandedIds;
	}

function savestatewithlink(id,days,hours,minutes,linkid) {
	savestate(id,days,hours,minutes);
	if ( !document.cookie ) {
		var link=document.getElementById(linkid+"link");
		var idx = link.href.indexOf('?');
		var nextseperator = "?";
	
		if (idx != -1) {
			var ca = link.href.substring(idx+1,link.href.length).split('&');
			link.href = link.href.substring(0,idx);	//Remove existing query section from href
			for (var i = 0; i < ca.length; i++) {
				var c = ca[i];
				if (c.indexOf(id) == -1) {
					link.href = link.href + nextseperator + c;
					nextseperator = "&";
					}
				}
			}
		
		link.href = link.href + nextseperator + menuid + "=" + returnExpandedIds(id);
		}
	}

function savestate(id,days,hours,minutes) {
	var div = document.getElementById(id);
	var expiration = (days*24*60*60*1000) + (hours*60*60*1000) + (minutes*60*1000);
	var nodetype = 1;
	var tagname = 'DIV';
	var cookievalue = '';
	var expire = '';
	if (div && div.childNodes.length > 0)
		for (var i = 0; i < div.childNodes.length; i++) 
			if (div.childNodes[i].nodeType == nodetype && div.childNodes[i].nodeName == tagname)
				cookievalue = addToList(cookievalue,returnExpandedIds(div.childNodes[i].id));
	date = new Date();
	date.setTime(date.getTime() + expiration);

	if (expiration>0) expire = '; expires=' + date.toGMTString();

	document.cookie = id + '=' + cookievalue + expire + '; path=/';
	}

// Expand/collapse all items in the menu
function setAll(rootId, action) {
	toggleChildren(rootId, action);
	/*document.cookie = "menuState=" + action + "; ; path=/";*/
	}
