﻿/********************************************************************
	Contains javascript functions for
	 - Simple DHTML dropdown menu, for a top bar menu
		functions: ShowDropMenu, HideDropMenu, ProcessMouseOver
	 - Simple Delayed popup
		functions: ShowPopup, ShowDynPopup, ShowDynPopupNow, ResetPopup, HidePopup, ProcessMouseOver
	 - Commonly used routines for element positioning
		functions: GetClientWidth, GetClientHeight, GetElementXPos, GetElementYPos, GetElementWidth, 
			GetElementHeight, GetScrollYOffset, GetScrollXOffset
	 - Commonly used dynamic content loading functions
		functions: WriteSyncDynamicData, WriteDynamicData
	
	Last Modified - July 2009
	Copyright 2005 - Dundas Data Visualization
*************************************************************************/
	
	//global variables for tracking the menu and popups
	var dropMenuParent = null;
	var currentDropMenu = null;
	var popupTimerID = -1;
	var popupTimerTimeout = 700;
	var popupLeftPos = null;
	
	function ShowDynDropMenu(url,elementID, parentObj)
	{
		WriteSyncDynamicDataOnce(url, elementID);
		ShowDropMenu(elementID, parentObj);
	}
	
	//called to show a dropdown menu for a horizontal parent menu
	function ShowDropMenu(elementID, parentObj)
	{
		HideDropMenu();
		currentDropMenu = document.getElementById(elementID);
		dropMenuParent = parentObj;
		
		currentDropMenu.style.top = GetElementYPos(parentObj) + 27;
		currentDropMenu.style.left = GetElementXPos(parentObj) + -1;
		
		//move the menu if required
		var rEdge = -1;
		while(rEdge < 0)
		{
			if(GetElementXPos(currentDropMenu) < 10)
			{
				break;
			}

			rEdge = ( GetClientWidth()+ GetScrollXOffset() - 20 ) - 
					( GetElementXPos(currentDropMenu) + GetElementWidth(currentDropMenu) );

			if(rEdge < 0)
			{
				currentDropMenu.style.left = GetElementXPos(currentDropMenu) + rEdge + "px";
			}
		}

		//show the menu
		if(document.getElementById(elementID).style.visibility != 'visible')
			document.getElementById(elementID).style.visibility = 'visible';				
	}
	
	//called to show a dropdown menu for a vertical parent menu
	function ShowDropMenu2(elementID, parentObj)
	{
		HideDropMenu();
		currentDropMenu = document.getElementById(elementID);
		dropMenuParent = parentObj;
	
		currentDropMenu.style.left = GetElementXPos(parentObj) + GetElementWidth(parentObj) + 1;
		currentDropMenu.style.top = GetElementYPos(parentObj);
		
		//move the menu if required
		var bEdge = -1;
		while(bEdge < 0)
		{
			if(GetElementYPos(currentDropMenu) < 10)
				break;

			bEdge = ( GetClientHeight()+ GetScrollYOffset() - 20 ) - 
					( GetElementYPos(currentDropMenu) + GetElementHeight(currentDropMenu) );		
			if(bEdge < 0)
				currentDropMenu.style.top = GetElementYPos(currentDropMenu) + bEdge;				
		}	
		
		//show the menu
		if(document.getElementById(elementID).style.visibility != 'visible')
			document.getElementById(elementID).style.visibility = 'visible';				
	}

	//called to show a dynamicallly loaded popup, generally called on onmouseover
	function ShowDynPopupNow(url,elementID, parentObj)
	{
		WriteSyncDynamicData(url, elementID);
		ShowPopupIntern(elementID, parentObj,1);
	}
	function ShowDynPopup(url,elementID, parentObj)
	{
		WriteSyncDynamicData(url, elementID);
		ShowPopupIntern(elementID, parentObj,500);
	}

	//called to show a popup, generally called on onmouseover
	function ShowPopup(elementID, parentObj)
	{
		ShowPopupIntern(elementID, parentObj,600);
	}

	function ShowPopupIntern(elementID, parentObj, timeDelay)
	{
		HideDropMenu();
		currentDropMenu = document.getElementById(elementID);
		dropMenuParent = parentObj;
		popupTimerTimeout = timeDelay;

		//move the popup if required
		var rSpace = ( GetClientWidth() + GetScrollXOffset() ) - 
					( GetElementXPos(parentObj) + GetElementWidth(parentObj) );

		if(rSpace <= GetElementWidth(currentDropMenu))
		{
			//currentDropMenu.style.left = GetElementXPos(parentObj) - GetElementWidth(currentDropMenu);
			currentDropMenu.style.left = GetMouseX(parentObj) + parentObj.offsetWidth - currentDropMenu.offsetWidth;
		}
		else
		{
			currentDropMenu.style.left = GetMouseX(parentObj) + 10 + "px";
		}

		if(GetElementXPos(currentDropMenu) < 0)
			currentDropMenu.style.left = 0;

		//store the left position for when the popup actually appears
		//then make sure that the popup is positioned off screen
		//so that parts of it will not appear until it is actually visible
		//this is for working around an IE bug
		popupLeftPos = currentDropMenu.style.left;
		currentDropMenu.style.left = -1000;
		
		var bSpace = ( GetClientHeight() + GetScrollYOffset() ) -
					( GetElementYPos(parentObj) + GetElementHeight(parentObj) );
		
		if(bSpace <= GetElementHeight(currentDropMenu))
		{
			currentDropMenu.style.top = GetElementYPos(parentObj) - GetElementHeight(parentObj) - GetElementHeight(currentDropMenu) + "px";
		}
		else
		{
			currentDropMenu.style.top = GetElementYPos(parentObj) + GetElementHeight(parentObj) + "px";
		}

		if(GetElementYPos(currentDropMenu) < 0)
			currentDropMenu.style.top = 0;
			
		//show the menu, use a timer to indirectly call it so Firefox does not flash
		popupTimerID = window.setTimeout("DelayedShowPopup()",popupTimerTimeout);
	}
	
	//resets the popup, generally called on onmousemove
	function ResetPopup()
	{
		if(currentDropMenu != null)
		{
			if(currentDropMenu.style.visibility == 'hidden')
			{
				window.clearTimeout(popupTimerID);
				popupTimerID = window.setTimeout("DelayedShowPopup()",popupTimerTimeout);
			}
		}
	}
	
	//called by the other popup functions
	function DelayedShowPopup()
	{
		if(currentDropMenu != null)
		{
			currentDropMenu.style.left = popupLeftPos;
			currentDropMenu.style.visibility = 'visible';
		}
	}
	
	//hides a drop menu or popup
	function HidePopup()
	{
		if(currentDropMenu == null)
			return;
			
		if(event != null)
		{
    		var targetElement = null;
	    	if(event.srcElement)
		    	targetElement = event.srcElement;
		    else if(event.target)
			    targetElement = event.target;	
			if(targetElement != null)
			{
			    var hideMenu = true;
		        var par = targetElement;
		        while(par != null){
    								
			        if(par == dropMenuParent || par == currentDropMenu)
			        {
				        hideMenu = false;
				        break;
			        }
			        if(par.parentNode != null)
				        par = par.parentNode;
			        else
				        par = par.parentObjment;
		        }
		        if(hideMenu)
		        {
			        HideDropMenu();
		        }
			}
		}
			
		//HideDropMenu();
	}
	function HideDropMenu()
	{	
	    //return; //test
	    
		if(currentDropMenu == null)
			return;
		
		window.clearTimeout(popupTimerID);
			
		if(currentDropMenu.style.visibility != 'hidden')
		{
			currentDropMenu.style.visibility = 'hidden';
			currentDropMenu.style.left = -1000;
		}
					
		currentDropMenu = null;
		dropMenuParent = null;

	}

	//should be called in the Body tag using onmousemove, this function tracks the state of the menu
	//and automatically hides the menu when appropriate
	function ProcessMouseOver(event)
	{
		//get the source element that initiall fired the event, useful for bubbled events
		var targetElement = null;
		if(event.srcElement)
			targetElement = event.srcElement;
		else if(event.target)
			targetElement = event.target;		

		if(currentDropMenu != null)
		{		
		
		    var hideMenu = true;
		    var par = targetElement;
		    while(par != null){
								
			    if(par == dropMenuParent  || par == currentDropMenu)
			    {
				    hideMenu = false;
				    break;
			    }
			    if(par.parentNode != null)
				    par = par.parentNode;
			    else
				    par = par.parentObjment;
		    }
		    if(hideMenu)
		    {
			    HideDropMenu();
		    }
		}
	}

	//retrieves the client width of the browser, works with IE5+, NS6+ and Firefox1+
	function GetClientWidth()
	{
	    if (window.innerWidth!=window.undefined) return window.innerWidth; 
	    if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	    if (document.body) return document.body.clientWidth; 
	    
		return screen.x;
	}		
	
	//retrieves the client height of the browser, works with IE5+, NS6+ and Firefox1+
	function GetClientHeight()
	{
	    if (window.innerHeight!=window.undefined) return window.innerHeight;
	    if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	    if (document.body) return document.body.clientHeight; 
	    
		return screen.y;
	}	
	
	function GetMouseX(parentObj)
	{
	    // Detect if the browser is IE or not.
        // If it is not IE, we assume that the browser is NS.
        var IE = document.all?true:false
        // Temporary variable to hold mouse x pos.
        var tempX = 0

        if (IE)
        {   // grab the x pos if browser is IE
            tempX = event.clientX + document.body.scrollLeft;
        }
        else
        {   // grab the x pos.s if browser is NS
            tempX = GetElementXPos(parentObj) + 20;
        } 
        // catch possible negative values in NS4
        if (tempX < 0) tempX = 0
        
        return tempX
	}
	
	function GetMouseY(parentObj)
	{
		// Detect if the browser is IE or not.
        // If it is not IE, we assume that the browser is NS.
        var IE = document.all?true:false
        // Temporary variable to hold mouse y pos.
        var tempY = 0

        if (IE)
        {   // grab the y pos if browser is IE
            tempY = event.clientY + document.body.scrollTop;
        }
        else
        {   // grab the y pos.s if browser is NS
            tempY = GetElementYPos(parentObj);
        }  
        // catch possible negative values in NS4
        if (tempY < 0) tempY = 0
        
        return tempY
	}
	
	//retrieves the left position of the given element, works with IE5+, NS6+ and Firefox1+
	function GetElementXPos(obj)
	{
		var xPos = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				xPos += obj.offsetLeft;
				obj = obj.offsetParent;
			}
		}
		else if (obj.x)
			xPos += obj.x;
		return xPos;
	}

	//retrieves the top position of the given element, works with IE5+, NS6+ and Firefox1+
	function GetElementYPos(obj)
	{
		var yPos = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				yPos += obj.offsetTop;
				obj = obj.offsetParent;
			}
		}
		else if (obj.y)
			yPos += obj.y;
		return yPos;
	}
	
	//retrieves the width of the given element, works with IE5+, NS6+ and Firefox1+
	//in NS and firefox, it may only return the visible width of the element
	function GetElementWidth(obj)
	{
	    var width = 0;

        if(obj.scrollWidth)
		    width =  obj.scrollWidth;
	    else if(obj.scrollOffsetWidth)
		    width =  obj.scrollOffsetWidth;
	    else if(obj.offsetWidth)
		    width =  obj.offsetWidth;
	    else if(obj.width)
		    width =  obj.width;

		return width;
	}
	
	//retrieves the height of the given element, works with IE5+, NS6+ and Firefox1+
	//in NS and firefox, it may only return the visible height of the element
	function GetElementHeight(obj)
	{
	    var height = 0;

	    if(obj.scrollHeight)
		    height = obj.scrollHeight;
	    else if(obj.scrollOffsetHeight)
		    height = obj.scrollOffsetHeight;
	    else if(obj.offsetHeight)
		    height = obj.offsetHeight;
	    else if(obj.height)
		    height = obj.height;

		return height;
	}
	
	//retrieves the vertical scrolling position, from the top of the document, works with IE5+, NS6+ and Firefox1+
	function GetScrollYOffset()
	{
	    if (document.compatMode=='CSS1Compat') return document.documentElement.scrollTop; 
		if(document.body.scrollTop) return document.body.scrollTop;
		if(window.pageYOffset) return window.pageYOffset;
		
		return 0;
	}
	
	//retrieves the horizontal scrolling position, from the left side of the document, works with IE5+, NS6+ and Firefox1+
	function GetScrollXOffset()
	{
	    if (document.compatMode=='CSS1Compat') return document.documentElement.scrollLeft; 
		if(document.body.scrollLeft) return document.body.scrollLeft;
		if(window.pageXOffset) return window.pageXOffset;
		
		return 0;
	}


	//sync dynamic loading function
	//*******************************************
	function WriteSyncDynamicDataOnce(url, elementID)
	{
		element = document.getElementById(elementID);
		if(element.innerHTML == null || element.innerHTML =="")
			WriteSyncDynamicData(url, elementID)
	}
	function WriteSyncDynamicData(url, elementID)
	{
		DynamicDataElement = document.getElementById(elementID);
		
		if (window.ActiveXObject)
		{
			DynamicDataRequest = new ActiveXObject("Microsoft.XMLHTTP");
			DynamicDataRequest.open("GET", url, false);
			DynamicDataRequest.send();
			if (DynamicDataRequest.status == 200)
				DynamicDataElement.innerHTML = DynamicDataRequest.responseText;
		}
		else if (window.XMLHttpRequest)
		{
			DynamicDataRequest = new XMLHttpRequest();
			DynamicDataRequest.open("GET", url, false);
			DynamicDataRequest.send(null);
			if (DynamicDataRequest.status == 200)
				DynamicDataElement.innerHTML = DynamicDataRequest.responseText;
		}
	}
		
	
	//AJAX async loading functions
	//**********************************************
	var DynamicDataElement = null;
	var DynamicDataRequest;

	function WriteDynamicData(url, elementID)
	{
		DynamicDataElement = document.getElementById(elementID);
		
		if (window.ActiveXObject)
		{
			DynamicDataRequest = new ActiveXObject("Microsoft.XMLHTTP");
			if (DynamicDataRequest)
			{
				DynamicDataRequest.onreadystatechange = dynamicDataReceived;
				DynamicDataRequest.open("GET", url, true);
				DynamicDataRequest.send();
			}
		}
		else if (window.XMLHttpRequest)
		{
			DynamicDataRequest = new XMLHttpRequest();
			DynamicDataRequest.onreadystatechange = dynamicDataReceived;
			DynamicDataRequest.open("GET", url, true);
			DynamicDataRequest.send(null);
		}	
	}
	//AJAX callback for async loading
	function dynamicDataReceived() 
	{
		// only if req shows "complete"
		if (DynamicDataRequest.readyState == 4)
		{
			if (DynamicDataRequest.status == 200)
			{
				DynamicDataElement.innerHTML = DynamicDataRequest.responseText;
	        }
	        else
	        {
				//error reading data
			}
		}
	}


/******************************************************************

                   	open popup in new window

******************************************************************/

function MM_openBrWindow(theURL,winName,features) { //v2.0

  window.open(theURL,winName,features);
	
}
 

/********************************************************************

                            leftNav

*********************************************************************/
function expandLeftMenuItem(parentEle)
{
    if ( parentEle == null )
        return;

	//find the first child IMG element
	SwapImage(parentEle,"/Common/Menu/expand.png","/Common/Menu/collapse.png");

    var nextTR = getNextSibling( parentEle.parentNode.parentNode, "tr" );
    
    if ( nextTR != null )
    {
	    nextTR.style.display = (nextTR.style.display == "none" ) ? "" : "none";
	}
}

/********************************************************************
Returns the specified child element, or null if not found
Params:
  parentEle - the parent element to start searching from
  tagName - the tag name of the child element to retrieve (ie. A, P, IMG, etc)
*********************************************************************/
function getChildElement(parentEle, tagName)
{
    if ( parentEle == null )
        return null;
        
	var tag = new String();
	var childEle = parentEle.firstChild;
	while(childEle != null)
	{
		if(childEle.tagName != null)
		{
			tag = childEle.tagName.toLowerCase();
			tag = tag.toLowerCase();
			if(tag == tagName.toLowerCase())
			{
				break;
			}
		}
		childEle = childEle.nextSibling;
	}
	return childEle;
}
/********************************************************************
Returns the specified child element, or null if not found
Params:
  parentEle - the parent element to start searching from
  tagName - the tag name of the child element to retrieve (ie. A, P, IMG, etc)
*********************************************************************/
function getNextElement(currentEle, tagName)
{
    if ( currentEle == null )
        return null;

	var tag = new String();
	var foundEle = null;

	var childEle = currentEle.firstChild;

	do{
		while(childEle != null)
		{
			if(childEle.tagName != null)
			{
				tag = childEle.tagName.toLowerCase();
				tag = tag.toLowerCase();
				if(tag == tagName.toLowerCase()){
					foundEle = childEle;
					break;
				}
			}
			childEle = childEle.nextSibling;
		}
		if(foundEle != null)
			break;
	
	    if ( childEle.parentNode == null )
	        return null;
	        
		childEle = childEle.parentNode.nextSibling;	
	}while(childEle != null);

	return foundEle;
}

/********************************************************************
The getNextSibling method will enumerate through all sibling objects
starting at the current element untill it finds element with specified
tag name.
*********************************************************************/
function getNextSibling(currentEle, tagName)
{
    if ( currentEle == null )
        return null;
        
	var tag = new String();
	var foundEle = null;
	var tempEle = currentEle.nextSibling;

	do
	{
		if(tempEle != null)
		{
			if(tempEle.tagName != null)
			{
				tag = tempEle.tagName.toLowerCase();

				if(tag == tagName.toLowerCase())
				{
					foundEle = tempEle;
					break;
				}
			}
			
		}
		if( foundEle != null )
			break;
	
	    if( tempEle == null )
	        return null;
	
		tempEle = tempEle.nextSibling;
	}while(tempEle != null);

	return foundEle;
}

/********************************************************************
The getNextSibling method will enumerate through all sibling objects
starting at the current element untill it finds element with specified
tag name.
*********************************************************************/
function getPrevSibling(currentEle, tagName)
{
    if ( currentEle == null )
        return null;
        
	var tag = new String();
	var foundEle = null;
	var tempEle = currentEle.previousSibling;

	do
	{
		if(tempEle != null)
		{
			if(tempEle.tagName != null)
			{
				tag = tempEle.tagName.toLowerCase();

				if(tag == tagName.toLowerCase())
				{
					foundEle = tempEle;
					break;
				}
			}
			
		}
		if(foundEle != null)
			break;
	
	    if( tempEle == null )
	        return null;
	
		tempEle = tempEle.previousSibling;
	}while(tempEle != null);

	return foundEle;
}

/********************************************************************
*********************************************************************/
function SwapClass(ele, class1, class2)
{
    if ( ele == null )
        return;
        
	if(ele.className == class1)
		ele.className = class2;
	else
		ele.className = class1;
}

/********************************************************************
*********************************************************************/
function SwapImage(ele, image1Src, image2Src)
{
    if ( ele == null )
        return;
        
	image1Src = image1Src.toLowerCase();
	image2Src = image2Src.toLowerCase();
	imgSrc = ele.src.toLowerCase();

	if(imgSrc.indexOf(image1Src,0) >0)
		ele.src = image2Src;
	else
		ele.src = image1Src;
}

/********************************************************************
 SelectLeftMenuItem("menuID");
*********************************************************************/
function SelectLeftMenuItem(eleId)
{
    if ( eleId != null && eleId != "{{PageID}}" )
    {
	    //find the menu to open
	    var ele = document.getElementById(eleId);
    	
	    if ( ele != null )
	    {
	        ele.className = "Selected";
	        
	        // loop through all parent elements and change the display style
	        // for all TR tags that are not visible
	        parentEle = ele;
	        
	        // make sure that all parent nodes are expanded to show this item
	        while( parentEle != null )
	        {
	            if ( parentEle.tagName != null )
	            {
	                if ( parentEle.tagName.toLowerCase() == "tr" && parentEle.style.display == "none" )
	                {
                        var expTR = getPrevSibling( parentEle, "tr" );
                        var expTD = getChildElement( expTR, "td" );
                        var expImg = getChildElement( expTD, "img" );
                        expandLeftMenuItem( expImg );
	                }
	            }
	        
	            parentEle = parentEle.parentNode;
	        }
	        
	        // if this menu item is expandable, show its child items
            var nextTR = getNextSibling( ele, "tr" );
            
            if ( nextTR != null )
            {
                if ( nextTR.style.display == "none" )
	            {
                    var expTD = getChildElement( ele, "td" );
                    var expImg = getChildElement( expTD, "img" );
                    expandLeftMenuItem( expImg );
	            }
	        }	        
	    }
	}
}

var menuClass = "Normal";
/********************************************************************
*********************************************************************/
function menuHover(ele)
{
    if ( ele == null )
        return;
        
	menuClass = ele.className;
	ele.className = "Hover";
}
/********************************************************************
*********************************************************************/
function menuOut(ele)
{
    if ( ele == null )
        return;
        
	ele.className = menuClass;
	menuClass = "Normal";
}
/********************************************************************
*********************************************************************/
function gotoPage(url)
{
    if ( url != null && url != "" )
    {
	    document.location.href = url;
	}
}


/**** Map Resource Table Collapse / Expand Manager ****/


    function getItem(id)
    {
        var itm = false;
        if(document.getElementById)
            itm = document.getElementById(id);
        else if(document.all)
            itm = document.all[id];
        else if(document.layers)
            itm = document.layers[id];

        return itm;    
	}

    function toggleAll(dowhat)
    {
        var tags = document.getElementsByTagName('tbody');
        if(!tags)
            return false;

        for(var i = 0; i < tags.length; i++)
        {
            if(tags[i].className == 'collapse_obj')
            {
                if(dowhat == 'collapse')
                    tags[i].style.display = 'none';
                else
                    tags[i].style.display = '';
            }
        }

        return false;
    }

    function toggleItem(id)
    {
        itm = getItem(id);

        if(!itm)
            return false;

        if(itm.style.display == 'none')
            itm.style.display = '';
        else
            itm.style.display = 'none';

        return false;
    }
	

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}




/*  swap image behaviours */

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

/*  URL Jump Drop down menu */
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

// Splintered striper 1.3
// reworking of Zebra Tables and similar methods which works not only for tables and even/odd rows,
// but as a general DOM means of assigning any number of classes to children of a parent element.
// Patrick H. Lauke aka redux / www.splintered.co.uk
// Distributed under the Creative Commons Attribution-ShareAlike license - http://creativecommons.org/licenses/by-sa/2.0/


/*
 * Summary:      Core experiment function that applies any number of classes to all child elements
 *               contained in all occurences of a parent element (either with or without a specific class)
 * Parameters:   parentElementTag - parent tag name
 *               parentElementClass - class assigned to the parent; if null, all parentElementTag elements will be affected
 *               childElementTag -  tag name of the child elements to apply the styles to
 *               styleClasses - comma separated list of any number of style classes (using 2 classes gives the classic "zebra" effect)
 * Return:       none
 */
function striper(parentElementTag, parentElementClass, childElementTag, styleClasses)
{
	var i=0,currentParent,currentChild;
	// capability and sanity check
	if ((document.getElementsByTagName)&&(parentElementTag)&&(childElementTag)&&(styleClasses)) {
		// turn the comma separate list of classes into an array
		var styles = styleClasses.split(',');
		// get an array of all parent tags
		var parentItems = document.getElementsByTagName(parentElementTag);
		// loop through all parent elements
		while (currentParent = parentItems[i++]) {
			// if parentElementClass was null, or if the current parent's class matches the specified class
			if ((parentElementClass == null)||(currentParent.className == parentElementClass)) {
				var j=0,k=0;
				// get all child elements in the current parent element
				var childItems = currentParent.getElementsByTagName(childElementTag);
				// loop through all child elements
				while (currentChild = childItems[j++]) {
					// based on the current element and the number of styles in the array, work out which class to apply
					k = (j+(styles.length-1)) % styles.length;
					// add the class to the child element - if any other classes were already present, they're kept intact
					currentChild.className = currentChild.className+" "+styles[k];
				}
			}
		}
	}
}

// Magic Mouseover Scrolling for Homepage News Items
 var scrolling = null;

 function scroll_up() {
     var d = document.getElementById('newsScroller');

     d.scrollTop = d.scrollTop - 2;

     scrolling = window.setTimeout(function() {
         scroll_up();
     }, 0);
 }

 function scroll_down() {
     var d = document.getElementById('newsScroller');

     d.scrollTop = d.scrollTop + 2;

     scrolling = window.setTimeout(function() {
         scroll_down();
     }, 0);
 }

 function stop_scroll() {
     window.clearTimeout(scrolling);
 }



/* New drop-down menu */
function ShowDropDownMenu(element) {
    var dropdown = element.getElementsByTagName("ul")[0];
    if (dropdown.style.visibility == 'visible') {
        return;
    }
    dropdown.style.visibility = 'visible';
}

function HideDropDownMenu(element) {
    var dropdown = element.getElementsByTagName("ul")[0];
    if (dropdown.style.visibility == 'hidden') {
        return;
    }
    dropdown.style.visibility = 'hidden';
}



/* New left nav (accordion) */
var currentMenu;
//var ignoreMouseOver;
//var menuDelayTimer;

var hoverMenu;
var hoverConfig = {
    sensitivity: 3,
    interval: 100,
    over: function() { EnsureMenuOpen(hoverMenu, true); },
    timeout: 500,
    out: function() { }
};

function InitLeftNav() {
    // Add a style for all expandable menus
    var menuRoot = document.getElementById("leftNav");
    for (var i = 0; i < menuRoot.childNodes.length; i++) {
        if (menuRoot.childNodes[i].tagName == "DIV" && menuRoot.childNodes[i].className.indexOf("firstLevel") != -1 && GetSubMenu(menuRoot.childNodes[i]) != null) {
            //$(menuRoot.childNodes[i]).hoverIntent(hoverConfig);
            GetFirstLevelMenuItem(menuRoot.childNodes[i]).className += " closed";
        }
    }

    // Get the root-level menu item for this.  Mark it selected and open the menu.
    var selectedMenuItem = GetSelectedMenuItem();
    if (selectedMenuItem == null) {
        return;
    }

    selectedMenuItem.className += " selected";
    var selectedMenu = selectedMenuItem.parentNode;
    while (selectedMenu != null) {
        if (selectedMenu.className == "firstLevel") {
            break;
        }
        selectedMenu = selectedMenu.parentNode;
    }

    // Add a child selected class to the top-level item if a child is selected.
    if (!IsFirstLevelItem(selectedMenuItem))
    {
        GetFirstLevelMenuItem(selectedMenu).className += " childselected";
    }

    EnsureMenuOpen(selectedMenu, false);
}

function IsFirstLevelItem(menuItem) {
    return (menuItem.className.indexOf("firstLevelItem") != -1);
}

function GetFirstLevelMenuItem(menu) {
    return menu.getElementsByTagName("A")[0];
}

function GetSecondLevelMenuItem(menu, index) {
    return menu.getElementsByTagName("A")[index];
}

function GetSelectedMenuItem() {
    return document.getElementById(pageID);
}

function GetSubMenu(menu) {
    var childDivs = menu.getElementsByTagName("DIV");
    if (childDivs == null || childDivs.length == 0) {
        return null;
    }
    return subMenu = childDivs[0];
}

function EnsureMenuOpen(menu, showAnimation) {
    //if (ignoreMouseOver) {
        //return;
    //}

    if (currentMenu == menu || GetSubMenu(menu) == null) {
        return;
    } else if (currentMenu != null) {
        CloseMenu(currentMenu);
    }

    OpenMenu(menu, showAnimation);
}

function EnsureMenuClosed(menu) {
    if (currentMenu == menu) {
        CloseMenu(currentMenu);
    }
}

function OpenMenu(menu, showAnimation) {
    var subMenu = GetSubMenu(menu);
    if (subMenu == null) {
        return;
    }
    currentMenu = menu;

    // Stop any current animation.
    StopMenuAnimation(subMenu, true);

    // Make the menu visible and reset the height (necessary before animation).
    subMenu.className = subMenu.className.replace(/closed/, "opened");
    subMenu.style.height = "auto";

    // Set the opened style on the menu item.
    GetFirstLevelMenuItem(menu).className = GetFirstLevelMenuItem(menu).className.replace(/closed/, "opened");

    if (showAnimation) {
        ignoreMouseOver = true;
        var desiredHeight = GetElementHeight(subMenu);
        var grow = 1.0;
        subMenu.style.height = "0px";

        subMenu.OnAnimationStop = function() {
            subMenu.style.height = "auto";
            //window.setTimeout(function() { ignoreMouseOver = false; }, 50);
        };

        // Start the animation.
        subMenu.AnimationTimer = window.setInterval(
                function() {
                    if (subMenu.style.height && subMenu.style.height == (desiredHeight + "px")) {
                        StopMenuAnimation(subMenu, false);
                    } else {
                        var currentHeight = parseInt(subMenu.style.height.replace(/px/, ""));
                        subMenu.style.height = Math.min(desiredHeight, currentHeight + parseInt(grow)) + "px";
                        grow *= 1.4;
                    }
                }, 10);
    } 
}

function CloseMenu(menu) {
    var subMenu = GetSubMenu(menu);
    if (subMenu == null) {
        return;
    }
    currentMenu = null;

    // Stop any current animation.
    StopMenuAnimation(subMenu, true);

    // Remove the opened style on the menu item.
    GetFirstLevelMenuItem(menu).className = GetFirstLevelMenuItem(menu).className.replace(/opened/, "closed");

    var shrink = 1.0;

    subMenu.OnAnimationStop = function() {
        subMenu.className = subMenu.className.replace(/opened/, "closed");
        subMenu.style.height = "auto";
    };

    // Run the collapsing animation.
    subMenu.AnimationTimer = window.setInterval(
            function() {
                if (subMenu.style.height && subMenu.style.height == "0px") {
                    StopMenuAnimation(subMenu, false);
                } else {
                    if (!subMenu.style.height || subMenu.style.height == "auto") {
                        subMenu.style.height = GetElementHeight(subMenu) + "px";
                    }
                    var currentHeight = parseInt(subMenu.style.height.replace(/px/, ""));
                    subMenu.style.height = Math.max(0, currentHeight - parseInt(shrink)) + "px";
                    shrink *= 1.4;
                }
            }, 10);
}

function StopMenuAnimation(menuContainer, abort) {
    if (menuContainer.AnimationTimer != null) {
        window.clearInterval(menuContainer.AnimationTimer);
        menuContainer.AnimationTimer = null;

        if (menuContainer.OnAnimationStop != null && !abort) {
            menuContainer.OnAnimationStop();
        }
    }
}



function ToggleLoginPopup() {
    var x = document.getElementById('loginPopup');
    if (x) {
        if (x.style.display == 'none') {
            x.style.display = 'block';
        } else {
            x.style.display = 'none';
        }
    }
    return false;
}
