<!-- package au.com.tt.agweb.resources -->

/* ============================================================================================================================= */
/* General utilities														 */
/* ============================================================================================================================= */

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Open a new window.														 */
/*																 */
/* This function is usually used in the following manner:									 */
/*	 <a href="http://blah.com.au" onclick="newWindow('http://blah.com.au')">Link description</a>				 */
/*																 */
/* We do not simply call javascript from the href attribute of the link (Eg <a href="javascript:newWindow()">) as we want the	 */
/* link to display cleanly in the browser status bar. This also allows the user to use the 'open in new window' feature of their */
/* browser.															 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function newWindow(inURL)
{
    var newWindow = window.open(inURL);

    if ((navigator.userAgent.toLowerCase().indexOf("msie") == -1) || (parseInt(navigator.appVersion) >= 5))
    	newWindow.focus();

    return(false);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Open a window with a menubar and pass focus to it.										 */
/*																 */
/* This function opens or re-populates a window that has a menubar. It is intended as a link onclick handler, overriding the	 */
/* default link action while maintaining normal right-click menu functionality.							 */
/*																 */
/* We pass focus to the new window to ensure it re-appears if the link is clicked when the window is already open.		 */
/*																 */
/* Parameters:															 */
/*     inWinName	   The name of the window to be spawned (or re-populated if it already exists).				 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function openWindowWithMenu(inURL, inWinName)
{
    var width	  = 776;
    var height	  = 558;
    var left	  = (screen.width - width) / 2 + 100;
    var top	  = (screen.height - height) / 2 + 100;
    var newWindow = window.open(inURL, inWinName,
		    'toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, menubar=yes, resizable=yes,' +
		    'top=' + top + ',left=' + left + ',height=' + height + ',width=' + width);
    newWindow.focus();
    return(false);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Open a window with a menubar and pass focus to it.										 */
/*																 */
/* This function opens or re-populates a window that has a no menubar. It is intended as a link onclick handler, overriding the	 */
/* default link action while maintaining normal right-click menu functionality.							 */
/*																 */
/* We pass focus to the new window to ensure it re-appears if the link is clicked when the window is already open.		 */
/*																 */
/* Parameters:															 */
/*     inWinName	   The name of the window to be spawned (or re-populated if it already exists).				 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function openWindowNoMenu(inURL, inWinName)
{
    var width	  = 776;
    var height	  = 558;
    var left	  = (screen.width - width) / 2 + 100;
    var top	  = (screen.height - height) / 2 + 100;
    var newWindow = window.open(inURL, inWinName,
		    'toolbar=no, location=no, directories=no, status=yes, scrollbars=yes, menubar=no, resizable=yes,' +
		    'top=' + top + ',left=' + left + ',height=' + height + ',width=' + width);
    newWindow.focus();
    return(false);
}
// -----------------------------------------------------------------------------------------------------------------------------
// Clear all input text fields in a given selector. Also clears existing errors.  Also clears error divs.  If inErrorSelector is not  
// specified, then any element with class .error-text is cleared (clearing the innerHTML).  Otherwise, elements that match the
// selector inError selector are cleared.
//
// NOTE: This function only works for input elements with type text.
// -----------------------------------------------------------------------------------------------------------------------------
function clearFormBySelector(/* String representation of CSS selector */ inSelector,
						     /* String representation of CSS selector*/  inErrorSelector)
{
	var errorSelector = (inErrorSelector) ? inErrorSelector : ".error-text";
	$j(inSelector).each(function()
	{
		var currentDiv = this;
		$j('input[type=text]', currentDiv).each(function()
		{
			this.value = '';
		});
		$j(errorSelector, currentDiv).each(function()
		{
			this.innerHTML = '' ;
			this.style.display = 'none'
		});
	});	
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Set all checkboxes in the same form as the passed checkbox to the same value as the passed checkbox.				 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function checkAllFromBox(inCheckbox)
{
    var elements = inCheckbox.form.elements;

    for (var idx = 0; idx < elements.length; idx++)
    {
		if (elements[idx].type == 'checkbox')
		{
		    elements[idx].checked = inCheckbox.checked;
		}
    }
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Set all checkboxes in the form with the passed name to the passed value.							 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function CheckAllFromLink(inFormId, inCheckValue)
{
    var elements = document.getElementById(inFormId).elements;

    for (var idx = 0; idx < elements.length; idx++)
    {
		if (elements[idx].type == 'checkbox')
		{
		    elements[idx].checked = inCheckValue;
		}
    }
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Focus on 'inElement'. Does nothing if 'inElement' is not found.								 */
/*																 */
/* Parameters:															 */
/*    inElement		The element itself or the "id" of the element to focus on.						 */
/*    inHoldWindowAtTop (Optional) If supplied and true, ensure the window displaying the page displays the top of the page.	 */
/*			Otherwise, the browser is allowed to scroll the	window to display the focused element.			 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function FocusOnElement(inElement, inHoldWindowAtTop)
{
    var element = (typeof inElement == 'object' ? inElement : document.getElementById(inElement));

    if (element != null)
    	element.focus();

    if (inHoldWindowAtTop != null && inHoldWindowAtTop)
    	window.scrollTo(0,0);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Scroll to 'inElement', useful where inElement cannot receive focus (is not an input). Does nothing if 'inElement' is not found.								 */
/*																 */
/* Parameter:															 */
/*    inElement		The element itself or the "id" of the element to focus on.						 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function scrollToElement(inElement)
{
    var element = (typeof inElement == 'object' ? inElement : document.getElementById(inElement));

	var leftPos = 0;
	var topPos  = 0;

	//Find the position of the element
	while (element != null)
	{
		leftPos += element.offsetLeft;
		topPos  += element.offsetTop;
		element = element.offsetParent;
	}

	window.scrollTo(leftPos, topPos);
}
/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Jumps to the anchor specified and focuses (or selects) the element								 */
/* passed.															 */
/* Any element that has a select method will have this method executed. NOTE: the <option> elements do not have select methods.	 */
/* Select methods														 */
/*	 highlight all text for input elements such as text or textarea.							 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function MoveToElement(inAnchor, inElement)
{
    if (inAnchor != null)
    	document.location.hash=inAnchor;

    var element = document.getElementById(inElement);

    if (element != null)
    {
		element.focus();
		if (element.type == "text" || element.type == "textarea" || element.type == "password" || element.type == "file")
		{
		    element.select();
		}
    }
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Resets a passed element, and returns focus to it.										 */
/* If the element is disabled, nothing will be done.										 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function resetElement(inElement)
{
    var element = document.getElementById(inElement);

    if (element == null || element.disabled)
    	return;

    if (element.type == "select-one")
    {
		element.focus();
		element.selectedIndex = 0;
    }
    else
    {
		element.focus();
		element.value = "";
    }
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Set the value of an element, given its "id" attribute and its new value.							 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function setElement(inElement, inValue)
{
    var element = document.getElementById(inElement);

    if (element != null)
    	element.value = inValue;
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Checks the radio or checkbox element.											 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function CheckElement(targetElement, value)
{
    var target =  document.getElementById(targetElement);

    if (target == null)
    	return;

    target.checked = value;
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Return true if the passed value is a valid currency amount, given the number of decimal places in the currency.		 */
/* Caters for:	zero dp		1 or more digits										 */
/*		two dp		zero or more digits, optionally followed by							 */
/*				a decimal place and 1 or 2 digits.								 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function isCurrencyAmt(inValue, inDecPlaces)
{
    if (inValue == null || inDecPlaces == null || inValue == "")
    	return(false);

    var re = inDecPlaces > 0 ? /^\d*(\.\d{1,2})?$/ : /^\d+$/;

    return(re.test(inValue));
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Format/round a number to 2 decimal places. Appends trailing zeroes if required to ensure 2 decimal places are always shown.	 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function fmtAsCurrency(inValue)
{
    if (inValue == null)
    	return("");

    var con = ((Math.round(inValue * 100) / 100).toString() + ".00").split(".");
    	return(con[0] + "." + con[1] + (con[1].length < 2 ? "0" : ""));
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Wrapper for the native javascript submit() method.										 */
/* Submits inForm which can be either the form element itself or its id.							 */
/*																 */
/* Note: the browser will complain that "submit" is not a function of form if the identified form has an <input> element of type */
/* "submit" with a name "submit". Any other value of the name attribute is okay.						 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function submitForm(inForm)
{
    if (inForm == null)
    	return(false);

    var form = (typeof inForm == 'object' ? inForm : document.getElementById(inForm));

    form.submit();

    return(true);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Load the given URL into the current window.											 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function loadPage(inUrl)
{
    document.location.href = inUrl;
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Set the "class" attribute of the element with the given "id" attribute.							 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function setClass(inElementId, inClassName)
{
    var element = document.getElementById(inElementId);

    if (element != null)
    	element.className = inClassName;
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Set the disabled attribute of all elements of the form with the given "id" attribute to inDisabled.				 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function disableForm(inFormId, inDisabled)
{
    var form = document.getElementById(inFormId);
    var elements;

    if (form != null)
    	elements = form.elements;

    if (elements == null)
    	return;

    for (var idx = 0; idx < elements.length; idx++)
    	elements[idx].disabled = inDisabled;
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Toggle the CSS display property of inElement between '' and 'none'.								 */
/*																 */
/* inElement :	 Can be the Element itself or the Id of the element whose display property will be toggled.			 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function toggleDisplay(inElement)
{
    var element = (typeof inElement == 'object' ? inElement : document.getElementById(inElement));

    if (element != null)
    	element.style.display = (element.style.display != 'none') ? 'none' : '';
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Determines if the CSS property of 'display' for inElement is set to '' (empty) or 'none' (not displayed).			 */
/*																 */
/* If the CSS style property of inElementId is 'none' (not displayed), false will be returned.					 */
/* Otherwise, true will be returned. Which for our purposes can be deemed to mean that inElement is displayed.			 */
/*																 */
/* Note: This function does not determine whether inElement is currently shown or not as it does not consider the visibility of	 */
/*	 inElement's parents.													 */
/*																 */
/* inElement :	 Can be the Element itself or the Id of the element whose display property will be queried.			 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function isDisplayed(inElement)
{
    var element = (typeof inElement == 'object' ? inElement : document.getElementById(inElement));

    if (element != null)
    {
		if (element.style.display != 'none')
		    return(true);
    }

    return(false);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Set the CSS display property of a HTML element.										 */
/*
/* - inElement can be the element itself or the id of the element whose display property will be set.				 */
/* - inValue must be set to a legitimate CSS value for the display property.							 */
/*																 */
/* NB To show an element using default styling, inValue should be an empty string "", and to hide an element inValue should be	 */
/* "none".															 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function setDisplay(inElement, inValue)
{
    var element = (typeof inElement == 'object' ? inElement : document.getElementById(inElement));

    if (element != null)
    	element.style.display = inValue;
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Set the disabled attribute of a HTML element.										 */
/* - inElement is the id of the element which is to be set or the element itself.						 */
/* - inValue is the value which disabled will be set to, options are true or false.						 */
/*																 */
/* NB Ensure the HTML element supports the disabled attribute before using.							 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function setDisabled(inElement, inValue)
{
    var elem = (typeof inElement == 'object' ? inElement : document.getElementById(inElement));

    if (elem != null)
    	elem.disabled = inValue;
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Determines if the element is disabled or not. This function will return false if the inElement is null.			 */
/*																 */
/* inElement :	 Can be the Element itself or the Id of the element whose display property will be queried.			 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function isDisabled(inElement)
{
    if(inElement == null)
    	return(false);

    var element = (typeof inElement == 'object' ? inElement : document.getElementById(inElement));

    if (element != null && element.disabled)
    	return(true);

    return(false);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Adds an event (inEventName) Listener to inElement.										 */
/*																 */
/* inElement:	The element which the event listener will be added to.								 */
/* inEventName: The type of event which the listener will be catering for.							 */
/* inFuncName:	The function which will be called when the listener detects an event of type 'inEventName'.			 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function tt_addEvent(inElement, inEventName, inFuncName)
{
    if (inElement == null || inEventName == null || inFuncName == null)
    	return;

    if (inElement.addEventListener)						// Gecko / W3C
    	inElement.addEventListener(inEventName, inFuncName, true);
    else if (inElement.attachEvent)						// IE
    	inElement.attachEvent("on" + inEventName, inFuncName);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Removes an event (inEventName) Listener from inElement.									 */
/*																 */
/* inElement:	The element which the event listener will be removed from.							 */
/* inEventName: The type of event which the existing listener was catering for.							 */
/* inFuncName:	The function which the existing listener was detecting for events of type 'inEventName'.			 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function tt_removeEvent(inElement, inEventName, inFuncName)
{
    if (inElement == null || inEventName == null || inFuncName == null)
	return;

    if (inElement.removeEventListener)				// Gecko / W3C
    	inElement.removeEventListener(inEventName, inFuncName, true);
    else if (inElement.detachEvent)					// IE
    	inElement.detachEvent("on" + inEventName, inFuncName);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Used on the onmouseover intrinsic event of a HTML button to simulate that it has been pressed simply on mouse hover.		 */
/* Also see 'onMouseOutButton'													 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function onMouseOverButton(inButton)
{
    if (inButton == null)
    	return;

    inButton.style.border = '2px inset white';
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Used on the onmouseout intrinsic event of a HTML button to restore the styling that was changed by 'onMouseOverButton'.	 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function onMouseOutButton(inButton)
{
    if (inButton == null)
	return;

    inButton.style.border = '2px outset white';
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* This function will prevent the 'tab' key from having impact on the page. All other keys will be allowed to continue as	 */
/* per usual.															 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function swallowTabEvent(inEvent)
{
    var event = (!inEvent) ? window.event : inEvent;

    if (event.keyCode == 9)
    {
		killEvent(event);
		return(false);
    }

    return(true);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Will terminate 'inEvent' or the last event which has occurred 'window.event'. The outcome is as if no event ever occurred.	 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function killEvent(inEvent)
{
    var event = (!inEvent) ? window.event : inEvent;

    if (event.stopPropagation)
    {
		event.preventDefault();
		event.stopPropagation();
    }
    else
    {
		event.returnValue  = false;
		event.cancelBubble = true;
    }
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Shows/hides all HTML select elements that are on the page, so that they don't interfere with the display of layers on a web	 */
/* page which have a z-index higher than 0. This is only needed for the reasons explained in BQ0024.				 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function toggleSelectElemDisplay(inBrowseElem, inLeft, inTop)
{
    var selectElemArray = document.getElementsByTagName('select');

    /* If there aren't any select elements then don't do anything */

    if (selectElemArray.length == 0)
    	return;

    var browse = (typeof inBrowseElem == 'object' ? inBrowseElem : document.getElementById(inBrowseElem));

    /* If the browse window is hidden then you want to make all the select boxes visible */

    if (!isDisplayed(browse))
    {
    	for (var i = 0; i < selectElemArray.length; i++)
    		selectElemArray[i].style.visibility = 'visible';

    	return;
    }

    /* Otherwise see what is below the browse window and hide it */

    var bl = inLeft;			// left border
    var br = bl + browse.offsetWidth;	// right border
    var bt = inTop;			// top border
    var bb = bt + browse.offsetHeight;	// bottom border

    for (var i = 0; i < selectElemArray.length; i++)
    {
		var e = selectElemArray[i];
	
		if (e.style.visibility == 'hidden')
		    continue;
	
		var p = findAbsolutePos(e);
	
		var el = p.left;		// left border
		var er = el + e.offsetWidth;	// right border
		var et = p.top;			// top border
		var eb = et + e.offsetHeight;	// bottom border
	
		/* Visibility is used instead of display since we don't want the page layout affected by calling this function */
	
		if (el <= br && er >= bl && et <= bb && eb >= bt)
		    e.style.visibility = 'hidden';
    }
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* This function determines if the 'inEvent' which has occurred originated from a target source which has the same class as	 */
/* inStyleClass or is a child (doesn't have to be direct) of an element with this class.						 */
/*																 */
/* If the event is determined to originate from such a source this function will return true, otherwise false will be returned.	 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function isEventFromClass(inEvent, inStyleClass)
{
    if (inStyleClass == null)
	return(false);

    var targ;
    var event = (!inEvent) ? window.event : inEvent;

    if (event.target)
    	targ = event.target;
    else if (event.srcElement)
    	targ = event.srcElement;

    for(var elem = targ; elem.parentNode != null; elem = elem.parentNode)
    {
		if (elem.className != null && elem.className == inStyleClass)
		    return(true);
    }

    return(false);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* This function determines the absolute position of inElement. [Only required for relatively placed elements]			 */
/*																 */
/* inElement can either be a string or an object. If it is a string, the element with the corresponding id is found and the	 */
/* absolute position of it is calculated. If it is an object then the absolute position of the object is found.			 */
/*																 */
/* Returns a coordinate object which will contain the left and top values for inElement.					 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function findAbsolutePos(inElement)
{
    var elem = (typeof inElement == 'object' ? inElement : document.getElementById(inElement));

    if (elem == null)
    	return(false);

    var left = 0;
    var top  = 0;

    for (; elem.offsetParent; elem = elem.offsetParent)
    {
		left += elem.offsetLeft;
		top  += elem.offsetTop;
    }

    return(new coordinate(left, top));
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Coordinate object.														 */
/*																 */
/* Parameters: inLeft - the left value (X coordinate).										 */
/*	       inTop  - the top value (Y coordinate).										 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function coordinate(inLeft, inTop)
{
    this.left = inLeft;
    this.top  = inTop;
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Handles the display and event management of a layer acting as an independent window.						 */
/*																 */
/* When this function displays the window, register an event listener to manage a mouse click event, and an event listener	 */
/* to prevent the user tabbing.													 */
/* The window will open top aligned to the bottom left corner of the position element.						 */
/*																 */
/* When this function hides the window, remove the event listeners which were discussed above.					 */
/*																 */
/* Shows/hides all HTML select elements that are on the page, so that they don't interfere with the display of the window BQ0024. */
/*																 */
/* inWindowId:		 the Id of the window.											 */
/* inPositionElemId:	 the element which will be used as a guide to position where a window should be opened.			 */
/* inOnMouseClickFunc:	 the function which handles a mouse click event for the window.						 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function toggleLayerWindow(inDivId, inPositionElemId, inOnMouseClickFunc)
{
    var window	     = document.getElementById(inDivId);
    var positionElem = document.getElementById(inPositionElemId)
    var left	     = 0;
    var top	     = 0;

    // Add/remove event listeners to the document.
    handleEventListenterForLayerWindow(inDivId, inOnMouseClickFunc);

    if (!isDisplayed(window))
    {
    	// Work out the co-ordinates of the position element.

    	var coordinate = findAbsolutePos(positionElem);
    	left	       = coordinate.left;
    	top	       = coordinate.top + positionElem.offsetHeight;

    	window.style.left = left + 'px';
    	window.style.top  = top + 'px';
    }

    toggleDisplay(window);

    // Show/hide all HTML select elements that are on the page BQ0024.

    toggleSelectElemDisplay(window, left, top);

    if (isDisplayed(window))
    	scrollToWindow(inDivId);
}

function toogleLayerWindowInTheCentre(inWindowId, inOnMouseClickFunc)
{	
	 // TODO: window is a global variable in the browsers.  The code below is at best confusing, at worst, a ticking time 
	 // Please rename.
	 var window		= document.getElementById(inWindowId);
	 var leftPos    = 0;
	 var topPos	    = 0;

	 // Add/remove event listeners to the document.
	 handleEventListenterForLayerWindow(inWindowId, inOnMouseClickFunc);

	 if (!isDisplayed(window))
	 {
		 var geometry = getBrowserGeometry();
		 var leftPos = Math.floor(geometry['left'] + (geometry['width']  / 2) - 200);
		 var topPos	= Math.floor(geometry['top']  + (geometry['height'] / 2) - 75);

		 window.style.left = leftPos + 'px';
		 window.style.top  = topPos  + 'px';
	 }

	 toggleDisplay(window);

	// Show/hide all HTML select elements that are on the page BQ0024.
	toggleSelectElemDisplay(window, leftPos, topPos);

	if (isDisplayed(window))
		scrollToWindow(inWindowId);
}

function handleEventListenterForLayerWindow(inWindowId, inOnMouseClickFunc)
{
	 // TODO: window is a global variable in browsers.  The code below is at best confusing, at worst, a ticking time bomb. 
	 // Please rename.	
	var window	     = document.getElementById(inWindowId);

	if (!isDisplayed(window))
    {
    	tt_addEvent(document, "click",    inOnMouseClickFunc);
    	tt_addEvent(document, "keypress", swallowTabEvent);		// NN
    	tt_addEvent(document, "keydown",  swallowTabEvent);		// IE
    }
    else
    {
    	tt_removeEvent(document, "click", inOnMouseClickFunc);
    	tt_removeEvent(document, "keypress", swallowTabEvent);
    	tt_removeEvent(document, "keydown", swallowTabEvent);
    }
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Scrolls the page so that the whole of the specified window is visible.							 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function scrollToWindow(inWindow)
{
    var windowObj = (typeof inWindow == 'object' ? inWindow : document.getElementById(inWindow));

    var pos = findAbsolutePos(windowObj);

    // window.scrollY and window.innerHeight used by firefox.
    // document.body.scrollTop and document.body.clientHeight are used by IE.

    var browserBottom = window.scrollY ? (window.scrollY + window.innerHeight) :
					 (document.body.scrollTop + document.body.clientHeight);

    var windowBottom = pos.top + windowObj.offsetHeight;

    if (windowBottom > browserBottom)
	window.scrollBy(0, windowBottom - browserBottom);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Will handle a mouse click when a warning window is open.									 */
/*																 */
/* If the mouse is clicked on anything except for the warning window the event will be captured and dispensed with.		 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function onMouseClickPopupWindow(inEvent)
{
    if (!isEventFromClass(inEvent, 'global-warning'))
    {
		killEvent(inEvent);
		return(false);
    }

    return(true);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Will handle a mouse click when a wait notice window is open.									 */
/*																 */
/* If the the mouse is clicked on certain elements the event will be captured and dispensed with. BQ 0026			 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function onMouseClickWaitWindow(inEvent)
{
    var event = (!inEvent) ? window.event : inEvent;

    killEvent(event);
    return(false);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Show agency warning window and then place focus on the acknowledge button.							 */
/*																 */
/* Firefox doesn't like working out the position of hidden elements, so display the 'generalError' element so that we can
/* perform our calculations and then hide it again once we are done.								 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function showAgencyWarning()
{
    // Firefox doesn't like working out the position of hidden elements, so display and then hide it when done.
    toggleDisplay('generalError');

    toggleLayerWindow('warningWindow', 'generalError', onMouseClickPopupWindow);

    toggleDisplay('generalError');

    FocusOnElement('ackButton');
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Show information messages window and then place focus on the acknowledge button.						 */
/*																 */
/* Firefox doesn't like working out the position of hidden elements, so display the 'generalError' element so that we can
/* perform our calculations and then hide it again once we are done.								 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function showInfoMsgs()
{
    // Firefox doesn't like working out the position of hidden elements, so display and then hide it when done.
    toggleDisplay('generalError');

    toggleLayerWindow('infoMsgsWindow', 'generalError', onMouseClickPopupWindow);

    toggleDisplay('generalError');

    FocusOnElement('ackButton');
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Show wait notice window.													 */
/*																 */
/* 1. Display the wait notice at the center of the browser window, hiding any select boxes behind it.				 */
/* 2. Attach key and mouse event handlers.											 */
/* 3. Disable all buttons (normal/submit) that are present on the page. Buttons are disabled due to BQ0026.			 */
/*																 */
/* NB This function attaches positionWaitNotice() to the onscroll window event, so that the wait notice will always be centered	 */
/*    in the browser window even if the user scrolls the viewable page.								 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function showWaitNotice()
{
    if (document.getElementById('waitNotice') == null)
	return;

    setDisplay('waitNotice', '');

    positionWaitNotice();

    tt_addEvent(document, "click",	  cancelWaitNotice);
    tt_addEvent(document, "keypress", cancelWaitNotice); // NN
    tt_addEvent(document, "keydown",  cancelWaitNotice); // IE

    /* Disable all buttons (normal/submit) that are present on the page								*/

    var inputElements = document.getElementsByTagName('input');

    for (var i = 0; i < inputElements.length; i++)
    {
		if (inputElements[i].type == 'button' || inputElements[i].type == 'submit')
		    setDisabled(inputElements[i], true);
    }

    window.onscroll = positionWaitNotice;
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Position the wait notice													 */
/*																 */
/* 1. Will first display all select dropdowns on the page (this is required as this function is called everytime the		 */
/*    the user scrolls the current page, as we reposition the wait notice we want to redisplay any select elements that might	 */
/*    have been hidden by the toggleSelectElemDisplay() function)								 */
/* 2. Position the wait notice in the center of the browser window.								 */
/* 3. Call toggleSelectElemDisplay() to hide any select boxes behind the new position of the wait notice.			 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function positionWaitNotice()
{
    var waitWindow = document.getElementById('waitNotice');

    var geometry = getBrowserGeometry();

    /* We want to show any select boxes which might have previously been hidden by toggleSelectElemDisplay().			 */

    var selectElemArray = document.getElementsByTagName('select');

    for (var i = 0; i < selectElemArray.length; i++)
    	selectElemArray[i].style.visibility = 'visible';

    var leftPos = Math.floor(geometry['left'] + (geometry['width']  / 2) - 200);
    var topPos	= Math.floor(geometry['top']  + (geometry['height'] / 2) - 75);

    waitWindow.style.left = leftPos + 'px';
    waitWindow.style.top  = topPos  + 'px';

    /* We want to hide any select boxes behind the new position of the wait notice.						 */

    toggleSelectElemDisplay(waitWindow, leftPos, topPos);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Hides the wait notice when the escape key is pressed.									 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function cancelWaitNotice(inEvent)
{
    var event = (!inEvent) ? window.event : inEvent;

    if (event.keyCode == 27)
    {
		hideWaitNotice();
		return(true);
    }

    killEvent(event);
    return(false);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Hide the wait notice and perform the required tasks to restore prior state.							 */
/*																 */
/* 1. Hide the wait notice window.												 */
/* 2. Remove the key and mouse event handlers set up in showWaitNotice().							 */
/* 3. Enable all buttons (normal/submit) that are present on the page that were disabled in showWaitNotice().			 */
/* 4. Show all select dropdown buttons that were hidden in showWaitNotice().							 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function hideWaitNotice()
{
    var waitWindow = document.getElementById('waitNotice');

    if (waitWindow == null)
    	return;

    setDisplay(waitWindow, 'none');

    tt_removeEvent(document, "click",    cancelWaitNotice);
    tt_removeEvent(document, "keypress", cancelWaitNotice); // NN
    tt_removeEvent(document, "keydown",  cancelWaitNotice); // IE

    /* Enable all buttons (normal/submit) that are present on the page that were disable when the wait notice was displayed.	 */

    var inputElements = document.getElementsByTagName('input');

    for (var i = 0; i < inputElements.length; i++)
    {
		if (inputElements[i].type == 'button' || inputElements[i].type == 'submit')
		    setDisabled(inputElements[i], false);
    }

    /* Show all select dropdown buttons that were hidden when the wait notice was displayed.					*/

    toggleSelectElemDisplay(waitWindow, 0, 0)
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* A convenience function to abstract away the browser specific methods of determining the geometry of the browser window.	 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function getBrowserGeometry()
{
    var geometry = new Array(4);

    geometry['top']    = window.scrollY	    ? window.scrollY	 : document.body.scrollTop;
    geometry['left']   = window.scrollX	    ? window.scrollY	 : document.body.scrollLeft;
    geometry['width']  = window.innerWidth  ? window.innerWidth	 : document.body.clientWidth;
    geometry['height'] = window.innerHeight ? window.innerHeight : document.body.clientHeight;

    return(geometry);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Toggle an element in a hierarchial list between the expanded and collapsed states (Eg. geobrowse.jspi).			 */
/*																 */
/* Parameters:	inId	The id attribute of the element fo the list of child nodes to toggle. Also used to construct the id	 */
/*			attribute of the list indicator (image).								 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function toggleHierElem(inId)
{
    var listIndicator = document.getElementById(inId + "Ind");
    var childList     = document.getElementById(inId);
    var displayed     = false;

    /* If the child list was displayed (expanded), hide (collapse) it. And vice-versa.						 */

    if (childList != null)
    {
    	displayed = (childList.style.display != 'none');
    	childList.style.display = displayed ? 'none' : '';
    }

    if (listIndicator != null)
    {
    	listIndicator.className = displayed ? "list-collapsed" : "list-expanded";
	}
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Return the value of the passed HTML element.											 */
/* - inElementId is the id of the element which contains the value that is desired.						 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function getElementValue(inElementId)
{
    if (inElementId == null || inElementId == "")
    	return("");

    var element = document.getElementById(inElementId);

    if (element == null)
    	return("");

    return(element.value);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Return the name of the passed HTML element.											 */
/* - inElementId is the id of the element for which the name is desired.							 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function getElementName(inElementId)
{
    if (inElementId == null || inElementId == "")
    	return("");

    var element = document.getElementById(inElementId);

    if (element == null)
    	return("");

    return(element.name);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Render the passed general error message within the generalError id declared within statuserr.jspi.				 */
/* If the passed message string is empty, the function simply hides the general message area.					 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function showGeneralError(inErrorMsg)
{
    if (inErrorMsg != "")
    {
		document.getElementById('generalError').innerHTML =  '<table class="global-errmsg" cellpadding="0" cellspacing="0">' +
									'<tr><td>' +
									inErrorMsg + '</td></tr>' +
								     '</table>';
		setDisplay('generalError', "");
		return;
    }

    setDisplay('generalError', 'none');
    return;
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Renders the passed field error message within the passed element, and give focus to the passed field.			 */
/* If the passed message string is empty, the function simply hides the message area.						 */
/*																 */
/* Parameters:															 */
/*    inFieldId		The "id" of the field which will receive focus if an error applies,					 */
/*			pass an empty string if focus is not to be changed;							 */
/*																 */
/*    inRowId		The "id" of the row element that contains the error message cell,					 */
/*			this will be hidden if error message is empty;								 */
/*																 */
/*    inCellId		The "id" of the cell element that contains the error message,						 */
/*			pass an empty string if message is to be removed;							 */
/*																 */
/*    inErrorMsg	The actual error message to be displayed, or empty string to remove an existing message.		 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function showFieldError(inFieldId, inRowId, inCellId, inErrorMsg)
{
    if (inErrorMsg != "")
    {
	document.getElementById(inCellId).innerHTML =  '<span class="error-text">' + inErrorMsg + '</span>';
	setDisplay(inRowId, "");

	if (inFieldId != null)
	    FocusOnElement(inFieldId);

	return;
    }

    setDisplay(inRowId, 'none');
    return;
}

/* ============================================================================================================================= */
/* QPL and product information list specific functions.										 */
/* ============================================================================================================================= */

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Global variables used by QPL and product information list functions.								 */
/*																 */
/* Keep track of the product index for the currently selected radio button and the supplier in which that radio button occurs.	 */
/*																 */
/* We do this only because when we collapse the products for a supplier, we need to know if we should uncheck the current radio	 */
/* button (as it will be hidden).												 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

var currentProductIdx = null;
var currentSupplierIdx = null;

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Expand/collapse the products for a supplier in a QPL or product information list.						 */
/*																 */
/* Parameters:															 */
/*	  inSupplierIdx		Must be specified. The index of the supplier whose products are to be expanded/collapsed.	 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function pLstS(inSupplierIdx)
{
    /* Toggle the display attribute for the <tbody> element containing the products for the clicked supplier.			 */

    var prdTbody = document.getElementById("prdTbody" + inSupplierIdx);

    if (prdTbody == null)
    	return;

    /* True if we are collapsing the products for inSupplierIdx.								 */

    var collapsing = (prdTbody.style.display == "");

    /* Swap the display state of the <tbody> element containing all the products for the specified supplier.			 */

    prdTbody.style.display = collapsing ? 'none' : '';

    /* Toggle the expanded/collapsed indicator image for the clicked supplier.							 */

    var supImg = document.getElementById("supImg" + inSupplierIdx);

    if (supImg != null)
    	supImg.className = collapsing ? "plst-prd-collapsed" : "plst-prd-expanded";

    /* If we are collapsing the current supplier, and one of its products is selected, deselect that product.			 */

    if (collapsing && currentSupplierIdx == inSupplierIdx)
    {
		CheckElement("prd" + currentProductIdx, false);
		pLstP(null, null);
    }
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Event handler for onclick of product radio buttons in 2-level QPL or product information list. Store the supplier belonging	 */
/* Store the supplier belonging to the current radio button for use by pLstS().							 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function pLstP(inSupplierIdx, inProductIdx)
{
    currentSupplierIdx = inSupplierIdx;
    currentProductIdx  = inProductIdx;
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Reset the global variables used by pLstP() and pLstS().									 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function resetPLstGlobals()
{
    currentSupplierIdx = null;
    currentProductIdx  = null;
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Should be called onLoad by pages with two level product lists.								 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function onLoadPLst()
{
    if (currentSupplierIdx != null && currentProductIdx != null)
    	pLstS(currentSupplierIdx, currentProductIdx);
}

/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Callback onclick function for product list links that forward current form parameters.					 */
/* Eg:	    "info" links require start date, duration, rooming for product pricing.						 */
/*	    "check avail" links in product information search page must	detect search criteria changes.				 */
/*	    "check avail" links in package expansion pages must	submit itinerary step number.					 */
/*																 */
/* In 2-level product lists, the currently selected radio button is maintained in a javascript variable via the			 */
/* onclick handler of each radio button. To make sure nothing wierd happens if the user clicks the link then stop,		 */
/* invoke the radio onclick.													 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function doPLstFwd(inRadioElementId, inFormId, inForward, inShowWaitNotice)
{
	var radioElement = document.getElementById(inRadioElementId);

    radioElement.checked = true;

    if (radioElement.onclick != null)
    	radioElement.onclick();

    if (inShowWaitNotice != null)
    	showWaitNotice();

    var myForm = document.getElementById(inFormId);

    myForm.action = inForward;
    myForm.submit();
}
/* ----------------------------------------------------------------------------------------------------------------------------- */
/* Hide the availability calendar.																								 */
/* ----------------------------------------------------------------------------------------------------------------------------- */

function hideAvailabilityCalendar()
{
    setDisplay('ac-content',	  'none');
    setDisplay('ac-title',		  'none');
    setDisplay('ac-close',		  'none');
    setDisplay('ac-header',	  	  'none');
    setDisplay('ac-wrapper',	  'none');
    setDisplay('ac-container',	  'none');
    setDisplay('availabilitycal', 'none');
}
