function isIE()
{
  return navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER';
}

//
// element functions
//

function getRadioGroupValue(rad)
{
  var i;

  for (i=0; i<rad.length; i++)
  {
    if (rad[i].checked)
      return rad[i].value;
  }

  return "undefined";
}

function getSelectValue(sel)
{
  return sel[sel.selectedIndex].value;
}

function setElementClassName(element, className)
{
  element.className = className;
}  

function setElementIdClassName(elementId, className)
{
  document.getElementById(elementId).className = className;
}  

function setElementVisibility(element, visible)
{
  element.style.visibility = (visible) ? "visible" : "hidden";
}

function trimAllTextInputElements(form)
{
  var i;

  for (i=0; i<form.elements.length; i++)
  {
    if (form.elements[i].type == "text")
      form.elements[i].value = trimString(form.elements[i].value);
  }
}

//
// event functions
//

function getKeyPressCharCode(evnt)
{
  if (typeof(evnt.keyCode) != "undefined")  // for IE
    return evnt.keyCode;

  if (typeof(evnt.which) != "undefined")  // for NS
    return evnt.which;

  return 0;
}

//
// array functions
//

function arrayIndexOf(arr, searchElem)
{
  var i;
  
  for (i=0; i<arr.length; i++)
  {
    if (arr[i] == searchElem)
      return i;
  }
  
  return -1;
}

//
// string functions
//

function encode_URI_Component(s)
{
  var i, result, legalChars, c;

  if (typeof encodeURIComponent != "undefined")
    return encodeURIComponent(s);

  result = "";
  legalChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()";
  for (i=0; i<s.length; i++)
  {
    c = s.charAt(i);
    if (legalChars.indexOf(c) != -1)
      result += c;
    else
      result += escape(c);
  }

  return result;
}

function isEmailAddress(s)
{
  // regular expression defines a legal email address as:
  // 1. The user name.
  //    Defined as one or more of "a"-"z", "A"-"Z", "0"-"9", "_", "-", ".", "'"
  // 2. Followed by "@"
  // 3. Followed by the domain name.
  //    Defined as one to four occurrences of: one or more of "a"-"z", "A"-"Z", "0"-"9", "_", "-" then "."\
  // 4. Followed by the top level domain name.
  //    Defined as one or more of "a"-"z", "A"-"Z", "0"-"9"
  var regexp = /^([a-zA-Z0-9_\-\.']+@([a-zA-Z0-9_\-]+\.){1,4}[a-zA-Z0-9]+)$/;
  return regexp.test(s);
}

function isIpAddress(s)
{
  var regexp = /^(\d{3}\.\d{3}\.\d{3}\.\d{3})$/;
  if (!regexp.test(s))
    return false;

  var words = s.split(".");
  if (parseInt(words[0],10)>255 || parseInt(words[1],10)>255 || parseInt(words[2],10)>255 || parseInt(words[3],10)>255)
    return false;

  return true;
}

function isIpAddressRange(s)
{
  var i, words, startIp, endIp;

  words = s.split("-");
  if (words.length<1 || words.length>2)
    return false;

  startIp = words[0];
  endIp = (words.length == 2) ? words[1] : words[0];
  if (!isIpAddress(startIp) || !isIpAddress(endIp) || startIp>endIp)
    return false;

  return true;
}

function ipAddressToInteger(s)
{
  var words = s.split(".");
  return (parseInt(words[0],10)<<24) + (parseInt(words[1],10)<<16) + (parseInt(words[2],10)<<8) + parseInt(words[3],10);
}

function leftTrimString(s)
{
  var i, whiteSpace;

  // if string has non-whitespace characters, return trimmed string
  whiteSpace = " \t\n\r";
  for (i=0; i<s.length; i++)
  {
    if (whiteSpace.indexOf(s.charAt(i)) == -1)
      return s.substring(i, s.length);
  }

  // string is entirely whitespace - return empty string
  return "";
}

function rightTrimString(s)
{
  var i, whiteSpace;

  // if string has non-whitespace characters, return trimmed string
  whiteSpace = " \t\n\r";
  for (i=s.length-1; i>=0; i--)
  {
    if (whiteSpace.indexOf(s.charAt(i)) == -1)
      return s.substring(0, i+1);
  }

  // string is entirely whitespace - return empty string
  return "";
}

function trimString(s)
{
  return leftTrimString(rightTrimString(s));
}

function stringStartsWith(s, startStr)
{
  if (s.length < startStr.length)
    return false;

  return s.substring(0,startStr.length)==startStr;
}

function stringEndsWith(s, endStr)
{
  if (s.length < endStr.length)
    return false;

  return s.substring(s.length-endStr.length)==endStr;
}
