function FormHelper(infoBar, ifuBuilder) {
    this._canSubmit    = true;
    this.infoBar       = infoBar;
    this.form          = null;
    this.errors        = {};
    this.ifuBuilder    = ifuBuilder;
    this.catchRedirect = true;
}


FormHelper.prototype = {
    _fire: function(evt, params) {
        evt = 'formHelper:' + evt;
        this.form.fire(evt, params);
    },
    removeListeners: function() {
        if (this.errorContainer) {
            this.form.stopObserving();
        }
    },
    observe: function(evt, callback) {
        evt = 'formHelper:' + evt;
        this.form.observe(evt, callback);
    },
    setForm: function(form) {
       /**
        * Iframe based upload handling code removed,
        * in favor of the flashuploader one, which can
        * be found in js/flashuploader.js
        */
        this.form = form;
    },
    preSubmitInit: function(e) {
        var form = this.form;
        for (var i in tinymce.editors) {
            var cur = tinymce.editors[i];
            if (!Object.isFunction(cur)) {
                var textarea = cur.getElement();
                if (textarea && textarea.descendantOf(form)) {
                    e.params[cur.getElement().readAttribute('name')] = cur.save();
                }
            }
        }
    },
    submit: function(values) {
        if (!this._canSubmit) {
            alert('Egy (vagy több) feltöltés folyamatban van, kérlek várj egy kicsit');
            return;
        }
        this.preSubmitInit({params: values});
        NCore.Ajax(this.form.getAttribute('action'), {
            method: 'POST',
            parameters: values,
            onFailure: function(o) {
                var json   = $H(o.responseJSON);
                var errors = json.get('errors');

                this.clearErrors();
                this._fire('validationFail', { errors: errors, response: json, formHelper: this });
                this.showErrors(errors);
            }.bind(this),
            onSuccess: function(o) {
                var json   = $H(o.responseJSON);
                this.clearErrors();
                this._fire('validationSuccess', { response: json.toObject(), formHelper: this });
            }.bind(this)
        }, { catchRedirect: this.catchRedirect });
    },
    clearError: function(element) {
        /**
         * Nagyon idióta módon van kulcsolva a tömb, pont forditva kéne,
         * de hash kulcsnak nem lehet objektumot használni.
         *
         * Itt kiválasztjuk azt a kulcsot ami pont az átadott elemhez tartozik. (php array_search)
         */
        for (var infoId in this.errors) {
            var curElem = this.errors[infoId];
            if (curElem === element) {
                var elemContainer = element.up('.caption-decorator');
                this.infoBar.remove(infoId);
                if (elemContainer) {
                    elemContainer.removeClassName('field-error');
                }
                break;
            }
        }
    },
    clearErrors: function() {
        for (var infoId in this.errors) {
            this.clearError(this.errors[infoId]);
        }
    },
    showError: function(element, error) {
        var container       = element.up('.caption-decorator');
        var caption         = container.select('label').first().innerHTML;
        var errorMsg        = error.first().interpolate({ fieldname: caption });
        var infoId          = this.infoBar.info(errorMsg, 'error-box');
        this.errors[infoId] = element;

        container.addClassName('field-error');
    },
    showErrors: function(errors) {
        $H(errors).forEach(function(field, error) {
            var element        = this.form.select('[name=' + field + ']').first();
            this.showError(element, error);
        }.bind(this));
    }
}

