function makeFormClickHideErrorPopups()
{
	document.body.onclick = hideErrorPopups;
	if (document.body.captureEvents)
		document.body.captureEvents(Event.CLICK);
}

function toggleErrorPopup(objEvent)
{
	if (!objEvent)
		objEvent = window.event;
	
	if (objEvent.stopPropagation)
		objEvent.stopPropagation();
	else
		objEvent.cancelBubble = true;
	
	var errimg;
	
	if (objEvent.target)
		errimg = objEvent.target;
	else
		errimg = objEvent.srcElement;
	
	var errpop = getPopupForImage(errimg);
	var currentDisplay = errpop.style.display;
	
	hideErrorPopups();
	
	if (currentDisplay == 'none')
	{
		errimg.style.zIndex = '3';
		errpop.style.display = 'block';
	}
}

function hideErrorPopups()
{
	// getElementsByName doesn't seem to work in IE. Can't see why not.
	var errImages = document.getElementsByTagName('img');
	for (i = 0; i < errImages.length; i++)
	{
		if (errImages[i].id.substring(errImages[i].id.length - 7, errImages[i].id.length) == '_errimg')
		{
			if (errImages[i].style.zIndex == '3')
				errImages[i].style.zIndex = '1';
			getPopupForImage(errImages[i]).style.display = 'none';
		}
	}
}

function getPopupForImage(img)
{
	return document.getElementById(img.id.substring(0, img.id.length - 7) + '_errpop')
}

function primeErrorElements(controlToPutImageById, controlToPutPopupById, imageId, popupDivId, imageXOverlap, imageYOverlap, popupXOffset, popupYOffset)
{
	var image = document.getElementById(imageId);
	var popupDiv = document.getElementById(popupDivId);

	if (image != null)
	{
		image.style.position = 'absolute';
		image.style.zIndex = '1';
		image.style.display = 'block';
	}
	
	if (popupDiv != null)
	{
		popupDiv.style.position = 'absolute';
		popupDiv.style.zIndex = '2';
	}
	
	// Wait the briefest of moments before positioning the elements. If we're in an AjaxModalPopup, then that
	// has to finish positioning itself before we position ourselves. This delay is normally about ten times
	// as long as it needs, and less time than the user will notice.
	// If it's not long enough, it doesn't matter - we'll be constantly repositioning soon anyway.
	setTimeout("positionErrorElements('" + controlToPutImageById + "', '" + controlToPutPopupById + "', '" + imageId + "', '" + popupDivId + "', " + imageXOverlap + ", " + imageYOverlap + ", " + popupXOffset + ", " + popupYOffset + ")", 10);
}

function positionErrorElements(controlToPutImageById, controlToPutPopupById, imageId, popupDivId, imageXOverlap, imageYOverlap, popupXOffset, popupYOffset)
{
	var controlToPutImageBy = document.getElementById(controlToPutImageById);
	var controlToPutPopupBy = document.getElementById(controlToPutPopupById);
	var image = document.getElementById(imageId);
	var popupDiv = document.getElementById(popupDivId);
		
	var oldZIndex;
	
	if (image != null)
		oldZIndex = image.style.zIndex;
	else
		oldZIndex = null;

	if (controlToPutImageBy == null || image == null || popupDiv == null)
	{
		// The control has been hidden or is no longer in error, so we probably needn't worry about it any more.
	}
	else if (controlToPutImageBy.offsetHeight == 0)
	{
		// The control is probably in an AjaxModalPopup and it hasn't displayed itself yet.
		// Do nothing - we'll try again shortly.
	}
	else
	{
		var controlToPutImageByOriginPos = findCoordinateOriginForObject(controlToPutImageBy);
		var imageOriginPos = findCoordinateOriginForObject(image);
		
		image.style.left = (imageOriginPos[0] - controlToPutImageByOriginPos[0] + controlToPutImageBy.offsetLeft + controlToPutImageBy.offsetWidth - imageXOverlap) + 'px';
		image.style.top = (imageOriginPos[1] - controlToPutImageByOriginPos[1] + controlToPutImageBy.offsetTop + controlToPutImageBy.offsetHeight - imageYOverlap) + 'px';
		
		var controlToPutPopupByOriginPos = findCoordinateOriginForObject(controlToPutPopupBy);
		var popupDivOriginPos = findCoordinateOriginForObject(popupDiv);
		popupDiv.style.left = (popupDivOriginPos[0] - controlToPutPopupByOriginPos[0] + popupXOffset) + 'px';
		popupDiv.style.top = (popupDivOriginPos[1] - controlToPutPopupByOriginPos[1] + controlToPutPopupBy.offsetHeight + popupYOffset) + 'px';
	}
	
	// The oldZIndex is nothing if the image isn't being (shouldn't be) shown at the moment. Otherwise, continually
	// reposition it, in case other javascript has moved things around
	if (oldZIndex == '1' || oldZIndex == '3')
		setTimeout("positionErrorElements('" + controlToPutImageById + "', '" + controlToPutPopupById + "', '" + imageId + "', '" + popupDivId + "', " + imageXOverlap + ", " + imageYOverlap + ", " + popupXOffset + ", " + popupYOffset + ")", 300);
}
