function getStyleObject(objectId) {
  // checkW3C DOM, then MSIE 4, then NN 4.
  //
  if(document.getElementById && document.getElementById(objectId)) {
        return document.getElementById(objectId).style;
   }
   else if (document.all && document.all(objectId)) {
        return document.all(objectId).style;
   }
   else if (document.layers && document.layers[objectId]) {
        return document.layers[objectId];
   } else {
        return false;
   }
}

function changeObjectVisibility(objectId, newVisibility, newDisplay) {
    // first get the object's stylesheet
    var styleObject = getStyleObject(objectId);

    // then if we find a stylesheet, set its visibility
    // as requested
    //
    if (styleObject) {
        styleObject.visibility = newVisibility;
        styleObject.display = newDisplay;
        return true;
    } else {
        return false;
    }
}

function switchDiv(div_id,display)
{
  var style_sheet = getStyleObject(div_id);
  if (display == "")
  {
  	display = "block";
  }
  if (style_sheet)
  {
    //hideAll();
    changeObjectVisibility(div_id, "visible", display);
  }
  else 
  {
    alert("sorry, this only works in browsers that do Dynamic HTML");
  }
}

function hideAll(formName)
{
	changeObjectVisibility(formName,"hidden","none");
}




function toggleLayer(id, prefix)
{
	theId = prefix + id;
	arrow = "arrow_" + id;

	//alert('id: ' + id + ' prefix: ' + prefix);

	var style = document.getElementById(theId).style;
	var arrow2 = document.getElementById(arrow);

	//alert('id: ' + id + ' style: ' + style.display);

	if (style.display == 'block') {
		document.getElementById(theId).style.display = "none";
		arrow2.className = "closed";
	} else {
		document.getElementById(theId).style.display = "block";
		arrow2.className = "open";
	}

}

function saveStateToCookie(id, prefix, cookieName)
{
	var stateInfo;

	if (getCookie(cookieName))
	{
		var theCookie,x,newInfo="";

		//alert('hmm: ' + prefix + id);

		theCookie = getCookie(cookieName);

		var theCookieArray = theCookie.split("|");

		var foundId = false;

		for (x=0; x<theCookieArray.length; x++) {

			var theId = prefix + id;
			var style = document.getElementById(theId).style;
			var individualState = theCookieArray[x].split(":");

			//alert(individualState[0]);

			if ((individualState[0] == theId) && (style.display == "none"))
			{
				//theCookieArray.splice(x,1);
				foundId = true;
				individualState[1] = "0";
				//alert('turn off: ' + individualState[0]);
			}
			else if ((individualState[0] == theId) && (style.display == "block"))
			{
				foundId = true;
				individualState[1] = "1";
				//alert('change status to 1');
			}


			if (foundId == true) {
				theCookieArray[x] = individualState[0] + ":" + individualState[1];
			}
			//	alert('last: ' + theCookieArray[x]);
		}

		if (!foundId)
		{
			if (style.display == "none")
				theCookieArray.push(theId + ":0");
			if (style.display == "block")
				theCookieArray.push(theId + ":1");
		}

		newInfo = theCookieArray.join("|");

		setCookie(cookieName, newInfo, 3);

	} else {

		var theId = prefix + id;
		
		if (document.getElementById(theId).style.display == "none")
			stateInfo = prefix + id + ":0";
		else if (document.getElementById(theId).style.display == "block")
			stateInfo = prefix + id + ":1";

		setCookie(cookieName, stateInfo, 3);
	}
}

function turnOnHiddenStates(cookies)
{
	
	var theCookieArray = cookies.split("|");
	var thePrefix = "";

	for (var i = 0; i < theCookieArray.length - 1; i++) {

		var smallerCookieArray = theCookieArray[i].split(":");
		thePrefix = smallerCookieArray[1];

		if (getCookie(smallerCookieArray[0])) {

			var hiddenStates,x,thePrefix;
		
			hiddenStates = getCookie(smallerCookieArray[0]);

			//alert('hiddenStates: ' + hiddenStates);

			var hiddenStatesArray = hiddenStates.split("|");

			//alert('hiddenStatesArray: ' + hiddenStatesArray.length);

			for (j = 0; j < hiddenStatesArray.length; j++) {

				var individualState = hiddenStatesArray[j].split(":");

				if (!document.getElementById(individualState[0]))
					break;

				//alert(individualState[0] + " " + individualState[1] + " " + document.getElementById(individualState[0]).style.display);
				theId = individualState[0].match(/(\d+)/g);
				thePrefix = individualState[0].match(/^(\w+)\_/g);
			
				if (individualState[1] == 0 && document.getElementById(individualState[0]).style.display == "block")
					toggleLayer(theId, thePrefix);
				else if (individualState[1] == 1 && document.getElementById(individualState[0]).style.display == "none")
					toggleLayer(theId, thePrefix);

			}
		}

	}
}

function randomBackground() {

	// If this is the index page
	if (document.URL.match('index.html')) {

		var background_img = document.getElementById('header').className;

		var bgs = new Array('1','2','3'); // Possible backgrounds

		var rand = Math.round(1 + (3-1)*Math.random());
		background_img = background_img.substring(0, background_img.length - 1) + rand;
		document.getElementById('header').className = background_img;

	}

}
