if (typeof(beejjs.util) != "object" || beejjs.util == null) {

	beejjs.util = {

		/**
		 * Pad a number with leading zeros until it's a certain size.
		 */
		leadingZeroPad: function(n, size) {
			var nstr = n.toString();
			while (nstr.length < size) {
				nstr = "0" + nstr;
			}

			return nstr;
		},
						
		/**
		 * gets an element from the DOM by its id 
		 */
		elementById: function(id) {
			if (document.getElementById != null) {
				return document.getElementById(id);
			}

			if (document.all != null) {
				return document.all[id];
			}

			if (document.layers != null) {
				return document.layers[id];
			}

			return null;
		},

		/*
		 * if parse is false, just returns location.search, otherwise it
		 * parses location.search into an associative array, where each member
		 * is an array itself (probably of one element, but maybe more if a
		 * particular field has been specified multiple times.)
		 */
		getQueryString: function(parse) {
			if (parse == false) {
				return location.search;
			}

			var result = new Array();

			var pairs = (location.search.substr(1)).split("&");
			for (p in pairs) {
				var keyvalue = pairs[p].split("=");
				var key = unescape(keyvalue[0]);
				var value = unescape(keyvalue[1]);
				if (result[key] == undefined) {
					result[key] = new Array();
				}
				result[key].push(value);
			}

			return result;
		},

		/*
		 * returns the encoded form data for the form with the given ID
		 */
		getFormData: function(id) { // id=string
			form = element(id);

			str = "";

			for (i in form.elements) {
				e = form.elements[i];
				if (i > 0) {
					str += "&";
				}
				str += form.elements[i].name + "=" + escape(form.elements[i].value);
			}
			
			return str;
		},

		/**
		 * returns true if the first character of the string is a space
		 */
		isspace: function(s) {
			return s[0] == ' ' || s[0] == '\t' || s[0] == '\n' || s[0] == '\r';
		},

		/**
		 * remove leading and trailing spaces
		 */
		stripEndSpaces: function(s) { // s=string
			start = 0;
			while (isspace(s[start])) start++;

			end = s.length-1;
			while (isspace(s[end])) end--;

			return s.slice(start, end+1);
		},

		/**
		 * Sets the opacity of an element
		 *
		 * @param e the DOM element to make opaque
		 * @param opacity percentage opaque, 0-100
		 */
		setElementOpacity: function(e, opacity) {
			if (e == null) { return; }
			e.style.opacity = opacity / 100.0;
			e.style.MozOpacity = opacity / 100.0;
			e.style.filter = "alpha(opacity=" + opacity + ")";
		},

		setElementStyle: function(e, styleStr) {
			if (e == null) { return; }
			e.setAttribute("style", styleStr);
			e.style.cssText = styleStr; // IE
		},

		setElementClass: function(e, classStr) {
			if (e == null) { return; }
			e.setAttribute("class", classStr);
			e.setAttribute("className", classStr); // IE
		},

		getWindowWidth: function() {
			var myWidth = 0;
			if (typeof(window.innerWidth) == 'number') {
				//Non-IE
				myWidth = window.innerWidth;
			} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
				//IE 6+ in 'standards compliant mode'
				myWidth = document.documentElement.clientWidth;
			} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
				//IE 4 compatible
				myWidth = document.body.clientWidth;
			}

			return myWidth;
		},

		getWindowHeight: function() {
			var myHeight = 0;
			if (typeof(window.innerWidth) == 'number') {
				//Non-IE
				myHeight = window.innerHeight;
			} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
				//IE 6+ in 'standards compliant mode'
				myHeight = document.documentElement.clientHeight;
			} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
				//IE 4 compatible
				myHeight = document.body.clientHeight;
			}

			return myHeight;
		},

		strip: function(s) {
			return s.replace(/(^\s*)|(\s*$)/gi,"");
		},

		getMainWindowWidth: function() {
			if (navigator.appName.indexOf("Microsoft")!=-1) {
				return document.body.offsetWidth;
			}
			return window.innerWidth;
		},

		getMainWindowHeight: function() {
			if (navigator.appName.indexOf("Microsoft")!=-1) {
				return document.body.offsetHeight;
			} 
			return window.innerHeight;
		},

		getInputValue: function(id) {
			var e = this.elementById(id);
			if (e.value == undefined) {
				return "";
			}
			return e.value;
		}

	} // beejjs.util
}

