/**
 * object that provide several simple operations with forms using current project URL structure
 */
var FormAction = {

    /**
     * prior action
     *
     * @var {string}
     */
    priorAction: null,

    /**
     * prior resource
     *
     * @var {string}
     */
    priorResource: null,

    /**
     * parameters
     *
     * @var {object}
     */
    params: {},

    /**
     * @var {bool}
     */
    submitStart: false,

    /**
     * submit form
     *
     * @param {string} formId
     * @param {string} resource
     * @param {string} action
     * @param {string} button
     * @param {boolean} withoutBack
     * @return void
     */
    submit: function(formId, resource, action, target, button, withoutBack)
    {
        withoutBack = withoutBack || false;
        if (this.submitStart) {
            return false;
        }
        target = target || '';
        button = button || null;
        this.submitStart = true;
        if (button != null) {
            button.disabled = 'disabled';
        }
        var form = Ext.get(formId);
        form.dom.action = buildURL(resource, action, this.params);
        form.dom.target = target;
        if (withoutBack) {
            this.priorAction = null;
            this.priorResource = null;
        }
        this.addPriorResourceAndAction(form);
        form.dom.submit();
        if (target == '') {
            setTimeout(function() {
                FormAction.submitStart = false;
                if (button != null) {
                    button.disabled = '';
                }
            }.createDelegate(this), 10000); /* if action don't reload page then after 10 seconds reset the flag*/
        } else {
            /* for target=_blank pages unlock functionality immediatelly */
            FormAction.submitStart = false;
            if (button != null) {
                button.disabled = '';
            }
        }
    },

    /**
     * submit data from lightbox
     *
     * @param {string} formId
     * @param {string} resource
     * @param {string} action
     */
    lightboxSubmit: function(formId, resource, action)
    {
        if (this.submitStart == true) {
            return false;
        }

        this.submitStart = true;
        var form         = Ext.get(formId);
        form.dom.action  = buildURL(resource, action, this.params);
        form.dom.target  = '';

        this.addPriorResourceAndAction(form);
        form = new Ext.form.BasicForm(formId);

        var elementLightbox = Lightbox.get(form.el);

        if (!Ext.isEmpty(elementLightbox)) {
            elementLightbox.load(
                    form.el.dom.action,
                    form.getValues(),
                    'POST');
            this.submitStart = false;
        }
    },

    /**
     * make preview (submit data to the new window)
     *
     * @param {string} formId
     * @param {string} resource
     * @param {string} action
     * @return void
     */
    preview: function(formId, resource, action)
    {
        var form = Ext.get(formId);
        form.dom.action = buildURL(resource, action, this.params);
        form.dom.target = 'blank';
        form.dom.submit();
    },

    /**
     * cancel form action
     *
     * @param {string} formId
     * @param {string} resource
     * @param {string} action
     * @return
     */
    cancel: function(formId, resource, action)
    {
        var form = Ext.get(formId);
        form.dom.action = buildURL(resource, action, this.params);
        form.dom.target = '';

        form.createChild({
            tag: 'INPUT',
            name: 'confirm',
            type: 'hidden',
            value: 'no'
        });
        this.addPriorResourceAndAction(form);

        form.dom.submit();
    },

    /**
     * set up prior resource and prior action from the page variables
     *
     * @return void
     */
    setPriorResourceAndAction: function()
    {
        if (typeof priorAction != 'undefined' && priorAction) {
            this.priorAction = priorAction;
        }

        if (typeof priorResource != 'undefined' && priorResource) {
            this.priorResource = priorResource;
        }
    },

    /**
     * add back action and back resource parameters to the form
     *
     * @param {HTML DOM object} form
     */
    addPriorResourceAndAction: function(form)
    {
        if (this.priorAction) {
            form.createChild({
                tag: 'INPUT',
                name: 'prior-action',
                type: 'hidden',
                value: this.priorAction
            });
        }

        if (this.priorResource) {
            form.createChild({
                tag: 'INPUT',
                name: 'prior-resource',
                type: 'hidden',
                value: this.priorResource
            });
        }

        if (this.priorAction || this.priorResource) {
            form.createChild({
                tag: 'INPUT',
                name: 'back-to',
                type: 'hidden',
                value: '1'
            });
        }
    },

    /**
     * select all checkboxes that have the same class name like input param have
     *
     * @param {HTML DOM element} el
     * @param {string} actionType
     * @param {bool} omitDisabled
     */
    selectCheckboxes: function(el, actionType, omitDisabled)
    {
        var checkboxes = Ext.query('INPUT.' + el.className, el.form);

        for (var checkboxIndex in checkboxes) {
            if (checkboxes[checkboxIndex] != el && typeof checkboxes[checkboxIndex] != 'function') {
                var checkbox = checkboxes[checkboxIndex];
                if (!(checkbox.disabled && omitDisabled)) {
                    if (checkbox.checked ? !el.checked : el.checked) { /*logical xor*/
                        checkbox.checked = el.checked;
                        if (typeof ActionHandler == 'object') {
                        	ActionHandler.select(el.checked, actionType);
                        }
                    }
                }
            }
        }
    },

    /**
     * select a single checkbox item
     *
     * @param {HTML DOM element} el
     * @param {string} actionType
     * @param {string} mainId - main checkbox id
     */
    selectSingleCheckbox: function(el, actionType, mainId) {

        ActionHandler.select(el.checked, actionType);

        var checkboxes = Ext.query('INPUT.' + el.className, el.form);

        var checked = [];
        for (var checkboxIndex in checkboxes) {
            var checkbox = checkboxes[checkboxIndex];
            if (typeof checkbox != 'function' && mainId != checkbox.id) {
                if (checkbox.checked) {
                    checked.push(1);
                }
            }
        }

        /* checkboxes length without main checkbox */
        if (checkboxes.length - 1 == checked.length) {
            Ext.getDom(mainId).checked = 'checked';
        } else {
            Ext.getDom(mainId).checked = false;
        }
    }
};

/**
 * response command object
 */
var ResponseCommand = {

        /**
         * The list of allowed commands
         */
        commands: {

            /**
             * Redirect command
             *
             * @param {string} url - URL for redirecting
             * @param {object} scope
             *
             * @return false
             */
            redirect: function(url, scope) {
                document.location.href = url;
                return false;
            },

            /**
             * Refresh current page
             *
             * @param {string} param
             * @param {object} scope
             *
             * @return false
             */
            refresh : function(param, scope) {
                document.location.reload(true);
                return false;
            },

            /**
             * Close lightbox window
             *
             * @param {string} msg
             * @param {object} scope - lightbox object instance
             *
             * @return false
             */
            closeLightbox: function(msg, scope)
            {
                scope.hide();
                showError(msg, null, true);
                return false;
            }

        },

        /**
         * run command that received from the server in the JSON object
         *
         * @param {string} parameters
         * @param {object} scope
         */
        run: function(commandObject, scope) {
            for (i in this.commands) {
                if (!Ext.isEmpty(commandObject[i])) {
                    this.commands[i](commandObject[i], scope);
                }
            }
        }
};

Ext.onReady(FormAction.setPriorResourceAndAction, FormAction);

