Ext.Ajax.calls = [];
Ext.getAjax = function(id) { return Ext.Ajax.calls[id] };
var FLAjax = function(id, scope) {
    this.id = id;
    if (scope)
        this.scope = scope;
    Ext.Ajax.calls[id] = this;
};
FLAjax.prototype = {
    transId: undefined,
    config: undefined,
    success: function(response, opt) {
        var rs;
        try {
            rs = eval("(" + response.responseText.substr(2) + ")");
        } catch (e) {
            rs = { __ex_t: 'Callback Error', __ex_m: response.responseText };
        }
        if (rs["__ex_t"] != null) {
            var msg = rs["__ex_m"];
            if (rs["__ex_s"])
                msg += "\n<div style='background:#F5F5F5 none repeat scroll 0 0;border-bottom:1px dotted #DDDDDD;border-top:1px dotted #DDDDDD;margin-top:10px;padding:0 5px;'>" + rs["__ex_s"] + "</div>";
            Ext.Msg.alert(rs["__ex_t"], msg, function() {
                if (rs['__ex_fn'])
                    rs['__ex_fn'](rs);
            });
            this.transId = undefined;
            return;
        }

        if (this.config.xfn) {
            this.config.xfn.call(this.scope, rs);
        }
        var fn = this.config.fn;
        if (fn) {
            if (typeof (fn) == "function")
                fn.call(this.config.scope, rs);
            else
                eval(fn).call(this.config.scope, rs);
        }
        this.transId = undefined;
    },

    failure: function(response) {
        Ext.Msg.alert('Falha na operação', response.responseText);
        this.transId = undefined;
    },

    callback: function(config) {
        if (this.transId !== undefined)
            return;
        if (!config)
            config = {};
        config.args = this.removeUndefined(config.args);
        Ext.applyIf(config, { id: this.id, url: window.document.URL, args: '' });
        this.config = config;
        this.transId = Ext.Ajax.request({ url: config.url, success: this.success, failure: this.failure, scope: this, params: { __CALLBACKID: config.id, __CALLBACKPARAM: Ext.util.JSON.encode(config.args), __VIEWSTATE: ""} });
    },
    removeUndefined: function(args) {
        for (k in args)
            if (args[k] == undefined)
            delete args[k];
        else if (typeof (args[k]) == 'object')
            args[k] = this.removeUndefined(args[k]);
        return args;
    },
    abort: function() {
        Ext.Ajax.abort(this.transId);
        this.transId = undefined;
    },

    isLoading: function() {
        Ext.Ajax.isLoading(this.transId);
    },

    replace: function(str, oldValue, newValue, replaceAll, caseInsensitive) {
        replaceAll = replaceAll || true;
        caseInsensitive = caseInsensitive || true;
        var flags = "";
        if (replaceAll)
            flags += "g";
        if (caseInsensitive)
            flags += "i";
        return str.replace(new RegExp(oldValue, flags), newValue);
    }
};