// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
    'use strict';
    window.addEventListener('load', function() {
        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;


    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('placeholder', '0000');
        paper_id.attr('pattern', '\\d{4}');
        paper_id.attr('required', 'required');
        //if (paper_id.attr('oldval').match(/^\\d{4}$/)) {
        //    paper_id.val(paper_id.attr('oldval'));
        //}
    } else {
        paper_id.attr('oldval', paper_id.val());
        paper_id.val('');
        paper_id.attr('disabled', 'disabled');
        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");
    }

}