
var OTOS = window.OTOS || {};
OTOS.FormElement = function () {

    return {

        /**
         * Function disables/enables the selected element.
         *
         * @param   {string}    elementId   The id of the html-element to be disabled/enabled
         * @param   {boolean}   bool        Are we going to disable the element (true) or enable (false)
         */
        disableElement: function (elementId, bool) {

            var el      = $(elementId),
                clsName = 'disabled';

            el.disabled = bool;

            if (bool && !Element.hasClassName(el, clsName)) {
                Element.addClassName(el, clsName);
            } else if (!bool && Element.hasClassName(el, clsName)) {
                Element.removeClassName(el, clsName);
            } // else if
        },


        /**
         * Selects all the options from the dropdown list
         *
         * @param   {element}   el  The element
         */
        selectAll: function (el) {
            for (var i = 0; i < el.options.length; i++) {
                el.options[i].selected = true;
            } // for
        },


        /**
         * Clears all selections of a selectlist
         *
         * @param   {element}   el  The select-element
         */
        selectNone: function (el) {
            for (var i = 0; i < el.options.length; i++) {
                el.options[i].selected = '';
            } // for
        },


        /**
         * Sets an option with a certain value selected in a select-list
         *
         * @param   {element}   el      The select-element
         * @param   {string}    val     The value we're searching
         * @return  Whether the value was found from the select-list
         * @type    {boolean}
         */
        selectOptionByValue: function (el, val) {

            for (var i = 0; i < el.options.length; i++) {

                if (el.options[i].value == val) {
                    el.options[i].selected = true;
                    return true;
                } // if

            } // for

            return false;

        },


        /**
         * Sets a text for an option element that has a certain value
         *
         * @param   {element}   el      The select-element
         * @param   {string}    val     The value we're searching
         * @param   {string}    str     The text that is set for the element
         */
        setOptionTextByValue: function (el, val, str) {

            for (var i = 0; i < el.options.length; i++) {

                if (el.options[i].value == val) {
                    el.options[i].text = str;
                    break;
                } // if

            } // for

        },


        /**
         * Disables or enables form's every input, select and textarea element.
         * If  the bool-attribute is true, function sets every element's disabled-
         * attribute as true and adds a class name 'disabled' to each of
         * the elements. If the bool-attribute is false, the disabled-attribute is
         * set to false and the class name 'disabled' is removed (if exists).
         *
         * @param   {element}   frm     The parent form
         * @param   {boolean}   bool    Whether to disable (true) or enable (false)
         *                              the form's elements
         */
        disableForm: function (frm, bool) {

            var tags = [ 'input', 'select', 'textarea' ];

            for (var i = 0; i < tags.length; i++) {

                var els = frm.getElementsByTagName(tags[i]);

                for (var j = 0; j < els.length; j++) {
                    OTOS.FormElement.disableElement(els[j], bool);
                } // for

            } // for

        }

    };

}();
