/*
		JavaScript for the div swapping
*/

//include('includes/css-class-functions.js');

 	function toggleVis(obj)
	{
		try
		{		
			var e = document.getElementById(obj);
			if (e != null)  //OMG OMG OMG SO IMPORTANT
			{
				if (e.style.display == 'none')
				{
					e.style.display = 'block';
				}
				else
				{
					e.style.display = 'none';
				}
			}
		}
		catch(err)
		{
			alert(err);
		}
		
	}

	function disappearElementsByClass(searchClass,node,tag) {
		
		var classElements = new Array();
		if ( node == null )
			node = document;
		if ( tag == null )
			tag = '*';
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
		for (i = 0, j = 0; i < elsLen; i++) {
			if ( pattern.test(els[i].className) ) 
			{
				els[i].style.display = 'none';
			}
		}
	}
	
	function menuSwapElement(objclass, obj)
	{
		disappearElementsByClass(objclass, null, null);
		toggleVis(obj);
	}
	
	function formDepthSelector(className, instrument, divID)
	{
		table = instrument.split(".");
		menuSwapElement(className, table[0]);
	}
	function clearGraphMenus()
	{	
			var menuList = [];
			menuList[0] = 'x_variable';
			menuList[1]='y2';
			menuList[2]='y3';
			menuList[3]='y4';
			for (i in menuList)
			{
				theThing = document.getElementById(menuList[i]);
				if (theThing != null)
				{
					theThing.options[0].selected=true;
					theThing.onchange();
					
				}
			}	
	}

	// ----------------------------------------------------------------------------
	// HasClassName
	//
	// Description : returns boolean indicating whether the object has the class name
	//    built with the understanding that there may be multiple classes
	//
	// Arguments:
	//    objElement              - element to manipulate
	//    strClass                - class name to add
	//
	function HasClassName(objElement, strClass)
	   {
	   // if there is a class
	   if ( objElement.className )
	      {
	      // the classes are just a space separated list, so first get the list
	      var arrList = objElement.className.split(' ');

	      // get uppercase class for comparison purposes
	      var strClassUpper = strClass.toUpperCase();

	      // find all instances and remove them
	      for ( var i = 0; i < arrList.length; i++ )
	         {
	         // if class found
	         if ( arrList[i].toUpperCase() == strClassUpper )
	            {
	            // we found it
	            return true;
	            }
	         }
	      }
	   // if we got here then the class name is not there
	   return false;
	   }
	// 
	// HasClassName
	// ----------------------------------------------------------------------------

	// ----------------------------------------------------------------------------
	// AddClassName
	//
	// Description : adds a class to the class attribute of a DOM element
	//    built with the understanding that there may be multiple classes
	//
	// Arguments:
	//    objElement              - element to manipulate
	//    strClass                - class name to add
	//
	function AddClassName(objElement, strClass, blnMayAlreadyExist)
	   {

	   // if there is a class
	   if ( objElement.className )
	      {
	      // the classes are just a space separated list, so first get the list
	      var arrList = objElement.className.split(' ');
	      // if the new class name may already exist in list
	      if ( blnMayAlreadyExist )
	         {
	         // get uppercase class for comparison purposes
	         var strClassUpper = strClass.toUpperCase();
	         // find all instances and remove them
	         for ( var i = 0; i < arrList.length; i++ )
	            {
	            // if class found
	            if ( arrList[i].toUpperCase() == strClassUpper )
	               {
	               // remove array item
	               arrList.splice(i, 1);
	               // decrement loop counter as we have adjusted the array's contents
	               i--;
	               }
	            }
	         }
	      // add the new class to end of list
	      arrList[arrList.length] = strClass;

	      // add the new class to beginning of list
	      //arrList.splice(0, 0, strClass);

	      // assign modified class name attribute
	      objElement.className = arrList.join(' ');
	      }
	   // if there was no class
	   else
	      {
	      // assign modified class name attribute      
	      objElement.className = strClass;
	      }
	   }
	// 
	// AddClassName
	// ----------------------------------------------------------------------------

	// ----------------------------------------------------------------------------
	// RemoveClassName
	//
	// Description : removes a class from the class attribute of a DOM element
	//    built with the understanding that there may be multiple classes
	//
	// Arguments:
	//    objElement              - element to manipulate
	//    strClass                - class name to remove
	//
	function RemoveClassName(objElement, strClass)
	   {
	   // if there is a class
	   if ( objElement.className )
	      {
	      // the classes are just a space separated list, so first get the list
	      var arrList = objElement.className.split(' ');
	      // get uppercase class for comparison purposes
	      var strClassUpper = strClass.toUpperCase();
	      // find all instances and remove them
	      for ( var i = 0; i < arrList.length; i++ )
	         {
	         // if class found
	         if ( arrList[i].toUpperCase() == strClassUpper )
	            {
	            // remove array item
	            arrList.splice(i, 1);
	            // decrement loop counter as we have adjusted the array's contents
	            i--;
	            }
	         }
	      // assign modified class name attribute
	      objElement.className = arrList.join(' ');
	      }
	   // if there was no class
	   // there is nothing to remove
	   }
	// 
	// RemoveClassName
	// ----------------------------------------------------------------------------
	function getElementByIDAndParent(elID, parentID)
	{
		var parentNodes = document.getElementById(parentID).childNodes;
		for (var i = 0; i<parentNodes.length;i++)
		{
			if (parentNodes[i].id == elID )
			{
				return parentNodes[i];
			}
		}
	}
	
	function DeClassNode(nodeID, strClass)
	{
		// For all elements under node identified by nodeID (eg: a menu container div)
		var nodeChildren = document.getElementById(nodeID).childNodes;
		// call RemoveClassName
		for(var i=0;i<nodeChildren.length; i++)
		{
			RemoveClassName(nodeChildren[i], strClass);
		}
	}
	
	
	function toggleClassNode(itemID, parentID, strClass)
	{
		DeClassNode(parentID, strClass);
		itemElement = getElementByIDAndParent(itemID, parentID)
		AddClassName(itemElement, strClass, false);

	}