function logout()
{
    $.get('/logout.php', function(e) {
        /* Make sure to remove get params. */
        var url = location.href.split('?')[0];
        location = url;
    });
}

function submitRegistration(e)
{
    data = $(e).serialize();
    $.post('/register.php', data, function(e)
    {
        $("#register-content")[0].innerHTML = "";
        var elem = document.createElement("div");
        elem.className = "blue centered";
        elem.style['font-size'] = "20pt";
        elem.style.margin = "10px";
        elem.innerHTML = "Registered!";

        $("#register-content").prepend(elem);

        setTimeout(function() { closeOpacityBox('.register-popup'); }, 2000);
    }).fail(function()
    {
        console.error("Registration failed!");
        $("#register-failed").remove();
        var elem = document.createElement("div");
        elem.style.color = "red";
        elem.style['margin-bottom'] = '10px';
        elem.style['margin-top'] = '5px';
        elem.id = "register-failed";
        elem.className = "centered";
        elem.innerHTML = "Registration failed!";

        $("#register-content").prepend(elem);
        $(".register-form")[0].reset();
        grecaptcha.reset()
    });
}

function submitLogin(e)
{
    data = $(e).serialize();
    $.post('/login.php', data, function(e)
    {
        location = location.href.split('?')[0];
    }).fail(function(jqXHR)
    {
        console.error("Login failed!");
        $("#login-failed").remove();
        var elem = document.createElement("div");
        elem.style.color = "red";
        elem.style['margin-bottom'] = '10px';
        elem.style['margin-top'] = '5px';
        elem.id = "login-failed";
        elem.className = "centered";
        if (/inactive/.test(jqXHR.responseText))
        {
            elem.innerHTML = "User inactive!";
        } else {
            elem.innerHTML = "Login failed!";
        }

        $("#register-content").prepend(elem);
        $(".register-form")[0].reset();
    });
}

function loginPopup()
{
    $.get("/login_popup.php", function (data)
    {
        var popup = document.createElement("div");
        popup.className = "register-popup";
        popup.innerHTML = data;
        document.body.appendChild(popup);
        overlay = document.getElementById("overlay");
        window.getComputedStyle(popup).opacity;

        popup.style.opacity = 1;
    });
}

function registerPopup(invite_token = '')
{
    $.get("/register_form.php", {invite_token: invite_token}, function (data)
    {
        var popup = document.createElement("div");
        popup.className = "register-popup";
        popup.innerHTML = data;
        document.body.appendChild(popup);
        window.getComputedStyle(popup).opacity;

        popup.style.opacity = 1;
        grecaptcha.render($(".g-recaptcha")[0]);
    });
}

