document.addEventListener("DOMContentLoaded", function () {
    function toggleRequiredField(checkboxId, fieldId) {
        const checkbox = document.getElementById(checkboxId);
        const field = document.getElementById(fieldId);

        if (!checkbox || !field) return;

        function updateRequired() {
            field.required = checkbox.checked;
        }

        checkbox.addEventListener("change", updateRequired);
        updateRequired(); // Apply on page load
    }

    // Apply function to the required fields
    toggleRequiredField("cnc_cf7_enable_registration", "cnc_cf7_user_role");
    toggleRequiredField("cnc_cf7_enable_registration", "cnc_cf7_successurl_field");

});


document.addEventListener('DOMContentLoaded', function () {
    var registrationCheckbox = document.getElementById('cnc_cf7_enable_registration');
    var passwordCheckbox = document.querySelector('input[name="cnc_cf7_enable_password_field"]');
    var skipEmailCheckbox = document.querySelector('input[name="cnc_cf7_skip_email"]');

    function togglePasswordCheckbox() {
        if (registrationCheckbox.checked) {
            passwordCheckbox.removeAttribute('disabled');
        } else {
            passwordCheckbox.setAttribute('disabled', 'disabled');
            passwordCheckbox.checked = false; // Uncheck when disabled
        }
    }

    function toggleSkipEmailCheckbox() {
        if (registrationCheckbox.checked) {
            skipEmailCheckbox.removeAttribute('disabled');
        } else {
            skipEmailCheckbox.setAttribute('disabled', 'disabled');
            skipEmailCheckbox.checked = false; // Uncheck when disabled
        }
    }

    // Run functions on page load to set the correct state
    togglePasswordCheckbox();
    toggleSkipEmailCheckbox();

    // Add event listener to registration checkbox
    registrationCheckbox.addEventListener('change', function () {
        togglePasswordCheckbox();
        toggleSkipEmailCheckbox();
    });
});
