<commit_before>/**
 * Licensed under AGPL v3
 * (https://github.com/plentymarkets/plenty-cms-library/blob/master/LICENSE)
 * =====================================================================================
 * @copyright   Copyright (c) 2015, plentymarkets GmbH (http://www.plentymarkets.com)
 * @author      <NAME> <<EMAIL>>
 * =====================================================================================
 */

(function( $, pm )
{
    pm.service( 'AddressDoctorService', function( API )
    {
        return {
            validateAddress: validateAddress
        };

        function validateAddress( addressForms )
        {
            var addressIsValid = true;
            addressForms = addressForms || '[data-plenty-address-doctor]';
            $( addressForms ).filter('[data-plenty-address-doctor]:visible').each( function( i, form )
            {
                var addressDoctor  = new AddressDoctor( form );
                var requiredFields = $( form ).attr( 'data-plenty-address-doctor' ).replace( /\s/g, '' ).split( ',' );
                if ( !addressDoctor.isValid( requiredFields ) )
                {
                    addressIsValid = false;
                }

            } );

            return addressIsValid;
        }

        function AddressDoctor( form )
        {
            var $form                = $( form );
            var $inputs              = {
                Street : $form.find( 'input[name="Street"]' ),
                ZIP    : $form.find( 'input[name="ZIP"]' ),
                City   : $form.find( 'input[name="City"]' ),
                HouseNo: $form.find( 'input[name="HouseNo"]' )
            };
            var $suggestionContainer = {};

            var suggestions;
            var requiredFields;

            return {
                isValid: isValid
            };

            function isValid( fields )
            {

                if ( isPackstation() )
                {
                    return true;
                }

                suggestions    = new AddressList( $form.getFormValues() );
                requiredFields = fields;

                refreshView();

                return suggestions.getAddresses().length == 1;
            }

            function refreshView()
            {
                $( '.suggestion-list' ).remove();

                var suggestionListVisible = false;
                for ( var i = 0; i < requiredFields.length; i++ )
                {
                    if ( !validateInput( requiredFields[i], suggestionListVisible ) )
                    {
                        $form.trigger( 'validationFailed' );
                        suggestionListVisible = true;
                    }
                }

                if ( suggestions.houseNoAllowed( $inputs.HouseNo.val() ) )
                {
                    $inputs.HouseNo.removeClass( 'has-error' );
                    $form.find( 'label[for="' + $inputs.HouseNo.attr( 'id' ) + '"]' ).removeClass( 'has-error' );

                    $inputs.HouseNo.addClass( 'has-success' );
                    $form.find( 'label[for="' + $inputs.HouseNo.attr( 'id' ) + '"]' ).addClass( 'has-success' );
                }
                else
                {
                    $inputs.HouseNo.removeClass( 'has-success' );
                    $form.find( 'label[for="' + $inputs.HouseNo.attr( 'id' ) + '"]' ).removeClass( 'has-success' );

                    $inputs.HouseNo.addClass( 'has-error' );<commit_msg>FIX AddressDoctorService validation of house no will be ignored if no alternatives are found<commit_after>/**
 * Licensed under AGPL v3
 * (https://github.com/plentymarkets/plenty-cms-library/blob/master/LICENSE)
 * =====================================================================================
 * @copyright   Copyright (c) 2015, plentymarkets GmbH (http://www.plentymarkets.com)
 * @author      <NAME> <<EMAIL>>
 * =====================================================================================
 */

(function( $, pm )
{
    pm.service( 'AddressDoctorService', function( API )
    {
        return {
            validateAddress: validateAddress
        };

        function validateAddress( addressForms )
        {
            var addressIsValid = true;
            addressForms = addressForms || '[data-plenty-address-doctor]';
            $( addressForms ).filter('[data-plenty-address-doctor]:visible').each( function( i, form )
            {
                var addressDoctor  = new AddressDoctor( form );
                var requiredFields = $( form ).attr( 'data-plenty-address-doctor' ).replace( /\s/g, '' ).split( ',' );
                if ( !addressDoctor.isValid( requiredFields ) )
                {
                    addressIsValid = false;
                }

            } );

            return addressIsValid;
        }

        function AddressDoctor( form )
        {
            var $form                = $( form );
            var $inputs              = {
                Street : $form.find( 'input[name="Street"]' ),
                ZIP    : $form.find( 'input[name="ZIP"]' ),
                City   : $form.find( 'input[name="City"]' ),
                HouseNo: $form.find( 'input[name="HouseNo"]' )
            };
            var $suggestionContainer = {};

            var suggestions;
            var requiredFields;

            return {
                isValid: isValid
            };

            function isValid( fields )
            {

                if ( isPackstation() )
                {
                    return true;
                }

                suggestions    = new AddressList( $form.getFormValues() );
                requiredFields = fields;

                refreshView();

                return suggestions.getAddresses().length == 1;
            }

            function refreshView()
            {
                $( '.suggestion-list' ).remove();

                var suggestionListVisible = false;
                for ( var i = 0; i < requiredFields.length; i++ )
                {
                    if ( !validateInput( requiredFields[i], suggestionListVisible ) )
                    {
                        $form.trigger( 'validationFailed' );
                        suggestionListVisible = true;
                    }
                }

                if ( suggestions.houseNoAllowed( $inputs.HouseNo.val() ) || suggestions.getAddresses().length == 1 )
                {
                    $inputs.HouseNo.removeClass( 'has-error' );
                    $form.find( 'label[for="' + $inputs.HouseNo.attr( 'id' ) + '"]' ).removeClass( 'has-error' );

                    $inputs.HouseNo.addClass( 'has-success' );
                    $form.find( 'label[for="' + $inputs.HouseNo.attr( 'id' ) + '"]' ).addClass( 'has-success' );
                }
                else
                {
                    $inputs.HouseNo.removeClass( 'has-success' );
                    $form.find( 'label[for="' + $inputs.HouseNo.attr( 'id' ) + '"]' ).removeClass( 'has-success' );

                    $inputs.HouseNo.addClass( 'has-error' );
{'fraction_non_alphanumeric': 0.17152513730460497, 'fraction_numerical': 0.0021123785382340513, 'mean_word_length': 1.4557399723374826, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6422119', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1701, 'n_words': 329}