/**
 * Bright Interactive, category.js
 *
 * Copyright 2003 Bright Interactive, All Rights Reserved.
 * 
 * Contains JavaScript functions used by the pages displaying categories.
 *
 */
/*
Ver  Date	        Who					Comments
--------------------------------------------------------------------------------
d1  08-Oct-2003     Martin Wilson       Created.
--------------------------------------------------------------------------------
*/


/**
* Called when a category dropdown is changed. Populates the sub-category list (if
* relevant) and hides/shows the other dropdowns.
* 
* @param a_listBox - the dropdown that has changed
* &param - the number of the list box. The first list, showing root cats, has an number of -1.
*		     the sub cats are number from 0.
*/
function changedCategory(a_listBox, iChangedListNumber) 
/*
------------------------------------------------------------------------
 d1   08-Oct-2003   Martin Wilson		Created.
------------------------------------------------------------------------
*/
{

	iNewListNumber = iChangedListNumber + 1;

	lParentId = a_listBox.options[a_listBox.selectedIndex].value;

	var catInfo;

	// If something was selected then get the information about it:
	if (lParentId > 0)
	{
		catInfo = catInfoArray['id' + lParentId];
	}

	// If something was selected and there are sub items then add to the list:
	if (lParentId > 0 && catInfo.subCatsArray.length > 0)
	{
	
		// Get the list to populate:
		subListControl = document.getElementById("cat" + iNewListNumber);

		// Clear the list:
		subListControl.options.length = 0;

		// Add the first item:
		subListControl.options[0] = new Option('- Please Select -',0);

		// Add the new items:
		for (i = 0; i < catInfo.subCatsArray.length; i++)
		{
			subListControl.options[subListControl.options.length] = 
				new Option(catInfo.subCatsArray[i].name,
							catInfo.subCatsArray[i].id);

		}
	}
	else
	{
		// Just show as far as the changed list:
		iNewListNumber = iChangedListNumber;

	}
	// Hide all list boxes that aren't relevant:
	for (i= 0; i <= iMaxSubListNumber; i++)
	{
		// Get the control:
		divToHide = document.getElementById("divcat" + i);

		// Hide or show it:
		if (i <= iNewListNumber)
		{
			// Show it:
			divToHide.style.display = "block";
		}
		else
		{
			// Hide it:
			divToHide.style.display = "none";
		}
	}

}


/**
* Adds the selected category to the list of chosen categories.
* 
* @param a_listBox - the dropdown that has changed
* &param - the number of the list box. The first list, showing root cats, has an number of -1.
*		     the sub cats are number from 0.
*/
function addSelectedCategory(a_sDropdownName) 
/*
------------------------------------------------------------------------
 d1   08-Oct-2003   Martin Wilson		Created.
------------------------------------------------------------------------
*/
{
	// Get the dropdown that was selected:
	lbDropdown = document.getElementById(a_sDropdownName);

	// Get the category id selected:
	lCatId = lbDropdown.options[lbDropdown.selectedIndex].value;

	if (lCatId > 0)
	{
		if (categoryIsInList(lCatId))
		{
			//category already selected
			alert("This category is already selected.");
		}
		else
		{
			if(document.getElementById("chosenCategoryList").options.length >= 10)
			{
				alert('You may only select up to 10 categories');
				return;
			}			

			addCategory(lCatId);
			
			if (ancestorIsInList(lCatId))
			{
				alert("You have already selected a parent of this category. Please check that you meant to include the parent and child category.");
			}
		}

		// Rebuild the comma separated list (which holds the chosen category ids):
		setCatIdsField();
	}
}

/**
* Adds the category with a_lCategory id to the list of chosen categories.
* 
* @param a_lCategory - the id of the category.
*/
function addCategory(a_lCategory) 
/*
------------------------------------------------------------------------
 d1   08-Oct-2003   Martin Wilson		Created.
------------------------------------------------------------------------
*/
{

	// Get the info about the selected category:
	catInfo = catInfoArray['id' + a_lCategory];

	// Add to the list:
	lbChosenCatList = document.getElementById("chosenCategoryList");

	lbChosenCatList.options[lbChosenCatList.options.length] = 
		new Option(catInfo.name, catInfo.id);
}

/**
* Adds the selected category to the list of chosen categories.
* 
* @param a_listBox - the dropdown that has changed
* &param - the number of the list box. The first list, showing root cats, has an number of -1.
*		     the sub cats are number from 0.
*/
function removeSelectedCategory() 
/*
------------------------------------------------------------------------
 d1   08-Oct-2003   Martin Wilson		Created.
------------------------------------------------------------------------
*/
{
	// Get the list:
	lbChosenCatList = document.getElementById("chosenCategoryList");

	// Remove the selected item:
	if (lbChosenCatList.selectedIndex != -1)
	{
		lbChosenCatList.options[lbChosenCatList.selectedIndex] = null;
	}

	// Rebuild the comma separated list (which holds the chosen category ids):
	setCatIdsField();

}


/**
* Sees whether a category is already in the list.
* 
* @param - id of the category to check.
*/
function categoryIsInList(a_lCategoryId) 
/*
------------------------------------------------------------------------
 d2   17-May-2005   Chris Preager		Created.
------------------------------------------------------------------------
*/
{
	bInList = false;

	var lbChosenCatList = document.getElementById("chosenCategoryList");

	// See if this is is already in the list:		
	for (j=0; j< lbChosenCatList.options.length; j++)
	{
		current = lbChosenCatList.options[j];

		if (a_lCategoryId == current.value)
		{
			bInList = true;
			break;
		}
	}

	return (bInList);
}


/**
* Sees whether the parent of a category is already in the list. If so, gives a warning.
* 
* @param - the dropdown list containing the selected category.
*/
function ancestorIsInList(a_lCategoryId) 
/*
------------------------------------------------------------------------
 d1   08-Oct-2003   Martin Wilson		Created.
------------------------------------------------------------------------
*/
{
	bInList = false;

	// Get the info object for the selected category:
	catInfo = catInfoArray['id' + a_lCategoryId];

	var lbChosenCatList = document.getElementById("chosenCategoryList");

	// Go through the parent ids for this category:
	for (i = 0; i < catInfo.ancestorIds.length; i++)
	{
	
		// See if this is is already in the list:		
		for (j=0; j< lbChosenCatList.options.length; j++)
		{
			current = lbChosenCatList.options[j];

			if (catInfo.ancestorIds[i] == current.value)
			{
				bInList = true;
				break;
			}

		}

		if (bInList)
		{
			break;
		}

	}

	return (bInList);
}

/*
	Sets the field that returns the cat ids as a string.
*/
function setCatIdsField()
/*
 d1   01-Nov-2002  Martin Wilson        Created.
*/   
{
	var lbChosenCatList = document.getElementById("chosenCategoryList");
	var sCatIds = "";
	
	for (i=0; i< lbChosenCatList.options.length; i++)
	{
		current = lbChosenCatList.options[i];

		sCatIds = sCatIds + current.value;

		// Add a comma if not the last one:
		if (i != (lbChosenCatList.options.length - 1))
		{
			sCatIds = sCatIds + ",";
		}

	}

	// Set the hidden field:
	var hidChosenCatIds = document.getElementsByName("categoryIds")[0];
	hidChosenCatIds.value = sCatIds;

}
