AdfRichUIPeer.createPeerClass(AdfDhtmlEditableValuePeer, "AdfDhtmlSelectManyPeer");
AdfDhtmlSelectManyPeer.InitSubclass = function() {
  if (!AdfAgent.AGENT.isTouchFirstDevice()) {
    AdfRichUIPeer.addComponentEventHandlers(this, AdfUIInputEvent.MOUSE_IN_EVENT_TYPE);
  }
  this._SELECT_ALL_ID = "saId";
  this._ITEM_VALUE_EXPANDO = "_adfIV";
};
AdfDhtmlSelectManyPeer.prototype.HandleComponentMouseOver = function(componentEvent) {
  this.ShowNoteWindowMouseOver(componentEvent);
};
AdfDhtmlSelectManyPeer.prototype.GetComponentLabel = function(component, domElement) {
  var labelElement = AdfDomUtils.getChildElementByAttributeValue(domElement, "for", AdfRichUIPeer.createSubId(component.getClientId(), AdfDhtmlEditableValuePeer._CONTENT_ID), "LABEL");
  if (labelElement == null) {
    var labelElements = domElement.getElementsByTagName("LABEL");
    labelElement = labelElements.length ? labelElements[0] : null;
  }
  if (labelElement != null) {
    var contentElement = AdfDhtmlEditableValuePeer.getContentNode(component, domElement);
    var parentElement = labelElement.parentNode;
    while (parentElement && parentElement != domElement) {
      if (parentElement == contentElement) {
        return null;
      }
      parentElement = parentElement.parentNode;
    }
    return AdfAgent.AGENT.getTextContent(labelElement);
  } else {
    return null;
  }
};
AdfDhtmlSelectManyPeer.prototype.getConvertedValue = function(component, submittedValue) {
  var newValue = submittedValue;
  if (component.getConverter != undefined) {
    var converter = component.getConverter();
    if (converter != null) {
      newValue = new Array;
      if (submittedValue != null) {
        for (var i = 0;i < submittedValue.length;i++) {
          newValue[i] = converter.getAsObject(submittedValue[i], component.getProperty("label"));
          if (newValue[i] === undefined) {
            return undefined;
          }
        }
      }
    }
  }
  return newValue;
};
AdfDhtmlSelectManyPeer.prototype.getConvertedObject = function(component, value) {
  if (value != null) {
    var valueAsString = value;
    if (component == null) {
      component = this.getComponent();
    }
    var converter = component.getConverter();
    if (converter != null) {
      valueAsString = new Array;
      for (var i = 0;i < value.length;i++) {
        valueAsString[i] = converter.getAsString(value[i], component.getProperty("label"));
        if (valueAsString[i] === undefined) {
          return undefined;
        }
      }
    }
    return valueAsString;
  }
  return null;
};
AdfDhtmlSelectManyPeer.prototype.IsCheckbox = function(element) {
  if (element != null) {
    return AdfAgent.AGENT.getNodeName(element) == "INPUT" && element.type == "checkbox";
  } else {
    return false;
  }
};
AdfDhtmlSelectManyPeer.prototype.GetCheckboxByEventTarget = function(element, componentEvent) {
  var agent = AdfAgent.AGENT;
  if (element && agent.getNodeName(element) != "INPUT") {
    var checkboxes = element.getElementsByTagName("input");
    if (checkboxes.length > 1) {
      return null;
    }
    var checkbox = checkboxes[0];
    if (checkbox && this.IsCheckbox(checkbox)) {
      if (!checkbox.disabled) {
        var checkboxStyle = agent.getComputedStyle(checkbox);
        var agentIsIE = agent.getPlatform() == AdfAgent.IE_PLATFORM;
        var agentIsGecko = agent.getPlatform() == AdfAgent.GECKO_PLATFORM;
        var nativeEvent = componentEvent.getNativeEvent();
        if (agent.getNodeName(element) != "LABEL" || agentIsIE && checkboxStyle.display == "none" || agentIsGecko && nativeEvent.ctrlKey) {
          if (checkboxStyle.display != "none") {
            checkbox.focus();
          }
          checkbox.checked = !checkbox.checked;
        }
      }
      return checkbox;
    }
  }
  return element;
};
AdfDhtmlSelectManyPeer.prototype.GetNullDefault = function() {
  return AdfCollections.EMPTY_ARRAY;
};
AdfDhtmlSelectManyPeer.prototype.GetSubmittedValue = function(component, domElement) {
  AdfAssert.assertDomElement(domElement);
  var readOnly = component.getReadOnly();
  if (readOnly) {
    var domNode = AdfDhtmlEditableValuePeer.getContentNode(component, domElement);
    var itemValues = domNode.getAttribute(AdfDhtmlSelectManyPeer._ITEM_VALUE_EXPANDO);
    if (itemValues) {
      var formattedItemValues = itemValues.replace(/'/g, '"');
      return JSON.parse(formattedItemValues);
    }
    return itemValues;
  } else {
    var boxes = this.GetCheckboxes(component, domElement);
    var values = new Array;
    if (boxes) {
      for (var i = 0;i < boxes.length;i++) {
        var cb = boxes[i];
        if (cb.checked) {
          values.push(cb.value);
        }
      }
    }
    return values;
  }
};
AdfDhtmlSelectManyPeer.prototype.GetCheckboxes = function(component, domElement, containerName) {
  AdfAssert.assertDomElement(domElement);
  var form = AdfDomUtils.getFormElement(domElement);
  var cbName = component.getClientId();
  var boxes = form.elements[cbName];
  if (boxes) {
    if (boxes.length) {
      return boxes;
    }
    var list = new Array;
    list.push(boxes);
    return list;
  }
  return null;
};
AdfDhtmlSelectManyPeer.prototype.GetComponentSelectItems = function(component, domElement) {
  AdfAssert.assertDomElement(domElement);
  var selectItems = new Array;
  var boxes = this.GetCheckboxes(component, domElement);
  if (boxes) {
    for (i = 0;i < boxes.length;i++) {
      selectItems[i] = new AdfSelectItem;
      selectItems[i].setValue(boxes[i].value);
      var label = AdfDhtmlEditableValuePeer.GetLabelElement(boxes[i]).textContent;
      if (label) {
        selectItems[i].setLabel(label);
      }
      selectItems[i].setDisabled(boxes[i].disabled);
    }
  }
  return selectItems;
};
AdfDhtmlSelectManyPeer.prototype.GetSelectAllCheckbox = function(component) {
  var selectAllID = AdfRichUIPeer.CreateSubId(component.getClientId(), AdfDhtmlSelectManyPeer._SELECT_ALL_ID);
  return AdfAgent.AGENT.getElementById(selectAllID);
};
AdfDhtmlSelectManyPeer.prototype.GetPreviousItem = function(component, domElement, item, ignoreAllCheckbox, containerName) {
  AdfAssert.assertDomElement(domElement);
  var boxes = this.GetCheckboxes(component, domElement, containerName);
  if (boxes) {
    var prevItem = null;
    if (!ignoreAllCheckbox) {
      prevItem = this.GetSelectAllCheckbox(component);
    }
    if (ignoreAllCheckbox || item != prevItem) {
      for (var i = 0;i < boxes.length;i++) {
        var cb = boxes[i];
        if (cb == item) {
          break;
        }
        if (!cb.disabled) {
          prevItem = cb;
        }
      }
      if (prevItem) {
        item = prevItem;
      }
    }
  }
  return item;
};
AdfDhtmlSelectManyPeer.prototype.GetNextItem = function(component, domElement, item, containerName) {
  AdfAssert.assertDomElement(domElement);
  var boxes = this.GetCheckboxes(component, domElement, containerName);
  if (boxes) {
    var nextItem = null;
    for (var i = boxes.length - 1;i >= 0;i--) {
      var cb = boxes[i];
      if (cb == item) {
        break;
      }
      if (!cb.disabled) {
        nextItem = cb;
      }
    }
    if (nextItem) {
      item = nextItem;
    }
  }
  return item;
};
AdfDhtmlSelectManyPeer.prototype.SelectRange = function(component, domElement, newItem, oldItem, containerName) {
  var boxes = this.GetCheckboxes(component, domElement, containerName);
  if (boxes) {
    var selectAllItem = this.GetSelectAllCheckbox(component);
    var nextItem = null;
    var started = false;
    var endItem;
    if (newItem == selectAllItem) {
      started = true;
      endItem = oldItem;
    } else {
      if (oldItem == selectAllItem) {
        started = true;
        endItem = newItem;
      }
    }
    for (var i = 0;i < boxes.length;i++) {
      var cb = boxes[i];
      if (!started) {
        if (cb == newItem) {
          started = true;
          endItem = oldItem;
        } else {
          if (cb == oldItem) {
            started = true;
            endItem = newItem;
          }
        }
      }
      if (started) {
        if (!cb.disabled) {
          cb.checked = true;
          this.UpdateSelectedStyle(cb);
        }
        if (cb == endItem) {
          break;
        }
      }
    }
  }
};
AdfDhtmlSelectManyPeer.prototype.UpdateAll = function(component, domElement, isChecked, validate) {
  var boxes = this.GetCheckboxes(component, domElement);
  if (boxes) {
    for (var i = 0;i < boxes.length;i++) {
      var box = boxes[i];
      if (!box.disabled) {
        box.checked = isChecked;
      }
    }
    if (validate) {
      var value = this.GetSubmittedValue(component, domElement);
      this.Validate(component, value);
    }
  }
};
AdfDhtmlSelectManyPeer.prototype.UpdateSelectedStyle = function(checkbox) {
  var parentElement = checkbox.parentNode.parentNode;
  if (checkbox) {
    if (checkbox.checked) {
      if (!AdfDomUtils.containsCSSClassName(parentElement, AdfDhtmlSelectManyPeer.SELECTED_STYLE)) {
        AdfDomUtils.addCSSClassName(parentElement, AdfDhtmlSelectManyPeer.SELECTED_STYLE);
        if (parentElement.getAttribute("role") == "option") {
          parentElement.setAttribute("aria-selected", "true");
        }
      }
    } else {
      if (AdfDomUtils.containsCSSClassName(parentElement, AdfDhtmlSelectManyPeer.SELECTED_STYLE)) {
        AdfDomUtils.removeCSSClassName(parentElement, AdfDhtmlSelectManyPeer.SELECTED_STYLE);
        if (parentElement.getAttribute("aria-selected")) {
          parentElement.removeAttribute("aria-selected");
        }
      }
    }
  }
};
AdfDhtmlSelectManyPeer.prototype.GetSelectItemsParentDomElement = function(component) {
  return AdfDhtmlEditableValuePeer.getContentNode(component);
};
AdfDhtmlSelectManyPeer.GetElementByAttributeValue = function(elementArr, attrName, attrValue) {
  AdfAssert.assertObject(elementArr);
  AdfAssert.assertNonEmptyString(attrName);
  AdfAssert.assertString(attrValue);
  var length = elementArr.length;
  for (var i = 0;i < length;i++) {
    var element = elementArr[i];
    if (element.getAttribute(attrName) == attrValue) {
      return element;
    }
  }
  return null;
};
AdfDhtmlSelectManyPeer.HIGHLIGHTED_STYLE = "p_AFHighlighted";
AdfDhtmlSelectManyPeer.SELECTED_STYLE = "p_AFSelected";
