// 23-11-2004
// neue Node-Eigenschaft: ausgewertet
// Nodes koennen nun auch doppelte realId haben
// fkt getNodeIdSchicht() nun erweitert um Parameter doppelte, gibt dann array zurueck
// 18-11-2004
//		neue Fuktionen:
//			optionsSchichtSelectboxwerteZurueckSetzen(),optionsSchichtSelectboxwertSetzen()
//		optionChanged() und setOptionChildrenRekursiv() nun schichtabhaengig
//  	kleinere Bugs ausgeraeumt
//		Arbeitet nun mit Schichten aus mehreren Datentabellen zusammen
//		Selectfelder haben nun eine schichtabhaengige Selectboxauswahl-Weitergabe
// 24-10-2004  
//		neu mit merkechecked und nur bei Aenderungen werden die Ergebnisse
//		bei checkedToString() in einen String geschrieben
//		neu checkNodeVonAussen,  checkboxfunktion
//		Root hat jetzt keine Checkbox mehr wenn tree.config.usechecked = true
// 06-10-2004
// ali:
// nun mit rooticon beim dtreeaufruf

/* dTree 2.05 | www.destroydrop.com/javascript/tree/ 
Copyright (c) 2002-2003 Geir Landrö Updated: 18.11.2004 */
// 09-09-2004 Weiter erweitert um:
//  config.HTMLcode, checkChildren,optionHTML
// erweitert von ali um:
// this.optionChange - zeigt an ob optionswerte aus evt vorhandenem select-Feld neu gewaehlt wurden
// node.ischecked ,  tree.config.usechecked
// im Node um:
//		ischecked , optionvalue
// Functionen: dTree.prototype.checkedToString()
// Ausgabe mit Checkhaekchen moeglich
// Beim Aufruf wird startpfad mit uebergeben, der bezeichnet ab wo die js, css img-Ordner stehen
//	somit ist auch ein includen oder Benutzen in Klassen moeglich
// Hinweis:
//		nodeId ist der interne Arrayindex von this.aNodes[]
//		id,realId = id die durch tree.add vom user uebergeben werden (bis jetzt nur Zahlen moeglich -BUG- weiss nicht warum)
//		pid = parent ID (vom user uebergeben worden)
//		schicht = Nummer der Einzelnen Datenschichten wenn nicht vorhanden immer = 0
//		Die Selectbox-IDs des Trees heissen: 'sel' + treename + nodeID
// Node object
function Node(id, pid, name, url, title, target, icon, iconOpen, open, ischecked,selectboxoptions,selectvorgabe,schicht) {
	this.id = id;
	this.pid = pid;
	this.name = name;
	this.url = url;
	this.title = title;
	this.target = target;
	this.icon = icon;
	this.iconOpen = iconOpen;
	this._io = open || false;
	this._is = false;
	this._ls = false;
	this._hc = false;
	this._ai = 0;
	this._p;
	this.ischecked = ischecked || false;
	this.merkechecked = this.ischecked;
	this.ausgewertet = false;
	if(schicht){
		this.schicht=schicht;
	} else {
		this.schicht = 0;
	}
	if(selectvorgabe){
		this.optionvaluevorgabe = selectvorgabe;
		this.optionvalue = selectvorgabe;
	} else {
		this.optionvaluevorgabe = false;
		this.optionvalue = false;
	}
	if(selectboxoptions){
		this.selectbox = selectboxoptions;
	} else {
		this.selectbox = false;
	}
};

// Tree object
function dTree(objName,startpfad,selectboxtext,rooticon,alsmenue) {
	var arr,st,st2,wert,i;
	this.startpfad = startpfad;
	this.config = {
		target					: null,
		folderLinks			: true,
		useSelection		: true,
		useCookies			: true,
		useLines				: true,
		useIcons				: true,
		useStatusText		: false,
		closeSameLevel	: false,
		inOrder					: false,
		usechecked   : true,
		startpfad	 : this.startpfad,
		HTMLcode : '',
		checkChildren : false,
		setOptionToChildren : false,
		useOptionDifferences : true,
		letztschicht : false,
		letztschichtMitDoppelte : false
	}
	if(alsmenue && alsmenue != undefined){
		this.icon = {
			root				: this.startpfad + 'img/emptymenue.gif',
			folder			: this.startpfad + 'img/emptymenue.gif',
			folderOpen	: this.startpfad + 'img/emptymenue.gif',
			node				: this.startpfad + 'img/emptymenue.gif',
			empty				: this.startpfad + 'img/emptymenue.gif',
			line				: this.startpfad + 'img/emptymenue.gif',
			join				: this.startpfad + 'img/emptymenue.gif',
			joinBottom	: this.startpfad + 'img/emptymenue.gif',
			plus				: this.startpfad + 'img/emptymenue.gif',
			plusBottom	: this.startpfad + 'img/emptymenue.gif',
			minus				: this.startpfad + 'img/emptymenue.gif',
			minusBottom	: this.startpfad + 'img/emptymenue.gif',
			nlPlus			: this.startpfad + 'img/emptymenue.gif',
			nlMinus			: this.startpfad + 'img/emptymenue.gif',
			checked : this.startpfad + 'img/haken.jpg',
			unchecked : this.startpfad + 'img/keinhaken.jpg'
		};		
	} else {	
		this.icon = {
			root				: this.startpfad + 'img/base.gif',
			folder			: this.startpfad + 'img/folder.gif',
			folderOpen	: this.startpfad + 'img/folderopen.gif',
			node				: this.startpfad + 'img/page.gif',
			empty				: this.startpfad + 'img/empty.gif',
			line				: this.startpfad + 'img/line.gif',
			join				: this.startpfad + 'img/join.gif',
			joinBottom	: this.startpfad + 'img/joinbottom.gif',
			plus				: this.startpfad + 'img/plus.gif',
			plusBottom	: this.startpfad + 'img/plusbottom.gif',
			minus				: this.startpfad + 'img/minus.gif',
			minusBottom	: this.startpfad + 'img/minusbottom.gif',
			nlPlus			: this.startpfad + 'img/nolines_plus.gif',
			nlMinus			: this.startpfad + 'img/nolines_minus.gif',
			checked : this.startpfad + 'img/haken.jpg',
			unchecked : this.startpfad + 'img/keinhaken.jpg'
		};
	}
	this.obj = objName;
	this.aNodes = [];
	this.aIndent = [];
	this.root = new Node(-1);
	this.selectedNode = null;
	this.selectedFound = false;
	this.completed = false;
	this.optionChange = false;
	this.checkboxChange = false;
	this.checkboxFunktion = false;	// true oder false
	if(selectboxtext && selectboxtext != ''){
		if(selectboxtext.substr(selectboxtext.length - 1)==';'){
			selectboxtext = selectboxtext.substring(0,selectboxtext.length -1);
		}
		arr = selectboxtext.split(';');
		st2='';
		i=0;
		for (i=0;i<arr.length;i = i + 2){
		// for (wert in arr){
			st2 += '<option value="' + arr[i] + '">' + arr[i + 1] + '</option>';
		}
		this.selectboxoptions = st2;
	} else {
		this.selectboxoptions = false;
	}
	if(rooticon && rooticon != undefined && rooticon !=''){
		this.icon.root = rooticon;
	}

};

// Adds a new node to the node array
dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open, ischecked,selectboxtext,selectvorgabe,schicht) {
	if(selectboxtext && selectboxtext != ''){
		if(selectboxtext.substr(selectboxtext.length - 1)==';'){
			selectboxtext = selectboxtext.substring(0,selectboxtext.length -1);
		}
		var arr = selectboxtext.split(';');
		selectboxoptions='';
		var i=0;
		if(selectvorgabe && selectvorgabe != ''){
			for (i=0;i<arr.length;i = i + 2){
				if(arr[i] == selectvorgabe){
					selectboxoptions += '<option value="' + arr[i] + '" selected>' + arr[i + 1] + '</option>'
				} else {
					selectboxoptions += '<option value="' + arr[i] + '">' + arr[i + 1] + '</option>';
				}
			}			
		} else {
			for (i=0;i<arr.length;i = i + 2){
				selectboxoptions += '<option value="' + arr[i] + '">' + arr[i + 1] + '</option>';
			}
		}
	} else {
		selectboxoptions = false;
	}
	this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open, ischecked, selectboxoptions,selectvorgabe,schicht);
};

// Open/close all nodes
dTree.prototype.openAll = function() {
	this.oAll(true);
};

dTree.prototype.closeAll = function() {
	this.oAll(false);
};

// Outputs the tree to the page
dTree.prototype.toString = function() {
//17-10-2004 erweitert von aliali
	this.checkParents();	// aliali
	var str = '<div class="dtree">\n';
	if (document.getElementById) {
		if (this.config.useCookies) this.selectedNode = this.getSelected();
		str += this.addNode(this.root);
	} else str += 'Browser not supported.';
	str += '</div>';
	if (!this.selectedFound) this.selectedNode = null;
	this.completed = true;
	return str;
};

// Creates the tree structure
dTree.prototype.addNode = function(pNode) {
	var str = '';
	var n=0;
	if (this.config.inOrder) n = pNode._ai;
	for (n; n<this.aNodes.length; n++) {
		if (this.aNodes[n].pid == pNode.id) {
			gefunden = true; // aliali
			var cn = this.aNodes[n];
			cn._p = pNode;
			cn._ai = n;
			this.setCS(cn);
			if (!cn.target && this.config.target) cn.target = this.config.target;
			if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
			if (!this.config.folderLinks && cn._hc) cn.url = null;
			if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
					cn._is = true;
					this.selectedNode = n;
					this.selectedFound = true;
			}
			str += this.node(cn, n);
			if (cn._ls) break;
		}
	}
	return str;
};

// Creates the node icon, url and text
dTree.prototype.node = function(node, nodeId) {
	var str = '<div class="dTreeNode">' + this.indent(node, nodeId);
	var realID = node.id;		// aliali
	var selid,fktname,st15;
	if (this.config.useIcons) {
		if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
		if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
		if (this.root.id == node.pid) {
			node.icon = this.icon.root;
			node.iconOpen = this.icon.root;
		}
		str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
	}
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------	
// aliali a ------------------------------------------------------------------------------------------------------------------------------------------
	if (this.config.usechecked){
		if(node.pid != this.root.id){
			if(node.ischecked){
				str += '<img id="ch' + this.obj + nodeId + '" src="' + this.icon.checked  + '" alt="auswaehlen"';
			} else {
				str += '<img id="ch' + this.obj + nodeId + '" src="' + this.icon.unchecked  + '" alt="auswaehlen"';
			}
			if(this.config.checkChildren){
				str += ' onclick="javascript: ' + this.obj + '.checkChildren(' + nodeId + ',' + this.checkboxFunktion + ');" />';
			} else {
				str += ' onclick="javascript: ' + this.obj + '.checkNodeToggle(' + nodeId + ',' + this.checkboxFunktion + ');" />';
			}
		}
	}
	if (this.config.HTMLcode != ''){
		str += this.config.HTMLcode;
	}
	if(node.selectbox){  // hat Vorrang vor der globalen Selectbox bei this.selectboxoptions
		selid = 'sel' + this.obj + nodeId;
		str += '<select id="' + selid + '" onchange="javascript: '  + this.obj + '.optionChanged(' + "'" + selid + "','" + nodeId + "'" + ',' + node.schicht + ');">' + node.selectbox + '</select>&nbsp;';	
	} else {
		if(this.selectboxoptions){
			selid = 'sel' + this.obj + nodeId;
			str += '<select id="' + selid + '" class=\"selectfelddef\" onchange="javascript: '  + this.obj + '.optionChanged(' + "'" + selid + "','" + nodeId + "'"  + ',' + node.schicht + ');">' + this.selectboxoptions + '</select>&nbsp;';
		}		
	}
	
// aliali e -----------------------------------------------------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	if (node.url) {
		str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';
		if (node.title) str += ' title="' + node.title + '"';
		if (node.target) str += ' target="' + node.target + '"';
		if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';
		if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
			str += ' onclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';
		str += '>';
	} else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
		str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">';
	str += node.name;
	if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
	str += '</div>';
	if (node._hc) {
		str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
		str += this.addNode(node);
		str += '</div>';
	}
	this.aIndent.pop();
	return str;
};

// Adds the empty and line icons
dTree.prototype.indent = function(node, nodeId) {
	var str = '';
	if (this.root.id != node.pid) {
		for (var n=0; n<this.aIndent.length; n++)
			str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
		(node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
		if (node._hc) {
			str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
			if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
			else str += ( (node._io) ? ((node._ls && this.config.useLines) ? this.icon.minusBottom : this.icon.minus) : ((node._ls && this.config.useLines) ? this.icon.plusBottom : this.icon.plus ) );
			str += '" alt="" /></a>';
		} else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
	}
	return str;
};

// Checks if a node has any children and if it is the last sibling
dTree.prototype.setCS = function(node) {
	var lastId;
	for (var n=0; n<this.aNodes.length; n++) {
		if (this.aNodes[n].pid == node.id) node._hc = true;
		if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
	}
	if (lastId==node.id) node._ls = true;
};

// Returns the selected node
dTree.prototype.getSelected = function() {
	var sn = this.getCookie('cs' + this.obj);
	return (sn) ? sn : null;
};

// Highlights the selected node
dTree.prototype.s = function(id) {
	if (!this.config.useSelection) return;
	var cn = this.aNodes[id];
	if (cn._hc && !this.config.folderLinks) return;
	if (this.selectedNode != id) {
		if (this.selectedNode || this.selectedNode==0) {
			eOld = document.getElementById("s" + this.obj + this.selectedNode);
			eOld.className = "node";
		}
		eNew = document.getElementById("s" + this.obj + id);
		eNew.className = "nodeSel";
		this.selectedNode = id;
		if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
	}
};

// Toggle Open or close
dTree.prototype.o = function(id) {
	var cn = this.aNodes[id];
	this.nodeStatus(!cn._io, id, cn._ls);
	cn._io = !cn._io;
	if (this.config.closeSameLevel) this.closeLevel(cn);
	if (this.config.useCookies) this.updateCookie();
};

// Open or close all nodes
dTree.prototype.oAll = function(status) {
	for (var n=0; n<this.aNodes.length; n++) {
		if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
			this.nodeStatus(status, n, this.aNodes[n]._ls)
			this.aNodes[n]._io = status;
		}
	}
	if (this.config.useCookies) this.updateCookie();
};

// Opens the tree to a specific node
dTree.prototype.openTo = function(nId, bSelect, bFirst) {
	if (!bFirst) {
		for (var n=0; n<this.aNodes.length; n++) {
			if (this.aNodes[n].id == nId) {
				nId=n;
				break;
			}
		}
	}
	var cn=this.aNodes[nId];
	if (cn.pid==this.root.id || !cn._p) return;
	cn._io = true;
	cn._is = bSelect;
	if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
	if (this.completed && bSelect) this.s(cn._ai);
	else if (bSelect) this._sn=cn._ai;
	this.openTo(cn._p._ai, false, true);
};

// Closes all nodes on the same level as certain node
dTree.prototype.closeLevel = function(node) {
	for (var n=0; n<this.aNodes.length; n++) {
		if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
			this.nodeStatus(false, n, this.aNodes[n]._ls);
			this.aNodes[n]._io = false;
			this.closeAllChildren(this.aNodes[n]);
		}
	}
}

// Closes all children of a node
dTree.prototype.closeAllChildren = function(node) {
	for (var n=0; n<this.aNodes.length; n++) {
		if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
			if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
			this.aNodes[n]._io = false;
			this.closeAllChildren(this.aNodes[n]);		
		}
	}
}

// Change the status of a node(open or closed)
dTree.prototype.nodeStatus = function(status, id, bottom) {
	eDiv	= document.getElementById('d' + this.obj + id);
	eJoin	= document.getElementById('j' + this.obj + id);
	if (this.config.useIcons) {
		eIcon	= document.getElementById('i' + this.obj + id);
		eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
	}
	eJoin.src = (this.config.useLines)?
	((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
	((status)?this.icon.nlMinus:this.icon.nlPlus);
	eDiv.style.display = (status) ? 'block': 'none';
};

// [Cookie] Clears a cookie
dTree.prototype.clearCookie = function() {
	var now = new Date();
	var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
	this.setCookie('co'+this.obj, 'cookieValue', yesterday);
	this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
};

// [Cookie] Sets value in a cookie
dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
	document.cookie =
		escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
};

// [Cookie] Gets a value from a cookie
dTree.prototype.getCookie = function(cookieName) {
	var cookieValue = '';
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else cookieValue = unescape(document.cookie.substring(posValue));
	}
	return (cookieValue);
};

// [Cookie] Returns ids of open nodes as a string
dTree.prototype.updateCookie = function() {
	var str = '';
	for (var n=0; n<this.aNodes.length; n++) {
		if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
			if (str) str += '.';
			str += this.aNodes[n].id;
		}
	}
	this.setCookie('co' + this.obj, str);
};

// [Cookie] Checks if a node id is in a cookie
dTree.prototype.isOpen = function(id) {
	var aOpen = this.getCookie('co' + this.obj).split('.');
	for (var n=0; n<aOpen.length; n++)
		if (aOpen[n] == id) return true;
	return false;
};
// If Push and pop is not implemented by the browser
if (!Array.prototype.push) {
	Array.prototype.push = function array_push() {
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	}
};
if (!Array.prototype.pop) {
	Array.prototype.pop = function array_pop() {
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
};

// aliali neue Funktionen -----------------------------------------------------------------------------------------
dTree.prototype.checkParents = function(){
// 17-11-2004
	// Nach allen dTree.Add einzusetzen !!!
	// Wenn von einem Node kein Node mit seiner ParentId existiert,
	// wird die die parentid auf 0 gesetzt 
	var i,j,parentid,gefunden;
	for (i=0; i<this.aNodes.length; i++) {
		parentid = this.aNodes[i].pid;
		if(parentid>0){	// nur wenn parents angegeben sind und nicht gleich dem root (pid=-1)
			gefunden = false;
			for (j=0; j<this.aNodes.length; j++){
				if(j != i){
					if(this.aNodes[j].id==parentid){
						gefunden = true;
						break;
					}
				}
			}
			if(!gefunden){this.aNodes[i].pid = 0;}
		}
	}	
};
dTree.prototype.getNodeId = function(realId){
	var n;
	for (n=1; n<this.aNodes.length; n++){
		if(this.aNodes[n].id==realId){
			return n;
		}
	}
	return false;
};
dTree.prototype.getNodeIdSchicht = function(realId,schicht,doppelte){
	var n,i;
	var arr = Array();
	if(doppelte && doppelte != undefined){
		i = -1;
		for (n=1; n<this.aNodes.length; n++){
			if(this.aNodes[n].id==realId){
				if(this.aNodes[n].schicht == schicht){
					i++;
					arr[i] = n;
				}
			}
		}
		if(i==-1){
			return false;
		} else {
			return arr;
		}
	} else {
		for (n=1; n<this.aNodes.length; n++){
			if(this.aNodes[n].id==realId){
				if(this.aNodes[n].schicht == schicht){
					return n;
				}
			}
		}
	}
	return false;
};
//  Checkbox +++++++++++++++++++++++++++++++++++++++++++
dTree.prototype.checkedToString = function(){
	var nId, n, st ='',a;
	if(this.config.usechecked){
		for (n=0; n<this.aNodes.length; n++) {
			if(this.aNodes[n].merkechecked != this.aNodes[n].ischecked){ // nur bei wirklichen Aenderungen
				st += this.aNodes[n].id + ';';
				if(this.aNodes[n].ischecked){
					a=1;
				} else {
					a=0;
				}
				st += a + ';';
			}
		}
	}
	if(st.substr(st.length - 1)==';'){
		st = st.substring(0,st.length -1);
	}	
	return st;
};

dTree.prototype.checkNodeToggle = function(nodeId,checkboxfunktion){
	var obj = document.getElementById('ch' + this.obj + nodeId);
	if(obj){
		if(this.aNodes[nodeId].ischecked){
			obj.src = this.icon.unchecked;
			this.aNodes[nodeId].ischecked = false;
		} else {
			obj.src = this.icon.checked;
			this.aNodes[nodeId].ischecked = true;
		}
		this.checkboxChange = true;
	}
	if(checkboxfunktion){
		bkCheckboxTree(this.obj,this.aNodes[nodeId].id,this.aNodes[nodeId].name,this.aNodes[nodeId].ischecked);
	}
};

dTree.prototype.checkNodeVonAussen = function(realId,checked,schicht,alsvorgabe,checkboxfunktion){
// 24-10-2004
// alsvorgabe : wenn true wird dieser Wert als Vorgabewert genommen, sonst wird spaeter ueberprueft
//		ob der neue Wert ungleich der schon vorhandenen Vorgabe ist
// ist checkboxfunktion true dann wird dies der Funktion checkNode mit uebergeben und die
//  Vorgabecheckboxklickfunktion aufgerufen in der der User eigene Klickreaktionen
// programmieren kann
	nodeId=this.getNodeIdSchicht(realId,schicht);
	this.checkNode(nodeId,checked,checkboxfunktion);
	if(alsvorgabe){
		this.aNodes[nodeId].merkechecked = checked;
	}
}
dTree.prototype.checkNode = function(nodeId,bol,checkboxfunktion){
	var obj = document.getElementById('ch' + this.obj + nodeId);
	if(obj){
		if(bol) { // Haken setzen bol = true
			if(!this.aNodes[nodeId].ischecked){
				obj.src = this.icon.checked;
				this.aNodes[nodeId].ischecked = true;
			}	
		} else {	// Haken wegnehmen bol = false
			if(this.aNodes[nodeId].ischecked){
				obj.src = this.icon.unchecked;
				this.aNodes[nodeId].ischecked = false;
			}
		}
	}
	if(checkboxfunktion){ // wird mit treename,nodeid und nodebez,ischecked aufgerufen leider Name noch fest vorgegeben
		bkCheckboxTree(this.obj,this.aNodes[nodeId].id,this.aNodes[nodeId].name,this.aNodes[nodeId].ischecked);
	}	
};
dTree.prototype.checkChildren = function(nodeId,checkboxfunktion){
	var pid;
	if(this.aNodes[nodeId].ischecked){
		this.checkNode(nodeId,false,checkboxfunktion);
		this.checkChildrenRekursiv(this.aNodes[nodeId].id,false,checkboxfunktion);
	} else {
		this.checkNode(nodeId,true,checkboxfunktion);
		this.checkChildrenRekursiv(this.aNodes[nodeId].id,true,checkboxfunktion);		
	}
	this.checkboxChange = true;
};
dTree.prototype.checkChildrenRekursiv = function(prealId,bol,checkboxfunktion){
	// bol = true = Haken setzen  bole=false = Haken wegnehmen
	// prealId = Parent-Node ID
	var pid,n,parent;
	for (n=1; n<this.aNodes.length; n++) {	// ab n=1 statt n=0 weil 0=Rootknoten der hat keine pid
		parent=this.aNodes[n].pid;
		if (parent == prealId){
			this.checkNode(n,bol,checkboxfunktion);
			// childrealID = this.aNodes[nodeID].id
			this.checkChildrenRekursiv(this.aNodes[n].id,bol,checkboxfunktion);
		}
	}
};

// Select-Feld +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

dTree.prototype.setOptionVonAussen = function(realId,wert,schicht){
	// Hier wird nicht 	this.optionChange = true;	gesetzt
	// Die optionvorgabe des jeweiligen nodes wird hierdurch vorgegeben
	var nodeId,obj,realId2,a,id2,i;
	var arr = Array();
	if(schicht && schicht > 0){
		realId = 's' + schicht + '-' + realId;
	}
	if(this.config.letztschichtMitDoppelte && (schicht==this.config.letztschicht)){
		// auf evt doppelte testen, da es die letzte Schicht ist muss nicht setOptionChildrenRekursiv aufgerufen werden
		arr = this.getNodeIdSchicht(realId,schicht,true);
		for (i=0;i<arr.length;i++){
			this.aNodes[arr[i]].optionvalue = wert;
			this.aNodes[arr[i]].optionvaluevorgabe = wert;
			obj = document.getElementById('sel' + this.obj + arr[i]);
			obj.value = wert;
		}
	} else {	
		nodeId=this.getNodeIdSchicht(realId,schicht);
		if(nodeId){
			obj = document.getElementById('sel' + this.obj + nodeId);
			if(obj){
				this.aNodes[nodeId].optionvalue = wert;
				this.aNodes[nodeId].optionvaluevorgabe = wert;
				obj.value = wert;
			}
		}
	}
};
/*  wird noch nicht benoetigt
dTree.prototype.setOption = function(nodeId,wert){
// fuer den internen Aufruf ist schneller als von aussen,
// da die nodeId bekannt ist
	var nodeId,obj,a;
	obj = document.getElementById('sel' + this.obj + nodeId);
	if(obj){
		this.aNodes[nodeId].optionvalue = wert;
		obj.value = wert;
	}
};	*/

dTree.prototype.optionChanged = function (objId,nodeId,schicht){
	// nachdem vom Benutzer eine Option ausgewaehlt wurde
	var obj,wert,a,id2,realId,realId3,nodeId2,treename;
	var arr = Array();
	obj = document.getElementById(objId);
	wert = obj.value;
	if(this.aNodes[nodeId].pid == -1){ // root
		schicht = undefined;	// Angaben bei der Root Select-Box werden an alle Elemente schichtunabhaengig weiter gegeben
	}
	if(obj){
		if(this.config.letztschichtMitDoppelte && (schicht==this.config.letztschicht)){
			treename = this.obj;
			realId = this.aNodes[nodeId].id;
			arr = this.getNodeIdSchicht(realId,schicht,true);
			for (i=0;i<arr.length;i++){
				this.aNodes[arr[i]].optionvalue = wert;
				obj = document.getElementById('sel' + treename + arr[i]);
				obj.value = wert;
				if(this.config.setOptionToChildren){
					this.setOptionChildrenRekursiv(this.aNodes[arr[i]].id,wert,schicht);
				}				
			}
		} else {
			this.aNodes[nodeId].optionvalue = wert;
			if(this.config.setOptionToChildren){
				this.setOptionChildrenRekursiv(this.aNodes[nodeId].id,wert,schicht);
			}
		}
		this.optionChange = true;  // gibt fuer den ges. Tree an, dass eine Option in einem Selectfeld geaendert wurde
	}
}
dTree.prototype.setOptionChildrenRekursiv = function(prealId,wert,schicht){
	// wird von optionChanged aufgerufen wenn setOptionToChildren=true (standard=false) ist
	// prealId = Parent-Node ID
	var pid,n,obj,treename;
	treename =  this.obj;
	if(schicht != undefined){
		for (n=1; n<this.aNodes.length; n++) {	// ab n=1 statt n=0 weil 0=Rootknoten der hat keine pid
			if (this.aNodes[n].pid == prealId && this.aNodes[n].schicht == schicht){
				obj = document.getElementById('sel' + treename + n);
				if(obj){
					this.aNodes[n].optionvalue = wert;
					obj.value = wert;
					this.setOptionChildrenRekursiv(this.aNodes[n].id,wert,schicht);
				}
			}
		}		
	} else {
		for (n=1; n<this.aNodes.length; n++) {	// ab n=1 statt n=0 weil 0=Rootknoten der hat keine pid
			if (this.aNodes[n].pid == prealId){
				obj = document.getElementById('sel' + treename + n);
				if(obj){
					this.aNodes[n].optionvalue = wert;
					obj.value = wert;
					this.setOptionChildrenRekursiv(this.aNodes[n].id,wert);
				}
			}
		}				
	}

};
dTree.prototype.optionsToString = function(schicht){
	// Schreibt alle neu gewaehlten Selectbox-werte in einen String mit ';' getrennt
	// realid;optionswert;realid;optionswert etc...
	// Vergleicht vorher auf evt. Vorgaben node[n].optionvorgabe und beruecksichtigt nur Differenzwerte
	//		wenn useOptionDifferences=true (standard)
	// Wird schicht angegeben dann werden nur nodes der entsprechenden Schicht beruecksicht
	var n, st ='',optionsvalue,objnode;
	var arr = Array();
	if(schicht != undefined){
		if(this.config.letztschichtMitDoppelte && (schicht==this.config.letztschicht)){
			for (n=1; n<this.aNodes.length; n++) {
				if(!this.aNodes[n].ausgewertet){
					arr = this.getNodeIdSchicht(this.aNodes[n].id,schicht,true);
					if(arr){
						objnode = this.aNodes[arr[0]];
						optionsvalue=objnode.optionvalue;
						if(optionsvalue && objnode.schicht==schicht){		
							if(this.config.useOptionDifferences){
								if(objnode.optionvaluevorgabe){
									if(optionsvalue != objnode.optionvaluevorgabe){
										// wurde als neue Klammer genommen weil sonst Fehler auftreten wenn vorgabe=false
										st += objnode.id + ';' + optionsvalue + ';';
									}
								} else {
									st += objnode.id + ';' + optionsvalue + ';';
								}
							} else { // ohne Differenzpruefung zur Vorgabe
								st += objnode.id + ';' + optionsvalue + ';';
							}
						}
						for (i=0;i<arr.length;i++){
							this.aNodes[arr[i]].ausgewertet = true;
						}
					}
				}
			}				
		} else {
			for (n=1; n<this.aNodes.length; n++) {
				optionsvalue=this.aNodes[n].optionvalue;
				if(optionsvalue && this.aNodes[n].schicht==schicht){
					if(this.config.useOptionDifferences){
						if(this.aNodes[n].optionvaluevorgabe){
							if(optionsvalue != this.aNodes[n].optionvaluevorgabe){
								// wurde als neue Klammer genommen weil sonst Fehler auftreten wenn vorgabe=false
								st += this.aNodes[n].id + ';' + optionsvalue + ';';
							}
						} else {
							st += this.aNodes[n].id + ';' + optionsvalue + ';';
						}
					} else { // ohne Differenzpruefung zur Vorgabe
						st += this.aNodes[n].id + ';' + optionsvalue + ';';
					}
				}
			}	
		}		
	} else {	// ohne Schichtpruefung
		for (n=1; n<this.aNodes.length; n++) {
			optionsvalue=this.aNodes[n].optionvalue;
			if(optionsvalue){
				if(this.useOptionDifferences){
					if(this.aNodes[n].optionvaluevorgabe){
						if(optionvalue != this.aNodes[n].optionvaluevorgabe){
							// wurde als neue Klammer genommen weil sonst Fehler auftreten wenn vorgabe=false
							st += this.aNodes[n].id + ';' + optionsvalue + ';';
						}
					} else {
						st += this.aNodes[n].id + ';' + optionsvalue + ';';
					}
				} else { // ohne Differenzpruefung zur Vorgabe
					st += this.aNodes[n].id + ';' + optionsvalue + ';';
				}
			}
		}
	}
	if(st.substr(st.length - 1)==';'){
		st = st.substring(0,st.length -1);
	}
	return st;
};
dTree.prototype.optionsSchichtSelectboxwertSetzen = function(schicht,selectboxwert,optionchangesetzen){
// 18-11-2004
// Setzt fuer eine gesamte Schicht die Selectbox-Angaben auf selectboxwert
	var n,obj,treename;
	treename =  this.obj;
	for (n=1; n<this.aNodes.length; n++) {	// ab n=1 statt n=0 weil der Rootknoten ignoriert werden soll
		if (this.aNodes[n].schicht == schicht){
			obj = document.getElementById('sel' + treename + n);
			if(obj){
				this.aNodes[n].optionvalue = selectboxwert;
				obj.value = selectboxwert;
			}
		}
	}
	if(optionchangesetzen && optionchangesetzen != undefined){
		this.optionChange = true;  // gibt fuer den ges. Tree an, dass eine Option in einem Selectfeld geaendert wurde
	}	
};
dTree.prototype.optionsSchichtSelectboxwerteZurueckSetzen = function(schicht){
// 18-11-2004
	var n,obj,treename,selectboxwert;
	treename =  this.obj;
	for (n=0; n<this.aNodes.length; n++) {	// ab n=0 statt n=0 weil der Rootknoten mit beruecksichtigt werden soll
		if (this.aNodes[n].schicht == schicht){
			obj = document.getElementById('sel' + treename + n);
			if(obj){
				selectboxwert = (this.aNodes[n].optionvaluevorgabe ? this.aNodes[n].optionvaluevorgabe : 0);
				this.aNodes[n].optionvalue = selectboxwert;
				obj.value = selectboxwert;
			}
		}
	}
	this.optionChange = false;  // gibt fuer den ges. Tree an, dass keine Option in einem Selectfeld geaendert wurde	
};
