function rtrim(str) {
  var i = 0;
  for (i = str.length - 1; i > -1; i--) {
    if (str.charAt(i) != ' ') break;
  }
  return str.substring(0, i+1);
}
function ltrim(str) {
  var trimStr = "";
  var i = 0;
  for (i = 0; i < str.length; i++) {
    if (str.charAt(i) != ' ') break;
  }
  if (i < str.length) trimStr = str.substring(i, str.length);
  return trimStr;
}
function trim(str) {
  return ltrim(rtrim(str));
}
function selectAll(elementName, b) {
  var theSel = getElement(elementName);
  var i;
  if (theSel != null) { 
    var numOptions = theSel.length; 
    for (i=0; i<numOptions; i++) {
      theSel[i].selected = b;
    }
  }
}
function findOption(options, optionValue) {
  var numOptions = options.length;
  var i;
  for (i=0; i<numOptions; i++) {
    if (options[i].value == optionValue) {
      return options[i];
    }
  }
  return null;
}
function moveSelected(destElementName, srcElementName) {
  var destSel = getElement(destElementName);
  var srcSel = getElement(srcElementName);

  if (destSel == null || srcSel == null) {
    return;
  }
  var numOptions = srcSel.length;
  var lastIndex = destSel.length;
  var unselectedOptions = new Array();
  var option;
  var selectedCount = 0;
  var i;

  for (i=0; i<numOptions; i++) {
    if (srcSel[i].selected) {
      selectedCount++;
      if (!findOption(destSel, srcSel[i].value)) {
        option = new Option(srcSel[i].label, srcSel[i].value, true, true);
        option.label = srcSel[i].label;
        destSel[lastIndex] = option;
        lastIndex++;
      }
    } else {
      unselectedOptions[unselectedOptions.length] = srcSel[i];
    }
  }

  if (selectedCount > 0) {
    // Leave only options that where not selected.
    // Must clear from the last to the first.
    for (i=srcSel.length-1; i>=0; i--) {
      srcSel[i] = null;
    }
    //srcSel.options = null; -- unimplemented in ie 5.x, do long way above.
    
    for (i=0; i<unselectedOptions.length; i++) {
      srcSel[i] = unselectedOptions[i];
    }
  }
}
function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i;
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
}
function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
}
function getCookie(cookieName) {
  var search = cookieName + "="
  var returnValue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { 
      offset += search.length
      end = document.cookie.indexOf(";", offset);
      if (end == -1) {
        end = document.cookie.length;
      }
      returnValue = unescape(document.cookie.substring(offset, end));
    }
  }
  return returnValue;
}
function setCookie(cookieName, cookieValue) {
  document.cookie=cookieName+"="+cookieValue;
}

var autoTabFieldLength=0;
var previousTabField=null;
function autoTabNext(currentField, keyDir, fieldLength, nextField) {
  if (keyDir == "down") {
    autoTabFieldLength=currentField.value.length;
    previousTabField = currentField;
  } else if (keyDir == "up") {
    if (currentField.value.length != autoTabFieldLength) {
      autoTabFieldLength=currentField.value.length;
      if (previousTabField == currentField && autoTabFieldLength == fieldLength) {
        nextField.focus();
      }
    }
  }
}
var disabledElement=null;
function disableElement(element) {
  disabledElement = element;
  element.disabled = true;
}
function enableElement(element) {
  if (element) {
    element.disabled = false;
    if (disabledElement == element) {
      disabledElement = null;
    }
  }
}
var disabledElement=null;
function disableElement(element) {
  disabledElement = element;
  element.disabled = true;
}
function enableElement(element) {
  if (element) {
    element.disabled = false;
    if (disabledElement == element) {
      disabledElement = null;
    }
  }
}
var disabledLink=null;
function _cancelLink() {
  return false;
}
function disableLink(link) {
  disabledLink = link;
  if (link.onclick) {
    link.oldOnClick = link.onclick;
  }
  link.onclick = _cancelLink;
  if (link.style) {
    link.style.cursor = 'default';
  }
}
function enableLink(link) {
  if (link) {
    link.onclick = link.oldOnClick ? link.oldOnClick : null;
    if (link.style) {
      link.style.cursor = document.all ? 'hand' : 'pointer';
    }
    if (disabledLink == link) {
      disabledLink = null;
    }
  }
}