/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */


$(window).on("message", function (e) {
    var data = e.originalEvent.data;
    var parts = data.split("&");
    var u = "";
    var p = "";

    for (var i = 0; i < parts.length; i++) {
        if (parts[i].startsWith("user:")) {
            u = parts[i].replace("user:", "");
        } else if (parts[i].startsWith("password:")) {
            p = parts[i].replace("password:", "");
        }
    }

    if (u != "" && p != "") {
        setStorage("username", u);
        setStorage("password", p);
        // Reset last change so we won't overwrite server settings
        setStorage("lastchange", 0, true);
        app.toast.show({
            text: "You are now logged in!",
            position: "bottom",
            destroyOnClose: true,
            closeTimeout: 1000 * 3
        });
        restartApplication();
    } else {
        app.dialog.alert("There was a problem. Try again later.", "Error");
    }
});