/**
 * This file contains overrides functions of Ext JS
 *
 * @author Interface
 * @created 12-07-2009
 * @version 1.0
 * 		
 */

/*
 * Setting the value of a combobox using setValue() function
 */

Ext.override(Ext.form.ComboBox, {
	setValue : function(v){		
		// Store not loaded yet? Set value when it *is* loaded.
		// Defer the setValue call until after the next load.
		if (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;
	}
});


//======================= Start of extention ============================
    
    /* Use this override to add the uxDisplayOnly method to Ext.form.Field
     * All descendants of Ext.form.Field will then inherit this method
     * Version 1.0
     * Known bug: Doesnt work with radiogroup and checkbox group in IE7 - the this.el.removeClass(cls)
     * results in the radiogroup or checkbox group becoming invisible
    */
    
    Ext.override(Ext.form.Field, {
	    uxDisplayOnly: function(displayOnly, cls) {
	        // If no parameter, assume true
	        var displayOnly = (displayOnly === false)? false : true;
	
	        // If a class name is passed in, use that, otherwise use the default one.
	        // The classes are defined in displayOnly.html for this example 
	        var cls = (cls)? cls : 'x-form-display-only-field';
	
	        // Add or remove the class
	        if (displayOnly) {
	            this.el.addClass(cls);
	        } else {
	            this.el.removeClass(cls);
	        }
	
	        // Set the underlying DOM element's readOnly attribute
	        this.el.dom.readOnly = displayOnly;
	
	        // Get this field's xtype (ie what kind of field is it?)
	        var xType = this.getXType();
	
	        if (xType == 'checkbox') {
	            this.readOnly = displayOnly; // We need to also set the config readOnly attribute for checkboxes
	        }
	
	        // For radio groups and checkbox groups 
	        // we need to set the config readOnly attribute for on each item in the group
	        if (xType == 'radiogroup' || xType == 'checkboxgroup') {
	            var items = this.items.items;
	            for (var i = 0; i < items.length; i++) {
	                items[i].readOnly = displayOnly;
	            }
	        }
	
	        // For fields that have triggers (eg date,time,dateTime), 
	        // show/hide the trigger
	        if (this.trigger) {
	            this.trigger.setDisplayed(!displayOnly);
	        }
	
	        if (this.isXType('combo') && this.keyNav) {
	            this.keyNav[displayOnly? 'disable' : 'enable']();
	        }
	    }
	});  
    //======================= End of extention ============================
	
/**
 * Date override : - used to date conversion to UTC or from UTC
 */
Ext.override(Date, {
    toUTC : function() {
                        // Convert the date to the UTC date
        return this.add(Date.MINUTE, this.getTimezoneOffset());
    },
    
    fromUTC : function() {
                        // Convert the date from the UTC date
        return this.add(Date.MINUTE, -this.getTimezoneOffset());
    }    
});

/**
 * TextField override : - used to remove issue in IE and Safari, 
 * to mask special characters as defined in vtype.
 */
Ext.override(Ext.form.TextField, {
    filterKeys : function(e){
        var k = e.getKey();
        if(Ext.isGecko && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
            return;
        }
        var c = e.getCharCode(), cc = String.fromCharCode(c);
        if(!Ext.isGecko && e.isSpecialKey() && !cc){
            return;
        }
        if(!this.maskRe.test(cc)){
            e.stopEvent();
        }
    }
});
