// --------------------------------------------------------------------------------
// ow_f_util.js
// Matthew Hogg 15-Nov-2004
// Contains various useful functions, used for functionality of front-end apps.
// --------------------------------------------------------------------------------

var ow_LoadAfterInitQueue = new Array();
var ow_PollForDOMCounter = 0;
var ow_PollForDOMInterval = null;

ow_f_AppendLoadEvent(InitSearchBox);
//ow_LoadAfterInitQueue[0] = InitSearchBox;
if (ow_isDOMInitialized())
	ow_RunInitMethods();
else
	ow_PollForDOMInterval = setInterval(ow_PollForDOM, 50);

// --- Functions and variables. ---

// --------------------------------------------------------------------------------
// InitSearchBox()
// Find all search textfields and wire up the [ENTER] key to submit the form properly.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function InitSearchBox() {
	var txt;
	var inp = ow_f_GetElementsByClassName("ow_sbox", "input");
	for (var i = 0; i < inp.length; i++) {
		if (inp[i].id.indexOf("ow_txtSearch") != -1) {
				ow_f_AddEvent(inp[i], "keypress", SearchOnEnter, false);
		}
	}
}

// --------------------------------------------------------------------------------
// SearchOnEnter()
// Click the corresponding search button when [ENTER] is pressed.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function SearchOnEnter(e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	
	if (code == 13) {
		if (e.target) elm = e.target;
		else elm = e.srcElement;
		var btn = document.getElementById((elm.id).replace("ow_txt", "ow_btn"));
		if (btn) {
			btn.click();
			if (e.preventDefault) e.preventDefault(); else e.returnValue = false;	
		}	
	}
}


// --------------------------------------------------------------------------------
// ow_isDOMInitialized()
// Checks to see if the DOM is initialized (full DOM hierarchy created).
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_isDOMInitialized() {
	return (typeof document.getElementsByTagName != "undefined" && (document.getElementsByTagName("body")[0] != null || document.body != null));
}

// --------------------------------------------------------------------------------
// ow_f_PollForDOM()
// Polls the DOM for full initialization (full DOM hierarchy created).
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_PollForDOM() {
	ow_PollForDOMCounter++;
	if (ow_isDOMInitialized()) {
		ow_RunInitMethods();
		clearInterval(ow_PollForDOMInterval);
		ow_PollForDOMInterval = null;
	} else if (ow_PollForDOMCounter = 10) { 
		// reset the interval to 250ms from 50 ms
		clearInterval(ow_PollForDOMInterval);
		ow_PollForDOMInterval = setInterval(ow_PollForDOM, 250);
	} else if (ow_PollForDOMCounter >= 60) { 
		// reset the interval to 1 sec
		clearInterval(ow_PollForDOMInterval); 
		ow_PollForDOMInterval = setInterval(ow_PollForDOM, 1000);
	}
}

// --------------------------------------------------------------------------------
// ow_RunInitMethods()
// Runs the initialization methods.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_RunInitMethods() {
	for (var i = 0; i < ow_LoadAfterInitQueue.length; i++) {
		try {
			ow_LoadAfterInitQueue[i]();
		} catch(ex) {
			var fn = ow_LoadAfterInitQueue[i] + "";
			fn = fn.substring(fn.indexOf(" ") + 1, fn.indexOf("("));
			alert("ERROR!\nThe function " + fn + "() was found in the DOM queue but could not be executed.\n" + ex.description);
		}
	}
	ow_LoadAfterInitQueue.length = 0;
}

// --------------------------------------------------------------------------------
// ow_f_AppendInitEvent()
// Appends a function to be executed when the DOM if fully initialised.  This method
//	simply adds the function pointer to the queue of other initializers.
// --------------------------------------------------------------------------------
// Arguments:
//	- f: function to be appended [function]
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_f_AppendInitEvent(f) {
	ow_f_AppendLoadEvent(f);
/*	IE is having a hard time with hard-refreshes not running these scripts
	ow_LoadAfterInitQueue[ow_LoadAfterInitQueue.length] = f;
	if (!ow_PollForDOMInterval)
		ow_RunInitMethods();
	*/
	
}

// --------------------------------------------------------------------------------
// ow_f_AppendLoadEvent()
// Append a function to be executed when the onload event fires.
// Normally ow_f_AddEvent() already does this, but IE seems to execute all functions
// in reverse order than how they were added.
// --------------------------------------------------------------------------------
// Arguments:
//	- f: function to be appended [function]
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_f_AppendLoadEvent(f) {
  var oldonload = window.onload;
  
  if (typeof window.onload != "function") {
    window.onload = f;
  } else {
    window.onload = function() {
      oldonload();
      f();
    }
  }
}

// --------------------------------------------------------------------------------
// ow_f_AppendUnloadEvent()
// Append a function to be executed when the onunload event fires.
// Normally ow_f_AddEvent() already does this, but IE seems to execute all functions
// in reverse order than how they were added.
// --------------------------------------------------------------------------------
// Arguments:
//	- f: function to be appended [function]
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_f_AppendUnloadEvent(f) {
  var oldonunload = window.onunload;
  
  if (typeof window.onunload != "function") {
    window.onunload = f;
  } else {
    window.onunload = function() {
      oldonunload();
      f();
    }
  }
}

// --------------------------------------------------------------------------------
// ow_f_AddEvent()
// Attach an event handler to an element.
// --------------------------------------------------------------------------------
// Arguments:
//	- elm: element to assign event handler to [object]
//	- evType: event to handle (i.e. "load", "click") [string]
//	- fn: name of function which will handle the event [function]
//	- useCapture: use Event capturing model? [boolean]
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_f_AddEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent("on" + evType, fn);
		return r;
	} else {
		elm.setAttribute("on" + evType, fn);
	}
}

// --------------------------------------------------------------------------------
// ow_f_RemoveEvent()
// Attach an event handler to an element.
// --------------------------------------------------------------------------------
// Arguments:
//	- elm: element to assign event handler to [object]
//	- evType: event to handle (i.e. "load", "click") [string]
//	- fn: name of function which will handle the event [function]
//	- useCapture: use Event capturing model? [boolean]
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_f_RemoveEvent(elm, evType, fn, useCapture) {
	if (elm.removeEventListener) {
		elm.removeEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.detachEvent) {
		var r = elm.detachEvent("on" + evType, fn);
		return r;
	} else {
		elm.removeAttribute("on" + evType);
	}
}

// --------------------------------------------------------------------------------
// ow_GetElementsByClassName()
// Find all document elements with the specified CSS class attribute.
// --------------------------------------------------------------------------------
// Arguments:
//	- cls: class name to be searched for [string]
//	- tag: searhc for specific tags with givn class, or "*" for all [string]
// Returns:
//	- all elements with the specified class [array]
// --------------------------------------------------------------------------------
function ow_f_GetElementsByClassName(cls, tag) {
	var arr = new Array();
	var j = 0;
	var i;
	var elm = document.getElementsByTagName(tag);
	
	for (i = 0; i < elm.length; i++) {
		if (elm[i].className == cls) {
			arr[j] = elm[i];
			j++;
		}
	}
	return arr;
}

// --------------------------------------------------------------------------------
// ow_GetTotalOffset()
// Compute the total offset of an element from the top-left corner of the screen.
// --------------------------------------------------------------------------------
// Arguments:
//	- elm: element to calculate the offset for [object]
//	- off: type of offset to calculate (i.e. "offsetLeft", "offsetTop") [string]
// Returns:
//	- total calculated offset for specified element [integer]
// --------------------------------------------------------------------------------
function ow_f_GetTotalOffset(elm, off) {
	var totalOffset = 0;
	var item = eval("elm");
	
	do {
		totalOffset += eval("item." + off);
		item = eval("item.offsetParent");
	} while (item != null);
	return totalOffset;
}

// --------------------------------------------------------------------------------
// ow_GetElementStyle()
// Get current value of a CSS style property for a specified element.
// --------------------------------------------------------------------------------
// Arguments:
//	- elm: element to find a style for [object]
//	- prop: name of property to find value for (i.e. "margin-top") [string]
// Returns:
//	- current value of specified property [string]
// --------------------------------------------------------------------------------
function ow_f_GetElementStyle(elm, prop) {
	if (window.getComputedStyle) {
		return window.getComputedStyle(elm, null).getPropertyValue(prop);
	} else if (elm.currentStyle) {
		var ieProp = "";
		for (var i = 0; i < prop.length; i++) {
			if (prop.charAt(i) == "-") {
				i++;
				ieProp += prop.charAt(i).toUpperCase();
			} else {
				ieProp += prop.charAt(i);
			}
		}
		return eval("elm.currentStyle." + ieProp);
	}
}

// --------------------------------------------------------------------------------
// ow_f_StopEventProp()
// Prevent triggered events from propogating to parent elements.
// --------------------------------------------------------------------------------
// Arguments:
//	- e: triggered event [object]
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_f_StopEventProp(e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	return true;
}

// --------------------------------------------------------------------------------
// ow_f_PreventDefault()
// Prevent triggered events from executing their default action.
// --------------------------------------------------------------------------------
// Arguments:
//	- e: triggered event [object]
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function ow_f_PreventDefault(e) {
	if (!e) var e = window.event;
	if (e.preventDefault) { e.preventDefault(); } else { e.returnValue = false; }
}
