/**************************************************
* James' Code
**************************************************/

var lastSelect //check what last option is/was
var lastRadio //check what last radio selection is/was

//utility function to get at elements by their id in various browsers
function getRefToObject(objID) {
    if( document.layers ) {					//Netscape layers
        return document.layers[objID]; }
    if( document.getElementById ) {			//DOM; IE5, NS6, Mozilla, Opera
        return document.getElementById(objID); }
    if( document.all ) {					//Proprietary DOM; IE4
        return document.all[objID]; }
    if( document[objID] ) {					//Netscape alternative
        return document[objID]; }
    return false;
}

//opt=show or hide, divid=id of div to alter, hid=id of hidden field to store show/hide value in for page submission
function showHide(opt, divid) {

	if(opt == 'show') {
		getRefToObject(divid).style.display="block" ;
	} else if(opt == 'hide') {
		getRefToObject(divid).style.display="none" ;
	}
}

//optv=value of <option> to check for match, showOpt=value that sends show result, divid=id of div to alter
function showHideFromSelect(optv, showOpt, divid) {
	if(optv == showOpt) {
		showHide('show', divid)
	} else {
		showHide('hide', divid)
	}
}

//v=variable to update (lastSelect or lastRadio) then can wrap showHide etc in a check so doesn't overide div display when unwanted
//opt=option that was sent to showHide* functions
function updateChecks(v, opt) {
	if(v == 'select') {
		lastSelect = opt
	} else if(v == 'radio') {
		lastRadio = opt
	}
}

/**************************************************
* End of James' Code
**************************************************/


/****** PERSISTING EXPANDABLE/COLLAPSIBLE ELEMENTS ********/
var enablepersist="off" //Enable saving state of content structure using session cookies? (on/off)
var collapseprevious="no" //Collapse previously open content when opening present? (yes/no)

if (document.getElementById){
	document.write('<style type="text/css">')
	document.write('.switchcontent{display:none;}')
	document.write('</style>')
}

function getElementbyClass(classname)
{
	ccollect=new Array()
	var inc=0
	var alltags=document.all? document.all : document.getElementsByTagName("*")
	for (i=0; i<alltags.length; i++)
	{
		if (alltags[i].className==classname)
		{
			ccollect[inc++]=alltags[i];
		}
	}
}

function get_cookie(Name)
{ 
	var search = Name + "=";
	var returnvalue = "";
	if (document.cookie.length > 0)
	{
		offset = document.cookie.indexOf(search);
		if (offset != -1)
		{ 
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1)
			{
				end = document.cookie.length;
			}
			returnvalue=unescape(document.cookie.substring(offset, end));
		}
	}
	return returnvalue;
}

function getselectedItem()
{
	if (get_cookie("ShowBits") != "")
	{
		selectedItem=get_cookie("ShowBits");
		return selectedItem;
	}
	else
	{
		return "";
	}
}

function revivecontent()
{
	selectedItem=getselectedItem();
	selectedComponents=selectedItem.split("|");
	for (i=0; i<selectedComponents.length-1; i++)
	{
		// We could do with making sure that the element still exists (i.e. are we still on the same page), otherwise the first page after a line listing will have a javascript error if filter box was showing
		if (document.getElementById(selectedComponents[i]))
		{
			document.getElementById(selectedComponents[i]).style.display="block";
		}
	}
}

function Expand(cid)
{
	if (typeof ccollect!="undefined")
	{
		document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none";
	}
}

function do_onload()
{
	getElementbyClass("switchcontent");
	if (enablepersist=="on" && typeof ccollect!="undefined")
	{
		revivecontent();
	}
}

function saveswitchstate()
{
	var inc=0, selectedItem="";
	while (ccollect[inc])
	{
		if (ccollect[inc].style.display=="block")
		{
			selectedItem+=ccollect[inc].id+"|";
		}
		inc++;
	}
	document.cookie="ShowBits" + "=" + selectedItem + ";path=/";
}

if (window.addEventListener)
{
	window.addEventListener("load", do_onload, false);
}
else if (window.attachEvent)
{
	window.attachEvent("onload", do_onload);
}
else if (document.getElementById)
{
	window.onload=do_onload;
}

if (enablepersist=="on" && document.getElementById)
{
	window.onunload=saveswitchstate;
}

/****************************/
