function getCookieName()
{
	return "program_categories";
}

function saveCategoryState()
{
	var category_settings = "";
	var page_url = "/programs/";
	for (i=0; i < programCategoryList.length; i++) // programCategoryList is defined globally on each details page
	{
		val = (isCategoryClosed(programCategoryList[i])) ? 0 : 1;
		category_settings += programCategoryList[i] + "=" + val + "|";
	}
	writeCookie(getCookieName(), category_settings, 365, page_url);
}

if (!Array.prototype.indexOf) 
{
	Array.prototype.indexOf = function(val, fromIndex) 
	{
		if (typeof(fromIndex) != 'number') fromIndex = 0;
		for (var index = fromIndex, len = this.length; index < len; index++)
		{
			if (this[index] == val) return index;
		}
		return -1;
	}
}

//
// Restores the show/hide settings for the topic
// that the user previously had
//
function restoreCategoryState()
{
	category_settings = readCookie(getCookieName());
	categorySettings = (category_settings != null) ? category_settings.split("|") : '';
	
	if (categorySettings.length > 0)
	{
		for (i=0; i < categorySettings.length; i++)
		{
			if (categorySettings[i] != "" && categorySettings[i] != null && categorySettings[i] != undefined)
			{
				category_data = categorySettings[i].split("=");
				category_id = category_data[0];
				category_display = category_data[1];
				if (category_display == 1) expandCategory(category_id, false);
				else collapseCategory(category_id, false);
			}
		}
		saveCategoryState();
	}
}

//
// Returns whether the topic is collapsed
//
function isCategoryClosed(idref)
{
	return (getElementDisplay("pcl_"+idref) == "none");
}

//
// Collapses a specific topic
//
function collapseCategory(idref, saveState)
{
	hideElement('pcl_'+idref);
	setElementClass('pch_'+idref, 'closed');
	if (saveState) saveCategoryState();
}

//
// Collapses a specific topic
//
function expandCategory(idref, saveState)
{
	showElement('pcl_'+idref);
	setElementClass('pch_'+idref, 'open');
	if (saveState) saveCategoryState();
}

//
// Toggles a program category on or off
// 
function toggleProgramCategory(idref)
{
	toggleElementDisplayAndClass("pcl_"+idref, "pch_"+idref, "open", "closed");
	saveCategoryState();
}