Ext.define("IceCap2.ux.form.field.sqlSearchBox", {

	extend: "Ext.form.field.ComboBox",

	xtype: "DDSEARCH",

	minChars : 3,

	maxLength : 10,

	valueField: "KEY",

	displayField:"VALUE",

	forceSelection: false,

	typeAhead: false,

	pageSize: 10,

	config: {
		sqlVoucher: "",
		searchMode: false
	},

	listeners: {
		keydown: {
			fn: function(i_field, i_event){

				var b_search = i_field.getSearchMode();

				if (b_search && i_event.getKey() == Ext.event.Event.UP){
					i_event.stopEvent();
				}
			}
		},
		focus: {
			fn: function(i_field, i_record, n_number){
				var i_field = this;
				i_field.setSearchMode(false);
			}
		},
		blur: {
			fn: function(i_field, i_record, n_number){
				var i_field = this;
				i_field.setSearchMode(false);
			}
		},
		beforequery: {
			fn: function(i_field, i_record, n_number){
				var i_field = this;
				i_field.setSearchMode(false);
			}
		},
		select: {
			fn: function(i_field, i_record, n_number){

				var i_field = this;
				i_field.setSearchMode(false);

				var a_fields = i_record.getFields();

				Ext.Array.each(a_fields, function(i_field) {

					var i_element = document.getElementsByName(i_field.name);

					if (i_element) {
						var value = i_record.get(i_field.name);
						f[0].value = vale;
					}
				});
			}
		}
	},

	initComponent:function() {

		var i_self = this;
		var i_store = i_self.createStore();

		i_self.store  = i_store;
		i_self.tpl = i_self.createTemplate()

		i_self.callParent();
	},

	createTemplate: function() {

		var i_template = new Ext.XTemplate(
			'<tpl for=".">',
				'<div class="x-form-field">',
					'{VALUE}',
				'</div>',
			'</tpl>'
		);

		return i_template;
	},

	createStore: function() {

		var i_self = this;

		var i_store = Ext.create("Ext.data.Store", {
			fields: [
				{
					name: "KEY",
					type: "auto"
				},
				{
					name: "VALUE",
					type: "auto"
				}
			],
			proxy: {
				url: "/menu/DDSQL.aspx?runSql=runsql&sqlVoucher=" + i_self.getSqlVoucher(),
				type: "ajax",
				reader: {
					type: "json",
				}
			}
		});

		return i_store;
	}
});
