var TopList = new Array();			// top level categories

function T(iID, sCaption) {
	var NewTop = new Array();
	
	NewTop[0] = iID;
	NewTop[1] = sCaption;
	NewTop[2] = new Array();
	TopList[iID] = NewTop;
	
	return true;
}

function M(iTopID, iID, sCaption) {
	var NewMid = new Array();
	
	NewMid[0] = iID;
	NewMid[1] = sCaption;
	NewMid[2] = new Array();
	
	TopList[iTopID][2][iID] = NewMid;
	
	return true;
}

function C(iTopID, iMidID, iID, sCaption) {
	var NewCat = new Array();
	
	NewCat[0] = iID;
	NewCat[1] = sCaption;
	
	TopList[iTopID][2][iMidID][2][iID] = NewCat;
	
	return true;
}




/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//			Combo Population Functions
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function loadTopCats(sList, sCatList, sTypeList) {
	var oList = document.getElementById(sList);
	
	if (sCatList != "" ) {
		var oCatList = document.getElementById(sCatList);
	}
	var oMidList = document.getElementById(sTypeList);
	if (sCatList != "" && oCatList != null) {
		var CatID = oCatList.value;
	}
	
	if ( oMidList != null ) {
		var MidID = oMidList.value;
	}

	if ( oList != null ) {
		var TopID = oList.value;
	}

	if ( oList == null ) {
		return true;
	}

	// clear the list
	oList.options.length = 0;
	if (sCatList != "") {
		oCatList.options.length = 0;
	}
	oMidList.options.length = 0;
	
	// go through the top level in the array
	oList.options[oList.options.length] = new Option("All",0);
	for (var oItem in TopList) {
		oList.options[oList.options.length] = new Option(TopList[oItem][1],TopList[oItem][0]);
	}
	
	return true;
}

function loadMidCats(sList, sCatList, sTopList) {
	var oList = document.getElementById(sList);
	if (sCatList != "") {
		var oCatList = document.getElementById(sCatList);
	}
	var oTopList = document.getElementById(sTopList);
	if (sCatList != "") {
		var CatID = oCatList.value;
	}
	var MidID = oList;
	var TopID = oTopList.value;
	
	if ( oList == null || oTopList == null ) {
		return true;
	}
	
	var TopID = oTopList.value;
	
	// clear the list
	oList.options.length = 0;
	if (sCatList != "") oCatList.options.length = 0;
	
	if ( TopID == 0 ) {
		// All was selected
		loadCats(sCatList,sList,sTopList); // the toplist item is not necessary, just send the catlist
		return false;
	}
	
	// go through the mid level in the array
	oList.options[oList.options.length] = new Option("All",0);
	
	for (var oItem in TopList[TopID][2]) {
		oList.options[oList.options.length] = new Option(TopList[TopID][2][oItem][1],TopList[TopID][2][oItem][0]);
	}
	
	return true;
}

function loadCats(sList, sMidList, sTopList) {
	if (sList != "") {
		var oList = document.getElementById(sList);
	}
	var oMidList = document.getElementById(sMidList);
	var oTopList = document.getElementById(sTopList);
	if (sList != "") {
		var CatID = oList.value;
	}
	var MidID = oMidList.value;
	var TopID = oTopList.value;
	//var oHidden = document.getElementById(sList);
	
	if ( oMidList == null || oList == null ) {
		return true;
	}
	
	// clear the list
	oList.options.length = 0;
	
	if ( TopID == 0 || MidID == 0 ) {
		// All was selected
		return false;
	}
	
	if (sList != "") {
		// go through the mid level in the array
		oList.options[oList.options.length] = new Option("All",0);
		
		for (var oItem in TopList[TopID][2][MidID][2]) {
			oList.options[oList.options.length] = new Option(TopList[TopID][2][MidID][2][oItem][1],TopList[TopID][2][MidID][2][oItem][0]);
		}
	}
	
	return true;
}



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//			Tree Population Functions
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function buildTreeview(TopID,MidID,iLevel) {
	// for the initial pass, send in 0 and 0 for the ID's and 1 for the level
	var sTmp = "";
	var oLevel = document.getElementById("txtNodeLevel");
	var oID = document.getElementById("txtNodeID");
	var oTxt1 = document.getElementById("txtExpanded1");
	var oTxt2 = document.getElementById("txtExpanded2");
	var oTxtCurrentNode = document.getElementById("txtCurrentNode");
	var oTxtScroll = document.getElementById("txtScrollTop");
	var arTmp1 = oTxt1.value.split("|");
	var arTmp2 = oTxt2.value.split("|");
	var sDisplay = "";
	var sBackColor = "";
	var sBold = "";
	
	if ( iLevel == 1 ) {
		// BUILD THE TOP LEVEL
		for (var oItem in TopList) {
			// set the display value
			sDisplay = "none";
			for ( var i in arTmp1 ) {
				if ( arTmp1[i] == "top_" + TopList[oItem][0] ) sDisplay = "block";
			}
			
			sBackColor = "E5F2F6";
			sBold = "normal";
			if ( oTxtCurrentNode.value == "top_" + TopList[oItem][0] ) {
				sBackColor = "FFE63E";
				sBold = "bold";
			}
			
			sTmp += "	<div id=\"top_" + TopList[oItem][0] + "\" onclick=\"exeFolder(this.id,1,false);\" ondblclick=\"exeFolder(this.id,1,true);\" style=\"background-color: #" + sBackColor + "; cursor: pointer; cursor: hand; white-space: nowrap; font-weight: " + sBold + ";\">\n";
			sTmp += "		<img align=\"absmiddle\" border=\"0\" id=\"imgtop_" + TopList[oItem][0] + "\" src=\"img/Icons/Tree/icoTopLevelDoc.gif\" />\n";
			sTmp += "		" + TopList[oItem][1] + "\n";
			sTmp += "	</div>\n";
			sTmp += "	<div id=\"fldtop_" + TopList[oItem][0] + "\" style=\"white-space: nowrap; display: " + sDisplay + "; padding-left: 10px;\">\n";
			
			// build the next level
			// the mid level still has no ID, so send in a 0
			sTmp += buildTreeview(TopList[oItem][0],0,2);
			
			sTmp += "	</div>\n";
		}
		
		return sTmp;
	}
	else {
		if ( iLevel == 2 ) {
			// BUILD THE MID LEVEL
			for (var oItem in TopList[TopID][2]) {
				// set the display value
				sDisplay = "none";
				for ( var i in arTmp2 ) {
					if ( arTmp2[i] == "mid" + TopID + "_" + TopList[TopID][2][oItem][0] ) sDisplay = "block";
				}
				
				sBackColor = "E5F2F6";
				sBold = "normal";
				if ( oTxtCurrentNode.value == "mid" + TopID + "_" + TopList[TopID][2][oItem][0] ) {
					sBackColor = "FFE63E";
					sBold = "bold";
				}
				
				sTmp += "	<div id=\"mid" + TopID + "_" + TopList[TopID][2][oItem][0] + "\" onclick=\"exeFolder(this.id,2,false);\" ondblclick=\"exeFolder(this.id,2,true);\" style=\"background-color: #" + sBackColor + "; cursor: pointer; cursor: hand; white-space: nowrap; font-weight: " + sBold + ";\">";
				sTmp += "		<img align=\"absmiddle\" border=\"0\" id=\"imgmid" + TopID + "_" + TopList[TopID][2][oItem][0] + "\" src=\"img/Icons/Tree/icoMidLevelDoc.gif\" />";
				sTmp += "		" + TopList[TopID][2][oItem][1] + "";
				sTmp += "	</div>";
				sTmp += "	<div id=\"fldmid" + TopID + "_" + TopList[TopID][2][oItem][0] + "\" style=\"white-space: nowrap; display: " + sDisplay + "; padding-left: 10px;\">\n";
				
				// build the next level
				// sTmp += buildTreeview(TopID,TopList[TopID][2][oItem][0],3);
				
				sTmp += "	</div>\n";
			}
			
			return sTmp;
		}
		else {
			// BUILD THE CATEGORY LEVEL
			for (var oItem in TopList[TopID][2][MidID][2]) {
				sBackColor = "E5F2F6";
				sBold = "normal";
				if ( oTxtCurrentNode.value == "cat_" + TopList[TopID][2][MidID][2][oItem][0] ) {
					sBackColor = "FFE63E";
					sBold = "bold";
				}
				
				sTmp += "	<div id=\"cat_" + TopList[TopID][2][MidID][2][oItem][0] + "\" onclick=\"exeFolder(this.id,3,false);\" ondblclick=\"exeFolder(this.id,3,true);\" style=\"background-color: #" + sBackColor + "; cursor: pointer; cursor: hand; white-space: nowrap; font-weight: " + sBold + ";\">\n";
				sTmp += "		<img align=\"absmiddle\" border=\"0\" id=\"imgcat_" + TopList[TopID][2][MidID][2][oItem][0] + "\" src=\"img/Icons/Tree/icoBottomLevelDoc.gif\" />\n";
				sTmp += "		" + TopList[TopID][2][MidID][2][oItem][1] + "\n";
				sTmp += "	</div>\n";
			}
			
			return sTmp;
		}
	}
	
	if ( oTxtScroll.value != "" ) {
		document.getElementById("divTree").scrolltop = oTxtScroll.value;
	}
	document.getElementById("divTree").moveTo = 40;
	
	return sTmp;
}

function getTreeNodeID(sKey) {
	// get the id from the right portion of the node's key field
	// the key field is the html element's ID
	var sTmp = "" + sKey;
	var sOut = "";
	var iStart = sTmp.indexOf("_");
	
	if ( iStart > 0 ) {
		sOut = sTmp.substring(iStart+1,sTmp.length);
	}
	else {
		sOut = sKey;
	}
	
	return sOut;
}