//window.onerror = handleError; //bug in IE if cancelling onunload

/* WINDOW FUNCTIONS */
function openChild(file, windowname, width, height) {
	childWindow = open(
			file,
			windowname,
			'width='
					+ width
					+ ',height='
					+ height
					+ ', toolbar = no, location = no, directories = no, status = no, mnubar = no, scrollbars = yes, resizable = yes');
	if (childWindow == null) {
		alert('It seems that you have a popup-blocker installed. Please allow popups for this site');
		return null;
	}
	if (childWindow.opener == null) {
		childWindow.opener = self;
	}
	if (childWindow.focus) {
		childWindow.focus();
	}
	return childWindow;
}

function loginCompleted() {
	alert(window.location);
	window.location.reload();
}

function updateParent() {
	opener.document.location.reload();
	self.close();
	return false;
}

/* FORM FUNCTIONS */
// Creates an url as if the formObj had been submitted
function getFormValues(formObj) {
	var valArray = new Object();
	var fElements = formObj.elements;
	for ( var i = 0; i < fElements.length; ++i) {
		var el = fElements[i];
		switch (el.type) {
		case 'text':
		case 'hidden':
		case 'textarea':
			valArray[el.name] = el.value;
			break;
		case 'radio':
			if (el.checked) {
				valArray[el.name] = el.value;
			}
			break;
		case 'select-one':
			valArray[el.name] = getSelectedOption(el);
			break;
		case 'checkbox':
			if (el.checked) {
				valArray[el.name] = 'On';
			} else {
				valArray[el.name] = '';
			}
			break;
		case 'button':
			break;
		case 'submit':
			break;
		default:
			alert('I dont know this type of field: ' + el.type);
		}
	}
	return valArray;
}
/* Function for getting the value of the selected item in a select */
function getSelectedOption(selectObj) {

	if (!('options' in selectObj)) {
		return null;
	}
	if (selectObj.selectedIndex < 0) {
		return null;
	}
	return selectObj.options[selectObj.selectedIndex].value;
	/*
	 * for (var i = 0; i < selectObj.options.length; ++i) { if
	 * (selectObj.options[i].selected) return selectObj[i].value; } return null;
	 */
}

function getSelectedRadioButton(radioButtonObjs) {
	var len = radioButtonObjs.length;
	var val = null;

	if (len > 0) {
		for ( var i = 0; i < len; ++i) {
			if (radioButtonObjs[i].checked) {
				val = radioButtonObjs[i].value;
				break;
			}
		}
	} else {
		val = radioButtonObjs.value;
	}
	return val;
}

function selectOption(selectObj, value) {
	if (!('options' in selectObj)) {
		return;
	}

	for ( var i = 0; i < selectObj.options.length; ++i) {
		if (selectObj.options[i].value == value) {
			selectObj.selectedIndex = i;
		}
	}
}

/* MISC FUNCTIONS */
function disableElement(elID, isDisabled) {
	if (isDisabled) {
		document.getElementById(elID).setAttribute('disabled', 'disabled');
	} else {
		document.getElementById(elID).removeAttribute('disabled');
	}
}

/* MULTIPLE-EVENT FUNCTIONS */
/* Functions for setting multiple onload-statements */
var _onloadArray = new Array();

function onloadPush(string) {
	document.onload = onloadExecute;
	_onloadArray[_onloadArray.length] = string;
}

function onloadExecute() {
	var string = '';
	for (i = 0; i < _onloadArray.length; ++i) {
		string += _onloadArray[i];
	}
	eval(string);
}

var _onunloadArray = new Array();

function onunloadPush(string) {
	document.onunload = onunloadExecute;
	_onunloadArray[_onunloadArray.length] = string;
}

function onunloadExecute() {

	var string = '';
	for (i = 0; i < _onunloadArray.length; ++i) {
		string += _onunloadArray[i];
	}
	eval(string);
}

function handleError() {
	return true;
}

var _onbeforeunloadArray = new Array();

function onbeforeunloadPush(string) {
	window.onbeforeunload = onbeforeunloadExecute;
	_onbeforeunloadArray[_onbeforeunloadArray.length] = string;
}

function onbeforeunloadExecute() {
	var string = '';
	for (i = 0; i < _onbeforeunloadArray.length; ++i) {
		string += _onbeforeunloadArray[i];
	}
	if (string) {
		return eval(string);
	}
	return '';
}

/* Functions for setting multiple onclick-statements on document */
var _onclickArray = new Array();

function onclickPush(string) {
	document.onclick = onclickExecute;
	_onclickArray[_onclickArray.length] = string;
}

function onclickExecute() {
	var string = '';
	for (i = 0; i < _onclickArray.length; ++i) {
		string += _onclickArray[i];
	}
	eval(string);
}

var _ontimerArray = new Array();
var _timerId = 0;

function ontimerPush(string) {
	if (!_timerId) {
		_timerId = window.setInterval(ontimerExecute, 500);
	}
	_ontimerArray[_ontimerArray.length] = string;
	return _ontimerArray.length - 1;
}

function ontimerExecute() {
	var string = '';

	for (i = 0; i < _ontimerArray.length; ++i) {
		string += _ontimerArray[i];
	}
	eval(string);
}

function ontimerRemoveat(id) {
	if (id >= 0 && id < _ontimerArray.length) {
		_ontimerArray[id] = '';
	}
}
function ontimerReset() {
	window.clearInterval(_timerId);
}

/* CONTEXT-SENSITIVE MENU FUNCTIONS */

function getPosition(e) {
	var left = 0;
	var top = 0;

	while (e.offsetParent) {
		left += e.offsetLeft;
		top += e.offsetTop;
		e = e.offsetParent;
	}

	left += e.offsetLeft;
	top += e.offsetTop;

	return {
		x :left,
		y :top
	};
}

function mouseCoords(ev) {
	if (ev.pageX || ev.pageY) {
		return {
			x :ev.pageX,
			y :ev.pageY
		};
	}
	return {
		x :ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y :ev.clientY + document.body.scrollTop - document.body.clientTop
	};
}

function windowSize() {
	var myWidth = 0, myHeight = 0;
	if (typeof (window.innerWidth) == 'number') {
		// Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement
			&& (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		// IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if (document.body
			&& (document.body.clientWidth || document.body.clientHeight)) {
		// IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return {
		x :myWidth,
		y :myHeight
	};
}
function bodySize() {
	var pageWidth = 0, pageHeight = 0;
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		pageWidth = window.innerWidth + window.scrollMaxX;
		pageHeight = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {// all
		// but
		// Explorer
		// Mac
		pageWidth = document.body.scrollWidth;
		pageHeight = document.body.scrollHeight;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		pageWidth = document.body.offsetWidth + document.body.offsetLeft;
		pageHeight = document.body.offsetHeight + document.body.offsetTop;
	}
	return {
		x :pageWidth,
		y :pageHeight
	};
}
/* The context-sensitive menu-class */
var _contextMenus = new Array();

function contextMenu(name) {
	this.name = name;
	this.submenus = 0;
	document
			.write('<div id="' + this.name + '" style="visibility: hidden; position: absolute; top: 0px; left: 0px; width: 180px; background-color: #ffffff; border: 1px solid #999999;"></div>');
	this.addToMenu = addToMenu;
	this.addSeparator = addSeparator;
	this.clearMenu = clearMenu;
	this.showMenu = showMenu;
	this.hideMenu = hideMenu;
	if (!_contextMenus.length) {
		onclickPush('hideAllContextMenus();');
	}
	_contextMenus.push(this);

	function addToMenu(text, onclick, id) {
		++this.submenus;
		if (id == '') {
			var id = this.name + '.' + this.submenus;
		}
		// document.getElementById(this.name).innerHTML += '<div id="' + id + '"
		// style="width: 100%; background-color: #ffffff;"
		// onmouseover="contextMenuMouseOver(\'' + id + '\');"
		// onmouseout="contextMenuMouseOut(\'' + id + '\');" onclick="' +
		// onclick + '">' + text + '</div>';
		document.getElementById(this.name).innerHTML += '<div id="'
				+ id
				+ '" style="width: 100%; background-color: #ffffff;" onmouseover="contextMenuMouseOver(this);" onmouseout="contextMenuMouseOut(this);" onclick="'
				+ onclick + '">' + text + '</div>';
	}

	function addSeparator() {
		document.getElementById(this.name).innerHTML += '<hr>';
	}

	function clearMenu() {
		document.getElementById(this.name).innerHTML = '';
	}

	function showMenu(e) {
		hideAllContextMenus();
		var ev = e ? e : window.event;
		var elem = (e.target) ? e.target : e.srcElement;
		var pos = mouseCoords(ev);
		document.getElementById(this.name).style.visibility = 'visible';
		document.getElementById(this.name).style.left = pos.x + 'px';
		document.getElementById(this.name).style.top = pos.y + 'px';
		return elem;
	}

	function hideMenu() {
		document.getElementById(this.name).style.visibility = 'hidden';
	}
}

/* Functions for context-sensitive menus */
function contextMenuMouseOver(obj) {
	obj.style.backgroundColor = 'rgb(31, 31, 127)';
	obj.style.color = 'rgb(255,255,255)';
}

function contextMenuMouseOut(obj) {
	obj.style.backgroundColor = 'rgb(255, 255, 255)';
	obj.style.color = 'rgb(0, 0, 0)';
}

function hideAllContextMenus() {
	for (i in _contextMenus) {
		_contextMenus[i].hideMenu();
	}
}

/* AJAX FUNCTIONS */
function detectAJAX() {
	if (window.XMLHttpRequest) {
		return true;
	}
	if (window.ActiveXObject) {
		return true;
	}
	return false;
}

function AJAXReceivedContents(httpRequest, nameOfJSCallback) {
	try {
		if (httpRequest.readyState == 4) {
			AJAXSetFree();
			if (httpRequest.status == 200) {
				AJAXevalReturn(httpRequest, nameOfJSCallback);
			} else {
				alert('There was a problem with the request.');
			}
		}
	} catch (e) {
		AJAXSetFree();
		alert('Caught Exception: ' + e.description);
	}
}

function AJAXtest(arr) {
	alert(arr);
}

function AJAXConnectServer(URL, requestType, params, nameOfJSCallback,
		showBusy, async) {
	showBusy = 1;
	var httpRequest = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			}
		}
	}
	if (!httpRequest) {
		return false;
	}
	if (showBusy) {
		AJAXSetBusy();
	}
	if (nameOfJSCallback && async) {
		httpRequest.onreadystatechange = function() {
			AJAXReceivedContents(httpRequest, nameOfJSCallback);
		};
	}
	//alert(URL + '?' + params);
	if (requestType == 'POST') {
		httpRequest.open(requestType, URL, async);
		httpRequest.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded; charset=UTF-8");
		httpRequest.send(params);
	} else if (requestType == 'GET') {
		URL += '?' + params;
		httpRequest.open(requestType, URL, async);
		httpRequest.send(null);
	}
	if (!async) {
		if (showBusy) {
			AJAXSetFree();
		}
		AJAXevalReturn(httpRequest, nameOfJSCallback);

	}
	return true;
}

function AJAXevalReturn(httpRequest, nameOfJSCallback) {
	var resArrayTmp = httpRequest.responseText.split("<CDATA>");
	var resArray = Array();
	for ( var i = 1; i < resArrayTmp.length; i += 2) {
		resArray[resArrayTmp[i]] = resArrayTmp[i + 1];
	}
	eval(nameOfJSCallback + '(resArray);');
}

function AJAXSetBusy() {
	if (document.getElementById('AJAXbusy') == null) {
		return;
	}
	document.getElementById('AJAXbusy').style.visibility = 'visible';
}

function AJAXSetFree() {
	if (document.getElementById('AJAXbusy') == null) {
		return;
	}
	document.getElementById('AJAXbusy').style.visibility = 'hidden';
}

// not finished yet!
function AJAXSubmitForm(formObj, async) {
	var urlString = getFormValues(formObj); // NOT WORKING!! getFormValues
	// redefined!
	var baseUrl = '';
	if (formObj.action) {
		baseUrl = formObj.action;
	} else {
		baseUrl = document.location; // Not correct...Should only contain
		// filename and not querystring
	}
	var method = 'GET';
	if (formObj.method.toLowerCase() == 'post') {
		method = 'POST';
	}
	// baseUrl='http://127.0.0.1/modules/Core/ajaxreturn.php';
	AJAXConnectServer(baseUrl, method, '', 'AJAXtest', true, async);
}

function AJAXSubmitFormObj(formObj, AJAXObj) {
	var paramArray = getFormValues(formObj);
	for ( var key in paramArray) {
		AJAXObj.setParam(key, paramArray[key]);
	}
	if (formObj.action) {
		AJAXObj.URL = formObj.action;
	}
	AJAXObj.send();
}

var smartAJAXPostJs = ''; // this var can be set in js from calling page
function smartAJAXReceived(resArray) {
	//alert(resArray['content']);
	var smartNode = document.getElementById(resArray['nodeID']);
	if (smartNode) {
		if (smartNode.tagName == 'TR' && window.ActiveXObject) {
			while (smartNode.childNodes[0]) {
				smartNode.removeChild(smartNode.childNodes[0]);
			}

			var doc = document.createElement('div');
			doc.innerHTML = '<table><tr>' + resArray['content'] + '</tr></table>';
			var tds = doc.getElementsByTagName('td');
			for ( var i = 0; i < tds.length; ++i) {
				var tdObj = tds[i].cloneNode(true);
				smartNode.appendChild(tdObj);
			}
		} else {
			smartNode.innerHTML = resArray['content'];
		}
	}
	if (resArray['postBody']) {
		document.body.innerHTML += resArray['postBody'];
	}
	if (resArray['postJs']) {
		if (window.execScript) {
			window.execScript(resArray['postJs']);
		} else {
			window.eval(resArray['postJs']);
		}
	}

}

function displayNode(node, padding) {
	var i;
	var padString = '';
	var html = '';
	if (padding == null)
		padding = 0;
	for (i = 0; i < padding; ++i)
		padString += ' ';
	html += padString + 'Type: ' + node.nodeType + '\n';
	html += padString + 'Name: ' + node.nodeName + '\n';
	html += padString + 'Value: ' + node.nodeValue + '\n';
	html += padString + 'Data: ' + node.data + '\n';
	html += padString + 'Children: ' + node.childNodes.length + '\n';
	if (node.childNodes.length) {

		for (i = 0; i < node.childNodes.length; ++i) {
			html += displayNode(node.childNodes[i], padding + 2);
		}
	}
	return html;
}

function splitXML(XML) {
	var result = Array();
	var rootNode = XML.getElementsByTagName('root')[0];
	for ( var i = 0; i < rootNode.childNodes.length; ++i) {
		result[rootNode.childNodes[i].tagName] = rootNode.childNodes[i].firstChild.nodeValue;
	}
	return result;
}

function AJAXConnection(URL, requestType, params, nameOfJSCallback, showBusy) {
	this.URL = URL;
	this.requestType = requestType;
	this.paramArray = Object();
	this.params = params;
	this.nameOfJSCallback = nameOfJSCallback;
	this.setURL = setURL;
	this.setAsync = setAsync;
	this.setRequestType = setRequestType;
	this.setParams = setParams;
	this.setParam = setParam;
	this.setNameOfJSCallback = setNameOfJSCallback;
	this.showBusy = showBusy;
	this.setShowBusy = setShowBusy;
	this.concatParams = concatParams;
	this.send = send;

	function setURL(URL) {
		this.URL = URL;
	}

	function setUrl(URL) {
		this.URL = URL;
	}
	function setAsync(async) {
		this.async = async;
	}

	function setRequestType(requestType) {
		this.requestType = requestType;
	}

	function setParams(params) {
		this.params = params;
	}

	function setParamArray(paramArray) {
		this.paramArray = paramArray;
	}

	function setParam(key, val) {
		this.paramArray[key] = val;
	}

	function setNameOfJSCallback(nameOfJSCallback) {
		this.nameOfJSCallback = nameOfJSCallback;
	}

	function setShowBusy(showBusy) {
		this.showBusy = showBusy;
	}

	function concatParams() {
		var string = '';
		for (key in this.paramArray) {
			string += key + '=' + encodeURIComponent(this.paramArray[key])
					+ '&';
		}
		return string;
	}
	function send() {
		var string = this.concatParams();
		if (string != '') {
			this.params = string + 'dummy=1';
		}
		if (this.requestType == null)
			return false;
		if (this.nameOfJSCallback == null) {
			return false;
		}
		if (this.async == null) {
			this.async = true;
		}
		AJAXConnectServer(this.URL, this.requestType, this.params,
				this.nameOfJSCallback, this.showBusy, this.async);
	}
}

function dojoReceived(responseObj, xhr) {
	if (responseObj.onArrival) {
		if (window.execScript) {
			window.execScript(responseObj.onArrival);
		} else {
			window.eval(responseObj.onArrival);
		}
	}
	/*
	 * dojo.forEach(responseObj.dijits, function(info) { var n =
	 * dojo.byId(info.id); if (null != n) { n.destroy(); } });
	 */
	currentDijits = responseObj.dijits;

	if (responseObj.nodeID) {
		var smartNode = document.getElementById(responseObj.nodeID);
		if (smartNode) {
			if (smartNode.tagName == 'TR' && window.ActiveXObject) {
				while (smartNode.childNodes[0]) {
					smartNode.removeChild(smartNode.childNodes[0]);
				}

				var doc = document.createElement('div');
				doc.innerHTML = '<table><tr>' + responseObj.content + '</tr></table>';
				var tds = doc.getElementsByTagName('td');
				for ( var i = 0; i < tds.length; ++i) {
					var tdObj = tds[i].cloneNode(true);
					smartNode.appendChild(tdObj);
				}
			} else {
				smartNode.innerHTML = responseObj.content;
			}
			// get the modules
			dojo.forEach(responseObj.dojomodules, function(module) {
				dojo.require(module);
			});

			dojo.forEach(responseObj.dijits, function(info) {
				var n = dojo.byId(info.id);
				if (null != n) {
					dojo.attr(n, dojo.mixin( {
						id :info.id
					}, info.params));
				}
			});
			dojo.parser.parse(smartNode);
		}
	}
	if (responseObj.postBody) {
		document.body.innerHTML += responseObj.postBody;
	}
	if (responseObj.postJs) {
		if (window.execScript) {
			window.execScript(reponseObj.postJs);
		} else {
			window.eval(reponseObj.postJs);
		}
	}
}
function dojoDestroyDialog(tag) {
	widget = dijit.byId(tag);
	if (widget) {
		widget.destroyRecursive();
	}
}
var dojoDisplayNodes = Array();
var currentDijits = Array();
function dojoDisplayGetContent(name, url, arguments) {
	arguments.nodeID = name + '_content';
	// remove old dijits in the Display
	/*
	 * dijit.registry.forEach(function(node) { for (var i = 0; i <
	 * currentDijits.length; ++i) { if (!currentDijits[i]) { continue; } if
	 * (currentDijits[i].id == node.id) { node.destroy(); currentDijits[i] = 0;
	 * return; } } });
	 */
	dojoHideAll();
	var o = document.getElementById(arguments.nodeID);
	if (o) {
		o.innerHTML = '<img src="/images/ajax-loader.gif">';
	}

	currentNode = dijit.byId(name);
	dojoDisplayNodes.push(currentNode);

	currentNode.show();

	dojo.xhrPost( {
		url :url,
		handleAs :'json',
		load :dojoReceived,
		content :arguments
	});

}

function dojoHideAll()
{
	for(var i = 0; i < currentDijits.length; ++i) {
		var n = dijit.byId(currentDijits[i].id);
		if (n) {
			dijit.byId(currentDijits[i].id).destroy();
		}
	}
	currentNode = dojoDisplayNodes.shift();
	if (currentNode) {
		currentNode.hide();
	}

}
var grayoutDialogs = new Array();
var grayoutVisibleDialogs = new Array();

function getGrayoutDialog(id) {
	for ( var i = 0; i < grayoutDialogs.length; ++i) {
		if (grayoutDialogs[i]._id == id) {
			return grayoutDialogs[i];
		}
	}
	return null;
}

function grayoutDialog(id) {
	this._id = id;
	this.display = display;
	this.hide = hide;
	this.sendBack = sendBack;
	this.sendFront = sendFront;
	this._AJAXLoader = null;
	this.setAJAXLoader = setAJAXLoader;
	this._content = '';
	this.setContent = setContent;
	grayoutDialogs.push(this);

	function setContent(content) {
		this._content = content;
	}

	function display(e) {
		if (document.getElementById(this._id)) {
			var d = document.getElementById(this._id);
			d.parentNode.removeChild(d);
		}

		document.getElementsByTagName('body')[0].innerHTML += this._content;
		if (grayoutVisibleDialogs.length) {
			grayoutVisibleDialogs[grayoutVisibleDialogs.length - 1].sendBack();
		}
		document.getElementById('grayout').style.display = 'block';
		var s = bodySize();
		document.getElementById('grayout').style.width = s.x + 'px';
		document.getElementById('grayout').style.height = s.y + 'px';
		document.getElementById(this._id).style.display = 'block';
		this.sendFront();
		var pos = mouseCoords(e);
		// make sure we dont go outside the window
		/*
		 * if (pos.y + document.getElementById(this._id).offsetHeight >
		 * windowSize().y) { pos.y = windowSize().y -
		 * document.getElementById(this._id).offsetHeight; }
		 * 
		 * if (pos.x + document.getElementById(this._id).offsetWidth >
		 * windowSize().x) { pos.x = windowSize().x -
		 * document.getElementById(this._id).offsetWidth; }
		 */
		document.getElementById(this._id).style.top = pos.y;
		document.getElementById(this._id).style.left = pos.x;
		// sendFront();
		if (this._AJAXLoader) {
			document.getElementById(this._id + 'Inner').innerHTML = '<br/><br/>&nbsp;&nbsp;Loading - please wait...<br/><br/>';
			this._AJAXLoader.setParam('nodeID', this._id + 'Inner');
			this._AJAXLoader.setAsync(true); // false
			this._AJAXLoader.send();
		}
		grayoutVisibleDialogs.push(this);
	}

	// Hide should only be called for the outermost window!
	function hide() {
		grayoutVisibleDialogs.pop();
		document.getElementById(this._id).style.display = 'none';

		if (!grayoutVisibleDialogs.length) {
			document.getElementById('grayout').style.display = 'none';
		} else {
			grayoutVisibleDialogs[grayoutVisibleDialogs.length - 1].sendFront();
		}
	}

	function sendBack() {
		document.getElementById(this._id).style.zIndex = parseInt(document
				.getElementById('grayout').style.zIndex) - 1;
	}

	function sendFront() {
		document.getElementById(this._id).style.zIndex = document
				.getElementById('grayout').style.zIndex + 1;

	}

	function setAJAXLoader(AJAXLoader) {
		this._AJAXLoader = AJAXLoader;
	}
}

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 16;
	var randomstring = '';
	for ( var i = 0; i < string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum, rnum + 1);
	}
	return randomstring;
}

function rand(n) {
	return (Math.floor(Math.random() * n + 1));
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


var TreeAJAX = null;
function changeTree(command, id, treeID) {
	if (!TreeAJAX) {
		TreeAJAX = new AJAXConnection(
				BaseUrl + '/modules/Core/Tree/changestate');
		TreeAJAX.setAsync(true);
		TreeAJAX.setRequestType('POST');
		TreeAJAX.setShowBusy(true);
		TreeAJAX.setNameOfJSCallback('treeReceived');
		TreeAJAX.setParam('treeID', treeID);
		TreeAJAX.setParam('simpleajax', '1');
	}
	TreeAJAX.setParam('ajaxID', id);
	TreeAJAX.setParam('command', command);
	TreeAJAX.send();
}

function treeReceived(resArray) {
	ajaxID = resArray['ajaxID'];
	command = resArray['command'];
	switch (command) {
	case 'expand':
	case 'expandFromHere':
		document.getElementById('col' + ajaxID).style.display = 'inline';
		document.getElementById('exp' + ajaxID).style.display = 'none';
		document.getElementById('reftr' + ajaxID).style.display = '';
		document.getElementById('line' + ajaxID).style.backgroundImage = 'url(' + BaseUrl + '/modules/Core/images/line.png)';
		document.getElementById('ref' + ajaxID).innerHTML = resArray['content'];
		break;
	case 'collapse':
	case 'collapseFromHere':
		document.getElementById('exp' + ajaxID).style.display = 'inline';
		document.getElementById('col' + ajaxID).style.display = 'none';
		document.getElementById('reftr' + ajaxID).style.display = 'none';
		document.getElementById('line' + ajaxID).style.backgroundImage = 'none';
		break;
	}
}
