treeview.prototype.load = function(){
	if(this.loaded || this.loading){
		return;
	}
	
	this.loading = true;
	this.setLoading();
	loadXML(this);
}
treeview.prototype.setLoading = function(){
	this.loadNode = new treenode('loading treeview data',this);
}

treeview.prototype.onloaded = function(){
	this.loadNode.remove();
	this.loaded = true;
	this.loading = false;
}

treeview.prototype.onloadedEmpty = function(){
	this.loadNode.remove();
	this.loaded = false;
	this.loading = false;
}

treeview.prototype.onError = function(){
	this.loadNode.oNodeIcon.src = treeConfig.errorIcon;

	switch(this.error.type){
		case 'undefined':
			this.loadNode.setLabel('Error: ' + this.error.src + ' ' + this.error.status + ' ' + this.error.statusText + '');
		break;
		case 'parser':
			this.loadNode.setLabel('Error: ' + this.error.src + ' ' + this.error.reason + ' ' + this.error.srcText + ' ' + this.error.errorCode + '');
		break;
	}
}

treenode.prototype.reload = function(){
	if(this.src){
		this.loaded = false;
		
		// hard index op 0 zetten want i verandert na elke remove
		for(i=this.oNode.oNodeChilds.childNodes.length;i>0;i--){
			this.oNode.oNodeChilds.childNodes[i-1].obj.remove(true);
		}
		
		this.loading = true;
		this.setLoading();
		loadXML(this);
	}else{
		alert('treenode method reload says: no datasource defined');
	}
}
treenode.prototype.hasDatasource = function(){
	if(this.src != null){
		return true;
	}else{
		return false;
	}
}
treenode.prototype.setDatasource = function(sUrl){
	this.src = sUrl;
}
treenode.prototype.load = function(){
	if(this.loaded || this.loading){
		return;
	}
	
	if(this.src){
		this.loading = true;
		this.setLoading();
		loadXML(this);
	}else{
		alert('treenode method load says: no datasource defined');
	}
}

treenode.prototype.setLoading = function(){
	//this.oNode.oNodeChilds.style.display = 'block';
	this.originalIcon = this.oNode.oNodeIcon.src;
	this.oNode.oNodeIcon.src = treeConfig.loadIcon;
	
	//this.loadNode = new treenode('loading treenode data',this);
}
treenode.prototype.onloaded = function(){
	// use the disableBehaviour attribute to prevent selecting the first childNode
	// this.loadNode.remove(true); unneeded because the expand method sets a new icon
	
	this.loaded = true;
	this.loading = false;
	this.expand();
	if(this.treeview.selectOnReload){
		this.select();
	}
}

treenode.prototype.onloadedEmpty = function(){
	this.loaded = true;
	this.loading = false;
	
	// remove plus icons there are no childs
	// set L icon if this node is the last in the current level
	if(this.getNextSibling()){
		this.oNodeImg.src = treeConfig.tIcon;
	}else{
		this.oNodeImg.src = treeConfig.lIcon;
	}
	this.oNode.oNodeIcon.src = this.originalIcon; // set in setLoading
	this.originalIcon = null;
}

treenode.prototype.onError = function(){
	this.oNode.oNodeIcon.src = treeConfig.errorIcon;
	
	switch(this.error.type){
		case 'undefined':
			this.setLabel('Error: ' + this.error.src + ' ' + this.error.status + ' ' + this.error.statusText + '');
		break;
		case 'parser':
			this.setLabel('Error: ' + this.error.src + ' ' + this.error.reason + ' ' + this.error.srcText + ' ' + this.error.errorCode + '');
		break;
	}
}

function loadXML(parentNode){
	
	var xmlHttp = XmlHttp.create();

	// prevent caching by browser, add &nocache if ? exists, else ?nocache
	var xmlUrl = parentNode.src 
	if(parentNode.src.indexOf('?') < 0){
		xmlUrl += '?nocache='+new Date().getTime();
	}else{
		xmlUrl += '&nocache='+new Date().getTime();
	}

	xmlHttp.open("GET", xmlUrl, true);
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4) {
			onXMLLoaded(xmlHttp,parentNode);
		}
	};
	
	//window.setTimeout(function () {
		xmlHttp.send(null);
	//}, 10);
}

function onXMLLoaded(oXML,parentNode) {
	
	var oXmlDoc = oXML.responseXML;
	var error;
	
	if(!oXmlDoc || oXmlDoc.parserError && oXmlDoc.parseError.errorCode != 0 || !oXmlDoc.documentElement){
		if (!oXmlDoc || oXmlDoc.parseError.errorCode == 0){
			error = {
				src : parentNode.src,
				status : oXML.status,
				statusText : oXML.statusText,
				type : 'undefined'
			}
		}else{
			error = {
				src : parentNode.src,
				reason : oXmlDoc.parseError.reason,
				srcText : oXmlDoc.parseError.srcText,
				errorCode : oXmlDoc.parseError.errorCode,
				type : 'parser'
			}
		}
		parentNode.error = error;
		parentNode.onError();
	}else{
		var root = oXmlDoc.documentElement;
		var cs = root.childNodes;
		var l = cs.length;
					
		for (var i = 0; i < l; i++) {
			if (cs[i].tagName == "tree") {
				xmlNodeToJsNode(cs[i],parentNode);
			}
		}
		
		if(cs.length <= 0){
			// remove plus icons, this prevents overlapping of background dots forming a solid line
			parentNode.onloadedEmpty();
		}else{
			parentNode.onloaded();
		}
	}
	
	delete oXML.onreadystatechange;
	//oXML.abort();
	oXML = null;
	delete oXML;
	
}
function xmlNodeToJsNode(oNode,parentNode) {
	
	var label = oNode.getAttribute("label") ? oNode.getAttribute("label") : treeConfig.defaultLabel;
	var src = oNode.getAttribute("src") ? oNode.getAttribute("src") : null;
	var href = oNode.getAttribute("href") ? oNode.getAttribute("href") : treeConfig.defaultAction;
	var target = oNode.getAttribute("target") ? oNode.getAttribute("target") : null; 
	var icon = oNode.getAttribute("icon") ? oNode.getAttribute("icon") : treeConfig.defaultIcon;
	var iconOpen = oNode.getAttribute("iconopen") ? oNode.getAttribute("iconopen") : treeConfig.defaultIconOpen;
	var additionalicons = oNode.getAttribute("additionalicons") ? oNode.getAttribute("additionalicons") : '';
	var onloadevent = oNode.getAttribute("onloadevent") ? oNode.getAttribute("onloadevent") : null;
	var parent = null;
	
	var newNode = new treenode(label,parentNode,src,href,target,icon,iconOpen,additionalicons,onloadevent);
			
	var cs = oNode.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		if (cs[i].tagName == "tree") {
			xmlNodeToJsNode(cs[i],newNode);
		}
	}
	// scope xml attributes
	newNode.xmlNode = oNode;
	
}
