jQuery(function ($) {
    const ANET_ECHECK_ACCOUNT_TYPE_CHECKING = anetEdit.account_types[0]; // 'checking'.
    const ANET_ECHECK_ACCOUNT_TYPE_SAVINGS = anetEdit.account_types[1];  // 'savings'. 
    // Remove any maxlength attributes that might be limiting input.
    $('#anet-echeck-edit-account-number').removeAttr('maxlength');
    $('#anet-echeck-edit-routing-number').removeAttr('maxlength');
    $('#anet-echeck-edit-name-on-account').removeAttr('maxlength');
    // Real-time input validation - Account Number (only digits).
    $('#anet-echeck-edit-account-number').on('input', function (e) {
        var $this = $(this);
        var value = $this.val();
        // Remove any non-digit characters and limit length.
        var cleanValue = value.replace(/\D/g, ''); // Remove all non-digits.
        if (cleanValue.length > 17) {
            cleanValue = cleanValue.substring(0, 17); // Limit to 17 digits.
        }
        // Update field if value changed.
        if (cleanValue !== value) {
            $this.val(cleanValue);
        }
    });
    // Real-time input validation - Routing Number (only digits, max 9).
    $('#anet-echeck-edit-routing-number').on('input', function (e) {
        var $this = $(this);
        var value = $this.val();
        // Remove any non-digit characters and limit length.
        var cleanValue = value.replace(/\D/g, ''); // Remove all non-digits.
        if (cleanValue.length > 9) {
            cleanValue = cleanValue.substring(0, 9); // Limit to 9 digits.
        }
        // Update field if value changed.
        if (cleanValue !== value) {
            $this.val(cleanValue);
        }
    });
    // Real-time input validation - Name on Account (only letters and spaces, max 22).
    $('#anet-echeck-edit-name-on-account').on('input', function (e) {
        var $this = $(this);
        var value = $this.val();
        // Remove any non-letter/non-space characters and limit length.
        var cleanValue = value.replace(/[^A-Za-z\s]/g, ''); // Remove non-letters and non-spaces.
        if (cleanValue.length > 22) {
            cleanValue = cleanValue.substring(0, 22); // Limit to 22 characters.
        }
        // Update field if value changed.
        if (cleanValue !== value) {
            $this.val(cleanValue);
        }
    });
    // Save button validation.
    $('#anet-echeck-save-btn').on('click', function (e) {
        e.preventDefault();
        var $error = $('#anet-echeck-edit-error');
        $error.hide().text('');
        var accountNumber = String($('#anet-echeck-edit-account-number').val()).trim();
        var routingNumber = String($('#anet-echeck-edit-routing-number').val()).trim();
        var accountType = $('#anet-echeck-edit-account-type').val();
        var nameOnAccount = String($('#anet-echeck-edit-name-on-account').val()).trim();
        const isAccountNumberMasked = accountNumber.startsWith('XXXX');
        const isRoutingNumberMasked = routingNumber.startsWith('XXXX');
        // Validate account number.
        if (accountNumber === '') {
            $error.text('Please enter an account number.').show();
            $('#anet-echeck-edit-account-number').focus();
            return false;
        }
        if (!isAccountNumberMasked) {
            // Check if account number contains only digits.
            if (!/^\d+$/.test(accountNumber)) {
                $error.text('Account number must contain only digits (0-9).').show();
                $('#anet-echeck-edit-account-number').focus();
                return false;
            }
            // Check if account number is within valid length (4-17 digits).
            if (accountNumber.length < 4 || accountNumber.length > 17) {
                $error.text('Account number must be between 4 and 17 digits.').show();
                $('#anet-echeck-edit-account-number').focus();
                return false;
            }
        }
        // Validate routing number.
        if (routingNumber === '') {
            $error.text('Please enter a routing number.').show();
            $('#anet-echeck-edit-routing-number').focus();
            return false;
        }
        if (!isRoutingNumberMasked) {
            // Check if routing number contains only digits.
            if (!/^\d+$/.test(routingNumber)) {
                $error.text('Routing number must contain only digits (0-9).').show();
                $('#anet-echeck-edit-routing-number').focus();
                return false;
            }
            // Check if routing number is exactly 9 digits.
            if (routingNumber.length !== 9) {
                $error.text('Routing number must be exactly 9 digits.').show();
                $('#anet-echeck-edit-routing-number').focus();
                return false;
            }
        }
        // Validate account type.
        if (ANET_ECHECK_ACCOUNT_TYPE_CHECKING !== accountType && ANET_ECHECK_ACCOUNT_TYPE_SAVINGS !== accountType) {
            $error.text('Please select a valid account type.').show();
            $('#anet-echeck-edit-account-type').focus();
            return false;
        }
        // Validate name on account.
        if (nameOnAccount === '') {
            $error.text('Please enter the name on the account.').show();
            $('#anet-echeck-edit-name-on-account').focus();
            return false;
        }
        // Check if name contains only alphabets and spaces.
        if (!/^[A-Za-z\s]+$/.test(nameOnAccount)) {
            $error.text('Name on account must contain only letters and spaces.').show();
            $('#anet-echeck-edit-name-on-account').focus();
            return false;
        }
        // Check if name is within valid length (max 22 characters).
        if (nameOnAccount.length > 22) {
            $error.text('Name on account must not exceed 22 characters.').show();
            $('#anet-echeck-edit-name-on-account').focus();
            return false;
        }
        $('#anet-echeck-edit-form form').submit();
    });
});
