<commit_before>/* jshint ignore:start */
function checkLoginStatus(cb, preventRedirect) {
    $.ajax({
        type: 'POST',
        url: 'ajax/check_login.php'
    })
        .done(function(msg) {
            var data = JSON.parse(msg);
            if (data.success) {
                if (cb) cb();
            } else if(preventRedirect !== true) {
                window.location = 'index.html';
            }
        });
}

function logout() {
    if (confirm('If you have unsaved changes logging out will cause you to lose them. Continue?')) {
        $.ajax({
            type: 'POST',
            url: 'ajax/logout.php'
        })
            .done(function() {
                window.location = 'index.html';
            });
    }
}
/* jshint ignore:end */
(function() {
    'use strict';

    var loginBtn = document.querySelector('#login-btn');
    var usernameField = document.querySelector('#username');
    var passwordField = document.querySelector('#password');

    function onLoginClick() {
        var username = usernameField.value;
        var password = passwordField.value;
        if (validateLogin(username, password)) {
            login(username, $.md5(password));
        }
    }

    function validateLogin(username, password) {
        var msg = '';
        if (!$.trim(username)) {
            msg += 'Username cannot be blank<br />';
        }
        if (!password) {
            msg += 'Password cannot be blank<br />';
        }
        if (msg) {
            $().toastmessage('showErrorToast', msg);
            return false;
        }
        return true;
    }

    function login(username, password) {
        $.ajax({
            type: 'POST',
            url: 'ajax/login.php',
            data: {
                username: username,
                password: password
            }
        })
            .done(function(msg) {
                var data = JSON.parse(msg);
                if (data.success) {
                    window.location = 'attendance.html';
                } else {
                    $().toastmessage('showErrorToast', 'Username or password is incorrect');
                }
            });
    }
    
    function redirectToApp() {
        window.location = 'attendance.html';
    }

    if (loginBtn) {
        loginBtn.addEventListener('click', onLoginClick);
        $(document).keypress(function(e) {
            if (e.which == 13) {
                onLoginClick();
            }
        });
        checkLoginStatus(redirectToApp, true);
    }
})();<commit_msg>Added URL redirect. Issue #56<commit_after>/* jshint ignore:start */
function checkLoginStatus(cb, preventRedirect) {
    $.ajax({
        type: 'POST',
        url: 'ajax/check_login.php'
    })
        .done(function(msg) {
            var data = JSON.parse(msg);
            if (data.success) {
                if (cb) cb();
            } else if(preventRedirect !== true) {
                window.location = 'index.html?url='+encodeURIComponent(window.location);
            }
        });
}

function logout() {
    if (confirm('If you have unsaved changes logging out will cause you to lose them. Continue?')) {
        $.ajax({
            type: 'POST',
            url: 'ajax/logout.php'
        })
            .done(function() {
                window.location = 'index.html';
            });
    }
}
/* jshint ignore:end */
(function() {
    'use strict';

    var loginBtn = document.querySelector('#login-btn');
    var usernameField = document.querySelector('#username');
    var passwordField = document.querySelector('#password');

    function onLoginClick() {
        var username = usernameField.value;
        var password = passwordField.value;
        if (validateLogin(username, password)) {
            login(username, $.md5(password));
        }
    }

    function validateLogin(username, password) {
        var msg = '';
        if (!$.trim(username)) {
            msg += 'Username cannot be blank<br />';
        }
        if (!password) {
            msg += 'Password cannot be blank<br />';
        }
        if (msg) {
            $().toastmessage('showErrorToast', msg);
            return false;
        }
        return true;
    }
    
    function getUrlParameter(sParam)
    {
        var sPageURL = window.location.search.substring(1);
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++) 
        {
            var sParameterName = sURLVariables[i].split('=');
            if (sParameterName[0] == sParam) 
            {
                return decodeURIComponent(sParameterName[1]);
            }
        }
        return null;
    }  

    function login(username, password) {
        $.ajax({
            type: 'POST',
            url: 'ajax/login.php',
            data: {
                username: username,
                password: password
            }
        })
        .done(function(msg) {
            var data = JSON.parse(msg);
            if (data.success) {
                var url = getUrlParameter('url');
                if(url) {
                    window.location = url;
                } else {
                    window.location = 'attendance.html';
                }
            } else {
                $().toastmessage('showErrorToast', 'Username or password is incorrect');
            }
        });
    }
    
    function redirectToApp() {
        var url = getUrlParameter('url');
        if(url)
            window.location = url;
        else
            window.location = 'attendance.html';
    }

    if (loginBtn) {
        loginBtn.addEventListener('click', onLoginClick);
        $(document).keypress(function(e) {
            if (e.which == 13) {
                onLoginClick();
            }
        });
        checkLoginStatus(redirectToApp, true);
    }
})();
{'fraction_non_alphanumeric': 0.13184549356223177, 'fraction_numerical': 0.0020600858369098714, 'mean_word_length': 1.538562091503268, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3057440', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1548, 'n_words': 341}