
var scr = {
    'errmsg': {
        'USER_NOT_EXISTS': 'E-Mail or Password is incorrect.',
        'AUTH_FAILED': 'E-Mail or Password is incorrect.',
        'LOGGED_IN': 'A logged in user cannot sign-up.',
        'PASSWORD': 'You must enter a password(more than 4 chars).',
        'PASSWORD_KEY': 'You must enter a password reset key.',
        'EMAIL': 'You must enter a E-Mail.',
        'EMAIL_EXISTS': 'Your E-Mail is already registered.'
    }

    , '_request': function(method, params, cb) {
        var self = this;

        $.post('/api/json/' + method, params, function (res, textStatus) {
            if (textStatus == 'success') {
                if (res.success) {
                    cb(res);
                    return;
                }

                if (res && res.error) {
                    var msg = self.errmsg[res.error] || res.error;
                    alert(msg);
                }
            }
            else {
                alert('Internal client error.');
            }
        }, 'json');
    }

    , 'update_profile': function(p) {
        this._request('update_profile', p, function() {
            window.alert('Your profile has been updated.');
        });
    }

    , 'reset_password': function(p) {
        this._request('reset_password', p, function() {
            window.alert('An email was sent to you.');
        });
    }

    , 'set_password': function(p) {
        this._request('set_password', p, function() {
            window.alert('Your password has been updated.');
            location.href = '/login';
        });
    }

    , 'login': function(p) {
        this._request('login', p, function() {
            location.reload();
        });
    }

    , 'logout': function() {
        this._request('logout', null, function() {
            location.href = '/';
        });
    }
}

