 
instantaspTree = new Object();
instantaspTreeIc = new Object();

function InstantASPTree(TreeID) { 
 this.TreeID = TreeID;
 this.Options = new Options();
 this.Icons = new Icons();
 this.NodeList = new Object();
 this.ContextMenu = null;
 this.NodeContextMenu = new Object();
 this.TreeReference="";
 this.RootNode = null;
 this.SelectedNode = null;
 this.Transition = null;
 this.EditId = null;
 this.NodeCount = 0; 
 this.XmlHttp = null;
 if (instantaspTree[this.TreeID]!=null) {
  alert("The tree with id "+this.TreeID+" already exist, please change the tree id.");
 } else { 
  instantaspTree[this.TreeID]=this;
 }
 return this;
}
 
function Icons(path) {
 if (path != undefined) {
	 this.pnb=path+"Lines/TreeView_Plusnb.gif";
	 this.pb=path+"Lines/TreeView_Plusb.gif";
	 this.pr=path+"Lines/TreeView_Plusr.gif";
	 this.mnb=path+"Lines/TreeView_Minusnb.gif";
	 this.mb=path+"Lines/TreeView_Minusb.gif";
	 this.mr=path+"Lines/TreeView_Minusr.gif";
	 this.opf=path+"TreeView_FolderOpen.gif";
	 this.clf=path+"TreeView_Folder.gif";
	 this.chd=path+"TreeView_Leaf.gif";
	 this.rot=path+"Lines/TreeView_Root.gif";
	 this.lnb=path+"Lines/TreeView_LineAng.gif";
	 this.lb=path+"Lines/TreeView_LineInts.gif";
	 this.lin=path+"Lines/TreeView_Line.gif";
	 this.bln=path+"Lines/TreeView_Blank.gif"; 
	 this.spc=path+"Lines/TreeView_Spacer.gif"; 
	 preloadIcon(this.pnb,this.pb,this.mnb,this.mb,this.opf,this.clf,this.chd,this.rot,this.lnb,this.lb,this.lin,this.bln);
	 this.toString = function() {return"Standard Icons"};
	 return this;
 }
}

function Options() { 
 this.Target = "_self";
 this.Sort = "no";
 this.Icons=true;
 this.Check=false;
 this.Editable=false;
 this.SelectRow=false;
 this.EditKey=113;
 this.EnableContext=true;
 this.OneExpand = false;
 this.OneClick=false;
 this.EnableCookie=false;
 this.ShowRoot = true;
 this.LoadingText="Loading...";
 this.ExpandedListTextBoxID = "";
 this.SelectedNodeTextBoxID = "";
 this.TabID = 0;
 this.LiveWriteCallBackURL = "";
 this.LiveDeleteCallBackURL = "";
 this.LiveMoveUpCallBackURL = "";
 this.LiveMoveDownCallBackURL = "";
 this.NodeEditURL = "";
 this.Expanded = false;
  return this;
}
 
function InstantASPTreeNode(NodeID, Text, NavigateURL, Icon, Expanded, Checked, CallBackURL, CategoryID) { 
 this.NodeID=NodeID;
 this.ID="";
 this.Text = Text;
 this.NavigateURL = (NavigateURL==null||NavigateURL=="")?"javascript:void(0)":NavigateURL;
 this.Icon = (Icon == null || Icon == "")?null:Icon.split(",");
 this.Expanded = Expanded==null?false:Expanded;
 this.Checked = (Checked?Checked:false);
 this.CallBackURL = CallBackURL==""?null:CallBackURL;
 this.ContextMenu=null;
 this.cstStyle="";
 if (this.Icon) {preloadIcon(this.Icon[0]); if (this.Icon.length>1) {preloadIcon(this.Icon[1]);}} 
 this.NextNode=null;
 this.PrevNode=null;
 this.FirstChild=null;
 this.LastChild=null;
 this.ParentNode=null;
 this.LoadingOnDemand = false;
 this.CategoryID = CategoryID==undefined?0:CategoryID; 
 this.equals=function(nd) {return(this.ID==nd.ID);};
 return this;
}

InstantASPTree.prototype.genIntTreeID = function(id) {
 return this.TreeID+id;
}

InstantASPTree.prototype.genOrgId = function(inTreeID) { 
 return inTreeID.substr(this.TreeID.length);
}

InstantASPTree.prototype.compareNode = function(aN,bN) { 
 return(aN.Text>=bN.Text);
}

InstantASPTree.prototype.add = function(id, prn, capt, url, ic, exp, chk, callbackurl, categoryid) {
 var nNd = new InstantASPTreeNode(((id==null||id=="")?("n_"+(++this.NodeCount)):id), capt, url, ic, exp, chk, callbackurl, categoryid);
 nNd.ID = this.genIntTreeID(nNd.NodeID);
 if (this.NodeList[nNd.ID]!=null) {alert("Item with id "+id+" already exist");return;}
 this.NodeList[nNd.ID]=nNd;
 if(this.RootNode==null) {
  this.RootNode=nNd
 } else {
  var pnd = this.NodeList[this.genIntTreeID(prn)];
  if (pnd != null) {
  nNd.ParentNode = pnd;
	if (pnd.LastChild == null) {
		pnd.FirstChild = nNd; 
		pnd.LastChild = nNd
	} else {
		var t = pnd.FirstChild;
		if(this.Options.Sort != "no") {
		   do { 
		    if (this.Options.Sort == "asc"?this.compareNode(t,nNd):this.compareNode(nNd,t)) {break;} 
			t=t.NextNode;
			}
		   while (t!=null); 
			if(t!=null) {
				if (t.PrevNode==null) {
				 t.PrevNode=nNd;
				 pnd.FirstChild=nNd
				} else {
				 nNd.PrevNode=t.PrevNode;
				 t.PrevNode.NextNode=nNd;
				 t.PrevNode=nNd;
				} 
			 nNd.NextNode=t;
		   } 
	   }
		if(this.Options.Sort=="no"||t==null) {
		 nNd.PrevNode=pnd.LastChild; 
		 pnd.LastChild.NextNode=nNd; 
		 pnd.LastChild=nNd;
		}
   }
   
  }
  
 }
  return nNd
}

InstantASPTree.prototype.addBefore = function(id,sib,capt,url,ic,exp,chk,callbackurl) {
 var nd=this.getNodeById(sib);
 if(nd==null) {return;}
 var nNd = new InstantASPTreeNode(((id==null||id=="")?("int"+(++this.NodeCount)):id),capt,url,ic,exp,chk,callbackurl);
 nNd.ID = this.genIntTreeID(nNd.NodeID);
 if(this.NodeList[nNd.ID]!=null) {alert("Item with id "+id+" already exist");return;} 
 this.NodeList[nNd.ID] = nNd;
 nNd.ParentNode = nd.ParentNode; 
 nNd.NextNode = nd;
 if(nd.PrevNode==null) {
  nd.PrevNode=nNd; 
  nd.ParentNode.FirstChild = nNd;
  } else { 
  nNd.PrevNode = nd.PrevNode; nd.PrevNode.NextNode = nNd; nd.PrevNode=nNd;
  }
 return nNd
}

InstantASPTree.prototype.addAfter = function(id,sib,capt,url,ic,exp,chk,callbackurl) { 
 var nd = this.getNodeById(sib);
 if(nd==null) {return};
 var nNd = new InstantASPTreeNode(((id==null||id=="")?("int"+(++this.NodeCount)):id),capt,url,ic,exp,chk,callbackurl);
 nNd.ID = this.genIntTreeID(nNd.NodeID);
 if(this.NodeList[nNd.ID]!=null){alert("Item with id "+id+" already exist"); return;}
 this.NodeList[nNd.ID] = nNd; 
 nNd.ParentNode = nd.ParentNode; 
 nNd.PrevNode = nd;
 if (nd.NextNode==null) {nd.NextNode=nNd;nd.ParentNode.LastChild=nNd} 
 else {nNd.NextNode = nd.NextNode;nd.NextNode.PrevNode=nNd;nd.NextNode=nNd}
 return nNd
}

InstantASPTree.prototype.append = function(id,prn,capt,url,ic,exp,chk,callbackurl) { 
 var nd = this.add(id,prn,capt,url,ic,exp,chk,callbackurl);
 this.reloadNode(prn);
 return nd
}

InstantASPTree.prototype.remove = function(id,reload) {
 var rNd = (id!=null?this.NodeList[this.genIntTreeID(id)]:this.SelectedNode);
	if (rNd!=null) {
		if (this.RootNode.equals(rNd)) {this.RootNode=null; this.NodeList=new Object(); this.SelectedNode=null; return rNd};
		if (this.SelectedNode!=null) {if (rNd.equals(this.SelectedNode)) {this.SelectedNode=null;}}
		var pr = rNd.ParentNode;
		if (pr.LastChild.equals(rNd)) {pr.LastChild = rNd.PrevNode;}
		if (pr.FirstChild.equals(rNd)) {pr.FirstChild = rNd.NextNode;}
		if (rNd.PrevNode!=null) {rNd.PrevNode.NextNode = rNd.NextNode;}
		if (rNd.NextNode!=null) {rNd.NextNode.PrevNode = rNd.PrevNode;}
		rNd.NextNode = null; rNd.PrevNode = null; rNd.ParentNode = null;
		var treeId = this.TreeID;
		this.loopTree(rNd, function(n) {instantaspTree[treeId].NodeList[n.ID]=null});
		if (reload==null||reload) {this.reloadNode(this.genOrgId(pr.ID));}
	}
 return rNd;
}

InstantASPTree.prototype.removeChilds = function(id, reload) {
 var rNd = (id!=null?this.NodeList[this.genIntTreeID(id)]:this.SelectedNode);
 if (rNd != null) {
  while (rNd.FirstChild) {this.remove(rNd.FirstChild.NodeID, false);}
   if (reload==null||reload) {this.reloadNode(id);}
 }
}

InstantASPTree.prototype.removeLoading = function(id) {
 var rNd = (id!=null?this.NodeList[this.genIntTreeID(id)]:this.SelectedNode);
 if (rNd != null) {
  if (rNd.FirstChild!=null) {
   if (rNd.FirstChild.Text == this.Options.LoadingText) {this.remove(rNd.FirstChild.NodeID, false);}
 }
}
}

InstantASPTree.prototype.getSelNode = function() {
 return this.SelectedNode
}


InstantASPTree.prototype.genANode = function(sNd) {
 var ev="";
 var st="";
 var cm="";
 var treeName=this.TreeReference+"instantaspTree."+this.TreeID;
 var ip=(sNd.NextNode!=null?this.Icons.lb:this.Icons.lnb);
 var sv=treeName+".selectNode(\""+sNd.ID+"\");";
 var cm=treeName+".contextMenu(event, \""+sNd.ID+"\");";
 var cn=treeName+".checkNode(\""+sNd.ID+"\");";
 if (sNd.FirstChild||sNd.CallBackURL!=null) {
  ev=treeName+".ParentNodeepareToggle(\""+sNd.ID+"\");"+treeName+".toggleNode(\""+sNd.ID+"\");";  
  st=(sNd.NavigateURL=="javascript:void(0)"?treeName+".selNToggle(\""+sNd.ID+"\");":"");
  ip=(sNd.NextNode!=null?(sNd.Expanded?(!this.Options.ShowRoot&&sNd.ParentNode.NodeID==1&&!sNd.PrevNode?this.Icons.mr:this.Icons.mb):(!this.Options.ShowRoot&&sNd.ParentNode.NodeID==1&&!sNd.PrevNode?this.Icons.pr:this.Icons.pb)):(sNd.Expanded?this.Icons.mnb:this.Icons.pnb));       
 } else {sNd.Expanded=false;}
 
  var s=(sNd.ParentNode==null?"":"<img id=ip_"+sNd.ID+" src='"+ip+"' "+(sNd.FirstChild==null&&sNd.CallBackURL==null?"":"onclick='"+ev+"'")+" class='tree' />");
  if(this.Options.Icons||sNd.equals(this.RootNode)) {
   var evl="' onclick='"+(sNd.FirstChild&&this.Options.OneClick?st:sv)+" return "+treeName+".treeOnClick(event);' oncontextmenu='return "+cm+"' ondblclick='"+st+" return "+treeName+".treeOnDblClick(event)' onmouseover='"+treeName+".treeOnMouseOver(event)' onmousemove='"+treeName+".treeOnMouseMove(event)' onmouseout='"+treeName+".treeOnMouseOut(event)' onmousedown='"+treeName+".treeOnMouseDown(event)' onmouseUp='"+treeName+".treeOnMouseUp(event)' />";
   s+=("<img id='ic_"+sNd.ID+"' class='" + (sNd.PrevNove?"tree":"treeroot") + "' src='"+this.toggleNodeImage(sNd)+evl);
  }
  s+=(this.Options.Check?"<td><input style='height:14px;margin-top:0px;margin-bottom:0px;padding:0px' type='checkbox' id=cb_"+sNd.ID+" "+(sNd.Checked?"checked":"")+" onclick='"+cn+treeName+".treeOnCheck("+sNd.NodeID+")'></td>":"")+"</td><td><a target=\""+this.Options.Target+"\" href=\""+sNd.NavigateURL+"\" id='ac_"+sNd.ID+"' class='"+(sNd.FirstChild?"prnnode":"node")+"' onclick='"+(sNd.FirstChild&&this.Options.OneClick?st:sv)+" return "+treeName+".treeOnClick(event);' oncontextmenu='return "+cm+"' ondblclick='"+st+" return "+treeName+".treeOnDblClick(event)' onmouseover='"+treeName+".treeOnMouseOver(event)' onmousemove='"+treeName+".treeOnMouseMove(event)' onmouseout='"+treeName+".treeOnMouseOut(event)' onmousedown='"+treeName+".treeOnMouseDown(event)' onmouseup='"+treeName+".treeOnMouseUp(event)'><span id='cstl_"+sNd.ID+"' "+(sNd.cstStyle!=""?"class='"+sNd.cstStyle+"'":"")+" >"+sNd.Text+"</span></a></td>";
  var n=sNd.ParentNode;
  while (n!=null&&!n.equals(this.RootNode)) 
  {s="<img src='"+(n.NextNode!=null?this.Icons.lin:this.Icons.bln)+"' class='tree' />"+s;
  n=n.ParentNode
  }
  s="<td>"+s;
  s="<table cellpadding='0' cellspacing='0'><tr>"+s+"</tr></table>";
  if (sNd.ContextMenu&&sNd.ContextMenu.mId && !this.NodeContextMenu[sNd.ContextMenu.mId]) 
   {s+=sNd.ContextMenu.genMenu();this.NodeContextMenu[sNd.ContextMenu.mId]=sNd.ContextMenu.mId}
  return s;
 
}
    
InstantASPTree.prototype.genNodes = function(sNd,incpar,wrt) { 
 var s = "";
 if (!this.Options.ShowRoot && sNd.equals(this.RootNode)) { // root node is disabled
    s = "<div id='"+sNd.ID+"' class='row'></div><div id='ch_"+sNd.ID+"'>";
 } 
 else { 
    s = incpar?("<div id='"+sNd.ID+"' class='row'>"+this.genANode(sNd)+"</div><div style='display:"+(sNd.FirstChild&&sNd.Expanded?"inline":"none")+"; visibility:"+(sNd.FirstChild&&sNd.Expanded?"visible":"hidden")+";' id='ch_"+sNd.ID+"'>"):"";
 }
 if (wrt) document.write(s);
 if (sNd.FirstChild != null) {
  var chNode = sNd.FirstChild;
  do { 
   if (wrt) {
    this.genNodes(chNode,true,wrt);
   } else { 
    s=s+this.genNodes(chNode,true,wrt);
   } 
   chNode=chNode.NextNode;
  } 
  while (chNode!=null)
 }
 if (wrt) { 
    if (incpar) document.write("</div>"); return"";
 } else {
    s=incpar?(s+"</div>"):s; return s;
 }
}
  
InstantASPTree.prototype.genTree = function() { 
 return this.genNodes(this.RootNode,true,false) + "<input id='ndedt"+this.TreeID+"' type='text' class='nodeedit' style='display:none;' value='' onblur='instantaspTree."+this.TreeID+".liveNodeWrite()'>" + (this.ContextMenu?this.ContextMenu.genMenu():"") + (!InstantASP_FindControl("ddGesture")?"<div id='ddGesture' style='display:none;' class='ddGesture'></div>":"");
}

InstantASPTree.prototype.render = function(plc) {
 if (plc && plc!="") {
  InstantASP_FindControl(plc).innerHTML = this.genTree();
 } else { 
 document.write("<div class='reeviewContainer'>");
  this.genNodes(this.RootNode,true,true);
  document.write("<input id='ndedt"+this.TreeID+"' type='text' class='nodeedit' style='display:none;' value='' onblur='instantaspTree."+this.TreeID+".liveNodeWrite()' />"+(this.ContextMenu?this.ContextMenu.genMenu():"")+(!InstantASP_FindControl("ddGesture")?"<div id='ddGesture' style='display:none;' class='ddGesture'></div>":""));
  document.write("</div>");
  
 }
 this.initEvent();
 if(this.Options.EnableCookie && this.getCookie) {
 var sid = this.getCookie(this.TreeID+"_selnd");
 if (sid && sid != "") {this.selectNodeById(sid);}
 }
}

InstantASPTree.prototype.initEvent = function() {
 var isIE=(navigator.userAgent.indexOf("MSIE")>=0);
 var orgEvent=(isIE?document.body.onkeydown:window.onkeydown);
 if(!orgEvent||orgEvent.toString().search(/orgEvent/gi)<0) {
	 var newEvent=function(e){if(instantaspTree.selectedTree) {instantaspTree.selectedTree.liveNodePress(isIE?event:e);}
	 if(orgEvent) {return orgEvent();}};
	 if (isIE) {document.body.onkeydown=newEvent;}
	 else {window.onkeydown=newEvent;}
 }
}

InstantASPTree.prototype.reloadNode = function(id) { 
 var inTreeID = this.genIntTreeID(id);
 var s = this.genNodes(this.NodeList[inTreeID],false);
 var dvN = InstantASP_FindControl("ch_"+inTreeID); 
  dvN.innerHTML=s;
  if (dvN.innerHTML=="") {
   dvN.style.display="none"; 
   s=this.genANode(this.NodeList[inTreeID]); 
   dvN=InstantASP_FindControl(inTreeID);
   dvN.innerHTML=s;
  }
 if(this.SelectedNode!=null) { 
  var sId=this.SelectedNode.ID;
  this.SelectedNode=null;
  this.selectNode(sId);
 }
}

InstantASPTree.prototype.selNToggle = function(id) {
 this.toggleNode(id);
 if (!this.SelectedNode||this.SelectedNode.ID!=id) {this.selectNode(id);}
 if (this.EditId!=null) {clearTimeout(this.EditId);this.EditId=null}
}

InstantASPTree.prototype.selectNode = function(id) { 
 instantaspTree.selectedTree=this;
 if (this.Options.Editable) {
  if (this.SelectedNode!=null && this.SelectedNode.ID!=id) {
   if (this.EditId) {clearTimeout(this.EditId);this.EditId=null}
  }
  if (this.SelectedNode!=null&&this.SelectedNode.ID==id) {
   this.EditId=setTimeout("instantaspTree."+this.TreeID+".liveNodeEdit('"+id+"')",500);
  }
  if (InstantASP_FindControl("ndedt"+this.TreeID).style.display=="") { 
   var edt=InstantASP_FindControl("ndedt"+this.TreeID);
   edt.style.display="none";
   edt.disabled=true
  }
 }

 var ac=null; var ic=null;var sNd=null;
 sNd = this.SelectedNode;
 if (sNd!=null) {
  if (this.Options.SelectedRow)
   InstantASP_FindControl(sNd.ID).className = "row"; 
   ac = InstantASP_FindControl("ac_"+sNd.ID);
  if (this.Options.Icons) { 
   var ic=InstantASP_FindControl("ic_"+sNd.ID);
   if (ic) {ic.style.display="none"; InstantASP_FindControl("ic_"+sNd.ID).style.display=""}
  }
  if (ac) {ac.className=(sNd.FirstChild?"prnnode":"node");}
 }
 
 sNd=this.NodeList[id]; 
 this.SelectedNode=sNd;
  if (this.Options.SelectRow) 
  InstantASP_FindControl(id).className="selrow";
  ac=InstantASP_FindControl("ac_"+id);
  if(this.Options.Icons) {
   var ic=InstantASP_FindControl("ic_"+id);
   if (ic) {InstantASP_FindControl("ic_"+id).style.display="none";ic.style.display=""}
  }
  if (ac) {ac.className=(sNd.FirstChild?"selprnnode":"selnode");}
  if (this.Options.EnableCookie && this.setCookie) 
  this.setCookie(this.TreeID+"_selnd",sNd.NodeID);
}

InstantASPTree.prototype.selectNodeById = function(id) { 
 var node = this.getNodeById(id);
 if (!node) {return};
 var tmp = node; 
 while (tmp.ParentNode!=null) {this.expandNode(tmp.NodeID); tmp = tmp.ParentNode}
 this.selectNode(node.ID);
}

InstantASPTree.prototype.isChild = function(c,p) { 
	var nd = this.getNodeById(c);
	if(!nd) {return false};
	var tmp=nd.ParentNode; 
	while(tmp!=null){if (tmp.NodeID==p) {return true;} tmp=tmp.ParentNode;}
	return false
}

InstantASPTree.prototype.hasChild = function(id) {
 var nd=this.getNodeById(id);
 return(nd.FirstChild!=null);
}

InstantASPTree.prototype.expandNode = function(id) { 
 var tmpTransition = null;
 var sNd = this.NodeList[this.genIntTreeID(id)];
 if (this.Transition!=null) {tmpTransition = this.Transition; this.Transition=null;}
 if (!sNd.Expanded) {this.toggleNode(sNd.ID);}
 if (tmpTransition!=null) {this.Transition=tmpTransition;};
}

InstantASPTree.prototype.collapseNode = function(id) { 
 var sNd = this.NodeList[this.genIntTreeID(id)];
 if (this.Transition!=null) {tmpTransition = this.Transition; this.Transition=null;}
 if(sNd.Expanded) {this.toggleNode(sNd.ID);}
 if (tmpTransition!=null) {this.Transition=tmpTransition;};
}

InstantASPTree.prototype.ParentNodeepareToggle = function(id) { 
 var sNd = this.SelectedNode;
 if (sNd==null) {this.selectNode(id); return;}
 if (sNd.ID==id) return;
 while (sNd!=null && sNd.ID!=id) {sNd=sNd.ParentNode;}
 if (sNd==null) {return;}
 if (sNd.ID==id) {this.selectNode(id);}
 
}

InstantASPTree.prototype.toggleNode = function(id) { 
 var nd=InstantASP_FindControl("ch_"+id);
 var ip=InstantASP_FindControl("ip_"+id);
 var ic=InstantASP_FindControl("ic_"+id);
 var sNd = this.NodeList[id];

 if (sNd.Expanded) { 
  sNd.Expanded=false;
  if (this.Transition!=null) {
   nd.style.height = '';
   this.Transition.CollapseDiv(nd);
  } else { 
   nd.style.display='none';
   nd.style.visibility = 'hidden';
  }
  if (ip!=null) {
   ip.src=(sNd.NextNode?this.Icons.pb:this.Icons.pnb);
  }

  if (ic!=null) {ic.src = this.toggleNodeImage(sNd);}
  this.treeOnCollapse(sNd.NodeID);
 } else { 
    if (this.Options.OneExpand && sNd.ParentNode) {
       var tNd=sNd.ParentNode.FirstChild; 
       while(tNd) { 
        if(tNd.id!=id&&tNd.Expanded) {
            this.collapseNode(tNd.NodeID);
	    }
       tNd=tNd.NextNode
       }
  }
  sNd.Expanded=true; 
   if (this.Transition!=null) {
   nd.style.height = '';
    this.Transition.ExpandDiv(nd);
   } else {
    nd.style.display='block';
	nd.style.visibility = 'visible';
   }

   if (ip!=null && (sNd.FirstChild != null||sNd.CallBackURL!=null)) {
    ip.src=(sNd.NextNode?this.Icons.mb:this.Icons.mnb);
   }
   
   if (ic!=null) {ic.src = this.toggleNodeImage(sNd);}
   this.treeOnExpand(sNd.NodeID);
 }
}

InstantASPTree.prototype.fetchContent = function(id) {
	var sNd = this.NodeList[id];
	if (sNd!=null) {
		if (sNd.LoadingOnDemand) {
		 return;
		} else {
		if (sNd.CallBackURL!=null&&sNd.FirstChild==null) {this.populatefetchedContent(id);}
		}
	}
}

InstantASPTree.prototype.populatefetchedContent = function(id) {
    var sNd = this.NodeList[id];
	var nd=InstantASP_FindControl("ch_"+id);
	var arrURL = sNd.CallBackURL.split("?");
	var tree = this; // can't use this within HttpHandler method
	
	tree.add(null, sNd.NodeID, this.Options.LoadingText, "", "", false, false, "");
	tree.reloadNode(sNd.NodeID); 
	
    if (!this.XmlHttp) {this.XmlHttp = new InstantASP_XmlHttpRequest();}
      var objXmlHttpHandler = function (obj) {
		sNd.LoadingOnDemand = true;
        if (obj.responseXML != null) {
			var items = obj.responseXML.getElementsByTagName("Node");
			if (items.length>0) {
				tree.removeLoading(sNd.NodeID);
				for (var i = 0; i<items.length;i++) {
					var nd = items[i];
					var capt = nd.getAttribute("Text")==null?"":nd.getAttribute("Text"); 
					if (capt=="") {capt = nd.getAttribute("TextNonLocalized");}
					var url = nd.getAttribute("NavigateURL")==null?"":nd.getAttribute("NavigateURL");
					var icon = nd.getAttribute("ImageURL")==null?"":nd.getAttribute("ImageURL");
					var expandedicon = nd.getAttribute("ExpandedImageUrl")==null?"":nd.getAttribute("ExpandedImageUrl");
					if (icon!='') {expandedicon = ',' + expandedicon}
					var expanded = nd.getAttribute("Expanded")==null?false:nd.getAttribute("Expanded");
					var categoryid = nd.getAttribute("CategoryID")==null?false:nd.getAttribute("CategoryID");
					var checked = nd.getAttribute("Checked")==null?false:nd.getAttribute("Checked");
					var callbackurl = nd.getAttribute("CallbackUrl")==null?"":nd.getAttribute("CallbackUrl");
					var nNd = tree.add(null, sNd.NodeID, capt, url, icon + expandedicon, expanded, checked, callbackurl, categoryid);

				}
				tree.reloadNode(sNd.NodeID); 
				sNd.LoadingOnDemand = false;
			}
        } else { 
			alert("Could not retreive data from " + sNd.CallBackURL + ", invalid response. Response received: " + obj.responseXML); 
			sNd.LoadingOnDemand = false;
		}
      };
    this.XmlHttp.Connect(arrURL[0], "GET", arrURL[1], objXmlHttpHandler);
}

InstantASPTree.prototype.toggleNodeImage = function(sNd) {
   var isrc = (sNd.equals
   (this.RootNode)?this.Icons.rot:sNd.FirstChild||sNd.CallBackURL!=null?(sNd.Expanded?this.Icons.opf:this.Icons.clf):
   (sNd.FirstChild?this.Icons.opf:this.Icons.chd));
   
   if (sNd.Icon) {isrc=(sNd.Icon.length>1?(sNd.Expanded?sNd.Icon[1]:sNd.Icon[0]):sNd.Icon[0]);} 
	return isrc;
}

InstantASPTree.prototype.expandAll = function() {
 var treeId = this.TreeID;
 this.loopTree(this.RootNode,function(n) { 
  if (n.FirstChild) {
   instantaspTree[treeId].expandNode(n.NodeID);
  }
 });
}

InstantASPTree.prototype.collapseAll = function(incPr) {
 var treeId = this.TreeID;
 this.loopTree(this.RootNode, function(n) {
  if(n.FirstChild&&(!instantaspTree[treeId].RootNode.equals(n)||incPr)) {
   instantaspTree[treeId].collapseNode(n.NodeID);
  }
 });
}

InstantASPTree.prototype.checkNode = function(inTreeID) { 
 var nd = InstantASP_FindControl("cb_"+inTreeID);
 var sNd = this.NodeList[inTreeID];
 sNd.CheckBox = nd.checked;
}
  
InstantASPTree.prototype.setNodeStyle = function(id,cls,rt) { 
 var nd = this.getNodeById(id);nd.cstStyle=cls;
 if (rt) {
  var oNd=InstantASP_FindControl("cstl_"+nd.ID);
  if(oNd) {oNd.className=cls;}
 }
}

InstantASPTree.prototype.setNodeText = function(id,Text) { 
 var inTreeID = this.genIntTreeID(id);
 var nd = InstantASP_FindControl("ac_"+inTreeID);
 var sNd = this.NodeList[inTreeID];
 nd.innerHTML = Text;
 sNd.Text = Text;
}

InstantASPTree.prototype.getNodeById = function(id) {
 return this.NodeList[this.genIntTreeID(id)];
}

InstantASPTree.prototype.setGlobalCtxMenu = function(ctx) {
 this.ContextMenu = ctx;
 ctx.container = this;
}

InstantASPTree.prototype.setGlobalTransition = function(transition) {
 this.Transition = transition;
 transition.Container = this;
}

InstantASPTree.prototype.setNodeCtxMenu = function(id,ctx) { 
 var nd = this.NodeList[this.genIntTreeID(id)];
 nd.ContextMenu=ctx;
 if (ctx.mId) {ctx.container=this;}
}

InstantASPTree.prototype.contextMenu = function(ev,id) {
 if(!this.Options.EnableContext) {return false;}
 var sNd=this.NodeList[id]; 
 var ctx=null;
 if (sNd.ContextMenu&&sNd.ContextMenu.mId) {ctx=sNd.ContextMenu;} 
 else if (sNd.ContextMenu=="DEFAULT") {ctx=null;} 
 else if (sNd.ContextMenu=="NONE") {return false;} 
 else {ctx = this.ContextMenu;}
 if (!ctx) {return true;}
 this.selectNode(id);
 if (this.EditId) clearTimeout(this.EditId);

 ctx.showMenu(ev.clientX, ev.clientY);
 return false
}

InstantASPTree.prototype.loopTree = function(sNd,act) { 
    act(sNd);
    if(sNd.FirstChild!=null) {
    var chNode=sNd.FirstChild;
    do{this.loopTree(chNode,act);chNode=chNode.NextNode}
    while(chNode!=null)
    }
}

InstantASPTree.prototype.liveNodeEditStart = function(id) { 
 this.EditId=setTimeout("instantaspTree."+this.TreeID+".liveNodeEdit('"+id+"')",0)
}

InstantASPTree.prototype.liveNodeEdit = function(id) { 
 if(this.EditId!=null) { 
  var edt=InstantASP_FindControl("ndedt"+this.TreeID);
  var ac=InstantASP_FindControl("ac_"+id);
  var sp=InstantASP_FindControl("cstl_"+id);
  var x=0,y=0,elm=ac;
    while (elm.tagName!="BODY") {
       x+=elm.offsetLeft;
       y+=elm.offsetTop;elm=elm.offsetParent};
       with (edt) {
            disabled=false;
            style.top=y + "px";
            style.left=x + "px";
            style.display="";
           focus();
           value=sp.innerHTML;
           select();
        }
    this.EditId=null
    }
}

InstantASPTree.prototype.liveNodeWrite = function() { 
 var edt=InstantASP_FindControl("ndedt"+this.TreeID);
 if(edt.style.display=="none") {return;}
    var ac=InstantASP_FindControl("cstl_"+this.SelectedNode.ID);
     // do we have a new value 
     if (edt.value!="") { 
        ac.innerHTML = edt.value;
        this.SelectedNode.Text = edt.value;
        this.treeOnNodeWrite(this) 
        edt.style.display="none";
        edt.disabled=true;

        // refresh parent node
        if (this.SelectedNode != null) {
            if (this.SelectedNode.ParentNode != null) {
                this.SelectedNode.ParentNode.Expanded = false;
                this.toggleNode(this.SelectedNode.ParentNode.ID)

            }
        }
     } 

}

InstantASPTree.prototype.liveNodePress = function(e) { 
 if(!this.Options.Editable) {return;}
	 if(e.keyCode==13) {
	   this.liveNodeWrite();
	 } else if (e.keyCode==27) {
	   var edt=InstantASP_FindControl("ndedt"+this.TreeID);
	   edt.style.display="none";
	   edt.disabled=true
	 } else if (e.keyCode==this.Options.EditKey) {
	   this.EditId=setTimeout("instantaspTree."+this.TreeID+".liveNodeEdit('"+this.SelectedNode.ID+"')",10);
	 }
}

InstantASPTree.prototype.setCookie = function(key,value,expire) { 
 document.cookie=escape(key)+"="+escape(value)+(expire?"; expires="+expire:"");
}

InstantASPTree.prototype.getCookie=function(key) {
 if(document.cookie) {
  var c=document.cookie.split(";")[0].split("=");
  if(unescape(c[0])==key) {return unescape(c[1]);}
 }
return ""
}

InstantASPTree.prototype.removeCookie = function(){this.setCookie(this.TreeID+"_selnd","-1","Fri, 31 Dec 1999 23:59:59 GMT;");};
InstantASPTree.prototype.treeOnClick = function(e){};
InstantASPTree.prototype.treeOnDblClick = function(e){};
InstantASPTree.prototype.treeOnMouseOver = function(e){};
InstantASPTree.prototype.treeOnMouseMove = function(e){};
InstantASPTree.prototype.treeOnMouseOut = function(e){};
InstantASPTree.prototype.treeOnMouseDown = function(e){};
InstantASPTree.prototype.treeOnMouseUp = function(e){};
InstantASPTree.prototype.treeOnCheck = function(id){};
InstantASPTree.prototype.treeOnExpand = function(id){}; 
InstantASPTree.prototype.treeOnCollapse = function(id){};
InstantASPTree.prototype.treeOnNodeWrite = function(tree){};


function preloadIcon() { 
 var arg=preloadIcon.arguments;
 for (var i=0;i<arg.length;i++) {
  if(!instantaspTreeIc[arg[i]]){instantaspTreeIc[arg[i]]=new Image();instantaspTreeIc[arg[i]].src=arg[i]}
 }
}
