/*
$Id: zesk.js,v 1.15 2007/05/25 16:45:04 sgraham Exp $

Copyright (C) 2007 Market Acumen, Inc. All rights reserved
*/
var isByID		= (document.getElementById)						? true : false;
var isAll		= (document.all)								? true : false;

var isIE4		= (isAll && !isByID)							? true : false;
var isIE5		= (isAll && isByID)								? true : false;
var isIE6		= (navigator.userAgent.indexOf('MSIE 6') > 0) 	? true : false;
var isFF		= (navigator.userAgent.indexOf('FireFox') > 0)	? true : false;
var isIE		= (isIE4 || isIE5 || isIE6)						? true : false;

var isIECSS1	= (isAll && (isIE5 || isIE6) && document.compatMode && document.compatMode=='CSS1Compat') ?
true : false;

var isNS4	= (document.layers)								? true : false;
/* isNS4=(navigator.appName=='Netscape' && parseInt(navigator.appVersion)==4); ? */
var isNS6	= (!isIE && !isAll && isByID)					? true : false;
var isNS	= (isNS4 || isNS6)								? true : false;
if (isNS4) {
	var nsWinX=window.innerWidth;
	var nsWinY=window.innerHeight;
}

is4		= (isIE4 || isNS4)								? true : false;

var isOpera		= (navigator.userAgent.indexOf('Opera') > 0) 	? true : false;

var _ud = "undefined";
var _udi = -9381232;	// Undefined Integer (preserves type)

/*
Browser-specific
*/
function Frame_GetX(f)
{
	if (!f || f == _ud)
	return 0;
	if (isIECSS1) {
		return f.document.documentElement.scrollLeft;
	} else if (isAll) {
		return f.document.body.scrollLeft;
	}
	if (typeof f.pageXOffset == _ud) {
		return f.pageXOffset;
	}
	return 0;
}

function Frame_GetY(f)
{
	if (!f || f == _ud)
	return 0;
	if (isIECSS1) {
		return f.document.documentElement.scrollTop;
	} else if (isAll) {
		return f.document.body.scrollTop;
	}
	if (typeof f.pageYOffset == _ud) {
		return f.pageYOffset;
	}
	return 0;
}

function Frame_GetWidth(f)
{
	if (!f || f == _ud)
	return 0;
	if (isIECSS1) {
		return f.clientWidth;
	} else if (isAll) {
		return f.document.body.clientWidth;
	} else if (isNS4 || isByID) {
		return f.clientWidth;
	}
	return false;
}

function Window_Width()
{
	if (isIE) {
		return document.documentElement.clientWidth;
	}
	return window.innerWidth;
}

function Document_Body(d)
{
	var tags = d.getElementsByTagName('body');
	if (tags.length == 0) return false;
	return tags[0];
}

function Window_Height()
{
	if (isIE) {
		return document.documentElement.clientHeight;
	}
	return window.innerHeight;
}

function Frame_GetHeight(f)
{
	if (!f || f == _ud)
	return false;
	if (isIECSS1) {
		return f.clientWidth;
	} else if (isAll) {
		return f.document.body.clientHeight;
	} else if (isNS4 || isByID) {
		return f.clientHeight;
	}
	return false;
}

/*
f		Frame
layer	layerID
text	layer text
*/
function Frame_WriteLayer(f, id, text)
{
	var x = FrameObject_Get(f, id);
	if (!x) {
		return false;
	}
	text +="\n";
	if (isNS4) {
		x = x.document;
		if (typeof x == _ud) {
			alert('x.document is ' + _ud);
			return false;
		}
		x.write(text);
		x.close();
	} else if (isAll) {
		x.innerHTML	= text;
	} else if (isByID) {
		var r = f.document.createRange();
		r.setStartBefore(x);
		frag = r.createContextualFragment(text);
		while(x.hasChildNodes()){
			x.removeChild(x.lastChild);
		}
		x.appendChild(frag);
	}
	return true;
}

function Event_GetX(e, f)
{
	if (isNS)
	return e.pageX;
	if (isIE5)
	return event.x + Frame_GetX(f);
	return event.x;
}

function Event_GetY(e, f)
{
	if (isNS)
	return e.pageY;
	if (isIE5)
	return event.y + Frame_GetY(f);
	return event.y;
}

function Object_GetStyle(id)
{
	var x = null;
	if (isNS4) {
		x = document.filters.document.layers[id];
		if (x && x != _ud)
		return x;
	} else if (isIE4)
	x = document.all[id];
	else if (isByID)
	x = document.getElementById(id);
	if (x && x != _ud)
	return x.style;
	return null;
}

function Object_Get(id)
{
	var x = null;
	if (isNS4)
	x = document.filters.document.layers[id];
	else if (isIE4)
	x = document.all[id];
	else if (isByID)
	x = document.getElementById(id);
	if (x && x != _ud)
	return x;
	return null;
}

function ObjectID_GetWidth(id)
{
	var x = Object_Get(id);
	return Object_GetWidth(x);
}

function ObjectID_GetHeight(id)
{
	var x = Object_Get(id);
	return Object_GetHeight(x);
}

function Object_GetHeight(x)
{
	if (x) {
		if (isIECSS1) {
			return x.offsetHeight;
		} else {
			return x.clientHeight;
		}
	}
	return false;
}

function Object_GetWidth(x)
{
	if (typeof x != "object") return false;
	if (isIECSS1) {
		return x.offsetWidth;
	} else {
		return x.clientWidth;
	}
}


function ObjectIDs_Width(x)
{
	var n = 0;
	for (var i = 0; i < x.length; i++) {
		n = n + ObjectID_GetWidth(x[i]);
	}
	return n;
}

function ObjectIDs_Height(x)
{
	var nn, n = 0;
	for (var i = 0; i < x.length; i++) {
		nn = ObjectID_GetHeight(x[i]);
		n = n + nn;
	}
	return n;
}


function Object_SetContents(obj, y)
{
	obj.innerHTML = y;
	return true;
}

function ObjectID_SetContents(id, y)
{
	var obj = Object_Get(id);
	if (!obj) {
		return false;
	}
	return Object_SetContents(obj, y);
}

function ObjectID_Prepend(id, y)
{
	var obj = Object_Get(id);
	if (!obj) {
		return false;
	}
	obj.outerHTML = y + obj.outerHTML;
}

function Object_Contents(obj)
{
	return obj.innerHTML;
}

function ObjectID_Contents(id)
{
	var obj = Object_Get(id);
	if (obj)
	return obj.innerHTML;
	return '';
}

function Object_Exists(id)
{
	return Object_Get(id) ? true : false;
}

function NS_OnResize()
{
	if (nsWinX != window.innerWidth || nsWinY != window.innerHeight)
	location.reload();
}

function Document_OnMouseMove(func)
{
	if (typeof func == _ud)
	return false;
	if (isNS4) {
		var nsWinX=window.innerWidth;
		var nsWinY=window.innerHeight;
		window.onresize = NS_OnResize;
	}
	if ((isNS4)||(isAll)||(isByID)){
		document.onmousemove=func;
		if (isNS4) {
			document.captureEvents(Event.MOUSEMOVE);
		}
		return true;
	}
	return false;
}

function FrameObject_Get(f,id)
{
	var x = null;
	if (isNS4) {
		x = f.document.filters.document.layers[id];
		if (x && x != _ud)
		return x;
	} else if (isIE4)
	x = f.document.all[id];
	else if (isByID)
	x = f.document.getElementById(id);
	if (x && x != _ud)
	return x;
	return null;
}

function FrameObject_GetStyle(f,id)
{
	var x = null;
	if (isNS4) {
		x = f.document.filters.document.layers[id];
		if (x && x != _ud)
		return x;
	} else if (isIE4)
	x = f.document.all[id];
	else if (isByID)
	x = f.document.getElementById(id);
	if (x && x != _ud)
	return x.style;
	return null;
}

function ObjectID_DisplayToggle(id)
{
	if (ObjectID_IsDisplay(id)) {
		ObjectID_DisplayHide(id);
		return false;
	} else {
		ObjectID_DisplayShow(id);
		return true;
	}
}

function ObjectID_ToggleLink(a,id0,id1,showText,hideText)
{
	var result = ObjectID_DisplayToggle(id0);
	ObjectID_DisplayToggle(id1);
	a.innerHTML = result ? showText : hideText;
}

function ObjectID_DisplayShow(id)
{
	var x = Object_GetStyle(id);
	if (x) {
		x._save_display = x.display;
		x.display = 'block';
	}
}

function ObjectID_DisplayShowOne(id,hideids)
{
	hideids = hideids.split(";");
	for (var i = 0; i < hideids.length; i++) {
		if (hideids[i].length != 0) {
			ObjectID_DisplayHide(hideids[i]);
		}
	}
	ObjectID_DisplayShow(id);
}

function ObjectID_DisplayHide(id)
{
	var x = Object_GetStyle(id);
	if (x) {
		var save_display = 'none';
		if (typeof x._save_display != _ud)
		save_display = x._save_display;
		x.display = save_display;
	}
}

function ObjectID_Display(id, value)
{
	if (value) {
		ObjectID_DisplayShow(id);
	} else {
		ObjectID_DisplayHide(id);
	}
}

function Object_IsDisplay(x)
{
	if (!x) {
		alert("Object_IsDisplay(!x)");
		return false;
	}
	return (x.display == 'none' || x.display == '') ? false : true;
}


function ObjectID_IsDisplay(id)
{
	return Object_IsDisplay(Object_GetStyle(id));
}

function ObjectID_VisibleToggle(id)
{
	return Object_VisibleToggle(Object_GetStyle(id));
}

function ObjectID_VisibleHide(id)
{
	return Object_VisibleHide(Object_GetStyle(id));
}

function ObjectID_VisibleShow(id)
{
	return Object_VisibleShow(Object_GetStyle(id));
}

function ObjectID_IsVisible(id)
{
	return Object_IsVisible(Object_GetStyle(id));
}

function ObjectID_MoveTo(id, x, y)
{
	return Object_MoveTo(Object_GetStyle(id),x,y);
}

function Object_MoveTo(obj, x, y)
{
	if (!obj || obj == _ud)
	return;
	var sfx='px';
	if (is4) {
		if (!isAll)
		sfx='';
	} else if (!isByID) {
		return;
	}
	obj.left		= x+sfx;
	obj.top			= y+sfx;
}

function Object_Left(obj)
{
	if (!obj || obj == _ud)
	return false;
	var n = 0;
	while (obj) {
		n += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return n;
}

function Object_Top(obj)
{
	if (!obj || obj == _ud)
	return false;
	var n = 0;
	while (obj) {
		n += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return n;
}

function Object_VisibleShow(x)
{
	if (!x) {
		alert("Object_VisibleShow: no x");
		return;
	}
	if (isNS4)
	x.visibility = "show";
	else if (isAll || isByID)
	x.visibility = "visible";
}

function Object_VisibleHide(x)
{
	if (!x)
	return;
	if (isNS4)
	x.visibility = "hide";
	else if (isAll || isByID)
	x.visibility = "hidden";
}

function Object_VisibleToggle(x)
{
	if (!x)
	return;
	if (Object_IsVisible(x))
	Object_VisibleHide(x);
	else
	Object_VisibleShow(x);
}

function Object_IsVisible(x)
{
	if (!x) {
		alert("Object_IsVisible: no x");
		return false;
	}
	if (isNS4)
	return (x.visibility == "hide") ? false : true;
	else if (isIE4 || isNS6)
	return (x.visibility == "hidden") ? false : true;
	return true;
}

function Object_SetBackground(x)
{
	if ((x == "") || (x == _ud) || (x == null))
	{
		if (isNS4) {
			x.background.src			= null;
		} else if (isIE4 || isNS6) {
			x.backgroundImage			= "none";
		}
	} else {
		if (isNS4) {
			x.background.src			= x;
		} else if (isIE4 || isNS6) {
			x.backgroundImage			= "url("+x+")";
		}
	}
}

/*
HTML Tools
*/
function _HTML_Tag(name, args, single)
{
	var a = args;
	var n, v;
	var r = new Array();
	r[r.length] = name;
	for (var i = 1; i < a.length; i += 2) {
		n = a[i];
		v = a[i+1];
		if (v != 0 && (v == '' || v == _udi || v == _ud || typeof v == _ud))
		continue;
		r[r.length] = n + '="' + v + '"';
	}
	return '<' + r.join(" ") + (single ? '/' : '') + '>';
}

function HTML_SingleTag(name)
{
	return _HTML_Tag(name, arguments, true);
}

function HTML_Tag(name)
{
	return _HTML_Tag(name, arguments, false);
}

function HTML_EndTag(name)
{
	return '</' + name + '>';
}

function IE_DHTML_Hack(form, hide)
{
	if (!isIE || isOpera)
	return;
	var i;
	var verb = (hide) ? "hidden" : "visible";
	for (i = 0; i < form.elements.length; i++) {
		if (form.elements[i].type.indexOf('select') == 0 && form.elements[i].name != "") {
			form.elements[i].style.visibility = verb;
		}
	}
}

function Document_Scan(n, expr)
{
	var c, v, x, offset, result = new Array();
	for (var i=0;i<n.childNodes.length;i++) {
		c = n.childNodes[i];
		if (c.nodeType == 3) {
			v 		= c.nodeValue;
		} else if (c.nodeType == 1 && c.nodeName.toLowerCase() == "input") {
			v 		= c.value;
		} else {
			v		= null;
		}
		if (v != null) {
			offset	= v.search(expr);
			if (offset >= 0) {
				x = new Object();
				x.offset	= offset;
				x.match 	= v.match(expr);
				x.node 		= c;
				result.push(x);
			}
		}
		result = result.concat(Document_Scan(c,expr));
	}
	return result;
}

function RandomString() {
	return (Math.random()+'').substring(2,10);
}

function noop()
{
}

function Cookie_Find(name, def)
{
	var c = document.cookie;
	var s = c.indexOf(name + '=');
	if (s < 0) {
		return def;
	}
	s += name.length + 1;
	var e = c.indexOf(';', s);
	if (e < 0) { e = c.length; }
	return unescape(c.substring(s,e));
}

function Cookie_Set(name, value)
{
	var d = new Date(2020, 1, 1, 0, 0, 0);
	document.cookie = name + "=" + escape(value) + '; path=/; expires=' + d.toGMTString();
}

var gStyleSheetTitle = Cookie_Find('style', 'normal');

function Server_Message(uri)
{
	var i = new Image();
	var r = RandomString();
	var p = uri.lastIndexOf('?');
	if (p < 0) {
		uri += '?';
	} else {
		uri += '&';
	}
	i.src = uri + "r=" + r;
}


function Style_SetActive(title/*, saveIt */)
{
	var i, a, main;
	var saveIt = (arguments.length > 1) ? arguments[1] : true;
	for(i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			if (a.getAttribute("title") == title) {
				a.disabled = false;
			}
		}
	}
	if (saveIt) {
		gStyleSheetTitle = title;
		setCookie('style', title);
	}
}

function Style_Refresh()
{
	var src;
	var i = document.images['style_change'];
	if (i) {
		src = 'style_'+gStyleSheetTitle+'.gif';
		i.src = '/images/' + src;
	}
	Style_SetActive(gStyleSheetTitle, true);
}

function Style_Toggle()
{
	var a = arguments;
	var i;
	var c;

	if (a.length == 0) {
		a[0] = 'normal';
		a[1] = 'big';
	}
	c = 0;
	for (i = 0; i < a.length; i++) {
		if (a[i] == gStyleSheetTitle) {
			c = (i + 1) % a.length;
			break;
		}
	}
	gStyleSheetTitle = a[c];
	Style_Refresh();
}

function ObjectID_DisplayToggleState(f, id, fname)
{
	f[fname].value = ObjectID_DisplayToggle(id) ? "block" : "none";
	return false;
}


function AppendSection(to, contents)
{
	ObjectID_SetContents(to, ObjectID_Contents(to) + contents);
	return true;
}

function String_Catenate(delimiter /*, string1, string2 */)
{
	var i;
	var s;

	s = "";
	for (i = 1; i < arguments.length; i++) {
		if (arguments[i] != "") {
			if (s != "")
			s += delimiter;
			s += arguments[i];
		}
	}
	return s;
}

function URL_Valid(u, schemes)
{
	u = u.trim();
	if (u == "")
	return true;
	var s = schemes.toLowerCase().split(";");
	var n = s.length;
	var x = u.indexOf('://');
	if (x < 0) {
		alert('Please specify a URL containing "://", such as http://cr.to.');
		return false;
	}
	var scheme = u.substr(0,x).toLowerCase();
	for (var i = 0; i < n ; i++) {
		if (s[i] == scheme)
		return true;
	}
	alert('Please specify a URL that begins with ' + s.join(' or '))
	return false;
}

function Email_Clean(e)
{
	var r = /[^\@A-Z0-9a-z_\.\-\']+/g;		// '
	e = e.replace(r, "");
	return e;
}

function Email_Valid(e)
{
	e = Email_Clean(e);
	var r = /^[^@]+@[a-z0-9][a-z0-9\.\-]+\.[a-z]{2,4}$/i;
	if (e.match(r))
	return true;
	return false;
}

function strltrim() {
	return this.replace(/^\s+/,'');
}

function strrtrim() {
	return this.replace(/\s+$/,'');
}
function strtrim() {
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
function htmlspecialchars(str)
{
	var re1, re2, re3, re4;
	re1 = /&/g; re2 = /</g; re3 = />/g; re4 = /"/g; //"
	return str.replace(re1, "&amp;").replace(re2, "&lt;").replace(re3, "&gt;").replace(re4, "&quot;");
}

String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;

function Window_NormalizeNameFromURI(uri)
{
	var e = /[^A-Z0-9_]/gi;
	var r = uri.replace(e, "_");
	if (r == "")
	r = 'default';
	return r;
}

function Window_FormName(w)
{
	if ((typeof w.form.name != "undefined") && w.form.name != "")
	return w.form.name;
	return w.form.id;
}

function Window_Open(theLink, width, height)
{
	var wparam = "resizable=yes,toolbar=no,menubar=no,scrollbars=yes,width=" + width + ",height=" + height;
	var name = Window_NormalizeNameFromURI(theLink);
	var newwin = window.open(theLink, name, wparam);
	if (newwin)
	newwin.focus();
	return newwin;
}

function Window_PopupRef(theLink, width, height, ref)
{
	var wparam = "resizable=yes,toolbar=no,menubar=no,scrollbars=yes,width=" + width + ",height=" + height;
	var c = (theLink.indexOf('?') < 0) ? '?' : '&';
	var name = Window_NormalizeNameFromURI(theLink);
	var newwin = window.open('/popup' + theLink + c + ref, name, wparam);
	if (newwin)
	newwin.focus();
	return newwin;
}

function Window_Popup(theLink, width, height)
{
	return Window_PopupRef(theLink, width, height, 'Ref=*close');
}

function Window_PopupPost(widget, theLink, width, height)
{
	return Window_PopupRef(theLink, width, height, 'Ref=*close:' + Window_FormName(widget));
}

function Window_PopupPostSet(widget, fieldName, theLink, width, height)
{
	return Window_PopupRef(theLink, width, height, 'Ref=*close:' + Window_FormName(widget) + ':' + fieldName);
}

function Window_PopupRefresh(theLink, width, height)
{
	return Window_PopupRef(theLink, width, height, 'Ref=*refresh');
}

var gFormsPending = 0;
var gConfirmTime = new Date();
var gFormState = new Object();
var gLastAnswer = false;

function Control_Value(control)
{
	var def = arguments[1] || "";
	var value;
	if (!control)
	return def;
	if (control.type.substr(0,6) == "select") {
		return control.options[control.selectedIndex].value;
	} else if (control.type == "checkbox") {
		return control.checked;
	} else {
		value = control.value;
	}
	if ((value == "") || (value == null)) return def;
	return value;
}

function Control_HasValue(control)
{
	var v = Control_Value(control, false);
	if ((v == "") || (v == null))
	return false;
	return true;
}

function Control_SelectText(control, def)
{
	if (!control)
	return def;
	if (control.type.substr(0,6) == "select") {
		return control.options[control.selectedIndex].text;
	}
	return def;
}

function Control_SetValue(control, value)
{
	if (!control) {
		alert("Control_SetValue(!control," + value + ")");
		return false;
	}
	if (control.type == "select") {
		var i;
		for (i = 0; i < control.options.length; i++) {
			if (control.options[i].value == value) {
				control.selectedIndex = i;
				return true;
			}
		}
		alert("Control_SetValue(" + form + ","+ name +"," + value + "): unable to find select value");
		return false;
	} else if (control.type == "checkbox") {
		control.checked = (value) ? true : false;
		return true;
	} else {
		if (control.value != value) {
			control.value = value;
		}
		return true;
	}
}

/*
This don't work, unless we alert between SetValue and submit
*/
function Control_SetValuePost(control, value)
{
	Control_SetValue(control, value);
	control.form.submit();
	return true;
}

function Control_Checked(control)
{
	if (control.type == "checkbox") {
		return control.checked;
	}
	//	alert("Control_Checked: control.type=" + control.type);
	return false;
}

function Form_SetChecked(form, name, value)
{
	var control;
	control = form.elements[name];
	if (!control) {
		//		alert("Form_SetChecked: no form element \"" + name + "\".");
	} else if (control.type == 'checkbox') {
		form.elements[name].checked = value;
	} else {
		//		alert("Form_SetChecked: control.type=" + control.type);
	}
}

function Form_GetControl(form, name /*, func*/)
{
	var control;
	var func = "unspecified";
	if (arguments.length > 2)	{ func = arguments[2]; }
	control = form.elements[name];
	if (!control) {
		//		alert(func + "(" + form + ","+ name +"," + value + "): undefined");
		return false;
	}
	return control;
}
function Form_SetBackground(form, name, color)
{
	var control = Form_GetControl(form, name, "Form_SetEnabled");
	if (control) {
		control.style.backgroundColor = color;
	}
}

function Form_SetEnabled(form, name, value)
{
	var control = Form_GetControl(form, name, "Form_SetEnabled");
	if (control) {
		control.disabled = (value) ? false : true;
	}
}

function Form_GetEnabled(form, name)
{
	var control = Form_GetControl(form, name, "Form_SetEnabled");
	if (control) {
		return !control.disabled;
	}
	return false;
}

function Form_Value(form, name, def)
{
	var control;
	control = form.elements[name];
	if (!control || typeof control == "undefined") {
		return def;
	}
	return Control_Value(control, def);
}

function Form_SetValue(form, name, value)
{
	var control = form.elements[name];
	if (!control) {
		//		alert("Form_SetValue(" + form + ","+ name +"," + value + "): undefined");
		return false;
	}
	if (!Control_SetValue(control, value)) {
		//		alert("Form_SetValue(" + form + ","+ name +"," + value + "): failed");
		return false;
	}
	return true;
}

function Form_SetEnabledLike(form, mixed, enabled)
{
	var e = form.elements;
	var i;
	if (typeof mixed == "object") {
		for (i = 0; i < mixed.length; i++) {
			Form_SetEnabledLike(form, mixed[i], enabled);
		}
	} else {
		for (i = 0; i < e.length; i++) {
			if (e[i].name.indexOf(mixed) >= 0) {
				e[i].disabled = enabled ? false : true;
			}
		}
	}
}

function Control_ClearFocus(w, value)
{
	if (w.value == value)
	w.value = '';
}

function Control_ResetFocus(w, value)
{
	if (w.value == '')
	w.value = value;
}

function clink(w)
{
	if (gFormsPending == 0)
	return true;
	var fname = false;
	if (typeof w == 'object') {
		fname = w.name;
	}
	var n, message='';
	gFormsPending = 0;

	for (n in gFormState) {
		if (gFormState[n].changed) {
			if (n != fname) {
				message += gFormState[n].message + "\n";
			}
			gFormsPending++;
		}
	}
	if (message != "") {
		var newTime = new Date();
		var delta = newTime.getTime() - gConfirmTime.getTime();
		//		alert(newTime.getTime() + " - " + gConfirmTime.getTime() + " = " + delta);
		if (delta < 2000)
		return gLastAnswer;
		gConfirmTime = new Date();
		gLastAnswer = confirm(message);
		return gLastAnswer;
	}
	return true;
}

function clinkhref(x)
{
	if (gFormsPending == 0)
	return true;
	if (clink(null)) {
		document.location = x;
		return true;
	}
	return false;
}

function Form_Register(form, message)
{
	gFormState[form.name] = new Object();
	gFormState[form.name].message = message;
	gFormState[form.name].changed = 0;
}

function Form_Cancel(form)
{
	form.reset();
	gFormState[form.name].changed = 0;
}

function Form_CancelAll()
{
	for (i in gFormState) {
		Form_Cancel(document.forms[i]);
	}
}

function ichanged(w)
{
	if (typeof gFormState[w.form.name] != "undefined") {
		gFormState[w.form.name].changed = 1;
		++gFormsPending;
	}
}

var gCallback = new Object();

function callback(o, func)
{
	o.to = setTimeout(func + "(" + o.n + ")", o.nMS);
}

function textarea_resizer(fname, ename, xpad, ypad)
{
	var e = document.forms[fname].elements[ename];

	this.xpad	= xpad;
	this.ypad	= ypad;

	this.ww		= Window_Width();
	this.wh		= Window_Height();

	this.e		= e;
	this.dx		= - Object_GetWidth(e);
	this.dy		= - Object_GetHeight(e);
	this.n		= gCallback.length;
	this.nMS	= 10;

	e.cols += 10;
	e.rows += 10;

	callback(this, "ta_test");

	gCallback[this.n] = this;
}

function ta_test(n)
{
	if (typeof Window_Height == "undefined") {
		callback(this, "ta_test");
	} else {
		var o = gCallback[n];
		var e = o.e;

		o.dx = (o.dx + Object_GetWidth(e)) / 10.0;
		o.dy = (o.dy + Object_GetHeight(e)) / 10.0;

		gCallback[n] = o;

		callback(this, "ta_resize");
	}
}

function ta_resize(n)
{
	var o = gCallback[n];
	var e = o.e;

	if (typeof e.cols == "undefined")
	{
		o.nMS = 1000;
		callback(this, "ta_resize");
		return;
	}

	var
	ww = Window_Width(),
	wh = Window_Height();

	e.cols = Math.max(parseInt((ww - o.xpad) / o.dx),10);
	e.rows = Math.max(parseInt((wh - o.ypad) / o.dy),10);

	o.nMS = 10;
	callback(this, "ta_resize");
}

var ajax_finish = new Object();

function noop() { }

function toggle(id)
{
	new Effect[Element.visible(id) ? 'BlindUp' : 'BlindDown'](id, {duration: 0.25});
}

function ajax_edit(id, uri)
{
	if (Element.empty(id)) {
		new Ajax.Updater(id, uri, {
			method:     'get',
			onFailure:  function() {Element.classNames(id).add('error')},
			onComplete: function() {new Effect.BlindDown(id, {duration: 0.25})}
		});
	} else {
		toggle(id);
	}
	if (arguments.length >= 3) {
		if (typeof arguments[2] == "function") {
			ajax_finish[id] = arguments[2];
		}
	}
}

function ajax_form_handle_result(id)
{
	var content = $(id).innerHTML;
	if (content.indexOf("*success*") > 0) {
		ajax_form_finish(id, '');
	}
}

function form_disable(form_id)
{
	var form;
	if (typeof form_id == "object") {
		form = form_id;
	} else {
		form = document.forms[form_id];
	}
	for (i = 0; i < form.elements.length; i++) {
		form.elements[i].disabled = true;
	}
}

function ajax_form_serialize(form)
{
	var elements = Form.getElements($(form));
	var queryComponents = new Array();
	for (var i = 0; i < elements.length; i++) {
		if (elements[i].type == 'button') continue;
		var queryComponent = Form.Element.serialize(elements[i]);
		if (queryComponent) queryComponents.push(queryComponent);
	}
	return queryComponents.join('&');
}

function ajax_form_submit(widget, id, uri/*, finish_fuction*/)
{
	var qs = ajax_form_serialize(widget.form) + "&OK=1&embed=1&ajax_id=" + id;
	form_disable(widget.form);
	if (typeof arguments[3] == "function") {
		ajax_finish[id] = arguments[3];
	}
	new Ajax.Updater(id, uri, {
		method:		'post',
		postBody:	qs,
		onFailure:	function()	{alert("A problem occurred");},
		onComplete:	function()	{ajax_form_handle_result(id);}
	});
}

function ajax_form_continue(form,id)
{
	var form_name = form;
	form = $(form);
	var uri = form.action;
	if (form_name == "ajax_main_form") {
		form.action = form.action + ((form.action.indexOf("?") < 0) ? "?" : "&") + "_continue=1";
		form.submit();
		return true;
	} else {
		var qs = ajax_form_serialize(form) + "&embed=2&_continue=1&ajax_id="+id;
		form_disable(form);
		new Ajax.Updater(id, uri, {
			method:		'post',
			postBody:	qs,
			onFailure:	function()	{alert("A problem occurred");},
			onComplete:	function()	{}
		});
	}
}

function ajax_form_cancel(id)
{
	var delay = arguments.length > 1 ? arguments[1] : 0;
	new Effect.Fade(id, {duration: 1, delay: delay, afterFinish: function() { $(id).innerHTML = ""; }});
	ajax_finish[id] = false;
}

function ajax_form_finish(id, message)
{
	if (message.length > 0) {
		ajax_set_content(id, '<div class="message">' + message + '</div>');
	}
	var finish_function = ajax_finish[id];
	ajax_finish[id] = null;
	ajax_form_cancel(id, 3);
	if (typeof finish_function == "function") {
		finish_function();
	}
}

function ajax_set_content(id, content)
{
	id = $(id);
	id.innerHTML = content;
}

/*********** Lists *******************/

/*
requires
{javascript:/templates/control/string.js} for String_Catenate
*/
function ListControl_CountChecked(form, prefix, value)
{
	var i 		= form.elements.length;
	var n		= 0;
	var name	= prefix + "[]";
	var nlen	= name.length;
	if (Form_Value(form, prefix + "CheckAll", false))
	return -1;
	while (i-- != 0) {
		if ((form.elements[i].type == "checkbox") &&
		(form.elements[i].name.substring(0,nlen) == name) &&
		(form.elements[i].checked == value)) {
			++n;
		}
	}
	return n;
}

function ListControl_SetChecked(form, prefix, value)
{
	var i		= form.elements.length;
	var nlen	= prefix.length;
	while (i-- != 0) {
		if (form.elements[i].name.substring(0,nlen) == prefix)
		form.elements[i].checked = value;
	}
}

function ListControl_CheckPage(control, prefix)
{
	var form	= control.form;
	var value	= Control_Value(control, false);

	ListControl_SetChecked(form, prefix + "[]", value);
	Form_SetChecked(form, prefix + "CheckAll", false);
}

function ListControl_CheckItem(control, prefix)
{
	var form	= control.form;
	var value	= Control_Value(control, false);
	if (value == false) {
		Form_SetChecked(form, prefix + "CheckAll", false);
	} else if (ListControl_CountChecked(form, prefix, false) == 0) {
		Form_SetChecked(form, prefix + "CheckAll", true);
	}
}

function ListControl_CheckAll(control, prefix)
{
	var form	= control.form;
	var value	= Control_Value(control, false);

	if (value || (ListControl_CountChecked(form, prefix, false) == 0)) {
		ListControl_SetChecked(form, prefix + "[]", value);
	}
}

function ListControl_Action(control, name, promptPrefix, promptSuffix, confirmPrefix, confirmSuffix)
{
	var form = control.form;

	var id = Control_Value(control, false);
	if (id == false)
	return false;
	if (id == "new") {
		var promptString = String_Catenate(" ", promptPrefix, name, promptSuffix);
		nn = window.prompt(promptString);
		if (nn == null || nn == "null" || nn == "") {
			return false;
		} else {
			control.selectedIndex = 0;
			Form_SetValue(form, control.name + "New", nn);
			form.submit();
			return true;
		}
	} else {
		var message = String_Catenate(" ", confirmPrefix, name, confirmSuffix);
		if (confirm(message)) {
			form.submit();
			return true;
		}
	}
	return false;
}

function ListControl_ConfirmAction(button, prefix /*, object, extraMessage */)
{
	var form = button.form;
	var message;
	var object = "";
	var extraMessage = "";
	var checkedCount;
	var argn = 2;
	var actionName = prefix + 'Action';
	var selControl = Form_GetControl(form, actionName);
	var verb = Control_SelectText(selControl, actionName, Control_Value(selControl)).toLowerCase();

	if (arguments.length > argn) {
		object = arguments[argn];
	}
	++argn;
	if (arguments.length > argn) {
		extraMessage = arguments[argn];
	}
	++argn;

	if (object == "") {
		object = "item";
	}

	checkedCount	= ListControl_CountChecked(form, prefix, true);

	if (checkedCount == 0) {
		message =
		String_Catenate(
		" ",
		"You must select at least one",
		object,
		"to " + verb + "."
		);
		alert(message);
		return false;
	}
	if (checkedCount < 0) {
		message =
		String_Catenate(
		" ",
		"Are you sure you want to " + verb + " all",
		Locale_Plural(object),
		"?"
		);
	} else {
		message =
		String_Catenate(
		" ",
		"Are you sure you want to " + verb,
		Locale_This(object, checkedCount),
		"?"
		);
	}
	if (typeof extraMessage == 'string' && extraMessage.length != 0) {
		message += "\n" + extraMessage;
	}
	if (confirm(message)) {
		form.submit();
		return true;
	} else {
		return false;
	}
}

function ie_hack()
{
	if (!isIE) { return; }
	//	$('body').hide();
	//	$('body').show();
	//	setTimeout("$('body').show();",5);
	//	$('body').forceRerendering();
}

function toggle_edit(id)
{
	new Effect[Element.visible(id + "_edit") ? "SlideUp" : "SlideDown"](id + "_edit",{duration:0.5});
}

function slide_toggle(id)
{
	new Effect[Element.visible(id) ? "SlideUp" : "SlideDown"](id,{duration:0.5});
}


function html_control_append(id, content)
{
	var new_div = document.createElement('div');
	new_div.innerHTML = content;
	$(id).parentNode.appendChild(new_div);
}

function arrow_down_right(id)
{
	var is_vis = Element.visible(id);
	if (false) {
		if (is_vis) { $(id).hide(); } else { $(id).show(); }
		$(id + "_img").src = (is_vis) ? "/share/images/toggle/small-right.gif" : "/share/images/toggle/small-down.gif";
	} else {
		var args = arguments[1] || {duration: 0.25};
		if (is_vis) {
			args.afterFinish = function () { $(id + "_img").src = "/share/images/toggle/small-right.gif"; }
		} else {
			args.afterFinish = function () { $(id + "_img").src = "/share/images/toggle/small-down.gif"; }
		}
		new Effect[is_vis ? 'BlindUp' : 'BlindDown'](id, args);
	}
}

function arrow_down_right_load(id, url)
{
	if ($(id).innerHTML == "") {
		new Ajax.Updater(id, url, { onComplete: arrow_down_right(id) });
	} else {
		arrow_down_right(id);
	}
}

function list_dhtml_extract(w,id)
{
	var f = w.form;
	var find = '['+id+']';
	var qs = '';
	for (var i = 0; i < f.elements.length; i++) {
		var e = f.elements[i];
		if (e.name.substring(e.name.length-find.length) == find) {
			qs += (qs ? '&' : '') + e.name.substring(0,e.name.length-find.length) + '=' + escape(Control_Value(e));
		}
	}
	return qs;
}

function list_dhtml_save(w, id)
{
	w.value = "Saving...";
	w.disabled = true;
	new Ajax.Updater('list-message-'+id,'edit.php?ID='+id+'&'+list_dhtml_extract(w,id)+'&ajax_list=1',{
		onComplete: function () {
			w.disabled = false;
			w.value = "Save";
			new Effect.Fade('list-message-'+id,{delay:2,duration:1});
		}
	});
}


function list_dhtml_delete(w, id)
{
	if (!confirm('Are you sure you want to delete?')) return false;
	w.value = "Deleting ...";
	w.disabled = true;
	new Ajax.Updater('list-message-'+id,'edit.php?ID='+id+'&'+list_dhtml_extract(w,id)+'&action=delete&ajax_list=1',{
		onComplete: function () {
			w.disabled = true;
			w.value = "Deleted!";
			new Effect.Fade('list-row-'+id,{delay:0.2,duration:1});
		}
	});
}

function QS_get(n)
{
	var d = arguments.length > 1 ? arguments[1] : null;
	var q = location.search;
	if (q.substring(0,1) == "?") q = q.substring(1);
	q = q.split("&");
	var p;
	n = n.toLowerCase();
	for (var i = 0; i < q.length; i++) {
		p = q[i].split("=",2);
		if (p.length == 2 && p[0].toLowerCase() == n)
		return unescape(p[1]);
	}
	return d;
}

function QueryParameter(name/*, default */)
{
	var s = document.location.search;
	var def = (arguments.length > 1) ? arguments[1] : false;
	if (s.substr(0,1) == '?') {
		s = s.substr(1);
	}
	var x = s.split('&'), i, nv;
	for (i=0;i<x.length;i++) {
		nv = x[i].split('=');
		if (nv[0].toLowerCase() == name) {
			if (nv.length == 1) {
				return true;
			}
			return unescape(nv[1]);
		}
	}
	return def;
}

function hide_id(id)
{
	Element.hide(id);
}

//
// <input type="text" ... onfocus="text_disappear(this, 'Enter your text here')" />
//
function text_disappear(w,matching_text) 
{
	if(w.value==matching_text)
	{
		w.value = "";
	}
}

function text_reappear(w,matching_text)
{
	if(w.value=="")
	{
		w.value = matching_text;
	}
}

function closebox_fade(area,indelay,induration)
{
	return new Effect.SlideUp(area,{delay:indelay,duration:induration});
}