var Login = function () {
    var priv =
    {
        Regexes: {
            email: /^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
        },
        Login: function () {
            $('.login-error').html('');
            if (!priv.IsValid()) {
                return;
            }

            $.ajax({
                type: "POST",
                url: "../Handlers/Login.ashx",
                data: "job=1&email=" + Utils.Trim($('.email-box-real').val()) + "&password=" + Utils.Trim($('.password-box-real').val()),
                success: function (data) {
                    if (data == undefined)
                        return;
                    if (data["response"] == 0) {
                        location.reload(true);
                    }
                    else if (data["response"] == -1) {
                        $('.login-error').html(messages.LOGIN_ERROR_MISSING_PARAMETER_REFRESH);
                    }
                    else if (data["response"] == -2) {
                        $('.login-error').html(messages.LOGIN_ERROR_INCOMPLETE_PREDICTION);
                    }
                    else if (data["response"] == -3) {
                        $('.login-error').html(messages.LOGIN_ERROR_INVALID_EMAIL);
                    }
                    else if (data["response"] == -4) {
                        $('.login-error').html(messages.LOGIN_ERROR_INVALID_PASSWORD);
                    }
                    else if (data["response"] == -5) {
                        $('.login-error').html(messages.LOGIN_ERROR_NOT_FOUND_EMAIL);
                    }
                    else if (data["response"] == -6) {
                        $('.login-error').html(messages.LOGIN_ERROR_INCORRECT_PASSWORD);
                    }
                    else if (data["response"] == -7) {
                        $('.login-error').html(messages.LOGIN_ERROR_NOT_ACTIVATED_USER);
                    }
                    else if (data["response"] == -8) {
                        $('.login-error').html(messages.LOGIN_ERROR_UNEXPECTED_ERROR);
                    }
                    else if (data["response"] == -101) {
                        $('.login-error').html(messages.LOGIN_ERROR_ALREADY_PREDICTED_ACCOUNT);
                    }
                    else if (data["response"] == -102) {
                        $('.login-error').html(messages.LOGIN_ERROR_ALREADY_PREDICTED_EMAIL);
                    }
                    else if (data["response"] == -103) {
                        $('.login-error').html(messages.LOGIN_ERROR_UNEXPECTED_ERROR);
                    }
                    else if (data["response"] == -201) {
                        $('.login-error').html(messages.LOGIN_ERROR_LOCKED_GAME);
                    }
                    else if (data["response"] == -202) {
                        $('.login-error').html(messages.LOGIN_ERROR_INVALID_PREDICTION);
                    }
                },
                error: function (data) {
                    return false;
                },
                dataType: "json"
            });

        },
        IsValid: function () {
            var email = Utils.Trim($('.email-box-real').val());
            var password = Utils.Trim($('.password-box-real').val());
            var valid = true;
            if (!Utils.TestRegex(email, priv.Regexes.email)) {
                $('.email-error').html(messages.EMAIL_INVALID);
                $('.email-error').show();
                valid = false;
            }
            if (password == undefined || password == '' || password == messages.PASSWORD_DUMMY_TEXT) {
                $('.password-error').html(messages.PASSWORD_INVALID);
                $('.password-error').show();
                valid = false;
            }
            return valid;
        }
    };
    return {
        Login: function () {
            priv.Login();
        }
    };
} ();
