// --> Utility functions for general use


// --> Creates a popup window and loads the specified URL

function popup (url) {
  wid = window.open (url, 'POPUP', 'scrollbars,status,height=400,width=300');
  wid.focus();
}


// --> Creates a new standard size window and loads the specified URL

function newWindow (url, winName) {
  wid = window.open (url, winName, 'scrollbars,status,height=700,width=800');
  wid.focus();
}


// --> Sets the initial position of a drop-down menu

function setDropdown (dropdown, value) {

  flag = 0;
  for (ii=0; ii<dropdown.options.length; ii++) {
if (flag) {
  alert ('ii,value,options[ii].value = ' + ii + ', ' + value + ', ' + dropdown.options[ii].value);
}
    if (dropdown.options[ii].value == value) {
      dropdown.selectedIndex = ii;
      flag = 0;
    }
  }
  return true;
}


// --> Disable/Enable out a list of form elements. The array must be DOM references

function disableElement (formElement, disable) {
	for (ii=0; ii<formElement.length; ii++) {
		formElement[ii].disabled = disable;
	}
}
function enableIpElements (form, checked) {
	disList = new Array();
	for (ii=1; ii<=form.ipCount.value; ii++) {
		for (jj=1; jj<=4; jj++) {
			tmps = "ip" + ii + "_q" + jj;
			disList[disList.length] = form[tmps];
		}
	}
	disableElement (disList, !checked);
}

// --> MouseOver help controls

/*	Define the HelpText object creator. It takes 1-3 arguments. The first
 *	argument is the help text. The second argumnet (optional) is the
 *	width of the displayed text. The third argument (optional) is the
 *	height of the displayed text. Default values are provided for the
 *	second and third arguments.
 *
 *	Note that IE always positions a SELECT element above any CSS-controled
 *	feature. The only way to "fix" this is to use an iFrame above the
 *	page (higher z-index) and matching the overlay help box.
*/


var globalPosition = new Array();


function HelpText (text, width, height) {
  defaultWidth = 300;
  defaultHeight = 40;

  this.text = text;
  if (HelpText.arguments.length >= 1) {
    this.width = width;
    if (HelpText.arguments.length >= 2) {
      this.height = height;
     } else {
      this.height = defaultHeight;
    }
   } else {
    this.width = defaultWidth;
    this.height = defaultHeight;
  }
}


/* --> Display the help text. There must be an element with ID
 *	'HelpTextWindow' defined on the page. This routine uses
 *	that window to positon, size, and display the text for
 *	the requested topic. If no topic is specified, the 
 *	element is hidden on the page.
*/

function helpText (topic) {
  if (topic == undefined) {
    return;

   } else if (topic == '') {
    id = document.getElementById ('HelpTextWindow');
    id.style.display = 'none';

   } else {

    id = document.getElementById ('HelpTextWindow');

    id.innerHTML     = Help[topic].text;
//    id.innerHTML     = '(' + globalPosition[0] + ', ' + globalPosition[1] + ') ' + Help[topic].text;
    id.style.width   = Help[topic].width + 'px';
    id.style.height  = Help[topic].height + 'px';
    id.style.left    = (globalPosition[0] - 40) + 'px';
    id.style.top     = (globalPosition[1] - (Help[topic].height + 10)) + 'px';
    id.style.display = 'block';

  }
  return;
}


// --> Loads the help for the specified group. This needs to be
//	called prior to the end of the document.

function helpTextLoad (helpGroup) {
  document.writeln ('<script language="JavaScript" src="../scripts/help-'+helpGroup+'.js"></script>');
  return;
}


/* --> Taken from http://www.thescripts.com/forum/thread149029.html
 *	Addition of scrolling displacement by Leonard Daly
*/

var movement = (function(e, topic) {

/* The default getCoordinates (gC) function. If neither the
 * Microsoft or DOM approach is deemed supported, this will be
 * used to always return (0, 0).
*/
function gC(e) {return {x: 0, y: 0};}

/* The DOM getCoordinates (dC) function. */
function dC(e) {
    return {
	x: e.clientX + ((document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft),
	y: e.clientY + ((document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop)
    };
}

/* The Microsoft getCoordinates (mC) function. */
function mC(e) {
    return {
	x: e.x + ((document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft),
	y: e.x + ((document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop)
	};
}

/* Tests if the given argument is a number. */
function isN(o) {return 'number' == typeof o;}

/* Check if the clientX and clientY event properties are
 * supported. If so, replace the default, gC, with dC.
*/
if (isN(e.clientX) && isN(e.clientY)) {gC = dC;}

/* If not, try again with the x and y properties and replace
 * gC with mC if successful.
*/
  else if(isN(e.x) && isN(e.x)) {gC = mC;}

/* Now our testing is out of the way, replace the initial
 * function with a streamlined version and call it.
*/
(movement = function(e, topic) {
var div = gC(e);

/* [save output using div.x and div.y] */
  globalPosition[0] = div.x;
  globalPosition[1] = div.y;

  helpText (topic);

})(e);
});

