﻿// JScript File

function TreeSwitch(nodePath, treeFile, ctrlPrefix, navigateUrl, targetTab, targetModule, language, appImageRoot)
{
	var nodeId   = ctrlPrefix + '_' + nodePath;
	var subNodes = document.getElementById(nodeId + 'sn');
	var li       = document.getElementById(nodeId + 'li');
	
	if (subNodes) 
	{
		<!-- subtree is loaded -->
		var opened = !(subNodes.style.display == 'none');
		subNodes.style.display = opened ? "none" : "block";
		TreeSwitchIcons (li, !opened, appImageRoot);
	} 
	else 
	{
		<!-- subtree must be loaded -->
		var http = GetHttpRequest();
		if (http) 
		{
		    // prepare new ul (request cannot repeat) 
		    var nextLi = li.nextSibling;
		    var sn = document.createElement("UL");
		    sn.id = nodeId + 'sn';
		    if (nextLi != null)  
		    {
		        //li.parentNode.insertBefore(sn, nextLi);
		        //li.insertBefore(sn, nextLi);		        
		        sn.className = "joined";
		        
		        li.appendChild(sn);			    
		    } 
		    else 
		    {
			    li.appendChild(sn);
			    //li.parentNode.appendChild(sn);
		    }
		    var url = 'TreeViewLoader.aspx?l=' + language + '&tr=' + treeFile + '&ct=' + ctrlPrefix + "&qi=" + nodePath + "&tb=" + targetTab + "&tm=" + targetModule;		     
            
            http.open("GET", url, true);
		    
			http.onreadystatechange=function() {
				if (http.readyState==4) {
					sn.innerHTML = http.responseText;
					TreeSwitchIcons (li, 1, appImageRoot);
				}
			}
		    http.send(null)
		} 
		else 
		{		    
		    location.href = navigateUrl + nodePath;
		}
	}
}
		
	function TreeSwitchIcons(li, open, appImageRoot){
		icons = li.getElementsByTagName('IMG');
		var last = !(li.nextSibling.nextSibling);
		icons[0].src =  appImageRoot + "TreeView_" + (open ? "Minus" : "Plus") + (last ? "Last" : "") + ".gif";
		icons[1].src = appImageRoot  + "TreeView_Symbol" + (open ? "Opened" : "Closed") + ".gif";
	}
	
	function GetHttpRequest(){
		var xmlhttp=false;
		
		if (!xmlhttp && typeof(XMLHttpRequest)!='undefined') {
			xmlhttp = new XMLHttpRequest();
		}
		return xmlhttp;
	}

