/*
 * 730-2009
 * Copyright(c) 2006, Andrea Bruglia, Davide Stefanini.
 *
 * This code is licensed under BSD license. Use it as you wish,
 * but keep this copyright intact.
 */
/**
 * @author Davide Stefanini, Andrea Bruglia
 */

Ext.override(Ext.form.Field, {
    /* 
     * showMessageInfo è la funzione che permettere di far visualizzare l'help nei field
     * di default non viene visualizzato basta settare nella configurazione del
     * costruttore la proprietà "showMsg: true" e "filterMsg:'ALL'||'DB'||'USER'"
     * la funzione che visualizza la finestra sta nel file message.js
     */
    //stripCharsRe : /\,/g ,
    
    escludi: false,		// Proprietà che permette di escludere il campo quando si prendono i valori tramite getValuesFromForm()
	disabledClass: 'x-item-disabled-custom',
	//changeOnSet: false,
	resetOnDisable: false,
	
    showMessageInfo: function(){
        getMessage(this.name, this.filterMsg, this.id);
    },
    
    onFocus: Ext.form.Field.prototype.onFocus.createInterceptor(function(){
        this.showMsg = (this.showMsg == undefined || this.showMsg === true) ? true : this.showMsg;
        this.filterMsg = (Ext.isEmpty(this.filterMsg)) ? 'DB' : this.filterMsg;
        if (this.showMsg) 
            this.showMessageInfo();
    }),
    
    onDestroy: Ext.Button.prototype.onDestroy.createInterceptor(function(){
        myWindowMgr.each(function(items){
            items.close();
            lastField = undefined;
        })
    }),
    
    /* Metodo per cambiare la fieldLabel runtime */
    setFieldLabel: function(text){
		if (this.rendered) {
        	this.el.up('.x-form-item', 10, true).child('.x-form-item-label').update(text);
		}
    },
    
    /* Metodo per visualizzare un campo nascosto a runtime */
    showContainer: function(){
		if (this.rendered) {
			this.enable();
			this.show();
			this.getEl().up('.x-form-item').setDisplayed(true); // show entire container and children (including label if applicable)
		}
    },
    
    /* Metodo per nascondere un campo a runtime */
    hideContainer: function(){
		if (this.rendered) {
	        this.disable(); // for validation
	        this.hide();
	        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;
    },
    
    /* Fix per il getName() del Field */
    getName: function(){
        return this.rendered && this.el.dom.name ? this.el.dom.name : (this.name || this.id);
    },
	
	afterRender : Ext.form.Field.prototype.afterRender.createSequence(function(){
		var qt = this.qtip;
	    if (qt) {
			Ext.apply(qt, {
				target:  this,
				//showDelay: qt.showDelay || 5,
				enabled: true
			})
			
			Ext.QuickTips.register(qt);
			
			/*Ext.QuickTips.register({
		        target:  this,
		        title: qt.title || '',
		        text: qt.text || '',
		        enabled: true,
		        showDelay: 20
	    	});*/
	    }
		
		if (this.ptip) {
			this.setPersistentTooltip(this.ptip);
			
			/*var div = document.createElement("div");
	        div.setAttribute("id", this.id+"-ptip");
			this.el.appendChild(div);
			
			Ext.apply(this.ptip, {
				target: div,
				targetXY: this.el.getXY()
			})
			
			this.ptipInstance = new Ext.QuickTip(this.ptip)
			
			this.on({
				'focus': {
					fn: function(){
						this.ptipInstance.show()
						this.ptipInstance.getEl().alignTo(this.el, 'bl-tl')
					},
					scope: this
				},
				'blur': {
					fn: function(){
						this.ptipInstance.hide();
					},
					scope: this
				}
			})*/
		}
	}),
	
	clearInvalid: Ext.form.Field.prototype.clearInvalid.createSequence(function(){
		var qt = this.qtip;
		if (qt) {
			Ext.apply(qt, {
				target:  this,
				//showDelay: qt.showDelay || 5,
				enabled: true
			})
			
			Ext.QuickTips.register(qt);
			
			/*Ext.QuickTips.register({
		        target:  this,
		        title: qt.title || '',
		        text: qt.text || '',
		        enabled: true,
		        showDelay: 20
	    	});*/
	    }
	}),
	
	markInvalid: Ext.form.Field.prototype.markInvalid.createInterceptor(function(msg){
		if (this.qtip) {
			Ext.QuickTips.unregister(this);
		}
	}),
	
	disable: Ext.form.Field.prototype.disable.createInterceptor(function(){
		this.suspendEvents();
		this.clearInvalid();
		this.resumeEvents();
		if (this.resetOnDisable) {
			this.reset();
		}
	}),
	
	setPersistentTooltip: function(ptipConf){
		if (this.rendered) {
			var div = document.createElement("div");
	        div.setAttribute("id", this.id+"-ptip");
			try{	// try catch perchè schifo explorer va in errore
				this.el.appendChild(div);
			}catch(e){}
			
			Ext.apply(ptipConf, {
				target: div,
				autoHide: false,
				targetXY: this.el.getXY()
			})
			
			this.removePersistentTooltip();
			this.ptipInstance = new Ext.QuickTip(ptipConf)
			
			this.ptipFocus = function(){
				this.ptipInstance.show()
				this.ptipInstance.getEl().alignTo(this.el, (ptipConf.align || 'bl-tl'))
			};
			this.ptipBlur = function(){
				this.ptipInstance.hide.defer((ptipConf.hideDelay || 0), this.ptipInstance);
			}
			
			this.on({
				'focus': {
					fn: this.ptipFocus,
					scope: this
				},
				'blur': {
					fn: this.ptipBlur,
					scope: this
				},
				'destroy': {
					fn: this.ptipBlur,
					scope: this
				},
				'hide': {
					fn: this.ptipBlur,
					scope: this
				},
				'disable': {
					fn: this.ptipBlur,
					scope: this
				}
			})
		}
		this.ptip = ptipConf;
	},
	
	removePersistentTooltip: function(){
		if (this.ptipInstance) {
			Ext.QuickTips.unregister(this.id+"-ptip");
			delete this.ptipInstance;
			delete this.ptip;
			this.un('blur', this.ptipBlur);
			this.un('focus', this.ptipFocus);
			this.un('destroy', this.ptipFocus);
			this.un('hide', this.ptipFocus);
			this.un('disable', this.ptipFocus);
		}
	}
	
	//** Lancia l'evento change se necessario **//
	/*setValue:  Ext.form.Field.prototype.setValue.createSequence(function(value){
		if (this.changeOnSet) this.fireEvent('change', this);
	})*/
});

/*
 * Setta openDate al 1 Gennaio dell'anno di dichiarazione, e restringe il campo di valori al solo anno di dichiarazione
 */
Ext.override(Ext.ux.Namirial.form.DateField, {
	openDate: new Date(ANNO_DICHIARAZIONE, 0, 1),
	minValue: MIN_DATA_DICHIARAZIONE,
	maxValue: MAX_DATA_DICHIARAZIONE
});

Ext.override(Ext.ux.Namirial.form.NumberField, {
	maxValue: 99999999,
	allowNegative: false
});

Ext.override(Ext.form.Checkbox, {
	afterRender : Ext.form.Checkbox.prototype.afterRender.createSequence(function(){
		var qt = this.qtip;
	    if (qt) {
			try{
				var idInnerWrap = Ext.getDom(this.innerWrap.id);
				var checkBox = idInnerWrap.getElementsByClassName('x-form-check')[0];
				
				/*Ext.apply(qt, {
					target:  checkBox,
					//showDelay: qt.showDelay || 5,
					enabled: true
				})*/
				
				//Ext.QuickTips.register(qt);
				Ext.QuickTips.register({
			        target:  checkBox,
			        title: qt.title || '',
			        text: qt.text || '',
			        enabled: true,
			        showDelay: 5
		    	})
			}catch(e){
				log.warn('Errore checkbox con Explorer');
			}
	    }
	})
})

/**
 * Override temporaneo per disabilitare i campi contenuti quando un pannello viene disabilitato
 */
Ext.override(Ext.Panel, {
	disableInternalFields: false,
	
	disable: Ext.Panel.prototype.disable.createSequence(function(){
		if (this.disableInternalFields) {
			this.disableFields();
		}
	}),
	
	enable: Ext.Panel.prototype.enable.createSequence(function(){
		if (this.disableInternalFields) {
			this.enableFields();
		}
	}),
	
	disableFields : function(){
		this.cascade(function(){
			var tipo = this.getXType();
			switch (tipo) {
				case 'checkbox':
				case 'namirial_checkbox':
				case 'combo':
				case 'namirial_combo':
				case 'datefield':
				case 'namirial_datefield':
				case 'field':
				case 'htmleditor':
				case 'numberfield':
				case 'namirial_numberfield':
				case 'radio':
				case 'textarea':
				case 'textfield':
				case 'namirial_textfield':
				case 'timefield':
				case 'trigger':
					this.disable();
			}
		})
	},
	
	enableFields : function(){
		this.cascade(function(){
			var tipo = this.getXType();
			switch (tipo) {
				case 'checkbox':
				case 'namirial_checkbox':
				case 'combo':
				case 'namirial_combo':
				case 'datefield':
				case 'namirial_datefield':
				case 'field':
				case 'htmleditor':
				case 'numberfield':
				case 'namirial_numberfield':
				case 'radio':
				case 'textarea':
				case 'textfield':
				case 'namirial_textfield':
				case 'timefield':
				case 'trigger':
					this.enable();
			}
		})
	},
	
	setFieldsDisabled : function(state){
		this[(state ? "disable" : "enable")+"Fields"]();
	}
})

Ext.override(Ext.Window, {
	modal: true
})
