var SAIT = {};

SAIT.Util = {
	
	_appendLoadEvent: function(f) {
		var oldonload = window.onload;
		
		if (typeof window.onload != "function") {
			window.onload = f;
		} else {
			window.onload = function() {
				oldonload();
				f();
			}
		}
	},
	
	_addEvent: function (element, type, handler, useCapture) {		
		if (element.addEventListener) {
			element.addEventListener(type, handler, useCapture);
		} else {			
			if (!handler.$$guid) handler.$$guid = (typeof(SAIT.Util._addEvent.guid)=="undefined") ? SAIT.Util._addEvent.guid = 1 : ++SAIT.Util._addEvent.guid;			
			if (!element.events) element.events = {};			
			var handlers = element.events[type];
			if (!handlers) {
				handlers = element.events[type] = {};
				if (element["on" + type]) {
					handlers[0] = element["on" + type];
				}
			}
			handlers[handler.$$guid] = handler;			
			element["on" + type] = SAIT.Util._handleEvent;
		}
	},
	
	// _handleEvent()
	_handleEvent: function (event) {		
		var returnValue = true;		
		event = event || SAIT.Util._fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
		// get a reference to the hash table of event handlers
		var handlers = this.events[event.type];
		// execute each event handler
		for (var i in handlers) {
			if (!Object.prototype[i]) {
				this.$$handleEvent = handlers[i];
				if (this.$$handleEvent(event) === false) {
					returnValue = false;
					break;
				}
			}
		}

		if (this.$$handleEvent)
			this.$$handleEvent = null;
		return returnValue;
	},

	_fixEvent: function (event) {
		/// <summary>Used internally to add W3C standard event methods to IE event objects.</summary>
		event.preventDefault = function () { this.returnValue = false; };
		event.stopPropagation = function () { this.cancelBubble = true; };
		return event;
	},
	
	_getElementsByClassName: function(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;
	},
	
	// --------------------------------------------------------------------------------
// InitSearchBox()
// Find all search textfields and wire up the [ENTER] key to submit the form properly.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
	_initSearchBox: function() {
		var txt;
		var inp = SAIT.Util._getElementsByClassName("s_sbox", "input");		
		for (var i = 0; i < inp.length; i++) {
			SAIT.Util._addEvent(inp[i], "keypress", SAIT.Util._searchOnEnter);
		}
	},

// --------------------------------------------------------------------------------
// SearchOnEnter()
// Click the corresponding search button when [ENTER] is pressed.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
	_searchOnEnter: function(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("s_txt", "s_btn"));
			if (btn) {				
				btn.click(SAIT.Map.doSearch);
				if (e.preventDefault) e.preventDefault(); else e.returnValue = false;	
			}	
		}
	}
	
}

SAIT.Util._appendLoadEvent(SAIT.Util._initSearchBox);