/**********************************************************************************
 * menus.js - handles the navagation drop down menus
 **********************************************************************************/
var bLoaded = false;
var dom = (document.getElementById != null);
var ie = (document.all != null);
var ns = (document.layers != null);

function getEl(id) {
	var obj;
	if (dom) {
		obj = document.getElementById(id);
	} else if (ie) {
		obj = eval("document.all." + id);
	} else if (ns) {
		obj = eval("document.layers[\"" + id + "\"]");
	}
	return obj;
}

function showEl(obj) {
	if ((obj == null) || (!bLoaded)) return;
	if (dom) {
		obj.style.visibility = "visible";
	} else if (ie) {
		obj.style.visibility = "visible";
	} else if (ns) {
		obj.visibility = "visible";
	}
}

function hideEl(obj) {
	if ((obj == null) || (!bLoaded)) return;
	if (dom) {
		obj.style.visibility = "hidden";
	} else if (ie) {
		obj.style.visibility = "hidden";
	} else if (ns) {
		obj.visibility = "hidden";
	}
}

function blockEl(obj) {
	if ((obj == null) || (!bLoaded)) return;
	if (dom) {
		obj.style.display = "block";
	} else if (ie) {
		obj.style.display = "block";
	} else if (ns) {
		obj.display = "block";
	}
}

function noneEl(obj) {
	if ((obj == null) || (!bLoaded)) return;
	if (dom) {
		obj.style.display = "none";
	} else if (ie) {
		obj.style.display = "none";
	} else if (ns) {
		obj.display = "none";
	}
}

function showMenu(controlid) {
	showEl(getEl("menuTabHover" + controlid));
	showEl(getEl("menu" + controlid));
	showEl(getEl("mousetrap"));
	negotiateSelects(getEl("menu" + controlid), true, false);		//Hide selects
	return;
}

function hideMenu(controlid) {
	hideEl(getEl("menuTabHover" + controlid));
	hideEl(getEl("menu" + controlid));
	negotiateSelects(getEl("menu" + controlid), false, false);		//Show selects
	return;
}

function showSubmenu(controlid) {
	var el = getEl("menu" + controlid);
	if (el) {
		showEl(el);
		negotiateSelects(getEl("menu" + controlid.split('-')[0]), true, false);
		negotiateSelects(el, true, true);							//Hide selects
	}
	return;
}

function hideSubmenu(controlid) {
	var el = getEl("menu" + controlid);
	if (el) {
		hideEl(el);
		//negotiateSelects(el, false, false);						//Hide selects
	}
	return;
}

function hideAll() {
	var i = 1;
	var obj = getEl("menuTabHover" + i);
	while (obj != null) {
		hideMenu(i);
		obj = getEl("menuTabHover" + (++i));
	}
}

function mouseTrap() {
	hideAll();
	hideEl(getEl("mousetrap"));
}

var clientAreaWidth = 0;
var clientAreaHeight = 0;
function handleResize() {
    var cWidth = clientAreaWidth;
    var cHeight = clientAreaHeight;    
    clientAreaWidth = getViewportWidth();
	clientAreaHeight = getViewportHeight();
    //if (((cWidth != clientAreaWidth) || (cHeight != clientAreaHeight)) && (clientAreaHeight < screen.height)) {
    //if ((cWidth != clientAreaWidth) || (cHeight != clientAreaHeight)) {
    //    bLoaded = false;
    //}
    depositionMenus();
    document.getElementById("mousetrapImg").width = clientAreaWidth;
    document.getElementById("mousetrapImg").height = clientAreaHeight - 10;
    positionMenus();
    //if (!(bLoaded)) {        
    //    positionFooter();
    //   bLoaded = true;
    //}
}
function handleResizeS() {
	depositionMenus();
	clientAreaWidth = getViewportWidth();
	clientAreaHeight = getViewportHeight();
	document.getElementById("mousetrapImg").width = clientAreaWidth;
	document.getElementById("mousetrapImg").height = clientAreaHeight - 10;
	positionMenus();
	bLoaded = true;
}
function depositionMenus() {
	var i = 1;
	var x;
	var obj = getEl("menu" + i);
	while (obj != null) {
		obj.style.left = "0px";
		obj = getEl("menu" + (++i));
	}
}
function positionFooter() {
    var pageHeaderHeight = 0;
        if (getEl("pageHeader")) {
            pageHeaderHeight = getEl("pageHeader").offsetHeight;
        }
    var footerKeyHeight = 0;
        if (getEl("footerKey")) {
            footerKeyHeight = getEl("footerKey").offsetHeight;
        }
    var extra = 0;
    var browser = navigator.appName;
        if (browser.indexOf('Netscape') != -1) {
            extra = 15;
        }    
    var newHeight = 0 ;
    obj = getEl("leftCol");
    obj.style.height = "0px";
    //DR31745
    //var availHeight = getViewportHeight();
    //if ((footerKeyHeight + pageHeaderHeight)  > availHeight) {
        newHeight = (footerKeyHeight - pageHeaderHeight) + extra;
    //}
    //else {
    //    newHeight = (availHeight - pageHeaderHeight) + extra;
    //}
    obj.style.height = newHeight + "px";
}
function positionMenus() {
	var i = 1;
	var isub = 1; //submenus
	var x;
	var obj = getEl("menu" + i);
	while (obj != null) {
		isub = 1;
		x = eval(obj.getAttribute("xpos"));
        //Because it is static for this site, we can set the y-axis value for the obj.
        //Remember, though, that we're only doing this to accommodate the 1px white line at the bottom of the nav images.
        //9-19-08 changed below value from 26px to 49px to accommodate the english/spanish links
        //  at top right of page JFW  DR 34581
        obj.style.top = "49px";
		if (x + obj.offsetWidth > clientAreaWidth) x -= (x + obj.offsetWidth - clientAreaWidth);
		obj.style.left = x + "px"; 
		
		var subobj = getEl("menu" + i + "-" + isub);
		while(subobj != null) {
            //Orginal subobj x positioning line follows
   			//subobj.style.left = (obj.offsetWidth-1) + "px"; 
            //Begin Tim code
            if ((eval(subobj.getAttribute("xpos")) + obj.offsetWidth + subobj.offsetWidth) > clientAreaWidth) {
                subobj.style.left = (obj.offsetWidth - obj.offsetWidth - subobj.offsetWidth + 1) + "px";
            }
            else {
                subobj.style.left = (obj.offsetWidth-1) + "px"; 
            }
            //End Tim code  
			subobj.style.top = ((findPosY(subobj.parentNode) - findPosY(obj))) + "px";
			subobj = getEl("menu" + i + "-" + (++isub));
		}
		
		obj = getEl("menu" + (++i));
		hideMenu(i);
	}
	obj = getEl("contentControl");
	if (obj != null) {
		//obj.style.left = (clientAreaWidth - 79) + "px";
		obj.style.left = "0px";
        //9-19-08 changed below value from 26px to 49px to accommodate the english/spanish links
        //  at top right of page JFW  DR 34581
		if (getEl("banner") == null) {
			obj.style.top = "49px";
		}
	}
}
function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getViewportWidth() {
	var width = 0;
	if( document.documentElement && document.documentElement.clientWidth ) {
		width = document.documentElement.clientWidth;
	}
	else if( document.body && document.body.clientWidth ) {
		width = document.body.clientWidth;
	}
	else if( window.innerWidth ) {
		width = window.innerWidth - 18;
	}
	return width;
}

function getViewportHeight() {
	var height = 0;
	if( document.documentElement && document.documentElement.clientHeight ) {
		height = document.documentElement.clientHeight;
	}
	else if( document.body && document.body.clientHeight ) {
		height = document.body.clientHeight;
	}
	else if( window.innerHeight ) {
		height = window.innerHeight - 18;
	}
	return height;
}

function getAvailHeight() {
	var height = 0;
	if( document.all && document.documentElement && document.documentElement.offsetHeight )
		height = document.documentElement.offsetHeight;
	else if( window.innerHeight )
		height = window.innerHeight + 4;
	else if( document.body && document.body.scrollHeight )
		height = document.body.scrollHeight;
	return height;
}

function openLink(link, target) {
	if (target == null) target = "_self";
	//alert(link + "\n" + document.location.href + "\n" + document.referrer);
	window.open(link, target);
	
	if (window.event && this.event.type) {
		this.event.cancelBubble = true;
	}
	else { 
		/* need to figure out a way to get event bubble cancellation into mozilla
		the current way is in the assignment of events to objects through script.
		example obj.onclick = foo;
		function foo(e) {
			//e = the event
		}
		this is not apparently available when events are written as strings to 
		server-side constructed html
		*/
	}
}

function openDepartmentLink(obj) {
	if (obj.value != "") { openLink(obj.value, "_self"); }
}
function openGroupLink(obj) {
	if (obj.value != "") { 
		var target = obj.options[obj.selectedIndex].getAttribute("target");
		if (target == null || target == "") target = "_self";
		openLink(obj.value, target); 
	}
}

function increaseContent() {
	var obj;
	var i = 1;
	var px = 12;
	obj = document.getElementById("content" + i++);
	if (obj != null && obj.style.fontSize != "")
		px = eval(obj.style.fontSize.replace("px",""));
	if (px >= 20) return;
	px++;
	while (obj != null) {
		obj.style.fontSize = px + "px";
		obj = document.getElementById("content" + i++);
	}
	saveFaceSize(px);
}

function decreaseContent() {
	var obj;
	var i = 1;
	var px = 12;
	obj = document.getElementById("content" + i++);
	if (obj != null && obj.style.fontSize != "")
		px = eval(obj.style.fontSize.replace("px",""));
	if (px <= 8) return;
	px--;
	while (obj != null) {
		obj.style.fontSize = px + "px";
		obj = document.getElementById("content" + i++);
	}
	saveFaceSize(px);
}

function saveFaceSize(fontSize) {
	var expire = new Date ();
	  	expire.setTime (expire.getTime() + (6000 * 24 * 3600000)); //expires in 6 days from users clock
	  	expire = expire.toGMTString();
	document.cookie="fontSize="+fontSize+"; path=/; expires="+expire;
}

function toolbarItemMouseOut(obj) {
	obj.parentNode.previousSibling.style.display = "none";
}
function toolbarItemMouseOver(obj) {
	obj.parentNode.previousSibling.style.display = "block";
}

function siteSearch() {
	var obj = document.getElementById("siteSearch");
	if (obj.value.length > 0)
		document.forms.frmSiteSearch.submit();
	else
		alert("Please enter your search query");
}
function siteSearch2() {
	var obj = document.getElementById("siteSearch2");
	if (obj.value.length > 0)
		document.forms.frmSiteSearch2.submit();
	else
		alert("Please enter your search query");
}

function toggleElement(id) {
	var obj = document.getElementById(id);
	
	if (obj.style.display == "none")
		obj.style.display = "block";
	else
		obj.style.display = "none";
}

//Toggle selects when menu is displayed
//Nehal Shah, 3/14/2003
function negotiateSelects(topEl, activate, keepHidden) {
	var allEls = document.getElementsByTagName('select');
	for (var i=0; i<allEls.length; i++) {
		if (activate) {
			if (topEl != allEls[i]) {
				if (checkOverlap(topEl, allEls[i])) {
					hideEl(allEls[i]);
				}
				else {
					if (!keepHidden) showEl(allEls[i]);
				}
			}
		}
		else {
			showEl(allEls[i]);
		}
	}
}
	
function checkOverlap(topEl, bottomEl) {
	var dims1 = getDimensions(topEl);
	var dims2 = getDimensions(bottomEl);
	if (overlap(dims1, dims2)) {
		return true;
	}
	else {
		return false;
	}
}

function getDimensions(el) {
	var points = new Array();	
	points[0] = getOffsetLeft(el);					//left
	points[1] = points[0] + getOffsetWidth(el);		//right
	points[2] = getOffsetTop(el);					//top
	points[3] = points[2] + getOffsetHeight(el);	//bottom
	return points;
}
	
function getOffsetLeft(el) {
	var left = 0;
	var curEl = el;
	while (curEl != null) {
		left += curEl.offsetLeft;
		curEl = curEl.offsetParent;
	}
	return left;
}
	
function getOffsetTop(el) {
	var top = 0;
	var curEl = el;
	while (curEl != null) {
		top += curEl.offsetTop;
		curEl = curEl.offsetParent;
	}
	return top;
}
	
function getOffsetWidth(el) {
	var width = el.offsetWidth;
	return width;
}
	
function getOffsetHeight(el) {
	var height = el.offsetHeight;
	return height;
}
	
function overlap(dims1, dims2) {
	if (((dims1[0] > dims2[0] && 
		  dims1[1] > dims2[0] && 
		  dims1[0] > dims2[1] && 
		  dims1[1] > dims2[1]) ||
		 (dims1[0] < dims2[0] && 
		  dims1[1] < dims2[0] && 
		  dims1[0] < dims2[1] && 
		  dims1[1] < dims2[1])) ||
	    ((dims1[2] > dims2[2] &&
		  dims1[3] > dims2[2] &&
		  dims1[2] > dims2[3] &&
		  dims1[3] > dims2[3]) ||
		 (dims1[2] < dims2[2] &&
		  dims1[3] < dims2[2] &&
		  dims1[2] < dims2[3] &&
		  dims1[3] < dims2[3]))) {
			return false;	
	}
	else {
		return true;
	}
}

function quicklinkSubmit(type) {
	if (!document.forms.quicklinkPost) return;
	document.forms.quicklinkPost.quickLink_action.value = type;
	document.forms.quicklinkPost.submit();
	if (window.event && this.event.type) {
		this.event.cancelBubble = true;
	}
}
function quickLinkSelect(obj) {
	if (!document.forms["quicklinkSelect"] || !document.getElementById("quicklinkSelect_Link")) return;
	document.getElementById("quicklinkSelect_Link").value = obj.getAttribute("href");
	document.forms["quicklinkSelect"].submit();
	if (window.event && this.event.type) {
		this.event.cancelBubble = true;
	}
}
function checkQuickLinkDropdown() {
	var obj = getEl("quickLinkDropdown");
	if (!obj) return;
	if (dom || ie) {
		if (obj.style.display == "" || obj.style.display == "none") {
			blockEl(obj);
			negotiateSelects(obj, true, false);
		}
		else {
			noneEl(obj);
			negotiateSelects(obj, false, false);
		}
	}
}
function repositionFooter() {
    var footer = getEl("footer");
    var footerKey = getEl("footerKey");
    var currY = findPosY(footerKey) + 25;
    var height = getViewportHeight();
    if(height > currY) {
        footer.style.position = "absolute";
        footer.style.top = ((height) - (footer.offsetHeight)) + "px";
    }
}

