/**
 * Used in conjunction with htmk5validator this helper highlights the 
 * tabs with errors en marks incorrect fields.
 * 
 * @author Visual Media
 */

(function($) {    
    
    $(document).ready(function() {
        
        /**
         * Mark tabs that contains errors
         * 
         * @param {DOMElement} frm       Form to check
         * @param {Boolean}    autofocus Autofocus first tab with erros
         * 
         * @return {undefined}
         */
        var updateTabStatus = function(frm, autofocus) {            
            frm.find('a[data-toggle="tab"]').each(function() {            
                var tab = $(this);
                var href_re = /#([a-z_0-9]+)$/;
                var href = tab.attr('href');
                if (!href_re.test(href)) {
                    return;
                }                        
                var id = href_re.exec(href)[1];            
                var pane = frm.find('#' + id + '.tab-pane');
                
                if (pane.find('.html5-error').length > 0) {
                    if (!tab.hasClass('html5-tab-error')) {
                        tab.addClass('html5-tab-error').append('<span class="html5-error-sign">&nbsp;<i class="icon icon-exclamation-sign"></i></span>');
                    }
                } else {
                    tab.removeClass('html5-tab-error').find('.html5-error-sign').remove();                
                }            
            });

            if (autofocus) {
                if (frm.find('a[data-toggle="tab"]').length > 0) {
                    frm.find('a.html5-tab-error').first().tab('show');
                }
            }
        };
        
        // Go trough forms to validate
        $('.form-html5-validation').each(function() {
            
            var frm = $(this);
            
            // Initialize new html5validator version
            $(this).html5validator({
                
                /**
                 * {@inheritdoc}
                 */                
                validField : function(field, onsubmit) {                    
                    if (field.parent().parent('.control-group').length !== 0) {
                        field.parent().parent('.control-group').removeClass('html5-error').removeClass('error');
                        field.siblings('.help-block.html5-error-message').remove();
                    }
                    updateTabStatus(frm, false);                    
                    return true;
                },
                       
                /**
                 * {@inheritdoc}
                 */     
                inValidField : function(field, message, onsubmit) {
                    if (onsubmit) {
                        if (field.parent().parent('.control-group').length !== 0) {
                            field.parent().parent('.control-group').addClass('html5-error').addClass('error');
                            if (field.siblings('.help-block.html5-error-message').length === 0) {
                                field.parent().append($('<p class="help-block html5-error-message" />').text(message));
                            }
                        }
                    }
                    return false;
                },
                
                /**
                 * {@inheritdoc}
                 */     
                beforeSubmit : function(invalid) {
                    updateTabStatus(frm, true);
                    if (invalid.length === 0) {
                        return true;
                    }
                    if ($('.html5-error-modal').length !== 0) {
                        $('.html5-error-modal').modal();
                    }  
                    return false;
                },
                        
                requiredMessage : 'Dit is een verplicht veld',
                requiredGroupMessage : 'Selecteer een optie',
                patternMessage : 'Ongeldige invoer',
                types: {
                    number: {
                        message: "Vul een nummer in."
                    },
                    email: {
                        message: "Vul een geldig e-mailadres in."
                    },
                    url: {
                        message: "Vul een geldig internet adres in."
                    },
                    tel: {
                        message: "Vul een geldig telefoonnummer in."
                    }
                }
                
            });
        });
    });    
    
})(jQuery);
