
/* ===== 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()
	{

		for (var 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.	
		var theForm = document.getElementById("form");
		if(theForm != null)
		{
			theForm.onsubmit=formOnsubmit;
		}		
	}
	
	window.addToOnload(setupOnsubmit);
	
/* ======= */

/* ===== Custom Objects ==== */	

	// FeedbackArea Object - used for creating special feedback area divs with 
	// extra functionality. the 'scriptFeedback' and 'displayFeedback' use this.
	function FeedbackArea(div)
	{
		this.div = div;
		this.list = document.createElement("ul");
		this.div.appendChild(this.list);
		//this.list.className = "feedback";
		this.hide 	= function() { this.div.style.display="none"; }
		this.show 	= function() { this.div.style.display="block";}
		this.clear 	= function() { this.list.innerHTML = ""; }		
		this.addNode 	= function(feedbackKind, messageNode) 
		{ 
			//this.text = this.text + newText; this.reloadText(); 
			var listItem = document.createElement("li");
			listItem.className = feedbackKind;
			listItem.appendChild(messageNode);
			this.list.appendChild(listItem);
		}
		this.addText = function(feedbackKind, messageText) 
		{
			this.addNode(feedbackKind, document.createTextNode(messageText));
		}				
		this.insert	= function(feedbackKind, newText) 
		{ 
			//this.text = newText + this.text; this.reloadText(); 
			var listItem = document.createElement("li");
			listItem.className = feedbackKind;
			listItem.innerHTML = newText;
			this.list.insertBefore(listItem, this.list.firstChild);
		}
		this.focus = function()
		{
			this.list.focus();
		}
	}
	
	var FeedbackKind =
	{
		Message: "message",
		Warning:	"warning",
		Error:	"error"
	}

/* ======= */

/* ====== Custom Code/Methods ===== */

	// == 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.replaceCssClass = function(theControl, oldCssClass, newCssClass)
	{
		theControl.className = theControl.className.replace(oldCssClass, 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");
		
		// Add displayFeedback element into the DOM. 
		// (Good for reporting events/updates to the user)		
		var theContent = document.getElementById("content");
		var theFeedbackDiv = document.getElementById("feedback");
		var feedbackExists = true;
		if(theFeedbackDiv == null) 
		{
			theFeedbackDiv = document.createElement("div");
			theFeedbackDiv.id = "feedback";
			feedbackExists = false;
		}
		displayFeedback = new FeedbackArea(theFeedbackDiv);	
		if(!feedbackExists) 
		{
			displayFeedback.hide();		
			theContent.parentNode.insertBefore(theFeedbackDiv, theContent);	
		}
		
	}
	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;
	
	}
	
/* ======= */		
	






