﻿
Ext.override(Ext.form.Field, {
    showContainer: function() {
        this.enable();
        this.show();
        if (this.getEl() != null)
            this.getEl().up('.x-form-item').setDisplayed(true); // show entire container and children (including label if applicable)
    },

    hideContainer: function() {
        this.disable(); // for validation
        this.hide();
        if (this.getEl() != null)
            this.getEl().up('.x-form-item').setDisplayed(false); // hide container and children (including label if applicable)
    },

    setContainerVisible: function(visible) {
        if (visible) {
            this.showContainer();
        } else {
            this.hideContainer();
        }
        return this;
    }
});



// Combobox drop down list has incorrect width in Firefox (bug AS-24)
Ext.override(Ext.form.ComboBox, {
    initList: function() {
        if (!this.list) {
            var cls = 'x-combo-list';

            this.list = new Ext.Layer({
                shadow: this.shadow, cls: [cls, this.listClass].join(' '), constrain: false
            });

            // >>> FIX
            if (0 == this.wrap.getWidth())
                this.wrap.setWidth(this.el.getWidth() + this.trigger.getWidth());
            // <<< FIX
            var lw = this.listWidth || Math.max(this.wrap.getWidth(), this.minListWidth);
            this.list.setWidth(lw);
            this.list.swallowEvent('mousewheel');
            this.assetHeight = 0;

            if (this.title) {
                this.header = this.list.createChild({ cls: cls + '-hd', html: this.title });
                this.assetHeight += this.header.getHeight();
            }

            this.innerList = this.list.createChild({ cls: cls + '-inner' });
            this.innerList.on('mouseover', this.onViewOver, this);
            this.innerList.on('mousemove', this.onViewMove, this);
            this.innerList.setWidth(lw - this.list.getFrameWidth('lr'));

            if (this.pageSize) {
                this.footer = this.list.createChild({ cls: cls + '-ft' });
                this.pageTb = new Ext.PagingToolbar({
                    store: this.store,
                    pageSize: this.pageSize,
                    renderTo: this.footer
                });
                this.assetHeight += this.footer.getHeight();
            }

            if (!this.tpl) {
                this.tpl = '<tpl for="."><div class="' + cls + '-item">{' + this.displayField + '}</div></tpl>';
            }

            /**
            * The {@link Ext.DataView DataView} used to display the ComboBox's options.
            * @type Ext.DataView
            */
            this.view = new Ext.DataView({
                applyTo: this.innerList,
                tpl: this.tpl,
                singleSelect: true,
                selectedClass: this.selectedClass,
                itemSelector: this.itemSelector || '.' + cls + '-item'
            });

            this.view.on('click', this.onViewClick, this);

            this.bindStore(this.store, true);

            if (this.resizable) {
                this.resizer = new Ext.Resizable(this.list, {
                    pinned: true, handles: 'se'
                });
                this.resizer.on('resize', function(r, w, h) {
                    this.maxHeight = h - this.handleHeight - this.list.getFrameWidth('tb') - this.assetHeight;
                    this.listWidth = w;
                    this.innerList.setWidth(w - this.list.getFrameWidth('lr'));
                    this.restrictHeight();
                }, this);
                this[this.pageSize ? 'footer' : 'innerList'].setStyle('margin-bottom', this.handleHeight + 'px');
            }
        }
    }
});

// Combobox drop down arrow is hidden if combobox is disabled in IE6 (bug AS-23)
Ext.override(Ext.form.TriggerField, {
    onDisable: function() {
        Ext.form.TriggerField.superclass.onDisable.call(this);
        // >>> FIX
        this.wrap.setWidth(this.el.getWidth() + this.trigger.getWidth());
        // <<< FIX
        if (this.wrap) {
            this.wrap.addClass('x-item-disabled');
        }
    }
});


Ext.override(Ext.grid.GridView, {
    // private
    handleHdOver: function(e, t) {
        var hd = this.findHeaderCell(t);
        if (hd && !this.headersDisabled) {
            this.activeHd = hd;
            this.activeHdIndex = this.getCellIndex(hd);
            var fly = this.fly(hd);
            this.activeHdRegion = fly.getRegion();
            if (!this.cm.isMenuDisabled(this.activeHdIndex)) {
                fly.addClass("x-grid3-hd-over");
                this.activeHdBtn = fly.child('.x-grid3-hd-btn');
                if (this.activeHdBtn && hd.firstChild.offsetHeight > 0) {
                    this.activeHdBtn.dom.style.height = (hd.firstChild.offsetHeight - 1) + 'px';
                }
            }
        }
    }
});


// Get parameter from url
Ext.getUrlParam = function(param) {
    var params = Ext.urlDecode(location.search.substring(1));
    return param ? params[param] : params;
};


Ext.lib.Ajax.handleTransactionResponse = function(o, callback, isAbort) {

    if (!callback) {
        this.releaseObject(o);
        return;
    }

    var httpStatus, responseObject;

    try {
        if (o.conn.status !== undefined && o.conn.status != 0) {
            httpStatus = o.conn.status;
        }
        else {
            httpStatus = 13030;
        }
    }
    catch (e) {


        httpStatus = 13030;
    }

    if (httpStatus >= 200 && httpStatus < 300) {

        if (o.conn.responseText && o.conn.responseText.indexOf('{ redirect:') == 0) {
        
            var responseObject = Ext.decode(o.conn.responseText)
            if (responseObject && responseObject.redirect) {
                window.location.href = responseObject.redirect;
            }
        }

        responseObject = this.createResponseObject(o, callback.argument);
        if (callback.success) {
            if (!callback.scope) {
                callback.success(responseObject);
            }
            else {


                callback.success.apply(callback.scope, [responseObject]);
            }
        }
    }
    else {
        switch (httpStatus) {

            case 12002:
            case 12029:
            case 12030:
            case 12031:
            case 12152:
            case 13030:
                responseObject = this.createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false));
                if (callback.failure) {
                    if (!callback.scope) {
                        callback.failure(responseObject);
                    }
                    else {
                        callback.failure.apply(callback.scope, [responseObject]);
                    }
                }
                break;
            default:
                responseObject = this.createResponseObject(o, callback.argument);
                if (callback.failure) {
                    if (!callback.scope) {
                        callback.failure(responseObject);
                    }
                    else {
                        callback.failure.apply(callback.scope, [responseObject]);
                    }
                }
        }
    }

    this.releaseObject(o);
    responseObject = null;
};
