define([
    "jquery",
    "../registry",
    '../core/logger',
    "jquery_validate"
], function($, patterns, logger) {
    var log = logger.getLogger('pat.validate');

    var pattern_spec = {
        name: "validate",
        trigger: "form.validate",

        init: function($el) {
            var validators = $el.find('[data-validator]').toArray().reduce(function(acc, el) {
                var i, path_to_func, $el = $(el),
                    varray = $el.data('validator').split(' ', 2),
                    message = $el.data('validator-message');

                path_to_func = varray[1].split('.');
                func = window[path_to_func[0]];
                for (i=1; i<path_to_func.length; i++) { 
                    try {
                        func = func[path_to_func[i]]; 
                    } catch(e) {
                        log.error('Could not find the validator function: ' + varray[1]);
                    }
                }
                $.validator.addMethod(varray[0], func, message);
            }, {});


            var rules = $el.find('[data-required-if]').toArray().reduce(function(acc, el) {
                var $el = $(el),
                    id = $el.attr('id');
                if (!id) {
                    log.error('Element needs id, skipping:', $el);
                    return acc;
                }
                acc[id] = {required: $el.data('required-if')};
                return acc;
            }, {});
            log.debug('rules:', rules);

            // ATTENTION: adding the debug option to validate, disables
            // form submission
            $el.validate({rules: rules});
            return $el;
        }
    };

    patterns.register(pattern_spec);
    return pattern_spec;
});
// jshint indent: 4, browser: true, jquery: true, quotmark: double
// vim: sw=4 expandtab

define([
    "jquery",
    "../registry",
    '../core/logger',
    "jquery_validate"
], function($, patterns, logger) {
    var log = logger.getLogger('pat.validate');

    var pattern_spec = {
        name: "validate",
        trigger: "form.validate",

        init: function($el) {
            var validators = $el.find('[data-validator]').toArray().reduce(function(acc, el) {
                var i, path_to_func, $el = $(el),
                    varray = $el.data('validator').split(' ', 2),
                    message = $el.data('validator-message');

                path_to_func = varray[1].split('.');
                var func = window[path_to_func[0]];
                for (i=1; i<path_to_func.length; i++) {
                    try {
                        func = func[path_to_func[i]];
                    } catch(e) {
                        log.error('Could not find the validator function: ' + varray[1]);
                    }
                }
                $.validator.addMethod(varray[0], func, message);
            }, {});


            var rules = $el.find('[data-required-if]').toArray().reduce(function(acc, el) {
                var $el = $(el),
                    id = $el.attr('id');
                if (!id) {
                    log.error('Element needs id, skipping:', $el);
                    return acc;
                }
                acc[id] = {required: $el.data('required-if')};
                return acc;
            }, {});
            log.debug('rules:', rules);

            // ATTENTION: adding the debug option to validate, disables
            // form submission
            $el.validate({rules: rules});
            return $el;
        }
    };

    patterns.register(pattern_spec);
    return pattern_spec;
});
// jshint indent: 4, browser: true, jquery: true, quotmark: double
// vim: sw=4 expandtab

declare variable, whitespace