/********
 * @version: 0.7.0 - Professional Edition (Coolite Commercial License)
 * @author: Coolite Inc. http://www.coolite.com/
 * @date: 2008-12-22
 * @copyright: Copyright (c) 2006-2008, Coolite Inc. (http://www.coolite.com/). All rights reserved.
 * @license: See license.txt and http://www.coolite.com/license/. 
 * @website: http://www.coolite.com/
 ********/

Ext.namespace("Coolite.Ext");

Coolite.Ext.HttpWriteProxy = function (conn) {
    Coolite.Ext.HttpWriteProxy.superclass.constructor.call(this);
    this.conn = conn;
    this.useAjax = !conn || !conn.events;
    if (conn && conn.handleSaveResponseAsXml) {
        this.handleSaveResponseAsXml = conn.handleSaveResponseAsXml;
    }
};
Ext.extend(Coolite.Ext.HttpWriteProxy, Ext.data.HttpProxy, {
    handleSaveResponseAsXml: false,
    save: function (params, reader, callback, scope, arg) {
        if (this.fireEvent("beforesave", this, params) !== false) {
            var o = {
                params: params || {},
                request: {
                    callback: callback,
                    scope: scope,
                    arg: arg
                },
                reader: reader,
                scope: this,
                callback: this.saveResponse
            };
            if (this.useAjax) {
                Ext.applyIf(o, this.conn);
                if (this.activeRequest) {
                    Ext.Ajax.abort(this.activeRequest);
                }
                this.activeRequest = Ext.Ajax.request(o);
            } else {
                this.conn.reequest(o);
            }
        } else {
            callback.call(scope || this, null, arg, false);
        }
    },

    saveResponse: function (o, success, response) {
        delete this.activeRequest;
        if (! success) {
            this.fireEvent("saveexception", this, o, response, {message: response.statusText});
            o.request.callback.call(o.request.scope, null, o.request.arg, false);
            return;
        }
        var result;
        try {
            if (!this.handleSaveResponseAsXml) {
                var json = response.responseText;
                var responseObj = eval("(" + json + ")");
                result = {
                    success: responseObj.Success,
                    msg: responseObj.Msg,
                    data: responseObj.Data
                }; 
            }
            else {
                var doc = response.responseXML;
                var root = doc.documentElement || doc;
                var q = Ext.DomQuery;
                
                var sv = q.selectValue("Success", root, false);
                success = sv !== false && sv !== "false";                
                var msg = q.selectValue("Msg", root, "");
                
                result = {success: success, msg: msg};                
            }
        } catch (e) {
            this.fireEvent("saveexception", this, o, response, e);
            o.request.callback.call(o.request.scope, null, o.request.arg, false);
            return;
        }
        this.fireEvent("save", this, o, o.request.arg);
        o.request.callback.call(o.request.scope, result, o.request.arg, true);
    }
});

Ext.override(Ext.data.GroupingStore, {
    applySort : function(){
        Ext.data.GroupingStore.superclass.applySort.call(this);
        if(!this.groupOnSort && !this.remoteGroup){
            var gs = this.getGroupState();
            if(gs && gs != (Ext.isEmpty(this.sortInfo) ? "" : this.sortInfo.field)){
                this.sortData(this.groupField);
            }
        }
    }
})

Coolite.Ext.Store = function (config) {
    Ext.apply(this, config);

    this.deleted = [];
    this.addEvents(
        "beforesave",
        "save",
        "saveexception",
        "commitdone",
        "commitfailed");

    if (this.updateProxy) {
        this.relayEvents(this.updateProxy, ["saveexception"]);
    }

    Coolite.Ext.Store.superclass.constructor.call(this);
};

Ext.extend(Coolite.Ext.Store, Ext.data.GroupingStore, {
    pruneModifiedRecords: true,

    warningOnDirty: true,
    dirtyWarningTitle: "Uncommitted Changes",
    dirtyWarningText: "You have uncommitted changes.  Are you sure you want to load/reload data?",

    updateProxy: null,

    // "none" - no refresh after saving
    // "always" - always refresh after saving
    // "auto" - auto refresh. If no new records then refresh doesn't perfom. If new records exists then refresh will be perfom for refresh id fields
    refreshAfterSave: "Auto",

    useIdConfirmation: false,

    refreshIds: function(newRecordsExists, deletedExists, dataAbsent) {
        switch (this.refreshAfterSave) {
            case "None":
                return;
            case "Always":
                if (dataAbsent) {
                    this.reload();
                }
                else {
                    Coolite.Ext.Store.superclass.reload.call(this);
                }
                break;
            case "Auto":
                if (newRecordsExists || deletedExists) {
                    if (dataAbsent) {
                        this.reload();
                    }
                    else {
                        Coolite.Ext.Store.superclass.reload.call(this);
                    }
                }
                break;
        }
    },

    reload: function(options, baseReload) {
        if (this.proxy.refreshByUrl && baseReload !== true) {
            var opts = options || {};
            var prms = opts.params || {};
            this.callbackReload(this.warningOnDirty, prms);
        } else {
            Coolite.Ext.Store.superclass.reload.call(this, options);
        }
    },

    load: function(options) {
        var loadData = function(store, options) {
            store.deleted = [];
            store.modified = [];
            return Coolite.Ext.Store.superclass.load.call(store, options);
        };

        if (this.warningOnDirty && this.isDirty() && !this.silentMode) {
            this.silentMode = false;
            Ext.MessageBox.confirm(
                this.dirtyWarningTitle,
                this.dirtyWarningText,
                function(btn, text) {
                    return (btn == "yes") ? loadData(this, options) : false;
                },
                this
            );
        } else {
            return loadData(this, options);
        }
    },

    save: function(options) {
        if (Ext.isEmpty(this.updateProxy)) {
            this.callbackSave();
            return;
        }

        options = options || {};
        if (this.fireEvent("beforesave", this, options) !== false) {
            var json = this.getChangedData();

            if (json.length > 0) {
                var p = Ext.apply(options.params || {}, { data: "{" + json + "}" });
                this.updateProxy.save(p, this.reader, this.recordsSaved, this, options);
            } else {
                this.fireEvent("commitdone", this, options);
            }
        }
    },

    getChangedData: function() {
        var json = "";
        var d = this.deleted, m = this.modified;
        if (d.length > 0) {
            json += '"Deleted":[';
            for (var i = 0; i < d.length; i++) {
                var obj = {};
                var list = Ext.apply(obj, d[i].data);
                if (this.reader.meta.id && Ext.isEmpty(list[this.reader.meta.id], false)) {
                    list[this.reader.meta.id] = d[i].id;
                }
                json += Ext.util.JSON.encode(list) + ",";
            }
            json = json.substring(0, json.length - 1) + "]";
        }

        var jsonUpdated = "";
        var jsonCreated = "";
        for (var j = 0; j < m.length; j++) {
            var obj2 = {};

            var list2 = Ext.apply(obj2, m[j].data);
            if (this.reader.meta.id && Ext.isEmpty(list2[this.reader.meta.id], false)) {
                list2[this.reader.meta.id] = m[j].id;
            }
            if (m[j].newRecord) {
                jsonCreated += Ext.util.JSON.encode(list2) + ",";
            }
            else {
                jsonUpdated += Ext.util.JSON.encode(list2) + ",";
            }

        }

        if (jsonUpdated.length > 0) {
            jsonUpdated = jsonUpdated.substring(0, jsonUpdated.length - 1) + "]";
        }

        if (jsonCreated.length > 0) {
            jsonCreated = jsonCreated.substring(0, jsonCreated.length - 1) + "]";
        }


        if (jsonUpdated.length > 0) {
            if (json.length > 0) {
                json += ",";
            }
            json += '"Updated":[';
            json += jsonUpdated;
        }

        if (jsonCreated.length > 0) {
            if (json.length > 0) {
                json += ",";
            }
            json += '"Created":[';
            json += jsonCreated;
        }

        return json;
    },

    getByDataId: function(id) {
        if (!this.reader.meta.id) {
            return undefined;
        }

        var m = this.modified;
        for (var i = 0; i < m.length; i++) {
            if (m[i].data[this.reader.meta.id] == id) {
                return m[i];
            }
        }

        return undefined;
    },

    recordsSaved: function(o, options, success) {
        if (!o || success === false) {
            if (success !== false) {
                this.fireEvent("save", this, options);
            }
            if (options.callback) {
                options.callback.call(options.scope || this, options, false);
            }
            //this.fireEvent("commitfailed", this);
            return;
        }

        var serverSuccess = o.success;
        var msg = o.msg;

        this.fireEvent("save", this, options);
        if (options.callback) {
            options.callback.call(options.scope || this, options, true);
        }

        var serviceResult = o.data || {};

        var newRecordsExists = false;
        var deletedExists = this.deleted.length > 0;
        var m = this.modified;
        for (var j = 0; j < m.length; j++) {
            if (m[j].newRecord) {
                newRecordsExists = true;
                break;
            }
        }

        if (!serverSuccess) {
            this.fireEvent("commitfailed", this, msg);
            return;
        }

        if (this.useIdConfirmation) {
            if (Ext.isEmpty(serviceResult.confirm)) {
                this.fireEvent("commitfailed", this, "The confirmation list is absent");
                return;
            }

            var r = serviceResult.confirm;
            var failCount = 0;
            for (var i = 0; i < r.length; i++) {

                if (r[i].s === false) {
                    failCount++;
                }
                else {
                    var record = this.getById(r[i].oldId) || this.getByDataId(r[i].oldId);
                    if (record) {
                        record.commit();
                        if (record.newRecord || false) {
                            delete record.newRecord;
                            var index = this.data.indexOf(record);
                            this.data.removeAt(index);
                            record.id = r[i].newId || r[i].oldId;
                            this.data.insert(index, record);
                        }
                    } else {
                        var d = this.deleted;
                        for (var i2 = 0; i2 < d.length; i2++) {
                            if (this.reader.meta.id && d[i2].id == r[i].oldId) {
                                this.deleted.splice(i2, 1);
                                failCount--;
                                break;
                            }
                        }
                        failCount++;
                    }
                }
            }            

            if (failCount > 0) {
                this.fireEvent("commitfailed", this, "Some records have no success confirmation!");
                return;
            }
            this.modified = [];
            this.deleted = [];
        } else {
            this.commitChanges();
        }


        this.fireEvent("commitdone", this, options);

        var dataAbsent = true;
        if (serviceResult.data && serviceResult.data != null && this.proxy.refreshData) {
            dataAbsent = false;
            this.proxy.refreshData(serviceResult.data);
        }

        this.refreshIds(newRecordsExists, deletedExists, dataAbsent);
    },

    getDeletedRecords: function() {
        return this.deleted;
    },

    remove: function(record) {
        if (!record.newRecord) {
            this.deleted.push(record);
        }
        Coolite.Ext.Store.superclass.remove.call(this, record);
    },

    commitChanges: function() {
        Coolite.Ext.Store.superclass.commitChanges.call(this);
        //var d = this.deleted.slice(0);
        this.deleted = [];
        //for (var i = 0, len = d.length; i < len; i++) {
        //    d[i].commit();
        // }
    },

    rejectChanges: function() {
        Coolite.Ext.Store.superclass.rejectChanges.call(this);
        var d = this.deleted.slice(0);
        this.deleted = [];
        for (var i = 0, len = d.length; i < len; i++) {
            d[i].reject();
        }
    },

    isDirty: function() {
        return (this.deleted.length > 0 || this.modified.length > 0) ? true : false;
    },

    prepareCallback: function(context) {
        var options = { params: {} };
        if (context.fireEvent("beforesave", context, options) !== false) {
            var json = context.getChangedData();
            if (json.length > 0) {
                var p = { data: "{" + json + "}", extraParams: options.params };
                return p;
            } else {
                context.fireEvent("commitdone", context, options);
            }
        }
        return null;
    },

    callbackHandler: function(response, result, context, type, action, extraParams) {
        try {
            var responseObj = result.serviceResponse;
            result = { success: responseObj.Success, msg: responseObj.Msg, data: responseObj.Data };
        } catch (e) {
            context.fireEvent("saveexception", context, {}, response, e);
            return;
        }
        context.recordsSaved(result, {}, true);
    },

    silentMode: false,

    callbackRefreshHandler: function(response, result, context, type, action, extraParams) {
        var p = context.proxy;
        try {
            var responseObj = result.serviceResponse;
            result = { success: responseObj.Success, msg: responseObj.Msg || null, data: responseObj.Data || {} };
        } catch (e) {
            context.fireEvent("loadexception", context, {}, response, e);
            return;
        }

        if (result.success === false) {
            context.fireEvent("loadexception", context, {}, response, { message: result.msg });
            return;
        }

        if (p.refreshData) {
            if (result.data.data && result.data.data != null) {
                p.refreshData(result.data.data);
            }
            else {
                p.refreshData({});
            }
        }

        context.silentMode = true;
        Coolite.Ext.Store.superclass.reload.call(context);
    },

    callbackErrorHandler: function(response, result, context, type, action, extraParams) {
        context.fireEvent("saveexception", context, {}, response, { message: result.errorMessage || response.statusText });
    },

    callbackRefreshErrorHandler: function(response, result, context, type, action, extraParams) {
        context.fireEvent("loadexception", context, {}, response, { message: result.errorMessage || response.statusText });
    },

    callbackSave: function() {
        var requestObject = this.prepareCallback(this);
        if (requestObject != null) {
            var config = {}, ac = this.ajaxEventConfig;
            ac.userSuccess = this.callbackHandler;
            ac.userFailure = this.callbackErrorHandler;
            ac.extraParams = requestObject.extraParams;

            Ext.apply(config, ac, {
                control: this,
                eventType: "postback",
                action: "update",
                serviceParams: requestObject.data
            });
            Coolite.AjaxEvent.request(config);
        }
    },

    submitData: function(data) {
        if (Ext.isEmpty(data)) {
            var records = this.store.getRange() || [];

            var values = [];
            for (var i = 0; i < records.length; i++) {
                var obj = {};
                if (this.store.reader.meta.id) {
                    obj[this.store.reader.meta.id] = records[i].id;
                }

                values.push(Ext.apply(obj, records[i].data));
            }

            data = values;
        }

        if (Ext.isEmpty(this.updateProxy)) {
            var options = { params: {} };
            if (data.length > 0) {
                if (this.fireEvent("beforesave", this, options) !== false) {
                    var config = {}, ac = this.ajaxEventConfig;
                    ac.userSuccess = this.submitSuccess;
                    ac.userFailure = this.submitFailure;
                    ac.extraParams = options.params;

                    Ext.apply(config, ac, {
                        control: this,
                        eventType: "postback",
                        action: "submit",
                        serviceParams: Ext.encode(data)
                    });

                    Coolite.AjaxEvent.request(config);
                }
            }
        }
        else {
            var options = { params: {} };
            if (data.length > 0) {
                if (this.fireEvent("beforesave", this, options) !== false) {
                    var p = Ext.apply(options.params || {}, { data: Ext.encode(data) });
                    this.updateProxy.save(p, this.reader, this.finishSubmit, this, options);
                }
            }
        }
    },

    finishSubmit: function(o, options, success) {
        if (!o || success === false) {
            if (success !== false) {
                this.fireEvent("save", this, options);
            }
            return;
        }

        var serverSuccess = o.success;
        var msg = o.msg;

        if (!serverSuccess) {
            context.fireEvent("saveexception", this, options, {}, { message: msg });
            return;
        }

        this.fireEvent("save", this, options);
    },

    submitFailure: function(response, result, context, type, action, extraParams) {
        context.fireEvent("saveexception", context, {}, response, { message: result.errorMessage || response.statusText });
    },

    submitSuccess: function(response, result, context, type, action, extraParams) {
        try {
            var responseObj = result.serviceResponse;
            result = { success: responseObj.Success, msg: responseObj.Msg };
        } catch (e) {
            context.fireEvent("saveexception", context, {}, response, e);
            return;
        }

        if (!result.success) {
            context.fireEvent("saveexception", context, {}, response, { message: result.msg });
        }

        context.fireEvent("save", context, {});
    },

    callbackReload: function(dirtyConfirm, reloadOptions) {
        var params = reloadOptions || {};
        var lastOpts = this.lastOptions || {};
        Ext.apply(params, lastOpts.params || {});

        var options = { params: params };

        var reload = function(store, options) {
            if (store.fireEvent("beforeload", store, options) !== false) {
                var config = {}, ac = store.ajaxEventConfig;
                ac.userSuccess = store.callbackRefreshHandler;
                ac.userFailure = store.callbackRefreshErrorHandler;
                ac.extraParams = options.params;

                Ext.apply(config, ac, { control: store, eventType: "postback", action: "refresh" });
                Coolite.AjaxEvent.request(config);
            }
        };

        if (dirtyConfirm && this.isDirty()) {
            Ext.MessageBox.confirm(
                this.dirtyWarningTitle,
                this.dirtyWarningText,
                function(btn, text) {
                    if (btn == "yes") {
                        reload(this, options);
                    }
                },
                this
            );
        }
        else {
            reload(this, options);
        }
    }
});

//------Save mask-------------
Coolite.Ext.SaveMask = function (el, config) {
    this.el = Ext.get(el);
    Ext.apply(this, config);
    if (this.writeStore) {
        this.writeStore.on("beforesave", this.onBeforeSave, this);
        this.writeStore.on("save", this.onSave, this);
        this.writeStore.on("saveexception", this.onSave, this);
        this.writeStore.on("commitdone", this.onSave, this);
        this.writeStore.on("commitfailed", this.onSave, this);
        this.removeMask = Ext.value(this.removeMask, false);
    }
};

Coolite.Ext.SaveMask.prototype = {
    msg: "Saving...",
    msgCls: "x-mask-loading",
    disabled: false,
    
    disable: function () {
        this.disabled = true;
    },
    
    enable: function () {
        this.disabled = false;
    },

    onSave: function () {
        this.el.unmask(this.removeMask);
    },

    onBeforeSave: function () {
        if (!this.disabled) {
            this.el.mask(this.msg, this.msgCls);
        }
    },

    show: function () {
        this.onBeforeSave();
    },

    hide: function () {
        this.onSave();    
    },

    destroy: function () {
        if (this.writeStore) {
            this.writeStore.un("beforesave", this.onBeforeSave, this);
            this.writeStore.un("save", this.onSave, this);
            this.writeStore.un("saveexception", this.onSave, this);
            this.writeStore.un("commitdone", this.onSave, this);
            this.writeStore.un("commitfailed", this.onSave, this);
        }
    }
};

//----------------------------

Coolite.Ext.GridPanel = function(config) {
    this.selectedIds = {};
    this.memoryIDField = 'id';

    Ext.apply(this, config);
    this.addEvents("editcompleted");
    Coolite.Ext.GridPanel.superclass.constructor.call(this);
    this.initSelection();    
};

Ext.extend(Coolite.Ext.GridPanel, Ext.grid.EditorGridPanel, {
    doSelection: function() {
        var data = this.selModel.selectedData
        var silent = true;
        if (!Ext.isEmpty(this.fireSelectOnLoad)) {
            silent = !this.fireSelectOnLoad;
        }
        if (!Ext.isEmpty(data)) {
            if (silent) {
                this.suspendEvents();
                this.selModel.suspendEvents();
            }

            if (this.selModel.select) {
                if (!Ext.isEmpty(data.recordID) && !Ext.isEmpty(data.name)) {
                    var rowIndex = this.store.indexOfId(data.recordID);
                    var colIndex = this.getColumnModel().findColumnIndex(data.name);

                    if (rowIndex > -1 && colIndex > -1) {
                        this.selModel.select(rowIndex, colIndex);
                    }
                }
                else if (!Ext.isEmpty(data.rowIndex) && !Ext.isEmpty(data.colIndex)) {
                    this.selModel.select(data.rowIndex, data.colIndex);
                }

                if (silent) {
                    this.updateCellSelection();
                }
            }
            else if (this.selModel.selectRow && data.length > 0) {
                var records = [];
                var record;
                for (var i = 0; i < data.length; i++) {
                    if (!Ext.isEmpty(data[i].recordID)) {
                        record = this.store.getById(data[i].recordID);
                        if (this.selectionMemory) {
                            var idx = data[i].rowIndex || -1;
                            if (!Ext.isEmpty(record)) {
                                idx = this.store.indexOfId(record.id);
                                idx = this.getAbsoluteIndex(idx);
                            }

                            this.onMemorySelectId(null, idx, data[i].recordID);
                        }
                    }
                    else if (!Ext.isEmpty(data[i].rowIndex)) {
                        record = this.store.getAt(data[i].rowIndex);
                        if (this.selectionMemory && !Ext.isEmpty(record)) {
                            this.onMemorySelectId(null, data[i].rowIndex, record.id);
                        }
                    }

                    if (!Ext.isEmpty(record)) {
                        records.push(record);
                    }
                }
                this.selModel.selectRecords(records);
                if (silent) {
                    this.updateSelectedRows();
                }
            }
            if (silent) {
                this.resumeEvents();
                this.selModel.resumeEvents();
            }
        }
    },

    updateSelectedRows: function() {

        var records = [];
        if (this.selectionMemory) {
            for (var id in this.selectedIds) {
                records.push({ RecordID: this.selectedIds[id].id, RowIndex: this.selectedIds[id].index });
            }
        }
        else {
            var selectedRecords = this.selModel.getSelections();
            for (var i = 0; i < selectedRecords.length; i++) {
                records.push({ RecordID: selectedRecords[i].id, RowIndex: this.store.indexOfId(selectedRecords[i].id) });
            }
        }

        this.hField.dom.value = Ext.encode(records);
    },

    updateCellSelection: function(sm, selection) {
        if (selection == null) {
            this.hField.dom.value = "";
        }
    },

    cellSelect: function(sm, rowIndex, colIndex) {
        var r = this.store.getAt(rowIndex);
        var selection = {
            record: r,
            cell: [rowIndex, colIndex]
        };

        var name = this.getColumnModel().getDataIndex(selection.cell[1]);
        var value = selection.record.get(name);
        var id = selection.record.id || "";

        this.hField.dom.value = Ext.encode({ RecordID: id, Name: name, SubmittedValue: value, RowIndex: selection.cell[0], ColIndex: selection.cell[1] });
    },

    selectionMemory: true,

    initComponent: function() {
        Coolite.Ext.GridPanel.superclass.initComponent.call(this);

        if (this.selectionMemory) {
            this.selModel.on('rowselect', this.onMemorySelect, this);
            this.selModel.on('rowdeselect', this.onMemoryDeselect, this);
            this.store.on('remove', this.onStoreRemove, this);
            this.getView().on('refresh', this.memoryReConfigure, this);
        }

        if (!this.record && this.store) {
            this.record = this.store.recordType;
        }

        if (this.disableSelection) {
            if (this.selModel.select) {
                this.selModel.select = Ext.emptyFn;
            } else if (this.selModel.selectRow) {
                this.selModel.selectRow = Ext.emptyFn;
            }
        }

        if (this.store.getCount() > 0) {
            this.on("render", this.doSelection, this, { single: true, delay: 100 });
        } else {
            this.store.on("load", this.doSelection, this, { single: true, delay: 100 });
        }
    },

    /*Selection Memory*/

    memoryReConfigure: function() {
        this.store.on('clear', this.onMemoryClear, this);
        this.store.on('datachanged', this.memoryRestoreState, this);
    },

    onMemorySelect: function(sm, idx, rec) {
        var id = this.getRecId(rec);
        var absIndex = this.getAbsoluteIndex(idx);
        this.onMemorySelectId(sm, absIndex, id);
    },

    onMemorySelectId: function(sm, index, id) {
        var obj = { id: id, index: index };
        this.selectedIds[id] = obj;
    },

    getAbsoluteIndex: function(pageIndex) {
        var absIndex = pageIndex;
        if (!Ext.isEmpty(this.pbarID)) {
            if (!this.pbar) {
                this.pbar = Ext.getCmp(this.pbarID);
            }
            absIndex = ((this.pbar.getPageData().activePage - 1) * this.pbar.pageSize) + pageIndex;
        }

        return absIndex;
    },

    onMemoryDeselect: function(sm, idx, rec) {
        delete this.selectedIds[this.getRecId(rec)];
    },

    onStoreRemove: function(store, rec, idx) {
        this.onMemoryDeselect(null, idx, rec);
    },

    memoryRestoreState: function() {
        if (this.store != null) {
            var i = 0;
            var sel = [];
            var all = true;
            this.store.each(function(rec) {
                var id = this.getRecId(rec);
                if (!Ext.isEmpty(this.selectedIds[id])) {
                    sel.push(i);
                }
                else {
                    all = false;
                }

                ++i;
            }, this);

            var silent = true;
            if (!Ext.isEmpty(this.fireSelectOnLoad)) {
                silent = !this.fireSelectOnLoad;
            }

            if (sel.length > 0) {
                if (silent) {
                    this.suspendEvents();
                    this.selModel.suspendEvents();
                }

                this.selModel.selectRows(sel);

                if (silent) {
                    this.resumeEvents();
                    this.selModel.resumeEvents();
                }
            }

            if (this.selModel.checkHeader) {
                if (all) {
                    this.selModel.checkHeader();
                }
                else {
                    this.selModel.uncheckHeader();
                }
            }
        }
    },

    getRecId: function(rec) {
        var id = rec.get(this.memoryIDField);
        if (Ext.isEmpty(id)) {
            id = rec.id;
        }

        return id;
    },

    onMemoryClear: function() {
        var sel = [];
        this.selectedIds = {};
    },

    /*------------------------------*/

    initSelection: function() {
        if (!Ext.isEmpty(this.selModelHidden)) {
            this.hField = Ext.get(this.selModelHidden);

            if (!Ext.isEmpty(this.hField)) {
                if (this.selModel.select) {
                    this.selModel.on("cellselect", this.cellSelect, this);
                    this.selModel.on("selectionchange", this.updateCellSelection, this);
                }
                else if (this.selModel.selectRow) {
                    this.selModel.on("rowselect", this.updateSelectedRows, this);
                    this.selModel.on("rowdeselect", this.updateSelectedRows, this);
                    this.store.on('remove', this.updateSelectedRows, this);
                }
            }
        }
    },

    onRender: function(ct, position) {
        Coolite.Ext.GridPanel.superclass.onRender.call(this, ct, position);

        if (this.menu instanceof Ext.menu.Menu) {
            this.on("contextmenu", this.showContextMenu);
            this.on("rowcontextmenu", this.onRowContextMenu);
        }

        this.relayEvents(this.selModel, ["rowselect", "rowdeselect"]);
        this.relayEvents(this.store, ["commitdone", "commitfailed"]);

        this.keymap = new Ext.KeyMap(this.view.el, {
            key: [13, 35, 36],
            scope: this,
            fn: this.handleKeys
        });
    },

    onEditComplete: function(ed, value, startValue) {
        Coolite.Ext.GridPanel.superclass.onEditComplete.call(this, ed, value, startValue);
        if (!ed.record.dirty && ed.record.firstEdit) {
            this.store.remove(ed.record);
        }
        delete ed.record.firstEdit;
        this.fireEvent("editcompleted", ed, value, startValue);
    },

    onRowContextMenu: function(grid, rowIndex, e) {
        e.stopEvent();
        if (!this.selModel.isSelected(rowIndex)) {
            this.selModel.selectRow(rowIndex);
            this.fireEvent("rowclick", this, rowIndex, e);
        }
        this.showContextMenu(e, rowIndex);
    },

    showContextMenu: function(e, rowIndex) {
        e.stopEvent();
        if (rowIndex === undefined) {
            this.selModel.clearSelections();
        }
        if (this.menu) {
            this.menu.showAt(e.getXY());
        }
    },

    handleKeys: function(key, e) {
        e.stopEvent();
        switch (key) {
            case 13:  // return key
                var rowIndex = this.selModel.last;
                var keyEvent = (e.shiftKey === true) ? "rowdblclick" : "rowclick";
                this.fireEvent(keyEvent, this, rowIndex, e);
                break;
            case 35:  // end key
                if (this.store.getCount() > 0) {
                    this.selModel.selectLastRow();
                    this.getView().focusRow(this.store.getCount() - 1);
                }
                break;
            case 36:  // home key
                if (this.store.getCount() > 0) {
                    this.selModel.selectFirstRow();
                    this.getView().focusRow(0);
                }
                break;
        }
    },

    reload: function(options) {
        this.store.reload(options);
    },

    isDirty: function() {
        if (this.store.modified.length > 0 || this.store.deleted.length > 0) {
            return true;
        }
        return false;
    },

    hasSelection: function() {
        return this.selModel.hasSelection();
    },

    addRecord: function(values) {
        var rowIndex = this.store.data.length;
        this.insertRecord(rowIndex, values);
        return rowIndex;
    },

    insertRecord: function(rowIndex, values) {
        var f = this.record.prototype.fields, dv = [];
        for (var i = 0; i < f.length; i++) {
            dv[f.items[i].name] = f.items[i].defaultValue;
        }
        var record = new this.record(dv);
        record.firstEdit = true;
        record.newRecord = true;
        this.stopEditing();
        this.store.insert(rowIndex, record);
        values = values || {};
        for (var v in values) {
            record.set(v, values[v]);
        }
    },

    deleteRecord: function(record) {
        this.store.remove(record);
    },

    deleteSelected: function() {
        var s = this.selModel.getSelections();
        for (var i = 0, len = s.length; i < len; i++) {
            this.deleteRecord(s[i]);
        }
    },

    load: function(options) {
        this.store.load(options);
    },

    save: function(options) {
        this.store.save(options);
    },

    clear: function() {
        this.store.removeAll();
    },

    //--------------------------
    saveMask: false,
    initEvents: function() {
        Coolite.Ext.GridPanel.superclass.initEvents.call(this);

        if (this.saveMask) {
            this.saveMask = new Coolite.Ext.SaveMask(this.bwrap,
                    Ext.apply({ writeStore: this.store }, this.saveMask));
        }
    },

    reconfigure: function(store, colModel) {
        Coolite.Ext.GridPanel.superclass.reconfigure.call(this, store, colModel);
        if (this.saveMask) {
            this.saveMask.destroy();
            this.saveMask = new Coolite.Ext.SaveMask(this.bwrap,
                    Ext.apply({ writeStore: store }, this.initialConfig.saveMask));
        }
    },

    onDestroy: function() {
        if (this.rendered) {
            if (this.saveMask) {
                this.saveMask.destroy();
            }
        }
        Coolite.Ext.GridPanel.superclass.onDestroy.call(this);
    },

    insertColumn: function(index, newCol) {
        var c = this.getColumnModel().config;
        if (index >= 0) {
            c.splice(index, 0, newCol);
        }

        Ext.apply(c, { events: this.getColumnModel().events, ajaxEvents: this.getColumnModel().ajaxEvents })
        var cm = new Ext.grid.ColumnModel(c);

        this.reconfigure(this.store, cm);
    },

    addColumn: function(newCol) {
        var c = this.getColumnModel().config;
        c.push(newCol);

        Ext.apply(c, { events: this.getColumnModel().events, ajaxEvents: this.getColumnModel().ajaxEvents })
        var cm = new Ext.grid.ColumnModel(c);
        this.reconfigure(this.store, cm);
    },

    removeColumn: function(index) {
        var c = this.getColumnModel().config;
        if (index >= 0) {
            c.splice(index, 1);
        }

        Ext.apply(c, { events: this.getColumnModel().events, ajaxEvents: this.getColumnModel().ajaxEvents })
        var cm = new Ext.grid.ColumnModel(c);

        this.reconfigure(this.store, cm);
    },

    reconfigureColumns: function(cfg) {

        var oldCM = this.getColumnModel();
        Ext.apply(cfg, { events: oldCM.events, ajaxEvents: oldCM.ajaxEvents });

        var specialCols = ['checker', 'expander'];

        for (var i = 0; i < specialCols.length; i++) {
            var specCol = oldCM.getColumnById(specialCols[i]);
            if (!Ext.isEmpty(specCol)) {
                var index = oldCM.getIndexById(specialCols[i]);
                if (index != 0 && index >= cfg.length) {
                    index = cfg.length - 1;
                }
                cfg.splice(index, 0, specCol);
            }
        }

        var cm = new Ext.grid.ColumnModel(cfg);

        this.reconfigure(this.store, cm);
    },

    getRowsValues: function(selectedOnly) {
        if (Ext.isEmpty(selectedOnly)) {
            selectedOnly = true;
        }

        var records = (selectedOnly ? this.selModel.getSelections() : this.store.getRange()) || [];

        var values = [];
        for (var i = 0; i < records.length; i++) {
            var obj = {};
            if (this.store.reader.meta.id) {
                obj[this.store.reader.meta.id] = records[i].id;
            }

            values.push(Ext.apply(obj, records[i].data));
        }

        return values;
    },

    submitData: function(selectedOnly) {
        this.store.submitData(this.getRowsValues(selectedOnly || false));
    }
});

Ext.reg("coolitegrid", Coolite.Ext.GridPanel);

Ext.data.PagingMemoryProxy = function (data, isUrl) {
	Ext.data.PagingMemoryProxy.superclass.constructor.call(this);
	this.data = data;
	this.isUrl = isUrl || false;		
	this.isNeedRefresh = this.isUrl;
	this.url = this.isUrl ? data : "";	
};

Ext.extend(Ext.data.PagingMemoryProxy, Ext.data.MemoryProxy, {

    refreshData: function(data, store) {
        if (this.isUrl === true) {
            this.isNeedRefresh = true;
        } else {
            if (data && data != null) {
                this.data = data;
            } else {
                store.callbackReload(store.warningOnDirty);
            }
        }
    },

    refreshByUrl: function(params, reader, callback, scope, arg) {
        var o = {
            method: "GET",
            request: {
                callback: callback,
                scope: scope,
                arg: arg,
                params: params || {}
            },
            reader: reader,
            url: this.url,
            callback: this.loadResponse,
            scope: this
        };

        if (this.activeRequest) {
            Ext.Ajax.abort(this.activeRequest);
        }

        this.activeRequest = Ext.Ajax.request(o);
    },

    loadResponse: function(o, success, response) {
        delete this.activeRequest;
        if (!success) {
            this.fireEvent("loadexception", this, o, response);
            o.request.callback.call(o.request.scope, null, o.request.arg, false);
            return;
        }

        try {
            if (o.reader.getJsonAccessor) {
                this.data = response.responseText;
            } else {
                this.data = response.responseXML;
            }

            if (!this.data) {
                throw { message: "The data doesn't available" };
            }
        } catch (e) {
            this.fireEvent("loadexception", this, o, response, e);
            o.request.callback.call(o.request.scope, null, o.request.arg, false);
            return;
        }

        this.isNeedRefresh = false;
        this.load(o.request.params, o.reader, o.request.callback, o.request.scope, o.request.arg);
    },

    load: function(params, reader, callback, scope, arg) {

        this.fireEvent("beforeload", this, params);
        params = params || {};

        if (this.isNeedRefresh === true) {
            this.refreshByUrl(params, reader, callback, scope, arg);
            return;
        }

        var result;
        try {
            result = reader.readRecords(this.data);
        } catch (e) {
            this.fireEvent("loadexception", this, arg, null, e);
            callback.call(scope, null, arg, false);
            return;
        }

        if (params.gridfilters !== undefined) {
            var r = [];
            for (var i = 0, len = result.records.length; i < len; i++) {
                if (params.gridfilters.call(this, result.records[i])) {
                    r.push(result.records[i]);
                }
            }
            result.records = r;
            result.totalRecords = result.records.length;
        }


        if (params.sort !== undefined) {
            var dir = String(params.dir).toUpperCase() == "DESC" ? -1 : 1;

            var fn = function(v1, v2) {
                return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
            };

            result.records.sort(function(a, b) {
                var v = 0;
                if (typeof (a) == "object") {
                    v = fn(a.data[params.sort], b.data[params.sort]) * dir;
                } else {
                    v = fn(a, b) * dir;
                }
                if (v === 0) {
                    v = (a.index < b.index ? -1 : 1);
                }
                return v;
            });
        }

        if (params.start !== undefined && params.limit !== undefined) {
            result.records = result.records.slice(params.start, params.start + params.limit);
        }

        callback.call(scope, result, arg, true);
    }
});

Coolite.Ext.initRefreshPagingToolbar = function (grid) {
    var bBar = grid.getBottomToolbar();
    for (i = 0; i < bBar.items.items.length; ++i) {
        var item = bBar.items.items[i];
        if (item.iconCls == "x-tbar-loading" && item.tooltip == bBar.refreshText) {
            item.setHandler(function () {
                if (grid.getStore().proxy.refreshData) {
                    grid.getStore().proxy.refreshData(null, grid.getStore());
                }
                if (grid.getStore().proxy.isUrl) {
                    item.initialConfig.handler();
                }
            });
            return;
        }
    }
};

Coolite.Ext.PropertyGrid = function () {
    Coolite.Ext.PropertyGrid.superclass.constructor.call(this);	
	this.addEvents("beforesave", "save", "saveexception");
};

Coolite.Ext.PropertyGrid = Ext.extend(Ext.grid.PropertyGrid, {

    editable: true,

    initComponent: function() {
        Coolite.Ext.PropertyGrid.superclass.initComponent.call(this);
        if (!this.editable) {
            this.on("beforeedit", function(e) {
                return false;
            });
        }
    },

    callbackHandler: function(response, result, context, type, action, extraParams) {
        try {
            var responseObj = result.serviceResponse;
            result = { success: responseObj.Success, msg: responseObj.Msg || null };
        } catch (e) {
            context.fireEvent("saveexception", context, response, e);
            return;
        }

        if (result.success === false) {
            context.fireEvent("saveexception", context, response, { message: result.msg });
            return;
        }

        context.fireEvent("save", context, response);
    },

    callbackErrorHandler: function(response, result, context, type, action, extraParams) {
        context.fireEvent("saveexception", context, response, { message: result.errorMessage || response.statusText });
    },

    save: function() {
        var options = { params: {} };
        if (this.fireEvent("beforesave", this, options) !== false) {
            var config = {}, ac = this.ajaxEventConfig;
            ac.userSuccess = this.callbackHandler;
            ac.userFailure = this.callbackErrorHandler;
            ac.extraParams = options.params;

            Ext.apply(config, ac, { control: this, eventType: "postback", action: "update", serviceParams: Ext.encode(this.getSource()) });
            Coolite.AjaxEvent.request(config);
        }
    }
});

Ext.reg("coolitepropertygrid", Coolite.Ext.PropertyGrid);

Ext.data.ArrayReader.override({
    isArrayReader:true
});

Coolite.Ext.DataSourceProxy = function () {
    Coolite.Ext.DataSourceProxy.superclass.constructor.call(this);
};

Ext.extend(Coolite.Ext.DataSourceProxy, Ext.data.DataProxy, {
    ro: {},
    load: function(params, reader, callback, scope, arg) {
        if (this.fireEvent("beforeload", this, params) !== false) {
            this.ro = {
                params: params || {},
                request: {
                    callback: callback,
                    scope: scope,
                    arg: arg
                },
                reader: reader,
                callback: this.loadResponse,
                scope: this
            };

            var config = {}, ac = scope.ajaxEventConfig;
            ac.userSuccess = this.successHandler;
            ac.userFailure = this.errorHandler;
            ac.extraParams = params;

            Ext.apply(config, ac, { control: scope, eventType: "postback", action: "refresh" });
            Coolite.AjaxEvent.request(config);
        } else {
            callback.call(scope || this, null, arg, false);
        }
    },

    successHandler: function(response, result, context, type, action, extraParams) {
        var p = context.proxy;

        try {
            var responseObj = result.serviceResponse;
            result = { success: responseObj.Success, msg: responseObj.Msg || null, data: responseObj.Data || {} };
        } catch (e) {
            context.fireEvent("loadexception", context, {}, response, e);
            p.ro.request.callback.call(p.ro.request.scope, null, p.ro.request.arg, false);
            return;
        }

        if (result.success === false) {
            context.fireEvent("loadexception", context, {}, response, { message: result.msg });
            p.ro.request.callback.call(p.ro.request.scope, null, p.ro.request.arg, false);
            return;
        }

        try {
            var meta = p.ro.reader.meta;

            //&& result.data.totalCount > 0
            if (!Ext.isEmpty(result.data.totalCount)) {
                meta.totalProperty = "totalCount";
            }

            if (Ext.isEmpty(meta.root)) {
                meta.root = "data";
            }

            if (Ext.isEmpty(result.data[meta.root])) {
                result.data[meta.root] = [];
            }

            if (p.ro.reader.isArrayReader) {
                result = p.ro.reader.readRecords(result.data.data);
            }
            else {
                result = p.ro.reader.readRecords(result.data);
            }            

        } catch (ex) {
            p.fireEvent("loadexception", p, p.ro, response, ex);
            p.ro.request.callback.call(p.ro.request.scope, null, p.ro.request.arg, false);
            return;
        }
        p.fireEvent("load", p, p.ro, p.ro.request.arg);
        p.ro.request.callback.call(p.ro.request.scope, result, p.ro.request.arg, true);

    },

    errorHandler: function(response, result, context, type, action, extraParams) {
        var p = context.proxy;
        p.fireEvent("loadexception", p, p.ro, response);
        p.ro.request.callback.call(p.ro.request.scope, null, p.ro.request.arg, false);
    }
});

Ext.grid.RowExpander = function (config) {
    Ext.apply(this, config);

    this.addEvents({
        beforeexpand : true,
        expand: true,
        beforecollapse: true,
        collapse: true
    });

    Ext.grid.RowExpander.superclass.constructor.call(this);

    if (this.tpl) {
        if (typeof this.tpl == "string") {
            this.tpl = new Ext.Template(this.tpl);
        }
        this.tpl.compile();
    }

    this.state = {};
    this.bodyContent = {};
};

Ext.extend(Ext.grid.RowExpander, Ext.util.Observable, {
    header: "",
    width: 20,
    sortable: false,
    fixed: true,
    menuDisabled: true,
    dataIndex: "",
    id: "expander",
    lazyRender: true,
    enableCaching: true,
    collapsed: true,

    getRowClass: function (record, rowIndex, p, ds) {
        p.cols = p.cols - 1;
        var content = this.bodyContent[record.id];
        if (!content && !this.lazyRender) {
            content = this.getBodyContent(record, rowIndex);
        }
        if (content) {
            p.body = content;
        }

        if (this.state[record.id] === undefined) {

            if (this.collapsed === false) {
                this.state[record.id] = true;
                if (this.tpl && this.lazyRender) {
                    p.body = this.getBodyContent(record, rowIndex);
                }         
                return "x-grid3-row-expanded";
            }
            
            return "x-grid3-row-collapsed";
        }

        return this.state[record.id] ? "x-grid3-row-expanded" : "x-grid3-row-collapsed";
    },

    init: function (grid) {
        this.grid = grid;

        var view = grid.getView();
        view.getRowClass = this.getRowClass.createDelegate(this);

        view.enableRowBody = true;

        grid.on("render", function () {
            view.mainBody.on("mousedown", this.onMouseDown, this);
        }, this);
    },

    getBodyContent: function (record, index) {
        if (!this.enableCaching) {
            return this.tpl.apply(record.data);
        }
        var content = this.bodyContent[record.id];
        if (!content) {
            content = this.tpl.apply(record.data);
            this.bodyContent[record.id] = content;
        }
        return content;
    },

    onMouseDown: function (e, t) {
        if (t.className == "x-grid3-row-expander") {
            e.stopEvent();
            var row = e.getTarget(".x-grid3-row");
            this.toggleRow(row);
        }
    },

    renderer: function (v, p, record) {
        p.cellAttr = 'rowspan="2"';
        return '<div class="x-grid3-row-expander">&#160;</div>';
    },

    beforeExpand: function (record, body, rowIndex) {
        if (this.fireEvent("beforeexpand", this, record, body, rowIndex) !== false) {
            if (this.tpl && this.lazyRender) {
                body.innerHTML = this.getBodyContent(record, rowIndex);
            }
            return true;
        } else {
            return false;
        }
    },

    toggleRow: function (row) {
        if (typeof row == "number") {
            row = this.grid.view.getRow(row);
        }
        this[Ext.fly(row).hasClass("x-grid3-row-collapsed") ? "expandRow" : "collapseRow"](row);
    },

    expandRow: function (row) {
        if (typeof row == "number") {
            row = this.grid.view.getRow(row);
        }
        var record = this.grid.store.getAt(row.rowIndex);
        var body = Ext.DomQuery.selectNode("tr:nth(2) div.x-grid3-row-body", row);
        if (this.beforeExpand(record, body, row.rowIndex)) {
            this.state[record.id] = true;
            Ext.fly(row).replaceClass("x-grid3-row-collapsed", "x-grid3-row-expanded");
            this.fireEvent("expand", this, record, body, row.rowIndex);
        }
    },

    collapseRow: function (row) {
        if (typeof row == "number") {
            row = this.grid.view.getRow(row);
        }
        var record = this.grid.store.getAt(row.rowIndex);
        var body = Ext.fly(row).child("tr:nth(1) div.x-grid3-row-body", true);
        if (this.fireEvent("beforecollapse", this, record, body, row.rowIndex) !== false) {
            this.state[record.id] = false;
            Ext.fly(row).replaceClass("x-grid3-row-expanded", "x-grid3-row-collapsed");
            this.fireEvent("collapse", this, record, body, row.rowIndex);
        }
    }
});

Ext.grid.CheckColumn = function (config) {
    Ext.apply(this, config);
    if (!this.id) {
        this.id = Ext.id();
    }
    this.renderer = this.renderer.createDelegate(this);
};

Ext.grid.CheckColumn.prototype = {
    init: function(grid) {
        this.grid = grid;
        //this.grid.on("beforerender", function() {            
            var view = this.grid.getView();
            view.mainBody.on("mousedown", this.onMouseDown, this);
        //}, this);
    },

    onMouseDown: function(e, t) {
        if (t.className && t.className.indexOf("x-grid3-cc-" + this.id) != -1) {
            e.stopEvent();
            var record = this.grid.store.getAt(this.grid.getView().findRowIndex(t));
            record.set(this.dataIndex, !record.data[this.dataIndex]);
        }
    },

    renderer: function(v, p, record) {
        p.css += " x-grid3-check-col-td";
        return '<div class="x-grid3-check-col' + (v ? "-on" : "") + " x-grid3-cc-" + this.id + '">&#160;</div>';
    }
};

Ext.grid.TableGrid = function(config) {
    config = config || {};
    Ext.apply(this, config);
    var cf = config.fields || [], ch = config.columns || [];

    if (config.table.isComposite) {
        if (config.table.elements.length > 0) {
            table = Ext.get(config.table.elements[0]);
        }
    }
    else {
        table = Ext.get(config.table);
    }

    var ct = table.insertSibling();

    var fields = [], cols = [];
    var headers = table.query("thead th");
    for (var i = 0, h; h = headers[i]; i++) {
        var text = h.innerHTML;
        var name = "tcol-" + i;

        fields.push(Ext.applyIf(cf[i] || {}, {
            name: name,
            mapping: "td:nth(" + (i + 1) + ")/@innerHTML"
        }));

        cols.push(Ext.applyIf(ch[i] || {}, {
            "header": text,
            "dataIndex": name,
            "width": h.offsetWidth,
            "tooltip": h.title,
            "sortable": true
        }));
    }

    var ds = new Ext.data.Store({
        reader: new Ext.data.XmlReader({
            record: "tbody tr"
        }, fields)
    });

    ds.loadData(table.dom);

    var cm = new Ext.grid.ColumnModel(cols);

    if (config.width || config.height) {
        ct.setSize(config.width || "auto", config.height || "auto");
    } else {
        ct.setWidth(table.getWidth());
    }

    if (config.remove !== false) {
        table.remove();
    }

    Ext.applyIf(this, {
        "ds": ds,
        "cm": cm,
        "sm": new Ext.grid.RowSelectionModel(),
        autoHeight: true,
        autoWidth: false
    });
    Ext.grid.TableGrid.superclass.constructor.call(this, ct, {});
};

Ext.extend(Ext.grid.TableGrid, Ext.grid.GridPanel);

Ext.reg("tablegrid", Ext.grid.TableGrid);

Ext.override(Ext.grid.RowNumberer,{
    renderer: function(v, p, record, rowIndex) {
        if (this.rowspan) {
            p.cellAttr = 'rowspan="' + this.rowspan + '"';
        }

        var so = record.store.lastOptions;
        var sop = so ? so.params : null;
        return ((sop && sop.start) ? sop.start : 0) + rowIndex + 1;
    }
});

Ext.override(Ext.grid.CheckboxSelectionModel, {

    onMouseDown : function(e, t){
        /* 
           * If you want make selections only by checking the checker box,
           add "&& t.className == 'x-grid3-row-checker'" to next if statement
           
           * If you want to make selection only with Ctrl key pressed, add
              "&& e.ctrlKey" to next if statement
        */
        if (e.button === 0 ){ 
            e.stopEvent();
            var row = e.getTarget('.x-grid3-row');
            if(row){
                var index = row.rowIndex;
                if(this.isSelected(index)){
                    if (!this.grid.enableDragDrop){
                      this.deselectRow(index);
                      this.uncheckHeader();
                    }
                    else
                      this.deselectingFlag = true;                        
                }else{
                    if (this.grid.enableDragDrop)
                      this.deselectingFlag = false;
                    this.selectRow(index, true);
                }
            }
        }
    },
    
    handleMouseDown : Ext.emptyFn ,

    uncheckHeader: function() {
        var view = this.grid.getView();
        var t = Ext.fly(view.innerHd).child('.x-grid3-hd-checker');

        var isChecked = t.hasClass('x-grid3-hd-checker-on');
        if (isChecked) {
            t.removeClass('x-grid3-hd-checker-on');
        }
    },

    toggleHeader: function() {
        var view = this.grid.getView();
        var t = Ext.fly(view.innerHd).child('.x-grid3-hd-checker');

        var isChecked = t.hasClass('x-grid3-hd-checker-on');
        if (isChecked) {
            t.removeClass('x-grid3-hd-checker-on');
        } else {
            t.addClass('x-grid3-hd-checker-on');
        }
    },

    checkHeader: function() {
        var view = this.grid.getView();
        var t = Ext.fly(view.innerHd).child('.x-grid3-hd-checker');

        var isChecked = t.hasClass('x-grid3-hd-checker-on');
        if (!isChecked) {
            t.addClass('x-grid3-hd-checker-on');
        }
    }
});

Ext.grid.CheckboxSelectionModel.prototype.initEvents = Ext.grid.CheckboxSelectionModel.prototype.initEvents.createSequence(function() {
    this.grid.on('rowclick', function(grid, rowIndex, e) {
        if (this.deselectingFlag && this.grid.enableDragDrop) {
            this.deselectingFlag = false;
            this.deselectRow(rowIndex);
            this.uncheckHeader();
        }
    }, this);
});

Ext.ux.NumericPagingToolbar = function() {
    Ext.ux.NumericPagingToolbar.superclass.constructor.apply(this, arguments);
}

Ext.extend(Ext.ux.NumericPagingToolbar, Ext.PagingToolbar, {
    onRender: function(el, position) {
        Ext.PagingToolbar.prototype.onRender.apply(this, arguments);
        this.preceding = [];
        for (var i = 0; i < 3; i++) {
            this.preceding[i] = this.insertButton(i + 4, {
                handler: this.onPageNumberClick,
                scope: this
            });
            this.preceding[i].el.setVisibilityMode(Ext.Element.DISPLAY);
            this.preceding[i].el.child(".x-btn-text").setStyle({
                "font-weight": "bold",
                "color": "#083772"
            });
        }
        this.following = [];
        for (var i = 0; i < 3; i++) {
            this.following[i] = this.insertButton(i + 8, {
                handler: this.onPageNumberClick,
                scope: this
            });
            this.following[i].el.setVisibilityMode(Ext.Element.DISPLAY);
            this.following[i].el.child(".x-btn-text").setStyle({
                "font-weight": "bold",
                "color": "#083772"
            });
        }
    },

    onPageNumberClick: function(b, e) {
        var pageNum = parseInt(b.el.child(".x-btn-text").dom.innerHTML, 10);
        var d = this.getPageData();
        if (!isNaN(pageNum) && (pageNum > 0) && (pageNum <= d.pages)) {
            this.field.dom.value = pageNum;
            pageNum--;
            this.store.load({ params: { start: pageNum * this.pageSize, limit: this.pageSize} });
        }
    },

    updateInfo: function() {
        Ext.PagingToolbar.prototype.updateInfo.apply(this, arguments);
        var d = this.getPageData();
        var p = d.activePage - 3;
        for (var i = 0; i < 3; i++, p++) {
            if (p < 1) {
                this.preceding[i].el.hide();
            } else {
                this.preceding[i].el.show();
                this.preceding[i].el.child(".x-btn-text").dom.innerHTML = p;
            }
        }
        p = d.activePage + 1;
        for (var i = 0; i < 3; i++, p++) {
            if (p > d.pages) {
                this.following[i].el.hide();
            } else {
                this.following[i].el.show();
                this.following[i].el.child(".x-btn-text").dom.innerHTML = p;
            }
        }
    }
});

Ext.reg("numpaging", Ext.ux.NumericPagingToolbar);

if(typeof Sys!=="undefined"){Sys.Application.notifyScriptLoaded();}