﻿/**
* All Ext Overrides to add or fix functionality
* If a particular class already has an override, add your new or extended feature to the override
* Be very careful if your are overriding an existing method
*/

// Ext.form.BasicForm =============================================================================
Ext.override(Ext.form.BasicForm, 
	{
		//a clearDirty overide to give dirty reset ability
		clearDirty: function () 
		{
			function handleField(fld)
			{
				if (fld.isFormField)
				{
					if (fld.isComposite && fld.rendered) { fld.items.each(handleField); }
					else { fld.originalValue = String(fld.getValue()); }
				}
			}
			
			this.items.each(handleField);
		}
	});

// Ext override for cell align, valign and styles =================================================
Ext.layout.TableLayout.prototype.renderItem = function(c, position, target)
{
	if(c && !c.rendered)
	{
		var td=this.getNextCell(c), 
				el=Ext.get(td);

		if (c.align) { el.applyStyles("text-align:"+c.align); }
		if (c.valign) { el.applyStyles("vertical-align:"+c.valign); }
		if (c.tdStyle) { el.applyStyles(c.tdStyle); }
		c.render(td);
		if(this.extraCls)
		{
			var t = c.getPositionEl ? c.getPositionEl() : c;
			t.addClass(this.extraCls);
		}
	}
};

/** AV.FormPanel ===================================================================================
	* Adds Centralized field validation error recording and a fieldvalidation event to Ext.form.FormPanel
*/
Ext.namespace('AV');
AV.FormPanel = Ext.extend(Ext.form.FormPanel, {
	
	initComponent: function() {
		
		this.errorFields = new Ext.util.MixedCollection();

		AV.FormPanel.superclass.initComponent.call(this);

		this.addEvents('fieldvalidation');
	}, 

	errorPush: function(key, obj) {
		if (this.errorFields.containsKey(key)) { this.errorFields.replace(key, obj); }
		else { this.errorFields.add(key, obj); }

		this.fireEvent('fieldvalidation', this, this.errorFields, this.errorFields.getCount());
	},

	errorRemove: function(key) {
		var obj = this.errorFields.removeKey(key);
		if (obj) { this.fireEvent('fieldvalidation', this, this.errorFields, this.errorFields.getCount()); }
	}
});
Ext.reg('avform', AV.FormPanel);


/** Ext override for Ext.form.field ===================================================================================
	* getParentForm is a new method
	* validateValue is an override and is backwards compatible
*/
Ext.override(Ext.form.Field, {
	
	getParentForm	: function() {
		var frm = this.ownerCt, 
				cnt = 0;

		while (cnt < 5 && frm) {
			if (!frm.errorFields) { frm = frm.ownerCt; }
			else { break; }
			cnt++;
		}
		return frm;
	},

	validateValue : function(value) {
	  var errors, error, frm;

		if (this.getErrors) {
			errors	= this.getErrors(value);
			error		= errors[0];
		}
		frm			= this.getParentForm();

    if (error == undefined) { 
			if (frm && frm.errorRemove) { frm.errorRemove(this.id); }
			return true; 
		}

		if (frm && frm.errorPush) { frm.errorPush(this.id, { id: this.id, name: this.name, errors: errors } ); }
    this.markInvalid(error);
    return false;
  }, 

	unsetActiveError: function(suppressEvent) {
		var frm	= this.getParentForm();

		delete this.activeError;
		if (frm && frm.errorRemove) { frm.errorRemove(this.id); }
		if (suppressEvent !== true) this.fireEvent('valid', this);
	}

});

/** AV.ComboBox ===================================================================================
	* OverRides search (doQuery) to perform a wildcard search
*/
AV.ComboBox = Ext.extend(Ext.form.ComboBox, {

	doQuery: function(q, forceAll) {
		if(q === undefined || q === null){ q = ''; }
		var qe = {
			query: q,
			forceAll: forceAll,
			combo: this,
			cancel:false
		};

		if(this.fireEvent('beforequery', qe)===false || qe.cancel){ return false; }
		q = qe.query;
		forceAll = qe.forceAll;

		if(forceAll === true || (q.length >= this.minChars))
		{
			if(this.lastQuery !== q)
			{
				this.lastQuery = q;
				if(this.mode === 'local')
				{
					this.selectedIndex = -1;
					if (forceAll) { this.store.clearFilter(); } 

					// this is the difference between the Ext version, passing true as the 3rd parameter 
					// matches any part of the the search string
					else { this.store.filter(this.displayField, q, true); }

					this.onLoad();
				}
				else
				{
					this.store.baseParams[this.queryParam] = q;
					this.store.load({ params: this.getParams(q) });
					this.expand();
				}
			}
			else
			{
				this.selectedIndex = -1;
				this.onLoad();
			}
		}
	}
});
Ext.reg('avcombobox', AV.ComboBox);

/** Ext.Window ===================================================================================
	* Add startdrag and enddrag events to Ext Window
*/
Ext.override(Ext.Window, {
	initComponent : Ext.Window.prototype.initComponent.createSequence(function() {
    this.addEvents('startdrag', 'enddrag');
	})
});

Ext.override(Ext.Window.DD, {
	startDrag : Ext.Window.DD.prototype.startDrag.createInterceptor(function() {
		this.win.fireEvent("startdrag");
		return true;
	}),
	endDrag : Ext.Window.DD.prototype.endDrag.createSequence(function() {
    this.win.fireEvent("enddrag");
	})
});
