// an array to hold names of script that will be run on page load
var onloadFunctions = new Array();

// a function to add a function to the onload function array
function setOnloadFunction(functionName) {
    onloadFunctions[onloadFunctions.length] = functionName;
}

// convenience for those who disagree if the L should be capitalized or not
function setOnLoadFunction(functionName) {
    setOnloadFunction(functionName)
}


// the master function that runs on page load
function executeOnloadFunctions() {
    for (var i=0; i<onloadFunctions.length; i++) {
        eval(onloadFunctions[i]);
    }
}

window.onload = executeOnloadFunctions;


/** Javascript from the tabbed example from struts-menu
    Checks current (active) menu against current document location. **/
if (!document.getElementById) {
    document.getElementById = function() { return null; }
}

function initMenu() {
    var uls = document.getElementsByTagName("ul");
    for (i = 0; i < uls.length; i++) {
        if (uls[i].className == "menuList") {
            decorateMenu(uls[i]);
        }
    }
}

function decorateMenu(menu) {
    var items = menu.getElementsByTagName("li");
    for (var i=0; i < items.length; i++) {
        items[i].firstChild.myIndex = i;
        // retain any existing onclick handlers from menu-config.xml
        if (items[i].firstChild.onclick) {
            items[i].firstChild.onclick=function() {
                eval(items[this.myIndex].firstChild.getAttribute("onclick"));
                setCookie("menuSelected", this.myIndex);
                };
        } else {
            items[i].firstChild.onclick=function() {
                setCookie("menuSelected", this.myIndex);
            };
        }
    }
    activateMenu(items);
}

function activateMenu(items) {
    var activeMenu;
    var numExactMatch = 0;
    var numPartialMatch = 0;
    var firstExactMatchIndex = 0;
    var firstPartialMatchIndex = 0;
    var current = document.location.toString();

    for (var i=0; i < items.length; i++) {
        var url = items[i].firstChild.getAttribute("href");
        var matchIndex = current.indexOf(url);

        if (matchIndex != -1) {
            // If document URL exactly ends with menu URL
            // (because menu isn't configured with host:port URL),
            // then it's an exact match
            if ((matchIndex + url.length) == current.length) {
                if (numExactMatch == 0) {
                    firstExactMatchIndex = i;
                }
                numExactMatch++;
            } else {
                if (numPartialMatch == 0) {
                    firstPartialMatchIndex = i;
                }
                numPartialMatch++;
            }
        }
    }
	if ((numExactMatch == 0) && (numPartialMatch == 0)) {
		if (current.toLowerCase().indexOf("press") != -1) {
			for (var i=0; i < items.length; i++) {
		        var url = items[i].firstChild.getAttribute("href");
		        if (url.toLowerCase().indexOf("press-releases") != -1) {
		        	firstExactMatchIndex = i;
		        	numExactMatch++;
		        }
		    }
		}
		if (numExactMatch == 0) {
			if (current.toLowerCase().indexOf("news") != -1) {
				for (var i=0; i < items.length; i++) {
			        var url = items[i].firstChild.getAttribute("href");
			        if (url.toLowerCase().indexOf("news") != -1) {
			        	firstExactMatchIndex = i;
			        	numExactMatch++;
			        	break;
			        }
			    }
			}
		}
	}
    if (numExactMatch == 1) {
        if (items[firstExactMatchIndex].parentNode.className == "submenu") {
            items[firstExactMatchIndex].firstChild.className="active";
            items[firstExactMatchIndex].parentNode.parentNode.className="active";
        } else {
            items[firstExactMatchIndex].className+="active";
        }
    }
    else if (numExactMatch > 1) {
        var activeIndex = firstExactMatchIndex;
        var menuSelected = getCookie("menuSelected");

        // If menuSelected index makes sense, then use it
        if (menuSelected != null) {
            var url = items[menuSelected].firstChild.getAttribute("href");
            var matchIndex = current.indexOf(url);

            if ((matchIndex + url.length) == current.length) {
                activeIndex = menuSelected;
            }
        }

        if (items[activeIndex].parentNode.className == "submenu") {
            items[activeIndex].firstChild.className="active";
            items[activeIndex].parentNode.parentNode.className="active";
        } else {
            items[activeIndex].className+="active";
        }
    }
    else if (numPartialMatch == 1) {
        if (items[firstPartialMatchIndex].parentNode.className == "submenu") {
            items[firstPartialMatchIndex].firstChild.className="active";
            items[firstPartialMatchIndex].parentNode.parentNode.className="active";
        } else {
            items[firstPartialMatchIndex].className+="active";
        }
    }
    else if (numPartialMatch > 1) {
        var activeIndex = firstPartialMatchIndex;
        var menuSelected = getCookie("menuSelected");

        // If menuSelected index makes sense, then use it
        if ((menuSelected != null) &&
            (current.indexOf(items[menuSelected].firstChild.getAttribute("href")) != -1)) {
            activeIndex = menuSelected;
        }

        if (items[activeIndex].parentNode.className == "submenu") {
            items[activeIndex].firstChild.className="active";
            items[activeIndex].parentNode.parentNode.className="active";
        } else {
            items[activeIndex].className+="active";
        }
    }
}

// Select the menu that matches the URL when the page loads
setOnloadFunction("initMenu()");

// =========================================================================
//                          Cookie functions
// =========================================================================
/* This function is used to set cookies */
function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

/* This function is used to get cookies */
function getCookie(name) {
	var prefix = name + "="
	var start = document.cookie.indexOf(prefix)

	if (start==-1) {
		return null;
	}

	var end = document.cookie.indexOf(";", start+prefix.length)
	if (end==-1) {
		end=document.cookie.length;
	}

	var value=document.cookie.substring(start+prefix.length, end)
	return unescape(value);
}

/* This function is used to delete cookies */
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
