Ext.namespace("Ext.ux.plugins");

Ext.ux.plugins.FieldValidatePlugin = function(config) {
	var field;
	this.init = function(f){
		field = f;
		field.markInvalid = markInvalid;
		field.clearInvalid = clearInvalid;
	}
    function markInvalid(msg){
        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:
            	this.clearInvalid();
            	var validators = OM.ajax.getParentForm(this.el.dom)._validators;
            	for (var n = 0; n < validators.length; n++) {
			        if(validators[n]._id == this.el.id){
			        	var validator = validators[n];
			        	if (validator._display) {
					        var el = document.getElementById(validator._display);
					        if (el) {
					            if (!el.innerHTML) {
					                el.style.display = "";
					                el.innerHTML = msg;
					            }
					        }
					    }
			        }
			    }
                break;
        }
        this.fireEvent('invalid', this, msg);
    }
    
    function clearInvalid(){
    	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.un('resize', this.alignErrorIcon, this);
                }
                break;
            default:
            	var validators = OM.ajax.getParentForm(this.el.dom)._validators;
            	for (var n = 0; n < validators.length; n++) {
			        if(validators[n]._id == this.el.id){
			        	var validator = validators[n];
			        	if (validator._display) {
					        var el = document.getElementById(validator._display);
					        if (el) {
					            el.style.display = "none";
					            el.innerHTML = "";
					        }
					    }
			        }
			    }
                break;
        }
        this.fireEvent('valid', this);
    }
};

