// Function: fetchObjectById.
function fetchObjectById(objectId)
{
	// Depending on browser version, fetch object properties. 
	if (document.getElementById)
	{
		return document.getElementById(objectId);
	}
	else if (document.all)
	{
		return document.all[objectId];
	}
	else if (document.layers)
	{
		return document.layers[objectId];
	}
}


// Function: fetchObjectByName.
function fetchObjectByName(objectName)
{
	// Depending on browser version, fetch object properties. 
	if (document.getElementByName)
	{
		return document.getElementByName(objectName);
	}
	else if (document.all)
	{
		return document.all[objectName];
	}
	else if (document.layers)
	{
		return document.layers[objectName];
	}
}

// Function: calcAbsoluteElementPos.
function calcAbsoluteElementPos(whichElement)
{
 	// When reaching the root (the body tag), start returning relative positions.
 	if (whichElement.offsetParent == document.body)
	{
	 	 var newPoint = new Object();
		 newPoint.x = whichElement.offsetLeft;
		 newPoint.y = whichElement.offsetTop;

		 return newPoint;
	}

	// When starting to return relative positions (root has been reached), start adding up and return. 
	returnedPoint = calcAbsoluteElementPos(whichElement.offsetParent);
	
	returnedPoint.x += whichElement.offsetLeft;
	returnedPoint.y += whichElement.offsetTop;

	return returnedPoint;
}


// Function: fetchStyle.
function fetchStyle(whichLayer)
{
 var theObject = fetchObjectById(whichLayer);
 return theObject.style;
}


// Function: toggleLayer.
function toggleLayer(whichLayer)
{
	var stylesheet = fetchStyle(whichLayer);
	stylesheet.display = stylesheet.display? "":"block";
}

// Function: showTooltip.
// whichLayer - the id of the tooltip element.
// containerId - the id of the element in which the tooltip is to be displayed (what is considered pos 0,0).
// Example: showTooltip(event, 'myTooltipId', 'myBaseElementId');
function showTooltip(event, whichLayer, containerId)
{
	var stylesheet = fetchStyle(whichLayer);
	var containerObj = fetchObjectById(containerId);
	var posX = 0;
	var posY = 0;

	// Fetch coordinates.
	if (event.x || event.y)
	{
		posX = event.x;
		posY = event.y;
	}
	else if (event.pageX || event.pageY)
	{
		posX = event.pageX;
		posY = event.pageY;
	}
	else
	{
		posX = event.clientX;
		posY = event.clientY;
	}

	returnedPoint = calcAbsoluteElementPos(containerObj);
	
	// Activate div.
	stylesheet.left = posX - returnedPoint.x + 30;
	stylesheet.top = posY - returnedPoint.y + 30;
	
	stylesheet.display = "block";
}

// Function: hideTooltip.
function hideTooltip(whichLayer)
{
	var stylesheet = fetchStyle(whichLayer);
	stylesheet.display = "none";
}


// Function: isChecked.
function isChecked(formName, checkboxName)
{
 	return document.forms[formName].elements[checkboxName].checked;	
}


// Function: getRadioButtonValue.
function getRadioButtonValue(formName, radioName, numRadioButtons)
{
	buttonCount = 0;
	theForm = document.forms[formName];
	numElements = theForm.length;
	
	// Loop through all elements in the form and look for radio buttons with the same name.
	// Return the value of the checked one.
	for (i = 0; i < numElements && buttonCount < numRadioButtons; i++)
	{
	 	theButton = theForm.elements[i];
	
	 	if (theButton.name == radioName)
		{
		 	 if (theButton.checked == true)
			 		return theButton.value;
		
		 	 buttonCount++;
		}
	}

	// No match was found, so some parameter has not been spcified correctly to the function.
	return null;
}

// Function: setRadioButtonValue.
function setRadioButtonValue(formName, radioName, numRadioButtons, valueToSet)
{
 	buttonCount = 0;
	theForm = document.forms[formName];
	numElements = theForm.length;
	
	// Loop through all elements in the form and look for radio buttons with the same name.
	for (i = 0; i < numElements && buttonCount < numRadioButtons; i++)
	{
	 	theButton = theForm.elements[i];
		
		if (theButton.name == radioName)
		{
		 	 if (theButton.value == valueToSet)
			 {
			 		theButton.checked = true;
			 		return true;
			 }
			 
			 buttonCount++;
		}
	}
	
	// Couldn't find a matching value in that radio button group.
	return false;
}
