﻿function FormField(visibleId, submitId, isRadio) {
   // OBJECT CONSTRUCTION
   this.isRadio = (typeof (isRadio) == "undefined") ? false : isRadio;
   this.id = visibleId;
   this.submitId = submitId;
   this.submitId;
   if (this.isRadio) {
      this.visibleOptions = document.getElementsByName(this.id);
      this.submitOptions = document.getElementsByName(this.submitId);
      if (this.visibleOptions.length > 0 && this.submitOptions.length > 0) {
         // Update checked state of visible options
         for (var i = 0; i < this.submitOptions.length; i++) {
            this.visibleOptions[i].checked = this.submitOptions[i].checked;
            var label = this.visibleOptions[i].getAttribute("label");
            if (label) {
               this.visibleOptions[i].label = document.getElementById(label);
            }
         }
         this.getOption = __ff_getOption;
         this.selectedOption = __ff_selectedOption;
         this.updateSubmitOptions = __ff_updateSubmitOptions;
      }
      return;
   }
   if (this.id) {
      this.element = document.getElementById(this.id);
      if (this.element) {
         this.element._formField = this;
         this.focus = __ff_focus;
      }
   }
   if (this.submitId) {
      this.submitElement = document.getElementById(this.submitId);
      if (this.element && this.submitElement) {
         __setElementText(this.element, __getElementText(this.submitElement));
      }
   }
}
function __getElementText(e) {
   if (typeof (e.value) != "undefined") {
      return (e.value);
   }
   return (e.innerHTML);
}
function __setElementText(e, t) {
   if (typeof (e.value) != "undefined") {
      e.value = t;
   }
   else {
      e.innerHTML = t;
   }
}
FormField.prototype.getText = function() {
   if (this.element)
      return (__getElementText(this.element));
   if (this.submitElement)
      return (__getElementText(this.submitElement));
};
FormField.prototype.setText = function(t) {
   if (this.element) {
      __setElementText(this.element, t);
   }
   if (this.submitElement) {
      __setElementText(this.submitElement, t);
   }
};
FormField.prototype.RegEx_replaceQuotes = /[\'|\"]/g;
FormField.prototype.str_quoteReplacement = "`";
FormField.prototype.fixValueBeforeSubmit = function(v) {
   return (v.replace(this.RegEx_replaceQuotes, this.str_quoteReplacement));
};
FormField.prototype.updateSubmitField = function() {
   if (this.isRadio) {
      if (typeof (this.updateSubmitOptions) != "undefined")
         this.updateSubmitOptions();
      return;
   }
   if (!this.element || !this.submitElement)
      return;
   var updateValue = this.fixValueBeforeSubmit(__getElementText(this.element));
   if (this.submitElement)
      __setElementText(this.submitElement, updateValue);
};
function __ff_focus() {
   this.element.focus();
}
function __ff_updateSubmitOptions() {
   if (this.submitOptions.length == 0 || this.visibleOptions.length == 0)
      return;
   for (var i = 0; i < this.visibleOptions.length; i++) {
      this.submitOptions[i].checked = this.visibleOptions[i].checked;
   }
}
function __ff_selectedOption() {
   if (this.visibleOptions && this.visibleOptions.length > 0) {
      for (var i = 0; i < this.visibleOptions.length; i++) {
         if (this.visibleOptions[i].checked)
            return (this.visibleOptions[i]);
      }
   }
   return (null);
}
function __ff_getOption(i) {
   if (this.visibleOptions && this.visibleOptions.length > 0 && this.visibleOptions.length > i) {
      return (this.visibleOptions[i]);
   }
}
function __ff_stripAlphaChars(v) {
   return (v.replace(/[^0-9]/g, ''));
}
function RadioField(name, linkField) {
   this.name = name;
   this.linkField = linkField;
   this.options = document.getElementsByName(this.name);
   this.optionsByValue = new Array();
   this.labels = new Array();
   // Initialize the option labels and by value array
   this.iterate(this._initOption);
   /* init value */
   if (this.linkField) {
      var selectedOption = this.optionsByValue[this.linkField.Object.getText()];
      if (selectedOption) {
         selectedOption.checked = true;
      }
   }
}
function __getNodeText(node) {
   if (node.innerHTML)
      return (node.innerHTML);
   if (node.data)
      return (node.data);
   if (node.textContent)
      return (node.textContent);
   return ("");
}
function _getOptionText(opt) {
   if (opt.label) {
      if (opt.label.childNodes && opt.label.childNodes.length > 0) {
         var text = "";
         for (var i = 0; i < opt.label.childNodes.length; i++) {
            var node = opt.label.childNodes[0];
            if (node.childNodes.length > 0) {
               for (var j = 0; j < node.childNodes.length; j++) {
                  text += __getNodeText(node.childNodes[j]);
               }
            }
            else
               text += __getNodeText(node);
         }
         return (text);
      }

      return (opt.label.innerHTML);
   }
   return (opt.value);
}
RadioField.prototype.iterate = function(iterator) {
   if (!iterator)
      return (null);
   for (var i = 0; i < this.options.length; i++) {
      var retVal = iterator(this.options[i], this);
      if (retVal)
         return (retVal);
   }
   return (null);
};
RadioField.prototype._initOption = function(option, field) {
   option.fieldObj = field;
   var label = document.getElementById(option.id + "_label");
   if (label) {
      option.label = label;
   }
   option.fieldObj.optionsByValue[_getOptionText(option)] = option;
};
RadioField.prototype._getSelectedIterator = function(option) {
   if (option.checked)
      return (option);
   return (null);
};
RadioField.prototype.getSelected = function() {
   return (this.iterate(this._getSelectedIterator));
};
RadioField.prototype.getValue = function() {
   var selected = this.getSelected();
   if (selected) {
      return (_getOptionText(selected));
   }
   return (null);
};
RadioField.prototype.updateLink = function() {
   this.linkField.Object.setValue(this.getValue());
};
RadioField.prototype.errorOnClick = function() {
   var srcOption = null;
   if (window.event) {
      if (window.event.srcElement)
         srcOption = window.event.srcElement;
   }
   if (srcOption) {
      srcOption.fieldObj.clearError();
   }
};
RadioField.prototype.setErrorIterator = function(option, field) {
   option.style.backgroundColor = field.errorBackgroundColor;
   option.label.style.backgroundColor = field.errorBackgroundColor;
   option.style.color = field.errorColor;
   option.label.style.color = field.errorColor;
   option.onclick = field.errorOnClick;
};
RadioField.prototype.setError = function(errorColor, errorBackgroundColor) {
   this.errorColor = errorColor;
   this.errorBackgroundColor = errorBackgroundColor;
   this.iterate(this.setErrorIterator);
};
RadioField.prototype.clearErrorIterator = function(option) {
   option.onclick = null;
   option.style.color = "";
   option.label.style.color = "";
   option.style.backgroundColor = "";
   option.label.style.backgroundColor = "";
};
RadioField.prototype.clearError = function() {
   this.iterate(this.clearErrorIterator);
};
