var cWindow_Popup_UserRegister = {

    processButtonClick: function(button_id)
    {
        if (button_id != 'send') {
            this.parent_prcoessbuttonclick = cWindow_Popup.processButtonClick;
            return this.parent_prcoessbuttonclick(button_id);
        }


        try {
            var r = Request.create();
            if (r !== null) {
                r.send(URL_AJAX_HANDLER, {'rm': 'user_register_process',
                                          'user_email': $(_(this.name + '_user_email')).val(),
                                          'user_login': $(_(this.name + '_user_login')).val(),
                                          'user_password': $(_(this.name + '_user_password')).val(),
                                          'user_password2': $(_(this.name + '_user_password2')).val(),
                                          'captcha': $(_(this.name + '_captcha')).val()}, this);
                $(this.buttons['send']).hide();
                this.showProcessingAnimation();
                delete(r);
            }
        }
        catch (e) {
            this.handleJSException(e);
        }

    },

    checkField: function(field, params)
    {
        try {
            var r = Request.create();
            if (r !== null) {
                r.send(URL_AJAX_HANDLER, {'rm': 'user_register_check_field', 'field': field, 'params': params}, this);
                delete(r);
            }
        }
        catch (e) {
            this.handleJSException(e);
        }
    },

    requestCallback: function(result, request_params, exception)
    {
        if (request_params.rm == 'user_register_check_field') {
            if (!result.successful) {
                this.showError(request_params.field, result.error);
            } else {
                this.showNotice(request_params.field, 'POSITIVE');
            }
        } else if (request_params.rm == 'user_register_process') {

            if (!result.successful) {
                for (var i in result.error) {
                    this.showError(i, result.error[i]);
                }

                $(_(this.name + '_captcha')).val('');
                this.showNotice('captcha', 'MESSAGES');

                this.reloadCaptcha();
                this.hideProcessingAnimation();
                $(this.buttons['send']).show();
            } else {

                this.hideProcessingAnimation();
                this.dom_register_form.hide();
                this.dom_result_register.show();
            }

        } else {
            this.m = cWindow.requestCallback;
            return this.m(result, request_params);
        }
    },

    showNotice: function(field, mode)
    {
        var notice = $(_(this.name + '_' + field + '_notice'));
        if (mode == 'MESSAGES') {
            notice.removeClass('error')
            .removeClass('positive')
            .addClass('info')
            .html(this.messages[field]);
        } else if (mode == 'POSITIVE') {
            notice.removeClass('error')
            .removeClass('rule')
            .addClass('positive')
            .html(this.positive[field]);
        }
    },

    showError: function(field, errorCode)
    {
        $(_(this.name + '_' + field + '_notice')).removeClass('positive')
            .removeClass('info')
            .addClass('error')
            .html(this.errors[errorCode]);
    },

    initOpen: function()
    {
        this.reloadCaptcha();
        $(this.buttons['send']).show();

        for (var i in this.testable) {
            $(_(this.name + '_' + i)).val('');
            this.showNotice(i, 'MESSAGES');
        }

        this.dom_register_form.show();
        this.dom_result_register.hide();
    },

    open: function()
    {
        this.initOpen();

        this.m = cWindow_Popup.open;
        this.m(this.popup_modal);
    },

    reloadCaptcha: function()
    {
        var img = this.dom_captcha_image;
        img.attr('src', img.attr('src') + '&' + Math.random());
    }
}

function Window_Popup_UserRegister(data)
{
    this.pc = Window_Popup;
    this.pc(data);

    for (var f in cWindow_Popup_UserRegister) {
        this[f] = cWindow_Popup_UserRegister[f];
    }

    this.messages = {
        'user_login' : "Minumum 4 characters, maximum 15 characters",
        'user_email': "Don't worry, your email will never be shared",
        'user_password': "Must be at least 4 characters long",
        'user_password2': "Type in your password again for verification purposes",
        'captcha': "Can’t read the text? Click to image"
    };

    this.positive = {
        'user_login' : "Done",
        'user_email': "Done",
        'user_password': "Done",
        'user_password2': "Done",
        'captcha': "Done"
    };

    this.errors = {
        'EMAIL_NOT_VALID': "The email provided does not appear to be valid",
        'EMAIL_ALREADY_EXISTS': "The email provided does not appear to be valid",
        'LOGIN_NOT_VALID': "Usernames may only contain letters and numbers",
        'LOGIN_ALREADY_EXISTS': "Is already taken. Please choose a different username",
        'SHORT_PASSWORD': "Passwords must be at least 4 characters",
        'PASSWORD_NOT_LIKE': "Passwords do not match",
        'CAPTCHA_ERROR': "Text do not match"
    };

    this.testable = {
        'user_email': [this.name + '_user_email'],
        'user_login': [this.name + '_user_login'],
        'user_password': [this.name + '_user_password'],
        'user_password2': [this.name + '_user_password', this.name + '_user_password2'],
        'captcha': [this.name + '_captcha']
    };

    var _this = this;

    for (var i in this.testable) {
        var active = _(this.name + '_' + i);
        active.onfocus = function(field) {
                    return function(e) {
                        if (!$(_(_this.name + '_' + field)).hasClass('positive')) {
                            _this.showNotice(field, 'MESSAGES');
                        }
                    }
                }(i);

        active.onblur = function(field, sending) {
                    return function(e) {
                        var id = _this.name + '_' + field;
                        var params = {};
                        for (var i = 0; i < sending.length; i++) {
                            var current_send = $(_(sending[i]));
                            params[current_send.attr('name')] = current_send.val();
                        }

                        if ($(_(id)).val().length != 0) {
                            _this.checkField(field, params);
                        }
                    }
                }(i, this.testable[i]);
    }

    this.dom_captcha_image = $(_(this.name + '_captcha_image'))
        .click(function() {
            _this.reloadCaptcha();
        });

    this.dom_register_form = $(_(this.name + '_register_form'));
    this.dom_result_register = $(_(this.name + '_result_register'));

    //alert(Dump(data));
    if (data.outside) {
        this.open();
    }

}
