﻿// JScript File
function IWasClicked(osrc,evElement)
{	
	var clickedTag = '', clickedID = '', clickedClass = '', clickedName = '';
	
	// 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;
		//clickedName = evElement.n;
		clickedName = evElement.getAttribute('n');
	}
	else
	{
		// IE
		clickedTag = osrc.tagName;
		clickedID = osrc.id;
		clickedClass = osrc.className;
		clickedName = osrc.n;
	}
	
	// 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);
				//SelectCat(true, clickedName);
				break;
				
			case 's':
				document.getElementById(clickedID).className = 'u';		// if it's selected, un-select it
				//SelectCat(false, iCatItemID);
				//SelectCat(false, clickedName);
				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;
	}
}


function init()
{
	assignClickActions();
}

// loops trhough 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;
}

function SendSelectedCats()
{
	var arr = document.getElementsByTagName('li');
	var selCatIds = "";
	var selCats = "";
	
	if(navigator.appName == "Microsoft Internet Explorer")
	{
	    for(var i = 0; i < arr.length; i++)
	    {
    	    if(arr[i].getAttributeNode("class").value == "s")
    	    {
                selCatIds = selCatIds + arr[i].getAttributeNode("id").value.replace("I", "") + ",";
                selCats = selCats + arr[i].getAttributeNode("cat").value + ",";
    	    }
	    }
	}
	else
	{
	    for(var i = 0; i < arr.length; i++)
	    {
    	    if(arr[i].className == "s")
    	    {
                selCatIds = selCatIds + arr[i].id.replace("I", "") + ",";
                selCats = selCats + arr[i].getAttributeNode("cat").value + ",";
    	    }
	    }
	}
	if(selCatIds.length > 0)
	{
	    selCatIds = selCatIds.substring(0, selCatIds.length - 1);
	    selCats = selCats.substring(0, selCats.length - 1);
	}
	window.opener.SetCategory(selCatIds, selCats);
}

function ClearSelection()
{
    window.opener.SetCategory("", "", 1);
}

function SetCategory(catIds, catNames,flag)
{
    document.getElementById("hdCats").value = catIds;
    document.getElementById("txtCats").value = catNames;
    if(flag != 1)
    {
        oPickWindow.close();
    }
}

function ShowCompanyTree()
{
    var winx = "500";
    var winy = "500";
	oPickWindow = window.open("CompanyTree.aspx", "CompanyTypePicker", "width=530,height=500,top=" + parseInt(winy) + ",left=" + parseInt(winx)+ ",scrollbars=no", false);
	oPickWindow.focus();
}