<commit_before>(function() {

    var formCheckValid = function(el) {
        if (!$(el).is(':visible')) {return null};

        var inputType = (el.tagName.toLowerCase() == 'textarea') ? 'textarea' : $(el).attr('type');

        switch (inputType) {
            case 'radio':
            case 'checkbox':
                return (el.checked) ? el.name : null;
                break;
            case 'text':
            case 'number':
            case 'email':
            case 'tel':
            case 'textarea':
                var pattern = (pattern = $(el).attr('data-pattern')) ? pattern : $(el).attr('pattern');

                if (pattern != null) {
                    var result = $(el).val().match(new RegExp(pattern,"gi"));
                    return (result!= null && result.length > 0 && result[0] != '') ?el.name : null;
                } else {
                    return ($(el).val() != '') ? el.name : null;
                }
                break;
            default:
                return null;
        }
    };

    $(document).ready(function() {

        var formErrorMessages = [];
        formErrorMessages['fraud-suspect'] = 'Please make sure you enter at least<ol class="list-bullet">' +
                                                '<li>A name, approximate age (or date of birth) and an address</li>' +
                                                '<li>A name, approximate age (or date of birth) and some additional info</li>' +
                                                '<li>A National insurance number and an approximate age (or date of birth)</li>' +
                                                '<li>A National insurance number and an address</li>' +
                                                '</ul>';

        $('a.previousPage').each(function() {
            $(this).on('click', function() {
                if (!confirm('Are you sure you want to go back? You may lose form data.\n\nNB: We need to assess the need and functionality of this link')) {
                    return false;
                }
            });

        });


        /* display-none-js is a class that should only work to hide information when javascript is enabled */
        $('.display-none-js').hide();

        /* generic toggle functionality */
        $('.toggle-section').on('click', function() {
            var sections = $(this).parents('.form-group').attr('data-target');
            var sectionOn = $(this).parents('.form-group').attr('data-target') + '__' + $(this).val().toLowerCase();
            $('.' + sections).hide();
            $('.' + sectionOn).show();
        });


        $('form.crm-check input[type="submit"]').on('click', function(e) {

            e.preventDefault();

            var form = $(this).parents('form'),
                validationMessage = '',
                errorMessages = '',
                validationType = '',
                inputFields = ['input[type="text"]', 'input[type="number"]', 'input[type="tel"]', 'input[type="email"]', 'input[type="checkbox"]', 'input[type="radio"]', 'select', 'textarea'],
                fieldsWithValidValue,
                allFields;

            form.removeClass('invalid').find('div.validation-message').remove();

            // loop each validation-group

            form.find('.validation-group').removeClass('invalid valid').each(function() {
                console.log('data-validation-id = ' + $(this).attr('data-validation-id'));
                if ($(this).is(':visible')) {
                    validationType = $(this).attr('data-validation-type');
                    validationMessage = $(this).attr('data-validation-message');

                    var typesToCheck = ['required--one-or-more', 'required--one', 'required--all', 'not-required--one-or-more', 'not-required--one']
                    if (typesToCheck.indexOf(validationType) != -1) {
                        fieldsWithValidValue = $.unique($(this).find(inputFields.join(',')).map(function () {
                            return formCheckValid(this);
                        }).get());
                    }

                    switch (validationType) {
                        // it's not required, so don't display error messages. But when there is valid data, mark it as such
                        case 'not-required--one-or-more':
                        case 'not-required--one':
                            if (fieldsWithValidValue.length > 0) {
                                $(this).addClass('valid');
                            }
                            break;
                        case 'required--one-or-more':
                        case 'required--one':
                            if (fieldsWithValidValue.length == 0) {
                                $(this).addClass('invalid');
                                errorMessages += '<li id="validation-message-' + 0 + '">' + validationMessage + '</li>';
                            } else {
                                $(this).addClass('valid');
                            }
                            break;

                        case 'required--all':
                            allFields = $.unique($(this).find(inputFields.join(',')).map(function () {
                                return this.name;
                            }).get());

                            if (allFields.length > fieldsWithValidValue.length) {
                                $(this).addClass('invalid');
                                errorMessages += '<li id="validation-message-' + 0 + '">' + validationMessage + '</li>';
                            } else {
                                $(this).addClass('valid');
                            }
                            break;

                        case 'required--set':

                            var fields, setOK,
                                setsOK = false,
                                sets = $(this).attr('data-validation-set').split('|');

                            for (var s = 0, sl = sets.length; s < sl; s++) {

                                fields = sets[s].split(',');
                                setOK = true;
                                for (var f = 0, fl = fields.length; f < fl; f++) {
                                    if (!formCheckValid($(this).find('input[name="' + fields[f] + '"]').get(0))) {
                                        setOK = false;
                                        break;
                                    }
                                }
                                if (setOK) {
                                    setsOK = true;
                                    $(this).addClass('valid');
                                    break;
                                }
                            }
                            if (!setsOK) {
                                $(this).addClass('invalid');
                                errorMessages += '<li id="validation-message-' + 0 + '">' + validationMessage + '</li>';
                            }
                            break;
                    }
                }
            });

            if (errorMessages != '') {
                e.preventDefault();
                form.find('input[type="submit"]').before('<div class="validation-message"><ul class="list-bullet">' + errorMessages + '</ul></div>');
            } else {



                // all the validate-groups have been checked - now check the form
                var formValidationType;
                if (formValidationType = form.attr('data-form-validation-type')) {
                    switch (formValidationType) {
                        case 'required--set':
                            var groups, groupOK,
                                setsOK = false,
                                sets = form.removeClass('valid invalid').attr('data-form-validation-set').split('|');

                            for (var s = 0, sl = sets.length; s < sl; s++) {
                                groups = sets[s].split(',');
                                groupOK = true;
                                console.log('checking sets[' + s + '] = ' + sets[s]);
                                for (var f = 0, fl = groups.length; f < fl; f++) {
                                    console.log('checking groups[' + f + '] = ' + groups[f]);
                                    if (!$('[data-validation-id="' + groups[f] + '"]').hasClass('valid')) {
                                        groupOK = false;
                                        break;
                                    }
                                }
                                if (groupOK) {
                                    setsOK = true;
                                    break;
                                }
                            }
                            if (!setsOK) {
                                e.preventDefault();
                                var formErrorMessage = formErrorMessages[form.attr('data-form-validation-message').replace('$message--', '')];
                                form.addClass('invalid').find('input[type="submit"]').before('<div class="validation-message">' + formErrorMessage + '</div>');
                                return false;
                            }

                            break;
                    }
                }
            }


            // MANUAL OVERRIDES HERE

            if (form.attr('id') == 'form__fraud-type') { // redirect to new or old website based on user input<commit_msg>Removed some console.logs<commit_after>(function() {

    var formCheckValid = function(el) {
        if (!$(el).is(':visible')) {return null};

        var inputType = (el.tagName.toLowerCase() == 'textarea') ? 'textarea' : $(el).attr('type');

        switch (inputType) {
            case 'radio':
            case 'checkbox':
                return (el.checked) ? el.name : null;
                break;
            case 'text':
            case 'number':
            case 'email':
            case 'tel':
            case 'textarea':
                var pattern = (pattern = $(el).attr('data-pattern')) ? pattern : $(el).attr('pattern');

                if (pattern != null) {
                    var result = $(el).val().match(new RegExp(pattern,"gi"));
                    return (result!= null && result.length > 0 && result[0] != '') ?el.name : null;
                } else {
                    return ($(el).val() != '') ? el.name : null;
                }
                break;
            default:
                return null;
        }
    };

    $(document).ready(function() {

        var formErrorMessages = [];
        formErrorMessages['fraud-suspect'] = 'Please make sure you enter at least<ol class="list-bullet">' +
                                                '<li>A name, approximate age (or date of birth) and an address</li>' +
                                                '<li>A name, approximate age (or date of birth) and some additional info</li>' +
                                                '<li>A National insurance number and an approximate age (or date of birth)</li>' +
                                                '<li>A National insurance number and an address</li>' +
                                                '</ul>';

        $('a.previousPage').each(function() {
            $(this).on('click', function() {
                if (!confirm('Are you sure you want to go back? You may lose form data.\n\nNB: We need to assess the need and functionality of this link')) {
                    return false;
                }
            });

        });


        /* display-none-js is a class that should only work to hide information when javascript is enabled */
        $('.display-none-js').hide();

        /* generic toggle functionality */
        $('.toggle-section').on('click', function() {
            var sections = $(this).parents('.form-group').attr('data-target');
            var sectionOn = $(this).parents('.form-group').attr('data-target') + '__' + $(this).val().toLowerCase();
            $('.' + sections).hide();
            $('.' + sectionOn).show();
        });


        $('form.crm-check input[type="submit"]').on('click', function(e) {

            //e.preventDefault();

            var form = $(this).parents('form'),
                validationMessage = '',
                errorMessages = '',
                validationType = '',
                inputFields = ['input[type="text"]', 'input[type="number"]', 'input[type="tel"]', 'input[type="email"]', 'input[type="checkbox"]', 'input[type="radio"]', 'select', 'textarea'],
                fieldsWithValidValue,
                allFields;

            form.removeClass('invalid').find('div.validation-message').remove();

            // loop each validation-group

            form.find('.validation-group').removeClass('invalid valid').each(function() {

                if ($(this).is(':visible')) {
                    validationType = $(this).attr('data-validation-type');
                    validationMessage = $(this).attr('data-validation-message');

                    var typesToCheck = ['required--one-or-more', 'required--one', 'required--all', 'not-required--one-or-more', 'not-required--one']
                    if (typesToCheck.indexOf(validationType) != -1) {
                        fieldsWithValidValue = $.unique($(this).find(inputFields.join(',')).map(function () {
                            return formCheckValid(this);
                        }).get());
                    }

                    switch (validationType) {
                        // it's not required, so don't display error messages. But when there is valid data, mark it as such
                        case 'not-required--one-or-more':
                        case 'not-required--one':
                            if (fieldsWithValidValue.length > 0) {
                                $(this).addClass('valid');
                            }
                            break;
                        case 'required--one-or-more':
                        case 'required--one':
                            if (fieldsWithValidValue.length == 0) {
                                $(this).addClass('invalid');
                                errorMessages += '<li id="validation-message-' + 0 + '">' + validationMessage + '</li>';
                            } else {
                                $(this).addClass('valid');
                            }
                            break;

                        case 'required--all':
                            allFields = $.unique($(this).find(inputFields.join(',')).map(function () {
                                return this.name;
                            }).get());

                            if (allFields.length > fieldsWithValidValue.length) {
                                $(this).addClass('invalid');
                                errorMessages += '<li id="validation-message-' + 0 + '">' + validationMessage + '</li>';
                            } else {
                                $(this).addClass('valid');
                            }
                            break;

                        case 'required--set':

                            var fields, setOK,
                                setsOK = false,
                                sets = $(this).attr('data-validation-set').split('|');

                            for (var s = 0, sl = sets.length; s < sl; s++) {

                                fields = sets[s].split(',');
                                setOK = true;
                                for (var f = 0, fl = fields.length; f < fl; f++) {
                                    if (!formCheckValid($(this).find('input[name="' + fields[f] + '"]').get(0))) {
                                        setOK = false;
                                        break;
                                    }
                                }
                                if (setOK) {
                                    setsOK = true;
                                    $(this).addClass('valid');
                                    break;
                                }
                            }
                            if (!setsOK) {
                                $(this).addClass('invalid');
                                errorMessages += '<li id="validation-message-' + 0 + '">' + validationMessage + '</li>';
                            }
                            break;
                    }
                }
            });

            if (errorMessages != '') {
                e.preventDefault();
                form.find('input[type="submit"]').before('<div class="validation-message"><ul class="list-bullet">' + errorMessages + '</ul></div>');
            } else {



                // all the validate-groups have been checked - now check the form
                var formValidationType;
                if (formValidationType = form.attr('data-form-validation-type')) {
                    switch (formValidationType) {
                        case 'required--set':
                            var groups, groupOK,
                                setsOK = false,
                                sets = form.removeClass('valid invalid').attr('data-form-validation-set').split('|');

                            for (var s = 0, sl = sets.length; s < sl; s++) {
                                groups = sets[s].split(',');
                                groupOK = true;

                                for (var f = 0, fl = groups.length; f < fl; f++) {
                                    if (!$('[data-validation-id="' + groups[f] + '"]').hasClass('valid')) {
                                        groupOK = false;
                                        break;
                                    }
                                }
                                if (groupOK) {
                                    setsOK = true;
                                    break;
                                }
                            }
                            if (!setsOK) {
                                e.preventDefault();
                                var formErrorMessage = formErrorMessages[form.attr('data-form-validation-message').replace('$message--', '')];
                                form.addClass('invalid').find('input[type="submit"]').before('<div class="validation-message">' + formErrorMessage + '</div>');
                                return false;
                            }

                            break;
                    }
                }
            }


            // MANUAL OVERRIDES HERE

            if (form.attr('id') == 'form__fraud-type') { // redirect to new or old website based on user input
{'fraction_non_alphanumeric': 0.13619294441212249, 'fraction_numerical': 0.001375152060083567, 'mean_word_length': 1.025929497482053, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 61, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5984976', 'n_tokens_mistral': 4353, 'n_tokens_neox': 4061, 'n_words': 905}