var Cookie = {

	/**
	 * Sets a Cookie with the given name and value.
	 *
	 * name       Name of the cookie
	 * value      Value of the cookie
	 * [expires]  Expiration date of the cookie (default: end of current session)
	 * [path]     Path where the cookie is valid (default: path of calling document)
	 * [domain]   Domain where the cookie is valid
	 *              (default: domain of calling document)
	 * [secure]   Boolean value indicating if the cookie transmission requires a
	 *              secure transmission
	 */
	set: function(name, value, expires, path, domain, secure)
	{
		document.cookie= name + "=" + escape(value) +
			((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
	},

	/**
	 * Gets the value of the specified cookie.
	 *
	 * name  Name of the desired cookie.
	 *
	 * Returns a string containing value of specified cookie,
	 *   or null if cookie does not exist.
	 */
	get: function(name)
	{
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1)
		{
			begin = dc.indexOf(prefix);
			if (begin != 0) return null;
		}
		else
		{
			begin += 2;
		}
		var end = document.cookie.indexOf(";", begin);
		if (end == -1)
		{
			end = dc.length;
		}
		return unescape(dc.substring(begin + prefix.length, end));
	},
	
	/*
	 * Returns true if the browser accepts first party cookies.
	 *
	 * @param secure  a boolean value indicating if the test should be
	 * a for a secure cookie or not.
	 */
	accepts: function(secure) {
		Cookie.set("test", "on", null, "/", null, secure);
		return Cookie.get("test");
	}

}

var Utils = {

	/*
	 * Returns the value of the supplied key in the supplied string. Handy to use when
	 * looking for a parameter value in a URL. This function returns false if the key
	 * could not be found.
	 *
	 * str	The string to parse (i.e. window.location.href)
	 * key	The target string to look for
	 */
	getParam: function(str, key) {
		return strings.getValue(str, key + "=", "&");
	},

	/*
	 * Returns a portion of the str that is sandwiched between the before and after
	 * substrings. This function returns false if the key could not be found.
	 *
	 * str		The string to parse
	 * before	The substring delimiting the left-hand side of the target string
	 * after	The substring delimiting the right-hand side of the target string
	 */
	getValue: function(str, before, after) {
		var value = false;
		var start = str.indexOf(before);
		var end;
		if (start > -1) {
			start = start + before.length;
			end = str.indexOf(after, start) > -1 ? str.indexOf(after, start) : str.length;
			value = str.substring(start, end);
		}
		return value;
	}

}

var Agent = {

	/**
	 * The following functions return true for the appropriate browser
	 */

	isOpera: function() {
		return window.opera;
	},

	isSafari: function() {
		return !window.opera && navigator.userAgent.indexOf('Safari') != -1;
	}

}

var Widget = {

	/*
	 * Use this "widget" to hide the submit button of a form that only has
	 * select element for the user to interact with and 1 submit button.
	 *
	 * @param formEl  a reference to the form element
	 * @param eventElName  the name of the element to make an event listener
	 * @param event  the event type (i.e. "click")
	 */
	hideSubmit: function(formEl, eventElName, event) {
		var formElements = new Form.getElements(formEl);
		formElements.each(function(el) {
			if (el.name == eventElName) {
			Event.observe(el, event, Widget.submit, false);
			} else if (el.type.toLowerCase() == "submit") {
			Element.hide(el);
			}
		});
	},

	submit: function(e) {
		Event.findElement(e, "form").submit();
	}

}
