/**
 * Generated by Apache Royale Compiler from org\apache\royale\jewel\beads\validators\FormValidator.as
 * org.apache.royale.jewel.beads.validators.FormValidator
 *
 * @fileoverview
 *
 * @suppress {checkTypes|accessControls}
 */

goog.provide('org.apache.royale.jewel.beads.validators.FormValidator');

goog.require('org.apache.royale.core.UIBase');
goog.require('org.apache.royale.events.Event');
goog.require('org.apache.royale.events.IEventDispatcher');
goog.require('org.apache.royale.jewel.Group');
goog.require('org.apache.royale.jewel.Snackbar');
goog.require('org.apache.royale.jewel.beads.validators.Validator');
goog.require('org.apache.royale.utils.Language');



/**
 *  constructor.
 *
 *  @langversion 3.0
 *  @playerversion Flash 10.2
 *  @playerversion AIR 2.6
 *  @productversion Royale 0.9.4
 * @constructor
 * @extends {org.apache.royale.jewel.beads.validators.Validator}
 */
org.apache.royale.jewel.beads.validators.FormValidator = function() {
  org.apache.royale.jewel.beads.validators.FormValidator.base(this, 'constructor');
  org.apache.royale.jewel.beads.validators.FormValidator.superClass_.set__requiredFieldError.apply(this, [ null] );
};
goog.inherits(org.apache.royale.jewel.beads.validators.FormValidator, org.apache.royale.jewel.beads.validators.Validator);


/**
 * Prevent renaming of class. Needed for reflection.
 */
goog.exportSymbol('org.apache.royale.jewel.beads.validators.FormValidator', org.apache.royale.jewel.beads.validators.FormValidator);


/**
 * @private
 * @type {org.apache.royale.events.IEventDispatcher}
 */
org.apache.royale.jewel.beads.validators.FormValidator.prototype.org_apache_royale_jewel_beads_validators_FormValidator__trigger;


/**
 * @private
 * @type {string}
 */
org.apache.royale.jewel.beads.validators.FormValidator.prototype.org_apache_royale_jewel_beads_validators_FormValidator__triggerEvent;


/**
 * @private
 * @type {boolean}
 */
org.apache.royale.jewel.beads.validators.FormValidator.prototype.org_apache_royale_jewel_beads_validators_FormValidator__isError;


/**
 *  Override of the base class validate() method to call all validators in the form.
 *  dispatch invalid/valid event when validation fails/succeeds. 
 * 
 *  @langversion 3.0
 *  @playerversion Flash 10.2
 *  @playerversion AIR 2.6
 *  @productversion Royale 0.9.4
 * @export
 * @override
 */
org.apache.royale.jewel.beads.validators.FormValidator.prototype.validate = function(event) {
  event = typeof event !== 'undefined' ? event : null;
  this.org_apache_royale_jewel_beads_validators_FormValidator__isError = false;
  this.validateAll(org.apache.royale.utils.Language.as(this.hostComponent, org.apache.royale.jewel.Group));
  if (this.isError) {
    if (this.requiredFieldError)
      org.apache.royale.jewel.Snackbar.show(this.requiredFieldError);
    this.hostComponent.dispatchEvent(new org.apache.royale.events.Event("invalid"));
  } else {
    this.hostComponent.dispatchEvent(new org.apache.royale.events.Event("valid"));
  }
  return !this.isError;
};


/**
 * @protected
 * @param {org.apache.royale.jewel.Group} group
 */
org.apache.royale.jewel.beads.validators.FormValidator.prototype.validateAll = function(group) {
  for (var /** @type {number} */ i = 0; i < group.numElements; i++) {
    var /** @type {org.apache.royale.core.UIBase} */ child = org.apache.royale.utils.Language.as(group.getElementAt(i), org.apache.royale.core.UIBase);
    var /** @type {org.apache.royale.jewel.beads.validators.Validator} */ validator = org.apache.royale.utils.Language.as(child.getBeadByType(org.apache.royale.jewel.beads.validators.Validator), org.apache.royale.jewel.beads.validators.Validator);
    if (validator && !org.apache.royale.utils.Language.is(validator, org.apache.royale.jewel.beads.validators.FormValidator)) {
      if (!validator.validate()) {
        this.org_apache_royale_jewel_beads_validators_FormValidator__isError = true;
      }
    }
    if (org.apache.royale.utils.Language.is(child, org.apache.royale.jewel.Group)) {
      this.validateAll(org.apache.royale.utils.Language.as(child, org.apache.royale.jewel.Group));
    }
  }
};


org.apache.royale.jewel.beads.validators.FormValidator.prototype.get__trigger = function() {
  return this.org_apache_royale_jewel_beads_validators_FormValidator__trigger;
};


org.apache.royale.jewel.beads.validators.FormValidator.prototype.set__trigger = function(value) {
  if (this.org_apache_royale_jewel_beads_validators_FormValidator__triggerEvent) {
    if (this.org_apache_royale_jewel_beads_validators_FormValidator__trigger)
      this.org_apache_royale_jewel_beads_validators_FormValidator__trigger.removeEventListener(this.org_apache_royale_jewel_beads_validators_FormValidator__triggerEvent, org.apache.royale.utils.Language.closure(this.validate, this, 'validate'));
    if (value)
      value.addEventListener(this.org_apache_royale_jewel_beads_validators_FormValidator__triggerEvent, org.apache.royale.utils.Language.closure(this.validate, this, 'validate'));
  }
  this.org_apache_royale_jewel_beads_validators_FormValidator__trigger = value;
};


org.apache.royale.jewel.beads.validators.FormValidator.prototype.get__triggerEvent = function() {
  return this.org_apache_royale_jewel_beads_validators_FormValidator__triggerEvent;
};


org.apache.royale.jewel.beads.validators.FormValidator.prototype.set__triggerEvent = function(value) {
  if (this.org_apache_royale_jewel_beads_validators_FormValidator__trigger) {
    if (this.org_apache_royale_jewel_beads_validators_FormValidator__triggerEvent)
      this.org_apache_royale_jewel_beads_validators_FormValidator__trigger.removeEventListener(this.org_apache_royale_jewel_beads_validators_FormValidator__triggerEvent, org.apache.royale.utils.Language.closure(this.validate, this, 'validate'));
    if (value)
      this.org_apache_royale_jewel_beads_validators_FormValidator__trigger.addEventListener(value, org.apache.royale.utils.Language.closure(this.validate, this, 'validate'));
  }
  this.org_apache_royale_jewel_beads_validators_FormValidator__triggerEvent = value;
};


org.apache.royale.jewel.beads.validators.FormValidator.prototype.get__isError = function() {
  return this.org_apache_royale_jewel_beads_validators_FormValidator__isError;
};


Object.defineProperties(org.apache.royale.jewel.beads.validators.FormValidator.prototype, /** @lends {org.apache.royale.jewel.beads.validators.FormValidator.prototype} */ {
/**
  * @export
  * @type {org.apache.royale.events.IEventDispatcher} */
trigger: {
get: org.apache.royale.jewel.beads.validators.FormValidator.prototype.get__trigger,
set: org.apache.royale.jewel.beads.validators.FormValidator.prototype.set__trigger},
/**
  * @export
  * @type {string} */
triggerEvent: {
get: org.apache.royale.jewel.beads.validators.FormValidator.prototype.get__triggerEvent,
set: org.apache.royale.jewel.beads.validators.FormValidator.prototype.set__triggerEvent},
/**
  * @export
  * @type {boolean} */
isError: {
get: org.apache.royale.jewel.beads.validators.FormValidator.prototype.get__isError}}
);


/**
 * Metadata
 *
 * @type {Object.<string, Array.<Object>>}
 */
org.apache.royale.jewel.beads.validators.FormValidator.prototype.ROYALE_CLASS_INFO = { names: [{ name: 'FormValidator', qName: 'org.apache.royale.jewel.beads.validators.FormValidator', kind: 'class' }] };



/**
 * Reflection
 *
 * @return {Object.<string, Function>}
 */
org.apache.royale.jewel.beads.validators.FormValidator.prototype.ROYALE_REFLECTION_INFO = function () {
  return {
    variables: function () {return {};},
    accessors: function () {
      return {
        'trigger': { type: 'org.apache.royale.events.IEventDispatcher', access: 'readwrite', declaredBy: 'org.apache.royale.jewel.beads.validators.FormValidator'},
        'triggerEvent': { type: 'String', access: 'readwrite', declaredBy: 'org.apache.royale.jewel.beads.validators.FormValidator'},
        'isError': { type: 'Boolean', access: 'readonly', declaredBy: 'org.apache.royale.jewel.beads.validators.FormValidator'}
      };
    },
    methods: function () {
      return {
        'FormValidator': { type: '', declaredBy: 'org.apache.royale.jewel.beads.validators.FormValidator'},
        'validate': { type: 'Boolean', declaredBy: 'org.apache.royale.jewel.beads.validators.FormValidator', parameters: function () { return [  { index: 1, type: 'org.apache.royale.events.Event', optional: true } ]; }}
      };
    }
  };
};