if (Garmin == undefined) var Garmin = {};

Ext.form.InlineTextField = function(config) {
	Ext.form.InlineTextField.superclass.constructor.call(this, config);
 
	this.on('specialkey', function(f, e) {
		if (e.getKey() == e.ESC) {
			f.setValue(this.startValue);
			f.blur();
		}
	}, this);
		
};
 
Ext.extend(Ext.form.InlineTextField, Ext.form.TextField, {
	inlineClass: 'x-form-inline-field',
	disabledClass: 'x-form-inline-field-disabled',
	saveOnlyOnChange: true,
	confirmSave: false,
	confirmText: 'The data has been successfully saved.',
 
	doSave : function() {
		var cfg = this.autoSave;
 
		this.params = (this.name || this.id) + '=' + this.getValue();
 
		if (typeof cfg == 'object') {
			this.method = cfg.method || 'POST';
			this.callback = (!cfg.callback) ? {success: Ext.emptyFn, failure: Ext.emptyFn} :
				{success: cfg.callback.success || cfg.callback, failure: cfg.callback.failure || Ext.emptyFn};
			this.scope = cfg.scope || this.callback;
 
			if (this.confirmSave === true) {
				var success = function() {
					Ext.MessageBox.alert('Success', this.confirmText);
				}.createDelegate(this);
 
				this.callback.success = success.createSequence(this.callback.success);
			}
 
			var p = (cfg.params) ? cfg.params : '';
 
			if (p) {
				if (typeof p == 'object') {
					this.params += '&' + Ext.urlEncode(p);
				}
				else if (typeof p == 'string' && p.length) {
					this.params += '&' + p;
				}
			}
 
			this.url = (this.method == 'POST') ? cfg.url : cfg.url + '?' + this.params;
		}
		else if (typeof cfg == 'string') {
			this.method = 'POST';
			this.url = (this.method == 'POST') ? cfg : cfg + '?' + this.params;
		}
 
		Ext.Ajax.request({
			url: this.url,
			method: this.method,
			success: this.callback.success,
			failure: this.callback.failure,
			params: this.params,
			scope: this.scope
		});
	},
 
	reset : function() {
		Ext.form.TextField.superclass.reset.call(this);
 
		if (this.value) {
			this.setRawValue(this.value);
		}
		else if (this.emptyText && this.getRawValue().length < 1) {
			this.setRawValue(this.emptyText);
			this.el.addClass(this.emptyClass);
		}
	}
});
 
Ext.override(Ext.form.InlineTextField, {
	onRender : Ext.form.TextField.prototype.onRender.createSequence(function() {
		this.el.addClass(this.inlineClass);
 
		if (this.editable === false) {
			this.disabled = true;
		}
	}),
 
	onFocus : Ext.form.TextField.prototype.onFocus.createSequence(function() {
		if (this.editable !== false) {
			this.el.removeClass(this.inlineClass);
		}
	}),
 
	onBlur : Ext.form.TextField.prototype.onBlur.createSequence(function() {
		if (this.isValid() && !this.el.hasClass(this.inlineClass)) {
			this.el.addClass(this.inlineClass);
 
			if (this.autoSave && (this.saveOnlyOnChange === false || this.getValue() != this.startValue)) {
				this.doSave();
			}
		}
	})
});

Ext.form.InlineTextArea = function(config) {
	Ext.form.InlineTextArea.superclass.constructor.call(this, config);
 
	this.on('specialkey', function(f, e) {
		if (e.getKey() == e.ESC) {
			f.setValue(this.startValue);
			f.blur();
		}
	}, this);
};
 
Ext.extend(Ext.form.InlineTextArea, Ext.form.TextArea, {
	inlineClass: 'x-form-inline-field',
	disabledClass: 'x-form-inline-field-disabled',
	saveOnlyOnChange: true,
	confirmSave: false,
	confirmText: 'The data has been successfully saved.',
 
	doSave : function() {
		var cfg = this.autoSave;
 
		this.params = (this.name || this.id) + '=' + this.getValue();
 
		if (typeof cfg == 'object') {
			this.method = cfg.method || 'POST';
			this.callback = (!cfg.callback) ? {success: Ext.emptyFn, failure: Ext.emptyFn} :
				{success: cfg.callback.success || cfg.callback, failure: cfg.callback.failure || Ext.emptyFn};
			this.scope = cfg.scope || this.callback;
 
			if (this.confirmSave === true) {
				var success = function() {
					Ext.MessageBox.alert('Success', this.confirmText);
				}.createDelegate(this);
 
				this.callback.success = success.createSequence(this.callback.success);
			}
 
			var p = (cfg.params) ? cfg.params : '';
 
			if (p) {
				if (typeof p == 'object') {
					this.params += '&' + Ext.urlEncode(p);
				}
				else if (typeof p == 'string' && p.length) {
					this.params += '&' + p;
				}
			}
 
			this.url = (this.method == 'POST') ? cfg.url : cfg.url + '?' + this.params;
		}
		else if (typeof cfg == 'string') {
			this.method = 'POST';
			this.url = (this.method == 'POST') ? cfg : cfg + '?' + this.params;
		}
 
		Ext.Ajax.request({
			url: this.url,
			method: this.method,
			success: this.callback.success,
			failure: this.callback.failure,
			params: this.params,
			scope: this.scope
		});
	},
 
	reset : function() {
		Ext.form.TextField.superclass.reset.call(this);
 
		if (this.value) {
			this.setRawValue(this.value);
		}
		else if (this.emptyText && this.getRawValue().length < 1) {
			this.setRawValue(this.emptyText);
			this.el.addClass(this.emptyClass);
		}
	}
});
 
Ext.override(Ext.form.InlineTextArea, {
	onRender : Ext.form.TextArea.prototype.onRender.createSequence(function() {
		this.el.addClass(this.inlineClass);
 
		if (this.editable === false) {
			this.disabled = true;
		}
	}),
 
	onFocus : Ext.form.TextArea.prototype.onFocus.createSequence(function() {
		if (this.editable !== false) {
			this.el.removeClass(this.inlineClass);
		}
	}),
 
	onBlur : Ext.form.TextArea.prototype.onBlur.createSequence(function() {
		if (this.isValid() && !this.el.hasClass(this.inlineClass)) {
			this.el.addClass(this.inlineClass);
 
			if (this.autoSave && (this.saveOnlyOnChange === false || this.getValue() != this.startValue)) {
				this.doSave();
			}
		}
	})
});

Garmin.DynamicInlineArea = function(config) {
	Garmin.DynamicInlineArea.superclass.constructor.call(this, config);
};

Ext.extend(Garmin.DynamicInlineArea, Ext.form.InlineTextArea, {
	shrinkedSize: 20,
	defaultValue: "Click here to edit the description..."
});

Garmin.DynamicInlineArea.prototype.onRender = Ext.form.InlineTextArea.prototype.onRender.createSequence (function() {
	this.el.setHeight(this.shrinkedSize);
});

Garmin.DynamicInlineArea.prototype.onFocus = Ext.form.InlineTextArea.prototype.onFocus.createSequence (function() {
	this.el.setHeight(this.growMin);
});

Garmin.DynamicInlineArea.prototype.onBlur = Ext.form.InlineTextArea.prototype.onBlur.createSequence (function() {
	this.el.setHeight(this.shrinkedSize);
	if (this.getValue() == '') {
		this.setValue(this.defaultValue);
	}
});

Ext.form.InlineComboBox = function(config) {
	Ext.form.InlineComboBox.superclass.constructor.call(this, config);
};
 
Ext.extend(Ext.form.InlineComboBox, Ext.form.ComboBox, {
	inlineClass: 'x-form-inline-field',
	disabledClass: 'x-form-inline-field-disabled',
	saveOnlyOnChange: true,
	confirmSave: false,
	confirmText: 'The data has been successfully saved.',
 
	doSave : function() {
		var cfg = this.autoSave;
 
		this.params = (this.name || this.id) + '=' + this.getRawValue();
 
		if (this.hiddenName) {
			this.params += '&' + this.hiddenName + '=' + this.hiddenField.value;
		}
 
		if (typeof cfg == 'object') {
			this.method = cfg.method || 'POST';
			this.callback = (!cfg.callback) ? {success: Ext.emptyFn, failure: Ext.emptyFn} :
				{success: cfg.callback.success || cfg.callback, failure: cfg.callback.failure || Ext.emptyFn};
			this.scope = cfg.scope || this.callback;
 
			if (this.confirmSave === true) {
				var success = function() {
					Ext.MessageBox.alert('Success', this.confirmText);
				}.createDelegate(this);
 
				this.callback.success = success.createSequence(this.callback.success);
			}
 
			var p = (cfg.params) ? cfg.params : '';
 
			if (p) {
				if (typeof p == 'object') {
					this.params += '&' + Ext.urlEncode(p);
				}
				else if (typeof p == 'string' && p.length) {
					this.params += '&' + p;
				}
			}
 
			this.url = (this.method == 'POST') ? cfg.url : cfg.url + '?' + this.params;
		}
		else if (typeof cfg == 'string') {
			this.method = 'POST';
			this.url = (this.method == 'POST') ? cfg : cfg + '?' + this.params;
		}
 
		Ext.Ajax.request({
			url: this.url,
			method: this.method,
			success: this.callback.success,
			failure: this.callback.failure,
			params: this.params,
			scope: this.scope
		});
	}
});
 
Ext.override(Ext.form.InlineComboBox, {
	onRender : Ext.form.ComboBox.prototype.onRender.createSequence(function() {
		this.lastValue = '';
		this.lastRawValue = '';
		this.el.addClass(this.inlineClass);
		this.trigger.setDisplayed(false);
	}),
 
	onFocus : Ext.form.ComboBox.prototype.onFocus.createSequence(function() {
		this.el.removeClass(this.inlineClass);
		//this.trigger.setDisplayed(true);
	}),
 
	triggerBlur : Ext.form.ComboBox.prototype.triggerBlur.createSequence(function() {
		if (this.isValid() && !this.el.hasClass(this.inlineClass)) {
			this.el.addClass(this.inlineClass);
			this.trigger.setDisplayed(false);
 
			if (!this.store.getAt(this.view.getSelectedIndexes()[0]) || !this.getRawValue()) {
				this.selectedIndex = -1;
				if (this.hiddenField) {
					this.hiddenField.value = '';
				}
				this.value = '';
			}
 
			if (this.autoSave && (this.saveOnlyOnChange === false || this.getValue() != this.lastValue || this.getRawValue() != this.lastRawValue)) {
				this.doSave();
			}
 
			this.lastValue = this.getValue();
			this.lastRawValue = this.getRawValue();
		}
	})
});
