function makeFormClickHideCommonPopups()
{
	document.body.onclick = hideCommonPopups;
	if (document.body.captureEvents)
		document.body.captureEvents(Event.CLICK);
}

function hideCommonPopups()
{
	hideErrorPopups();
	hidePopupMenu();
}

function positionElement(elementToPosition, elementToPutNear, anchorToCorner, insideX, insideY, favouredXOffset, favouredYOffset, minHorizontalMargin, minVerticalMargin)
{
	// anchorToCorner: 1=top-left; 2=top-right; 3=bottom-right; 4=bottom-left
	// insideX,Y: 1 to overlap with the element; 0 to keep it outside
	
	var putNearOriginPos = findCoordinateOriginForObject(elementToPutNear);
	
	var left = -putNearOriginPos[0] + elementToPutNear.offsetLeft + favouredXOffset;
	var top = -putNearOriginPos[1] + elementToPutNear.offsetTop + favouredYOffset;
	
	// On the left
	if (anchorToCorner == 1 || anchorToCorner ==4)
		if (insideX == 0)
			left -= elementToPosition.offsetWidth;
	
	// On the right
	if (anchorToCorner == 2 || anchorToCorner == 3)
	{
		left += elementToPutNear.offsetWidth;
		if (insideX == 1)
			left -= elementToPosition.offsetWidth;
	}
	
	// At the top
	if (anchorToCorner == 1 || anchorToCorner == 2)
		if (insideY == 0)
			top -= elementToPosition.offsetHeight;
	
	// At the bottom
	if (anchorToCorner == 3 || anchorToCorner == 4)
	{
		top += elementToPutNear.offsetHeight;
		if (insideY == 1)
			top -= elementToPosition.offsetHeight;
	}
	
	var viewPortOffset = findViewPortOffset();
	var viewPortSize = findViewPortSize();
	
	var horizontalLimit = viewPortOffset[0] + viewPortSize[0];
	var verticalLimit = viewPortOffset[1] +viewPortSize[1];
	
	if (this.document.body.offsetWidth > horizontalLimit)
		horizontalLimit = this.document.body.offsetWidth;
	
	if (this.document.body.offsetHeight > verticalLimit)
		verticalLimit = this.document.body.offsetHeight;
	
	if (left + elementToPosition.offsetWidth > horizontalLimit - minHorizontalMargin)
		left = horizontalLimit - minHorizontalMargin - elementToPosition.offsetWidth;
	
	if (top + elementToPosition.offsetHeight > verticalLimit - minVerticalMargin)
		top = verticalLimit - minVerticalMargin - elementToPosition.offsetHeight;
	
	if (left < minHorizontalMargin)
		left = minHorizontalMargin;
	
	if (top < minVerticalMargin)
		top = minVerticalMargin;
	
	var toPositionOriginPos = findCoordinateOriginForObject(elementToPosition);
	
	elementToPosition.style.left = (toPositionOriginPos[0] + left) + 'px';
	elementToPosition.style.top = (toPositionOriginPos[1] + top) + 'px';
}

function continuallyPositionElement(elementToPositionId, elementToPutNearId, anchorToCorner, insideX, insideY, favouredXOffset, favouredYOffset, minHorizontalMargin, minVerticalMargin)
{
	var elementToPosition = document.getElementById(elementToPositionId);
	var elementToPutNear = document.getElementById(elementToPutNearId);

	if (elementToPosition == null || elementToPutNear == null)
	{
		// Can't find the elements; stop trying
		return;
	}
	else if (elementToPutNear.offsetHeight == 0)
	{
		// The control is probably in an AjaxModalPopup and it hasn't displayed itself yet.
		// Do nothing - we'll try again shortly.
	}
	else
	{
		// Position the element
		positionElement(elementToPosition, elementToPutNear, anchorToCorner, insideX, insideY, favouredXOffset, favouredYOffset, minHorizontalMargin, minVerticalMargin);
	}
	
	setTimeout("continuallyPositionElement('" + elementToPositionId + "', '" + elementToPutNearId + "', " + anchorToCorner + ", " + insideX + ", " + insideY + ", " + favouredXOffset + ", " + favouredYOffset + ", " + minHorizontalMargin + ", " + minVerticalMargin + ")", 300);
}

function clickLinkInRow(row)
{
	clickLinkInElement(row);
}

function clickLinkInCell(cell)
{
	clickLinkInElement(cell);
}

function clickLinkInElement(element)
{
	var linkElements = element.getElementsByTagName("a");

	if (linkElements.length > 0)
		clickElement(linkElements[0]);
}

function clickElement(element)
{
	if (element.click)
	{
		element.click();
		return true;
	}
	else if (element.dispatchEvent) 
	{
		window.location = element.href;
		return true;
	}
	else
	{
		return  false;
	}
}

function openWindow(pageName, width, height)
{
	var top;
	var left;
	
	top = (window.screen.height / 2) - (height / 2);
	left = (window.screen.width / 2) - (width / 2);

	var popup_window = window.open(pageName,'popup_window','top=' + top + ',left=' + left + ',width=' + width + ',height=' + height + ',title=no,status=no,resizable=no,scrollbars=yes,menubar=no');
	popup_window.focus();
}

function jumpOpenerToBookmark(bookmark)
{
	var url = window.opener.location.href;
	var hashPos = url.indexOf('#');
	
	if (hashPos > -1)
		url = url.substr(0, hashPos);
	
	window.opener.location.href = url + "#" + bookmark;
}

function disableAllInputsInTable(table, disable)
{
	for (var i=0; i<table.childNodes.length; ++i)
	{
		if (table.childNodes[i].nodeName == "TR")
		{
			disableAllInputsInRow(table.childNodes[i], disable);
		}
		else if (table.childNodes[i].nodeName == "TBODY")
		    disableAllInputsInTable(table.childNodes[i], disable)
	}
}

function disableAllInputsInRow(row, disable)
{
	for (var i=0; i<row.childNodes.length; ++i)
	{
		if (row.childNodes[i].nodeName == "TD")
		{
			disableAllInputsInCell(row.childNodes[i], disable);
		}
	}
}

function disableAllInputsInCell(cell, disable)
{
	for (var i=0; i<cell.childNodes.length; ++i)
	{
		if (cell.childNodes[i].nodeName == "INPUT")
		{
			cell.childNodes[i].disabled = disable;
		}
	}
}

function setAllCheckBoxes(containerElementId, checked)
{
	var containerElement = document.getElementById(containerElementId);
	var inputs = containerElement.getElementsByTagName("input");
	
	for (i = 0; i < inputs.length; i++)
		if (inputs[i].type == "checkbox")
			inputs[i].checked = checked;
}

function doNotBubble(objEvent)
{
	if (!objEvent)
		objEvent = window.event;
	
	if (objEvent.stopPropagation)
		objEvent.stopPropagation();
	else
		objEvent.cancelBubble = true;
}

// Returns the coordinates of the top left of the viewport, as should be used for the
// absolute positioning of this object.
function findCoordinateOriginForObject(obj)
{
	var left = 0;
	var top = 0;
	
	while (obj.offsetParent)
	{
		obj = obj.offsetParent;
		left -= obj.offsetLeft;
		top -= obj.offsetTop;
	}
	
	return [left, top];
}

function findViewPortSize()
{
	// Thanks to andylangton.co.uk for this bit...
	var viewPortWidth;
	var viewPortHeight;

	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
		viewPortWidth = window.innerWidth,
		viewPortHeight = window.innerHeight
	}
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{
		viewPortWidth = document.documentElement.clientWidth,
		viewPortHeight = document.documentElement.clientHeight
	}
	// older versions of IE
	else
	{
		viewPortWidth = document.getElementsByTagName('body')[0].clientWidth,
		viewPortHeight = document.getElementsByTagName('body')[0].clientHeight
	}
	// ... end of thanks. kthxbye

	return [viewPortWidth, viewPortHeight];
}

function findViewPortOffset()
{
	var viewPortLeft;
	var viewPortTop;
	
	// Thanks to http://ptaff.ca/xhtml/?lang=en_CA for this bit...


	if (window.innerHeight)
	{
		viewPortLeft = window.pageXOffset;
		viewPortTop = window.pageYOffset;
	}
	else if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollTop == 0))
	{
		viewPortLeft = document.documentElement.scrollLeft;
		viewPortTop = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		viewPortLeft = document.body.scrollLeft;
		viewPortTop = document.body.scrollTop;
	}
	// ... end of thanks. kthxbye

	return [viewPortLeft, viewPortTop];
}

function selectAllText(textbox)
{
	textbox.focus();
	textbox.select();
}

function clearHtmlTagsFromElement(elementId)
{
   document.getElementById(elementId).value = '';
}
