

function addEvent(elm, evType, fn, useCapture) {
// Generic function that adds events handlers to document objects. The function 'fn' is added as an 'evType' event handler for 
// the document element 'elm'. The last parameter "useCapture", refers to whether the event handler should be executed in the 
// capturing (true) or bubbling (false) phase.  It expects a value of true or false, and defaults to false.
    // useCapture = (typeof(useCapture)=="undefined") ? false : true;
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}

function openWindow(url, width, height, left, top) {
// Used to open popup windows. The first 3 parameters are required, the rest are not. If the last 2 params are not specified
// the window will open either in the middle of the page (if a modern browser), or else near the middle of the page (for
// older browsers). This function is called in the footer of public pages.
  if (typeof left == "undefined") {
    if (screen) left = (screen.availWidth - width)/2;
    else left = (640 - width)/2;
  }
  if (typeof top == "undefined") {
    if (screen) top = (screen.availHeight - height)/2;
    else top = (480 - height)/2;
  }
  var windowPos = 'screenX='+left+',screenY='+top+',left='+left+',top='+top;
  var w = window.open(url, 'popupWindow','height='+height+',width='+width+','+windowPos+',scrollbars=yes');
  return w;
}

// Reloads the page when the user chooses a state from the list of Australian states. This is then saved to session
// memory
function saveStateOfAustralia(states, url)
{
	// If there already is a querystring, append to it
	var pos = url.lastIndexOf("?");
	// Are there QS arguments?
	var hasArgs = false;
	if ((pos > -1) && (pos < url.length)) hasArgs = true;
	// If a particular state was selected, pass the ID of the state in the QS
	var stateId = states.options[states.selectedIndex].value;
	if (pos == -1) location.href = url + "?stateId=" + stateId;
	else if (hasArgs) location.href = url + "&stateId=" + stateId;
}
