function getThis(sId)
{
	var oObject;
	oObject = false;
	
	if (IS_DOM) {
		if (document.getElementById(sId)) {
			oObject = document.getElementById(sId);
		}
	}
	
	return oObject;
}

function changeStyle(oElement, sPropertyName, sNewValue)
{
	// get element
	oStyle = oElement.style;
	// apply new style value to provided property
	eval("oStyle." + sPropertyName + " = '" + sNewValue + "'");
}

function getAncestry(oElement, iLevels)
{
	oAncestor = oElement;
	
	for (i = 0; i < iLevels; i++) {
		oAncestor = oAncestor.parentNode;
	}
	
	return oAncestor;
}

// display box for the tab that's been clicked on
function showTabBox(sBoxId, oTab, sTabRowId)
{
	turnOffTabs(sTabRowId);
	oTabBox = getThis(sBoxId);
	
	if (oTabBox != false) {
		sNewState = changeDisplayState(sBoxId, "closed");
		if (sNewState == "open") {
			oTab.className = "selectedTab";
		} else {
			oTab.className = "unselectedTab";
		}
	}
}

function turnOffTabs(sTabRowId)
{
	oTabRow = getThis(sTabRowId);
	
	if (oTabRow != false) {
		oTabCollection = oTabRow.getElementsByTagName("div");
		for (i = 0; i < oTabCollection.length; i++) {
			oTabCollection[i].className = "unselectedTab";
		}
	}
}

// hide all the boxes accumulated in aTabBoxes by getTabBoxList()
function hideAllBoxes()
{
	for (i = 0; i < aTabBoxes.length; i++) {
		oTabBox = getThis(aTabBoxes[i]);
		if (oTabBox != false) {
			changeStyle(oTabBox, "display", "none");
		}
	}
}

// run this function onload to populate a list of all tabbed boxes on the page 
aTabBoxes = [];
function getTabBoxList()
{
	oContentArea = getThis("mainContent");
	
	if (oContentArea != false) {
		iNextPosition = aTabBoxes.length;
		oBoxes = oContentArea.getElementsByTagName("div");
		for (i = 0; i < oBoxes.length; i++) {
			sBoxId = oBoxes[i].id;
			if (sBoxId.indexOf("tabbox") != -1) {
				aTabBoxes[iNextPosition] = sBoxId;
				iNextPosition++;
			}
		}
	}
}

/* 
interface to show/ hide box element given it's @id
must pass the box's initial state (either "closed" or "open")
so we know what to do in the NULL case 
*/
function changeDisplayState(sBoxId, sInitialState)
{
	sNewState = "";
	
	if (IS_DOM) {
		oBox = getThis(sBoxId);

		if (oBox.style.display == "none" || (oBox.style.display == "" && sInitialState == "closed")) {
			changeStyle(oBox, "display", "block");
			sNewState = "open";
		} else {
			changeStyle(oBox, "display", "none");
			sNewState = "closed";
		}
	}
	
	return sNewState;
}

function showBox(sBoxId, sInitialState)
{
	changeDisplayState(sBoxId, sInitialState);
}

// event attacher
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
