define([
    '/common/login.js',
    '/common/cryptpad-common.js',
    '/common/cryptget.js',
    '/common/credential.js',
    '/bower_components/jquery/dist/jquery.min.js',
], function (Login, Cryptpad, Crypt) {
    var $ = window.jQuery;

    var APP = window.APP = {
        Login: Login,
    };

    var Messages = Cryptpad.Messages;

    $(function () {
        var $main = $('#mainBlock');

        // Language selector
        var $sel = $('#language-selector');
        Cryptpad.createLanguageSelector(undefined, $sel);
        $sel.find('button').addClass('btn').addClass('btn-secondary');
        $sel.show();

        $(window).click(function () {
            $('.cryptpad-dropdown').hide();
        });

        // main block is hidden in case javascript is disabled
        $main.removeClass('hidden');

        // Make sure we don't display non-translated content (empty button)
        $main.find('#data').removeClass('hidden');

        if (Cryptpad.isLoggedIn()) {
            // already logged in, redirect to drive
            document.location.href = '/drive/';
            return;
        } else {
            $main.find('#userForm').removeClass('hidden');
        }

        // text and password input fields
        var $uname = $('#username');
        var $passwd = $('#password');
        var $confirm = $('#password-confirm');

        if (sessionStorage.login_user) {
            $uname.val(sessionStorage.login_user);
        }
        if (sessionStorage.login_pass) {
            $passwd.val(sessionStorage.login_pass);
        }

        [ $uname, $passwd, $confirm]
        .some(function ($el) { if (!$el.val()) { $el.focus(); return true; } });

        // checkboxes
        var $checkImport = $('#import-recent');
        var $checkAcceptTerms = $('#accept-terms');

        var $register = $('button#register');

        var logMeIn = function (result) {
            localStorage.User_hash = result.userHash;

            var proxy = result.proxy;
            proxy.edPublic = result.edPublic;
            proxy.edPrivate = result.edPrivate;

            Cryptpad.whenRealtimeSyncs(result.realtime, function () {
                Cryptpad.login(result.userHash, result.userName, function () {
                    if (sessionStorage.redirectTo) {
                        var h = sessionStorage.redirectTo;
                        var parser = document.createElement('a');
                        parser.href = h;
                        if (parser.origin === window.location.origin) {
                            delete sessionStorage.redirectTo;
                            window.location.href = h;
                            return;
                        }
                    }
                    window.location.href = '/drive/';
                });
            });
        };

        $register.click(function () {
            var uname = $uname.val();
            var passwd = $passwd.val();
            var confirmPassword = $confirm.val();

            var shouldImport = $checkImport[0].checked;
            var doesAccept = $checkAcceptTerms[0].checked;

            /* basic validation */
            if (passwd !== confirmPassword) { // do their passwords match?
                return void Cryptpad.alert(Messages.register_passwordsDontMatch);
            }

            if (!doesAccept) { // do they accept the terms of service?
                return void Cryptpad.alert(Messages.register_mustAcceptTerms);
            }

            Cryptpad.confirm("<h2 class='bright'>" + Messages.register_warning + "</h2>",
            function (yes) {
                if (!yes) { return; }

                Cryptpad.addLoadingScreen(Messages.login_hashing);
                Login.loginOrRegister(uname, passwd, true, function (err, result) {
                    if (err) {
                        switch (err) {
                            case 'NO_SUCH_USER':
                                Cryptpad.removeLoadingScreen(function () {
                                    Cryptpad.alert(Messages.login_noSuchUser);
                                });
                                break;
                            case 'INVAL_USER':
                                Cryptpad.removeLoadingScreen(function () {
                                    Cryptpad.alert(Messages.login_invalUser);
                                });
                                break;
                            case 'INVAL_PASS':
                                Cryptpad.removeLoadingScreen(function () {
                                    Cryptpad.alert(Messages.login_invalPass);
                                });
                                break;
                            case 'ALREADY_REGISTERED':
                                Cryptpad.removeLoadingScreen(function () {
                                    Cryptpad.confirm(Messages.register_alreadyRegistered, function (yes) {
                                        if (!yes) { return; }
                                        result.proxy.login_name = uname;

                                        if (!result.proxy[Cryptpad.displayNameKey]) {
                                            result.proxy[Cryptpad.displayNameKey] = uname;
                                        }
                                        Cryptpad.eraseTempSessionValues();
                                        logMeIn(result);
                                    });
                                });
                                break;
                            default: // UNHANDLED ERROR
                                Cryptpad.errorLoadingScreen(Messages.login_unhandledError);
                        }
                        return;
                    }
                    var proxy = result.proxy;

                    Cryptpad.eraseTempSessionValues();
                    if (shouldImport) {
                        sessionStorage.migrateAnonDrive = 1;
                    }

                    proxy.login_name = uname;
                    proxy[Cryptpad.displayNameKey] = uname;
                    sessionStorage.createReadme = 1;

                    logMeIn(result);
                });
            }, {
                ok: Messages.register_writtenPassword, //'I have written down my password, proceed',
                cancel: Messages.register_cancel, // 'Go back',
                cancelClass: 'safe',
                okClass: 'danger',
                reverseOrder: true,
            }, true, function ($dialog) {
                $dialog.find('> div').addClass('half');
            });
        });
    });
});

@@ -3770,32 +3770,79 @@
 (err, result) %7B%0A
+                    var proxy = result.proxy;%0A%0A
                 
@@ -5058,31 +5058,24 @@
             
-result.
 proxy.login_
@@ -5134,23 +5134,16 @@
    if (!
-result.
 proxy%5BCr
@@ -5212,23 +5212,16 @@
         
-result.
 proxy%5BCr
@@ -5773,55 +5773,8 @@
   %7D%0A
-                    var proxy = result.proxy;%0A%0A
     
@@ -6240,54 +6240,8 @@
 ord,
- //'I have written down my password, proceed',
 %0A   
@@ -6290,22 +6290,8 @@
 cel,
- // 'Go back',
 %0A   

clean up registration code a little bit