﻿var __mcells = [];
var isOp = (window.opera);
var isIE = (document.all && !isOp);
function __isArray(value) {
	return value != null && Array.prototype.isPrototypeOf(value);
}
function isIn(value, list) {
	var i, args = [];
	if (typeof(list) != 'undefined' && isNaN(list.length) || arguments.length > 2 || typeof(list) != "string") {
		for (i = 1; i < arguments.length; i++)
			args.push(arguments[i]);
	}
	else
		args = list;
	for (i = 0; i < args.length; i++)
		if (value == args[i]) return true;
	return false;
}
function $() {
	// <summary>This function is a short-hand for document.getElementById call</summary>
	//	when one parameter is passed in - behaves in exact same way,
	//	when a number of input parameters (ids) - returns the array of corresponding objects
	var result = [], args = (arguments.length > 1 || !__isArray(arguments[0])) ? arguments : arguments[0];
	for (var i = 0, len = args.length; i < len; i++) {
		var e = args[i];
		if (typeof(e) == "string")
			e = document.getElementById(e);
		if (len == 1) return e;
		result.push(e);
	}
	return result;
}
function _getLeft(node){
	return (!node) ? 0 : node.offsetLeft + _getLeft(node.offsetParent);
}
function _getTop(node){
	return (!node) ? 0 : node.offsetTop + _getTop(node.offsetParent);
}
function menuStaticOver(cell) {
	cell.className = "mnSel";
	_displayDynamic(cell.aIndex);
}
function menuStaticOut(ev, cell) {
	cell.className = (cell.hasLinks) ? "mn" : "mnSel";
	_hideDynamic(ev, cell.aIndex);
}
function _initMenu(cells) {
	for (var i = 0, els = document.getElementsByTagName("UL"); i < cells.length; i++) {
		var cell = cells[i], j, links = cell.getElementsByTagName("A");
		cell.aIndex = i;
		cell.outs = [];
		cell.hasLinks = links.length != 0;
		for (j = 0; j < links.length; j++)
			links[j].onmouseover = new Function("_displayDynamic(" + i + ");");
		for (j = 0, id = cell.id + "_1"; j < els.length; j++)
			if (els[j].id == id) {
				var ul = cell.ul = els[j];
				_initDynamic(ul, i);
				break;
			}
	}
	__mcells = cells;
}
function _initDynamic(ul, i) {
	ul.opValue = 0;
	ul.onmouseout = function() {
		var ev = arguments[0] || event;
		_hideDynamic(ev, i);
	}
	for (var j = 0, lis = ul.childNodes; j < lis.length; j++)
		lis[j].onmouseover = new Function("_display(" + i + ", " + 100 + ");_clearTimeout(" + i + ");");
}
function _clearTimeout(i) {
	var cell = __mcells[i], outs = (cell) ? cell.outs : null;
	if (!outs) return;
	for (var j = outs.length - 1; j > -1; j--) {
		window.clearTimeout(outs[j]);
	}
	outs.length = 0;
	return outs;
}
function _hideDynamic(ev, i) {
	var cell = __mcells[i], ul = cell.ul, to = (isIE) ? ev.toElement : ev.relatedTarget;
	if (!ul || ul.style.display == "none" || !isIE && ev.currentTarget == ev.relatedTarget) return;
	if (to && (to == cell || to.parentNode == cell)) {
		_displayDynamic(i);
		return;
	}
	var outs = _clearTimeout(i), opValue = ul.opValue, k = 0;
	for (var j = opValue - 1; j > -1; j -= 5)
		outs[outs.length] = window.setTimeout("_display(" + i + ", " + j + ");", ++k * 5);
	if (k != 0)
		outs[outs.length] = window.setTimeout("__mcells[" + i + "].ul.style.display = \"none\"; _clearTimeout(" + i + ");", ++k * 5);
	else if (!to || !isIn(cell, to, to.parentNode))
		outs[outs.length] = window.setTimeout("__mcells[" + i + "].ul.style.display = \"none\"; _clearTimeout(" + i + ");", 5);
}
function _displayDynamic(i) {
	var cell = __mcells[i], ul = (cell) ? cell.ul : null;
	if (!ul) return;
	var opValue = ul.opValue, k = 0, st = ul.style;
	if (st.display != "none") return;

	st.left = _getLeft(cell) + "px";
	st.top = _getTop(cell) + cell.offsetHeight - 11 + "px";
	st.minWidth = cell.offsetWidth - 20 + "px";
	st.display = "";
	var outs = _clearTimeout(i);
	for (var j = opValue + 1; j < 101; j += 5)
		outs[outs.length] = window.setTimeout("_display(" + i + ", " + j + ");", ++k * 5);
	if (k != 0) outs[outs.length] = window.setTimeout("_clearTimeout(" + i + ");", ++k * 5);
}
function _display(i, op) {
	var ctl = __mcells[i].ul;
	ctl.opValue = op;
	ctl.style.opacity = op / 100;
	ctl.style.filter = "alpha(opacity=" + op + ")";
}
function validator_ControlOnChange(ctl) {
	//	this method is required to invoke control validation from javascript - this way the neccessary properties are set and Microsoft validation scripts do not fail
	if (!ctl || typeof(ctl.onchange) != "function") return;
	var body = ctl.onchange.toString();
	var insert = "var ev = (typeof(event)!='undefined' && event && event.type=='change') ? event : _getChangeEvent($('" + ctl.id + "'));\r\nValidatorOnChange(ev);";
	var val = "ValidatorOnChange(event);", valIndex = body.indexOf(val);
	if (body.indexOf(insert) == -1 && valIndex != -1) {
		var newBody = body.substr(0, valIndex - 1) + insert + body.substr(valIndex + val.length);
		setEventFunction(ctl, "onchange", _getFunctionBody(newBody));
	}
	ctl.onchange();
}
function _getFunctionBody(func) {
	var body = (typeof(func) == "string") ? func : func.toString();
	return body.substring(body.indexOf("{") + 1, body.lastIndexOf("}") - 1);
}
function setEventFunction(node, type, content) {
	var func = (isIE) ? new Function(content) : new Function("event", content);
	eval("node." + type + " = func;");
}
function _getChangeEvent(ctl) {
	var ev = new Object();
	ev.srcElement = ctl;
	ev.type = "change";
	return ev;
}
function resetInputFile(id) {
	var fl = $(id);
	if (!fl) return;
	var i, flNew = document.createElement("INPUT"), prn = fl.parentNode, nxt = fl.nextSibling;
	for (i = 0, atts = fl.attributes; i < atts.length; i++) {
		var att = atts[i];
		if (!att.isId && att.value != null && !isIn(att.value, "null", "inherit", ""))
			if (att.name=='class')
				flNew.className = att.value;
			else
				flNew.setAttribute(att.name, att.value);
	}
	for (var prp in fl)
		if (prp.substr(0, 2) == "on" && typeof(fl[prp]) == "function")
			setEventFunction(flNew, prp, _getFunctionBody(fl[prp]));
	prn.removeChild(fl);
	var vals = (typeof(Page_Validators) != "undefined") ? Page_Validators : null;
	if (__isArray(vals)) {
		flNew.Validators = new Array();
		for (i = 0; i < vals.length; i++) {
			var val = vals[i];
			var isSng = isIn(id, val.controltovalidate, val.controlhookup);
			var isMlt = typeof (val.controlstovalidate) == 'string' && val.controlstovalidate.indexOf(id) != -1;
			if (isSng || isMlt) {
				flNew.Validators[flNew.Validators.length] = val;
				val.isvalid = true;
			}
		}
	}
	prn.insertBefore(flNew, nxt);
	flNew.disabled = false; flNew.readOnly = false;
	validator_ControlOnChange(flNew);
}
function get_Attribute(node, name) {
	return (!isOp) ? node.getAttribute(name) : (node.attributes[name]) ? node.attributes[name].value : null;
}
