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 Major Networks...',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;
}

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();
}

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
}

treenode.prototype.onException = function(){
	this.oNode.oNodeIcon.src = treeConfig.errorIcon;
}

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();
	}

	// async must be true for mozilla!!!
	xmlUrl = prepareSafari(xmlUrl);	// need to do this replacement for Safari.
	xmlHttp.open("GET", xmlUrl, true);

	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4) {
			
			///if(oXmlDoc == null || oXmlDoc.documentElement == null){
				//var oXmlDoc = xmlHttp.responseXML
				//oXmlDoc.loadXML(xmlHttp.responseText);
			//}
			
			onXMLLoaded(xmlHttp,parentNode);
		}
	};
	
	//window.setTimeout(function () {
		xmlHttp.send(null);
	//}, 10);
}

function onXMLLoaded(oXML,parentNode) {

	var oXmlDoc = oXML.responseXML;
	var error;
	
	if( oXmlDoc == null || oXmlDoc.documentElement == null) {
		alert('onXMLLoaded function returned error: the file '+parentNode.src+' could not be read.');
		parentNode.onException();
	}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 small line
			parentNode.onloadedEmpty();
		}else{
			parentNode.onloaded();
		}
	}
}
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 strType = oNode.getAttribute("strType") ? oNode.getAttribute("strType") : '';
	var lngId = oNode.getAttribute("lngId") ? oNode.getAttribute("lngId") : '';
	var mainLink = oNode.getAttribute("mainLink") ? oNode.getAttribute("mainLink") : '';
	var topLink = oNode.getAttribute("topLink") ? oNode.getAttribute("topLink") : '';
	var newLink = oNode.getAttribute("newLink") ? oNode.getAttribute("newLink") : '';
	var issueLink = oNode.getAttribute("issueLink") ? oNode.getAttribute("issueLink") : '';
	var infoLink = oNode.getAttribute("infoLink") ? oNode.getAttribute("infoLink") : '';
	var onloadevent = oNode.getAttribute("onloadevent") ? oNode.getAttribute("onloadevent") : null;
	var parent = null;
	
	var newNode = new treenode(label,parentNode,src,href,target,icon,iconOpen,additionalicons,strType,lngId,mainLink,topLink,newLink,issueLink,infoLink,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);
		}
	}
	
	// zorgt ervoor dat alle attributen van deze node worden toegevoegd aan de parameter nodeAttributes
	newNode.xmlNode = oNode;
}