
/* ===== Body Onload Events ======= */
	window.onloadArray = new Array();
	
	// to add functions to the onload event. 
	// this should get called from following js files, instead of window.onload	
	window.addToOnload = function(aFunction)
	{
		//alert("/* Added Function: */  \n" + aFunction.toString()); 
		window.onloadArray.push(aFunction)
	} 
	
	function windowOnload()
	{

		/*		
		var feedback = new FeedbackArea();
		feedback.div.className = "displayFeedback";
		var theWrapper = document.getElementById("wrapper");
		theWrapper.appendChild(feedback.div);		
		feedback.add("<pre>" + window.onloadArray + "</pre>");
		feedback.show();
		*/
		
		for (i = 0; i<window.onloadArray.length;i++)
		{
			//alert("/* Call Function: */  \n" + window.onloadArray[i].toString());
			window.onloadArray[i]();
		}
	}
	
	//add to onload event
	window.onload=windowOnload;

/* ======= */

/* ===== Form Onsubmit Events ======= */
	
	window.onsubmitArray = new Array();
	
	// to add functions to the form onsubmit event. this should get called 
	// from following js files, instead of form.onsubmit
	window.addToOnsubmit = function(aFunction)
	{
		window.onsubmitArray.push(aFunction);
	}
	
	var submitReturn;
	function formOnsubmit()
	{
		submitReturn = true;
		
		for (var i = 0; i < window.onsubmitArray.length; i++)
		{
			window.onsubmitArray[i]();
		}
		
		if (submitReturn == false)
		{
			return false;
		}
	}	
	
	function setupOnsubmit()
	{
		// Setup the Form Submit.
/*		if(document.getElementsByTagName("form").length > 0)
		{
			var theForm = document.getElementsByTagName("form")[0];
			theForm.onsubmit=formOnsubmit;
		}*/
		if(document.getElementById("form"))
		{
			var theForm = document.getElementById("form");
			theForm.onsubmit=formOnsubmit;
		}				
	}
	
	window.addToOnload(setupOnsubmit);
	
/* ======= */

/* ===== Window Resize Events ====== */

	window.onresizeArray = new Array();
	
	window.addToOnresize = function(aFunction)
	{
		window.onresizeArray.push(aFunction);
	}
	
	function windowResize()
	{
		for (i = 0; i<window.onresizeArray.length;i++)
		{
			window.onresizeArray[i]();
		}
	}
	
	//add to onresize event
	window.onresize=windowResize;	


/* ======= */

/* ===== Window Unload Events ====== */

	window.unloadArray = new Array();
	
	window.addToUnload = function(aFunction)
	{
		window.unloadArray.push(aFunction);
	}
	
	function windowUnload()
	{		
		for (i = 0; i<window.unloadArray.length;i++)
		{
			window.unloadArray[i]();
		}
	}
	
	//add to onresize event
	window.onunload=windowUnload;	


/* ======= */

/* ===== Custom Objects ==== */	

	// FeedbackArea Object - used for creating special feedback area divs with 
	// extra functionality. the 'scriptFeedback' and 'displayFeedback' use this.
	function FeedbackArea(setupDiv)
	{
		if(!setupDiv) var setupDiv = document.createElement("div");
		this.div = setupDiv;
		this.div.className = "feedback";
		this.hide 	= function() { this.div.style.display="none"; }
		this.show 	= function() { this.div.style.display="block"; }
		this.clear 	= function() { this.div.innerHTML = ""; }		
		this.addNode 	= function(feedbackKind, messageNode) 
		{ 
			//this.text = this.text + newText; this.reloadText(); 
			var newItem = document.createElement("div");
			newItem.className = feedbackKind;
			newItem.appendChild(messageNode);
			this.div.appendChild(newItem);
		}
		this.addText = function(feedbackKind, messageText) 
		{
			this.addNode(feedbackKind, document.createTextNode(messageText));
		}	
		this.insert	= function(feedbackKind, newText) 
		{ 
			//this.text = newText + this.text; this.reloadText(); 
			var newItem = document.createElement("div");
			newItem.className = feedbackKind;
			newItem.innerHTML = newText;
			this.div.insertBefore(newItem, this.list.firstChild);
		}
		this.focus = function()
		{
			this.div.focus();
		}		
	}
	
	var FeedbackKind =
	{
		Message: "message",
		Warning:	"warning",
		Error:	"error"
	} 
	
	// Event object, used for multiple event handling.
	function Event()
	{
		this._allHandlers = new Array();		
		this.add = function(handler)
		{
			this._allHandlers.push(handler);
		}	
		this.remove = function(handler)
		{
			for(var i = 0; i <= this._allHandlers.length; i++)
			{
				if(this._allHandlers[i] == handler) this._allHandlers.splice(i, 1);
			}
		}	
		this.execute = function()
		{
			for(var i = 0; i <= this._allHandlers.length; i++)
			{
				try
				{
					this._allHandlers[i]();
				}
				catch(e){}
			}	
		}		
	}
	
	// --

/* ======= */

/* ====== Custom Code/Methods ===== */

	window.escapeHTML = function(str)
	{
	   var div = document.createElement('div');
	   var text = document.createTextNode(str);
	   div.appendChild(text);
	   return div.innerHTML;
	}; 
	
	// Use this function when you want to stop the default behavoir of
	// an event without adding extra functionality. 
	// Using this aviods creating a Circular Reference in a Closure - that Leaks Memory.
	window.ReturnFalse = function(){ return false; };		

	// == allows us to getElementsByClassName like we would getElementsByTagName
	document.getElementsByClassName = function(className) 
	{
	  var children = document.getElementsByTagName('*') || document.all;
	  var elements = new Array();
	  
	  for (var i = 0; i < children.length; i++) {
	    var child = children[i];
	    var classNames = child.className.split(' ');
	    for (var j = 0; j < classNames.length; j++) {
	      if (classNames[j] == className) {
	        elements.push(child);
	        break;
	      }
	    }
	  }
	  return elements;
	}
	
	/*document.getChildElementsByClassName = function(className, parent) {
	  //alert(parent);
	  //var children = parent.getElementsByTagName('*') || document.all;
	  var children = parent.getElementsByTagName('*');
	  var elements = new Array();
	  
	  for (var i = 0; i < children.length; i++) {
	    var child = children[i];
	    var classNames = child.className.split(' ');
	    for (var j = 0; j < classNames.length; j++) {
	      if (classNames[j] == className) {
	        elements.push(child);
	        break;
	      }
	    }
	  }
	  return elements;
	}*/
	
	document.addCssClass = function(theControl, newCssClass)
	{
		if(theControl.className.indexOf(newCssClass) == -1)
		{
			if(theControl.className.length != 0)
			{
				newCssClass = " " + newCssClass;
			}
			theControl.className = theControl.className + newCssClass;
		}
	}
	
	document.removeCssClass = function(theControl, newCssClass)
	{
		theControl.className = theControl.className.replace(" " + newCssClass, "");
		theControl.className = theControl.className.replace(newCssClass, "");
	}		
	
	document.addBodyCssClass = function(newCssClass)
	{
		var theBody = document.getElementsByTagName("body")[0];
		document.addCssClass(theBody, newCssClass);
	}
	
	Date.prototype.time = function()
	{
		return this.toTimeString().substr(0, 8);
	}

/* ======= */
	
/* ===== Script Setup ==== */	
	
	var scriptFeedback;
	var displayFeedback;
	//var errorFeedback;
	function setupScript(){
		
		// Add body class 'scripted' to allow for CSS switches when Script is 'on'
		document.addBodyCssClass("scripted");
		
		var theContent = document.getElementById("content");
		
		// Add displayFeedback element into the DOM. 
		// (Good for reporting events/updates to the user)
		var existingFeedback = document.getElementsByClassName("feedback")[0];
		if(!existingFeedback) 
		{ 
			displayFeedback = new FeedbackArea(); displayFeedback.hide(); 
		}
		else
		{
			displayFeedback = new FeedbackArea(existingFeedback);
		}
		theContent.insertBefore(displayFeedback.div, theContent.firstChild);		
		
	}
	window.addToOnload(setupScript);

/* ======= */	
	
/* ===== Test Code ======= */
	
	// == NOT TESTED == 
	document.getElementsByAttributeValue = function(attributeName, attributeValue) 
	{
	  var children = document.getElementsByTagName('*') || document.all;
	  var elements = new Array();
	  
	  for (var i = 0; i < children.length; i++) {
	    	var child = children[i];
	    	var attributeValueFound = child.getAttribute(attributeName); //.split(' ');
			if (attributeValueFound == attributeValue) 
			{
				elements.push(child);
				break;
			}
	  }
	  
	  return elements;
	
	}
	
/* ======= */		
	







