<!--
  // Utility Functions for CGI
  // $Id: cgiutils.js,v 1.13 2007/10/15 17:38:51 caran Exp $
  //
  // These are generic functions used in CGI forms.
  //
  // get_button( selectObj ) - Returns the value of the checked button
  //
  // get_option( selectObj ) - Returns the index of the option selected (or -1)
  //
  // limit_keys( selectObj, type, evt ) - Limit key presses to a valid subset
  //
  // no_init_space( selectObj, evt ) - No white space at beginning of input
  //
  // open_popup( url, name, attributes ) - Open a popup window
  //
  // print_status( test, status ) - Set (and clear) the status bar.
  //
  // test_keycode( stringObj, keycode ) - Test if a keyCode is in a string
  //
  // update_buttons( selectObj, value ) - Choose correct radio/select button
  //
  // update_menu( selectObj, value ) - Choose the single correct menu option
  //
 
// Function to retrieve radio button value
function get_button( selectObj )
 {
  var btn_val = '';
  var i = 0;

  while (!btn_val && (i < selectObj.length)) {
    if (selectObj[i].checked) {
      btn_val = selectObj[i].value;
     }
    i++;
   }

  return (btn_val);

 } // End of get_button()

// Function to retrieve option selected
function get_option( selectObj )
 {
  var opt_val = -1;
  var i = 0;
  while ((opt_val < 0) && (i < selectObj.length)) {
    if (selectObj.options[i].selected) {
      opt_val = i;
     }
    i++;
   }

  return (opt_val);

 } // End of get_option()

//
// Function to limit key presses for phone numbers, zip codes, letters, etc
// To use: <input ... onKeyPress="return limit_keys( this, 'numeric', event )">
//
function limit_keys( selectObj, type, evt )
 {
  var keyCode = 0;
  var ret_val = true;

  if (evt) {
    keyCode = evt.keyCode || evt.which;
   }
  else {
    // The old version of this file did not use the evt parameter and would
    // only work under IE.
    keyCode = window.event.keyCode;
   }

  // Allow special characters: BACKSPACE, TAB, RETURN, LEFT ARROW,
  // RIGHT ARROW to go through
  if ((keyCode == 8) || (keyCode == 9) || (keyCode == 13)
   || (keyCode == 37) || (keyCode == 39)) {
    return (ret_val);
   }

  if (type == 'phone') {
    // Numeric values and punctuation are OK
    ret_val = test_keycode( '0123456789()-.', keyCode );
   }
  else if (type == 'alphanum') {
    ret_val = ((keyCode >= 48) && (keyCode <= 57))
			|| ((keyCode >= 65) && (keyCode <= 90))
			|| ((keyCode >= 97) && (keyCode <= 122));
   }
  else if (type == 'numeric') {
    // Simply test for a numeric value
    ret_val = ((keyCode >= 48) && (keyCode <= 57));
   }

  return (ret_val);

 } // End of limit_keys()

//
// Function to prevent spaces at the beginning of a form input
// To use: <input ... onKeyPress="return no_init_space( this, event )">
//
function no_init_space( selectObj, evt )
 {
  var keyCode = 0;
  var ret_val = true;

  if (evt) {
    keyCode = evt.keyCode || evt.which;
   }
  else {
    // The old version of this file did not use the evt parameter and would
    // only work under IE.
    keyCode = window.event.keyCode;
   }

  // No spaces allowed for the first character entered
  if (selectObj.value.length == 0) {
    if (keyCode == 32) {
      ret_val = false;
     }
   }

  return (ret_val);

 } // End of no_init_space()

//
// Function to open a popup window
// Usage: <a href="javascript:open_popup( 'my_script.pl', 'ScriptTitle',
//			'height=520,width=400,status=0,scrollbars=1,resizable=1');">
//
// NOTE: The 'name' parameter can't have spaces in it
//
function open_popup( url, name, attributes )
 {
  // Make sure the name doesn't have spaces in it
  name = name.replace( /\s+/g, '' );

  var windowHandle = window.open( url, name, attributes );
  windowHandle.focus();

  return windowHandle;

 } // End of open_popup()

//
// Function to set (and clear) the status bar.
// Usage: onMouseOver="return print_status( true, 'Message' );"
//			onMouseOut="return print_status();"
//
function print_status( test, status ) {

  if (test && status) {
    window.status = status;
   }
  else {
    window.status = '';
   }

  return true;
 }

//
// setStyleByClass: given an element type and a class selector,
// style property and value, apply the style.
// args:
//  t - type of tag to check for (e.g., SPAN) (* - wildcard)
//  c - class name
//  p - CSS property
//  v - value
//

function setStyleByClass( t, c, p, v )
 {
  var elements;
  var ie = (document.all) ? true : false;

  if (t == '*') {
    // '*' not supported by IE/Win 5.5 and below
    elements = (ie) ? document.all : document.getElementsByTagName( '*' );
   }
  else {
    elements = document.getElementsByTagName( t );
   }

  for (var i = 0; i < elements.length; i++) {
    var node = elements.item(i);
    for (var j = 0; j < node.attributes.length; j++) {
      if (node.attributes.item( j ).nodeName == 'class') {
        if (node.attributes.item(j).nodeValue == c) {
          eval( 'node.style.' + p + " = '" +v + "'" );
         }
       }
     }
   }
 } // End of setStyleByClass()

// Function to test if a keyCode is in a certain string
function test_keycode( stringObj, keycode )
 {
  var ret_val = false;
  var i = 0;

  while ((i < stringObj.length) && !ret_val) {
    if (keycode == stringObj.charCodeAt( i )) {
      ret_val = true;
     }
    else {
      i++;
     }
   }

  return (ret_val);

 } // End of test_keycode()

// Function to select correct radio/select button
function update_buttons( selectObj, value )
 {
  // Make sure object exists
  if (!selectObj) {
    return;
   }

  if (value) {
    // Iterate through the select object, searching for value
    for (var i = 0; i < selectObj.length; i++) {
      var chk = (selectObj[i].value == value);
      selectObj[i].checked = chk;
      selectObj[i].defaultChecked = chk;
     }
   }
 } // End of update_buttons()

//
// Function to select the single correct menu choice option
//
function update_menu( selectObj, value )
 {
  var selectedIndex = -1;

  // Make sure object exists
  if (!selectObj) {
    return;
   }

  if (value) {
    // Iterate through the select object, searching for value
    var i = 0;
    while ((selectedIndex < 0) && (i < selectObj.length)) {
      // Check the option value (or the text if value isn't present)
      var option_value = selectObj.options[i].value;
      if (!option_value) {
        option_value = selectObj.options[i].text;
       }

      if (option_value == value) {
        selectedIndex = i;
       }
      i++;
     }

    // Select this option and make it the default
    if (selectedIndex >= 0) {
      selectObj.selectedIndex = selectedIndex;
      selectObj.options[selectedIndex].defaultSelected = 1;
     }
   }

 } // End of update_menu()

//
// AJAX - getHTTPObject() - take from http://www.webpasties.com/xmlHttpRequest/
//
function getHTTPObject()
 {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     }
    catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       }
      catch (E) {
        xmlhttp = false;
       }
     }
  @else
    xmlhttp = false;
  @end @*/  

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
     }
    catch (e) {
      xmlhttp = false;
     }
   }
  return xmlhttp;

 } // End of getHTTPObject()

// -->

