top._StrixForm = {
	message: Array(
		'It seems that there are unsaved changes on this page.',
		'If you navigate away, changes will be discarded.'
	).join("\n"),
	forms: [],

	init: function() {
		for (var i = 0; i < this.forms.length; i++) {
			if (!this.forms[i].id) continue;
			var f = self.document.getElementById(this.forms[i].id) || null;
			if (f) f._stateChanged = this.forms[i].state ? true : false;
			this.forms[i].state = null;
		}
	},

	changed: function() {
		var is_changed = false;
		for (var i = 0; !is_changed && i < this.forms.length; i++) {
			if (!this.forms[i].id) continue;
			var f = self.document.getElementById(this.forms[i].id) || null;
			if (!f) continue;
			if (f._stateChanged) is_changed = true;
		}
		return is_changed ? true : false;
	}
};

try {
	YAHOO.util.Event.addListener(self, 'load', function(ev) {
		top._StrixForm.init();
		return true;
	});
	YAHOO.util.Event.addListener(self, 'beforeunload', function(ev) {
		if (!ev) {
			ev = self.event;
		}
		if (top._StrixForm.changed()) {
			ev.returnValue = top._StrixForm.changed() ? top._StrixForm.message : null;
			return ev.returnValue;
		}
	});
}
catch (ex) { }


function StrixValueWatch(params) {
	this.id = params.id;
	this.obj = null;
	this.prop = params.prop || 'value';
	if (params.onchange) {
		params.onchange = String(params.onchange);
		if (!params.onchange.match(/^[\s\r\n]+function\s*\(/i)) {
			params.onchange = 'function(){' + params.onchange + '};';
		}
		try {
			eval('this.onchange = ' + params.onchange);
			if (!this.onchange) this.onchange = eval(params.onchange);
		}
		catch (ex) { this.onchange = null; }
	}
	//this.obj._old_onchange = null;
	this.init = function() {
		this.obj._oldValue = this.obj[this.prop];
		this._enabled = true;
		this.obj._onvaluechange = function() {
			var val = this[this._strixform_valuewatch_wrapper.prop];
			if (val === this._oldValue) return null;
			this._oldValue = val;
			if (this.form) this.form._stateChanged = true;
			if (this._strixform_valuewatch_wrapper._enabled) {
				try {
					if (this._strixform_valuewatch_wrapper.onchange) {
						this._strixform_valuewatch_wrapper.onchange.apply(this);
					}
				}
				catch (ex) { }
				try {
					if (this._old_onchange) {
						this._old_onchange();
					}
				}
				catch (ex) { }
			}
			return this._oldValue;	// which is actually NEW value now...
		};
		this.obj._setValue = function(v) {
			this._strixform_valuewatch_wrapper._enabled = false;
			this[this._strixform_valuewatch_wrapper.prop] = v;
			var _t = this;
			setTimeout(function() { _t._strixform_valuewatch_wrapper._enabled = true; }, 10);
		}
		try {
			this.obj.watch(this.prop, function(prop, oldval, newval) {
				var _t = this;
				setTimeout(function() {
					_t._onvaluechange();
				}, 1);
				return newval;
			});
		}
		catch (ex) {
			try {
				var _t = this;
				setInterval(function() {
					_t.obj._onvaluechange();
				}, 200);
			}
			catch (ex) { }
		}
		this.obj._old_onchange = this.obj.onchange || null;
		this.obj.onchange = function() {
			this._onvaluechange();
			return true;
		};
	};

	var _this = this;
	YAHOO.util.Event.addListener(self, 'load', function(ev) {
		var e = self.document.getElementById(_this.id) || null;
		if (e) {
			e._strixform_valuewatch_wrapper = _this;
			_this.obj = e;
			_this.init();
		}
	});

	return this;
}


// original idea from http://www.hedgerwow.com/360/dhtml/ui_select_with_fixed_width/demo.php
top.StrixWidthLimiter_retry = [];
top.StrixWidthLimiter_init_retry = function() {
	if (!top.StrixWidthLimiter_retry) return;
	if (top.StrixWidthLimiter_retry.length <= 0) return;
	for (var i = 0; i < top.StrixWidthLimiter_retry.length; i++) {
		if (!top.StrixWidthLimiter_retry[i]) {
			continue;
		}
		if (top.StrixWidthLimiter_retry[i].init()) {
			top.StrixWidthLimiter_retry[i] = null;
		}
	}
};

function StrixWidthLimiter(params) {
	this.id = params.id || '';
	this.maxWidth = params.maxWidth || '';
	this.animate = params.animate || false;
	this.no_extend = params.no_extend || false;
	this.obj = null;
	this.isIE = (self.document.uniqueID && self.createPopup) ? true : false;
	this.isIE7 = (this.isIE && String(self.navigator.userAgent || '').match(/MSIE\s+7/i)) ? true : false;
	this.widthMin = null;
	this.widthMax = null;
	this.widthCurr = null;
	this.timerId = null;
	this.animStep = params.animStep || 2;
	this.animTime = params.animTime || 10;
	this._old_zIndex = null;
	//this.currStyle = { ro: null, rw: null };

	this._addListener = function(o, e, f) {
		if (o.addEventListener) {
			o.addEventListener(e, f, true);
		} else if (o.attachEvent) {
			o.attachEvent('on' + e, f);
		} else {
			o['on' + String(e)] = function(evt) {
				f.apply(o, [ evt ]);
			}
		}
	};

	this.getStyle = function(rw) {
/*
		if (this.currStyle && this.currStyle[rw ? 'rw' : 'ro']) {
			return this.currStyle[rw ? 'rw' : 'ro'];
		}
*/
		var candidates = [
			this.obj.runtimeStyle,
			(!rw && self.getComputedStyle) ? self.getComputedStyle(this.obj, null) : null,
			this.obj.currentStyle,
			this.obj.style
		];
		for (var i = 0; i < candidates.length; i++) {
			if (candidates[i]) {
				//this.currStyle[rw ? 'rw' : 'ro'] = candidates[i];
				return candidates[i];
			}
		}
		return null;
	};

	this.getWidth = function() {
		return parseInt(this.obj.offsetWidth);
	};

	this.getWidths = function() {
/*
		var vis;
		if (this.isIE) {
			vis = this.getStyle().visibility;
			this.getStyle(true).visibility = 'hidden';
		}
*/
		var wid = this.getStyle().width;
		if (this.maxWidth) {
			try { this.getStyle(true).width = this.maxWidth; }
			catch (ex) { }
		}
		var w1 = this.getWidth();
		this.getStyle(true).width = 'auto';
		var w2 = this.getWidth();
		this.getStyle(true).width = wid || '';
/*
		if (this.isIE) {
			this.getStyle(true).visibility = vis;
		}
*/
		return [ w1, w2 ];
	};

	this.init = function() {
		this.obj = self.document.getElementById(this.id) || null;
		if (!this.obj) return false;
		var w = this.getWidths();
//alert([ this.id, this.obj.name, w[0], w[1], this.getStyle().display ].join(' :: '));
		if (w[0] <= 0 && w[1] <= 0) {
			// probably a non-visible element
			return false;	// FIXME
		}
		if (w[0] >= w[1]) return true;	// we have enough space, no need for limiting
		this.widthMin = w[0];
		this.widthMax = w[1];
		if (this.maxWidth) {
			try { this.getStyle(true).width = this.maxWidth; }
			catch (ex) { }
		}
		if (!this.no_extend) {
			this.ieFix();
		}
		return true;
	};

	this.widthExtend = function(isNext, isFinal) {
		if (top._strix_autocomplete_shown) {
			return;
		}
		if (this.animate && !isFinal) {
			if (this._old_zIndex == null) {
				this._old_zIndex = this.div.runtimeStyle.zIndex;
				if (!top._strix_autocomplete_shown) {
					top._strix_autocomplete_shown = 0;
				}
				this.zIndexChanged = false;
				if (top._strix_autocomplete_shown <= 0) {
					this.div.runtimeStyle.zIndex = '1000';
					this.zIndexChanged = true;
				}
			}
			if (this.timerId) {
				clearTimeout(this.timerId);
			}
			this.timerId = null;
			if (isNext) {
				this.widthCurr += Math.max(1, Math.round((this.widthMax - this.widthCurr) / this.animStep));
				if (this.widthCurr >= this.widthMax) {
					this.widthExtend(null, true);
				} else {
					this.getStyle(true).width = String(this.widthCurr) + 'px';
					var _t = this;
					this.timerId = setTimeout(function() {
						_t.widthExtend(isNext);
					}, this.animTime);
				}
			} else {
				this.widthCurr = this.getWidth();
				this.widthExtend(true);
			}
		} else {
			this.getStyle(true).width = 'auto';
		}
	};

	this.widthContract = function(isNext, isFinal) {
		if (this.animate && !isFinal) {
			if (this.timerId) clearTimeout(this.timerId);
			this.timerId = null;
			if (isNext) {
				this.widthCurr -= Math.max(Math.round((this.widthCurr - this.widthMin) / this.animStep));
				if (this.widthCurr <= this.widthMin) {
					this.widthContract(null, true);
				} else {
					this.getStyle(true).width = String(this.widthCurr) + 'px';
					var _t = this;
					this.timerId = setTimeout(function() {
						_t.widthContract(isNext);
					}, this.animTime);
				}
			} else {
				this.widthCurr = this.getWidth();
				this.widthContract(true);
			}
		} else {
			this.getStyle(true).width = this.maxWidth || '';
			if (this._old_zIndex != null) {
				this.div.runtimeStyle.zIndex = this._old_zIndex;
				this._old_zIndex = null;
			}
		}
	};

	this.ieFix = function() {
		if (!this.isIE) return;	// not M$IE, no need for ugly tricks
		var div = self.document.createElement('SPAN');
		var obj = this.obj.cloneNode(true);
		obj.selectedIndex = Math.max(0, this.obj.selectedIndex);
		this.obj.parentNode.insertBefore(div, this.obj);
		this.obj.parentNode.removeChild(this.obj);
		obj.id = this.obj.id;
		this.obj = div.obj = obj;
		obj._strix_width_limiter = this;
		div.appendChild(obj);
		div.runtimeStyle.zIndex = this.isIE7 ? '0' : '-1';
		div.runtimeStyle.display = 'block';
		div.runtimeStyle.position = 'relative';
		obj.runtimeStyle.position = 'absolute';
		obj.runtimeStyle.left = '0px';
		obj.runtimeStyle.top = '0px';

		div.runtimeStyle.width = String(this.obj.offsetWidth) + 'px';
		div.runtimeStyle.height = String(this.obj.offsetHeight) + 'px';
		this.div = div;

		// the SPAN might have changed something, so we better re-read
		var w = this.getWidths();
		this.widthMin = w[0];
		this.widthMax = w[1];

		var _t = this;
		this._addListener(self.document, 'mousemove', function(ev) {
			var src = ev.target || ev.srcElement || null;
			if (!src) return;
			//if (src !== self.document) return;
			var mx = (ev.pageX || ev.clientX) + (self.document.body.scrollLeft || self.document.documentElement.scrollLeft);
			var my = (ev.pageY || ev.clientY) + (self.document.body.scrollTop || self.document.documentElement.scrollTop);
			if (isNaN(mx) || isNaN(my)) return;
			var xy = YAHOO.util.Dom.getXY(_t.obj);
			var cx1 = xy[0];
			var cy1 = xy[1];
			var cx2 = cx1 + _t.obj.offsetWidth;
			var cy2 = cy1 + _t.obj.offsetHeight;
			if (isNaN(cx1) || isNaN(cx2) || isNaN(cy1) || isNaN(cy2)) return;
			var inside = true;
			if (inside && mx < cx1) inside = false;
			if (inside && mx > cx2) inside = false;
			if (inside && my < cy1) inside = false;
			if (inside && my > cy2) inside = false;
			if (inside) {
				if (!_t.obj._mouseover) {
					_t.obj.onmouseover();
				}
			} else {
				if (_t.obj._mouseover) {
					_t.obj.onmouseout();
				}
			}
			return true;
		});
		obj.onmouseover = function(ev) {
			this._mouseover = true;
			this._strix_width_limiter.widthExtend();
		};
		obj.onmouseout = function(ev) {
			this._mouseover = false;
			if (!this._focused) this._strix_width_limiter.widthContract();
		};
		obj.onfocus = function(ev) {
			this._focused = true;
			this.onmouseover(ev);
		};
		obj.onblur = function(ev) {
			this._focused = false;
			this.onmouseout(ev);
		};
		this._addListener(obj, 'change', function() {
			if (obj.blur) {
				setTimeout(function() { obj.blur(); }, 10);
			}
		});
	};

	if (!this.init()) {
		var _t = this;
/*
		YAHOO.util.Event.addListener(self, 'load', function(ev) {
			_t.init();
		});
*/
		this._addListener(self, 'load', function(ev) {
			if (!_t.init()) {
				top.StrixWidthLimiter_retry[top.StrixWidthLimiter_retry.length] = _t;
			}
		});
	}

	return this;
}


top._addClass = function(obj, classNames) {
	if (!obj) return;
	var classes = String(obj.className || '').split(/\s+/);
	for (var i = 0; i < classNames.length; i++) {
		var cname1 = String(classNames[i] || '').toLowerCase();
		if (!cname1) continue;
		var fnd = false;
		for (var j = 0; !fnd && j < classes.length; j++) {
			var cname2 = String(classes[j] || '').toLowerCase();
			if (!cname2) continue;
			if (cname1 == cname2) fnd = true;
		}
		if (!fnd) classes[classes.length] = classNames[i];
	}
	obj.className = classes.join(' ');
};

top._removeClass = function(obj, classNames) {
	if (!obj) return;
	var classes = String(obj.className || '').split(/\s+/);
	var newClasses = [];
	for (var i = 0; i < classes.length; i++) {
		var cname1 = String(classes[i] || '').toLowerCase();
		if (!cname1) continue;
		var fnd = false;
		for (var j = 0; !fnd && j < classNames.length; j++) {
			var cname2 = String(classNames[j] || '').toLowerCase();
			if (!cname2) continue;
			if (cname1 == cname2) fnd = true;
		}
		if (!fnd) newClasses[newClasses.length] = cname1;
	}
	obj.className = newClasses.join(' ');
};

//Object.prototype.addClass = function(classNames) {
//	return top._addClass(this, classNames);
//};
//
//Object.prototype.removeClass = function(classNames) {
//	return top._removeClass(this, classNames);
//};
