/**
	Copyrights (c) GatherWorks, Inc.
*/
	var webapp				= "omniview";
	var UNDEFINED			= "undefined";

/************************************** HOOKING LOAD AND UNLOAD OF DOCUMENT ***********************************/
	var onLoad0			= new CALLBACK("onLoad0");	//use this one for stuff that must be initialized before everything else
	var onLoad			= new CALLBACK("onLoad");	//use this one for most onLoad operations
	var onLoad2			= new CALLBACK("onLoad2");	//use this one when you want things to happen at the last possible time
	var onBeforeunload	= new CALLBACK("onBeforeunload");
	var onUnload0		= new CALLBACK("onUnload0");	//do first
	var onUnload		= new CALLBACK("onUnload");	//most should use this one
	var onUnload2		= new CALLBACK("onUnload2");	// do onUnload0, then onUnload, then onUnload2
	
	window.onDomReady = DomReady;
	function DomReady(fn)
	{
		if(document.addEventListener)
		{
			document.addEventListener("DOMContentLoaded", fn, false);
		}
		else
		{
			document.onreadystatechange = function(){readyState(fn)}
		}
	}
	function readyState(fn)
	{
		if(document.readyState == "interactive")
		{
			fn();
		}
	}

	window.onDomReady(gw_init);
	function gw_init() {
		onLoad0.doCallbacks();
		onLoad.doCallbacks();
		onLoad2.doCallbacks();
	}

/************************************** GENERAL FUNCTIONS ***********************************/
	function isundefined(ob) {
		try {
			return typeof(ob)==UNDEFINED;
		} catch(err) {
			return true;
		}
	}
	
	function undefinedOrNull(ob) {
		try {
			return typeof(ob)==UNDEFINED || ob==null;
		} catch(err) {
			return true;
		}
	}

	function isdefined(ob) {
		try {
			return typeof(ob)!=UNDEFINED;
		} catch(err) {
			return false;
		}
	}
	
	function pushChildren(obj, a) {
			var ar = obj.childNodes;
			for (var i=0; i<ar.length; i++) {
				if (ar[i].nodeName.toLowerCase()=="input") {
					a.push(ar[i]);
				}
				if (ar[i].childNodes.length>0) pushChildren(ar[i], a);
			}
	}
	
	//http://www.opensourcetutorials.com/tutorials/Client-Side-Coding/JavaScript/javascript-document-object-model/page4.html
	function getChildren(obj) {
		if (document.all) return obj.all;
		if (document.getElementById) {
			var a = new Array();
			pushChildren(obj, a);
			/* firefox
			for (x in obj) {
				if (typeof(obj[x].nodeName)=="string") {
					a.push(obj[x]);
					//document.writeln(x + " = " + typeof(obj[x].nodeName) + "<br />");
				} else {
					break;
				}
			}
			*/
			return a;	//http://xulplanet.com/references/objref/HTMLInputElement.html
		}
	}
	
	
	function getObject(name) {
		if (document.all) return document.all(name);
		if (document.getElementById) return document.getElementById(name);
		if (document.layers) return document.layers(name);
		return null;
	}
	
	function getFrame(frameid) {
		return document.frames[frameid];
	}

	function getFieldType(field) {
		try {
			return field.nodeName.toLowerCase()
		} catch(err) { return null; }
	}
	
	function getField(name) {
		return oGetField(getObject(name));
	}
	function oGetField(obj) {
		try {
			if (getFieldType(obj)=="input" && obj.type=="checkbox") return obj.checked;
			return obj.value;
		} catch(err) {
			return "";
		}
	}
	function oSetField(obj, value) {
		try {
			if (getFieldType(obj)=="input" && obj.type=="checkbox") {
				obj.checked = (value=="true");
			} else {
				obj.value = value;
			}
		} catch(err) {
		}
	}
	
	function setField(o, value) {
		var obj	= getObject(o);
		try {
			if (getFieldType(obj)=="input" && obj.type=="checkbox") {
				obj.checked = (value=="true");
			} else {
				obj.value = value;
			}
		} catch(err) {
		}
	}
	
	function getIntField(name) {
		try {
			return parseInt(getObject(name).value);
		} catch(err) {
			return -1;
		}
	}
	
	function getTime() {
		return new Date().getTime();
	}
	
	function sleep(millis) {
		var d = new Date();
		while (true) {
			if (new Date() - d > millis) break;
		}
	}

	var gw_frameidCounter = 0;
	function addIframe(id, width, height) {
		if (typeof(width)==UNDEFINED) width = "0";
		if (typeof(height)==UNDEFINED) height = "0";
		var newIframe	= document.createElement("iframe");
		if (typeof(id)==UNDEFINED) id = "gw_frameid"+ gw_frameidCounter++;
		newIframe.setAttribute("id", id);
		newIframe.setAttribute("width", width);
		newIframe.setAttribute("height", height);
		document.body.appendChild(newIframe);
		if (width=="0" && height=="0") hideObject(id);
		return newIframe;
	}
	
	function addDiv(id) {
		var newDiv	= document.createElement("div");
		newDiv.setAttribute("id", id);
		try {
			document.body.appendChild(newDiv);
		} catch(err) {	
			return null;	//happens when document is not loaded yet (in firefox)
		}
		return newDiv;
	}
	
	function setRootPath(path)
	{
		if (path.endsWith("/")==false)
		{
			path	+= "/";
		}
		gw_rootPath = path;
	}
	var gw_rootPath		= null;
	var gw_sessionString	= "";
	function getRootPath()
	{
		if (gw_rootPath!=null)
		{
			return gw_rootPath;
		}
		var i, path = document.location.href;
		i = path.indexOf(";");
		if (i>0)
		{
			var temp	= path.substring(i);
			i = temp.indexOf("?");
			if (i>0)
			{
				temp	= temp.substring(0,i);
			}
			gw_sessionString	= temp;//includes ;
			path = path.substring(0, i);
		}
		path += "/";
		path = path.replace(/\\/g, "/");
		var count = 2;
		for (i = 8; i < path.length && count>0; i++) {
			if (path.charAt(i) == "/")
			{
				count--;
			}
		}
		gw_rootPath = path.substring(0, i-1);
		if (gw_rootPath.indexOf(webapp)==-1)
		{
			gw_rootPath = gw_rootPath.substring(0, gw_rootPath.lastIndexOf("/"));
		}
		gw_rootPath += "/";
		return gw_rootPath;	
	}
	
	function getHttpSessionId() {
		var temp = document.location.pathname;
		if (temp.substring(temp.length)=="#") temp = temp.substring(0, temp.length-1);
		var pos = temp.indexOf(";");
		if (pos==-1) {
			return "";
		} else {
			return temp.substring(pos);
		}
	}

	function getRootPathPort(newPort, removeApp) {
		var path	= getRootPath();
		var pos		= -1;

		var i;
		for (i = 7; i < path.length; i++) {
			if (path.charAt(i) == ":") pos=i;
			if (path.charAt(i) == "/") break;
		}
		if (pos==-1) pos = i;
		
		var temp = path.substring(0, pos)+":"+newPort+(removeApp?"/" : path.substring(i));
		return temp;
	}
	
	function showObject(object) {
		if (document.getElementById && document.getElementById(object) != null)
			 node = document.getElementById(object).style.visibility='visible';
		else if (document.layers && document.layers[object] != null)
			document.layers[object].visibility = 'visible';
		else if (document.all)
			document.all[object].style.visibility = 'visible';
	}
	
	function hideObject(object) {
		if (document.getElementById && document.getElementById(object) != null)
			 node = document.getElementById(object).style.visibility='hidden';
		else if (document.layers && document.layers[object] != null)
			document.layers[object].visibility = 'hidden';
		else if (document.all)
			 document.all[object].style.visibility = 'hidden';
	}
	
	// Apply a CSS style to a form element ("node"). Works in the
	// Netscape6 DOM and the MSIE DOM. The name of the stylesheet
	// property in CSS syntax ("property") is automatically converted
	// to the MSIE syntax when necessary. The value ("value") should
	// be in CSS syntax.
	function setStyle(node, property, value) {
		var ieproperty = "";
		for (var i = 0; i < property.length; i ++) {
			if (property.charAt(i) == "-" && i < property.length)
				ieproperty += property.charAt(++ i).toUpperCase();
			else
				ieproperty += property.charAt(i);
		}
		if (node.style) {
			if (node.style[ieproperty] != null)
				node.style[ieproperty] = value;
			else
				node.style = property + ': ' + value;
		}
	}

	//onKeyUp="ifEnter(event, init);" 
	function ifEnter(event, callback) {     
		var code = 0;
		if (typeof(event.which)!=UNDEFINED)
			code = event.which;
		else
			code = event.keyCode;
		if (code==13) callback();
	}

	function oSetHtml(obj, s) {
		obj.innerHTML = s;
	}
	
	function setHtml(obj, s) {
		var temp = getObject(obj);
		if (typeof(temp)==UNDEFINED) {
			msg("Invalid HTML object: "+obj);
		} else {
			oSetHtml(temp, s);
		}
	}
	
	function getHtml(obj) {
		var temp = getObject(obj);
		if (typeof(temp)==UNDEFINED) {
			msg("Invalid HTML object: "+obj);
			return ""; 
		} else {
			return temp.innerHTML;
		}
	}
	
	function setImage(img, file, alt) {
		var image = getObject(img);
		image.src = gw_rootPath+"images/"+file;
		image.alt = alt;
	}

	
/************************************** CALLBACK ENGINE ***********************************/
	function CALLBACK(name) {
		this.name					= name;
		this.myCallbacks			= new Array();
		this.notifyOnAddCallback	= null;
		CALLBACK.prototype.add = function(callback) {
			if (callback==null || typeof(callback)==UNDEFINED) {
				return;
			}
			//do not add twice
			for (i=0; i<this.myCallbacks.length; i++) {
				if (this.myCallbacks[i] == callback) return;
			}
			this.myCallbacks.push(callback);
			
			if (this.notifyOnAddCallback != null) this.notifyOnAddCallback(callback);
		}
		CALLBACK.prototype.doCallbacks = new Function('var args = new Array(), i;for(i=0;i<arguments.length;i++){args.push(arguments[i]);};	try { return this.doCallbacksNow(args); } catch(err) {}');
		CALLBACK.prototype.doCallbacksNow = function(args) {
			var i;
			for (i=0; i<this.myCallbacks.length; i++) {
				var callback = this.myCallbacks[i];
				callback(args);
			}
			return (this.myCallbacks.length>0);	// returns true if there are some callbacks
		}
		CALLBACK.prototype.doCallbacksLogicalAnd = function(defaultValue) {
			if (this.myCallbacks.length==0) return defaultValue;
			var i;
			var result = true;
			for (i=0; i<this.myCallbacks.length; i++) {
				var callback = this.myCallbacks[i];
				result &= callback();
			}
			return result;
		}
		CALLBACK.prototype.doCallbacksAppend = function() {
			var i;
			var result = "";
			for (i=0; i<this.myCallbacks.length; i++) {
				var callback = this.myCallbacks[i];
				result += callback();
			}
			return result;
		}
		CALLBACK.prototype.clear = function() {
			this.myCallbacks = new Array();
		}
		CALLBACK.prototype.notifyOnAdd = function(callback) {
			this.notifyOnAddCallback = callback;
		}
		CALLBACK.prototype.size = function() {
			return this.myCallbacks.length;
		}
	}
			

	function queryString(parm) {
		var querystringpassed = location.search.substring(1);
		if (!querystringpassed) return '';
		var startPos = 0 + querystringpassed.indexOf(parm + '=');
		while (startPos > -1) {
			startPos = startPos + parm.length + 1;
			var endPos = 0 + querystringpassed.indexOf('&',startPos);
			if (endPos == -1) endPos = querystringpassed.length;
			return unescape(querystringpassed.substring(startPos,endPos));
		}
		return '';
	}

		String.prototype.max = function(len) {
		if (this.length > len) return this.substring(0,len-3)+"..."; 
		return this;
	}

	/* (("Hello world!").endsWith("!")) */
	String.prototype.endsWith = function(sEnd) {
		return (this.substr(this.length-sEnd.length)==sEnd);
	}
	
	/* (("Hello world!").startsWith("H")) */
	String.prototype.startsWith = function(sStart) {
		return (this.indexOf(sStart)==0);
	}
	
	/* (("Hello world!  ").trim().replace(" ",".") */
	String.prototype.trim = function() {
		var b=0,e=this.length -1;
		while(this.substr(b,1) == " ") b++;
		while(this.substr(e,1) == " ") e--;
		return this.substring(b,e+1);
	}
	
	
	/* (("Hello world!").toCharArray().join("\n")) */
	String.prototype.toCharArray = function() {
		var arrRet = new Array();
		for(var i=0;i<this.length;i++) arrRet.push(this.substr(i,1));
		return arrRet;
	}
	
	
	/* (("Hello world!").reverse()); */
	String.prototype.reverse = function() {
		var a = new Array();
		for(var i=0;i<this.length;i++) a.push(this.substr(i,1));
		return a.reverse().join("");
	}
	
	
	/* (("first,second,third").split(",").indexOf("second")); */
	Array.prototype.indexOf = function() {
		if(arguments.length==1) {
			for(var i=0;i<this.length;i++) {
				if(this[i]==arguments[0]) return i;
			}
		}
		return -1;
	}
	
	/* var a = ("first,second,third").split(","); msg(a.length); a.clear(); msg(a.length); */
	Array.prototype.clear = function() {
		return this.splice(0,this.length);
	}
	
	/*var a = ("first,second,third").split(","); var b = a.copy; msg("a:" + a.length + "\nb:" + b.length); */
	Array.prototype.copy = function() {
		return this.slice(0,this.length);
	}
	
	/* msg((100).changeSystem(16)); */
	Number.prototype.changeSystem = function(iSystem) {
		if(typeof iSystem != "number") return "0";
		var arrIndex = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
		var arrValue = new Array();
		var intPower = 0;
		var intValue = this;
		var strRet   = "";
	
		if(iSystem>arrIndex.length) throw "Index out of range, max=" + arrIndex.length;
	
		while(intValue > Math.pow(iSystem,intPower)) intPower++;
	
		for(i=intPower;i>0;i--)	{
			arrValue[i] = arrIndex[Math.floor(intValue / Math.pow(iSystem,i))];
			intValue %= Math.pow(iSystem,i);
		}
		arrValue[i--] = arrIndex[intValue];
		strRet = arrValue.reverse().join("");
		if(strRet.substr(0,1)=="0") strRet = strRet.substr(1);
		return strRet;
	}
	
	/* Timer.start(10, repeatFn, callbackFn); */
	var Timer = {
		delay: 0,
		repeat: null,
		callback: null,
		loop: function() {
			if (this.delay>0) {
				var t = setTimeout("Timer.loop()", 1000);
				this.repeat();
				this.delay--;
			} else {
				this.callback();
			}
		},
		start: function(delay, repeat, callback) {
			this.delay = delay;
			this.repeat = repeat;
			this.callback = callback;
			this.loop();
		}
	};
	
	function preview(url)
	{
		var target = "_blank";
		var options = "";
		var win		= null;
		options += "width="+screen.width/2;
		options += ",height="+screen.height;
		options += ",scrollbars=yes";
		if (window.open)
		{
			win	= window.open(url, target, options);
		}
		if (win==null)
		{
			alert("Cannot open window. Please allow pop-ups for this page.");
		}
	}
	
/************************************** COOKIES ***********************************/
	var cookieTimeout = new Date();
	fixDate(cookieTimeout);
	cookieTimeout.setTime(cookieTimeout.getTime() + 5 * 365 * 24 * 60 * 60 * 1000);
	
	function setCookie(name, value, expires, path, domain, secure) {
		//((path) ? "; path=" + path : "")
		var curCookie = name + "=" + escape(value) + "; expires=" + (expires ? expires.toGMTString() : cookieTimeout) +  "; path=" + (path ? path : "/") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
		document.cookie = curCookie;
	}
	
	function getCookie(name, defaultValue) {
		var dc = document.cookie;
		var prefix = name + "=";
		var tmp;
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1) {
			begin = dc.indexOf(prefix);
			if (begin != 0) return "";
		} else {
			begin += 2;
		}
		var end = document.cookie.indexOf(";", begin);
		if (end == -1) end = dc.length;
		tmp = unescape(dc.substring(begin + prefix.length, end));
		if (tmp==null) {
			if (typeof(defaultValue)==UNDEFINED) return "";
			return defaultValue;
		}
		return tmp;
	}
	
	function deleteCookie(name, path, domain) {
		document.cookie = name + "=" +  "; path=" + (path ? path : "/") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
	
	function fixDate(date) {
		var base = new Date(0);
		var skew = base.getTime();
		if (skew > 0) date.setTime(date.getTime() - skew);
	}
	
	function getVar(v, defaultvalue) {
		var tmp = getCookie(v, null);
		if (tmp==null) {
			if (typeof(defaultvalue)!=UNDEFINED) {
				tmp = defaultvalue;
				setVar(v, tmp);
			}
		}	
		return tmp;
	}
	
	function setVar(t, val) {   // pass varname as a string and not the variable itself
		setCookie(t, val, cookieTimeout, "/");
	}

	function removeVar(t, val) {
		deleteCookie(t);
		//deleteCookie(t);	//just in case; saw double cookies on Fiona's laptop
	}
	

	function modalDialog(url, param, w, h) {
		if (param==null) param = new Object();
		try {
			var temp = window.showModalDialog(url, param, "help:0;center:1; dialogWidth:"+w+"px; dialogHeight:"+h+"px");
			if (temp!=null) return temp;
		} catch (err) { }
		msg("Please disable your popup blockers for this site");
	}
	
	function dialog(url, name, w, h, x, y, resizeable, scrollbars, menubar, location, toolbar, personalbar, status, fullscreen) {
		var opts = new Array;
		opts[opts.length] = "screenX=" + (x||400);
		opts[opts.length] = "screenY=" + (y||400);
		opts[opts.length] = "left=" + (x||400);
		opts[opts.length] = "top=" + (y||400);
		opts[opts.length] = "width=" + (w||400);
		opts[opts.length] = "height=" + (h||400);
		opts[opts.length] = "resizeable=" + ((resizeable) ? "yes" : "no");
		opts[opts.length] = "scrollbars=" + ((scrollbars) ? "yes" : "no");
		opts[opts.length] = "menubar=" + ((menubar) ? "yes" : "no");
		opts[opts.length] = "location=" + ((location) ? "yes" : "no");
		opts[opts.length] = "toolbar=" + ((toolbar) ? "yes" : "no");
		opts[opts.length] = "personalbar=" + ((personalbar) ? "yes" : "no");
		opts[opts.length] = "status=" + ((status) ? "yes" : "no");
		if (fullscreen) {
			opts[opts.length] = "fullscreen";	//IE only
		}
		if (name.indexOf(" ") != -1) {
			msg("No spaces allowed in the name of a window");
			return null;
		}
		var win = window.open(url, name, opts.join(","));
		if (win != null) {
			win.opener = this;
			win.focus();
		} else {
			msg("Please disable your popup blockers for this site");
		}
		return win;
	}

	function enc(s) {
		return encodeURIComponent(s);
	}
