// JScript File

// these are used to store the selected id's and cat names
var strCatIDList = '';
var strCatNameList = '';

// need this for the currently selected category
var iCatID = null;
var selectedCatInTree = '';

// called when a node in the tree is clicked
function iWasClicked(osrc,evElement)
{	
	var clickedTag = '', clickedID = '', clickedClass = '', clickedText = '';
	
	// first we need to deal with the event handling to capture the source event
	if(!osrc)
	{
		osrc = window.event.srcElement;
		window.event.cancelBubble = true;		// cancel the bubble in IE
	}
	else
	{
		osrc.cancelBubble = true;	// cancel the bubble in NN
	}
		
	// now we need to detect the element that was clicked, it's tag name and id. Have to
	// cater for Netscape and IE cos they deal with the detection differently!
	if(!evElement)
	{
		// Netscape
		evElement = this;
		clickedTag = evElement.tagName;
		clickedID = evElement.id;
		clickedClass = evElement.className;
		
		//for Netscape
		if (navigator.appName == "Netscape")
		{
			clickedText = evElement.textContent;
		}
		else
		{
			clickedText = evElement.innerText;
		}
	}
	else
	{
		// IE
		clickedTag = osrc.tagName;
		clickedID = osrc.id;
		clickedClass = osrc.className;
		clickedText = osrc.innerText;
	}
	
	// now we have all the info we need, let's figure out what we need to do....
	// get the basic controlling data items from the form.
	var iCatItemID = '';
		
	// now take an action....
	if (clickedTag == 'LI')
	{		
		iCatItemID = clickedID.substring(1, clickedID.length);	// extract the category ID from the id tag
		// now deal with the hiding/showing of sub cats
		switch (clickedClass)
		{
			case 'p':
				document.getElementById(clickedID).className = 'm';		// if class is plus, swap it back to minus and select it
				break;
				
			case 'm':
				document.getElementById(clickedID).className = 'p';		// if minus, swap back to plus and de-selected it
				break;
				
			case 'u':
				document.getElementById(clickedID).className = 's';		// if it's unselected, select it
				selectCat(true, iCatItemID, clickedText);
				break;
				
			case 's':
				document.getElementById(clickedID).className = 'u';		// if it's selected, un-select it
				selectCat(false, iCatItemID, clickedText);
				break;
		}
		
		selectedCatInTree = clickedID;		// set the id of the currently selected category
								
		// do I need to fetch the sub tree?
		if (clickedID.substring(0,1) == 'F')
		{
			document.getElementById(clickedID).className = 'f';
			return fetchSubtree(iCatItemID);
		}
		else
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}

//Add the selected category and remove de-selected category to list
function selectCat(bSelect, iCatItemID, strName)
{
	if (bSelect)
	{
		strCatIDList += iCatItemID + ',';
		strCatNameList += strName + ',';
	}
	else 
	{
		strCatIDList = strCatIDList.replace(iCatItemID + ',', '');
		strCatNameList = strCatNameList.replace(strName + ',', '');
	}
	//alert(strCatIDList);
	//alert(strCatNameList);
	return true;
}

// loops through the category nodes and assigned the onClick event.
function assignClickActions()
{
	var arr = document.getElementsByTagName('li');
	
	for(var x = 0; x < arr.length; x++)
	{
		document.getElementById(arr[x].id).onclick = iWasClicked;
	}
}

//Populates the category tree
function populateSubtree()
{
	var errorText = fraHidden.document.getElementById('txtErrorDesc').value;
	if(errorText == "")
	{
		var oSelNode = document.getElementById('F' + iCatID);
		var oNewNode = fraHidden.document.getElementById('divResult');
		var strNewText = oNewNode.innerHTML;

		if(oSelNode == null) 
		{
			// user might have pressed back button.. what to do?
			// go back until no error (this goes to the correct previous page, to the Categories tree page)
			// setStatusMsg('Unable to retrieve.');			
			window.history.go(-1);
			return false;
		}	
		
		oSelNode.innerHTML = strNewText;
		oSelNode.className = 'm';
		oSelNode.id = 'C' + oSelNode.id.substring(1,oSelNode.id.length);
		
		selectedCatInTree = oSelNode.id;	// set this again as the ID of the object will have changed!
		iCatID = null;
		assignClickActions();		// need to assign the functions to LI nodes again
		
		return true;
	}
	else
	{
		// display the error on the page
		document.getElementById('divStatusText').innerText = errorText;
	}
}

//Closes the Category picker
function cancelPicker()
{
	if (window.opener)
	{
		window.close();
	}
}

//Clears the all the category selections
function clearCatList()
{
	var items = document.getElementsByTagName('li');
	for(var i=0; i<items.length; i++)
	{
		if(items[i].className == 's')
		{
			items[i].className = 'u';
		}
	}
	strCatIDList = '';
	strCatNameList = '';
	window.opener.setExternalCFValue('', '', 'no');
	
	//window.close();
}

//Send selected categories to the parent window
function sendCatList()
{
	if (window.opener)
	{
		try
		{
			if (strCatIDList.length == 1)
			{
				window.opener.setExternalCFValue('', '');
			}
			else
			{			
				window.opener.setExternalCFValue(strCatIDList.substring(0, strCatIDList.length - 1), strCatNameList.substring(0, strCatNameList.length - 1));
			}
		}
		catch(err)
		{
			//alert(err.description);
		}
		window.close();
	}
}

// submits the hidden form to return the children of the selected category
function fetchSubtree(iCategoryItemID)
{
	var f = document.frmHidden;
    
	if (iCatID != null)
	{
		alert('The previous data fetch request has not yet completed. Please wait a few seconds and try again.');
		return false;
	}
	else
	{
		iCatID = iCategoryItemID;
		f.c.value = iCategoryItemID;
		f.submit();
		return true;
	}
}

// assign click events to cat tree
window.onload = assignClickActions;		

