/*
     Generic JavaScript functions.     
*/

BRAINS.generic = function() 
{
	return {
		/*
		 * IMPORTANT: The last method here should not have a trailing comma.
		 * This will cause all of the JavaScript on the page to break with Microsoft I.E.		
		 * 
		 * Public methods.
		 */ 
		include: function (f) {
		  var tempOptions = new Object();
		  var x = 0;
		  
		  for(var i = 0; i < f.excluded.options.length; i++)  {
		    if(f.excluded.options[i].selected == true) {
		      tempOptions[x] = f.excluded.options[i];     
		      x++;
		    }
		  }
		  
		  for(var i = 0; i < x; i++)  {   
		    for(var c = 0; c < f.excluded.options.length; c++) {
		      var tempOption = tempOptions[i]
		      if (tempOption.value == f.excluded.options[c].value) {
		        f.excluded.options[c] = null; 
		      }
		    } 
		  }
		    
		  for(var i = 0; i < x; i++)  {   
		    f.included.options[f.included.options.length] = tempOptions[i];     
		  }
		  
		  f.included.selectedIndex = -1;    
		},
		
		exclude: function (f) {
		  var tempOptions = new Object();
		  var x = 0;
		  
		  for(var i = 0; i < f.included.options.length; i++) {
		    if(f.included.options[i].selected == true) {
		      tempOptions[x] = f.included.options[i];     
		      x++;
		    }
		  }
		  
		  for(var i = 0; i < x; i++)
		  {   
		    for(var c = 0; c < f.included.options.length; c++) {
		      var tempOption = tempOptions[i]
		      if (tempOption.value == f.included.options[c].value) {
		        f.included.options[c] = null; 
		      }
		    } 
		  }
		    
		  for(var i = 0; i < x; i++)  {   
		    f.excluded.options[f.excluded.options.length] = tempOptions[i];     
		  }
		  
		  f.excluded.selectedIndex = -1;    
		},
		
		showLayer: function (theId, show) 
		{
			var layer = document.getElementById(theId);
				
			if (show) {	  
			  layer.style.display = 'block';
			  layer.style.visibility = 'visible';
			  layer.style.height = 'auto';
			}
			else {		
			  layer.style.display = 'none';
			  layer.style.visibility = 'hidden';
			  layer.style.height = '0px';
			}
		},

		tableRuler: function()
		{  
		    var tables = document.getElementsByTagName('tbody');
		    
		    for (var i = 0; i < tables.length; i++)
		    {
		       if(tables[i].className == 'ruler') 
		       {        
		         var trs = tables[i].getElementsByTagName('tr');
		         for(var j = 0; j < trs.length; j++)
		         {
				   	trs[j].onmouseover = function() {				   		
						if (!YAHOO.util.Dom.hasClass(this, 'content')) {
							this.className = 'ruled';
							return false;
						}						
				   	}
				   	trs[j].onmouseout = function() {						
						if (!YAHOO.util.Dom.hasClass(this, 'content')) {
							this.className = '';
							return false;
						}						
				   	}				            	
		         }
		      }
		   } 			  
		}
	};
}(); // force the anonymous function to execute and return

BRAINS.alert = function() 
{	
	return {
		/*
		 * IMPORTANT: The last method here should not have a trailing comma.
		 * This will cause all of the JavaScript on the page to break with Microsoft I.E.		
		 * 
		 * Public methods.
		 */ 
		init: function ()
		{	
			// Dialog to show error messages to the users.
			var alertDialog = new YAHOO.widget.SimpleDialog("dlg1", {
			  width: "400px",
			  effect: { effect:YAHOO.widget.ContainerEffect.FADE,duration:0.15 },
			  fixedcenter: true,
			  modal: true,
			  visible: false,
			  close: true,
			  constraintoviewport: true,
			  buttons: [{ text:"Close", handler: function(){this.hide();}, isDefault:true }],
			  draggable: true,
			  effect: [{ effect:YAHOO.widget.ContainerEffect.FADE,duration:0.1 }]
			});
		
			alertDialog.setHeader("Validation Alert");      
			alertDialog.render(document.body);
		
			// This is called when the alert function is called.
			window.alert = function(text) {
			  alertDialog.cfg.setProperty("text", text);  
			  alertDialog.show();
			};
		}
	};
}(); 

BRAINS.generic.view = function() 
{	
	/*
	 * Private methods.
	 */
	var clickHandler = function(e) 
	{		
		var elTarget = YAHOO.util.Event.getTarget(e);
		
		var parentTable = YAHOO.util.Dom.getAncestorByTagName(elTarget, "table");
		var tableChildren = YAHOO.util.Dom.getChildren(parentTable);		
		var tableBody = tableChildren[1];

		if(tableBody.style.display == "none") {
			tableBody.style.display = "block";
			tableBody.style.visibility = "visible";
			tableBody.style.height = "auto";
			elTarget.style.backgroundPosition = "-2px -88px";
		}
		else {
			tableBody.style.display = "none";
			tableBody.style.visibility = "hidden";
			tableBody.style.height = "0px";
			elTarget.style.backgroundPosition = "0px 0px";
		}
						
		if(YAHOO.util.Dom.hasClass(elTarget, 'arrow')){
			toggle(elTarget.parentNode.parentNode, elTarget);					
			YAHOO.util.Event.stopEvent(e);			
		}
    };   

	return {
		/*
		 * IMPORTANT: The last method here should not have a trailing comma.
		 * This will cause all of the JavaScript on the page to break with Microsoft I.E.		
		 * 
		 * Public methods.
		 */ 
		init: function ()
		{
		  YAHOO.util.Event.addListener(YAHOO.util.Dom.getElementsByClassName("arrow"), "click", clickHandler, this, true);
		}
	};
}(); // force the anonymous function to execute and return	


BRAINS.scroll = function() 
{	
	/*
	 * Private methods.
	 */
	var currentYPosition = function()
	{
		if (self.pageYOffset)
			 return self.pageYOffset;
		if (document.documentElement && document.documentElement.scrollTop)
			return document.documentElement.scrollTop;
		if (document.body.scrollTop)
			 return document.body.scrollTop;
		return 0;
	};	
	
	var elmYPosition = function (eID)
	{
		var elm = document.getElementById(eID);
		var y = elm.offsetTop;
		var node = elm;
		while (node.offsetParent && node.offsetParent != document.body) {
			node = node.offsetParent;
			y += node.offsetTop;
		} 
		return y;
	};
	
	var smoothScroll = function(eID)
	{		
		var startY = currentYPosition();
		var stopY = elmYPosition(eID);
		var distance = stopY > startY ? stopY - startY : startY - stopY;
		
		if (distance < 100) {
			scrollTo(0, stopY); return;
		}	
	
		var speed = Math.round(distance / 35);
		var step = Math.round(distance / 30);
	
		var leapY = stopY > startY ? startY + step : startY - step;
		var timer = 0;
		
		if (stopY > startY) {
			for (var i = startY; i < stopY; i += step) {
				setTimeout("window.scrollTo(0, " + leapY +")", timer * speed);
				leapY += step; if (leapY > stopY) leapY = stopY; timer++;
			} return;
		}
		for (var i = startY; i > stopY; i-= step) {
			setTimeout("window.scrollTo(0, " + leapY + ")", timer * speed);
			leapY -= step; if (leapY < stopY) leapY = stopY; timer++;
		}
	};
	
	var clickHandler = function(e) 
	{
		var elTarget = YAHOO.util.Event.getTarget(e);
		var elHref =elTarget.href;
		
		var eId = elHref.substring(elHref.lastIndexOf("#") + 1, elHref.length);
		
		smoothScroll(eId);
				
		YAHOO.util.Event.stopEvent(e);	
	};
	
	return {
		/*
		 * IMPORTANT: The last method here should not have a trailing comma.
		 * This will cause all of the JavaScript on the page to break with Microsoft I.E.		
		 * 
		 * Public methods.
		 */ 
		init: function ()
		{
		  var anchors = document.getElementsByTagName('a');
		  		  		  
		  for (var i = 0; i < anchors.length; i++) {
		  	if (anchors[i].href.indexOf("#") > -1) {
		  		YAHOO.util.Event.addListener(anchors[i], "click", clickHandler, this, true);
		  	}
		  }
		}
	};
}(); // force the anonymous function to execute and return
