/*
Ext.override(Ext.layout.FormLayout, {
	renderItem : function(c, position, target){
		if(c && !c.rendered && c.isFormField && c.inputType != 'hidden'){
			var args = [
				   c.id, c.fieldLabel,
				   c.labelStyle||this.labelStyle||'',
				   this.elementStyle||'',
				   typeof c.labelSeparator == 'undefined' ? this.labelSeparator : c.labelSeparator,
				   (c.itemCls||this.container.itemCls||'') + (c.hideLabel ? ' x-hide-label' : ''),
				   c.clearCls || 'x-form-clear-left' 
			];
			if(typeof position == 'number'){
				position = target.dom.childNodes[position] || null;
			}
			if(position){
				c.formItem = this.fieldTpl.insertBefore(position, args, true);
			}else{
				c.formItem = this.fieldTpl.append(target, args, true);
			}

//          Remove the form layout wrapper on Field destroy.
			c.on('destroy', c.formItem.remove, c.formItem, {single: true});
			c.on('hide', c.formItem.hide, c.formItem, {single: false});
			c.on('show', c.formItem.show, c.formItem, {single: false});
			c.render('x-form-el-'+c.id);
		}else {
			Ext.layout.FormLayout.superclass.renderItem.apply(this, arguments);
		}
	}
});
Ext.override(Ext.form.BasicForm, {
  setValues : function(values){
	if(values instanceof Array){             
		for(var i = 0, len = values.length; i < len; i++){
			var v = values[i];
			var f = this.findField(v.id);
			if(f){
				if ( f.getEl().dom.type == 'radio' ) {
					var group = this.el.dom.elements[f.getName()];
					for (var i=0; i < group.length; i++ ) {
						if(group[i].__ext_field) {
							group[i].__ext_field.setValue(group[i].value == v);
							if(this.trackResetOnLoad){
								group[i].__ext_field.originalValue = group[i].__ext_field.getValue();
							}
						}
					}
				}
				else
				{
					f.setValue(v.value);
					if(this.trackResetOnLoad){
						f.originalValue = f.getValue();
					}
				}
			}
		}
	}else{
		var field, id;
		for(id in values){
			if(typeof values[id] != 'function' && (field = this.findField(id))){
				if( field.getEl().dom.type == 'radio' ) {
					var group = this.el.dom.elements[field.getName()];
					for (var i=0; i < group.length; i++ ) {
						if(group[i].__ext_field) {
							group[i].__ext_field.setValue(group[i].value == values[id]);
							if(this.trackResetOnLoad){
								group[i].__ext_field.originalValue = group[i].__ext_field.getValue();
							}
						}
					}
				}
				else
				{
					field.setValue(values[id]);
					if(this.trackResetOnLoad){
						field.originalValue = field.getValue();
					}
				}
			}
		}
	}
	return this;
  }
});
*/
/* square bracket fix */
Ext.DomQuery.matchers[2].re = /^(?:([\[\{])(?:@)?([\w-]+)\s?(?:(=|.=)\s?(?:"(.*?)"|'(.*?)'|(.*?)))?[\]\}])/;

Ext.override(Ext.form.Radio, {
	getGroupValue : function(){
		var c = this.getParent().child('input[name="'+this.el.dom.name+'"]:checked', true);
		return c ? c.value : null;
	},
	toggleValue : function() {
		if(!this.checked){
			var els = this.getParent().select('input[name="'+this.el.dom.name+'"]');
			els.each(function(el){
				if(el.dom.id == this.id){
					this.setValue(true);
				}else{
					Ext.getCmp(el.dom.id).setValue(false);
				}
			}, this);
		}
	},
	setValue : function(v){
		if(typeof v=='boolean') {
			Ext.form.Radio.superclass.setValue.call(this, v);
		}else{
			var r = this.getParent().child('input[name="'+this.el.dom.name+'"][value="'+v+'"]', true);
			if(r && !r.checked){
				Ext.getCmp(r.id).toggleValue();
			};
		}
	}
});

/*
Ext.override(Ext.tree.TreeEventModel, {
	delegateClick : function(e, t){
		if(!this.beforeEvent(e)){
			return;
		}
		if(e.getTarget('input[type="checkbox"]', 1)){
			this.onCheckboxClick(e, this.getNode(e));
		}
		else if(e.getTarget('.x-tree-ec-icon', 1)){
			this.onIconClick(e, this.getNode(e));
		}
		else if(this.getNodeTarget(e)){
			this.onNodeClick(e, this.getNode(e));
		}
	}
});


Ext.override(Ext.form.BasicForm,{
	load : function(options){
		//default load options
		var _options = {
			failure: function(form, action){
				if(form.errorReader){
					var rs = form.errorReader.read(action.response);
					var errors = [];
					if(rs.records){
						for(var i = 0, len = rs.records.length; i < len; i++) {
							var r = rs.records[i];
							errors[i] = r.data;
						}
					}
					if(errors.length < 1){
						errors = null;
					}
					action.result = {
						success : rs.success,
						errors : errors
					};
				}else{
					action.result =  Ext.decode(action.response.responseText);
				}
			}
		}
		//specific load options
		Ext.apply(_options, options);

		//do the load
		this.doAction('load', _options);
		return this;
	} 	
});

*/



//From Condor:
Ext.override(Ext.form.Field, {
	afterRender: Ext.form.Field.prototype.afterRender.createSequence(function(){
		if(this.qtip){
			var target = this.getTipTarget();
			if(typeof this.qtip == 'object'){
				Ext.QuickTips.register(Ext.apply({
					  target: target
				}, this.qtip));
			} else {
				target.dom.qtip = this.qtip;
			}
		}
	}),
	getTipTarget: function(){
		return this.el;
	}
});
//checkboxes and radios, the main element is a hidden input.
Ext.override(Ext.form.Checkbox, {
	getTipTarget: function(){
		return this.imageEl;
	}
});
/*
Ext.Panel.override({
	setAutoScroll: function() {
		if (this.rendered && this.autoScroll) {
			var el = this.body || this.el;
			if (el) {
				el.setOverflow('auto');
				// Following line required to fix autoScroll
				el.dom.style.position = 'relative';
			}
		}
	}
});
*/
Ext.apply(Ext.form.DateField.prototype.menuListeners, {
	  select: function(m, d) {
	    this.setValue(d);
	    this.fireEvent('select', this, d);
	  }
	});
	
Ext.override(Ext.form.Field, {
  setFieldLabel : function(text) {
	if (this.rendered) {
	  this.el.up('.x-form-item', 10, true).child('.x-form-item-label').update(text);
	}
	this.fieldLabel = text;
  }
});

Ext.override(Ext.form.TextArea, {
  myCounter:null,
  trigger:null,
  useCounter:false,
  onRender : function(ct, position){
		if(!this.el){
			this.defaultAutoCreate = {
				tag: "textarea",
				style:"width:100px;height:60px;",
				autocomplete: "off"
			};
		}
		Ext.form.TextArea.superclass.onRender.call(this, ct, position);
		if(this.grow){
			this.textSizeEl = Ext.DomHelper.append(document.body, {
				tag: "pre", cls: "x-form-grow-sizer"
			});
			if(this.preventScrollbars){
				this.el.setStyle("overflow", "hidden");
			}
			this.el.setHeight(this.growMin);
		}

		if(this.useCounter){
			this.enableKeyEvents=true;
			this.myCounter = this.el.wrap({cls: "x-form-field-wrap"});
			this.trigger = this.myCounter.createChild({tag: "div",id:"counter_"+this.id,text:"aass","class":"x-form-field-counter"},this.el);
			this.trigger.update(this.getValue().length,false);
			this.on("keyup", this.onKeyUp2, this, {preventDefault:false});
		}
	},
	onKeyUp2:function(){
		if(this.useCounter)
		{
			if(this.maxLength != false)
			{
				if(this.getValue().length >= this.maxLength)
				{ 
					this.setValue( this.getValue().slice(0, this.getValue().length-(this.getValue().length-this.maxLength)) );
				}
			}
		}
		this.trigger.update(this.getValue().length);
	}
});


Ext.apply(Ext.util.Format, {
		/*
		 * @param {Number} v The number to format.
		 * @param {String} format The way you would like to format this text.
		 * @return {String} The formatted number.
		 */
		number: function(v, format) {
			if(!format){
				return v;
			}
			v = Ext.num(v, NaN);
			if (isNaN(v)){
				return '';
			}
			var comma = ',',
				dec = '.',
				i18n = false,
				neg = v < 0;

			v = Math.abs(v);
			if(format.substr(format.length - 2) == '/i'){
				format = format.substr(0, format.length - 2);
				i18n = true;
				comma = '.';
				dec = ',';
			}

			var hasComma = format.indexOf(comma) != -1,
				psplit = (i18n ? format.replace(/[^\d\,]/g, '') : format.replace(/[^\d\.]/g, '')).split(dec);

			if(1 < psplit.length){
				v = v.toFixed(psplit[1].length);
			}else if(2 < psplit.length){
				throw ('NumberFormatException: invalid format, formats should have no more than 1 period: ' + format);
			}else{
				v = v.toFixed(0);
			}

			var fnum = v.toString();

			psplit = fnum.split('.');

			if (hasComma) {
				var cnum = psplit[0], parr = [], j = cnum.length, m = Math.floor(j / 3), n = cnum.length % 3 || 3;

				for (var i = 0; i < j; i += n) {
					if (i != 0) {
						n = 3;
					}
					parr[parr.length] = cnum.substr(i, n);
					m -= 1;
				}
				fnum = parr.join(comma);
				if (psplit[1]) {
					fnum += dec + psplit[1];
				}
			} else {
				if (psplit[1]) {
					fnum = psplit[0] + dec + psplit[1];
				}
			}

			return (neg ? '-' : '') + format.replace(/[\d,?\.?]+/, fnum);
		},


/**
		 * Returns a number rendering function that can be reused to apply a number format multiple times efficiently
		 * @param {String} format Any valid number format string for {@link #number}
		 * @return {Function} The number formatting function
		 */
		numberRenderer : function(format){
			return function(v){
				return Ext.util.Format.number(v, format);
			};
		}
})
