// -------------------------------------------------------------------------
// Functions for selecting topics from comboboxes. If a topic has subtopics
// a new combobox for the subtopics will be created after selection.
// Put the following div tag where you want the selector to appear. 
//		<div id="oDivTopicSelector" style="width:300">
//		</div>
// The comboboxes will all have the same width as specified in the style
// for oDivTopicSelector.
// -------------------------------------------------------------------------
/*
var g_arrTopicNames = new Array(
		"Affärssystem",
		"Inloggning och behörigheter",
		"Nätverk - uppkoppling",
			"Nätverk - uppkoppling/Secure ID",
			"Nätverk - uppkoppling/ISDN",
			"Nätverk - uppkoppling/ADSL", 
			"Nätverk - uppkoppling/LAN",
			"Nätverk - uppkoppling/Lokala inloggningsproblem",
			"Nätverk - uppkoppling/Proxyinställningar",
			"Nätverk - uppkoppling/Surfa externt",
				"Nätverk - uppkoppling/Surfa externt/Subtopic 1",
				"Nätverk - uppkoppling/Surfa externt/Subtopic 2",
 
		"e-post",
		"Program",
			"Program/Windows 2000", 
			"Program/Word",
			"Program/Excel",
			"Program/PowerPoint",
			"Program/Outlook",
			"Program/F-secure",
			"Program/Acrobat Reader", 

		"Skrivare",
		"Internet - Intranät",
		"Mobilt - Telefoni",
		"Maskinvara"
  );
*/		
var g_sTopicNameDelimiter = "/";		
var g_sAllTopicsTitle = "All topics";
var g_OnTopicTreeChangeExtraEvalCode = ""; 

function addToStringArray(str, arr, bIgnoreDuplicates)
{
	if (bIgnoreDuplicates) 
	{
		for (var i = 0; i < arr.length; i++) 
		{
			if (str == arr[i]) 
				return;
		}
	}
	arr[arr.length] = new String(str);
}

function doMakeTopicSelectors(sCurrentTopicPath, sAllTopicsTitle, sSelectedTopic, arrTopicSelectors)
{
	var arrLevelSpecificTopicNames = new Array();
	for (var i = 0; i < g_arrTopicNames.length; i++) 
	{
		var sTopicName = g_arrTopicNames[i];
		if (sCurrentTopicPath == "" || sTopicName.indexOf(sCurrentTopicPath) == 0)
		{
			sTopicName = sTopicName.substring(sCurrentTopicPath.length, sTopicName.length);
		
			// Get the parts of the topic name, the first part is the 
			// topmost level.
			var arrTopicNameParts = sTopicName.split(g_sTopicNameDelimiter);
			if (arrTopicNameParts.length > 0) 
			{
				var sTopicNameFirstPart = arrTopicNameParts[0];
				addToStringArray(sTopicNameFirstPart, arrLevelSpecificTopicNames, true); 
			}
		}
	}
	if (arrLevelSpecificTopicNames.length > 0)
	{
		var sTopicSelectorHTML = 
				//'<select id="oComboBoxTopicSelector' + arrTopicSelectors.length + '" name=tn ' +
				'<select id="oComboBoxTopicSelector' + arrTopicSelectors.length + '"' +
				'style="width:100%" onchange="onTopicSelectorChange()">';
		// Add an entry for all topics at this level, it has the value "" 
		sTopicSelectorHTML +=
			'<option value=""' + (sSelectedTopic == "" ? " selected" : "") + '>' + sAllTopicsTitle + '</option>'; 
		for (var i = 0; i < arrLevelSpecificTopicNames.length; i++) 
		{
		    var sTopicName = arrLevelSpecificTopicNames[i];
			sTopicSelectorHTML +=
				'<option value="' + sTopicName + '"' + 
					(sSelectedTopic == sTopicName ? " selected" : "") + 
				'>' + sTopicName + '</option>'; 
		}
		sTopicSelectorHTML += '</select>';
		arrTopicSelectors[arrTopicSelectors.length] = new String(sTopicSelectorHTML);
	}
	
}

function makeTopicSelectors(sCurrentTopicPath)
{
	// If we have no topics, just display all topics option
	if (g_arrTopicNames.length == 0)
	{
		oDivTopicSelector.innerHTML = 
			'<select id="oComboBoxTopicSelector0"' +
				'style="width:100%" onchange="onTopicSelectorChange()">' +
				'<option value="" selected>' + g_sAllTopicsTitle + '</option>' +
			'</select>';
	}
	else
	{
		var arrTopicSelectors = new Array();
		
		// Create a selector for every level in the path
		var arrTopicPathParts = sCurrentTopicPath.split(g_sTopicNameDelimiter);
		var sTopicPath = "";
		var sSelectedTopic = "";
	
		// Create the overview combobox
		doMakeTopicSelectors("", g_sAllTopicsTitle, arrTopicPathParts.length > 0 ? arrTopicPathParts[0] : "", arrTopicSelectors);
	
		// Create a combobox for every level beneath
		for (var i = 0; i < arrTopicPathParts.length; i++) 
		{
			sTopicPath += arrTopicPathParts[i] + g_sTopicNameDelimiter;
			sSelectedTopic = (i < arrTopicPathParts.length - 1) ? arrTopicPathParts[i+1] : "";
			doMakeTopicSelectors(sTopicPath, g_sAllTopicsTitle, sSelectedTopic, arrTopicSelectors);
		}
	
		var sTopicSelectorHTML = "";
		for (var i = 0; i < arrTopicSelectors.length; i++) 
			sTopicSelectorHTML += arrTopicSelectors[i] + '<div style="margin: 2px"></div>';
			
		oDivTopicSelector.innerHTML = sTopicSelectorHTML;
	}
}

function getCurrentTopicPath()
{
	// Create the currently selected path
	var sCurrentTopicPath = "";
	var i = 0;
	oComboBoxTopicSelector = document.getElementById("oComboBoxTopicSelector" + i);
	while (oComboBoxTopicSelector != null) 
	{
		if (oComboBoxTopicSelector.value != "")
			sCurrentTopicPath += ((sCurrentTopicPath != "") ? g_sTopicNameDelimiter : "") + oComboBoxTopicSelector.value;
		i++;
		oComboBoxTopicSelector = document.getElementById("oComboBoxTopicSelector" + i);
	}
	return sCurrentTopicPath; 
}
	
function onTopicSelectorChange()
{
	var sCurrentTopicPath = getCurrentTopicPath();
	//alert(i + ": " + sCurrentTopicPath);
	// Recreate the topic selector
	makeTopicSelectors(sCurrentTopicPath);
    if (g_OnTopicTreeChangeExtraEvalCode != "") {
        eval(g_OnTopicTreeChangeExtraEvalCode);
    }
}

// -------------------------------------------------------------------------
// End of topic selector functions
// -------------------------------------------------------------------------


