// <script type="text/javascript" language="JavaScript">
	

function replaceAll( str, from, to ) {
    var idx = str.indexOf( from );


    while ( idx > -1 ) {
        str = str.replace( from, to );
        idx = str.indexOf( from );
    }

    return str;
}
// defaults to returning window height, pass in "width" to get width
function getWindowSize( dimension ) {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
    	//Non-IE
    	myWidth = window.innerWidth;
    	myHeight = window.innerHeight;
  	} else if( document.documentElement &&
      	( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    	//IE 6+ in 'standards compliant mode'
    	myWidth = document.documentElement.clientWidth;
    	myHeight = document.documentElement.clientHeight;
  	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    	//IE 4 compatible
    	myWidth = document.body.clientWidth;
    	myHeight = document.body.clientHeight;
 	 }
  	//window.alert( 'Width = ' + myWidth );
 	// window.alert( 'Height = ' + myHeight );
  	if ( dimension == "width" ) {
 	 	return  myWidth ;
 	}
 	return myHeight ;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

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 crossBrowserEvent( windowEvent ) {
	if ( window.event ) {
		// only occurs on windows...
		windowEvent = window.event;
		windowEvent.target=window.event.srcElement ;
    	if (windowEvent.type == 'mouseover') 
    		windowEvent.relatedTarget = windowEvent.fromElement;
    	else if (windowEvent.type == 'mouseout') 
    		windowEvent.relatedTarget = windowEvent.toElement;
	}
	return windowEvent;
}

function setDropValue( inputDivId, value, menuDivId ) {

	document.getElementById( inputDivId ).value = value;
	menuDiv = document.getElementById(menuDivId);
	menuDiv.className="eipNoDisplay"; 
	return false;
}

/* 
	This wipes out any trailing showMenu events and forces the menu to be 
	displayed
*/
function showMenuClick( windowEvent, parent, divId, xOffset, yOffset, menuNavClass) {
	LAST_MENU_DIV_ID = null;
	showMenu( windowEvent, parent, divId, xOffset, yOffset, menuNavClass);
}
/* 
	A LOT of events are triggered by iframes. If the menu is being shown by a
	onHover event, this menu should be called. It will exit quickly if the menu
	is already being shown.
*/
function showMenu( windowEvent, parent, divId, xOffset, yOffset, menuNavClass) {

	//alert("showMenu(): LAST_MENU_DIV_ID: " + LAST_MENU_DIV_ID);
	if ( LAST_MENU_DIV_ID == divId ) {
		//debug("showMenu(): LAST_MENU_DIV_ID: " + LAST_MENU_DIV_ID);
		// we are already displaying the menu, just set the flag so that it is not hidden
		HIDE_MENU_DIV_ID = divId ;
		return;
	} else {
		closeLastMenu() ;
	}
	HIDE_MENU_DIV_ID = divId ;
	//alert("showMenu() => HIDE_MENU_DIV_ID/divId: " + divId);
	//crossEvent = crossBrowserEvent( windowEvent ) ;
	//var topContainerDivId = crossEvent.target.parentNode.parentNode.parentNode.id;
	//debug( "showMenu topContainerDivId:" + topContainerDivId) ;
	
	//debug( "showMenu=> crossEvent.type: " + crossEvent.type ) ;
	//debug( "showMenu=> crossEvent.target.class: " + crossEvent.target.className ) ;
	var menuDiv = document.getElementById( divId );
	
	menuDiv = document.getElementById( divId );
	var menuCss = "navMenu";
	if ( menuNavClass ) {
		//alert("Menu Nav Class");
		menuCss = menuNavClass;
	}
	menuDiv.className=menuCss;
	
	// add a close button...
	if ( false ) {
		menuHtml = menuDiv.innerHTML ;		
		
		if ( (menuHtml.indexOf('eipClose') == -1) && 
			(menuHtml.toUpperCase().indexOf('<UL>') != -1)) { 
			// do not do this on non ul drop windows, they do their own
			//alert("Adding close");
			var endListInt = menuHtml.toUpperCase().indexOf('</UL>') ;
			menuHtml = menuHtml.substring(0, endListInt );
			menuHtml += "<li id='eipClose'>"
			menuHtml += "<a style='text-align: right;s'"
			menuHtml += " href=\"#\" onclick=\"closeLastMenu()\">";
			menuHtml += "Close Menu&nbsp;&nbsp;&nbsp;</a></li></ul>";
			menuDiv.innerHTML = menuHtml;
		}
	}
	//alert (menuHtml);
	//debug(menuHtml);
	
	var parentDiv = document.getElementById( parent );

	var div2 = document.getElementById('applyOuterBlock');
	var topPos=findPosY(parentDiv) + yOffset;
	var accPos=findPosX(div2);

	var leftPos=findPosX(parentDiv) - accPos + xOffset;

	
	menuDiv.style.zIndex = 999 ;
	menuDiv.style.top = topPos +"px";
	// Keep windows on line
	var menuWidth = leftPos + menuDiv.offsetWidth
	var winWidth = getWindowSize("width");
	//alert("Screen width: " + winWidth + "\n menuWidth" + menuWidth);
    if ( (menuWidth +20) > winWidth ) {
		var leftPos = winWidth - menuDiv.offsetWidth - 20;
		//alert("Menu Div Offest "+ menuDiv.offsetWidth);
		//alert("menuBack: " + menuBack)
	}
	//alert("****** menuDiv.style.left = " + leftPos);
	menuDiv.style.left = leftPos + "px";
	//alert("****** menuDiv.style.left = " + leftPos);
	// pop Iframe over top of any selects.
	//toggleIframeShim( divId, true , topPos, leftPos);
}

/*
	Function invoked when rippling across onhover menus 
*/
function closeLastMenu() {
	
		if ( LAST_MENU_DIV_ID != null) {
			//debug("showMenu() => closing menu: " + LAST_MENU_DIV_ID);
			var menuDiv = document.getElementById(LAST_MENU_DIV_ID);
			menuDiv.className="eipNoDisplay";
			//toggleIframeShim( LAST_MENU_DIV_ID, false );
			LAST_MENU_DIV_ID = null;
		}
		// return false to avoid traversing the href
		return false;
}
/* 
	when menu items are rippled, a hide event is triggered, then a show event. A timer is used
	so that if a show event does appear, the doHideMenu will not hide the menu.
	
	If an entirely new menu is displayed, the LAST_MENU_DIV_ID id is used to immediately close it
*/
var HIDE_MENU_DIV_ID = null; 
var LAST_MENU_DIV_ID = null; 
function hideMenu( windowEvent, divId ) {
	LAST_MENU_DIV_ID = HIDE_MENU_DIV_ID; 
	HIDE_MENU_DIV_ID = null ;	
	crossEvent = crossBrowserEvent( windowEvent ) ;
	//debug( "hideMenu() => divId" + divId );
	var topContainerDivId = crossEvent.target.parentNode.parentNode.parentNode.id;
	//debug( "hideMenu> topContainerDivId:" + topContainerDivId) ;
	var menuDiv = document.getElementById( divId );

	//currMenuId = divId ;
	// There seems to be a race condition  here : 500 seems to work.
	setTimeout("doHideMenu(\""+ divId +"\")" , 200) ;
}

function doHideMenu( divId) {

	//debug("doHideMenu() => Div Id " + divId  + " HIDE_MENU_DIV_ID: "  + HIDE_MENU_DIV_ID);
	if ( divId == HIDE_MENU_DIV_ID ) return; // we are still in it
	//debug("doHideMenu() => Closing menu: " + divId  + " HIDE_MENU_DIV_ID: "  + HIDE_MENU_DIV_ID);

	// invoke this to hide menu
	var menuDiv = document.getElementById(divId);
	
	if ( menuDiv != null ) {
		if ( menuDiv.className == "eipNoDisplay" ) {
			// already closed the window, probably in showMenu
			// the iframe shim has been repositioned, so do not return out of here
			return;
		}
		menuItemSelected( divId );
	}
}

/* 
	menuItemSelected: this is called when we are hiding a menu drop down when:
		- we are ripping across on hover menus; and
		- when we select an item in the dropdown
*/
function menuItemSelected( divId ) {

	//debug("menuItemSelected(): divId: " + divId );
	LAST_MENU_DIV_ID = null;
	var menuDiv = document.getElementById(divId);
	menuDiv.className="eipNoDisplay";
	//toggleIframeShim( divId, false );
	
	// check for highlighting
	var parentName = divId + "Header" ;
	var linkId = divId + "Link"  ;
	highlightSelection( linkId );
	
}

var lastSkinSelectedId = "dummy";
var lastFlatSelectedId = "dummy";
function highlightSelection( linkId) {
	if ( document.getElementById(linkId) ) {
		//debug("Found linkid: " + linkId + " className: " + document.getElementById(linkId).className);
		
		// classNames can be compound, hence the need for the replace
		if ( document.getElementById(linkId).className.indexOf("skinButton") != -1 ) {
			if ( document.getElementById(lastSkinSelectedId) ) {
				var lastClass = document.getElementById(lastSkinSelectedId).className;
				lastClass = lastClass.replace("skinSelectedButton", "skinButton");
				//debug("lastClass new: " + lastClass);
				document.getElementById(lastSkinSelectedId).className = lastClass;
			}
			var newClass = document.getElementById(linkId).className ;
			//debug("newClass Orig: " + newClass);
			newClass = newClass.replace("skinButton", "skinSelectedButton");
			//debug("newClass replaced: " + newClass);
			document.getElementById(linkId).className = newClass;
			lastSkinSelectedId = linkId;
		}
		if ( document.getElementById(linkId).className.indexOf("flatButton") != -1 ) {
			if ( document.getElementById(lastFlatSelectedId) ) {
				var lastClass = document.getElementById(lastFlatSelectedId).className;
				lastClass = lastClass.replace("flatSelectedButton", "flatButton");
				document.getElementById(lastFlatSelectedId).className = lastClass;
			}
			var newClass = document.getElementById(linkId).className ;
			//debug("newClass Orig: " + newClass);
			newClass = newClass.replace("flatButton", "flatSelectedButton");
			//debug("newClass replaced: " + newClass);
			document.getElementById(linkId).className = newClass;
			lastFlatSelectedId = linkId;
		}
	}
}

var menuArray = new Array(10);
function addToMenuArray( menuDivId ) {
	var y=0;
	for (y=0; y<10; y++) {
		if ( menuArray[y] == null ) {
			 menuArray[y] = menuDivId;
			debug("addToMenuArray==> index: " + y + " DivId: " + menuDivId );
			 return;
		}
	} 
	
}

// 
//	Shims are placed behind the menus in both fireFox and ie to address to very different bugs.
// 		IE: an iframe is placed directly behind the menu to cover up html select statement which bleed through divs
//		FireFox: a div 10px larger is placed behind the menu in order to catch the mouseout events when the menu
//				is displayed over another frame/iframe.
//
function toggleIframeShim(popDiv, isShowIframe, topPos, leftPos) {
	
	//alert("toggleIframeShim: " + popDiv + isShowIframe + topPos + leftPos);
	var DivRef = document.getElementById( popDiv );

	// default for the IE hack (this is a iframe)
	var hackRef = document.getElementById('ieShim');
	var widthExtra = 0;
	var widthOffset = 0;
	var heightExtra = 0;
	var heightOffset = 0;
	// now override with firefox settings (this is a div)
	if ( ! document.all  ) {
		hackRef = document.getElementById('fireFoxShim');
		widthExtra = 20; // we push 10 px on both left and right sides
		widthOffset = 10;   //
		//alert("DivRef.style.float: " + DivRef.style.width );
		heightExtra = 10; // Above the menu we assume is always in the menu frame. Else we might
		heightOffset = 0 ;// see some scrolling on top bar menus
	}
	/*
   if ( hackRef == null ) {
		var msg = "Menus require a iframe and div on the page.\n";
		msg+= "Refer to altHeaderInclude for an example and explanation";
		//alert(	msg );
		return;   
   }
   */
   if( isShowIframe ) {
   //alert("displaying iframe");
    hackRef.style.width = DivRef.offsetWidth + widthExtra +"px";
    hackRef.style.height = DivRef.offsetHeight + heightExtra +"px";
    hackRef.style.top = (topPos - heightOffset) + "px";
    // keep menus on screen:
    hackRef.style.left = (leftPos - widthOffset) + "px";
    hackRef.style.zIndex = 998;
    hackRef.style.display = "block";
   	//alert("iframe zindex:" + hackRef.style.zIndex);
   } else {
	   //alert ("hiding iframe");
    hackRef.style.display = "none";
    hackRef.style.height = 0;
     hackRef.style.width = 0;
   }
}
  
var count = 0;
function debug( message ) {

	msgDiv = document.getElementById( "message" );
	msgDiv.style.display="block";
	count++;
	msgDiv.innerHTML =  "<b>" + count+ "</b>&nbsp;&nbsp;" + message + "<br>" + msgDiv.innerHTML;
}


function toggleDisplay ( divId, style ) {

	divObject = document.getElementById( divId ) ;
	if ( divObject == null ) {
		alert("coding error: div not found in document:" + divId );
	} else {
		if ( divObject.className == "eipNoDisplay" ) {
			divObject.className = style;
		} else {
			divObject.className = "eipNoDisplay";
		}
	}
}
// </Script>

