Ext.define("Ext.ux.grid.filter.util", {
	singleton: true,

	requires: [
		"Ext.ux.grid.filter.Base",
		"Ext.ux.grid.filter.Array",
		"Ext.ux.grid.filter.Boolean",
		"Ext.ux.grid.filter.Date",
		"Ext.ux.grid.filter.Entity",
		"Ext.ux.grid.filter.Enum",
		"Ext.ux.grid.filter.Number",
		"Ext.ux.grid.filter.String"
	],

	create: function(config) {

		var i_self = this;
		var i_panel = i_self.createPanel(config);

		return i_panel;
	},

	createFilter: function(o_config) {

		var i_self = this;
		var i_button;

		switch (o_config.dataType){
			case "string":
				i_button = Ext.widget("filterstring", o_config);
			break;
			case "int":
			case "float":
			case "decimal":
			case "number":
				i_button = Ext.widget("filternumber", o_config);
			break;
			case "enum":
				i_button = Ext.widget("filterenum", o_config);
			break;
			case "boolean":
				i_button = Ext.widget("filterboolean", o_config);
			break;
			case "entity":
				i_button = Ext.widget("filterentity", o_config);
			break;
			case "date":
				i_button = Ext.widget("filterdate", o_config);
			break;
			case "array":
				i_button = Ext.widget("filterarray", o_config);
			break;
		}
		return i_button;
	},

	createPanel: function (config) {

		var i_self = this;
		var size;

		switch (config.dataType) {
			case "string":
				size = {
					width: 380
				};
			break;
			case "int":
			case "float":
			case "decimal":
			case "number":
				size = {
					width: 380
				};
			break;
			case "date":
				if (config.dataDateType == "datetime" || config.dataDateType == "datetimesec") {
					size = {
						width: 600
					};
				}
				else {
					size = {
						width: 500
					};
				}
			break;
			case "boolean":
				size = {
					width: 380
				};
			break;
			case "enum":
				size = {
					width: 380
				};
			break;
			case "entity":
				size = {
					width: 380
				};
			break;
			case "array":
				size = {
					width: 380
				};
			break;
		}

		var a_checkbox = i_self.GenerateCheckBoxes(config);
		var a_fields = i_self.GenerateFields(config);

		var a_containers = [
			i_self.GenerateCheckBoxNot(config)
		];


		Ext.Array.each(a_checkbox, function(i_checkBox, n_index) {

			var i_field = a_fields[n_index];
			i_field.flex = 1;
			i_field.margin = "0 0 0 10";

			var i_fieldcontainer = Ext.widget("fieldcontainer", {
				layout: {
					type: "hbox",
					align: "stretch"
				},
				items: [
					i_checkBox,
					i_field
				]
			});

			a_containers.push(i_fieldcontainer);
		});

		if (config.dataType == "string") {
			a_containers.push(i_self.GenerateCheckBoxBlank(config));
		}

		var i_toolbar = Ext.widget("toolbar", {
			dock: "bottom",
			ui: "footer",
			items: [
				{
					xtype: "button",
					disabled: false,
					text: ip2.i18n({
						da: "Anvend",
						en: "Apply"
					}),
					dataIndex: config.dataIndex,
					dataType: config.dataType,
					handler: function (item) {
						var i_menu = item.up("menu");
						var i_form = i_menu.down("form");
						var a_fields = i_form.query("field");
						var b_valid = true;
						Ext.Array.each(a_fields, function (i_field) {
							if (!i_field.isValid()) {
								b_valid = false;
							}
						});
						if (b_valid) {
							i_menu.hide();
							config.applyFn();
						}
					}
				},
				"->",
				{
					xtype: "button",
					disabled: !config.overRuleable ? true : false,
					text: ip2.i18n({
						da: "Fjern",
						en: "Remove"
					}),
					handler: function (item) {
						var i_menu = item.up("menu");
						i_menu.hide();
						config.removeFn(i_menu.button);
					}
				},
			]
		});

		var i_panel = Ext.widget("form", {
			border: false,
			bodyBorder: false,
			width: size.width,
			layout: {
				type: "vbox",
				align: "stretch"
			},
			bodyPadding: 10,
			items: a_containers
		});

		i_panel.addDocked(i_toolbar);

		return i_panel;
	},

	GenerateCheckBoxes: function (config) {

		var i_self = this;
		var items = [];

		switch (config.dataType) {
			case "string":
				items.push(i_self.GenerateCheckBox(config, "like"));
				items.push(i_self.GenerateCheckBox(config, "eq"));
			break;
			case "number":
				items.push(i_self.GenerateCheckBox(config, "lt"));
				items.push(i_self.GenerateCheckBox(config, "gt"));
				items.push(i_self.GenerateCheckBox(config, "eq"));
				items.push(i_self.GenerateCheckBox(config, "bt"));
			break;
			case "date":
				items.push(i_self.GenerateCheckBox(config, "gt"));
				items.push(i_self.GenerateCheckBox(config, "eq"));
				items.push(i_self.GenerateCheckBox(config, "bt"));
			break;
			case "boolean":
				items.push(i_self.GenerateCheckBox(config, "eq"));
			break;
			case "enum":
				if (config.useLike) {
					items.push(i_self.GenerateCheckBox(config, "like"));
					items.push(i_self.GenerateCheckBox(config, "eq"));
				}
				else {
					items.push(i_self.GenerateCheckBox(config, "eq"));
				}

			break;
			case "array":
				items.push(i_self.GenerateCheckBox(config, "eq"));

			break;
			case "entity":
				items.push(i_self.GenerateCheckBox(config, "eq"));
			break;
		}
		config.checkboxes = items;
		return items;
	},

	GenerateCheckBox: function (config, criteria) {

		var i_self = this;
		var o_chkConfig;

		if (criteria == "like") {
			o_chkConfig = {
				fieldLabel: ip2.i18n({
					da: "Indholder",
					en: "Like"
				}),
				criteria: "like '%{0}%'",
				inputValue: ""
			};
		}
		if (criteria == "eq") {
			o_chkConfig = {
				fieldLabel: ip2.i18n({
					da: "Lige med",
					en: "Equal to"
				}),
				criteria: "= {0}",
				inputValue: ""
			};
		}
		if (criteria == "gt") {
			o_chkConfig = {
				fieldLabel: ip2.i18n({
					da: "Større end",
					en: "Greater than"
				}),
				criteria: "> {0}",
				inputValue: ""
			};
			if (config.dataType == "date") {
				o_chkConfig.fieldLabel = ip2.i18n({
					da: "Efter",
					en: "After"
				});
			}
		}
		if (criteria == "lt") {
			o_chkConfig = {
				fieldLabel: ip2.i18n({
					da: "Mindre end",
					en: "Less than"
				}),
				criteria: "< {0}",
				inputValue: ""
			};
			if (config.dataType == "date") {
				o_chkConfig.fieldLabel = ip2.i18n({
					da: "Før",
					en: "Before"
				});
			}
		}
		if (criteria == "bt") {
			o_chkConfig = {
				fieldLabel: ip2.i18n({
					da: "Mellem",
					en: "Between"
				}),
				criteria: "between {0} and {1}",
				inputValue: ""

			};
		}
		Ext.Object.merge(o_chkConfig, {
			dataType: config.dataType,
			dateType: config.dataDateType,
			type: criteria,
			allowChange: true
		});

		o_chkConfig.listeners = {
			change: {
				fn: function (item, newValue, oldValue) {
					var i_menu = item.up("menu");
					var button = i_menu.button;
					if (item.allowChange) {
						i_self.HandleCheckChange(button.config, item.type, newValue);
					}
				}
			}
		};

		o_chkConfig.isFormField = false;
		var i_checkBox = Ext.widget("checkboxfield", o_chkConfig);

		return i_checkBox;
	},

	GenerateFields: function(config) {

		var i_self = this;
		var a_items = [];

		switch (config.dataType) {
			case "string":
				a_items.push(i_self.GenerateField(config, "like"));
				a_items.push(i_self.GenerateField(config, "eq"));
			break;
			case "number":
				a_items.push(i_self.GenerateField(config, "lt"));
				a_items.push(i_self.GenerateField(config, "gt"));
				a_items.push(i_self.GenerateField(config, "eq"));
				a_items.push(i_self.GenerateField(config, "bt"));
			break;
			case "enum":
			case "entity":
			case "boolean":
				if (config.useLike) {
					a_items.push(i_self.GenerateField(config, "like"));
					a_items.push(i_self.GenerateField(config, "eq"));
				}
				else {
					a_items.push(i_self.GenerateField(config, "eq"));
				}
			break;
			case "array":
				a_items.push(i_self.GenerateField(config, "eq"));
			break;
			case "date":
				a_items.push(i_self.GenerateDateTimeField(config, "gt"));
				a_items.push(i_self.GenerateDateTimeField(config, "eq"));
				a_items.push(i_self.GenerateDateTimeField(config, "bt"));
			break;
		}

		if (a_items.length == config.checkboxes.length) {
			for (var i = 0; i < a_items.length; i++) {
				var f = a_items[i];
				var c = config.checkboxes[i];
				if (config.dataDateType != "datetime" && config.dataDateType != "datetimesec") {
					if (config.dataType != "date") {
						if (f.fieldtype != "bt") {
							f.checkBox = c;
							i_self.AddListener(f, config);
						}
						else {
							var f1 = f.items.items[0];
							var f2 = f.items.items[1];

							f1.checkBox = c;
							f2.checkBox = c;

							i_self.AddListener(f1, config);
							i_self.AddListener(f2, config);
						}
					}
					else {
						if (f.fieldtype != "bt") {
							var i_datefield = f.getDateField();
							i_datefield.checkBox = c;
							i_self.AddListener(i_datefield, config);
						}
						else {
							var fc1 = f.items.items[0];
							var fc2 = f.items.items[1];

							var i_datefield1 = fc1.getDateField();
							var i_datefield2 = fc2.getDateField();

							i_datefield1.checkBox = c;
							i_datefield2.checkBox = c;

							i_self.AddListener(i_datefield1, config);
							i_self.AddListener(i_datefield2, config);
						}
					}
				}
				else {
					if (f.fieldtype != "bt") {
						var i_datefield = f.getDateField();
						var i_timefield = f.getTimeField();

						i_datefield.checkBox = c;
						i_timefield.checkBox = c;

						i_self.AddListener(i_datefield, config);
						i_self.AddListener(i_timefield), config;
					}
					else {
						var fc1 = f.items.items[0];
						var fc2 = f.items.items[1];

						var i_datefield1 = fc1.getDateField();
						var i_timefield1 = fc1.getTimeField();

						var i_datefield2 = fc2.getDateField();
						var i_timefield2 = fc2.getTimeField();

						i_datefield1.checkBox = c;
						i_timefield1.checkBox = c;

						i_datefield2.checkBox = c;
						i_timefield2.checkBox = c;

						i_self.AddListener(i_datefield1, config);
						i_self.AddListener(i_timefield1, config);

						i_self.AddListener(i_datefield2, config);
						i_self.AddListener(i_timefield2, config);
					}
				}
			}
		}
		config.fields = a_items;
		return a_items;
	},

	GenerateField: function(config, type) {

		var i_self = this;
		var o_field;
		var labelAlign = "left";
		var i_field;

		var i_tpl = Ext.create("Ext.XTemplate",
			"<tpl for=\".\">",
				"<li role=\"option\" class=\"x-boundlist-item\">",
					"{[this.renderer(values.value)]}",
				"</li>",
			"</tpl>",
			{
				renderer: function(s_key) {
					return ip2.i18n(s_key) + " ";
				}
			}
		);

		var i_displayTpl = Ext.create("Ext.XTemplate",
			"<tpl for=\".\">",
				"{[this.renderer(values.value)]}",
			"</tpl>",
			{
				renderer: function(s_key) {
					return ip2.i18n(s_key) + " ";
				}
			}
		);

		switch (type) {
			case "like":
				if (config.dataType == "enum") {

					o_field = {
						xtype: "combobox",
						anchor: "100%",
						readOnly: true,
						labelAlign: labelAlign,
						name: "cmbEqual",
						forceSelection: true,
						editable: false,
						multiSelect: config.multiSelect || false,
						valueField: "key",
						tpl: i_tpl,
						displayTpl: i_displayTpl,
						queryMode: "local",
						triggerAction: "all",
						store: {
							model: Ext.define(Ext.id(), {
								extend: "Ext.data.Model",
								fields: [
									{ name: "key" },
									{ name: "value" }
								]
							}),
							autoDestroy: true,
							autoLoad: true,
							proxy: {
								data: config.values,
								type: "memory"
							}
						},
						listeners: {
							afterrender: {
								fn: function (item) {
									var value = item.getValue();
									if (value == null) {
										item.setValue(item.store.getAt(0).get("key"));
									}
								}
							}
						}
					};
				}
				else {
					o_field = {
						anchor: "100%",
						labelAlign: labelAlign,
						name: "fieldLike",
						readOnly: true
					};
				}
			break;
			case "lt":
				o_field = {
					anchor: "100%",
					labelAlign: labelAlign,
					selectOnFocus: true,
					name: "fieldLess",
					readOnly: true
				};
			break;
			case "gt":
				o_field = {
					anchor: "100%",
					labelAlign: labelAlign,
					selectOnFocus: true,
					name: "fieldGreater",
					readOnly: true
				};
			break;
			case "eq":
				if (config.dataType == "enum") {
					o_field = {
						xtype: "combobox",
						anchor: "100%",
						readOnly: true,
						labelAlign: labelAlign,
						name: "cmbEqual",
						forceSelection: true,
						editable: false,
						multiSelect: config.multiSelect || false,
						valueField: "key",
						tpl: i_tpl,
						displayTpl: i_displayTpl,
						queryMode: "local",
						triggerAction: "all",
						store: {
							model: Ext.define(Ext.id(), {
								extend: "Ext.data.Model",
								fields: [
									{ name: "key" },
									{ name: "value" }
								]
							}),
							autoDestroy: true,
							autoLoad: true,
							proxy: {
								data: config.values,
								type: "memory"
							}
						},
						listeners: {
							afterrender: {
								fn: function (item) {
									var value = item.getValue();
									if (value == null) {
										item.setValue(item.store.getAt(0).get("key"));
									}
								}
							}
						}
					};
				}
				else if (config.dataType == "entity") {
					o_field = {
						anchor: "100%",
						dmd: config.dmd,
						readOnly: true,
						labelAlign: labelAlign,
						forceSelection: true,
						queryMode: "remote",
						triggerAction: "all"
					};
				}
				else if (config.dataType == "array") {
					o_field = {
						anchor: "100%",
						dmd: config.dmd,
						readOnly: true,
						labelAlign: labelAlign,
						forceSelection: true,
						queryMode: "remote",
						triggerAction: "all",
						useRaw: config.useRaw || true,
						multiSelect: true,
						typeAhead: false,
						listeners: {
							afterrender: {
								fn: function (item) {
									var value = item.getValue();
									if (value == null) {
										item.setValue(item.store.getAt(0));
									}
								}
							}
						}
					};
				}
				else if (config.dataType == "boolean") {
					o_field = {
						xtype: "combobox",
						anchor: "100%",
						readOnly: true,
						labelAlign: labelAlign,
						name: "cmbEqual",
						forceSelection: true,
						displayField: "value",
						editable: false,
						valueField: "key",
						listConfig: {
							getInnerTpl: function () {
								return "<div><SPAN>{value}</SPAN></div>";
							}
						},
						queryMode: "local",
						triggerAction: "all",
						store: {
							model: Ext.define(Ext.id(), {
								extend: "Ext.data.Model",
								fields: [
									{ name: "key" },
									{ name: "value" }
								]
							}),
							autoDestroy: true,
							autoLoad: true,
							proxy: {
								data: [{
									key: 1,
									value: ip2.i18n({
										da: "Ja",
										en: "Yes"
									})
								},
								{
									key: 0,
									value: ip2.i18n({
										da: "Nej",
										en: "No"
									})
								}],
								type: "memory"
							}
						},
						listeners: {
							afterrender: {
								fn: function (item) {
									item.setValue(item.store.getAt(0).get("key"));
								}
							}
						}
					};
				}
				else {
					o_field = {
						anchor: "100%",
						labelAlign: labelAlign,
						name: "fieldEqual",
						readOnly: true
					};
				}
			break;
			case "bt":
				switch (config.dataType) {
					case "number":
						var fField = Ext.widget("numberfield", {
							anchor: "100%",
							flex: 1,
							name: "FirstBetween",
							emptyText: ip2.i18n({
								da: "Fra",
								en: "From"
							}),
							readOnly: true,
							selectOnFocus: true,
							decimals: config.decimals || 0,
							isFormField: false,
							validator: function() {
								var i_self = this;
								if (i_self.readOnly) {
									return true;
								}
								else {
									var i_parent = i_self.up("form");
									var a_from = i_parent.query("field[name=SecondBetween]");
									var i_from = a_from.length > 0 ? a_from[0] : null;
									if (i_from) {
										var n_fromValue = i_from.getValue();
										var n_value = i_self.getValue();
										if (n_fromValue && n_fromValue < n_value) {
											return ip2.i18n({
												da: "Dette felt skal have den mindste værdi",
												en: "This field has to have the smallest value"
											});
										}
									}
									return true;
								}
							}
						});
						var sField = Ext.widget("numberfield", {
							margin: "0 0 0 0",
							anchor: "100%",
							flex: 1,
							name: "SecondBetween",
							emptyText: ip2.i18n({
								da: "Til",
								en: "To"
							}),
							readOnly: true,
							decimals: config.decimals || 0,
							isFormField: false,
							validator: function() {
								var i_self = this;
								if (i_self.readOnly) {
									return true;
								}
								else {
									var i_parent = i_self.up("form");
									var a_from = i_parent.query("field[name=FirstBetween]");
									var i_from = a_from.length > 0 ? a_from[0] : null;
									if (i_from) {
										var n_fromValue = i_from.getValue();
										var n_value = i_self.getValue();
										if (n_value && n_fromValue > n_value) {
											return ip2.i18n({
												da: "Dette felt skal have den største værdi",
												en: "This field has to have the largest value"
											});
										}
									}
									return true;
								}
							}
						});
						if (config.decimals > 0) {
							fField.forcePrecision = true;
							fField.decimalSeparator = ".";

							sField.forcePrecision = true;
							sField.decimalSeparator = ".";
						}
						o_field = {
							border: false,
							defaults: { "margin": "0 5 0 0" },
							items: [fField, sField],
							layout: {
								type: "hbox",
								align: "strech"
							},
							bodyPadding: 0
						};
					break;
				}
			break;
		}

		o_field.fieldtype = type;

		if (type == "bt") {
			i_field = Ext.widget("fieldcontainer", o_field);
		}
		else {
			o_field.isFormField = false;
			switch (config.dataType) {
				case "string":
					i_field = Ext.widget("textfield", o_field);
				break;
				case "number":
					if (type != "bt") {
						if (o_field.decimals > 0) {
							o_field.decimalSeparator = ".";
							o_field.forcePrecision = true;
						}
						i_field = Ext.widget("numberfield", o_field);
					}
				break;
				case "enum":
				case "boolean":
					i_field = Ext.widget("combobox", o_field);
				break;
				case "entity":
				case "array":
					i_field = Ext.widget("entitycombobox", o_field);
				break;
			}
		}
		return i_field;
	},

	GenerateDateTimeField: function (config, type) {

		var i_self = this;
		var o_field = undefined;
		var labelAlign = "left";
		var i_field = undefined;

		switch (type) {
			case "gt":
			case "eq":
				o_field = {
					anchor: "100%",
					flex: 1,
					labelAlign: labelAlign,
					readOnly: true,
					selectOnFocus: true,
					time: (config.dataDateType == "datetime" || config.dataDateType == "datetimesec") ? true : false,
					isFormField: false
				};
			break;
			case "bt":
				var fField = Ext.widget("isotimestampfield", {
					anchor: "100%",
					flex: 1,
					emptyText: ip2.i18n({
						da: "Fra",
						en: "From"
					}),
					time: (config.dataDateType == "datetime" || config.dataDateType == "datetimesec") ? true : false,
					readOnly: true,
					selectOnFocus: true,
					isFormField: false
				});
				var sField = Ext.widget("isotimestampfield", {
					anchor: "100%",
					flex: 1,
					emptyText: ip2.i18n({
						da: "Til",
						en: "To"
					}),
					asHigh: config.asHigh ? config.asHigh : false,
					time: (config.dataDateType == "datetime" || config.dataDateType == "datetimesec") ? true : false,
					readOnly: true,
					selectOnFocus: true,
					timevalue: 23.59,
					isFormField: false
				});
				o_field = {
					border: false,
					items: [fField, sField],
					layout: {
						type: "hbox",
						align: "strech"
					},
					bodyPadding: 0,
					isFormField: false
				};
			break;
		}

		o_field.dateType = config.dataDateType;
		o_field.fieldtype = type;

		if (type != "bt") {
			i_field = Ext.widget("isotimestampfield", o_field);
			var i_datefield = i_field.getDateField();
			i_datefield.on({
				expand: {
					fn: function() {
						var i_menu = i_datefield.up("menu");
						if (i_menu) {
							i_menu.doHiding = false;
						}
					}
				},
				collapse: {
					fn: function() {
						var i_menu = i_datefield.up("menu");
						if (i_menu) {
							i_menu.doHiding = true;
						}
					}
				}
			});
		}
		else {
			i_field = Ext.widget("fieldcontainer", o_field);

			var i_isodatetimefield1 = i_field.items.items[0];
			var i_isodatetimefield2 = i_field.items.items[1];

			var i_datefield1 = i_isodatetimefield1.getDateField();
			var i_datefield2 = i_isodatetimefield2.getDateField();

			i_datefield1.on({
				expand: {
					fn: function() {
						var i_menu = i_datefield1.up("menu");
						if (i_menu) {
							i_menu.doHiding = false;
						}
					}
				},
				collapse: {
					fn: function() {
						var i_menu = i_datefield1.up("menu");
						if (i_menu) {
							i_menu.doHiding = true;
						}
					}
				},
				change: {
					fn: function(i_field, newValue, oldValue) {
						if (i_field.isValid()) {
							i_datefield2.setMinValue(newValue);
						}
					}
				}
			});

			i_datefield2.on({
				expand: {
					fn: function() {
						var i_menu = i_datefield2.up("menu");
						if (i_menu) {
							i_menu.doHiding = false;
						}
					}
				},
				collapse: {
					fn: function() {
						var i_menu = i_datefield2.up("menu");
						if (i_menu) {
							i_menu.doHiding = true;
						}
					}
				},
				change: {
					fn: function(i_field, newValue, oldValue) {
						if (i_field.isValid()) {
							i_datefield1.setMaxValue(newValue);
						}
					}
				}
			});
		}
		return i_field;
	},

	GenerateCheckBoxNot: function(config) {

		var i_self = this;
		var i_checkboxNot = Ext.widget("checkboxfield", {
			anchor: "100%",
			fieldLabel: ip2.i18n({
				da: "Ikke",
				en: "Not"
			}),
			inputValue: "",
			isFormField: false
		});
		config.checkBoxNot = i_checkboxNot;
		return i_checkboxNot;
	},

	GenerateCheckBoxBlank: function(config) {

		var i_self = this;

		var i_checkboxNot = Ext.widget("checkboxfield", {
			anchor: "100%",
			fieldLabel: ip2.i18n({
				da: "Udfyldt",
				en: "Filled"
			}),
			isFormField: false
		});
		config.checkBoxBlank = i_checkboxNot;
		return i_checkboxNot;
	},

	AddListener: function(i_field, config) {

		var i_self = this;

		i_field.on({
			render: {
				fn: function (item) {
					if (i_field.inputEl) {
						i_field.inputEl.on("click", function (item) {
							var i_checkbox = i_field.checkBox;
							if (!i_checkbox.checked) {
								i_checkbox.setValue(true);
							}
						}, i_field);
					}
				}
			},
			specialkey: {
				fn: function (item, e) {
					if (e.getKey() == e.ENTER) {
						var i_menu = item.up("menu");
						config.applyFn();
					}
				}
			}
		});
	},

	HandleCheckChange: function(config, type, value) {

		var a_checkbox = config.checkboxes;
		var a_fields = config.fields;
		var n_index = 0;

		Ext.Array.each(a_checkbox, function(i_checkbox) {

			var i_field = a_fields[n_index];

			if (i_checkbox.type == type) {
				if (i_checkbox.allowChange) {
					if (type != "bt") {
						if (i_checkbox.dataType != "date") {
							i_field.setReadOnly(!value);
							if (value) {
								i_field.focus(true, 20);
								i_field.fireEvent("enable", i_field);
							}
						}
						else {
							var i_datefield = i_field.getDateField();
							var i_timefield = i_field.getTimeField();

							i_datefield.setReadOnly(!value);
							i_timefield.setReadOnly(!value);

							i_datefield.focus(true, 20);
						}
					}
					else {
						if (i_checkbox.dataType != "date") {

							var f1 = i_field.items.items[0];
							var f2 = i_field.items.items[1];
							f1.setReadOnly(!value);
							f2.setReadOnly(!value);

							if (value) {
								f1.focus(true, 20);
							}
						}
						else {
							var i_isodatetimefield1 = i_field.items.items[0];
							var i_isodatetimefield2 = i_field.items.items[1];

							var i_datefield1 = i_isodatetimefield1.getDateField();
							var i_timefield1 = i_isodatetimefield1.getTimeField();

							var i_datefield2 = i_isodatetimefield2.getDateField();
							var i_timefield2 = i_isodatetimefield2.getTimeField();

							i_datefield1.setReadOnly(!value);
							i_timefield1.setReadOnly(!value);

							i_datefield2.setReadOnly(!value);
							i_timefield2.setReadOnly(!value);

							if (value) {
								i_datefield1.focus(true, 20);
							}
						}
					}
				}
			}
			else {
				i_checkbox.allowChange = false;
				i_checkbox.setValue(false);
				if (i_checkbox.type != "bt") {
					if (i_checkbox.dataType != "date") {
						i_field.setReadOnly(true);
					}
					else {
						var i_datefield = i_field.getDateField();
						var i_timefield = i_field.getTimeField();

						i_datefield.setReadOnly(true);
						i_timefield.setReadOnly(true);
					}
				}
				else {
					if (i_checkbox.dataType != "date") {

						var f1 = i_field.items.items[0];
						var f2 = i_field.items.items[1];
						f1.setReadOnly(true);
						f2.setReadOnly(true);
					}
					else {
						var i_isodatetimefield1 = i_field.items.items[0];
						var i_isodatetimefield2 = i_field.items.items[1];

						var i_datefield1 = i_isodatetimefield1.getDateField();
						var i_timefield1 = i_isodatetimefield1.getTimeField();

						var i_datefield2 = i_isodatetimefield2.getDateField();
						var i_timefield2 = i_isodatetimefield2.getTimeField();

						i_datefield1.setReadOnly(true);
						i_timefield1.setReadOnly(true);

						i_datefield2.setReadOnly(true);
						i_timefield2.setReadOnly(true);
					}
				}
				i_checkbox.allowChange = true;
			}
			n_index++;
		});
	}
});
