/* PASTET BY ARNE */
var isDOM=document.getElementById?1:0;
var isIE=document.all?1:0;
var isNS4=navigator.appName=='Netscape'&&!isDOM?1:0;
var isOp=window.opera?1:0;
var isWin=navigator.platform.indexOf('Win')!=-1?1:0;
var isDyn=isDOM||isIE||isNS4;

function getRef(id, par) {
	par=!par?document:(par.navigator?par.document:par);
	return isIE ? par.all[id] : (isDOM ? (par.getElementById ? par : par.ownerDocument ).getElementById( id ) : ( isNS4 ? par.layers[id] : null ) );
}


/* "BLACK-BOX" WRAPPER LOGIC FOR THE "COOLTREEPRO" SCRIPT FORMERLY USED */
function DOMtree(myName, nodes, format, isDirtyHiddenId ) {
	this.myName = myName;
	this.nodes_temp = nodes;
	this.ctCounter = 0;
	this.expNode = 0;
};

DOMtree.prototype.init = function() { 
	with (this)	{
		// Write out the correct HTML code for the tree.
		this.ctParser(myName, nodes_temp, '');

		// Switch to the proper object after this function has returned, call its init().
		setTimeout('window["' + myName + '"] = new CheckTree("' + myName + '"); with (window["' + myName + '"]) { countAllLevels=1; checkFormat="(%n% "+checked_text+")"; window["' + myName + '"].expNode = ' + expNode + '; window["' + myName + '"].init() }', 50);
	}
};

DOMtree.prototype.ctParser = function(myName, nodes, indent) { 
	with (this) {
		document.writeln(indent + '<ul id="tree-' + myName + (ctCounter ? '-' + ctCounter : '') + '" class="checktree">');
		for (var i = (indent ? 2 : 0); i < nodes.length; i++) if (nodes[i]) {
		
			// Commented out code is wrapper for old "COOLTREE" script format. Also loop from 3 when reverting.
			var nodeName = nodes[i][0]; //nodes[i][0].match(/^([^<]*)/)[1];
			var nodeID = nodes[i][1]; //nodes[i][0].match(/&quot;,&quot;(\d*)&quot;,&quot;/)[1];
			// Yes, this is non-XHTML-compliant :). They're stored for the pre-populating function below.
			var classN = (nodes[i+1]) ? '' : ' class="last"';
			if (nodes[i][2]) {
				// Sublevel -- recursively call this function.
				var levelID = myName + '-' + (++ctCounter);
				document.writeln(indent + ' <li ' + classN + ' id="show-' + levelID +  '" name="li' + nodeID + '">\n' + indent + '  <input id="check-' + levelID + '" type="checkbox" name="' + nodeID + '" onclick="document.getElementById(\'link-' + levelID + '\').innerHTML = this.checked ? remove_all_text : add_all_text" />' + nodeName + ' <a id="link-' + levelID + '" href="#" onmousedown="with(document.getElementById(\'check-' + levelID + '\')) checked = !checked;" onclick="return false">' + add_all_text + '</a> ' + '<span class="count" id="count-' + levelID + '"></span>');
				ctParser(myName, nodes[i], indent+'  ');
				document.writeln(indent + ' </li>');
			} else {
				// Normal LI item. Defaults to "Add" unchecked state, checked items are set later.
				document.writeln(indent + ' <li' + classN + '><input type="checkbox" name="' + nodeID + '" id="item-' + nodeID + '" onclick="document.getElementById(\'link-' + nodeID + '\').innerHTML = this.checked ? remove_text : add_text" />' + nodeName + ' <a id="link-' + nodeID + '" href="#" onmousedown="with(document.getElementById(\'item-' + nodeID + '\')) checked = !checked;" onclick="this.innerHTML = ' + 'document.getElementById(\'item-' + nodeID + '\').checked ? remove_text : add_text; ' + 'return false">' + add_text + '</a></li>');
			}
		}
		document.writeln(indent + '</ul>');
	}
};

DOMtree.prototype.setExpandedNode = function( nodeID ) {
	this.expNode = nodeID;
}




// Called from the bottom of the page, I'm hijacking this function to prepopulate the trees.
function RedrawAllTrees() {
	setTimeout('populateTrees()', 500);
};

// This expands all branches up to and including the one specified. Check for sublevels!
function expandBranch(nodeID) {
	var node = document.getElementsByName(nodeID)[0];
	if (node && node.id && node.id.match(/^[^-]*-(.*)/)) node = getRef('tree-' + RegExp.$1);
	while (node && node.style && node.style.display != 'block') {
		if (node.tagName == 'UL') {
			node.style.display = 'block';
			var li = document.getElementById('show-' + node.id.match(/^tree-(.*)/)[1]);
			if (li) {
				li.className = li.className.replace('plus', 'minus');
			}
		}
		node = node.parentNode;
	}
};

// SELECT fields -> checkbox states.
function populateTrees() {
	if (!document.getElementById) return;
	var oContent = document.getElementById( "content" );
	// fixes height issues in Mozilla, DO NOT DELETE!!!
	if ( oContent ) { oContent.style.display = "none"; }
	
	for (var i in CheckTree.list) {
	    if ( typeof( CheckTree.list[i] ) == "object" ) { //make sure we don't get unwanted members
            with (CheckTree.list[i]) {
            
                var sTest = ""
                for ( var p in CheckTree.list[i] ) {
                    sTest += p + " | " + CheckTree.list[i][p];
                }
                
		        // Sync up states of checkboxes that are already checked.
		        var boxes = root.getElementsByTagName('input');
		        if (boxes) {
			        for (var j = 0; j < boxes.length; j++) {
				        if (boxes[j].checked) boxes[j].onclick();
			        }
		        }

		        var select = document.getElementsByName(myName)[0];
		        if (!select) continue;
          
		        for (var c = 0; c < select.childNodes.length; c++) {
          
			        var checkbox = document.getElementsByName(select.childNodes[c].value)[0];
           
			        if (checkbox && !checkbox.checked) {
				        checkbox.checked = true;
				        expandBranch(checkbox.name);
				        checkbox.onclick();
			        }
		        }
		        if (expNode && expNode != 0) {
			        expandBranch(expNode);
		        }

		        // Pass the "countOnly" option to trust the server's checked/unchecked data...
		        root.setBoxStates({}, true, true);
            }
        }
	}

	// fixes height issues in Mozilla, DO NOT DELETE!!!
	if ( oContent ) { oContent.style.display = "block"; }
	if ( oGod && oGod.mHideWaitMessage ) {
		oGod.mHideWaitMessage();
	}
};


// Called onsubmit, this populates the output & storage select fields.
function copyTreeValues() {	
    for (var i in CheckTree.list) { 
	    if ( typeof( CheckTree.list[i] ) == "object" ) { //make sure we don't get unwanted members
	        with (CheckTree.list[i]) {
		        var select = document.getElementsByName(myName)[0];
		        var storage = document.getElementsByName('storage_tree_' + myName);
		        if (storage) { 
			        storage = storage[0]; if (storage) storage.value = '';
		        }
		        var boxes = root.getElementsByTagName('input');
		        if (!select || !boxes) continue;
          
		        while (select.firstChild) {
			        select.removeChild(select.firstChild);
		        }
          
		        for (var b = 0; b < boxes.length; b++) {
			        if (boxes[b].type == 'checkbox' && boxes[b].checked) {
				        var option = document.createElement('option');
				        option.value = boxes[b].name;
				        option.innerText = option.value;
				        select.appendChild(option);
        				
				        if (storage) {
					        if (storage.value != '') {
						        storage.value += '|';
					        }
					        storage.value += boxes[b].name;
				        }
			        }
		        }
            }
        }
	}
};

/*
CHECKTREE v1.0beta (c) 2004 Angus Turnbull, http://www.twinhelix.com
Altering this notice or redistributing this file is prohibited.
*/

CheckTree.list = [];

// Main object class for the script, adds itself to a list of menu objects we create.
function CheckTree(myName) {
	this.myName = myName;
	this.root = null;
	this.countAllLevels = false;
	this.checkFormat = '(%n% checked)';
	CheckTree.list[myName] = this;
};

// Called onload, this sets up a reference to the 'root' node and hides sublevels.
CheckTree.prototype.init = function() {	
	with (this) {
		if (!document.getElementById) return;
		
		root = document.getElementById('tree-' + myName);
		if ( root ) {
			var lists = root.getElementsByTagName('ul');
			for (var ul = 0; ul < lists.length; ul++) {
				// Hide all UL sublevels under the root node, and assign them toggle/onclick methods.
				lists[ul].style.display = 'none';
				lists[ul].treeObj = this;
				lists[ul].setBoxStates = setBoxStates;

				if (lists[ul].addEventListener) {
					lists[ul].addEventListener('click', new Function('e', 'this.setBoxStates(e)'), false);
				} else {
					lists[ul].onclick = setBoxStates;
				}
			}

			// Now do a similar setup for the 'root' node.
			root.treeObj = this;
			root.setBoxStates = setBoxStates;
	  
			if (root.addEventListener) {
				root.addEventListener('click', new Function('e', myName + '.click(e)'), false);
			} else {
				root.onclick = new Function('e', myName + '.click(e)');
			}
			
			
			
			// Trigger a quick state update, to set the counters for each level.
			root.setBoxStates({}, true, true);

			// Now go through and assign plus/plus-last classes to the appropriate <LI>s.
			var nodes = root.getElementsByTagName('li');
			for (var li = 0; li < nodes.length; li++) {
				if (nodes[li].id.match(/^show-/)) {
					nodes[li].className = (nodes[li].className=='last' ? 'plus-last' : 'plus');
				}
			}
		}
	}
};


// Called on click of the entire tree, this manages visibility of sublevels.
CheckTree.prototype.click = function(e) { 
	window.isDirty = true;
	with (this) {
		e = e || window.event;
		var elm = e.srcElement || e.target;

		// !!!! ADDITION TO STANDARD SCRIPT !!!!
		if (elm.id && elm.id.match(/^(link|check)-(.*)/)) {
			var targ = document.getElementById('tree-' + RegExp.$2);
			var box = document.getElementById('check-' + RegExp.$2);
			if (box)	{
				targ.style.display = box.checked ? 'block' : 'none';
			}
			var li = document.getElementById('show-' + RegExp.$2);
			if (li) {
				li.className = li.className.replace(box.checked?'plus':'minus', box.checked?'minus':'plus');
			}
		}


		// Has a checkbox been clicked, but not processed by a lower level onclick event?
		// If so, one of the 'root' checkboxes must have been clicked.
		// We must therefore trigger a manual 'downwards route' for that tree to update it.
		// !!!! ADDITION TO STANDARD SCRIPT !!!!
		if (!e.returnValue && elm.id && elm.id.match(/^(link|check)-(.*)/)) {
			var tree = document.getElementById('tree-' + RegExp.$2);
			if (tree) {
				tree.setBoxStates(e, true, false);
			}
		}

		while (elm) {
			// Dont' do expand/collapses for clicks on checkboxes, or nested within menus.
			// !!!! ADDITION TO STANDARD SCRIPT !!!!
			if (elm.tagName == 'INPUT' || elm.tagName == 'A' || elm.tagName == 'UL') {
				break;
			}
			// Show/hide the menu element that matches the source id="show-xxx" tag and quit.
			if (elm.id && elm.id.match(/^show-(.*)/)) {
				var targ = document.getElementById('tree-' + RegExp.$1);
				if (targ.style) {
					var col = (targ.style.display == 'none');
					targ.style.display = col ? 'block' : 'none';
					// Swap the class of the <span> tag inside, maintaining "-last" state if applied.
					elm.className = elm.className.replace(col?'plus':'minus', col?'minus':'plus');
				}
				break;
			}
			// Otherwise, continue looping up the DOM tree, looking for a match.
			elm = elm.parentNode;
		}
	}
};


// This function is called as a property of the UL nodes of the DOM.
// If a triggering checkbox has been clicked, it will check/clear all boxes under it.
// Secondly, it will count the number of checked boxes at each level, and set the state
// of parent checkboxes/counters accordingly.
// It is automatically called on all parent UL nodes of the clicked checkbox, due to event
// bubbling, and will manually call itself for sublevels to ensure correct counting.
function setBoxStates(e, routingDown, countOnly) { 
	with (this) {
		// Opera <7 fix... don't perform any actions in those browsers.
		if (!this.childNodes) return;

		e = e || window.event;
		var elm = e.srcElement || e.target;

		// Initial check: if the parent checkbox for a tree level has been clicked, trigger a
		// pre-emptive downwards route within that tree, and set returnValue to true so that we
		// don't repeat it or mess with any of the original checkbox's siblings.
		// !!!! ADDITION TO STANDARD SCRIPT !!!!
		if (elm && elm.id && elm.id.match(/^(link|check)-(.*)/) && !routingDown && !e.returnValue) {
			var refTree = document.getElementById('tree-' + RegExp.$2);
			if (refTree) {
				refTree.setBoxStates(e, true, countOnly);
				e.returnValue = true;
			}
		}

		// Some counter and reference variables.
		var allChecked = true, boxCount = 0, subBoxes = null;
		// Get the name of this branch and see if the source element has id="check-xxxx".
		var thisLevel = this.id.match(/^tree-(.*)/)[1];
		var parBox = document.getElementById('check-' + thisLevel);

		// Loop through all children of all list elements inside this UL tag.
		for (var li = 0; li < childNodes.length; li++) {
			for (var tag = 0; tag < childNodes[li].childNodes.length; tag++) {
				var child = childNodes[li].childNodes[tag];
				if (!child) continue;
				if (child.tagName == 'INPUT' && child.type == 'checkbox') {
					// Set this box's state depending on its parent state, if we're routing downwards.
					// !!!! ADDITION TO STANDARD SCRIPT !!!!
					if (routingDown && parBox && elm && elm.id && elm.id.match(/^(link|check)-/) && !countOnly) {
						child.checked = parBox.checked;
						// !!!! ADDITION TO STANDARD SCRIPT !!!!
						if (child.onclick) {
							child.onclick();
						}
					}
					
					// Count the checked boxes directly under this level.
					allChecked &= child.checked;
					if (child.checked) {
						boxCount++;
					}
				}
			
				// And route this event to sublevels, to update their nodes, during a downwards route.
				if (child.tagName == 'UL' && (!e.returnValue || routingDown)) {
					child.setBoxStates(e, true, countOnly);
				}
			}
		}

		// Once we've routed the event to all sublevels, set the 'returnValue' to true, so that
		// upper levels don't re-trigger a downwards route. This is a bit of a hack, admittedly :).
		if (!routingDown) e.returnValue = true;

		// Next, set the parent parBox state depending if all checkboxes in this menu are checked.
		// Of course, we don't set its state if it's the source of the event!
		if (parBox && parBox != elm && !countOnly) {
			parBox.checked = allChecked;
			// !!!! ADDITION TO STANDARD SCRIPT !!!!
			if (parBox.onclick) {
				parBox.onclick();
			}
		}

		// If "countAllLevels" is set, overwrite the previous one-level-only count.
		if (treeObj.countAllLevels) {
			boxCount = 0;
			var subBoxes = this.getElementsByTagName('input');
			for (var i = 0; i < subBoxes.length; i++) {
				if (subBoxes[i].checked) {
					boxCount++;
				}
			}
		}

		// Either way, assign the counted value to the id="count-xxx" page element.
		var countElm = document.getElementById('count-' + thisLevel);
		if (countElm) {
			if (countElm.firstChild) {
				countElm.removeChild(countElm.firstChild);
			}
			// !!!! ADDITION TO STANDARD SCRIPT !!!!
			if (boxCount) {
				countElm.appendChild(document.createTextNode(treeObj.checkFormat.replace('%n%', boxCount + (allChecked?1:0))));
			}
		}
	}
};
