jQuery(function ($) {
    // Validate Routing Number  
    $(document).on('blur', '#anet-echeck-routing-number', function () {
        const value = $(this).val();
        const isValid = /^\d{9}$/.test(value);
        toggleValidation('#anet-bank-routing-number-div', isValid);
    });
    // Validate Account Number
    $(document).on('blur', '#anet-echeck-account-number', function () {
        const value = $(this).val();
        const isValid = /^\d{5,17}$/.test(value);
        toggleValidation('#anet-bank-account-number-div', isValid);
    });
    // Validate Account Name
    $(document).on('blur', '#anet-echeck-account-name', function () {
        const value = $(this).val().replace(/^\s+|\s+$/g, '');
        const isValid = /^[a-zA-Z\s]{4,22}$/.test(value);
        // Only apply validation styling on blur, not during typing
        if (isValid) {
            $('#anet-bank-account-name-div').removeClass('woocommerce-invalid woocommerce-invalid-required-field force-validation');
            $('#anet-bank-account-name-div').addClass('woocommerce-validated');
        } else {
            $('#anet-bank-account-name-div').removeClass('woocommerce-validated');
            // Show invalid styling for empty fields or fields that don't meet requirements
            $('#anet-bank-account-name-div').addClass('woocommerce-invalid woocommerce-invalid-required-field force-validation');
        }
    });
    // Validate Account Type   
    $(document).on('blur change', '#anet-echeck-bank-account-type', function () {
        const value = $(this).val();
        const isValid = value !== '';
        toggleValidation('#anet-bank-account-type-div', isValid);
    });
    // Utility function to toggle validation classes   
    function toggleValidation(selector, isValid) {
        const $field = $(selector);
        if (isValid) {
            $field.removeClass('woocommerce-invalid woocommerce-invalid-required-field');
            $field.addClass('woocommerce-validated');
        } else {
            $field.removeClass('woocommerce-validated');
            $field.addClass('woocommerce-invalid woocommerce-invalid-required-field');
        }
    }
});
// Enhanced input handling for routing number - only numeric, exactly 9 digits
jQuery(document).ready(function($) {
    $(document).on('input keypress paste', '#anet-echeck-routing-number', function(e) {
        if (e.type === 'keypress') {
            // Prevent non-numeric input
            const char = String.fromCharCode(e.which);
            if (!/[0-9]/.test(char) && e.which !== 8 && e.which !== 0) {
                e.preventDefault();
            }
            // Limit to 9 characters
            if (this.value.length >= 9 && e.which !== 8) {
                e.preventDefault();
            }
        } else {
            // Filter and limit input
            this.value = this.value.replace(/[^0-9]/g, '').slice(0, 9);
        }
    });
});
// Enhanced input handling for account number - only numeric, 6-17 digits
jQuery(document).ready(function($) {
    $(document).on('input keypress paste', '#anet-echeck-account-number', function(e) {
        if (e.type === 'keypress') {
            // Prevent non-numeric input
            const char = String.fromCharCode(e.which);
            if (!/[0-9]/.test(char) && e.which !== 8 && e.which !== 0) {
                e.preventDefault();
            }
            // Limit to 17 characters
            if (this.value.length >= 17 && e.which !== 8) {
                e.preventDefault();
            }
        } else {
            // Filter and limit input
            this.value = this.value.replace(/[^0-9]/g, '').slice(0, 17);
        }
    });
});
// Enhanced input handling for account name - only alphabetic and spaces, 4-22 characters
jQuery(document).ready(function($) {
    var accountNameTouched = false;
    
    // Remove validation styling when user focuses on account name field
    $(document).on('focus', '#anet-echeck-account-name', function() {
        $('#anet-bank-account-name-div').removeClass('woocommerce-invalid woocommerce-invalid-required-field force-validation');
        accountNameTouched = true;
    });
    
    $(document).on('input keypress paste', '#anet-echeck-account-name', function(e) {
        accountNameTouched = true;
        if (e.type === 'keypress') {
            // Allow alphabetic characters and spaces
            const char = String.fromCharCode(e.which);
            if (!/[a-zA-Z\s]/.test(char) && e.which !== 8 && e.which !== 0) {
                e.preventDefault();
            }
            // Limit to 22 characters
            if (this.value.length >= 22 && e.which !== 8) {
                e.preventDefault();
            }
        } else {
            // Filter and limit input
            this.value = this.value.replace(/[^a-zA-Z\s]/g, '').slice(0, 22);
            // Remove validation styling during typing
            $('#anet-bank-account-name-div').removeClass('woocommerce-invalid woocommerce-invalid-required-field force-validation');
        }
    });
});
// Show the card number input field when selected the eCheck radio button
jQuery(function ($) {
    function toggleEcheckFields() {
        const selectedToken = $('input[name="wc-anet_echeck-payment-token"]:checked').val();
        const isNewPayment = $('#wc-anet_echeck-payment-token-new').is(':checked');
        if (isNewPayment || !selectedToken) {
            $('.woocommerce-billing-fields__field-wrapper_echeck').show();
        } else {
            $('.woocommerce-billing-fields__field-wrapper_echeck').hide();
        }
    }
    // If no radio button is selected, select the "new payment method" by default
    if ($('input[name="wc-anet_echeck-payment-token"]:checked').length === 0) {
        $('#wc-anet_echeck-payment-token-new').prop('checked', true);
    }
    // Initial check on page load
    toggleEcheckFields();
    // Bind change event to all eCheck token radio buttons
    $(document).on('change', 'input[name="wc-anet_echeck-payment-token"]', function () {
        toggleEcheckFields();
    });
});
