window.onunload = function(){saveState(menuId);}
getState(menuId);  //menuId = 'Menu';
var openDisplay = "block";
var closedDisplay = "none";

// expandit function
//   calls toggleSection function after a
//   short delay so that images display 
//   correctly
function expandit(itemId)  
{
	//itemId 格式：WebBuilder_Menu_SectionPanel_8
	window.setTimeout("toggleSection('" + itemId + "')",10);
}

// toggleSection function
//   opens or closes menu section 
function toggleSection(itemId) {
	
	//alert(itemId);
	
			var arrowId = itemId.replace("SectionPanel", "ArrowImage");
			var arrow, item;
		    
			if (document.all) {
				item = document.all[itemId];
				arrow = document.all[arrowId];
			}
		    
			if (!document.all && document.getElementById) {
				item = document.getElementById(itemId);
				arrow = document.getElementById(arrowId);
			}
		
			if (item.style != null) {
			
			
					if (item.style.display == openDisplay  ) {
						arrow.src = arrowRight.src;
						item.style.display = closedDisplay;
					}
					else {
						arrow.src = arrowDown.src;	
						item.style.display = openDisplay;		
					}
					
			}
			else {
				item.style.display = closeDisplay;
				
			}

			
			
	
	
}

// saveState function
//   sets the state of each menu section and 
//   adds the open menu sections to a cookie
function saveState(menuId) {			
	//menuID 格式：	WebBuilder_Menu_SectionPanel_1
	//相关图片格式：WebBuilder_Menu_ArrowImage_1
	//alert("menuId:"+menuId);
	
	
		var sectionCount = 0;
        var sections= document.getElementsByTagName("table");
        var realSections = new Array();
        
        for (i = 0; i < sections.length; i++) {
            if (sections[i].getAttribute("name") == "WebBuilder_Menu_SectionPanel") {
               
                realSections[sectionCount] = sections[i];
                sectionCount++; 
            }
        }

	
			var cookieInfo = "";
			for (i = 0; i < realSections.length; i++) {
			
				var item = realSections[i];
				
				if (item.style.display == openDisplay ) 
					cookieInfo = cookieInfo + "|" + item.id;; 	
				
				
			}
			
			setCookie(menuCookieName,cookieInfo, getExpirationDate(1));
			//alert("in saveState method : getCookie(menuCookieName) :" + getCookie(menuCookieName));
		
}

// getState function
//   gets and opens menu sections based on 
//   the values from the cookie set in saveState
function getState(menuId) {
	

					var sections = new Array();
					sections = getCookie(menuCookieName).split("|");
					/////alert(getCookie(menuCookieName));
					/////alert(sections.length);
					var realsections = new Array();
					var j = 0;
					for (i = 0; i < sections.length; i++) {
						/////alert("sections[i]" + sections[i]);
						//if (sections[i] != null && sections[i] != ""  ) {   //朱振寰修改
						if (sections[i] != null && sections[i] != "" && sections[i] != ";" ) {	
							realsections[j] = sections[i] ;
							j++;
						}
					}
					/////alert(realsections.length);
					var index = 0;
					//alert("In getState method: sections:'"+sections+"'  sections.length:"+sections.length+" ");
					for (i = 0; i < realsections.length; i++) {
						//alert("executing getState for statement...  i:" + i );
						//alert(realsections[i]);
						
						if (i < realsections.length) {
							//var itemId = menuClientId.replace(menuId + "_Menu",menuId + "_MenuSections__ctl" + realsections[i] + "_SectionPanel");
							//alert("exectuing expandit("+itemId+")");
							//var itemId = "WebBuilder_Menu_SectionPanel_" + realsections[i];
							//expandit(itemId);
							expandit(realsections[i]);
						}
					}
					
		
}	

// getCookie function
//   gets the menu cookie
function getCookie(cookieName) {

		
				var cookie;
				cookie = "" + document.cookie;
				//alert("getcookie:   " + document.cookie);
				var start = cookie.indexOf(cookieName);
				if (cookie == "" || start == -1) 
					return "";
				var end = cookie.indexOf(';',start);
				if (end == -1)
					end = cookie.length;
				return unescape(cookie.substring(start+cookieName.length + 1,end));
				
		
}

// setCookie function
//   sets the menu cookie
function setCookie(cookieName, value, expires) {
	
			//alert(cookieName + " " + value);
			//注意：left control 中的连接如果不是在 path 目录中，那么不能正确读取 cookie
			//cookieInfo = cookieName + "=" + escape(value) + ";path=/admin"
			cookieInfo = cookieName + "=" + escape(value) + ";path=/";
			document.cookie = cookieInfo; 
			//alert("setcookie:   " + document.cookie);
			
			 
			return document.cookie;
	
}

// getExpirationDate function
//   gets the menu cookie from the browser
function getExpirationDate(days){
	
			today = new Date();
			today.setTime(Date.parse(today) + (days * 60 * 60 * 24 * 100));
			return  today.toUTCString();
	
}


