  function trim(str)
  {
    return str.replace(/^\s*|\s*$/g,"");
  }
  function validateInteger( elementId, displayName, low, high )
  {
    var element = document.getElementById( elementId );
    var elementIntValue = parseIntegerFromString( element.value )
    if( elementIntValue == null || elementIntValue < low || elementIntValue > high )
    {
      return invalidate( element, displayName + ' must be an integer between ' + low + ' and ' + high )
    }
    return true;
  }
  //exposed in case we ever need to validate without the trappings of the element and invalidate steps.
  function parseIntegerFromString( passedInString )
  {
    var elementStrValue = trim( passedInString );
    var elementIntValue = 0;
    for( var i = 0; i < elementStrValue.length; i++ )
    {
      var curChar = elementStrValue.charAt(i);
      if( curChar < '0' || curChar > '9' )
      {
        return null;
      }
      elementIntValue = elementIntValue * 10 + (curChar - '0');
    }
    return elementIntValue;
  }
  function validateSelect( elementId, displayName, selectedNullOptionErrorMessage )
  {
    var element = $( elementId );
    var choices = new Array
    for( var i = 0; i < element.options.length; i++ )
    {
      if( element.options[i].selected )
      {
        if( i == 0 && selectedNullOptionErrorMessage )
        {
          return invalidate( element, selectedNullOptionErrorMessage );
        }
        return true;
      }
    }
    return invalidate( element, 'One ' + displayName + ' must be selected.' );
  }
  function validateCheckboxes( elementName, displayName )
  {
    var elements = document.getElementsByName( elementName );
    var checked = false;
    if( elements )
    {
      for( var i = 0; !checked && i < elements.length; i++ )
      {
        if( elements[i].checked )
        {
          checked = true;
        }
      }
    }
    if( !checked )
    {
      return invalidate( null, 'At least one ' + displayName + ' must be checked' );
    }
    return true;
  }
  function validateString( elementId, displayName, minLength, maxLength )
  {
    var element = document.getElementById( elementId );
    var value = trim( element.value );
    if( value.length < minLength || value.length > maxLength )
    {
      return invalidate( element, displayName + ' must be a string between ' + minLength + ' and ' + maxLength + ' characters long.' );
    }
    return true;
  }
  function invalidate( element, message )
  {
    alert( message );
    if( element )
    {
      element.focus();
    }
    return false;
  }
  function showLoadingDiv( hoverOverButtonId )
  {
    //var theElementToHoverOver = document.getElementById( hoverOverButtonId );
    //alert('theElementToHoverOver = ' + theElementToHoverOver);
    //var coordinates = findPosition( theElementToHoverOver );
    //alert('got coords: t: ' + coordinates[1] + ', l: ' + coordinates[0])
    var theLoadingDiv = document.getElementById( 'loadingdiv' );
    //theLoadingDiv.style.top = coordinates[1] + 'px';
    //theLoadingDiv.style.left = coordinates[0] + 'px';
    //TODO: make it actually pop up above whatever menus we are in
    theLoadingDiv.style.display = 'block';
  }
  function hideLoadingDiv( )
  {
    var theLoadingDiv = document.getElementById( 'loadingdiv' );
    theLoadingDiv.style.display = 'none';
  }
  //this doesn't work.
  function findPosition( obj )
  {
    var curtop = 0;
    var curleft = 0;
    if( obj.offsetParent )
    {
      curleft = obj.offsetLeft
      curtop = obj.offsetTop
      while( obj = obj.offsetParent )
      {
        curleft += obj.offsetLeft
        curtop += obj.offsetTop
      }
    }
    return [curleft,curtop];
  }