// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
    'use strict';
    window.addEventListener('load', function() {
        const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
        const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
        update_total_due();
        update_paper_id_field();
        // Fetch all the forms we want to apply custom Bootstrap validation styles to
        var forms = document.getElementsByClassName('needs-validation');
        // Loop over them and prevent submission
        var validation = Array.prototype.filter.call(forms, function(form) {
            form.addEventListener('submit', function(event) {
                if (form.checkValidity() === false) {
                    event.preventDefault();
                    event.stopPropagation();
                }
                form.classList.add('was-validated');
            }, false);
        });
    }, false);
})();

function update_total_due() {
    var costs = 0;
    var vegetarian_dinner = $("#vegetarian_dinner");

    if ($('#conference_dinner').is(":checked")) {
        vegetarian_dinner.removeAttr('disabled');
    } else {
        vegetarian_dinner.attr('disabled', 'disabled');
    }

    if ($('#participate').is(":checked")) {
        costs += parseInt($("#conference_fee").text(), 10);
        if ($('#student').is(":checked")) {
            costs -= parseInt($("#student_discount").text(), 10);
        }
        if ($('#conference_dinner').is(":checked")) {
            costs += parseInt($("#conference_dinner_price").text(), 10);
        }
        const lunches = ["lunch_day_1", "lunch_day_2", "lunch_day_3"];
        lunches.forEach(function(lunch) {
            if ($("input[name='" + lunch + "']:checked").val() != "--") {
                costs += parseInt($("#lunch_costs").text().replace(/[^0-9]/g, ''), 10) / 100;
            }
        });
    }
    $("#total_due").val(costs.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }));
}

function update_paper_id_field() {
    var paper_id = $("#paper_id");
    if ($('#author').is(":checked")) {
        paper_id.removeAttr('disabled');
        paper_id.attr('pattern', '\\d{1,4}');
        paper_id.attr('required', 'required');
        val = paper_id.val();
        if (!x.match(/^\d{1,4}$/)) {
            paper_id.val('');
        }
    } else {
        paper_id.attr('disabled', 'disabled');
        paper_id.val('');
        paper_id.removeAttr('placeholder');
        paper_id.removeAttr('pattern');
        paper_id.removeAttr('required');
    }
}

function check_password_match() {
    pass = $("#pw1").val();
    pass2 = $("#pw2").val();

    if (pass2.length > 0) {
        $("#pwconfirm")[0].classList.add('was-validated')
        if (pass != pass2) {
            $("#pw2")[0].setCustomValidity("Passwords do not match");
        } else {
            $("#pw2")[0].setCustomValidity(""); // is valid
        }
    } else {
        $("#pwconfirm")[0].classList.remove('was-validated')
    }
    passUpdated();
}

TOO_SHORT = 'Password too short, still %% characters needed';
TOO_LONG = 'Password too long, please remove %% characters';
INVALID_CHARS = 'Password contains invalid characters';
QUAL_NONE = 'Password is very weak'
QUAL_LOW = 'Password is weak';
QUAL_MEDIUM = 'Password is average'
QUAL_GOOD = 'Password is good';
QUAL_STRONG = 'Password is strong';
REP_OK = 'Repetition ok';
REP_NE = 'Passwords not identical';
PWNED = 'Password found in public password list';

String.prototype.strReverse = function() {
    var newstring = '';
    for (var s = 0; s < this.length; s++)
        newstring = this.charAt(s) + newstring;
    return newstring;
};

function isPasswordPwned(pass, callback) {
    const sha1Hash = CryptoJS.enc.Hex.stringify(CryptoJS.SHA1(pass)).toUpperCase();
    const passwordChunk1 = sha1Hash.substring(0, 5);
    const passwordChunk2 = sha1Hash.substring(5);
    $.get('https://api.pwnedpasswords.com/range/' + passwordChunk1).done(function(data, status) {
        var isPwned = false;
        if (status == "success") {
            if (data && data.length) {
                const chunks = data.split('\r\n');
                const matches = chunks.filter(s => s.includes(passwordChunk2));

                if (matches.length) {
                    isPwned = true
                }
            }
            callback(isPwned)
        }
    });
}

//var checkTimer;

function passUpdated() {
    var nScore = 0;
    var message = '';

    var pass = $('#pw1').val();
    var pass2 = $('#pw2').val();

    //clearTimeout(checkTimer);

    try {
        if (!pass)
            throw '';

        if (pass.match(/[^a-zA-Z0-9!@#$%()_+=:;",.?/-]/))
            throw INVALID_CHARS;

        var nLength = pass.length;
        if (nLength < 8)
            throw TOO_SHORT.replace('%%', 8 - nLength);
        if (nLength > 20)
            throw TOO_LONG.replace('%%', nLength - 20);

        nScore = 4 * nLength;

        // check for upper-/lowercase, numeric and special chars pattern matches
        var nAlphaUC = 0,
            nAlphaLC = 0,
            nNumber = 0,
            nSpecial = 0;
        var nMidChar = 0,
            nRepChar = 0,
            nRepInc = 0;
        var nConsecAlphaUC = 0,
            nConsecAlphaLC = 0,
            nConsecNumber = 0;
        var nTmpAlphaUC = '',
            nTmpAlphaLC = '',
            nTmpNumber = '';
        for (var i = 0; i < nLength; i++) {
            if (pass[i].match(/[A-Z]/g)) { // uppercase characters
                if (nTmpAlphaUC !== '' && (nTmpAlphaUC + 1) == i) {
                    nConsecAlphaUC++;
                }
                nTmpAlphaUC = i;
                nAlphaUC++;
            } else if (pass[i].match(/[a-z]/g)) { // lowercase characters
                if (nTmpAlphaLC !== '' && (nTmpAlphaLC + 1) == i) {
                    nConsecAlphaLC++;
                }
                nTmpAlphaLC = i;
                nAlphaLC++;
            } else if (pass[i].match(/[0-9]/g)) { // numbers
                if (i > 0 && i < (nLength - 1)) {
                    nMidChar++;
                }
                if (nTmpNumber !== '' && (nTmpNumber + 1) == i) {
                    nConsecNumber++;
                }
                nTmpNumber = i;
                nNumber++;
            } else { // special characters
                if (i > 0 && i < (nLength - 1)) {
                    nMidChar++;
                }
                nSpecial++;
            }

            // check for repeated characters
            var bCharExists = false;
            for (var j = 0; j < nLength; j++) {
                if (pass[i] == pass[j] && i != j) {
                    bCharExists = true;
                    nRepInc += Math.abs(nLength / (j - i));
                }
            }
            if (bCharExists) {
                nRepChar++;
                var nUnqChar = nLength - nRepChar;
                nRepInc = (nUnqChar) ? Math.ceil(nRepInc / nUnqChar) : Math.ceil(nRepInc);
            }
        }

        // check for sequential alpha string patterns (forward and reverse)
        var sAlphas = "abcdefghijklmnopqrstuvwxyz";
        var nSeqAlpha = 0;
        for (var i = 0; i < 23; i++) {
            var sFwd = sAlphas.substring(i, i + 3);
            var sRev = sFwd.strReverse();
            if (pass.toLowerCase().indexOf(sFwd) != -1 ||
                pass.toLowerCase().indexOf(sRev) != -1)
                nSeqAlpha++;
        }

        // check for sequential numeric string patterns (forward and reverse)
        var sNumerics = "01234567890";
        var nSeqNumber = 0;
        for (var i = 0; i < 8; i++) {
            var sFwd = sNumerics.substring(i, i + 3);
            var sRev = sFwd.strReverse();
            if (pass.toLowerCase().indexOf(sFwd) != -1 ||
                pass.toLowerCase().indexOf(sRev) != -1)
                nSeqNumber++;
        }

        // general point assignment
        if (nAlphaUC > 0 && nAlphaUC < nLength) // uppercase characters
            nScore += 2 * (nLength - nAlphaUC);
        if (nAlphaLC > 0 && nAlphaLC < nLength) // lowercase characters
            nScore += 2 * (nLength - nAlphaLC);
        if (nNumber > 0 && nNumber < nLength) // numbers
            nScore += 2 * nNumber;
        if (nSpecial > 0) // special characters
            nScore += 4 * nSpecial;
        if (nMidChar > 0) // mid numbers/special characters
            nScore += 2 * nMidChar;

        // point deductions for poor practices
        if ((nAlphaLC > 0 || nAlphaUC > 0) &&
            nSpecial === 0 && nNumber === 0) // characters only
            nScore -= nLength;
        if (nAlphaLC === 0 && nAlphaUC === 0 &&
            nSpecial === 0 && nNumber > 0) // numbers only
            nScore -= nLength;
        if (nRepChar > 0) // same character exists more than once
            nScore -= nRepInc;
        if (nConsecAlphaUC > 0) // consecutive uppercase letters exist
            nScore -= 2 * nConsecAlphaUC;
        if (nConsecAlphaLC > 0) // consecutive lowercase letters exist
            nScore -= 2 * nConsecAlphaLC;
        if (nConsecNumber > 0) // consecutive numbers exist
            nScore -= 2 * nConsecNumber;
        if (nSeqAlpha > 0) // sequential alpha strings exist (3 chars or more)
            nScore -= 3 * nSeqAlpha;
        if (nSeqNumber > 0) // sequential numeric strings exist (3 chars or more)
            nScore -= 3 * nSeqNumber;

        // determine if mandatory requirements have been met
        var arrChars = [nAlphaUC, nAlphaLC, nNumber, nSpecial];
        var nReqChar = 0;
        for (var i = 0; i < arrChars.length; i++) {
            if (arrChars[i]) {
                nReqChar++;
            }
        }
        if (nReqChar >= arrChars.length)
            nScore += 2 * nReqChar;
        else if (nReqChar < arrChars.length - 1)
            nScore -= 2 * nReqChar;

        // limit points to 3..100
        nScore = Math.max(3, Math.min(nScore, 100));

        // set message according to points
        if (nScore >= 80)
            message = QUAL_STRONG;
        else if (nScore >= 60)
            message = QUAL_GOOD;
        else if (nScore >= 40)
            message = QUAL_MEDIUM;
        else if (nScore >= 10)
            message = QUAL_LOW;
        else
            message = QUAL_NONE;
    } catch (error) {
        nScore = 3;
        message = error;
    }
    /*
                if (pass.length > 0 && pass2.length > 0) {
                    message+=' / ';
                    message+=(pass == pass2) ? REP_OK : REP_NE;
                }
    */
    $('#pwqinfo').val(message);

    var progress = $('#pwqbar');
    progress.width(nScore + '%');
    progress.attr('aria-valuenow', nScore);
    if (nScore >= 60)
        progress.removeClass('bg-danger bg-warning').addClass('bg-success');
    else if (nScore >= 40)
        progress.removeClass('bg-danger bg-success').addClass('bg-warning');
    else
        progress.removeClass('bg-warning bg-success').addClass('bg-danger');

    if (nScore >= 40) {
        $("#password-div")[0].classList.add('was-validated');
        $('#btn_change').prop('disabled', pass !== pass2);
        $("#pw1")[0].setCustomValidity("");
        isPasswordPwned(pass, function(isPwned) {
            if (isPwned) {
                $('#pwqinfo').val(PWNED);
                progress.removeClass().addClass('low');
                progress.val(3);
                $("#pw1")[0].setCustomValidity("Password is pwned");
            }
        });
    } else {
        $("#password-div")[0].classList.add('was-validated');
        $('#btn_change').prop('disabled', 1);
        $("#pw1")[0].setCustomValidity("Password is to week");
    }

}