if (document.querySelectorAll('.password-visibility-toggler') != null) {
    var elem = document.querySelectorAll('.password-visibility-toggler');
    for (i = 0; i < elem.length; i++) {
        elem[i].addEventListener('click', function () {
            var hide = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\" fill=\"currentColor\" viewBox=\"0 0 16 16\">\
							<path d=\"M16 8s-3-5.5-8-5.5S0 8 0 8s3 5.5 8 5.5S16 8 16 8zM1.173 8a13.133 13.133 0 0 1 1.66-2.043C4.12 4.668 5.88 3.5 8 3.5c2.12 0 3.879 1.168 5.168 2.457A13.133 13.133 0 0 1 14.828 8c-.058.087-.122.183-.195.288-.335.48-.83 1.12-1.465 1.755C11.879 11.332 10.119 12.5 8 12.5c-2.12 0-3.879-1.168-5.168-2.457A13.134 13.134 0 0 1 1.172 8z\"/>\
							<path d=\"M8 5.5a2.5 2.5 0 1 0 0 5 2.5 2.5 0 0 0 0-5zM4.5 8a3.5 3.5 0 1 1 7 0 3.5 3.5 0 0 1-7 0z\"/>\
						</svg>";
            var show = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\" fill=\"currentColor\" viewBox=\"0 0 16 16\">\
							<path d=\"M13.359 11.238C15.06 9.72 16 8 16 8s-3-5.5-8-5.5a7.028 7.028 0 0 0-2.79.588l.77.771A5.944 5.944 0 0 1 8 3.5c2.12 0 3.879 1.168 5.168 2.457A13.134 13.134 0 0 1 14.828 8c-.058.087-.122.183-.195.288-.335.48-.83 1.12-1.465 1.755-.165.165-.337.328-.517.486l.708.709z\"/>\
							<path d=\"M11.297 9.176a3.5 3.5 0 0 0-4.474-4.474l.823.823a2.5 2.5 0 0 1 2.829 2.829l.822.822zm-2.943 1.299.822.822a3.5 3.5 0 0 1-4.474-4.474l.823.823a2.5 2.5 0 0 0 2.829 2.829z\"/>\
							<path d=\"M3.35 5.47c-.18.16-.353.322-.518.487A13.134 13.134 0 0 0 1.172 8l.195.288c.335.48.83 1.12 1.465 1.755C4.121 11.332 5.881 12.5 8 12.5c.716 0 1.39-.133 2.02-.36l.77.772A7.029 7.029 0 0 1 8 13.5C3 13.5 0 8 0 8s.939-1.721 2.641-3.238l.708.709zm10.296 8.884-12-12 .708-.708 12 12-.708.708z\"/>\
						</svg>";
            var password = this.getAttribute("data-password");
            var e = document.getElementById(password);
            if (e.type === "password") {
                e.type = "text";
                this.innerHTML = show;
            } else {
                e.type = "password";
                this.innerHTML = hide;
            }
        });
    }
}

if (document.querySelectorAll('a[data-lang]') != null) {
    document.querySelectorAll('a[data-lang]').forEach(function (item) {
        item.addEventListener('click', (e) => {
            var code = e.currentTarget.getAttribute("data-lang");
            var formData = new FormData();
            formData.append("language", code);
            request('/auth/change-lang', formData, function (data) {
                try {
                    data = JSON.parse(data);
                    if ((data instanceof Object)) {
                        if (data['status']) {
                            location.reload();
                        }
                    }
                } catch (e) {
                    console.log("An unexpected error occurred");
                }
            });
        });
    });
}

if (document.querySelectorAll('input[data-check="password"]') != null) {
    document.querySelectorAll('input[data-check="password"]').forEach(function (item) {
        item.addEventListener('keyup', (e) => {
            var input_id = e.currentTarget.getAttribute('id');
            validatePassword(input_id);
        });
    });
}

if (document.querySelectorAll('input[data-check="compare"]') != null) {
    document.querySelectorAll('input[data-check="compare"]').forEach(function (item) {
        item.addEventListener('keyup', (e) => {
            var input_id = e.currentTarget.getAttribute('id');
            var target_id = e.currentTarget.getAttribute('data-target');
            compareInputs(input_id, target_id);
        });
        item.addEventListener('focus', (e) => {
            var input_id = e.currentTarget.getAttribute('id');
            var target_id = e.currentTarget.getAttribute('data-target');
            compareInputs(input_id, target_id);
        });
    });
}

/*----------------------------
    XMLHttpRequest POST Function
------------------------------ */
function request(url, data, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('POST', url, true);
    xhr.addEventListener('readystatechange', function () {
        if (xhr.readyState === 4) {
            if (callback) {
                callback(xhr.response);
            }
        }
    });

    var formdata = data ? (data instanceof FormData ? data : new FormData(document.querySelector(data))) : new FormData();

    var checkboxes = document.querySelectorAll('input[type=checkbox]');
    for (i = 0; i < checkboxes.length; i++) {
        var checkbox = checkboxes[i];
        if (!checkbox.checked) {
            formdata.append(checkbox.getAttribute("name"), 0);
        } else {
            formdata.append(checkbox.getAttribute("name"), 1);
        }
    }

    var csrfMetaTag = document.querySelector('meta[name="csrf_token"]');
    if (csrfMetaTag) {
        formdata.append('csrf_token', csrfMetaTag.getAttribute('content'));
    }

    xhr.send(formdata);
}

function compareInputs(element, target) {
    if (document.getElementById(element).value != document.getElementById(target).value || document.getElementById(element).value.trim() == '') {
        document.getElementById(element).classList.add('has-error');
    } else {
        document.getElementById(element).classList.remove('has-error');
    }
}

function validatePassword(input_id) {
    var error_list = "Parolanız aşağıdaki kriterleri içermelidir:";
    var pass = document.getElementById(input_id).value;
    var error = 0;

    // Validate lowercase letters
    var lowerCaseLetters = /[a-z]/g;
    if (!pass.match(lowerCaseLetters)) {
        error_list += "<span>En az 1 <strong>küçük harf</strong></span>";
        error = true;
    }

    // Validate capital letters
    var upperCaseLetters = /[A-Z]/g;
    if (!pass.match(upperCaseLetters)) {
        error_list += "<span>En az 1 <strong>büyük harf</strong></span>";
        error = true;
    }

    // Validate numbers
    var numbers = /[0-9]/g;
    if (!pass.match(numbers)) {
        error_list += "<span>En az 1 <strong>rakam</strong></span>";
        error = true;
    }

    // Validate special characters
    var specialCharacters = /[\~?!@#\$%\^&\.,_\-\+*]/g;
    if (!pass.match(specialCharacters)) {
        error_list += "<span>En az 1 <strong>özel karakter</strong></span>";
        error = true;
    }

    // Validate length
    if (pass.length < 8) {
        error_list += "<span>En az 8 <strong>karakter</strong></span>";
        error = true;
    }

    if (error) {
        if (document.getElementById(input_id + "-error-container") != null) {
            document.getElementById(input_id + "-error-container").innerHTML = error_list;
        } else {
            var error_container = document.createElement("div");
            error_container.id = input_id + "-error-container";
            error_container.className = "error-list";
            error_container.innerHTML = error_list;
            var input_element = document.getElementById(input_id);
            input_element.parentNode.insertBefore(error_container, input_element.nextSibling);
        }
        document.getElementById(input_id).classList.add('has-error');
    } else {
        if (document.getElementById(input_id + "-error-container") != null) {
            document.getElementById(input_id + "-error-container").remove();
        }
        document.getElementById(input_id).classList.remove('has-error');
    }
}