var cWindow_RestoreConfirm = {
    init: function()
    {
        this.dom_form_area.show();
        this.dom_result_area.hide()
        $(this.dom_button_send).show();

        for (var i in this.info) {
            this['dom_' + i].val('');
            this.changeNotice(i, 'info', this.info[i]);
        }
    },

    setEventListener: function(listener)
    {
        if (!this.listeners_collection) {
            this.listeners_collection = new ListenersCollection();
        }
        this.listeners_collection.addListener(listener);
    },

    eventButtonCancelClick: function(e)
    {
        e.preventDefault();
        this.listeners_collection.notify('cancelSend', null);
    },

    eventButtonSendClick: function(e)
    {
        e.preventDefault();

        try {
            var r = Request.create();
            if (r !== null) {
                this.listeners_collection.notify('startSend', null);
                r.send(URL_AJAX_HANDLER, {'rm': 'user_restore_confirm',
                                          'user_email':  this.dom_user_email.val()}, this);
                delete(r);
            }
        }
        catch (e) {
            this.handleJSException(e);
        }
    },

    requestCallback: function(result, request_params, exception)
    {
        if (request_params.rm != 'user_restore_confirm') {
            this.m = cWindow.requestCallback;
            return this.m(result, request_params);
        }

        if (exception) {
            this.finishSend();
            alert('Server error. Try again');
            result.exception.handled = true;
        } else {

            if (result.successful) {
                this.showResult();
            } else {
                this.finishSend();

                var error = this.errors[result.error_code];
                this.changeNotice(error.field, 'error', error.text);
            }
        }
    },

    showResult: function()
    {
        this.listeners_collection.notify('doneSend', null);
        this.dom_form_area.hide();
        this.dom_result_area.show()
        $(this.dom_button_send).hide();
    },

    finishSend: function()
    {
        this.listeners_collection.notify('doneSend', null)
        $(this.dom_button_send).show();
        $(this.dom_button_cancel).show();
    },

    changeNotice: function(field, mode, text) {
        var notice = $(_(this.name + '_' + field + '_notice'))
            .removeClass('positive')
            .removeClass('error')
            .removeClass('info')
            .addClass(mode).html(text);
    }
}

function Window_RestoreConfirm(data)
{
    this.pc = Window;
    this.pc(data);

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

    this.errors = {
        'USER_REGISTERED': {'field': 'user_email', 'text': 'User already registered'},
        'INVALID_EMAIL': {'field': 'user_email', 'text': 'User with this e-mail was not found'}
    };

    this.info = {
        'user_email': 'Enter your e-mail address'
    };

    this.dom_form_area = $(_(this.name + '_form_area'));
    this.dom_result_area = $(_(this.name + '_result_area'));

    this.dom_button_send = _(this.name + '_button_send');
    this.dom_button_cancel = _(this.name + '_button_cancel');

    var _this = this;

    jEvent.addEvent(this.dom_button_send, 'click', function(e) {_this.eventButtonSendClick(e)}, true);
    jEvent.addEvent(this.dom_button_cancel, 'click', function(e) {_this.eventButtonCancelClick(e)}, true);

    for (var i in this.info) {
        this['dom_' + i + '_notice'] = $(_(this.name + '_' + i + '_notice'));
        this['dom_' + i] = $(_(this.name + '_' + i))
            .val('')
            .click(function(field) {
               return function() {
                    if (!_this['dom_' + field + '_notice'].hasClass('positive')) {
                        _this.changeNotice(field, 'info', _this.info[field]);
                    }
               }
            }(i));
    }

}

