function postBack(eventCommand, eventArgument) {
	var form = document.forms[0];
	form.elements["event-command"].value = eventCommand;
	form.elements["event-argument"].value = eventArgument;
	form.submit();
}

function postBackWithAlert(eventCommand, eventArgument, alertText) {
	alert(alertText);
	postBack(eventCommand, eventArgument);
}

function postBackWithPrompt(eventCommand, eventArgument, promptText) {
	var proceed = confirm(promptText);
	if (proceed) postBack(eventCommand, eventArgument);
}

function postBackForm(eventCommand, eventArgument, node) {
	while (node.nodeName.toUpperCase() != "FORM")
		node = node.parentNode;	
	
	node.elements["event-command"].value = eventCommand;
	node.elements["event-argument"].value = eventArgument;
		
	node.submit();
}

function postBackFormWithAlert(eventCommand, eventArgument, node, alertText) {
	alert(alertText);
	postBackForm(eventCommand, eventArgument, node);
}

function postBackFormWithPrompt(eventCommand, eventArgument, node, promptText) {
	var proceed = confirm(promptText);
	if (proceed)
		postBackForm(eventCommand, eventArgument, node);
}

function postBackFormWithFragmentIdentifier(eventCommand, eventArgument, form, fragmentIdentifier) {
	form.action += "#" + fragmentIdentifier;
	postBackForm(eventCommand, eventArgument, form);
}

function postBackFormWithPromptAndFragmentIdentifier(eventCommand, eventArgument, form, promptText, fragmentIdentifier) {
	form.action += "#" + fragmentIdentifier;
	postBackFormWithPrompt(eventCommand, eventArgument, form, promptText);
}

function autoComplete(input, select) {
	var optionLength = select.options.length;
	for (var i = 0; i < optionLength; ++i) {
		if (select.options[i].text.toUpperCase().indexOf(input.value.toUpperCase()) == 0) {
			select.selectedIndex = i;
			break;
		}
	}
}

/*
Giving warning to users when session is about to be abandoned
*/

function displayTimeout() {
	var now = new Date();
	now.setMinutes(now.getMinutes() + 5); // Add 5 minutes to the current time to indicate when you will be logged out
	var warningTimeout = "Warning: you will be logged out in 5 minutes (" + now.toLocaleTimeString() + ") due to inactivity.";
	alert(warningTimeout);
	return false;
}

// Schedule the function to run 11 hours 55 minutes (42900000 milliseconds) after this script file is loaded
// Note: the setTimoutWarning boolean is set in head.xsl
if (setTimeoutWarning) setTimeout("displayTimeout()", 42900000);

// Common javascript functions

function trimString(s) {
	var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
	return (m == null) ? "" : m[1];
}

function $() {
	var elements = new Array();
	var argLength = arguments.length;
	for (var i = 0; i < argLength; ++i) {
		var element = arguments[i];
		if (typeof element == "string")
		   element = document.getElementById(element);
		if (arguments.length == 1)
		   return element;
		if (element)
			elements.push(element);
	}
	return elements;
}

function toggle(obj) {
	if (obj.style.display != "none" ) {
		obj.style.display = "none";
	}
	else {
		obj.style.display = "";
	}
}

Array.prototype.inArray = function(value) {
	for (var i = this.length - 1; i >= 0; --i) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

// Returns the index of the removed item
Array.prototype.remove = function(value) {
	for (var i = this.length - 1; i >= 0; --i) {
		if (this[i] === value) {
			this.splice(i, 1);
			return i;
		}
	}
	return -1;
};

function addEvent(obj, type, fn) {
  if (obj.addEventListener) {
    obj.addEventListener(type, fn, false);
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e" + type + fn] = fn;
		obj[type + fn] = function() {
			obj["e" + type + fn](window.event);
		}
		obj.attachEvent("on" + type, obj[type + fn]);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function() {
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler) {
			listEvents.push(arguments);
		},
		flush : function() {
			var i, item;
			for (i = listEvents.length - 1; i >= 0; --i) {
				item = listEvents[i];
				if (item[0].removeEventListener) {
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if (item[1].substring(0, 2) != "on") {
					item[1] = "on" + item[1];
				};
				if (item[0].detachEvent) {
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();

addEvent(window, "unload", EventCache.flush);