on_click('login_submit_button', _login);
on_click('reset_submit_button', _reset);
on_click('login_reset_link',
         function (e) {
             hide('login');
             show('reset');
         });
on_click('back_to_login_link',
         function (e) {
             hide('reset');
             show('login');
         });

hide('reset');
show('login');

function report_error(error) {
    document.getElementById('login_info').innerHTML = error;
}


function _login(e) {

    var email = value('login_email_input');
    if (email  == '') {
        report_error('Please enter your email address.');
        return;
    }

    var password = value('login_password_input');
    if (password == '') {
        report_error('Please enter your password.');
        return;
    }

    password = sha1_hash(password);

    function done(response) {
        if (!response.status) {
            report_error('I could not log you in.<br><br>' +
                         'Check your email and password and try again.');
        } else {
            if (!response.output) {
                report_error('The site barfed. Sorry. See if a retry helps.');
                return;
            }
            if (response.output.cookie) {
                set_cookie(response.output.cookie.name, response.output.cookie.value);
            }
            if (response.output.page) {
                window.location = response.output.page;
            }
        }
    }

    make_http_request('/api/account', 'login', {'email': email, 'password': password}, done);
}


function _reset(e) {

    var email = value('reset_email_input');
    if (email == '') {
        alert('Please enter your email address.');
        return;
    }

    var password = value('reset_password_input');
    if (password == '') {
        alert('Please enter a password.');
        return;
    }

    var reenter_password = value('reset_reenter_password_input');
    if (reenter_password == '') {
        alert('Please reenter the password.');
        return;
    }

    if (password != reenter_password) {
        alert('Passwords do not match.');
        return;
    }

    password = sha1_hash(password);

    function done(response) {

        if (!response.status) {
            document.getElementById('reset_info').innerHTML = 'Oops! Something went wrong when I tried to reset your password.<br><br>Try again and see if the problem goes away.';
        } else {
            hide('reset');
            show('login');
            document.getElementById('login_info').innerHTML = 'We are almost done! Now check your email to see how to confirm your new password.';
            if (!response.output) {
                alert('Response does not have an output!');
            }
            if (response.output.cookie) {
                set_cookie(response.output.cookie.name, response.output.cookie.value);
            }
            if (response.output.page) {
                window.location = response.output.page;
            }
        }
    }

    // TODO: Rename account to authentication.
    make_http_request('/api/account', 'set_password',
                      {'email': email, 'password': password}, done);

}


