/**
 *
 * Umożliwia wyświetlanie ostrzeżeń przy polach edycji
 * @author Piotr Krokowski
 * @copyright LGPL
 * @version 1.0
 * @since 20.12.2007
 * 
 * Wykorzystanie: uruchamianie metody markWarning(msg) na polu Ext.form.Field
 */

Ext.override(Ext.form.Field,  {
	warningClass : "x-form-warning",
	/**
     * Mark this field as invalid
     * @param {String} msg The validation message
     */
    markWarning : function(msg){
    	try {
	        if(!this.rendered || this.preventMark){ // not rendered
	            return;
	        }
	        this.hasWarning = true;
	        this.el.addClass(this.warningClass);
	        msg = msg || this.invalidText;
	        switch(this.msgTarget){
	            case 'qtip':
	                this.el.dom.qtip = msg;
	                this.el.dom.qclass = 'x-form-warning-tip';
	                if(Ext.QuickTips){ // fix for floating editors interacting with DND
	                    Ext.QuickTips.enable();
	                }
	                break;
	            case 'title':
	                this.el.dom.title = msg;
	                break;
	            case 'under':
	                if(!this.errorEl){
	                    var elp = this.el.findParent('.x-form-element', 5, true);
	                    this.errorEl = elp.createChild({cls:'x-form-warning-msg'});
	                    this.errorEl.setWidth(elp.getWidth(true)-20);
	                }
	                this.errorEl.update(msg);
	                Ext.form.Field.msgFx[this.msgFx].show(this.errorEl, this);
	                break;
	            case 'side':
	                if(!this.warningIcon){
	                    var elp = this.el.findParent('.x-form-element', 5, true);
	                    this.warningIcon = elp.createChild({cls:'x-form-warning-icon'});
	                }
	                this.alignWarningIcon();
	                this.warningIcon.dom.qtip = msg;
	                this.warningIcon.dom.qclass = 'x-form-warning-tip';
	                this.warningIcon.show();
	                this.on('resize', this.alignWarningIcon, this);
	                break;
	            default:
	                var t = Ext.getDom(this.msgTarget);
	                t.innerHTML = msg;
	                t.style.display = this.msgDisplay;
	                break;
	        }
	        this.fireEvent('warning', this, msg);
    	} catch (e) {
    		console.warn(e);
    	}
    },
    markInvalid : function(msg){
    	try {
	        if(!this.rendered || this.preventMark){ // not rendered
	            return;
	        }
	        this.el.addClass(this.invalidClass);
	        msg = msg || this.invalidText;
	        switch(this.msgTarget){
	            case 'qtip':
	                this.el.dom.qtip = msg;
	                this.el.dom.qclass = 'x-form-invalid-tip';
	                if(Ext.QuickTips){ // fix for floating editors interacting with DND
	                    Ext.QuickTips.enable();
	                }
	                break;
	            case 'title':
	                this.el.dom.title = msg;
	                break;
	            case 'under':
	                if(!this.errorEl){
	                    var elp = this.el.findParent('.x-form-element', 5, true);
	                    this.errorEl = elp.createChild({cls:'x-form-invalid-msg'});
	                    this.errorEl.setWidth(elp.getWidth(true)-20);
	                }
	                this.errorEl.update(msg);
	                Ext.form.Field.msgFx[this.msgFx].show(this.errorEl, this);
	                break;
	            case 'side':
	                if(!this.errorIcon){
	                    var elp = this.el.findParent('.x-form-element', 5, true);
	                    this.errorIcon = elp.createChild({cls:'x-form-invalid-icon'});
	                }
	                this.alignErrorIcon();
	                this.errorIcon.dom.qtip = msg;
	                this.errorIcon.dom.qclass = 'x-form-invalid-tip';
	                this.errorIcon.show();
	                this.on('resize', this.alignErrorIcon, this);
	                break;
	            default:
	                var t = Ext.getDom(this.msgTarget);
	                t.innerHTML = msg;
	                t.style.display = this.msgDisplay;
	                break;
	        }
	        this.fireEvent('invalid', this, msg);
        } catch (e) {
    		console.warn(e);
    	}
    },
    clearInvalid : function(){
    	if (this.hasWarning) {
    		this.hasWarning = false;
    		return ;
    	}
    	this.clearWarning();
        if(!this.rendered || this.preventMark){ // not rendered
            return;
        }
        this.el.removeClass(this.invalidClass);
        switch(this.msgTarget){
            case 'qtip':
                this.el.dom.qtip = '';
                break;
            case 'title':
                this.el.dom.title = '';
                break;
            case 'under':
                if(this.errorEl){
                    Ext.form.Field.msgFx[this.msgFx].hide(this.errorEl, this);
                }
                break;
            case 'side':
                if(this.errorIcon){
                    this.errorIcon.dom.qtip = '';
                    this.errorIcon.hide();
                    this.errorIcon.remove();
                    this.un('resize', this.alignErrorIcon, this);
                    delete this.errorIcon;
                }
                break;
            default:
                var t = Ext.getDom(this.msgTarget);
                t.innerHTML = '';
                t.style.display = 'none';
                break;
        }
        this.fireEvent('valid', this);
    },
     clearWarning : function(){
     	this.hasWarning = false;
        if(!this.rendered || this.preventMark){ // not rendered
            return;
        }
        this.el.removeClass(this.warningClass);
        switch(this.msgTarget){
            case 'qtip':
                this.el.dom.qtip = '';
                this.el.dom.qclass = '';
                break;
            case 'title':
                this.el.dom.title = '';
                break;
            case 'under':
                if(this.errorEl){
                    Ext.form.Field.msgFx[this.msgFx].hide(this.errorEl, this);
                }
                break;
            case 'side':
                if(this.warningIcon){
                    this.warningIcon.dom.qtip = '';
                    this.warningIcon.hide();
                    this.warningIcon.remove();
                    this.un('resize', this.alignWarningIcon, this);
                    delete this.warningIcon;
                }
                break;
            default:
                var t = Ext.getDom(this.msgTarget);
                t.innerHTML = '';
                t.style.display = 'none';
                break;
        }
        this.fireEvent('valid', this);
    },
    reset : function(){
        this.setValue(this.originalValue);
        this.clearWarning();
        this.clearInvalid();
    },
    setValue : function(v){
        this.value = v;
        if(this.rendered){
            this.el.dom.value = (v === null || v === undefined ? '' : v);
            this.validate();
        }
    },
    alignWarningIcon : function(){
        //this.warningIcon.alignTo(this.el, 'tl-tr', [2, 0]);
    },
    alignErrorIcon : function(){
        //this.errorIcon.alignTo(this.el, 'tl-tr', [2, 0]);
    }
});



