Ext.orig = {};

Ext.orig.ComboBox = {};
Ext.orig.ComboBox.onRender = Ext.form.ComboBox.prototype.onRender;
Ext.orig.ComboBox.onKeyUp = Ext.form.ComboBox.prototype.onKeyUp;

Ext.override(Ext.form.ComboBox,
{
    onRender: function(ct, position)
    {
        var ret = Ext.orig.ComboBox.onRender.call(this, ct, position);
        if (this.name && this.hiddenName && this.editable) {
            this.el.dom.setAttribute('name', this.name);
        }

        return ret;
    },

    onKeyUp: function(e)
    {
        var ret = Ext.orig.ComboBox.onKeyUp.call(this, e);
        Ext.form.ComboBox.superclass.onKeyUp.call(this, e);

        return ret;
    }
});

Ext.orig.BasicForm = {};
Ext.orig.BasicForm.submit = Ext.form.BasicForm.prototype.submit;
Ext.orig.BasicForm.load = Ext.form.BasicForm.prototype.load;

Ext.override(Ext.form.BasicForm,
{
    submit: function(options)
    {
        this.method = 'post';
        return Ext.orig.BasicForm.submit.call(this, options);
    },

    load: function(options)
    {
        this.method = 'get';
        return Ext.orig.BasicForm.load.call(this, options);
    }
});

Ext.apply(Date.parseCodes,
{
    j: {
        g: 1,
        c: "d = parseInt(results[{0}], 10);\n",
        s: "(\\d{1,2})" // day of month without leading zeroes (1 - 31)
    },
    M: function()
    {
        for (var a = [], i = 0; i < 12; a.push(Date.getShortMonthName(i)), ++i); // get localised short month names
        return Ext.applyIf({
            s: "(" + a.join("|") + ")"
        }, Date.formatCodeToRegex("F"));
    },
    n: {
        g :1,
        c :"m = parseInt(results[{0}], 10) - 1;\n",
        s :"(\\d{1,2})" // month number without leading zeros (1 - 12)
    },
    o: function()
    {
        return Date.formatCodeToRegex("Y");
    },
    g: function()
    {
        return Date.formatCodeToRegex("G");
    },
    h: function()
    {
        return Date.formatCodeToRegex("H");
    },
    P: function()
    {
        return Ext.applyIf({
            s: "([+\-]\\d{2}:\\d{2})" // GMT offset in hrs and mins (with colon separator)
        }, Date.formatCodeToRegex("O"));
    }
});

Date.formatCodeToRegex = function(character, currentGroup)
{
    // Note: currentGroup - position in regex result array (see notes for Date.parseCodes above)
    var p = Date.parseCodes[character];

    if (p) {
        p = Ext.type(p) == 'function' ? p() : p;
        Date.parseCodes[character] = p; // reassign function result to prevent repeated execution
    }

    return p ? Ext.applyIf({
        c: p.c ? String.format(p.c, currentGroup || "{0}") : p.c
    }, p) : {
        g: 0,
        c: null,
        s: Ext.escapeRe(character) // treat unrecognised characters as literals
    }
};

Ext.override(Ext.form.ComboBox, {
    setValue : function(v){
//      Store not loaded yet? Set value when it *is* loaded.
        if (this.store && this.store.getCount() == 0) {
            this.store.on('load', this.setValue.createDelegate(this, [v]), null, {single: true});
            return;
        }
        var text = v;
        if(this.valueField){
            var r = this.findRecord(this.valueField, v);
            if(r){
                text = r.data[this.displayField];
            }else if(this.valueNotFoundText !== undefined){
                text = this.valueNotFoundText;
            }
        }
        this.lastSelectionText = text;
        if(this.hiddenField){
            this.hiddenField.value = v;
        }
        Ext.form.ComboBox.superclass.setValue.call(this, text);
        this.value = v;
    }
});
