function initFormElements()  {
	for (iform = 0; iform < document.forms.length; iform++)  {
		if (document.forms[iform].name  &&  document.forms[iform].name.substr(0,4) == "Form")  {
			var formelms = document.forms[iform].getElementsByTagName('INPUT');
			for (i = 0; i < formelms.length; i++)
				if (formelms[i].type != "hidden") {
					formelms[i].onfocus=elmOnFocus;
					formelms[i].onblur=elmOnBlur;
					if (formelms[i].type == "checkbox"  &&  formelms[i].checked) {
						if (formelms[i].onclick)
							formelms[i].onclick();
					}
				}
			formelms = document.forms[iform].getElementsByTagName('TEXTAREA');
			for (i = 0; i < formelms.length; i++)  {
				formelms[i].onfocus=elmOnFocus;
				formelms[i].onblur=elmOnBlur;		
				}
			}
		}
	}
	
function initIframes()  {
	var iframes = document.getElementsByTagName('IFRAME');
	for (i = 0; i < iframes.length; i++)  {
		ifr = iframes[i];
		css = ifr.getAttribute('css');
		if (css != null)  {
			if (ifr.contentDocument)
				doc = ifr.contentDocument;
			else
				doc = ifr.contentWindow.document;
			var links = doc.getElementsByTagName('LINK');
			if (links.length > 0)
				links[0].href = css;
			}
		}
		
		window.onresize = window.onload;
  	}
  	
function winOnResize()  {

}

function initScrollPos()  {
	if (document.forms.length < 1)
		return;
	y = document.forms[0].scrollPos.value;
	if (y > 0)
		window.scrollTo(0, y);	
	}

function docOnEvent(e)  {
	if (document.forms.length < 1)
		return;
		
	if (typeof idleTime == 'number')
		startIdleTimer();
		
	if (!e) var e = window.event;			
	if (e.target) trg = e.target;
	else if (e.srcElement) trg = e.srcElement; 
	if (trg.nodeType == 3) trg = trg.parentNode;
	
	switch (e.type)  {
		case "mousedown":
			var rClick;
			if (e.button) rClick = (e.button == 2);
			else if (e.which) rClick = (e.which == 3);
			if (rClick)
				return cancelDefault(e);
			break;
			
		case "keydown":
			if (trg.tagName=="A" || trg.type=="image" || trg.type=="submit")
				break;
			if (e.keyCode) code = e.keyCode;
			else if (e.which) code = e.which;

			document.forms[0].keyCode.value = code;
			
			if (code == 13  || !e.preventDefault  &&  (code == 27  || code >= 112  &&  code <= 123)) {
				if (trg.type == "textarea"  &&  code == 13  &&  trg.allowReturn) 
					break;
				return doSubmit(e, trg);
				}
			if (code == 75)  {
				if (e.modifiers  &&  (e.modifiers & Event.CONTROL_MASK)  ||  e.ctrlKey)  {
					openDialog("/aDISWeb/app?service=page/aDIS_Keyboard", trg);
					return cancelDefault(e);	
					}
				}
			if (e.ctrlKey == true  &&  (code == 48  ||  code == 96))  {
				num2char(trg);
				return cancelDefault(e);
			 	}
			 	
			break;
		
		case "keypress":
			if (trg.tagName=="A")
				break;
			if (e.keyCode) code = e.keyCode;
			else if (e.which) code = e.which;

			if (e.preventDefault  &&  e.which == 0  &&  (code == 27  ||  code >= 112  &&  code <= 123)) {
				if (trg.type == "textarea"  &&  code == 13  &&  trg.allowReturn) 
					break;
				return doSubmit(e, trg);
				}
			break;
		}
	}

function elmOnFocus(e)  {
	if (!e) var e = window.event;
	if (e.target) trg = e.target;
	else if (e.srcElement) trg = e.srcElement; 
	if (trg.nodeType == 3) trg = trg.parentNode;

	document.forms[0].focus.value = trg.getAttribute('fld');
	document.forms[0].focusform = trg.form;
	}

function elmOnBlur(e)  {
	if (!e) var e = window.event;
	if (e.target) trg = e.target;
	else if (e.srcElement) trg = e.srcElement; 
	if (trg.nodeType == 3) trg = trg.parentNode;

	document.forms[0].focus.value = "";
	document.forms[0].focusform = null;
	}

function formOnSubmit()  {
	document.forms[0].scriptEnabled.value=true;
	
	if (window.pageYOffset != undefined)
		document.forms[0].scrollPos.value = window.pageYOffset;
	else if (document.documentElement.clientWidth)
		document.forms[0].scrollPos.value = document.documentElement.scrollTop;
	else if (document.body.scrollTop != undefined)
		document.forms[0].scrollPos.value = document.body.scrollTop;
		
	return true;
	}
	
function doSubmit(e, trg)  {
	while (trg.nodeName != "FORM"  &&  trg.nodeName != "HTML")
		trg = trg.parentNode;
	if (trg == null  ||  trg.nodeName == "HTML")
		focusform = document.forms[0];
	else if (trg.name.substr(0,4) == "Form")
		focusform = trg;
    else
		return;	
	
	if (!e.preventDefault)
		e.keyCode = 0;
	cancelDefault(e);
	formOnSubmit();
	
	focusform.submit();
	return false;
	}
	
function cancelDefault(e)  {
	if (e.preventDefault)  {
		e.preventDefault();	
		e.stopPropagation();
		}
	e.cancelBubble = true;
	e.returnValue = false;
	return false;
	}

function cancelF1()  {
	window.event.cancelBubble = true;
	window.event.returnValue = false;
	}
	
function checkNumeric(e, obj)  {
	var code = e.keyCode ? e.keyCode : e.which;
	
        if(code > 47 && code < 58)
                return true;
        else if (code < 32  ||  (!e.shiftKey && (code == 44 || code == 46)))
		return true;
	else if (code == 45  &&  obj.value.length == 0)
		return true;
        else if(e.ctrlKey || (code > 36 && code < 41))
               return true;

	return cancelDefault(e);
	}	
	
function openDialog(url, dialogInfo)  {
	if (window.showModalDialog)  {
		var options = "dialogWidth:680px;dialogHeight:400px;resizable:yes;status:no"; 
		window.showModalDialog(url, dialogInfo, options);
		}
	else  {
		var options = "location=no,menubar=no,toolbar=no,status=no,dependent=yes," +
		             "dialog=yes,minimizable=no,modal=yes,alwaysRaised=yes," +
		             "resizable=yes,width=680,height=400,top=100,left=200";
		var win = window.open('', "keyboard", options);
		win.location.href = url;
		win.dialogArguments = dialogInfo;
		window.parentArguments = dialogInfo;
		win.focus();
		}
	}
	
function NumInfo(str, len)  {
	this.nstr = str;
	this.leng = len;
	this.numb;
	this.evaluate = evaluate;
	}

function evaluate()  {
	var eval = false;
	var len = 0;
	var chr;
	var start;
	var isNum = 1;
	var numstr;
		
	start = this.leng - 1;
	if (start > 0)  {
		do  {
			chr = this.nstr.charAt(start);
			if ((chr < "0")  ||  (chr > "9"))  {
				rlen = 0;
				isNum = 0;
				}
			else  {
				--start;
				++len;
				} 
			}
		while(isNum == 1 && start>=0 && len < 6);
		}
	if (len > 0)  {
		numstr = this.nstr.substr(start + 1, len);
		num = parseInt(numstr);
		if (isFinite(num))  {
			this.leng = len;
			this.nstr = String.fromCharCode(num);
			this.numb = num;
			eval = true;
			}
		else
			this.num = 0;
		}
	return eval;
}

function num2char(element)  {
	var numinf;
	var pos;
	var eval;
	var	nlen = 6;
	element.focus();
	
	/* for Gecko browsers*/
	if (typeof element.selectionStart != 'undefined')  {
		var end = element.selectionEnd;
		var scrollTop = element.scrollTop;	
		if (end > 0)  {
			if (end < nlen)
				nlen = end;
			str = element.value.substr(end-nlen, nlen);
			numinf = new NumInfo(str, nlen);
			eval = numinf.evaluate();
			if (eval)  {
				start = end - numinf.leng;
				element.value = element.value.substring(0, start) +
								numinf.nstr +
								element.value.substring(end, element.value.length);
				pos = start + numinf.nstr.length;
				element.selectionStart = pos;
				element.selectionEnd = pos;
				element.scrollTop = scrollTop;
				} 
			}
		}
	/* for IE*/
	else if (typeof document.selection != 'undefined')  {
		var range = document.selection.createRange();
		var srange = range.duplicate();
		srange.moveStart('character', (0 - nlen));
		numinf = new NumInfo(srange.text, srange.text.length);
		eval = numinf.evaluate();
		if (eval)  {
			range.moveStart('character', (0-numinf.leng));
			range.text = numinf.nstr;
			}
		range = document.selection.createRange();
		range.select();
		}
	/* for other browsers */
	else
		alert("Numerische Unicode-Eingabe ist nicht definiert!");
	}
/***/
var MENU_POS = [
{
	'height': 1.4,
	'width': 10,
	'block_top': -.05,
	'block_left': 0,
	'top': 0,
	'left': 7,
	'hide_delay': 200,
	'expd_delay': 100,
	'css' : {
		'outer': ['menu_l0oout', 'menu_l0oover', 'menu_l0oout'],
		'inner': ['menu_l0iout', 'menu_l0iover', 'menu_l0idis']
	}
},
{
	'height': 1.3,
	'width': 15,
	'block_top': 1.35,
	'block_left': 0.4,
	'top': 1.3,
	'left': 0,
	'css': {
		'outer' : ['menu_l1oout', 'menu_l1oover', 'menu_l1oout'],
		'inner' : ['menu_l1iout', 'menu_l1iover', 'menu_l1idis']
	}
},
{
	'block_top': 0.7,
	'block_left': 11,
	'css': {
		'outer': ['menu_l1oout', 'menu_l1oover', 'menu_l1oout'],
		'inner': ['menu_l1iout', 'menu_l1iover', 'menu_l1idis']
	}
}
]

// --------------------------------------------------------------------------------
// global collection containing all menus on current page
var A_MENUS = [];

// --------------------------------------------------------------------------------
// menu class
function menu (a_items, a_tpl) {

	// browser check
	if (!document.body || !document.body.style)
		return;

	// store items structure
	this.a_config = a_items;

	// store template structure
	this.a_tpl = a_tpl;

	// get menu id
	this.n_id = A_MENUS.length;

	// declare collections
	this.a_index = [];
	this.a_children = [];

	// assigh methods and event handlers
	this.expand      = menu_expand;
	this.collapse    = menu_collapse;

	this.onclick     = menu_onclick;
	this.onmouseout  = menu_onmouseout;
	this.onmouseover = menu_onmouseover;
	this.onmousedown = menu_onmousedown;

	// default level scope description structure 
	this.a_tpl_def = {
		'block_top'  : 1,
		'block_left' : 1,
		'top'        : 20,
		'left'       : 4,
		'width'      : 15,
		'height'     : 1.3,
		'hide_delay' : 0,
		'expd_delay' : 0,
		'css'        : {
			'inner' : '',
			'outer' : ''
		}
	};
	
	// assign methods and properties required to imulate parent item
	this.getprop = function (s_key) {
		return this.a_tpl_def[s_key];
	};

	this.o_root = this;
	this.n_depth = -1;
	this.n_x = 0;
	this.n_y = 0;

	// 	init items recursively
	for (n_order = 0; n_order < a_items.length; n_order++)
		new menu_item(this, n_order);

	// register self in global collection
	A_MENUS[this.n_id] = this;

	// make root level visible
	for (var n_order = 0; n_order < this.a_children.length; n_order++)
		this.a_children[n_order].e_oelement.style.visibility = 'visible';
}

// --------------------------------------------------------------------------------
function menu_collapse (n_id) {
	// cancel item open delay
	clearTimeout(this.o_showtimer);

	// by default collapse to root level
	var n_tolevel = (n_id ? this.a_index[n_id].n_depth : 0);
	
	// hide all items over the level specified
	for (n_id = 0; n_id < this.a_index.length; n_id++) {
		var o_curritem = this.a_index[n_id];
		if (o_curritem.n_depth > n_tolevel && o_curritem.b_visible) {
			o_curritem.e_oelement.style.visibility = 'hidden';
			o_curritem.b_visible = false;
		}
	}

	// reset current item if mouse has gone out of items
	if (!n_id)
		this.o_current = null;
}

// --------------------------------------------------------------------------------
function menu_expand (n_id) {

	// expand only when mouse is over some menu item
	if (this.o_hidetimer)
		return;

	// lookup current item
	var o_item = this.a_index[n_id];

	// close previously opened items
	if (this.o_current && this.o_current.n_depth >= o_item.n_depth)
		this.collapse(o_item.n_id);
	this.o_current = o_item;

	// exit if there are no children to open
	if (!o_item.a_children)
		return;

	// show direct child items
	for (var n_order = 0; n_order < o_item.a_children.length; n_order++) {
		var o_curritem = o_item.a_children[n_order];
		o_curritem.e_oelement.style.visibility = 'visible';
		o_curritem.b_visible = true;
	}
}

// --------------------------------------------------------------------------------
//
// --------------------------------------------------------------------------------
function menu_onclick (n_id) {
	// don't go anywhere if item has no link defined
	return Boolean(this.a_index[n_id].a_config[1]);
}

// --------------------------------------------------------------------------------
function menu_onmouseout (n_id) {

	// lookup new item's object	
	var o_item = this.a_index[n_id];

	// apply rollout
	var state;
	if (o_item.dstate)
		state = 2;
	else
		state = 0;

	o_item.e_oelement.className = o_item.getstyle(0, state);
	o_item.e_ielement.className = o_item.getstyle(1, state);
	
	// update status line	
	o_item.upstatus(7);

	// run mouseover timer
	this.o_hidetimer = setTimeout('A_MENUS['+ this.n_id +'].collapse();',
		o_item.getprop('hide_delay'));
}

// --------------------------------------------------------------------------------
function menu_onmouseover (n_id) {

	// cancel mouseoute menu close and item open delay
	clearTimeout(this.o_hidetimer);
	this.o_hidetimer = null;
	clearTimeout(this.o_showtimer);

	// lookup new item's object	
	var o_item = this.a_index[n_id];

	// update status line	
	o_item.upstatus();

	// apply rollover
	if (o_item.dstate)
		state = 2;
	else
		state = 1;

	o_item.e_oelement.className = o_item.getstyle(0, state);
	o_item.e_ielement.className = o_item.getstyle(1, state);
	
	// if onclick open is set then no more actions required
	if (o_item.getprop('expd_delay') < 0)
		return;

	// run expand timer
	this.o_showtimer = setTimeout('A_MENUS['+ this.n_id +'].expand(' + n_id + ');',
		o_item.getprop('expd_delay'));

}

// --------------------------------------------------------------------------------
// called when mouse button is pressed on menu item
// --------------------------------------------------------------------------------
function menu_onmousedown (n_id) {
	
	// lookup new item's object	
	var o_item = this.a_index[n_id];

	// apply mouse down style
	o_item.e_oelement.className = o_item.getstyle(0, 2);
	o_item.e_ielement.className = o_item.getstyle(1, 2);

	this.expand(n_id);
//	this.items[id].switch_style('onmousedown');
}


// --------------------------------------------------------------------------------
// menu item Class
function menu_item (o_parent, n_order) {

	// store parameters passed to the constructor
	this.n_depth  = o_parent.n_depth + 1;
	this.a_config = o_parent.a_config[n_order + (this.n_depth ? 3 : 0)];

	// return if required parameters are missing
	if (!this.a_config) return;

	// store info from parent item
	this.o_root    = o_parent.o_root;
	this.o_parent  = o_parent;
	this.n_order   = n_order;

	// register in global and parent's collections
	this.n_id = this.o_root.a_index.length;
	this.o_root.a_index[this.n_id] = this;
	o_parent.a_children[n_order] = this;

	// calculate item's coordinates
	var o_root = this.o_root,
		a_tpl  = this.o_root.a_tpl;

	// assign methods
	this.getprop  = mitem_getprop;
	this.getstyle = mitem_getstyle;
	this.upstatus = mitem_upstatus;

	this.n_x = n_order
		? o_parent.a_children[n_order - 1].n_x + this.getprop('left')
		: o_parent.n_x + this.getprop('block_left');

	this.n_y = n_order
		? o_parent.a_children[n_order - 1].n_y + this.getprop('top')
		: o_parent.n_y + this.getprop('block_top');

	if (this.a_config[2] && this.a_config[2]['ds'])
		this.dstate = true;

	// generate item's HMTL
	document.write (
		'<a id="e' + o_root.n_id + '_'
			+ this.n_id +'o" class="' + this.getstyle(0, 0) + '" href="' + this.a_config[1] + '"'
			+ (this.a_config[2] && this.a_config[2]['tw'] ? ' target="'
			+ this.a_config[2]['tw'] + '"' : '') + ' style="position: absolute; top: '
			+ this.n_y + 'em; left: ' + this.n_x + 'em; width: '
			+ this.getprop('width') + 'em; height: '
			+ this.getprop('height') + 'em; visibility: hidden;'
			+' z-index: ' + this.n_depth + ';" '
			+ 'onclick="return A_MENUS[' + o_root.n_id + '].onclick('
			+ this.n_id + ');" onmouseout="A_MENUS[' + o_root.n_id + '].onmouseout('
			+ this.n_id + ');" onmouseover="A_MENUS[' + o_root.n_id + '].onmouseover('
			+ this.n_id + ');" onmousedown="A_MENUS[' + o_root.n_id + '].onmousedown('
			+ this.n_id + ');"><div  id="e' + o_root.n_id + '_'
			+ this.n_id +'i" class="' + (this.dstate ? this.getstyle(1, 2) : this.getstyle(1, 0)) + '">'
			+ this.a_config[0] + "</div></a>\n"
		);
	this.e_ielement = document.getElementById('e' + o_root.n_id + '_' + this.n_id + 'i');
	this.e_oelement = document.getElementById('e' + o_root.n_id + '_' + this.n_id + 'o');

	this.b_visible = !this.n_depth;

	// no more initialization if leaf
	if (this.a_config.length < 4)
		return;

	// node specific methods and properties
	this.a_children = [];

	// init downline recursively
	for (var n_order = 0; n_order < this.a_config.length - 3; n_order++)
		new menu_item(this, n_order);
}

// --------------------------------------------------------------------------------
// reads property from template file, inherits from parent level if not found
// ------------------------------------------------------------------------------------------
function mitem_getprop (s_key) {

	// check if value is defined for current level
	var s_value = null,
		a_level = this.o_root.a_tpl[this.n_depth];

	// return value if explicitly defined
	if (a_level)
		s_value = a_level[s_key];

	// request recursively from parent levels if not defined
	return (s_value == null ? this.o_parent.getprop(s_key) : s_value);
}
// --------------------------------------------------------------------------------
// reads property from template file, inherits from parent level if not found
// ------------------------------------------------------------------------------------------
function mitem_getstyle (n_pos, n_state) {

	var a_css = this.getprop('css');
	var a_oclass = a_css[n_pos ? 'inner' : 'outer'];

	// same class for all states	
	if (typeof(a_oclass) == 'string')
		return a_oclass;

	// inherit class from previous state if not explicitly defined
	for (var n_currst = n_state; n_currst >= 0; n_currst--)
		if (a_oclass[n_currst])
			return a_oclass[n_currst];
}

// ------------------------------------------------------------------------------------------
// updates status bar message of the browser
// ------------------------------------------------------------------------------------------
function mitem_upstatus (b_clear) {
	window.setTimeout("window.status=unescape('" + (b_clear
		? ''
		: (this.a_config[2] && this.a_config[2]['sb']
			? escape(this.a_config[2]['sb'])
			: '')
			) + "')", 1);
}

function menuOnSelect(stz)  {
	document.forms[0].source.value = "$M";
	document.forms[0].stz.value = stz;
	document.forms[0].submit();
	}
	
function menuOnConnect()  {
	alert("Verbindungsaufnahme nicht implementiert!");
	}
	
function menuOnPrint()  {
	alert("Druck nicht implementiert!");
	}
/***/
function initaDISCombo(cbedit, cblist)  {
	cbedit.style.width = cbedit.offsetWidth - 16;
	cbedit.style.marginRight = 16;

	body = document.getElementsByTagName("BODY")[0];
	elm = cbedit;
	x = 0;
	y = 0;
	while (elm != body  &&  elm.tagName.toUpperCase() != "DIV")  {
		x += elm.offsetLeft;
		y += elm.offsetTop;
		elm = elm.offsetParent;
	}
	
	cblist.style.left = x+2;
	cblist.style.top = y;
	cblist.style.width = cbedit.offsetWidth + 16;
	cblist.style.clip = "rect(0, "+(cbedit.offsetWidth+16+2)+", "+(cbedit.offsetHeight)+", "+(cbedit.offsetWidth-2)+")";
	updateSelection(cbedit, cblist)
	cblist.style.display="block";
}

function updateSelection(cbedit, cblist)  {
	cblist.selectedIndex = 0;
	for (var i = 0; i < cblist.options.length; i++)  {
		if (cbedit.value == cblist.options[i].value)  {
			cblist.selectedIndex = i;
			break;
		}
	}
}

function updateEdit(cbedit, cblist, autoSubmit)  {
	cbedit.value = cblist.options[cblist.selectedIndex].value;
	cbedit.focus();
	cbedit.select();
	if (autoSubmit)  {
		document.forms[0].keyCode.value = 13;
		document.forms[0].submit();
	}  
}
/***/
function htmlOnCheck (element, text) {
	selString = "ZTEXT       ";		
	selString += text;
	if (element.checked) {
		if (document.forms[0].selected.value && document.forms[0].selected.value != "") {
			var selections = new Array();
			selections = document.forms[0].selected.value.split(';');
			for (var i = 0; i < selections.length; i++) {
				if (selections[i] == selString)
					return;
				}
			document.forms[0].selected.value += ";";
			}
		document.forms[0].selected.value += selString;
		}
	else {
		var selections = new Array();
		selections = document.forms[0].selected.value.split(';');
		for (var i = 0; i < selections.length; i++) {
			if (selections[i] == selString) {
				selections.splice(i,1);
				document.forms[0].selected.value = selections.join(';');
				break;
				}
			}
		}
	}
	
function htmlOnLink (text) {
	selString = "ZTEXT       " + text;		
	document.forms[0].selected.value = selString;
	document.forms[0].keyCode.value = 0;
	document.forms[0].submit();
	}
/***/
function plCallBack(url, status)
{
	if (status != 0)
		return;
/*		alert("Plugin-Installation, Fehler: " + status); */
}
function plInstall(plId, version, url)
{
  if (window.InstallTrigger == undefined)
    return;
  var comp = InstallTrigger.compareVersion(plId, version);
  if (comp >= 0)
    return;
  if (!InstallTrigger.enabled())
  {
    alert("Browser erlaubt keine Plugin-Installation!");
    return;
  }
  var xpi = new Object();
  xpi[plId] = url;
  InstallTrigger.install(xpi, plCallBack);
}
