var _errorDiv;										// the DIv that contains our error
var _previousDocumentKeyDown;								// previous on key down function
var _previousDocumentClick;								// previous on key down function
var _errorVisible = false;								// current state of the error text
var defaultBackgroundColor = "#ffc320";

function displayError(errorHTML, nextTo, sideOf, inputCtl)
//
//	Display HTML for an error message.  The HTML may just be error text or could also contain any HTML elements for subsequent action
//
{
	if (!_errorDiv) {								// create this if we need a new one
		_errorDiv=document.createElement("DIV");				// no - create the DIV for the address list
		_errorDiv.id="errorDiv";
		_errorDiv.style.border="black 1px solid";
		_errorDiv.style.zIndex="255";
		_errorDiv.style.padding="2px";
		_errorDiv.style.visibility="hidden";
		_errorDiv.style.position="absolute";
		_errorDiv.style.fontSize="12px";
		_errorDiv.style.fontWeight="bold";
//		_errorDiv.style.backgroundColor="yellow";
//		_errorDiv.style.backgroundColor="#ffc320";
		_errorDiv.style.backgroundColor=defaultBackgroundColor;
		document.body.appendChild(_errorDiv);					// add it to the document
		if (document.onkeydown && document.onkeydown != keyDownHandler)		// is there a different key down handler?
			_previousDocumentKeyDown = document.onkeydown;			// yes - save it
		 document.onkeydown=keyDownHandler;					// install our key down handler
		if (document.onclick && document.onclick != clickHandler)		// is there a different key down handler?
			_previousDocumentClick = document.onclick;			// yes - save it
		 document.onclick=clickHandler;						// install our key down handler
		_errorVisible = false;							// the error text is not visible
  }
	if (!_errorVisible) {								// we are immune from errors while one is visible
		_errorDiv.innerHTML = errorHTML;					// put the error page in the DIV
		setErrorDivLocation(nextTo, sideOf);					// and position the DIV
		if (inputCtl) {								// is an input control specified?
			if (inputCtl.focus) {						// can it receive focus?
				inputCtl.focus();					// yes - give it focus
				if (inputCtl.select)					// can it have text selected?
					inputCtl.select();				// yes - select all of the text
			}
		} else {								// We must be positioned next to the input control
			if (nextTo.focus) {						// can it receive focus?
				nextTo.focus();						// yes - give it focus
				if (nextTo.select) 					// can it have text selected?
					nextTo.select();				// yes - select all of the tex
			}
		}
	}
}


function setErrorDivLocation(nextTo, sideOf)
//
//	Adjust the position and size of the address DIV to match the input field
{
	if (_errorDiv) {								// Ensure we have a div to start with
		var leftOffset;
		var topOffset;
		var wide = false;							// width and height
		var high = false;							// zero means size to contene
		if (!sideOf || sideOf == "right") {					// Put error on the right of the control
			leftOffset = nextTo.offsetWidth-1;
			topOffset = 0;
		} else if (sideOf == "left") {						// or to the left
			leftOffset = 1-_errorDiv.offsetWidth;
			topOffset = 0;
		} else if (sideOf == "bottom") {
			leftOffset = 0;
			topOffset = nextTo.offsetHeight-1;
		} else if (sideOf == "top") {
			leftOffset = 0;
			topOffset = 1-_errorDiv.offsetHeight;
		} else if (sideOf == "above") {
			leftOffset = 0;
//			topOffset = _errorDiv.offsetHeight+nextTo.offsetHeight;			
			topOffset = -55
		} else if (sideOf == "over") {
			if (document.all) {
						leftOffset = 5;
						wide = nextTo.offsetWidth-16;
			}	else {			
						leftOffset = 14;
						wide = nextTo.offsetWidth-16;
			}			
			topOffset = 0;
//			high = nextTo.offsetHeight-20;
		}
		_errorDiv.style.left=calculateOffsetLeft(nextTo)+leftOffset+"px";
		_errorDiv.style.top=calculateOffsetTop(nextTo)+topOffset+"px";
//		if (wide && high) {
		if (wide) {
		  _errorDiv.style.align = "center"
			_errorDiv.style.width = wide+"px";
//			_errorDiv.style.height = high+"px";
		} else {
			_errorDiv.style.width = "";
			_errorDiv.style.height = "";
		}		
		_errorDiv.style.visibility="visible";
		_errorVisible = true;								// flag the error as visible
	}
}
function keyDownHandler(event)
//
//	Doscment key down handler.  Clear the error then invoke any previously set event
{
	if (!event&&window.event) {
		event=window.event;
	}
	if (_errorVisible) {									// only bother with this if we can see the error
		_errorDiv.style.visibility = "hidden";						// hide the error
		_errorVisible = false;								// and flag it - alos allows a new error to occur
	}
	if (_previousDocumentKeyDown)
		_previousDocumentKeyDown(event);
}
function clickHandler(event)
//
//	Document click handler.  Clear the error then invoke any previously set event
{

	if (!event&&window.event) {
		event=window.event;
	}
	if (_errorVisible) {									// only bother with this if we can see the error
		_errorDiv.style.visibility = "hidden";						// hide the error
		_errorVisible = false;								// and flag it - alos allows a new error to occur
	}
	if (_previousDocumentClick)
		_previousDocumentClick(event);
}


function displayInDiv(errorHTML, nextTo, sideOf, inputCtl)
//
//	Display HTML for an error message.  The HTML may just be error text or could also contain any HTML elements for subsequent action
//
{
	if (!_errorDiv) {								// create this if we need a new one
		_errorDiv=document.createElement("DIV");				// no - create the DIV for the address list
		_errorDiv.id="errorDiv";
		_errorDiv.style.border="black 1px solid";
		_errorDiv.style.zIndex="255";
		_errorDiv.style.padding="2px";
		_errorDiv.style.visibility="hidden";
		_errorDiv.style.position="absolute";
		_errorDiv.style.fontSize="12px";
		_errorDiv.style.fontWeight="bold";
		_errorDiv.style.backgroundColor=defaultBackgroundColor;
		document.body.appendChild(_errorDiv);					// add it to the document
		_errorVisible = false;							// the error text is not visible
  } else {
    _errorDiv.style.visibility = "hidden";	
		_errorDiv = null;			
	}
	
	if (!_errorVisible) {								// we are immune from errors while one is visible
		_errorDiv.innerHTML = errorHTML;					// put the error page in the DIV
		setErrorDivLocation(nextTo, sideOf);					// and position the DIV
		if (inputCtl) {								// is an input control specified?
			if (inputCtl.focus) {						// can it receive focus?
				inputCtl.focus();					// yes - give it focus
				if (inputCtl.select)					// can it have text selected?
					inputCtl.select();				// yes - select all of the text
			}
		} else {								// We must be positioned next to the input control
			if (nextTo.focus) {						// can it receive focus?
				nextTo.focus();						// yes - give it focus
				if (nextTo.select) 					// can it have text selected?
					nextTo.select();				// yes - select all of the tex
			}
		}
	}
}

function hideDiv() {
   if (_errorDiv) {
	    _errorDiv.style.visibility = "hidden";	
		  _errorDiv = null;			
			_errorVisible = false;
	 }
}
