const setPasswordMatchingValidity = () => {
    const passwordFormControl = document.getElementById('PasswordFormControl');
    const confirmPasswordFormControl = document.getElementById('ConfirmPasswordFormControl');
    if (passwordFormControl.value !== confirmPasswordFormControl.value) {
        confirmPasswordFormControl.setCustomValidity('Passwords Must Match');
    }
    else {
        confirmPasswordFormControl.setCustomValidity('');
    };
}

const adjustZipCode = () => {
    let zipValue = document.getElementById('ZipCodeFormControl').value;
    const isSixthValueADash = isADashAtIndex(zipValue, 5);
    if (zipValue.length > 5 && !isSixthValueADash) {
        const newValue = `${zipValue.slice(0, 5)}-${zipValue.slice(4)}`;
        document.getElementById('ZipCodeFormControl').value = newValue;
    }
}

const adjustPhoneNumber = () => {
    let phoneValue = document.getElementById('PhoneFormControl').value;
    const isFourthValueADash = isADashAtIndex(phoneValue, 3);
    const isEigthValueADash = isADashAtIndex(phoneValue, 7);
    if (!isFourthValueADash && phoneValue.length > 4) {
        const newValue = `${phoneValue.slice(0, 3)}-${phoneValue.slice(3)}`;
        document.getElementById('PhoneFormControl').value = newValue;
    }
    else if (!isEigthValueADash && phoneValue.length > 7) {
        const newValue = `${phoneValue.slice(0, 7)}-${phoneValue.slice(7)}`;
        document.getElementById('PhoneFormControl').value = newValue;
    }
}

const isADashAtIndex = (string, index) => {
    return string.split('')[index] === '-'
}

const setValidity = (formControlName) => {
    const control = document.getElementsByName(formControlName)[0];
    control.classList.remove(...control.classList);
    control.classList.add(`form-valid-${control.validity.valid}`);
}
