// VALIDATE THE PASSWORD STRENGTH AND PASSWORD CONFIRMATION
function validatePasswords() {
    const password1Input = document.querySelector('input[name="password1"]');
    const password2Input = document.querySelector('input[name="password2"]');
    const passwordMatchingError = document.getElementById('passwordError');

    const password1 = password1Input.value;
    const password2 = password2Input.value;

    const isLengthValid = password1.length >= 8;

    passwordMatchingError.textContent = '';
    passwordMatchingError.style.display = 'none';

    if (password1 && password2) {
        if (password1 === password2) {
            let errorMessage = '';

            if (!isLengthValid) {
                errorMessage += '- At least 8 characters\n';
                errorMessage += '<br>'
            }

            if (errorMessage) {
                passwordMatchingError.innerHTML = errorMessage;
                passwordMatchingError.style.display = 'block';
            }
        } else {
            // Passwords do not match
            passwordMatchingError.textContent = 'Passwords do not match';
            passwordMatchingError.style.display = 'block';
        }
    }
}

document.addEventListener("DOMContentLoaded", function () {
    const password1Input = document.querySelector('input[name="password1"]');
    const password2Input = document.querySelector('input[name="password2"]');

    password1Input.addEventListener('input', validatePasswords);
    password2Input.addEventListener('input', validatePasswords);

    const forgotPasswordForm = document.getElementById("forgotPasswordConfirmationForm");

    forgotPasswordForm.addEventListener("submit", function (event) {
        const passwordMatchingError = document.getElementById('passwordError');
        const passwordValid = passwordMatchingError.textContent === "";

        if (!passwordValid) {
            event.preventDefault();
        }
    });
});