// JavaScript Document

// Have these functions run whent he browser loads this file
window.onload = function() {
	// Initialize form elements for style on focus
	initFormElementsForFocus('text');
	// Look for tables to highlight
	hiliteTableRows(new Array('hilite', 'sortable', 'sortablehilite', 'hilitesortable'), '#ffc', '#ffc', 0);
	// Initialize tabtastic
	//loadTabtastic();
	
	// Make the first input tag on the page automatically have focus
	if (document.getElementsByTagName != null) {
		var inputElems = document.getElementsByTagName('input');
		for (var i = 0; i < inputElems.length; i++) {
			// Make sure we don't give focus to <input type="hidden"> tags and the like
			if(inputElems[i].type == 'text') {
				inputElems[i].focus();
				break;
			}
		}
	}
	
	  var title = getDefaultStyleSheet();
	  changeStyleSheet(title);
}

var g_FocusElementStyle = "1px solid #000000";
var g_UnFocusElementStyle = "2px solid #cbcbcb";
var g_FocusBackColor = "#ffc";
var g_reEmail = /^[\w\.=-]+\@[\w\.-]+\.[a-z]{2,4}$/;
var g_invalidFields = 0;

function addEventHandler(obj, evt, handler)
{
	if (obj.attachEvent != null)
	{
		obj.attachEvent("on"+evt, handler);
	}
	else if (obj.addEventListener != null)
	{
		obj.addEventListener(evt, handler, true);
	}
}

function getEventTarget(evt)
{
	var evtSource = null;
	if (typeof(window.event) != "undefined") {
		evtSource = window.event.srcElement;
	}
	else {
		evtSource = evt.target;
	}
	return evtSource;
}

function initFormElementsForFocus(sValidElems)
{
	var inputElems = document.getElementsByTagName('textarea');
	for (var i = 0; i < inputElems.length; i++) {
		addEventHandler(inputElems[i], 'focus', highlightFormElement);
		addEventHandler(inputElems[i], 'blur', unHighlightFormElement);
	}
	var inputElems = document.getElementsByTagName('input');
	for (var i = 0; i < inputElems.length; i++) {
			addEventHandler(inputElems[i], 'focus', highlightFormElement);
			addEventHandler(inputElems[i], 'blur', unHighlightFormElement);
	}
	var inputElems = document.getElementsByTagName('select');
	for (var i = 0; i < inputElems.length; i++) {
			addEventHandler(inputElems[i], 'focus', highlightFormElement);
			addEventHandler(inputElems[i], 'blur', unHighlightFormElement);
	}
}

function highlightFormElement(evt)
{
	var elem = getEventTarget(evt);
	if (elem != null) {
		//elem.style.border = g_FocusElementStyle;
		elem.style.backgroundColor = g_FocusBackColor;
	}
}

function unHighlightFormElement(evt)
{
	var elem = getEventTarget(evt);
	if (elem != null) {
		//elem.style.border = g_UnFocusElementStyle;
		elem.style.backgroundColor = "";
	}
}

function getLabelByID(idStr)
{
	var formLabels = document.getElementsByTagName('label');
	for (var i=0; i < formLabels.length; i++) {
		if (formLabels[i].getAttribute("htmlFor") == idStr)
			return formLabels[i];
	}
	return null;
}


var currentTR = null; // if the user clicks on a row, we store it aside so we can unhilite it later.
function hiliteTableRows(classNameArray, hiliteColor, clickColor, useClick)
{
	// check for DOM-capable browser. If this method isn't available, don't do anything.
	if (document.getElementById != null)
	{
		// Get all the tables 
		var tables = document.getElementsByTagName("table");
		// Loop through all the tables
		for(var i=0;i<tables.length;i++) {
			// Check if the table has the class name we are looking for
			//if(tables.item(i).className == className) {
			if(in_array(tables.item(i).className, classNameArray))
			{
				var oTable=tables.item(i);
				if (oTable != null)
				{
					// get all the TR tags in the table.
					var aTRs=oTable.getElementsByTagName('tr');
					for(var j=0;j<aTRs.length;j++)
					{
						// only highlight those TRs that are children of the TBODY.
						// remove this "if" statement if you want to highlight ALL rows.
						if(aTRs[j].parentNode.nodeName=='TBODY')
						{
							// add event handlers for each TR to handle mouseover, mouseout,
							// and mousedown events. For each event, set the inline style
							// of the tag to whatever you want to happen when that event occurs.
							// Using inline styles instead of classes makes it easy to combine
							// this code with other functions, like the table striping example,
							// since when the inline style is removed, the class applied by
							// the striping code won't be affected.
							aTRs[j].onmouseover=function() {
								if (currentTR == this) return false;
								this.style.backgroundColor=hiliteColor;
								//this.style.fontWeight="bold";
								return false;}
							aTRs[j].onmouseout=function(){
								if (currentTR == this) return false;
								this.style.backgroundColor="";
								//this.style.fontWeight="";
								return false;}
							// Don't use click highlighting if useClick is false
							if(useClick == 1) {
								aTRs[j].onmousedown=function(){
									// if there is already another row that the user clicked on,
									// remove its style before highlighting the one that was clicked.
									if (currentTR != null) {
										currentTR.style.backgroundColor="";
										//currentTR.style.fontWeight="";
										// if the row the user clicked on was already highlighted, then
										// just remove the styling and return.
										if (currentTR == this) {
											currentTR = null;
											return false;
										}
									}
									currentTR = this; // remember which row was clicked
									// highlight the row
									this.style.backgroundColor=clickColor;
									//this.style.fontWeight="bold";
									return false;}
							}
						}
					}
				}
			}
		}
	}
}


// Returns true or false based on whether the specified string is found
// in the array. This is based on the PHP function of the same name.
function in_array(stringToSearch, arrayToSearch) {
	for (s = 0; s < arrayToSearch.length; s++) {
		thisEntry = arrayToSearch[s].toString();
		if (thisEntry == stringToSearch) {
			return true;
		}
	}
	return false;
}

