﻿function xFormElement(parent, element) {
  try {
    var type = element.getAttribute('type');
    if (null != type) {
      this.inputType = type.toLowerCase();
    } else {
      this.inputType = "";
    }

    this.parent = parent;
    this.form = parent.form;
    this.element = element;
    this.subElements = new Array();
    this.generateGUID();
    this.parseName();
    element.xelement = this;
    element.xform = this.form;
  } catch(e) {
  }
}
xFormElement.prototype.resetElements = xForm.prototype.resetElements;
xFormElement.prototype.addSubElement = xForm.prototype.addSubElement;
xFormElement.prototype.getSubElements = xForm.prototype.getSubElements;
xFormElement.prototype.getCustomProperty = xForm.prototype.getCustomProperty;
xFormElement.prototype.setCustomProperty = xForm.prototype.setCustomProperty;
xFormElement.prototype.getGUID = xForm.prototype.getGUID;
xFormElement.prototype.getElement = xForm.prototype.getElement;
xFormElement.prototype.reset = xForm.prototype.reset;
xFormElement.prototype.generateGUID = xForm.prototype.generateGUID;
xFormElement.prototype.getParent = xForm.prototype.getParent;
xFormElement.prototype.setParent = xForm.prototype.setParent;
xFormElement.prototype.setElementName = xForm.prototype.setElementName;

xFormElement.prototype.parseName = function() {
  if (this.element.xelement == null && this.element.getAttribute('name') != null) {//клонированный или новый элемент
    if (this.element.getAttribute('originalName') != null) {
      this.name = this.element.getAttribute('originalName');
    } else {
      this.name = this.element.getAttribute('name');
      this.element.setAttribute('originalName', this.name);
    }
  }
  this.setElementName(this.getFullName());

  if (this.isCheckable()) { // восстановить checked для одинаковых элементов
    var checked;
    if (this.element.outerHTML) {
      checked = this.element.getAttributeNode('checked').value == 'true';
    } else {
      checked = this.element.getAttribute('checked') != null;
    }
    this.element.checked = checked;
    //    this.element.style.border = "1px solid red !important";
  }
};

xFormElement.prototype.getValue = function() {
  return this.element.value;
};
xFormElement.prototype.getName = function() {
  return this.name;
};
xFormElement.prototype.setValue = function(value) {
  if (!this.isCheckable()) {
    this.element.value = value;
  }
};
xFormElement.prototype.isCheckable = function() {
  return this.inputType == 'checkbox' || this.inputType == 'radio';
};
xFormElement.prototype.getDataType = function() {
  return this.getCustomProperty('dataType', 'form');
};
xFormElement.prototype.isValidateable = function() {
  return true;
};
xFormElement.prototype.toString = function() {
  return this.name;
};
xFormElement.prototype.getFullName = function() {
  return this.getParent().getFullName() + "." + this.name;
};

/**----------------------------------*/
function xFormElementDatePicker(parent, element) {
  this.parent = parent;
  this.form = parent.form;
  this.element = element;
  this.subElements = new Array();
  this.generateGUID();
  this.parseName();

  element.xelement = this;
  element.xform = this.form;
  this.setUp();
}
xFormElementDatePicker.prototype = new xFormElement();
xFormElementDatePicker.prototype.setUp = function() {
  if (this.element.parsed ||
      this.getCustomProperty('noSelector', 'form')) {
    return;
  }
  if (this.element.nextSibling &&
      this.element.nextSibling.getAttribute &&
      this.element.nextSibling.getAttribute('calendar') == 'yes') {
    DOMUtl.removeNode(this.element.nextSibling);
  }
  var selector = DOMUtl.createNode(
          'input',
  {type : 'button', value : this.getCustomProperty('showCalendar', 'buttons'), calendar : 'yes'},
  {onclick : new Function('xCalendar.show(this.previousSibling);')},
          this.element.ownerDocument);
  DOMUtl.appendAfter(selector, this.element);
  this.element.parsed = true;
};
xFormElementDatePicker.prototype.reset = function() {
  //  alert();
};
/**----------------------------------*/
function xFormElementGroup(parent, element) {
  this.parent = parent;
  this.form = parent.form;
  this.element = element;
  this.subElements = new Array();
  this.generateGUID();
  this.name = this.getCustomProperty('set', 'form');
  this.suffix = xFormParser.getNextElementIndex();
  this.isCloned = this.getCustomProperty('cloned', 'form');

  element.xelement = this;
  element.xform = this.form;
}
xFormElementGroup.prototype = new xFormElement();
xFormElementGroup.prototype.getFullName = function() {
  return this.getParent().getFullName() + "." + this.name + "[" + this.suffix + "]";
};
xFormElementGroup.prototype.setElementName = function() {
  // do nothing
};
xFormElementGroup.prototype.setValue = function(value) {
  if (!value && 'yes' == this.isCloned) {
    /* tricky!
     set is appended to dom tree AFTER parsing
     root set has no parent and cant be removed from dom tree
     child sets have parents and are removed on reparsing after cloning */
    try {
      DOMUtl.removeNode(this.element);
    } catch(e) {
    }
  }
};

/**----------------------------------*/
var xFormElementFactory = {
  types : {
    date : xFormElementDatePicker,
    group : xFormElementGroup
  }
};
xFormElementFactory.createElement = function(form, node) {
  var dataType = node.getAttribute(xFormParser.getFilterNS('form') + ':dataType');
  if (node.xelement != null) {
    return node.xelement;
  }
  if (this.types[dataType]) {
    return new this.types[dataType](form, node);
  } else {
    return new xFormElement(form, node);
  }
};