var AdfDomUtils = new Object;
AdfDomUtils.isVisible = function(domElement) {
  if (domElement == null) {
    return false;
  }
  return domElement.style.display != "none";
};
AdfDomUtils.isInVisibleSubtree = function(node) {
  if (node == null) {
    return false;
  }
  while (node != null) {
    if (node.style && node.style.display == "none") {
      return false;
    }
    node = node.parentNode;
  }
  return true;
};
AdfDomUtils.stripScripts = function(domElement) {
  var children = domElement.childNodes;
  if (children != null) {
    var textNode = null;
    for (var i = children.length - 1;i > -1;i--) {
      textNode = children[i];
      if (textNode.nodeType == 1 && textNode.tagName == "SCRIPT") {
        domElement.removeChild(textNode);
      }
    }
  }
};
AdfDomUtils.setVisible = function(domElement, visible) {
  AdfAssert.assert(domElement != null);
  if (visible === "true" || visible === true) {
    visible = true;
  } else {
    visible = false;
  }
  domElement.style.display = visible ? "" : "none";
};
AdfDomUtils.getOffsetSizeFromCssLength = function(container, cssLength) {
  if (cssLength.indexOf("px") != -1) {
    return parseInt(cssLength, 10);
  }
  var ourDocument = container.ownerDocument;
  var div = ourDocument.createElement("div");
  try {
    div.style.width = cssLength;
  } catch (e) {
    AdfLogger.LOGGER.fine("Exception while trying to  style width to: " + cssLength);
  }
  container.appendChild(div);
  var offset = div.offsetWidth;
  container.removeChild(div);
  return offset;
};
AdfDomUtils.isNodeInDocumentHierarchy = function(domDocument, node) {
  AdfAssert.assertDomNode(domDocument);
  AdfAssert.assertDomNode(node);
  if (node == domDocument) {
    return true;
  }
  if (domDocument == node.ownerDocument) {
    while (node != null) {
      node = node.parentNode;
      if (node == domDocument) {
        return true;
      }
    }
  }
  return false;
};
AdfDomUtils.isAncestor = function(ancestorNode, node) {
  if (node != null) {
    var parentNode = node ? node.parentNode : null;
    while (parentNode) {
      if (parentNode == ancestorNode) {
        return true;
      }
      parentNode = parentNode.parentNode;
    }
  }
  return false;
};
AdfDomUtils.isAncestorOrSelf = function(ancestorNode, node) {
  return node == ancestorNode ? true : AdfDomUtils.isAncestor(ancestorNode, node);
};
AdfDomUtils.getNodeDistance = function(nodeA, nodeB) {
  if (nodeA == nodeB) {
    return 0;
  }
  var distance = 0;
  var parentNode;
  if (AdfDomUtils.isAncestor(nodeA, nodeB)) {
    parentNode = nodeB.parentNode;
    while (parentNode) {
      distance++;
      if (parentNode == nodeA) {
        return distance;
      }
      parentNode = parentNode.parentNode;
    }
  } else {
    if (AdfDomUtils.isAncestor(nodeB, nodeA)) {
      parentNode = nodeA.parentNode;
      while (parentNode) {
        distance++;
        if (parentNode == nodeB) {
          return distance;
        }
        parentNode = parentNode.parentNode;
      }
    }
  }
  var commonAncestor = AdfDomUtils.getCommonAncestor(nodeA, nodeB);
  if (commonAncestor) {
    return AdfDomUtils.getNodeDistance(commonAncestor, nodeA) + AdfDomUtils.getNodeDistance(commonAncestor, nodeB);
  }
  return Number.POSITIVE_INFINITY;
};
AdfDomUtils.getCommonAncestor = function(nodeA, nodeB) {
  if (nodeA == nodeB) {
    return nodeA;
  }
  var parentNode = nodeA;
  while (parentNode) {
    if (AdfDomUtils.isAncestor(parentNode, nodeB)) {
      return parentNode;
    }
    parentNode = parentNode.parentNode;
  }
  return null;
};
AdfDomUtils.getFormElement = function(node) {
  AdfAssert.assertDomNode(node);
  var ourDocument = node.ownerDocument;
  var agent = AdfAgent.AGENT;
  while (node != ourDocument) {
    if (agent.getNodeName(node) == "FORM") {
      return node;
    }
    node = node.parentNode;
    AdfAssert.assert(node, "Should never get this far with disconnected DOM");
  }
  return null;
};
AdfDomUtils.getInputFileForm = function(actionForm) {
  var inputs = actionForm.getElementsByTagName("input"), inputCount = inputs.length, multiPartForm = null;
  for (var i = 0;i < inputCount;++i) {
    var inputElem = inputs[i];
    if (inputElem.type == "file" && (inputElem.value || inputElem.getAttribute("_afrMultiFileUpload") == "true")) {
      multiPartForm = actionForm;
      break;
    }
  }
  return multiPartForm;
};
AdfDomUtils.isMultiPartForm = function(formElement) {
  var enctype = formElement.enctype;
  if (enctype != null && enctype.toLowerCase() == "multipart/form-data") {
    return true;
  }
  return false;
};
AdfDomUtils.getFormElementValue = function(formElement, formElementName) {
  var elements = [];
  var formElements = formElement.elements;
  for (var i = 0, size = formElements.length;i < size;++i) {
    if (formElements[i].getAttribute("name") == formElementName) {
      elements.push(formElements[i]);
    }
  }
  if (elements.length == 0) {
    return null;
  }
  var first = elements[0];
  switch(first.tagName) {
    case "TEXTAREA":
      return first.value;
    case "INPUT":
      var type = first.getAttribute("type");
      switch(type) {
        case "radio":
          for (i = 0, size = elements.length;i < size;++i) {
            if (elements[i].checked) {
              return elements[i].value;
            }
          }
          return null;
        case "checkbox":
          if (elements.length == 1) {
            return first.checked ? first.value : null;
          }
          value = [];
          for (i = 0, size = elements.length;i < size;++i) {
            if (elements[i].checked) {
              value.push(elements[i].value);
            }
          }
          return value;
        case "text":
        ;
        default:
          return first.value;
      }
    ;
    case "SELECT":
      if (first.multiple == true) {
        var value = [];
        var opts = first.options;
        for (i = 0, size = opts.length;i < size;++i) {
          if (opts[i].selected) {
            value.push(opts[i].value);
          }
        }
        return value.length == 0 ? null : value;
      }
      return first.value;
    default:
      return null;
  }
};
AdfDomUtils.getFirstElementByTagName = function(domElement, tagName) {
  if (domElement == null) {
    return null;
  }
  var labelElements = domElement.getElementsByTagName(tagName);
  return labelElements.length ? labelElements[0] : null;
};
AdfDomUtils.getLastElementByTagName = function(domElement, tagName) {
  if (domElement == null) {
    return null;
  }
  var labelElements = domElement.getElementsByTagName(tagName);
  return labelElements.length ? labelElements[labelElements.length - 1] : null;
};
AdfDomUtils.getFirstDescendentElement = function(parentElement, elementName) {
  return AdfDomUtils.getDescendentElement(parentElement, elementName, 0);
};
AdfDomUtils.getDescendentElement = function(parentElement, elementName, index) {
  AdfAssert.assertDomElement(parentElement);
  AdfAssert.assertString(elementName);
  AdfAssert.assertNumber(index);
  var elements = parentElement.getElementsByTagName(elementName);
  AdfAssert.assert(elements);
  var length = elements.length;
  if (length > index) {
    var firstElement = elements[0];
    if (firstElement == parentElement && length > index + 1) {
      index++;
    }
    return elements[index];
  }
  return null;
};
AdfDomUtils.getFirstChildElement = function(parentElement) {
  return AdfDomUtils.getFirstChildNodeByType(parentElement, 1);
};
AdfDomUtils.getElement = function(node) {
  if (node == null) {
    return null;
  }
  for (var n = node;n != null;n = n.parentNode) {
    if (n.nodeType == 1) {
      return n;
    }
  }
  return null;
};
AdfDomUtils.getChildElements = function(parentElement) {
  var childNodes = parentElement.childNodes;
  var filteredNodes = [];
  if (childNodes) {
    var length = childNodes.length;
    for (var i = 0;i < length;i++) {
      var child = childNodes[i];
      if (child.nodeType == 1) {
        filteredNodes.push(child);
      }
    }
  }
  return filteredNodes;
};
AdfDomUtils.getLastChildElement = function(parentElement) {
  var childNodes = parentElement.childNodes;
  if (childNodes) {
    var length = childNodes.length;
    for (var i = length - 1;i >= 0;i--) {
      var child = childNodes[i];
      if (child.nodeType == 1) {
        return child;
      }
    }
  }
  return null;
};
AdfDomUtils.getNonWhitespaceChildCount = function(parentElement) {
  var childCount = 0;
  var childNodes = parentElement.childNodes;
  if (childNodes) {
    var length = childNodes.length;
    for (var i = 0;i < length;i++) {
      var childNode = childNodes[i];
      var nodeType = childNode.nodeType;
      if (nodeType == 8) {
        continue;
      }
      if (nodeType == 3) {
        var stripped = childNode.data.replace(/\s*/, "");
        if (stripped.length == 0) {
          continue;
        }
        ++childCount;
      } else {
        if (nodeType == 1) {
          if (childNode.style == null || childNode.style.display != "none") {
            ++childCount;
          }
        }
      }
    }
  }
  return childCount;
};
AdfDomUtils.getOnlyChildElement = function(parentElement) {
  var childElement = null;
  var childNodes = parentElement.childNodes;
  if (childNodes) {
    var length = childNodes.length;
    for (var i = 0;i < length;i++) {
      childNode = childNodes[i];
      var nodeType = childNode.nodeType;
      if (nodeType == 8) {
        continue;
      }
      if (nodeType == 3) {
        var stripped = childNode.data.replace(/\s*/, "");
        if (stripped.length > 0) {
          return null;
        }
      } else {
        if (nodeType == 1) {
          if (childNode.style == null || childNode.style.display != "none") {
            if (childElement) {
              return null;
            }
            childElement = childNode;
          }
        } else {
          return null;
        }
      }
    }
  }
  return childElement;
};
AdfDomUtils.getInputElementsInNode = function(node, getAll, extraTags) {
  var domDoc = AdfAgent.AGENT.getDomDocument();
  var inputElems = [];
  if (domDoc.evaluate) {
    var expr = ".//input|.//select|.//textarea|.//button|.//a";
    if (extraTags) {
      for (var i = 0;i < extraTags.length;i++) {
        expr = expr + "|.//" + extraTags[i];
      }
    }
    var nodes = domDoc.evaluate(expr, node, null, XPathResult.ANY_TYPE, null);
    var elem = nodes.iterateNext();
    while (elem) {
      if ((getAll || !elem.disabled && elem.tabIndex != -1) && (elem.nodeName != "A" || elem.href)) {
        inputElems.push(elem);
      }
      elem = nodes.iterateNext();
    }
  } else {
    var nodes = node.getElementsByTagName("*");
    var nodeCount = nodes.length;
    var pattern = "^INPUT|SELECT|BUTTON|^A\\b|TEXTAREA";
    if (extraTags) {
      var newTag;
      for (var i = 0;i < extraTags.length;i++) {
        newTag = extraTags[i].toUpperCase();
        pattern = pattern + "|" + newTag;
      }
    }
    var inputRegExp = new RegExp(pattern);
    for (var i = 0;i < nodeCount;i++) {
      var elem = nodes[i];
      if (elem.tagName.match(inputRegExp) && (getAll || !elem.disabled && elem.tabIndex != -1) && (elem.nodeName != "A" || elem.href)) {
        inputElems.push(elem);
      }
    }
  }
  return inputElems;
};
AdfDomUtils.disableTabStop = function(domElement, disable) {
  var extraTags = ["ul"];
  var elems = AdfDomUtils.getInputElementsInNode(domElement, true, extraTags);
  for (var i = 0;i < elems.length;i++) {
    var elem = elems[i];
    if (disable) {
      if (elem._afrTabIndex === undefined && elem.tabIndex !== undefined && elem.tabIndex !== -1) {
        elem._afrTabIndex = elem.tabIndex;
        elem.tabIndex = -1;
      }
      if (elem._afrOverflow === undefined && elem.tagName == "UL" && AdfAgent.AGENT.getPlatform() == AdfAgent.GECKO_PLATFORM) {
        elem._afrOverflow = elem.style.overflow;
        elem.style.overflow = "hidden";
      }
    } else {
      if (elem._afrTabIndex !== undefined) {
        elem.tabIndex = elem._afrTabIndex;
        delete elem._afrTabIndex;
      }
      if (elem._afrOverflow !== undefined) {
        elem.style.overflow = elem._afrOverflow;
        delete elem._afrOverflow;
      }
    }
  }
};
AdfDomUtils.getNextElement = function(currentElement) {
  if (currentElement) {
    var el = currentElement.nextSibling;
    while (el) {
      if (el.nodeType == 1) {
        return el;
      }
      el = el.nextSibling;
    }
  }
};
AdfDomUtils.getPreviousElement = function(currentElement) {
  if (currentElement) {
    var el = currentElement.previousSibling;
    while (el) {
      if (el.nodeType == 1) {
        return el;
      }
      el = el.previousSibling;
    }
  }
};
AdfDomUtils.getFirstChildNodeByType = function(parentElement, type) {
  var childNodes = parentElement.childNodes;
  if (childNodes) {
    var length = childNodes.length;
    for (var i = 0;i < length;i++) {
      var child = childNodes[i];
      if (child.nodeType == type) {
        return child;
      }
    }
  }
  return null;
};
AdfDomUtils.getRelativePosition = function(elem) {
  var currLeft = elem.offsetLeft, currTop = elem.offsetTop;
  var agent = AdfAgent.AGENT;
  elem = elem.offsetParent;
  while (elem) {
    var style = agent.getComputedStyle(elem);
    if (style.position != "static" && !(agent.getPlatform() == AdfAgent.GECKO_PLATFORM && elem.tagName == "TD" && style.position == "relative")) {
      break;
    }
    currLeft += elem.offsetLeft;
    currTop += elem.offsetTop;
    elem = elem.offsetParent;
  }
  return {x:currLeft, y:currTop};
};
AdfDomUtils.getElementPosition = function(currentElement) {
  var pos = -1;
  if (currentElement) {
    var el = currentElement;
    var type = currentElement.nodeType;
    while (el) {
      if (el.nodeType == type) {
        pos++;
      }
      el = el.previousSibling;
    }
  }
  return pos;
};
AdfDomUtils.getChildElementByPosition = function(parentElement, type, position) {
  var el = null;
  if (parentElement) {
    var idx = -1;
    el = parentElement.firstChild;
    while (el && idx < position) {
      if (el.nodeType == type) {
        idx++;
      }
      if (idx != position) {
        el = el.nextSibling;
      }
    }
  }
  return el;
};
AdfDomUtils.getElementDepth = function(element) {
  AdfAssert.assertDomElement(element);
  var depth = -1;
  do {
    depth++;
    element = element.parentNode;
  } while (element);
  return depth;
};
AdfDomUtils.getElementAtPoint = function(element, pageX, pageY, closestMatch, ignoreList) {
  var agent = AdfAgent.AGENT;
  var zeroOffsetHeight = element.offsetHeight == 0;
  var shouldIgnore = ignoreList && ignoreList[element.localName];
  if (!shouldIgnore && (zeroOffsetHeight || agent.pointInElementBounds(element, pageX, pageY))) {
    if (!zeroOffsetHeight) {
      closestMatch = element;
    }
    var currChild = element.firstChild;
    while (currChild) {
      if (currChild.nodeType == 1) {
        var computedStyle = agent.getComputedStyle(currChild);
        if (!computedStyle || computedStyle.visibility != "hidden" && computedStyle.display != "none") {
          var newMatch = AdfDomUtils.getElementAtPoint(currChild, pageX, pageY, closestMatch, ignoreList);
          if (newMatch != closestMatch) {
            return newMatch;
          }
        }
      }
      currChild = currChild.nextSibling;
    }
  }
  return closestMatch;
};
AdfDomUtils.canScroll = function(domNode, viewPortWidth, viewPortHeight) {
  return domNode.scrollWidth != viewPortWidth || domNode.scrollHeight != viewPortHeight;
};
AdfDomUtils.getAutoScrollDeltas = function(target, clientX, clientY) {
  var clientWidth = target.clientWidth;
  var clientHeight = target.clientHeight;
  var deltaX = 0;
  var deltaY = 0;
  var scrollTop = target.scrollTop;
  var scrollLeft = target.scrollLeft;
  var viewPortX = clientX - scrollLeft;
  var viewPortY = clientY - scrollTop;
  if (scrollLeft && viewPortX < 5) {
    deltaX = -Math.min(scrollLeft, 10);
  } else {
    if (clientWidth - viewPortX < 5) {
      var maxScrollRight = target.scrollWidth - clientWidth;
      deltaX = Math.min(maxScrollRight, 10);
    }
  }
  if (scrollTop && viewPortY < 5) {
    deltaY = -Math.min(scrollTop, 10);
  } else {
    if (clientHeight - viewPortY < 5) {
      var maxScrollBottom = target.scrollHeight - clientHeight;
      deltaY = Math.min(maxScrollBottom, 10);
    }
  }
  if (deltaX || deltaY) {
    return {x:deltaX, y:deltaY};
  } else {
    return null;
  }
};
AdfDomUtils.removeAllNodes = function(element) {
  var oldNodes = element.childNodes;
  for (var i = oldNodes.length - 1;i >= 0;i--) {
    element.removeChild(oldNodes[i]);
  }
};
AdfDomUtils.scrollRightBy = function(domElement, deltaX) {
  AdfDomUtils.setScrollLeft(domElement, domElement.scrollLeft + deltaX);
};
AdfDomUtils.setScrollLeft = function(domElement, newScrollLeft) {
  newScrollLeft = Math.max(newScrollLeft, 0);
  var maxScrollLeft = domElement.scrollWidth - domElement.clientWidth;
  domElement.scrollLeft = Math.min(newScrollLeft, maxScrollLeft);
};
AdfDomUtils.scrollDownBy = function(domElement, deltaY) {
  AdfDomUtils.setScrollTop(domElement, domElement.scrollTop + deltaY);
};
AdfDomUtils.setScrollTop = function(domElement, newScrollTop) {
  newScrollTop = Math.max(newScrollTop, 0);
  var maxScrollTop = domElement.scrollHeight - domElement.clientHeight;
  domElement.scrollTop = Math.min(newScrollTop, maxScrollTop);
};
AdfDomUtils.scrollChildIntoViewY = function(childElement) {
  var offsetTop = childElement.offsetTop;
  var offsetHeight = childElement.offsetHeight;
  var offsetParent = childElement.offsetParent;
  var scrollParent = childElement.parentNode;
  while (scrollParent && scrollParent.scrollHeight <= scrollParent.offsetHeight) {
    if (scrollParent.offsetParent != offsetParent) {
      offsetTop += scrollParent.offsetTop;
      offsetParent = scrollParent.offsetParent;
    }
    scrollParent = scrollParent.parentNode;
  }
  if (scrollParent != null) {
    if (offsetTop < scrollParent.scrollTop) {
      scrollParent.scrollTop = offsetTop;
    } else {
      if (offsetTop + offsetHeight > scrollParent.scrollTop + scrollParent.clientHeight) {
        scrollParent.scrollTop = offsetTop + offsetHeight - scrollParent.clientHeight;
      }
    }
  }
};
AdfDomUtils.getNodeInfo = function(domNode) {
  var nodeInfo = "";
  if (domNode) {
    nodeInfo += domNode.nodeName;
    if (domNode.id) {
      nodeInfo += " id\x3d" + domNode.id;
    }
    if (domNode._afrpeerid) {
      nodeInfo += " peer id\x3d" + domNode._afrpeerid;
    }
    if (domNode.className) {
      nodeInfo += " class\x3d" + domNode.className;
    }
  }
  return nodeInfo;
};
AdfDomUtils.suppressActionLinkBrowserContextMenu = function(componentContextMenuEvent) {
  var agent = AdfAgent.AGENT;
  var nativeEvent = componentContextMenuEvent.getNativeEvent();
  var eventTarget = AdfDomUtils.getElement(agent.getEventTarget(nativeEvent));
  var href = eventTarget.getAttribute("href", 2);
  if (href == null || href == "") {
    var root = componentContextMenuEvent.getSource().getPeer().getDomElement().parentNode;
    var linkNode = AdfDomUtils.getParentWithAttr(root, eventTarget, "href");
    if (linkNode != null) {
      href = linkNode.getAttribute("href", 2);
    }
  }
  if (href != null) {
    if (href == "#" || href.indexOf("javascript:") == 0) {
      agent.preventDefault(nativeEvent);
    }
  }
};
AdfDomUtils.swapIcon = function(originalIcon, iconKey) {
  var laf = AdfPage.PAGE.getLookAndFeel();
  var rawIcon = laf.getRawIcon(iconKey);
  var agent = AdfAgent.AGENT;
  if (rawIcon != null && !agent.isEqualIcon(originalIcon, rawIcon)) {
    if (agent.getPlatform() == AdfAgent.IE_PLATFORM && originalIcon.tagName == rawIcon.tagName && rawIcon.tagName == "IMG") {
      originalIcon.src = rawIcon.src;
      originalIcon.alt = rawIcon.alt;
      originalIcon.title = originalIcon.title;
      originalIcon.border = rawIcon.border;
    } else {
      var clone = rawIcon.cloneNode(true);
      agent.replaceNode(clone, originalIcon);
    }
  }
};
AdfDomUtils._getCSSClassNameIndex = function(currentClassName, className) {
  if (!currentClassName || typeof currentClassName !== "string") {
    return -1;
  } else {
    if (className === currentClassName) {
      return 0;
    } else {
      var classNameLength = className.length;
      var currentClassNameLength = currentClassName.length;
      if (!currentClassName.indexOf) {
        return -1;
      }
      var nameIndex = currentClassName.indexOf(className);
      if (nameIndex >= 0) {
        var hasStart = nameIndex == 0 || currentClassName.charAt(nameIndex - 1) == " ";
        var endIndex = nameIndex + classNameLength;
        var hasEnd = endIndex == currentClassNameLength || currentClassName.charAt(endIndex) == " ";
        if (hasStart && hasEnd) {
          return nameIndex;
        } else {
          var lastNameIndex = currentClassName.lastIndexOf(className);
          if (lastNameIndex != nameIndex) {
            hasStart = currentClassName.charAt(lastNameIndex - 1);
            endIndex = lastNameIndex + classNameLength;
            hasEnd = endIndex == currentClassNameLength || currentClassName.charAt(endIndex) == " ";
            if (hasStart && hasEnd) {
              return lastNameIndex;
            } else {
              return currentClassName.indexOf(" " + className + " ");
            }
          }
        }
      }
      return -1;
    }
  }
};
AdfDomUtils.addCSSClassName = function(domElement, className) {
  AdfAssert.assertDomElement(domElement);
  var added = false;
  if (className != null) {
    AdfAssert.assertString(className);
    var currentClassName = domElement.className;
    var classNameIndex = AdfDomUtils._getCSSClassNameIndex(currentClassName, className);
    if (classNameIndex == -1) {
      var newClassName = currentClassName ? className + " " + currentClassName : className;
      domElement.className = newClassName;
      added = true;
    }
  }
  return added;
};
AdfDomUtils.removeCSSClassName = function(domElement, className) {
  AdfAssert.assertDomElement(domElement);
  var removed = false;
  if (className != null) {
    var currentClassName = domElement.className;
    var classNameIndex = AdfDomUtils._getCSSClassNameIndex(currentClassName, className);
    if (classNameIndex != -1) {
      var classNameEndIndex = classNameIndex + className.length;
      var beforeString = classNameIndex == 0 ? null : currentClassName.substring(0, classNameIndex);
      var afterString = classNameEndIndex == currentClassName.length ? null : currentClassName.substring(classNameEndIndex + 1);
      var newClassName;
      if (beforeString == null) {
        if (afterString == null) {
          newClassName = "";
        } else {
          newClassName = afterString;
        }
      } else {
        if (afterString == null) {
          newClassName = beforeString;
        } else {
          newClassName = beforeString + afterString;
        }
      }
      domElement.className = newClassName;
      removed = true;
    }
  }
  return removed;
};
AdfDomUtils.addOrRemoveCSSClassName = function(add, domElement, className) {
  var func = add ? AdfDomUtils.addCSSClassName : AdfDomUtils.removeCSSClassName;
  return func(domElement, className);
};
AdfDomUtils.containsCSSClassName = function(domElement, className) {
  if (className != null) {
    return AdfDomUtils._getCSSClassNameIndex(domElement.className, className) != -1;
  } else {
    return false;
  }
};
AdfDomUtils.addInlineStyle = function(element, newStyle, takesPrecedence) {
  if (newStyle == null) {
    return;
  }
  var existingStyle = element.style.cssText;
  if (existingStyle == null || existingStyle == "") {
    element.style.cssText = newStyle;
  } else {
    if (takesPrecedence == true) {
      element.style.cssText = existingStyle + ";" + newStyle;
    } else {
      element.style.cssText = newStyle + ";" + existingStyle;
    }
  }
};
AdfDomUtils.fillEmptyCell = function(cell) {
  AdfAssert.assertDomElement(cell, "TD");
  if (cell.firstChild == null) {
    var div = cell.ownerDocument.createElement("div");
    cell.appendChild(div);
  }
};
AdfDomUtils.getElementsByClassName = function(domNode, className) {
  if (domNode == null) {
    domNode = AdfAgent.AGENT.getDomDocument().documentElement;
  }
  var allElements = domNode.getElementsByTagName("*");
  var length = allElements.length;
  var classElements = [];
  var element;
  for (var i = 0;i < length;i++) {
    element = allElements[i];
    if (AdfDomUtils.containsCSSClassName(element, className)) {
      classElements.push(element);
    }
  }
  return classElements;
};
AdfDomUtils.setAccessibleState = function(component, state) {
  var agent = AdfAgent.AGENT;
  var subId = AdfRichUIPeer.CreateSubId(component.getClientId(), "state");
  var stateElement = agent.getElementById(subId);
  AdfAssert.assertDomNode(stateElement);
  if (state != null) {
    agent.setTextContent(stateElement, " " + state + " ");
  } else {
    agent.setTextContent(stateElement, "");
  }
};
AdfDomUtils.getFirstElementMatch = function(element, isExclusive, matchFunc, context) {
  AdfAssert.assertFunction(matchFunc);
  AdfAssert.assertDomElement(element);
  if (!isExclusive && matchFunc(element, context)) {
    return element;
  }
  if (element.getElementsByTagName) {
    var elements = element.getElementsByTagName("*");
    var length = elements.length;
    for (var i = 0;i < length;i++) {
      var currElement = elements[i];
      if (matchFunc(currElement, context)) {
        return currElement;
      }
    }
  }
  return null;
};
AdfDomUtils.getNextElementMatch = function(element, contextElement, isExclusive, matchFunc, context) {
  AdfAssert.assertFunction(matchFunc);
  AdfAssert.assertDomElement(element);
  AdfAssert.assertDomElementOrNull(contextElement);
  var matchElement = null;
  if (!isExclusive) {
    matchElement = AdfDomUtils.getFirstElementMatch(element, true, matchFunc, context);
    if (matchElement != null) {
      return matchElement;
    }
  }
  while (element && element != contextElement) {
    if (element.nextSibling) {
      element = element.nextSibling;
      if (element.nodeType == 1) {
        matchElement = AdfDomUtils.getFirstElementMatch(element, false, matchFunc, context);
        if (matchElement != null) {
          return matchElement;
        }
      }
    } else {
      element = element.parentNode;
    }
  }
  return null;
};
AdfDomUtils.getLastElementMatch = function(element, isExclusive, matchFunc, context) {
  AdfAssert.assertFunction(matchFunc);
  AdfAssert.assertDomElement(element);
  if (element.getElementsByTagName) {
    var elements = element.getElementsByTagName("*");
    for (var i = elements.length - 1;i >= 0;i--) {
      var currElement = elements[i];
      if (matchFunc(currElement, context)) {
        return currElement;
      }
    }
  }
  if (!isExclusive && matchFunc(element, context)) {
    return element;
  }
  return null;
};
AdfDomUtils.getPreviousElementMatch = function(element, contextElement, matchFunc, context) {
  AdfAssert.assertFunction(matchFunc);
  AdfAssert.assertDomElement(element);
  AdfAssert.assertDomElementOrNull(contextElement);
  while (element && element != contextElement) {
    var previousElement = element.previousSibling;
    while (previousElement && previousElement.nodeType != 1) {
      previousElement = previousElement.previousSibling;
    }
    if (previousElement) {
      matchElement = AdfDomUtils.getLastElementMatch(previousElement, false, matchFunc, context);
      if (matchElement != null) {
        return matchElement;
      }
      element = previousElement;
    } else {
      var parentElement = element.parentNode;
      if (parentElement != contextElement && matchFunc(parentElement, context)) {
        return parentElement;
      }
      element = parentElement;
    }
  }
  return null;
};
AdfDomUtils.getFirstFilteredElement = function(element, isExclusive, filterFunc, context) {
  AdfAssert.assertFunction(filterFunc);
  AdfAssert.assertDomElement(element);
  var filterResult = filterFunc(element, context);
  if (!isExclusive && filterResult == AdfDomUtils.FILTER_ACCEPT) {
    return element;
  } else {
    if (filterResult == AdfDomUtils.FILTER_REJECT) {
      return null;
    }
  }
  return AdfDomUtils.depthFirstFilter(element, element, filterFunc, context);
};
AdfDomUtils.depthFirstFilter = function(element, rootElement, filterFunc, context) {
  if (element == null) {
    return null;
  }
  var filterResult = filterFunc(element, context);
  if (element == rootElement) {
    filterResult = AdfDomUtils.FILTER_SKIP;
  }
  if (filterResult == AdfDomUtils.FILTER_ACCEPT) {
    return element;
  }
  var retElem = null;
  if (filterResult != AdfDomUtils.FILTER_REJECT) {
    var nextSearchElement = element.firstChild;
    while (nextSearchElement && !retElem) {
      retElem = AdfDomUtils.depthFirstFilter(nextSearchElement, rootElement, filterFunc, context);
      nextSearchElement = nextSearchElement.nextSibling;
    }
  }
  return retElem;
};
AdfDomUtils.getNextFilteredElement = function(element, contextElement, isExclusive, filterFunc, context) {
  AdfAssert.assertFunction(filterFunc);
  AdfAssert.assertDomElement(element);
  AdfAssert.assertDomElementOrNull(contextElement);
  var filteredElement = null;
  if (!isExclusive) {
    filteredElement = AdfDomUtils.getFirstFilteredElement(element, false, filterFunc, context);
    if (filteredElement != null) {
      return filteredElement;
    }
  }
  while (element && element != contextElement) {
    if (element.nextSibling) {
      element = element.nextSibling;
      if (element.nodeType == 1) {
        filteredElement = AdfDomUtils.getFirstFilteredElement(element, false, filterFunc, context);
        if (filteredElement != null) {
          return filteredElement;
        }
      }
    } else {
      var parentElement = element.parentNode;
      if (parentElement && parentElement != contextElement && parentElement.nodeType == 1 && filterFunc(parentElement, context) == AdfDomUtils.FILTER_ACCEPT) {
        return parentElement;
      }
      element = parentElement;
    }
  }
  return null;
};
AdfDomUtils.reverseDepthFirstFilter = function(element, rootElement, filterFunc, context) {
  if (element == null) {
    return null;
  }
  var filterResult = filterFunc(element, context);
  var retElem = null;
  if (element == rootElement) {
    filterResult = AdfDomUtils.FILTER_SKIP;
  }
  if (filterResult != AdfDomUtils.FILTER_REJECT) {
    var nextSearchElement = element.lastChild;
    while (nextSearchElement && !retElem) {
      retElem = AdfDomUtils.reverseDepthFirstFilter(nextSearchElement, rootElement, filterFunc, context);
      nextSearchElement = nextSearchElement.previousSibling;
    }
  }
  if (!retElem && filterResult == AdfDomUtils.FILTER_ACCEPT) {
    retElem = element;
  }
  return retElem;
};
AdfDomUtils.getLastFilteredElement = function(element, isExclusive, filterFunc, context) {
  AdfAssert.assertFunction(filterFunc);
  AdfAssert.assertDomElement(element);
  var filteredElement = AdfDomUtils.reverseDepthFirstFilter(element, element, filterFunc, context);
  if (filteredElement != null) {
    return filteredElement;
  } else {
    if (!isExclusive && filterFunc(element, context) == AdfDomUtils.FILTER_ACCEPT) {
      return element;
    }
  }
  return null;
};
AdfDomUtils.getPreviousFilteredElement = function(element, contextElement, filterFunc, context) {
  AdfAssert.assertFunction(filterFunc);
  AdfAssert.assertDomElement(element);
  AdfAssert.assertDomElementOrNull(contextElement);
  var acceptedElement = null;
  while (element && element != contextElement) {
    if (element.previousSibling) {
      element = element.previousSibling;
      if (element.nodeType == 1) {
        acceptedElement = AdfDomUtils.getLastFilteredElement(element, false, filterFunc, context);
        if (acceptedElement != null) {
          return acceptedElement;
        }
      }
    } else {
      var parentElement = element.parentNode;
      if (parentElement && parentElement != contextElement && parentElement.nodeType == 1 && filterFunc(parentElement, context) == AdfDomUtils.FILTER_ACCEPT) {
        return parentElement;
      }
      element = parentElement;
    }
  }
  return null;
};
AdfDomUtils.getParentWithAttr = function(rootElement, startElement, attr) {
  var target = startElement;
  while (target && target != rootElement) {
    if (target.nodeType == 1 && AdfAgent.AGENT.getAttribute(target, attr)) {
      return target;
    } else {
      target = target.parentNode;
    }
  }
  return null;
};
AdfDomUtils.getFirstChildElementByAttributeValue = AdfDomUtils.getChildElementByAttributeValue = function(domElement, attrName, attrValue, tagName) {
  AdfAssert.assertNonEmptyString(attrName);
  AdfAssert.assertString(attrValue);
  AdfAssert.assertDomElement(domElement);
  if (!tagName) {
    tagName = "*";
  }
  var childElements = domElement.getElementsByTagName(tagName);
  var length = childElements.length;
  for (var i = 0;i < length;i++) {
    element = childElements[i];
    if (element.getAttribute(attrName) == attrValue) {
      return element;
    }
  }
  return null;
};
AdfDomUtils.getLastChildElementByAttributeValue = function(domElement, attrName, attrValue, tagName) {
  AdfAssert.assertNonEmptyString(attrName);
  AdfAssert.assertString(attrValue);
  AdfAssert.assertDomElement(domElement);
  if (!tagName) {
    tagName = "*";
  }
  var childElements = domElement.getElementsByTagName(tagName);
  var length = childElements.length;
  for (var i = length - 1;i >= 0;i--) {
    element = childElements[i];
    if (element.getAttribute(attrName) == attrValue) {
      return element;
    }
  }
  return null;
};
AdfDomUtils.getChildElementsByAttributeValue = function(domElement, attrName, attrValue, tagName) {
  AdfAssert.assertNonEmptyString(attrName);
  AdfAssert.assertNonEmptyString(attrValue);
  AdfAssert.assertDomElement(domElement);
  if (!tagName) {
    tagName = "*";
  }
  var allChildElements = domElement.getElementsByTagName(tagName);
  var length = allChildElements.length;
  var childElements = [];
  for (var i = 0;i < length;i++) {
    var element = allChildElements[i];
    if (element.getAttribute(attrName) == attrValue) {
      childElements.push(element);
    }
  }
  if (childElements.length > 0) {
    return childElements;
  }
  return null;
};
AdfDomUtils.getChildElementsByMatch = function(domElement, matchFunction, tagName, context) {
  AdfAssert.assertDomElement(domElement);
  AdfAssert.assertFunction(matchFunction);
  if (!tagName) {
    tagName = "*";
  }
  var allChildElements = domElement.getElementsByTagName(tagName);
  var length = allChildElements.length;
  var matchedChildElements = [];
  for (var i = 0;i < length;i++) {
    var element = allChildElements[i];
    if (matchFunction(element, context)) {
      matchedChildElements.push(element);
    }
  }
  return matchedChildElements.length > 0 ? matchedChildElements : null;
};
AdfDomUtils.getChildElementsByMatch = function(domElement, matchFunction, tagName, context) {
  AdfAssert.assertDomElement(domElement);
  AdfAssert.assertFunction(matchFunction);
  if (!tagName) {
    tagName = "*";
  }
  var allChildElements = domElement.getElementsByTagName(tagName);
  var length = allChildElements.length;
  var matchedChildElements = [];
  for (var i = 0;i < length;i++) {
    var element = allChildElements[i];
    if (matchFunction(element, context)) {
      matchedChildElements.push(element);
    }
  }
  return matchedChildElements.length > 0 ? matchedChildElements : null;
};
AdfDomUtils.getChildElementByValueMap = function(domElement, keyValueArray, tagName) {
  AdfAssert.assertDomElement(domElement);
  AdfAssert.assertArray(keyValueArray);
  AdfAssert.assert(keyValueArray.length == 2);
  if (!tagName) {
    tagName = "*";
  }
  var attrKey = keyValueArray[0];
  var attrValue = keyValueArray[1];
  return AdfDomUtils.getChildElementByAttributeValue(domElement, attrKey, attrValue, tagName);
};
AdfDomUtils.getTabIndex = function(element) {
  AdfAssert.assertDomElement(element);
  var agent = AdfAgent.AGENT;
  var tabIndexAttr = element.attributes["tabIndex"];
  return tabIndexAttr == null || !agent.hasAttribute(element, tabIndexAttr) ? undefined : tabIndexAttr.value;
};
AdfDomUtils.cleanHtml = function(domDocument, value) {
  var offSpan = domDocument.createElement("span");
  offSpan.innerHTML = value;
  if (value && value.indexOf("\x3c") >= 0) {
    AdfDomUtils._cleanElementHtml(offSpan);
  }
  return offSpan;
};
AdfDomUtils._cleanElementHtml = function(node) {
  var agent = AdfAgent.AGENT;
  var children = node.childNodes;
  var count = children.length - 1;
  while (count >= 0) {
    var child = children.item(count);
    if (child.nodeType == 1) {
      if (AdfDomUtils._LEGAL_ELEMENTS[child.nodeName]) {
        var attrs = child.attributes;
        for (var i = attrs.length - 1;i >= 0;i--) {
          var attr = attrs[i];
          if (agent.hasAttribute(child, attr)) {
            if (!AdfDomUtils._LEGAL_ATTRIBUTES[attr.name]) {
              child.removeAttribute(attr.nodeName);
            }
          }
        }
        AdfDomUtils._cleanElementHtml(child);
      } else {
        node.removeChild(child);
      }
    }
    count--;
  }
};
AdfDomUtils.findAccessKeyDom = function(domElement, accessKeyValue) {
  if (accessKeyValue == null) {
    return null;
  }
  var children = domElement.childNodes;
  for (var i = 0;i < children.length;++i) {
    if (AdfAgent.AGENT.getTextContent(children[i]) == accessKeyValue) {
      return children[i];
    }
  }
  return null;
};
AdfDomUtils.handleTextChangeWithAccessKey = function(component, domElement, newValue) {
  if (domElement == null) {
    return false;
  }
  var accessKeyValue = component.getPropertyValue("accessKey");
  var accessKeySpan = AdfDomUtils.findAccessKeyDom(domElement, accessKeyValue);
  if (accessKeyValue != null) {
    var doc = component.getPeer().getDomDocument();
    if (accessKeySpan != null) {
      var index = newValue.indexOf(accessKeyValue);
      if (index == -1) {
        AdfDomUtils._removeAccessKeyElement(domElement, accessKeySpan);
        AdfDomUtils._setTextContent(domElement, newValue);
      } else {
        AdfDomUtils._removeSiblings(accessKeySpan);
        if (index > 0) {
          domElement.insertBefore(doc.createTextNode(newValue.substring(0, index)), accessKeySpan);
        }
        AdfDomUtils._setTextContent(accessKeySpan, accessKeyValue);
        if (index + 1 < newValue.length) {
          domElement.appendChild(doc.createTextNode(newValue.substr(index + 1)));
        }
      }
      return true;
    } else {
      return AdfDomUtils._setTextAndAccessKeySpan(component, domElement, accessKeyValue, accessKeySpan, newValue);
    }
  } else {
    if (accessKeySpan != null) {
      AdfDomUtils._removeAccessKeyElement(accessKeySpan);
    }
    AdfDomUtils._setTextContent(domElement, newValue);
    return true;
  }
  return false;
};
AdfDomUtils.launchHelpWindow = function(url) {
  var helpWindowName = AdfDomUtils._getHelpWindowName();
  if (helpWindowName == null) {
    helpWindowName = "RCFHelpWindow";
    AdfDomUtils._setHelpWindowName(helpWindowName);
  }
  var helpWindow = window.open(url, helpWindowName, "width\x3d650,height\x3d450,resizable\x3dyes,scrollbars\x3dyes,alwaysRaised");
  helpWindow.focus();
};
AdfDomUtils.writeAttributesToElement = function(domElement, attributesMap) {
  var agent = AdfAgent.AGENT;
  for (var attribute in attributesMap) {
    var value = attributesMap[attribute];
    if (value != undefined) {
      agent.setAttribute(domElement, attribute, value);
    }
  }
};
AdfDomUtils.copyAllAttributes = function(sourceElement, targetElement) {
  for (var i = 0;i < sourceElement.attributes.length;i++) {
    var attr = sourceElement.attributes[i];
    targetElement.setAttribute(attr.nodeName, attr.value);
  }
};
AdfDomUtils.getPopupAncestor = function(component) {
  var parent = null;
  try {
    parent = component.getParent();
  } catch (err) {
  }
  if (parent == null) {
    return null;
  }
  while (parent != null) {
    if (parent instanceof AdfRichPopup) {
      return parent;
    } else {
      try {
        parent = parent.getParent();
      } catch (err) {
        return false;
      }
    }
    if (parent == null) {
      return false;
    }
  }
};
AdfDomUtils.isInPopup = function(component) {
  var popupAncestor = AdfDomUtils.getPopupAncestor(component);
  if (popupAncestor == null) {
    return null;
  } else {
    if (popupAncestor == false) {
      return false;
    } else {
      return true;
    }
  }
};
AdfDomUtils._getHelpWindowName = function() {
  var nameEQ = AdfDomUtils._HELP_WINDOW_NAME_COOKIE + "\x3d";
  var ca = document.cookie.split(";");
  var caLen = ca.length;
  for (var i = 0;i < caLen;i++) {
    var c = ca[i];
    var strIdx = 0;
    while (c.charAt(strIdx) == " ") {
      strIdx++;
    }
    c = c.substring(strIdx, c.length);
    if (c.indexOf(nameEQ) == 0) {
      return c.substring(nameEQ.length, c.length);
    }
  }
  return null;
};
AdfDomUtils._setHelpWindowName = function(windowName) {
  document.cookie = AdfDomUtils._HELP_WINDOW_NAME_COOKIE + "\x3d" + windowName + "; ; path\x3d/";
};
AdfDomUtils._setTextAndAccessKeySpan = function(component, domElement, accessKeyValue, accessKeySpan, newValue) {
  var style = AdfAgent.AGENT.getExpandoProperty(domElement, "_adfAccessKeyStyleClass", null);
  if (style == null) {
    AdfDomUtils._setTextContent(domElement, newValue);
  } else {
    var index = newValue.indexOf(accessKeyValue);
    if (index >= 0) {
      if (index > 0) {
        AdfAgent.AGENT.setTextContent(domElement, newValue.substring(0, index));
      } else {
        while (domElement.lastChild != null) {
          domElement.removeChild(domElement.lastChild);
        }
      }
      var doc = component.getPeer().getDomDocument();
      accessKeySpan = doc.createElement("SPAN");
      accessKeySpan.className = style;
      AdfAgent.AGENT.setTextContent(accessKeySpan, accessKeyValue);
      domElement.appendChild(accessKeySpan);
      if (index + 1 < newValue.length) {
        domElement.appendChild(doc.createTextNode(newValue.substr(index + 1)));
      }
    } else {
      AdfAgent.AGENT.setTextContent(domElement, newValue);
    }
  }
  return true;
};
AdfDomUtils._removeSiblings = function(domElement) {
  var node;
  var parent = domElement.parentNode;
  while ((node = domElement.previousSibling) != null) {
    parent.removeChild(node);
  }
  while ((node = domElement.nextSibling) != null) {
    parent.removeChild(node);
  }
};
AdfDomUtils._setTextContent = function(domElement, value) {
  if (domElement.tagName == "BUTTON") {
    AdfAgent.AGENT.setButtonTextContent(domElement, value);
  } else {
    AdfAgent.AGENT.setTextContent(domElement, value);
  }
};
AdfDomUtils._removeAccessKeyElement = function(domElement, accessKeyElement) {
  AdfAgent.AGENT.setExpandoProperty(domElement, "_adfAccessKeyStyleClass", accessKeyElement.className);
  accessKeyElement.parentNode.removeChild(accessKeyElement);
};
AdfDomUtils.__componentTextChanged = function(component, domElement, newValue, oldValue) {
  if (oldValue != null) {
    var spanElement = AdfDomUtils.getFirstDescendentElement(domElement, "SPAN");
    var accessKeyValue = component.getPropertyValue("accessKey");
    var accessKeySpan = accessKeyValue == null ? null : AdfDomUtils.findAccessKeyDom(domElement, accessKeyValue);
    var textElement = spanElement && spanElement != accessKeySpan ? spanElement : domElement;
    return AdfDomUtils.handleTextChangeWithAccessKey(component, textElement, newValue);
  }
  return false;
};
AdfDomUtils.componentShowRequiredChanged = function(component, domElement, newValue, oldValue) {
  var reqIconSpan = AdfDomUtils.getFirstElementMatch(domElement, true, AdfDomUtils._matchRequiredIconSpan);
  if (!reqIconSpan) {
    return false;
  }
  if (newValue) {
    var rawIcon = AdfPage.PAGE.getLookAndFeel().getIcon("AFRequiredIcon");
    reqIconSpan.appendChild(rawIcon);
  } else {
    AdfDomUtils.removeAllNodes(reqIconSpan);
  }
  return true;
};
AdfDomUtils._matchRequiredIconSpan = function(element) {
  var tagName = element.tagName;
  if (tagName == "SPAN" && AdfDomUtils.containsCSSClassName(element, AdfDomUtils._REQUIRED_ICON_PLACEHOLDER_STYLE_CLASS)) {
    return true;
  }
  return false;
};
AdfDomUtils._REQUIRED_ICON_PLACEHOLDER_STYLE_CLASS = "p_rqi";
AdfDomUtils._LEGAL_ELEMENTS = {"SPAN":1, "B":1, "A":1, "I":1, "EM":1, "BR":1, "HR":1, "LI":1, "OL":1, "UL":1, "P":1, "TT":1, "BIG":1, "SMALL":1, "PRE":1};
AdfDomUtils._LEGAL_ATTRIBUTES = {"class":1, "style":1, "href":1};
AdfDomUtils._HELP_WINDOW_NAME_COOKIE = "ORA_ADF_HELPWINDOWNAME";
AdfDomUtils.FILTER_ACCEPT = 1;
AdfDomUtils.FILTER_REJECT = 0;
AdfDomUtils.FILTER_SKIP = -1;
