Ext.define("IceCap.util.Handler", {

	singleton: true,

	config: {
		scaling: {
			colSize: 9.7,
			textSize: 16,
			defaultWidth: 18,
			defaultHeight: 24,
			lineHeight: 28
		},
		sessions: [],
		fixed: true,
		scaleSize: 14,
		marginValue: 4,
		scale: 1,
		serviceUrl: "/icecap/icecap.aspx",
		provider: null
	},

	FFW: {
		// FFW identification flag (0 - 1)
		// Bypass (2)
		// Dup or Field Mark Enable (3)
		//  Modified Data Tag (MDT) (4)
		// Field Shift/Edit Specification (5 - 7)
		// Auto Enter (8)
		// Field Exit Required (FER) (9)
		// Monocase (10)
		// Reserved (11)
		// Mandatory Enter (ME) (12)
		// Right Adjust/Mandatory Fill (MF) (13-15)
		IDENT_FLAG: (1 << 0),
		BYPASS: (1 << 1),
		DUP_OR_MARK: (1 << 2),
		MOD_DATA: (1 << 3),
		APLHA_SHIFT: (1 << 4),
		ALPHA_ONLY: (1 << 5),
		NUMERIC_SHIFT: (1 << 6),
		NUMERIC_ONLY: (1 << 7),
		KATAKANA_SHIFT: (1 << 8),
		DIGITS_ONLY: (1 << 9),
		IO_FEATURE: (1 << 10),
		SIGNED_NUMERIC: (1 << 11),
		AUTO_ENTER: (1 << 12),
		EXIT_REQ: (1 << 13),
		MONOCASE: (1 << 14),
		MAND_ENTER: (1 << 15),
		RA_NONE: (1 << 16),
		RA_ZEROFILL: (1 << 17),
		RA_BLANKFILL: (1 << 18),
		RA_FILL: (1 << 19),
		RA_RESERVED: (1 << 20),
		RA_MANDATORY: (1 << 21),
		RA_READONLY: (1 << 22)
	},

	typeMode: {
		OVERWRITE: 1,
		INSERT: 2
	},

	requires: [
		"IceCap.state.session.Provider",
		"IceCap.state.local.Provider",
		"IceCap.util.Watchdog"
	],

	constructor: function(o_config) {

		var i_self = this;
		i_self.initConfig(o_config);

		var i_provider = Ext.widget("icecap-state-local-provider", {});

		i_self.setProvider(i_provider);

		return this;
	},

	getPath: function(o_config) {

		var s_base = IceCap.util.Handler.getServiceUrl();
		var s_session = o_config.session;
		var s_activeTerminals = IceCap.util.Handler.getActiveTerminals();

		if (s_activeTerminals) {
			Ext.apply(o_config, {
				activeTerminals: s_activeTerminals
			});
		}

		return s_session + "" + s_base + "?" + Ext.urlEncode(o_config);
	},

	getX: function(n_col) {
		return Math.ceil(n_col * IceCap.util.Handler.getColSize());
	},

	getY: function(n_lin) {
		return Math.ceil(n_lin * IceCap.util.Handler.getLineHeight());
	},

	toColAndLine: function(o_values) {

		var n_x = o_values.x - IceCap.util.Handler.getColSize();
		var n_y = o_values.y;

		var n_col = Math.ceil(n_x / IceCap.util.Handler.getColSize()) || 1;
		var n_lin = Math.floor(n_y / IceCap.util.Handler.getLineHeight()) || 1;

		var o_position = Ext.apply({}, {
			col: n_col,
			lin: n_lin
		});

		return o_position;
	},

	getColSize: function() {
		var o_scale = IceCap.util.Handler.getScalingObject();
		return o_scale.colSize * IceCap.util.Handler.getScale();
	},

	getLineHeight: function() {
		var o_scale = IceCap.util.Handler.getScalingObject();
		return o_scale.lineHeight * IceCap.util.Handler.getScale();
	},

	getMargin: function() {
		return IceCap.util.Handler.getMarginValue() * IceCap.util.Handler.getScale();
	},

	getTextSize: function() {
		var o_scale = IceCap.util.Handler.getScalingObject();
		return o_scale.textSize * IceCap.util.Handler.getScale();
	},

	getDefaultWidth: function() {
		var o_scale = IceCap.util.Handler.getScalingObject();
		return o_scale.defaultWidth * IceCap.util.Handler.getScale();
	},

	getDefaultHeight: function() {
		var o_scale = IceCap.util.Handler.getScalingObject();
		return o_scale.defaultHeight * IceCap.util.Handler.getScale();
	},

	getData: function(o_config, o_data, f_callback) {

		f_callback = f_callback || Ext.emptyFn;
		o_data = o_data || {};

		Ext.Ajax.request({
			url: IceCap.util.Handler.getPath(o_config),
			method: "POST",
			params: o_data,
			success: function(i_response, i_request) {

				var o_data = Ext.apply({}, {
					input: [],
					output: []
				});
				if (!Ext.String.startsWith(!i_response.responseText.toLowerCase(), "<html")) {

					Ext.USE_NATIVE_JSON = false; // DO THIS TO ALLOW THE PARSE OF FUNCTIONS

					o_data = Ext.decode(i_response.responseText, true) || Ext.apply({}, {
						input: [],
						output: []
					});

					//Ext.USE_NATIVE_JSON = true;
				}
				f_callback(o_data);
			},
			failure: function() {
				f_callback({
					input: [],
					output: []
				});
			}
		});
	},

	getNewSession: function(f_callback) {

		f_callback = f_callback || Ext.emptyFn;

		var s_activeTerminals = IceCap.util.Handler.getActiveTerminals();

		Ext.Ajax.request({
			url: IceCap.util.Handler.getServiceUrl() + "?activeTerminals=" + s_activeTerminals,
			method: "POST",
			success: function(i_response, i_request) {

				var o_data = Ext.decode(i_response.responseText, true) || Ext.apply({}, {
					success: false,
					session: ""
				});

				IceCap.util.Handler.addSession(o_data.session);

				f_callback(o_data);
			},
			failure: function(o_err) {

				var o_data = Ext.apply({}, {
					success: false,
					session: ""
				});
				f_callback(o_data);
			}
		});
	},

	useScale: function(n_scale) {

		var o_scale = IceCap.util.Scale.getFixed()[n_scale];

		if (o_scale) {
			IceCap.util.Handler.setScaling(o_scale);
		}
	},

	addSession: function(s_session) {

		var i_provider = IceCap.util.Handler.getProvider();

		var a_terminals = Ext.Array.from(i_provider.get("activeterminals"));

		var s_terminal = s_session.substring(s_session.length - 2);
		var n_terminal = parseFloat(s_terminal);

		if (isNaN(n_terminal)) {
			n_terminal = 0;
		}

		if (n_terminal && Ext.Array.indexOf(a_terminals, n_terminal) === -1) {
			a_terminals.push(n_terminal);
		}

		i_provider.set("activeterminals", a_terminals);

		var a_sessions = IceCap.util.Handler.getSessions() || [];

		 Ext.Array.push(a_sessions, s_session);

		IceCap.util.Handler.setSessions(a_sessions);
	},

	getActiveTerminals: function() {

		var i_provider = IceCap.util.Handler.getProvider();

		var a_terminals = Ext.Array.from(i_provider.get("activeterminals"));

		if (a_terminals.length === 0) {
			a_terminals.push(0);
		}

		return a_terminals.join(",");
	},

	removeSession: function(s_session) {

		var i_provider = IceCap.util.Handler.getProvider();

		var a_terminals = Ext.Array.from(i_provider.get("activeterminals"));

		var s_terminal = s_session.substring(s_session.length - 2);
		var n_terminal = parseFloat(s_terminal);

		if (isNaN(n_terminal)) {
			n_terminal = 0;
		}

		if (n_terminal) {
			Ext.Array.remove(a_terminals, n_terminal);
		}

		i_provider.set("activeterminals", a_terminals);

		var a_sessions = IceCap.util.Handler.getSessions() || [];

		Ext.Array.remove(a_sessions, s_session);

		IceCap.util.Handler.setSessions(a_sessions);
	},

	isSessionAlive: function(s_session, f_callback) {

		f_callback = f_callback || Ext.emptyFn;

		IceCap.util.Handler.addSession(s_session);

		Ext.Ajax.request({
			url: IceCap.util.Handler.getServiceUrl() + "?activeTerminals=" + IceCap.util.Handler.getActiveTerminals(),
			method: "POST",
			params: {
				isSessionAlive: s_session
			},
			success: function(i_response, i_request) {

				var o_result = Ext.decode(i_response.responseText, true) || Ext.apply({}, {
					success: false,
					message: ""
				});

				f_callback(o_result.success);
			},
			failure: function(o_err) {

				IceCap.util.Handler.removeSession(s_session);

				var o_result = Ext.apply({}, {
					success: false,
					session: ""
				});

				f_callback(o_result.success);
			}
		});
	},

	getScalingObject: function() {

		var b_fixed = IceCap.util.Handler.getFixed();
		var o_return = IceCap.util.Handler.getScaling();
		var n_scale = IceCap.util.Handler.getScaleSize();
		if (!b_fixed) {
			var o_free = IceCap.util.Scale.getFree()[n_scale];
			if (o_free) {
				o_return = o_free;
			}
		}

		return o_return;
	},

	createWindow: function(o_config) {

		var o_icecap = {
			func: o_config.func,
			initFunc: o_config.initFunc,
			responseCallBack: o_config.responseCallBack,
			requestCallBack: o_config.requestCallBack,
			theme: o_config.theme
		};

		var i_icecap = Ext.create("IceCap.view.Main", o_icecap);

		var i_window = Ext.widget("window", Ext.apply(o_config, {
			onEsc: Ext.emptyFn,
			items: [
				i_icecap
			]
		}));

		i_icecap.on({
			destroy: {
				fn: function () {
					if (!i_window.isDestroyed && !i_window.destroying) {
						i_window.close();
					}
				}
			}
		});

		return i_window;
	},

	createIndicators: function(s_hex16) {

		var i_self = this;

		// EXAMPLE
		// WE READ FROM LEFT TO RIGHT BITWISE
		// 0100 0011 0000 0110

		// 1 IN 16 BITS
		// RESULT 0000 0000 0000 0001

		// (1 << 15) SHIFT LEFT 15 BIT
		// RESULT 1000 0000 0000 0000

		var n_hex32 = 0;

		var n_hex16 = parseInt(s_hex16);

		if (!(n_hex16 & (1 << 15)) && (n_hex16 & (1 << 14))) {
			n_hex32 |= IceCap.util.Handler.FFW.IDENT_FLAG;
		}

		if (n_hex16 & (1 << 13) || (n_hex16 & (1 << 10 | 1 << 9 | 1 << 8) == (1 << 10 | 1 << 9))) {
			n_hex32 |= IceCap.util.Handler.FFW.BYPASS;
		}
		if (n_hex16 & (1 << 12)) {
			n_hex32 |= IceCap.util.Handler.FFW.DUP_OR_MARK;
		}
		if (n_hex16 & (1 << 11)) {
			n_hex32 |= IceCap.util.Handler.FFW.MOD_DATA;
		}

		n_hex32 |= i_self.ffw_spec(n_hex16);

		if (n_hex16 & (1 << 7)) {
			n_hex32 |= IceCap.util.Handler.FFW.AUTO_ENTER;
		}
		if (n_hex16 & (1 << 6)) {
			n_hex32 |= IceCap.util.Handler.FFW.EXIT_REQ;
		}
		if (n_hex16 & (1 << 5)) {
			n_hex32 |= IceCap.util.Handler.FFW.MONOCASE;
		}
		if (n_hex16 & (1 << 3)) {
			// n_hex32 |= IceCap.util.Handler.FFW.MAND_ENTER;
		}

		n_hex32 |= i_self.ffw_ra(n_hex16);

		return n_hex32;
	},

	ffw_spec: function(n_flag) {

		if (n_flag & (1 << 10) && n_flag & (1 << 9) && n_flag & (1 << 8)) {
			return IceCap.util.Handler.FFW.SIGNED_NUMERIC;
		}
		if (n_flag & (1 << 10) && n_flag & (1 << 9) && !(n_flag & (1 << 8))) {
			return IceCap.util.Handler.FFW.IO_FEATURE;
		}
		if (n_flag & (1 << 10) && !(n_flag & (1 << 9)) && n_flag & (1 << 8)) {
			return IceCap.util.Handler.FFW.DIGITS_ONLY;
		}
		if (n_flag & (1 << 10) && !(n_flag & (1 << 9)) && !(n_flag & (1 << 8))) {
			return IceCap.util.Handler.FFW.KATAKANA_SHIFT;
		}
		if (!(n_flag & (1 << 10)) && n_flag & (1 << 9) && n_flag & (1 << 8)) {
			return IceCap.util.Handler.FFW.NUMERIC_ONLY;
		}
		if (!(n_flag & (1 << 10)) && n_flag & (1 << 9) && !(n_flag & (1 << 8))) {
			return IceCap.util.Handler.FFW.NUMERIC_SHIFT;
		}
		if (!(n_flag & (1 << 10)) && !(n_flag & (1 << 9)) && n_flag & (1 << 8)) {
			return IceCap.util.Handler.FFW.ALPHA_ONLY;
		}

		return IceCap.util.Handler.FFW.APLHA_SHIFT;
	},

	ffw_ra: function(n_flag) {

		if (!(n_flag & (1 << 2)) && !(n_flag & (1 << 1)) && !(n_flag & (1 << 0))) {
			return IceCap.util.Handler.FFW.RA_NONE;
		}
		if ((n_flag & (1 << 2)) && !(n_flag & (1 << 1)) && (n_flag & (1 << 0))) {
			return IceCap.util.Handler.FFW.RA_ZEROFILL;
		}
		if ((n_flag & (1 << 2)) && (n_flag & (1 << 2)) && !(n_flag & (1 << 0))) {
			return IceCap.util.Handler.FFW.RA_BLANKFILL;
		}
		if ((n_flag & (1 << 2)) && (n_flag & (1 << 2)) && (n_flag & (1 << 0))) {
			return IceCap.util.Handler.FFW.RA_MANDATORY;
		}

		return IceCap.util.Handler.FFW.RA_RESERVED;
	}
});

Ext.define("IceCap.util.Scale", {

	singleton: true,

	config: {
		fixed: null,
		free: null
	},

	constructor: function(o_config) {

		var i_self = this;
		i_self.initConfig(o_config);

		i_self.setFixed({
			"16": {
				"textSize": 16,
				"colSize": 9.7,
				"defaultWidth": 18,
				"defaultHeight": 24,
				"lineHeight": 28
			},
			"15": {
				"textSize": 15,
				"colSize": 9.1,
				"defaultWidth": 17,
				"defaultHeight": 22,
				"lineHeight": 26
			},
			"14": {
				"textSize": 14,
				"colSize": 8.45,
				"defaultWidth": 17,
				"defaultHeight": 20,
				"lineHeight": 24
			},
			"13": {
				"textSize": 13,
				"colSize": 7.9,
				"defaultWidth": 15,
				"defaultHeight": 19,
				"lineHeight": 22
			},
			"12": {
				"textSize": 12,
				"colSize": 7.4,
				"defaultWidth": 14,
				"defaultHeight": 17,
				"lineHeight": 20
			},
			"11": {
				"textSize": 11,
				"colSize": 6.8,
				"defaultWidth": 13,
				"defaultHeight": 15,
				"lineHeight": 18
			}
		});

		i_self.setFree({
			"14": {
				"textSize": 13,
				"colSize": 12.2,
				"defaultWidth": 20,
				"defaultHeight": 20,
				"lineHeight": 24
			}
		});
	}
});
Ext.define("IceCap.util.Breakout", {

	alternateClassName: [
		"IceCap.breakout"
	],

	singleton: true,

	constructor: function(o_config) {

		var i_self = this;
		i_self.initConfig(o_config);
	},

	googleMap: function(o_data) {

		var i_self = this;

		o_data = o_data || {};

		if (s_title.toLowerCase() == "work with customers") {

			var s_url = "http://www.icebreak.org/geoCoding.html";

			var o_params = {
				address: "dk" + o_data.c5 + "," + o_data.c4
			};

			s_url = s_url + "?" + Ext.Object.toQueryString(o_params);

			Ext.GlobalEvents.fireEvent(i_self, "onAddWebContainer", {
				url: s_url
			});
		}
	},

	krak: function(o_data) {

		var i_self = this;

		o_data = o_data || {};

		if (s_title.toLowerCase() == 'work with customers') {

			var s_url = 'http://www.krak.dk/'
					+ o_data.c3.replace(' ', '+')
					+ '/'
					+ o_data.c4.replace(' ', '+')
					+ '+'
					+ o_data.c5
					+ '/søg.cs';

			Ext.GlobalEvents.fireEvent(i_self, "onAddWebContainer", {
				url: s_url
			});
		}
	}
});
Ext.define("IceCap.component.Marker", {

	xtype: "icecap-marker",

	alias: [
		"widget-icecap-marker"
	],

	extend: "Ext.Component",

	hidden: true,

	width: "100%",

	height: "100%",

	config: {
		inUse:  false,
		xValue: 0,
		yValue: 0
	},

	listeners: {
		afterrender: {
			fn: function(i_marker) {

				i_marker.update();
			}
		},
		show: {
			fn: function(i_marker) {

				i_marker.update();
			}
		}
	},

	renderTpl: [
		"<div id=\"{id}-outer\" data-ref=\"outerEl\" ",
			"style=\"",
			"width:100%;",
			"height:100%;",
			"background-color:transparent;",
		"\">",
			"<div id=\"{id}-body\" data-ref=\"bodyEl\" ",
				"style=\"",
				"width:100%;",
				"height:100%;",
				"background-color:transparent;",
			"\">",
				"<div id=\"{id}-frameBody\" data-ref=\"frameBody\">",
						"<div id=\"{id}-markerVertical\" class=\"icecap-marker-vertical-{ui}\" data-ref=\"markerVertical\" ",
							"style=\"",
							"width: 0px;",
							"height: 100%;",
							"top: 0px;",
							"left: 0px;",
							"clear: Both;",
						"\"></div>",
						"<div id=\"{id}-markerHorizontal\" class=\"icecap-marker-horizontal-{ui}\" data-ref=\"markerHorizontal\" ",
							"style=\"",
							"height: 0px;",
							"width: 100%;",
							"top: 0px;",
							"left: 0px;",
						"\"></div>",
				"</div>",
			"</div>",
		"</div>"
	],

	childEls: [
		{
			name: "outerEl",
			itemId: "outer"
		},
		{
			name: "frameBody",
			itemId: "frameBody"
		},
		{
			name: "markerVerticalEl",
			itemId: "markerVertical"
		},
		{
			name: "markerHorizontalEl",
			itemId: "markerHorizontal"
		},

	],

	constructor: function(o_config) {

		var i_self = this;

		i_self.callParent(arguments);
	},

	updateInUse: function(value) {

		var i_self = this;

		i_self[value ? "show" : "hide"]();

		i_self.fireEvent(value ? "show" : "hide", i_self);
	},

	updateXValue: function(value) {

		var i_self = this;

		if (i_self.rendered) {

			var i_markerVertical = i_self.markerVerticalEl;
			i_markerVertical.setLeft(value);
		}
	},

	updateYValue: function(value) {

		var i_self = this;

		if (i_self.rendered) {

			var i_markerHorizontal = i_self.markerHorizontalEl;
			i_markerHorizontal.setTop(value);
		}
	},

	update: function() {

		var i_self = this;

		var n_x = i_self.getXValue();
		var n_y = i_self.getYValue();

		i_self.updateXValue(n_x);
		i_self.updateYValue(n_y);
	},

	move: function(a_position) {

		var i_self = this;

		i_self.setXValue(a_position[0]);
		i_self.setYValue(a_position[1]);
	},

	toggle: function() {

		var i_self = this;

		var b_InUse = i_self.getInUse();

		i_self.setInUse(!b_InUse);
	}
});
Ext.define("IceCap.ux.form.panel.Panel4Area", {

	extend: "Ext.panel.Panel",

	alias: [
		"widget.iceCapPanel4Area"
	],

	layout: "form",

	border: false,

	bodyPadding: 5,

	defaultType: "textfield",

	focusable: false,

	listeners: {
		activate: {
			fn: function(i_panel) {

				var a_fields = i_panel.query("field[focusable]");

				if (a_fields.length > 0) {
					var i_field = a_fields[0];
					i_field.focus(true, 10);
				}
			}
		}
	},

	initComponent: function () {

		var i_self = this;

		var a_items = Ext.Array.slice(i_self.items, 0);

		Ext.Array.each(a_items, function(o_field) {

			o_field.value = o_field.value || o_field.dta;
			o_field.fieldLabel = o_field.fieldLabel || o_field.label;

			o_field.readOnly = o_field.type === "output";

			var o_meta = o_field.metaData || {
				xtype: "textfield"
			};

			delete o_field.metaData;

			Ext.apply(o_field, o_meta);
		});

		i_self.items = a_items;

		i_self.callParent();

		i_self.enableBubble("onAddWebContainer");
	}
});
Ext.define("IceCap.ux.form.panel.TabPanel", {

	extend: "Ext.tab.Panel",

	alias: [
		"widget.tabPanel"
	],

	listeners: {
		afterrender: {
			fn: function(i_panel) {

				var i_self = this;

				i_self.initKeyMap();
			}
		}
	},

	config: {
		keyMap: null
	},

	initComponent: function() {

		var i_self = this;

		i_self.callParent(arguments);
	},

	initKeyMap: function() {

		var i_self = this;

		var a_bindings = [{
			key: "1234567890",
			ctrl: true,
			alt: false,
			handler: function(n_keyCode, i_event) {

				i_event.stopEvent();

				var n_tab = n_keyCode - 49;

				var a_items = i_self.items;

				if (n_tab > a_items.length) {
					n_tab = 0;
				}

				i_self.setActiveTab(n_tab);
			}
		}];

		var i_keyMap = Ext.create("Ext.util.KeyMap", {
			target: i_self.getEl(),
			binding: a_bindings
		});

		i_self.setKeyMap(i_keyMap);
	}
});
Ext.define("IceCap.ux.grid.editor.Controller", {
	extend: "Ext.app.ViewController",

	alias: "controller.icecap.ux.grid.editor.main"
});
Ext.define("IceCap.ux.grid.editor.Main", {

	extend: "Ext.grid.Panel",

	alias: [
		"widget.iceCapEditorGrid",
		"widget.iceCapGrid"
	],

	requires: [
		"IceCap.ux.grid.editor.Controller"
	],

	config: {
		gridConfig: null,
		toolbar: null,
		pagingToolbar: null,
		keys: [],
		extraToolbarButtons: [],
		rowMenu: null,
		defaultOption: "ENTER",
		needFocus: false,
		focusTo: 0,
		theme: ""
	},

	flex: 1,

	listeners: {
		afterrender: {
			fn: function(i_panel) {

				var i_self = this;
				var i_store = i_self.getStore();
				var i_view = i_self.getView();
				var a_records = i_store.getRange()

				Ext.suspendLayouts();

				i_self.getView().setHeight(a_records.length * IceCap.util.Handler.getLineHeight());

				Ext.resumeLayouts(true);

				i_self.setTabIndex(i_self.initialConfig.tabIndex || -1);

				var i_navigationModel = i_self.getNavigationModel();
				var i_keyNav = i_navigationModel.keyNav;
				var i_map = i_keyNav.map;

				var i_tabBinding = Ext.Array.findBy(i_map.bindings, function(i_binding) {
					return i_binding.key == Ext.event.Event.TAB;
				});

				i_map.removeBinding(i_tabBinding);

				var i_enterBinding = Ext.Array.findBy(i_map.bindings, function(i_binding) {
					return i_binding.key == Ext.event.Event.ENTER;
				});

				i_map.removeBinding(i_enterBinding);

				i_keyNav.defaultEventAction = "";
				i_map.ignoreInputFields = false;

				i_map.addBinding({
					ctrl: false,
					shift: false,
					alt: false,
					key: Ext.event.Event.TAB,
					handler: function(n_key, i_event) {

						var i_selectionModel = i_self.getSelectionModel();
						var i_current = i_selectionModel.getCurrentPosition();

						var i_store = i_self.getStore();

						if (i_current.rowIdx != (i_store.getTotalCount() - 1)) {
							i_event.stopEvent();
						}

						i_self.getSelectionModel().selectNext(false);
					}
				});

				i_map.addBinding({
					ctrl: false,
					shift: true,
					alt: false,
					key: Ext.event.Event.TAB,
					handler: function(n_key, i_event) {

						var i_selectionModel = i_self.getSelectionModel();
						var i_current = i_selectionModel.getCurrentPosition();

						if (i_current.rowIdx > 0) {
							i_event.stopEvent();
						}
						i_selectionModel.selectPrevious(false);
					}
				});

				i_map.addBinding({
					key: Ext.event.Event.ENTER,
					handler: function(n_key, i_event) {

						var i_selectionModel = i_self.getSelectionModel();
						var i_current = i_selectionModel.getCurrentPosition();

						i_event.stopEvent();

						if (i_current) {

							var i_record = i_current.record;

							var s_id = i_record.getId().split("g.").pop();

							var s_defaultOption = i_self.getDefaultOption();

							var o_data = {
								field: s_id,
								option: s_defaultOption,
								col: Ext.Number.from(i_record.get("col"), 0),
								lin: Ext.Number.from(i_record.get("lin"), 0)
							};

							i_self.fireEvent("fieldoption", [o_data]);
						}
					}
				});

				if (i_self.getNeedFocus()) {

					var i_view = i_self.getView();
					i_view.select(i_self.getFocusTo() - 1);
				}
			}
		},
		itemcontextmenu: {
			fn: function(i_view, i_record, i_html, n_index, i_event) {

				var i_self = this;

				i_event.stopEvent();

				var a_position = i_event.getXY();

				var i_menu = i_self.getRowMenu();

				i_menu.showAt(a_position);
			}
		},
		itemdblclick: {
			fn: function (i_view, i_record, i_item, n_index, i_event, o_options) {

				var i_self = this;

				i_event.stopEvent();

				var s_id = i_record.getId().split("g.").pop();

				var s_defaultOption = i_self.getDefaultOption();

				var o_data = {
					field: s_id,
					option: s_defaultOption,
					col: Ext.Number.from(i_record.get("col"), 0),
					lin: Ext.Number.from(i_record.get("lin"), 0)
				};

				i_self.fireEvent("fieldoption", [o_data]);
			}
		},
		select: {
			fn: function(i_selectionModel, i_record) {

				var i_self = this;
				var o_gridConfig = i_self.getGridConfig();
				var i_columnManager = i_self.getColumnManager();

				if (o_gridConfig.showOption) {

					var i_column = i_columnManager.getHeaderByDataIndex(o_gridConfig.optionDataIndex);

					if (i_column) {
						var i_textfield = i_column.getWidget(i_record);
						i_textfield.focus(10, true);
					}
				}
			}
		}
	},

	initComponent: function() {

		var i_self = this;

		var o_viewConfig = {
			focus: function () {

				var i_self = this;
				i_self.callOverridden(arguments);

				var i_selectionModel = i_self.getSelectionModel();
				var i_current = i_selectionModel.getCurrentPosition();

				if (!i_current) {
					i_self.select(0, false);
				}
				else {
					var i_record = i_current.record;

					i_selectionModel.deselectAll(false);
					i_self.select(i_record);
				}
			},
			getRowClass: function () {
				var i_view = this;
				var i_grid = i_view.grid;
				return "iceCap-row-" + i_grid.getTheme();
			},
			cls: "iceCap-row-" + i_self.getTheme()
		};

		Ext.apply(i_self, {
			viewConfig: o_viewConfig
		});

		var i_store = i_self.createStore();
		var a_columns = i_self.createColumns();

		var o_config = i_self.getGridConfig();

		var i_selectionModel = Ext.create("Ext.selection.RowModel", {
			mode: "MULTI"
		});

		Ext.apply(i_self, {
			store: i_store,
			columns: a_columns,
			enableColumnMove: o_config.enableColumnMove || false,
			selModel: i_selectionModel
		});

		i_self.callParent(arguments);

		if (o_config.showToolbar) {
			i_self.createToolbar();
		}

		i_self.addDocked(i_self.createPagingToolbar());

		i_self.createRowMenu();
	},

	createStore: function() {

		var i_self = this;

		var o_config = i_self.getGridConfig();

		var o_store = o_config.store;
		var o_meta = o_store.metaData;

		var i_store = Ext.create("Ext.data.Store", {
			fields: o_meta.fields,
			autoload: false,
			proxy: {
				type: "memory",
				reader: {
					type: "json"
				}
			},
			data: o_store.rows
		});

		return i_store;
	},

	createColumns: function() {

		var i_self = this;

		var o_config = i_self.getGridConfig();

		var a_return = [];

		var a_columns = o_config.header || [];

		var a_quick = o_config.quickOption || [];

		Ext.Array.each(a_columns, function(o_config, n_index) {

			var o_column = Ext.clone(o_config);

			if (Ext.isFunction(o_column.renderer) &&
					o_column.renderer == (window.renderOptionField ||
					o_column.renderer == window.renderField)
				) {

				var o_option = Ext.Array.findBy(a_quick, function(o_values) {
					return o_values.dataIndex == o_column.dataIndex;
				}) || {};

				Ext.apply(o_column, {
					xtype: "widgetcolumn",
					widget: {
						xtype: "iField",
						metaData: {
							len: o_option.len || 10,
							ffw: o_option.ffw,
							fieldStyle: {
								"text-align": "right"
							}
						},
						listeners: {
							fieldfocus: {
								fn: function(i_field) {

									var i_store = i_self.getStore();

									var i_widgetRecord = i_field.getWidgetRecord();

									var n_index = i_store.find(i_widgetRecord.idProperty, i_widgetRecord.get(i_widgetRecord.idProperty));

									if (n_index > -1) {
										var i_view = i_self.view;
										i_view.select(i_widgetRecord);
									}
								}
							},
							specialKey: {
								fn: function(i_field, i_event) {

									var i_store = i_self.getStore();

									var a_range = i_store.getRange();

									var a_options = [];

									var i_column = i_field.getWidgetColumn();

									Ext.Array.each(a_range, function(i_record) {

										var s_id = i_record.getId().split("g.").pop();

										var i_widget = i_column.getWidget(i_record);

										if (i_widget.getValue()) {

											var o_data = {
												field: s_id,
												option: i_widget.getValue(),
												col: Ext.Number.from(i_record.get("col"), 0),
												lin: Ext.Number.from(i_record.get("lin"), 0)
											};

											a_options.push(o_data);
										}
									});

									i_self.fireEvent("fieldoption", a_options);
								}
							}
						}
					}
				});

				delete o_column.renderer;
			}
			else {
				o_column.renderer = function (value, i_meta, i_record, n_row, n_col) {

					var atr = i_record.get("atr" + (n_col + 1));
					i_meta.tdCls = ["iceCap-output", atr || 20, i_self.getTheme()].filter(Boolean).join("-");

					return value;
				};
			}

			o_column.width = o_column.width || null;
			o_column.flex = o_column.width ? null : 1;

			a_return.push(o_column);
		});

		return a_return;
	},

	createToolbar: function() {

		var i_self = this;

		var o_config = i_self.getGridConfig();

		var a_bottom = Ext.Array.from(o_config.bottomKeys);

		var a_keys = i_self.getKeys();

		var a_extraButtons = o_config.extraToolbarButtons || [];

		var a_omited = Ext.Array.filter(a_bottom, function(o_key) {
			return o_key.key;
		});

		var a_filtered = Ext.Array.filter(a_keys, function(o_key) {

			var i_found = Ext.Array.findBy(a_omited, function(o_filtered) {
				return o_key.id == o_filtered.key;
			});

			return !i_found;
		});

		var a_buttons = i_self.createButtons(a_filtered);

		if (a_extraButtons.length > 0) {

			a_extra = Ext.Array.filter(a_extraButtons, function(o_key) {

				var i_found = Ext.Array.findBy(a_omited, function(o_filtered) {
					return o_key.id == o_filtered.key;
				});

				return !i_found;
			});

			var a_return = i_self.createButtons(a_extra);

			a_buttons = a_buttons.concat(a_return);
		}

		if (a_buttons.length > 0) {
			var i_toolbar = Ext.widget("toolbar", {
				dock: "top",
				items: a_buttons
			});

			i_self.setToolbar(i_toolbar);

			i_self.addDocked(i_toolbar);
		}
	},

	createPagingToolbar: function() {

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

		var i_toolbar = Ext.widget("toolbar", {
			dock: "bottom",
			items: [
				{
					xtype: "button",
					iconCls: "icecap-icon-page-prev",
					handler: function() {

						i_self.fireEvent("key", "ROLLDOWN");
					}
				},
				'->',
				{
					xtype: "button",
					iconCls: "icecap-icon-page-next",
					handler: function() {

						i_self.fireEvent("key", "ROLLUP");
					}
				}
			]
		});

		i_self.setPagingToolbar(i_toolbar);

		return i_toolbar;
	},

	createRowMenu: function() {

		var i_self = this;
		var o_config = i_self.getGridConfig();

		var a_items = [];

		var s_options = o_config.defaultOption || "";

		var a_defaultsOptions = s_options.split(",");

		var n_position = 99;

		var s_defaultOption = "";

		Ext.Array.each(o_config.menu, function(o_item) {

			var i_item = Ext.widget("menuitem", {
				text: o_item.text + " ( " + o_item.id + " )",
				option: o_item.id,
				breakout: o_item.breakout || false,
				icon: "/system/snapOnIcon.aspx.png?keyword=" + escape(o_item.text) + "&default=folder.png&insertRoot=true",
				handler: function(i_menuitem) {

					var a_selected = i_self.getSelection();

					var f_breakout = eval(i_menuitem.breakout);

					if (!i_menuitem.breakout) {

						var a_options = [];

						Ext.Array.each(a_selected, function(i_record) {

							var s_id = i_record.getId().split("g.").pop();

							var o_data = {
								field: s_id,
								option: i_menuitem.option,
								col: Ext.Number.from(i_record.get("col"), 0),
								lin: Ext.Number.from(i_record.get("lin"), 0)
							};

							a_options.push(o_data);
						});

						i_self.fireEvent("fieldoption", a_options);
					}
					else {
						if (Ext.isFunction(f_breakout)) {

							Ext.Array.each(a_selected, function(i_record) {
								f_breakout(i_record.getData());
							});
						}
					}
				}
			});

			var n_index = a_defaultsOptions.indexOf(o_item.id);

			if (n_index > -1) {
				if (n_index < n_position) {
					n_position = n_index;
					s_defaultOption = o_item.id;
				}
			}

			a_items.push(i_item);
		});

		var i_found = Ext.Array.findBy(a_items, function(i_item) {
			return i_item.option == s_defaultOption;
		});

		if (i_found) {
			i_found.text = "<strong>" + i_found.text + "</strong>";
		}

		var i_menu = Ext.widget("menu", {
			items: a_items
		});

		i_self.setRowMenu(i_menu);
		i_self.setDefaultOption(s_defaultOption);
	},

	createButtons: function(a_config) {

		var i_self = this;

		var a_buttons = [];

		Ext.Array.each(a_config, function(o_config) {

			var o_key = {
				xtype: "button",
				key: o_config.id ? o_config.id : o_config.key,
				text: o_config.text,
				icon: "/system/snapOnIcon.aspx.png?keyword=" + escape(o_config.text) + "&default=folder.png&insertRoot=true",
				iconCls: o_config.iconCls,
				handler: function(i_button) {
					i_self.fireEvent("key", i_button.key);
				}
			};

			a_buttons.push(o_key);
		});

		return a_buttons;
	}

}, function() {

	Ext.apply(window, {
		onRowRightClick: Ext.emptyFn,
		renderField: function (val, meta, rec, row, col) {
			var atr = rec.data["atr" + (col + 1)],
			io = rec.data["io" + (col + 1)];

			if (io != "b" && io != "i") {
				return val;
			}

			var html = "<div id=\"" + Ext.id() + "." + rec.id + ".c" + (col + 1) + "\"";
			return html + "></div>";
		},
		renderOptionField: function(value, i_meta, i_record, n_row, n_col) {

			var s_attr = i_record.get("atr" + (n_col + 1));
			var s_html = "";

			s_html = "<div id=\"win-opt-" + n_row + "\""  ;

			if (!Ext.isEmpty(s_attr)) {
				s_html += " class=\"iceCap-output-" + s_attr + "\"";
			}

			s_html += "></div>";

			return s_html;
		},
		renderQuickKeyField: function(val, meta, rec, row, col) {
			var atr = rec.data['atr' + (col + 1)],
			html;

			if (rec.id.indexOf('q.') > -1) {
				html = '<div id=' + Ext.id() + '.qCol.' + col + '"';
				if (!Ext.isEmpty(atr)) {
					html += ' class="iceCap-output-' + atr + '"';
				}

				html += '></div>';
			}
			else if (!Ext.isEmpty(atr)) {
				html = '<div class="iceCap-output-' + atr + '">' + val + '</div>';
			}
			else {
				html = val;
			}
			return html;
		}
	});
});
Ext.define("IceCap.ux.container.Column", {
	
	extend: "Ext.Container",

	alias: [
		"widget.iColumn"
	],

	defaults: {
		xtype: "container",
		layout: {
			type: "form"
		},
		defaults: {
			autoEl: {},
			style: "padding:8px 0 0 5px"
		}
	},

	layout: {
		type: "column"
	},

	flex: 1,
	focusable: false,

  	constructor: function(o_config) {

		var i_self = this;

		i_self.name = i_self.id || o_config.id;

		delete i_self.id;
		delete o_config.id;

		i_self.callParent([o_config]);
	},

 	initComponent: function() {

 		var i_self = this;

 		i_self.callParent(arguments);
 	}
});Ext.define("IceCap.ux.form.label.Label", {
	
	extend: "Ext.form.field.Display",

	alias: [
		"widget.iLabel"
	],

	config: {
		theme: "default"
	},

	focusable: false,

	initComponent: function () {

		var i_self = this;

		var o_config = i_self.initialConfig;

		var s_value = o_config.text;

		if (o_config.ta == "right" && !o_config.off) {
			//o_config.col += o_config.len - o_config.dta.length;
		}

		if (o_config.off) {
			s_value = Ext.String.leftPad(s_value, s_value.length + o_config.off);
		}

		var o_field = {
			value: s_value,
			htmlEncode: true,
			cls: ["icecap-field", i_self.getTheme()].filter(Boolean).join("-")
		};

		var o_style = {
			"font": "normal " + IceCap.util.Handler.getTextSize() + "px Segoe UI,Segoe,DejaVu Sans,Trebuchet MS,Verdana,sans-serif",
			"margin-top": IceCap.util.Handler.getMargin() + "px !important",
			"min-height": "0px !important"
		};

		Ext.apply(o_field, {
			fieldStyle: o_style
		});

		if (o_config.atr == "27") {
			o_field.hidden = true;
		}

		Ext.apply(i_self, o_field);

		i_self.callParent();

		if (o_config.atr == 23) {

			i_self.addCls("icecap-invalid");
		}
	}
});Ext.define("IceCap.ux.form.field.Field", {

	extend: "Ext.form.FieldContainer",

	alias: [
		"widget.iField"
	],

	config: {
		theme: "default",
		field: null
	},

	border: false,

	layout: "fit",

	mixins: [
		"Ext.form.field.Field"
	],

	listeners: {
		focus: {
			fn: function(i_container) {

				var i_field = i_container.getField();
				i_field.focus(true, 10);
			}
		}
	},

	initComponent: function () {

		var i_self = this;

		var o_config = i_self.initialConfig;

		var n_inputWidth = o_config.width || 80;

		var n_default = IceCap.util.Handler.getDefaultWidth();
		var n_width = n_default;
		if (o_config.len > 1) {
			n_width = ((o_config.len - 1) * IceCap.util.Handler.getColSize()) + n_default;
		}

		if (o_config.len > n_inputWidth) {
			var n_temp = n_inputWidth - o_config.col;
			n_width = ((n_temp - 1) * IceCap.util.Handler.getColSize()) + n_default;
		}

		Ext.apply(i_self, {
			width: n_width
		});

		i_self.callParent();

		i_self.createField();

		var i_field = i_self.getField();

		if (o_config.atr == 25) {

			if (o_field.readOnly) {
				i_self.addCls("icecap-invalid");
			}
			else {
				i_field.markInvalid(true);
			}
		}
	},

	focus: function() {

		var i_self = this;
		var i_field = i_self.getField();

		i_field.focus(true, 10);
		i_self.fireEvent("fieldfocus", i_self);
	},

	createField: function() {

		var i_self = this;

		var o_config = i_self.initialConfig;

		var o_meta = o_config.metaData || {};

		var o_field = {
			inputType: "text",
			xtype: "textfield"
		};

		Ext.apply(o_field, i_self.applyFFW(o_config));

		var s_type = o_field.xtype || "textfield";
		var b_password = o_config.atr == "27";

		if (o_field.readOnly) {

			o_field.xtype = s_type = "displayfield";

			Ext.apply(o_field.fieldStyle, {
				"padding": "1px 3px 2px 3px",
				"margin-top": IceCap.util.Handler.getMargin() + "px !important",
				"margin-left": "1px",
				"border": "1px solid transparent"
			});
		}

		var o_listeners = Ext.apply({}, o_meta.listeners);
		delete o_meta.listeners;

		var a_field = ["icecap-field", i_self.getTheme()];

		var a_pass = [b_password ? "iceCap-input-password" : "iceCap-input-" + (o_config.atr || 20)];

		a_pass.push(i_self.getTheme());

		var a_cls = [a_field.filter(Boolean).join("-"), a_pass.filter(Boolean).join("-")];

		var s_cls = a_cls.join(" ");

		Ext.Object.merge(o_field, {
			enforceMaxLength: true,
			maxLength: o_config.len,
			flex: 1,
			minWidth: IceCap.util.Handler.getDefaultWidth(),
			lin: o_config.lin,
			col: o_config.col,
			emuId: o_config.name,
			value: o_config.dta || "",
			cls: s_cls,
			enableKeyEvents: true,
			selectOnFocus: true,
			listeners: {
				afterrender: {
					fn: function(i_field) {

						if (i_field.inputEl && !i_field.metaField) {
							i_field.inputEl.setMinHeight(0);
							i_field.inputEl.setHeight(IceCap.util.Handler.getDefaultHeight());

							i_field.inputEl.set({
								autocomplete: "off"
							});
						}
					}
				},
				specialKey: {
					fn: function(i_field, i_event) {

						if (i_event.getKey() == Ext.event.Event.ENTER) {
							i_self.fireEvent("specialKey", i_self, i_event);
						}
					}
				},
				focus: {
					fn: function(i_field) {

						i_self.fireEvent("fieldfocus", i_self);
					}
				}
			}
		}, o_meta);

		var o_clone = Ext.clone(o_field);

		if (o_meta.xtype) {

			o_clone.metaField = true;

			o_clone.xtype = s_type = o_meta.xtype;

			delete o_clone.cls;
			delete o_clone.fieldStyle;
			delete o_clone.inputType;
		}

		delete o_clone.width;

		var i_field = Ext.widget(o_clone.xtype, o_clone);

		i_self.setField(i_field);

		i_self.add(i_field);
	},

	getValue: function() {

		var i_self = this;
		var i_field = i_self.getField();
		return i_field.getValue();
	},

	setValue: function(value) {

		var i_self = this;
		var i_field = i_self.getField();
		return i_field.setValue(value);
	},

	applyFFW: function(o_config) {

		var i_self = this;
		var s_hex = o_config.ffw;
		var b_fixed = IceCap.util.Handler.getFixed();

		var o_field = {
			xtype: "textfield"
		};

		var o_fieldStyle = {
			font: "normal " + IceCap.util.Handler.getTextSize() + "px Segoe UI,Segoe,DejaVu Sans,Trebuchet MS,Verdana,sans-serif"
		};

		if (b_fixed) {

			Ext.apply(o_fieldStyle, {
				font: "normal " + IceCap.util.Handler.getTextSize() + "px Lucida Console, Courier New, monospace"
			});
		}

		if (o_config.ta) {

			Ext.apply(o_fieldStyle, {
				"text-align": o_config.ta
			});
		}

		if (s_hex) {

			var n_hex = parseInt(s_hex);

			var n_sanHex = IceCap.util.Handler.createIndicators(s_hex);

			if (n_sanHex & IceCap.util.Handler.FFW.BYPASS) {

				Ext.apply(o_field, {
					readOnly: true
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.ALPHA_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[a-z A-Z ,.-]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.NUMERIC_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[+\\-,. 0-9]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.DIGITS_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[0-9]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MOD_DATA) {

				Ext.apply(o_field, {
					listeners: {
						afterrender: {
							fn: function(i_field) {

								i_field.originalValue = 0xFF;
							},
							single: true
						}
					}
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.AUTO_ENTER) {

				// THIS WE DONT NEED IN THE MODERN WORLD...

				// Ext.apply(o_field, {
				// 	listeners: {
				// 		blur: {
				// 			fn: function(i_field) {
				// 				i_self.doKeyEvent("ENTER");
				// 			}
				// 		}
				// 	}
				// });
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MONOCASE) {

				Ext.apply(o_fieldStyle, {
					"text-transform": "uppercase"
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MAND_ENTER) {

				Ext.apply(o_field, {
					allowBlank: false
				});
			}

			if (o_config.atr == "27" && n_hex & (1 << 13)) {

				Ext.apply(o_field, {
					hidden: true,
					disabled: true
				});
			}
		}

		Ext.apply(o_field, {
			fieldStyle: o_fieldStyle
		});

		return o_field;
	}
});
Ext.define("IceCap.ux.form.panel.fusionChart", {

	extend: "Ext.panel.Panel",

	alias: [
		"widget.FUSIONCHART"
	],

	header: false,

	width: 500,

	height: 500,

	bodyPadding: 5,

	focusable: false,

	layout: "fit",

	config: {
		chartData: [],
		chartType: ""
	},

	border: false,

	constructor: function(o_config) {

		var i_self = this;


		Ext.apply(o_config, {
			chartData: o_config.data ? Ext.Array.from(o_config.data) : []
		});

		delete o_config.data;

		i_self.callParent([o_config]);

	},

	initComponent: function() {

		var i_self = this;
		i_self.callParent();

		var i_chart = i_self.createChart();

		i_self.add(i_chart);

	},

	createChart: function() {

		var i_self = this;

		var s_type = i_self.getChartType().toLowerCase();

		var i_chart;
		switch (s_type) {
			case "column3d":
				i_chart = i_self.createColumn3D(i_self.getChartData());
				break;
			case "column":
				i_chart = i_self.createColumn(i_self.getChartData());
				break;
			default:
				break;
		}

		return i_chart;
	},

	createColumn3D: function(a_input) {

		var a_data = [];

		var a_fields = [
			"category",
			"value"
		];

		Ext.Array.each(a_input, function(a_value) {

			var value = a_value[1] || 0;

			if(value) {
				value = value.replace(".", "").replace(",", ".");
				value = parseFloat(value);
			}

			var o_record = {
				category: a_value[0],
				value: value
			};

			a_data.push(o_record);
		});

		a_data = a_data.reverse();

		var i_store = Ext.create("Ext.data.Store", {
			proxy: {
				type: "memory",
				reader: {
					type: "json",
					rootProperty: "rows",
					totalProperty: "totalRows"
				}
			},
			fields: a_fields,
			data: a_data
		});

		var i_chart = Ext.widget("cartesian", {
			store: i_store,
			flex: 1,
			insetPadding: 40,
			axes: [
				{
					type: "numeric",
					position: "right",
					grid: true,
					fields: "value",
					label: {
						rotate: {
							degrees: -30
						}
					}
				},
				{
					type: "category3d",
					fields: "category",
					position: "bottom"
				}
			],
			series: {
				type: "bar3d",
				xField: "category",
				yField: "value",
				style: {
					maxBarWidth: 50,
					minGapWidth: 20
				},
				label: {
					field: "value",
					display: "insideEnd",
					renderer: function (value) {
						return value.toFixed(2);
					}
				}
			}
		});

		return i_chart;
	},

	createColumn: function (a_input) {

		var a_data = [];

		var a_fields = [
			"category",
			"value"
		];

		Ext.Array.each(a_input, function (a_value) {

			var value = a_value[1] || 0;

			if (value) {
				value = value.replace(".", "").replace(",", ".");
				value = parseFloat(value);
			}

			var o_record = {
				category: a_value[0],
				value: value
			};

			a_data.push(o_record);
		});

		a_data = a_data.reverse();

		var i_store = Ext.create("Ext.data.Store", {
			proxy: {
				type: "memory",
				reader: {
					type: "json",
					rootProperty: "rows",
					totalProperty: "totalRows"
				}
			},
			fields: a_fields,
			data: a_data
		});

		var i_chart = Ext.widget("cartesian", {
			store: i_store,
			flex: 1,
			insetPadding: 40,
			axes: [
				{
					type: "numeric",
					position: "right",
					grid: true,
					fields: "value",
					label: {
						rotate: {
							degrees: -30
						}
					}
				},
				{
					type: "category",
					fields: "category",
					position: "bottom"
				}
			],
			series: {
				type: "bar",
				xField: "category",
				yField: "value",
				style: {
					maxBarWidth: 50,
					minGapWidth: 20
				},
				label: {
					field: "value",
					display: "insideEnd",
					renderer: function (value) {
						return value.toFixed(2);
					}
				}
			}
		});

		return i_chart;
	}
});
Ext.define("IceCap.ux.form.field.SimpleComboBox", {

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

	alias: [
		"widget.COMBOBOX"
	],

	valueField: "key",

	displayField: "value",

	typeAhead: true,

	mode: "local",

	forceSelection: true,

	triggerAction: "all",

	selectOnFocus: true,

	minChars: 1,

	config: {
		valueMap: []
	},

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

				var n_key = i_event.getKey();
				
				if (i_field.isExpanded) {
					if (n_key == Ext.event.Event.DOWN || n_key == Ext.event.Event.UP) {
						i_event.stopEvent();
					}
				}
			}
		}
	},

	initComponent: function() {

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

		i_self.store = i_store;
		i_self.callParent();
	},

	createStore: function() {

		var i_self = this;

		var a_data = [];

		var a_valueData = i_self.getValueMap();

		Ext.Array.each(a_valueData, function(a_value) {

			if (a_value.length >= 2) {
				var o_data = {
					key: a_value[0],
					value: a_value[1]
				};

				a_data.push(o_data);
			}
		});

		var i_store = Ext.create("Ext.data.Store", {
			fields: [
				{
					name: "key",
					type: "auto"
				},
				{
					name: "value",
					type: "auto"
				}
			],
			autoLoad: true,
			data: a_data
		});

		return i_store;
	}
});
Ext.define("IceCap.ux.form.field.sqlComboBox", {

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

	alias: [
		"widget.DDSQL"
	],

	minChars: 1,

	valueField: "KEY",

	displayField: "VALUE",

	typeAhead: true,

	mode: "remote",

	triggerAction: "all",

	selectOnFocus: true,

	matchFieldWidth: false,

	editable: true,

	config: {
		sqlVoucher: ""
	},

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

				var n_key = i_event.getKey();
				
				if (i_field.isExpanded) {
					if (n_key == Ext.event.Event.DOWN || n_key == Ext.event.Event.UP) {
						i_event.stopEvent();
					}
				}
			}
		}
	},

	initComponent: function() {

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

		i_self.store = i_store;
		i_self.callParent();

		var i_model = i_store.getModel();

		var value = i_self.getValue();
		var rawValue = i_self.getRawValue();

		var i_created = i_model.create({
			KEY: rawValue,
			VALUE: value
		});

		if (i_created.get(i_created.idProperty)) {
			i_store.add(i_created);
		}
	},

	initValue: function() {

		var i_self = this;

		var i_store = i_self.getStore();

		var i_model = i_store.getModel();

		var value = i_self.value || null;

		var a_loaded = [];

		if (Ext.isEmpty(value)) {
			value = null;
		}

		var a_values = Ext.Array.from(value);

		var n_load = a_values.length;

		if (n_load) {
			var a_load = [];

			var a_result = [];

			Ext.Array.each(a_values, function(value) {

				var i_test = i_model.create({});
				i_test.set(i_model.idProperty, value);

				if (i_test.get(i_model.idProperty) === value) {
					a_load.push(value);
				}
			});

			if (a_load.length > 0) {

				i_store.load({
					callback: function(a_records, i_store) {

						a_records = Ext.Array.from(a_records);

						if (a_records.length === 0) {
							value = null;
						}

						i_self.suspendCheckChange++;
						i_self.setValue(value);
						i_self.suspendCheckChange--;
					}
				});

				i_self.originalValue = i_self.lastValue = value;
				i_self.resetOriginalValue();
			}
			else {

				i_self.suspendCheckChange++;
				i_self.setValue(null);
				i_self.suspendCheckChange--;
				i_self.originalValue = i_self.lastValue = null;
				i_self.resetOriginalValue();
			}
		}
		else {
			i_self.originalValue = i_self.lastValue = i_self.getValue();
			i_self.resetOriginalValue();
		}
	},

	createStore: function() {

		var i_self = this;

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

		return i_store;
	}
});
Ext.define("IceCap.ux.form.field.sqlSearchBox", {

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

	alias: [
		"widget.DDSEARCH"
	],

	minChars: 1,
	maxLength: 10,
	valueField: "KEY",
	displayField: "VALUE",
	forceSelection: false,
	typeAhead: false,
	pageSize: 25,
	matchFieldWidth: false,

	config: {
		sqlVoucher: ""
	},

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

				var n_key = i_event.getKey();
				
				if (i_field.isExpanded) {
					if (n_key == Ext.event.Event.DOWN || n_key == Ext.event.Event.UP) {
						i_event.stopEvent();
					}
				}
			}
		},
		select: {
			fn: function(i_field, i_record, n_number) {

				var a_fields = i_record.getFields();

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

					var a_elements = document.getElementsByName(i_recordField.name);

					if (a_elements.length > 0) {

						var i_element = Ext.get(a_elements[0]);
						var i_component = i_element.component;

						var value = i_record.get(i_recordField.name);

						if (i_component) {
							i_component.setValue(value);
						}
						else {
							i_element.dom.value = value;
						}
					}
				});
			}
		}
	},

	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=\".\">",
				"<li role=\"option\" class=\"x-boundlist-item\" style=\"text-overflow: ellipsis; white-space: nowrap; overflow:hidden;\">",
					"{VALUE}",
				"</li>",
			"</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"
				}
			],
			pageSize: i_self.pageSize || 25,
			proxy: {
				url: "/icecap/DDSQL.aspx?runSql=runsql&sqlVoucher=" + i_self.getSqlVoucher(),
				type: "ajax",
				reader: {
					type: "json"
				}
			}
		});

		return i_store;
	}
});
Ext.define("IceCap.ux.form.field.LINKFIELD", {

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

	alias: [
		"widget.LINKFIELD"
	],

	triggers: {
		switch: {
			cls: "icecap-form-link-trigger",
			overCls: "no-overcls",
			clickCls: "no-clickcls",
			weight: 1,
			scope: "this",
			handler: function(i_trigger) {

				var i_field = this;

				if (!i_field.getValue()) {
					Ext.MessageBox.alert("Message", "Url required");
				}
				else {
					window.open(i_field.getValue(), i_field.getValue(), "");
				}
			}
		}
	},

	validationEvent: false,
	validateOnBlur: false,

	initComponent: function () {

		var i_self = this;
		i_self.callParent();
	}
});
Ext.define("IceCap.ux.form.field.AdvancedComboBox", {

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

	alias: [
		"widget.DDLIB"
	],

	minChars: 1,  // character needed to start server search,
	maxLength: 10,
	displayField: "key",
	forceSelection: false,
	valueField: "key",
	typeAhead: false,
	pageSize: 25,
	hideTrigger: false,
	matchFieldWidth: false,

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

				var n_key = i_event.getKey();
				
				if (i_field.isExpanded) {
					if (n_key == Ext.event.Event.DOWN || n_key == Ext.event.Event.UP) {
						i_event.stopEvent();
					}
				}
			}
		},
	},

	initComponent: function() {

		var i_self = this;
		i_self.tpl = i_self.createTemplate();

		var i_store = Ext.create("Ext.data.Store", {
			proxy: {
				url: "/icecap/ddlibs.aspx?func=getRows",
				type: "ajax",
				reader: {
					type: "json"
				}
			},
			pageSize: i_self.pageSize || 25,
			fields: [
				{
					name: "key",
					type: "auto"
				},
				{
					name: "value",
					type: "auto"
				}
			],
			autoLoad: true
		});

		i_self.store = i_store;

		i_self.callParent();
	},

	createTemplate: function() {

		var i_template = new Ext.XTemplate(
			"<tpl for=\".\">",
				"<li role=\"option\" class=\"x-boundlist-item\" style=\"text-overflow: ellipsis; white-space: nowrap; overflow:hidden;\">",
					"<b>{key} - </b> {value}",
				"</li>",
			"</tpl>"
		);

		return i_template;
	}
});
Ext.define("IceCap.ux.form.field.CommandField", {

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

	alias: [
		"widget.CommandField"
	],

	config: {
		fldNo: 0
	},

	cls: "iceCap-input-field"

});
Ext.define("IceCap.ux.form.field.LibraryFileDependency", {

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

	alias: [
		"widget.LibraryFileDependency"
	],

	listeners: {
		blur: {
			fn: function(i_field) {

				var i_self = this;
				var i_ownerCt = i_self.ownerCt;

				var i_owner = Ext.getCmp(i_ownerCt.id);

				var a_inputs = i_owner.query("input");

				Ext.Array.each(a_inputs, function(i_input) {

					if (i_input.getStore) {

						var i_store = i_input.getStore();
						var i_proxy = i_store.getProxy();

						var o_params = i_proxy.getExtraParams();

						Ext.apply(o_params, {
							lib: i_field.getValue()
						});

						i_store.load();
					}
				});
			}
		}
	}
});
Ext.define("IceCap.ux.form.field.FileLibraryDependency", {

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

	alias: [
		"widget.FileLibraryDependency"
	],

	valueField: "obj",
	displayField: "txt",
	fieldLabel: "File",

	typeAhead: true,

	mode: "remote",

	triggerAction: "all",

	selectOnFocus: true,

	matchFieldWidth: false,

	editable: true,

	config: {
		lib: "qtemp",
		obj: "*all",
		typ: "*all",
		atr: "*all"
	},

	initComponent: function() {

		var i_self = this;
		i_self.tpl = i_self.createTemplate();

		var i_store = Ext.create("Ext.data.Store", {
			params: {
				lib: i_self.getLib(),
				obj: i_self.getObj(),
				typ: i_self.getTyp(),
				atr: i_self.getAtr()
			},
			proxy: {
				url: "/icecap/ddlibs.aspx?func=getRows",
				type: "ajax",
				reader: {
					type: "json"
				}
			},
			fields: [
				{
					name: "obj",
					type: "auto"
				},
				{
					name: "txt",
					type: "auto"
				}
			],
			autoLoad: true
		});

		i_self.store = i_store;

		i_self.callParent();
	},

	createTemplate: function() {

		var i_template = new Ext.XTemplate(
			"<tpl for=\".\">",
				"<li role=\"option\" class=\"x-boundlist-item\" style=\"text-overflow: ellipsis; white-space: nowrap; overflow:hidden;\">",
					"<b>{key} - </b> {value}",
				"</li>",
			"</tpl>"
		);

		return i_template;
	}
})
Ext.define("IceCap.ux.form.field.RadioGroup", {

	extend: "Ext.form.RadioGroup",

	alias: [
		"widget.RADIOGROUP"
	],

	config: {
		valueMap: []
	},

	layout: {
		type: "hbox",
		align: "stretch"
	},

	flex: 1,

	defaults: {
		margin: "0 20 0 0"
	},

	initComponent: function() {

		var i_self = this;

		i_self.items = i_self.createButtons();

		i_self.on({
			afterender: {
				fn: function(i_field) {

					var value = i_field.value;

					if (value) {

						i_field.setValue(value);
					}
				}
			},
			specialkey: {
				fn: function(i_field, e) {

					if (e.keyCode == 39 || e.keyCode == 37) {

						var n_index = 0;

						var b_found = false;

						var n_len = i_field.items.length;

						var a_items = i_field.getBoxes();

						Ext,Array.each(a_items, function(i_radio) {
							if (i_radio.innerWrap.id == e.target.id) {
								b_found = true;
							}
							if (!b_found) {
								n_index++;
							}
						});

						if (e.keyCode == 39) {
							n_index++;
						}
						else if (e.keyCode == 37) {
							n_index--;
						}
						if (b_found) {

							var i_radio;

							if (n_index == n_len) {
								i_radio = i_field.items.get(0);
							}
							else {
								if (n_index < 0) {
									i_radio = i_field.items.get(n_len - 1);
								}
								else {
									i_radio = i_field.items.get(n_index);
								}
							}

							i_radio.innerWrap.focus();

							i_field.setValue(i_radio.inputValue);
						}
					}
				}
			}
		});

		i_self.width = null;
		i_self.flex = 1;

		i_self.callParent(arguments);

		return i_self;
	},

	createButtons: function(a_values) {

		var i_self = this;
		var a_values = i_self.getValueMap();
		var a_return = [];

		Ext.Array.each(a_values, function(o_input) {

			var o_radio = {
				inputValue: o_input.key,
				boxLabel: o_input.value,
				name: i_self.name,
				flex: 1
			};
			a_return.push(o_radio);
		});

		return a_return;
	},

	getValue: function() {

		var i_self = this;
		var value = i_self.value;

		var a_items = i_self.getBoxes();

		Ext.Array.each(a_items, function(i_radio) {
			if (i_radio.inputValue == value) {
				value = i_radio.getGroupValue();
			}
		});

		return value;
	},

	setValue: function(value) {

		var i_self = this;

		var a_items = i_self.getBoxes();

		Ext.Array.each(a_items, function(i_radio) {
			if (i_radio.inputValue == value) {
				i_radio.setValue(true);
			}
			else {
				i_radio.setValue(false);
			}
		});

		i_self.value = value;
	},

	setFocus: function() {

		var i_self = this;
		var b_found = false;

		var a_items = i_self.getBoxes();

		Ext.Array.each(a_items, function(i_radio) {
			if (i_radio.checked) {
				b_found = true;
				i_radio.innerWrap.focus();
				i_self.setValue(i_radio.inputValue);
			}
		});

		if (!b_found) {
			var i_radio = i_self.items.get(0);
			i_radio.innerWrap.focus(true);
			i_self.setValue(i_radio.inputValue);
		}
	}
});
Ext.define("IceCap.ux.container.CommandPanel", {
	
	extend: "Ext.panel.Panel",

	alias: [
		"widget.COMMANDPANEL"
	],
    
    focusable: false,

    width:280,

    height:24,

    border: false,

    header: false,

 	initComponent: function() {

         var i_self = this;
         
         i_self.buttons = i_self.createButtons();

        i_self.callParent(arguments);

        i_self.add(i_self.createItems());
     },
     
     createItems: function() {

        var a_items = [];

        var i_store = Ext.create("Ext.data.Store", {
            autoLoad: true,
			fields: [
				"cmd",
				"description"
			],
			data:[
				{
					cmd: "wrksplf",
					description: "Work with spooled files"
				},
				{
					cmd: "wrksplf",
					description: "Work with spooled files"
				}
			]
        });

        var i_comboBox = Ext.widget("combobox", {
            store: i_store,
            displayField: "description",
            valueField: "cmd",
            width: 250,
            editable: false,
            selectOnFocus: true,
            emptyText: "Select a command and click OK..."
        });

        a_items.push(i_comboBox);

        return a_items;
     },

     createButtons: function() {

        var i_self = this;

        var a_buttons = [];

        var i_bntOK = Ext.widget("button", {
            text: "OK",
            handler: function() {
                debugger;
            }
        });

        a_buttons.push(i_bntOK);

        return a_buttons;
     }
});Ext.define("IceCap.ux.container.GMapPanel", {
	extend: "Ext.panel.Panel",

	alias: "widget.icecap-gmappanel",

	config: {
		center: null,
		mapOption: {},
		scaleControl: true,
		geoCoder: null,
		map: null,
		markers: [],
		zoom: 7,
		stored: null,
		added: [],
        api: "//maps.googleapis.com/maps/api/js",
        gestureHandling: "cooperative"
	},

	border: false,

	listeners: {
		afterrender: {
			fn: function(i_panel) {

				var i_self = this;

				var o_google = window.google || {};

				if (o_google.maps) {
					i_self.initMap();
				}
				else {
					var i_entry = Ext.Boot.getEntry(i_self.getApi(), {
						crossDomain: true,
						listeners: [
							function(i_entry) {
								o_google = window.google || {};
								if (o_google.maps) {
									i_self.initMap();
								}
								else {
									Ext.raise("Error loading google maps api at :" + i_self.getApi());
								}
							}
						]
					});
					i_entry.load(false);
				}
			},
			buffer: 10
		},
		beforedestroy: {
			fn: function(i_panel) {

				var i_self = this;
				var a_markers = i_self.getAdded();

				Ext.Array.each(a_markers, function(i_marker) {
					i_marker.setMap(null);
				});

				i_self.setAdded(null);
				i_self.setMap(null);
				i_self.setStored(null);
				i_self.setCenter(null);
				i_self.setGeoCoder(null);
			}
		}
	},

	initComponent: function() {

		var i_self = this;
		i_self.callParent();
	},

	initMap: function() {

		var i_self = this;

		var o_center = i_self.getCenter() || {
			lat: 55.846330,
			lng: 10.593858
		};

		var o_stored = i_self.getStored() || "";

		if (o_stored) {
			if (o_stored.lat && o_stored.lng) {
				o_center = o_stored;
			}
		}

		if (o_center) {
			if (o_center.geoCodeAddr) {
				i_self.lookupCode(o_center.geoCodeAddr, o_center.marker);
			}
			else {
				i_self.createMap(o_center);
			}
		}
		else {
			Ext.raise("center is required");
		}
	},

	createMap: function(o_center, o_marker) {

		var i_self = this;
		var a_markers = i_self.getMarkers();
		var google = window.google;

		var o_options = Ext.apply({}, i_self.mapOptions);

		o_options = Ext.applyIf(o_options, {
			zoom: i_self.getZoom(),
			center: o_center,
			mapTypeId: google.maps.MapTypeId.MAP,
            scaleControl: i_self.getScaleControl(),
            gestureHandling: i_self.getGestureHandling()
		});

		var i_map = new google.maps.Map(i_self.body.dom, o_options);

		i_self.setMap(i_map);

		if (o_marker) {
			i_self.addMarker(Ext.applyIf(o_marker, {
				position: o_center
			}));
		}

		Ext.Array.each(a_markers, i_self.addMarker, i_self);

		i_self.setCenter(o_center);

		i_self.fireEvent("mapready", i_self, i_map);
	},

	addMarker: function(o_config) {

		var i_self = this;
		var google = window.google;

		o_config = Ext.apply({
			map: i_self.getMap()
		}, o_config);

		if (!o_config.position) {
			o_config.position = new google.maps.LatLng(o_config.lat, o_config.lng);
		}
		var i_marker = new google.maps.Marker(o_config);

		Ext.Object.each(i_marker.listeners, function(s_name, f_handler) {
			google.maps.event.addListener(i_marker, s_name, f_handler);
		});

		var a_added = i_self.getAdded();
		a_added.push(i_marker);

		return i_marker;
	},

	removeMarker: function(i_marker) {

		var i_self = this;
		var a_added = i_self.getAdded();

		Ext.Array.remove(a_added, i_marker);

		i_marker.setMap(null);
	},

	lookupCode: function(addr, marker) {

		var i_self = this;
		var i_geocoder = i_self.getGeoCoder();
		var google = window.google;

		if (!i_geocoder) {
			i_geocoder = new google.maps.Geocoder();
			i_self.setGeoCoder(i_geocoder);
		}

		i_geocoder.geocode({
			address: addr
		}, Ext.Function.bind(i_self.onLookupComplete, i_self, [marker], true));
	},

	lookupAddr: function(addr, f_callback) {

		var i_self = this;

		f_callback = f_callback || function(o_data) {
			var i_element = i_self.getEl();
			if (i_element && i_element.dom) {
				if (i_self.getMap()) {
					var i_map = i_self.getMap();
					if (o_data[0]) {
						i_map.setCenter(o_data[0].geometry.location);

						var a_markers = i_self.getAdded();

						Ext.Array.each(a_markers, function(i_marker) {
							i_marker.setMap(null);
						});

						i_self.setAdded([]);

						i_self.addMarker({
							position: o_data[0].geometry.location
						});
					}
				}
				else {
					if (o_data[0]) {
						i_self.setStored(o_data[0].geometry.location);
					}
				}
			}
			else {
				if (o_data[0]) {
					i_self.setStored(o_data[0].geometry.location);
				}
			}
		};

		var i_geocoder = i_self.getGeoCoder();
		var google = window.google;

		if (!i_geocoder) {
			i_geocoder = new google.maps.Geocoder();
			i_self.setGeoCoder(i_geocoder);
		}

		i_geocoder.geocode({
			address: addr
		}, Ext.Function.bind(f_callback, i_self, [{}], true));
	},

	onLookupComplete: function(o_data, response, marker) {

		var i_self = this;

		if (response != "OK") {
			Ext.MessageBox.alert("Error", "An error occured: \"" + response + "\"");
			return;
		}
		var i_element = i_self.getEl();
		if (i_element.dom) {
			i_self.createMap(o_data[0].geometry.location, marker);
		}
		else {
			i_self.setStored(o_data[0].geometry.location);
		}
	},

	afterComponentLayout: function(w, h) {

		var i_self = this;

		i_self.callParent(arguments);
		i_self.redraw();
	},

	redraw: function() {

		var i_self = this;
		var google = window.google;
		var i_map = i_self.getMap();

		if (i_map) {
			google.maps.event.trigger(i_map, "resize");
		}
	}

});

Ext.define("IceCap.controller.Window", {
	extend: "Ext.app.ViewController",

	alias: "controller.icecap.view.window",

	onSendData: function(o_config, o_data) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var s_session = i_view.getSessionValue();

		var o_options = i_view.getOptions();

		o_data = Ext.apply(o_data, {
			i18nLearning: false
		});

		Ext.apply(o_config, {
			session: s_session
		});

		Ext.apply(o_config, o_options);

		if (!i_view.getWorking()) {
			i_view.setWorking(true);
			i_view.doTimer();
			IceCap.util.Handler.getData(o_config, o_data, function(o_inputData) {

				if (o_inputData.statusUpdate) {

					i_view.setWorking(false);
					i_view.updateMessage(o_inputData);
					if (o_inputData.status > -1) {
						i_view.doCreateFromData(o_inputData);
					}
					i_controller.onSendData(o_saved, o_data);
				}
				else {

					i_view.setLoading(false);
					i_view.cancelTimer();

					var f_callback = i_view.getResponseCallBack() || Ext.emptyFn;

					if (f_callback != Ext.emptyFn) {

						f_callback(o_inputData);
					}

					var o_plugin = o_inputData.plugin || Ext.apply({}, {
						require: []
					});

					var a_require = o_plugin.require || [];
					var s_run = o_plugin.run || "";

					i_controller.doRequire(a_require, function() {

						if (s_run) {
							eval(s_run);
						}

						if (o_inputData.close) {
							i_view.fireEvent("applicationClose", true);
						}
						else {

							i_view.setWorking(false);
							i_view.doCreateFromData(o_inputData);
						}
					});
				}
			});
		}
	},

	onWheel: function(i_event) {

		var i_controller = this;
		var i_view = i_controller.getView();

		var b_hasGrid = i_view.query("gridpanel").length > 0;
		var b_mappanel = i_view.query("icecap-gmappanel").length > 0;

		var n_delta = i_event.getWheelDelta();
		var s_event = "";

		if (n_delta > 0) {
			s_event = "ROLLDOWN";
		}
		else {
			s_event = "ROLLUP";
		}

		if (!b_hasGrid && !b_mappanel && !i_event.ctrlKey) {
			i_controller.doKeyEvent(s_event);
		}
	},

	onDblClick: function(i_event) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var i_cursor = i_view.getCursor();

		var b_fireEvent = i_view.getEnterOnDblClick();

		if (b_fireEvent) {

			var i_body = i_view.body;
			var a_local = i_body.getOffsetsTo(window);

			var o_click = Ext.apply({
				x: i_event.pageX - a_local[0],
				y: i_event.pageY - a_local[1]
			});

			var o_position = IceCap.util.Handler.toColAndLine(o_click);

			var i_currentField = i_view.getCurrentField();

			if (i_currentField) {
				o_position = Ext.apply(o_position, {
					lin: i_currentField.lin,
					col: i_currentField.col
				});
				if (i_currentField.inputEl && i_currentField.getValue()) {
					o_position = IceCap.util.Handler.toColAndLine(o_click);
				}
			}
			else {
				i_cursor.moveCursor(o_position);
				i_cursor.show();
			}

			i_view.setCurrentPosition(o_position);
			i_controller.doKeyEvent("ENTER");
		}
	},

	onClick: function(i_event) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var o_inputData = i_view.getInputData();
		var i_cursor = i_view.getCursor();

		var i_body = i_view.body;

		var a_viewLocal = i_view.getLocalXY();
		var a_local = i_body.getOffsetsTo(window);

		var o_click = Ext.apply({
			x: i_event.pageX - a_local[0],
			y: i_event.pageY - a_local[1]
		});

		var o_position = IceCap.util.Handler.toColAndLine(o_click);

		var i_currentField = i_view.getCurrentField();

		var a_metaFields = o_inputData.metaFields || [];

		if (i_currentField) {
			o_position = Ext.apply(o_position, {
				lin: i_currentField.lin,
				col: i_currentField.col
			});
			if (i_currentField.inputEl && i_currentField.getValue()) {
				o_position = IceCap.util.Handler.toColAndLine(o_click);
			}
		}
		else {
			var a_inputs = i_view.query("field[focusable]");

			var o_box = Ext.apply({
				x: IceCap.util.Handler.getX(o_position.col),
				y: IceCap.util.Handler.getY(o_position.lin),
				width: IceCap.util.Handler.getColSize(),
				height: 1
			});

			var b_intersect = false;

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

				var i_element = i_field.getEl();

				if (i_element) {
					var o_bound = i_field.el.dom.getBoundingClientRect();

					var o_rect = Ext.apply({}, {
						x: i_field.getLocalX(),
						y: i_field.getLocalY(),
						width: o_bound.width,
						height: o_bound.height
					});

					if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
						b_intersect = true;
						i_field.focus(false, 10);
						return false;
					}
				}
			});

			if (!b_intersect) {
				i_cursor.moveCursor(o_position);

				i_cursor[a_metaFields.length === 0 ? "show" : "hide" ]();
			}
		}

		i_view.setCurrentPosition(o_position);
	},

	doArrowKeys: function(n_direction) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var a_fields = i_view.query("[focusable]");
		var n_tabIndex = i_view.getCurrentTabIndex();
		var n_lastTabIndex = i_view.getLastTabIndex();
		var n_part = n_direction ? 1 : -1;

		var i_lastFocus;

		Ext.Array.each(a_fields, function(i_field) {
			if (i_field.getTabIndex() === n_tabIndex) {
				i_lastFocus = i_field;
				return false;
			}
		});

		var b_found = false;

		if (i_lastFocus) {

			var n_lin = i_lastFocus.lin;
			var n_col = i_lastFocus.col;
			var n_next = n_lin + n_part;

			Ext.Array.each(a_fields, function(i_field) {
				if (i_field.lin === n_next && i_field.col === n_col) {
					b_found = true;
					n_tabIndex = i_field.getTabIndex();
					i_field.focus(false, 10);
					return false;
				}
			});
		}

		if (!b_found) {

			n_tabIndex = i_view.getCurrentTabIndex() + n_part;

			if (n_direction) {
				if (n_tabIndex > n_lastTabIndex) {
					n_tabIndex = 1;
				}
			}
			else {
				if (n_tabIndex < 1) {
					n_tabIndex = n_lastTabIndex;
				}
			}

			Ext.Array.each(a_fields, function(i_field) {
				if (i_field.getTabIndex() === n_tabIndex) {
					i_field.focus(false, 10);
					return false;
				}
			});
		}

		i_view.setCurrentTabIndex(n_tabIndex);
	},

	doTabKey: function(n_direction) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var a_fields = i_view.query("[focusable]");
		var n_tabIndex = i_view.getCurrentTabIndex();
		var n_lastTabIndex = i_view.getLastTabIndex();
		var n_part = n_direction ? 1 : -1;

		n_tabIndex = i_view.getCurrentTabIndex() + n_part;

		if (n_direction) {
			if (n_tabIndex > n_lastTabIndex) {
				n_tabIndex = 1;
			}
		}
		else {
			if (n_tabIndex < 1) {
				n_tabIndex = n_lastTabIndex;
			}
		}

		Ext.Array.each(a_fields, function(i_field) {
			if (i_field.getTabIndex() === n_tabIndex) {
				i_field.focus(false, 10);
				return false;
			}
		});

		i_view.setCurrentTabIndex(n_tabIndex);
	},

	doKeyEvent: function(s_value) {

		var i_controller = this;
		var i_view = i_controller.getView();

		var i_currentField = i_view.getCurrentField();
		var o_position = i_view.getCurrentPosition();

		var o_data = {
			key: s_value
		};

		var i_content = i_view.getContent();
		var i_form = i_content.getForm();
		var i_collectionField = i_form.getFields();
		var o_values = {};

		Ext.Array.each(i_collectionField.items, function(i_field) {
			if (i_field.isDirty()) {
				var value = i_field.getSubmitValue ? i_field.getSubmitValue() : i_field.getValue();
				o_values[i_field.itemId || i_field.initialConfig.id || i_field.name] = value;
			}
		});

		Ext.Object.each(o_values, function(s_key, value) {
			var s_id = "ses2.win." + s_key;
			o_data[s_id] = value;
		});

		if (i_currentField) {

			o_data = Ext.apply(o_data, {
				csrLin: i_currentField.lin,
				csrCol: i_currentField.col
			});

			if (i_currentField.inputEl && i_currentField.getValue()) {

				var s_id = "ses2.win." + i_currentField.emuId;

				o_data = Ext.apply(o_data, {
					csrCol: o_position.col > 1 ? o_position.col : i_currentField.col
				});
			}
		}
		else {

			o_data = Ext.apply(o_data, {
				csrLin: o_position.lin,
				csrCol: o_position.col
			});
		}

		var f_callback = i_view.getRequestCallBack() || Ext.emptyFn;

		if (f_callback != Ext.emptyFn) {

			f_callback(o_data);
		}

		i_controller.onSendData({
			func: ""
		}, o_data);
	},

	doFKey: function(s_value) {

		var i_controller = this;

		i_controller.doKeyEvent(s_value);
	},

	doRequire: function(a_require, f_callback) {

		f_callback = f_callback || Ext.emptyFn;

		var a_require = a_require || [];

		if (a_require.length > 0) {

			var a_external = Ext.Array.filter(Ext.Array.map(a_require, function(s_value) {
				if (s_value.indexOf("/") > -1) {
					return s_value;
				}
			}), function(value) {
				return value;
			});

			var a_internal = Ext.Array.filter(Ext.Array.map(a_require, function(s_value) {
				if (s_value.indexOf("/") < 0) {
					return s_value;
				}
			}), function(value) {
				return value;
			});

			if (a_external.length > 0) {
				Ext.Loader.loadScriptsSync(a_external);
			}

			if (a_internal.length > 0) {
				Ext.syncRequire(a_internal);
			}
		}

		f_callback();
	}
});
Ext.define("IceCap.view.Window", {
	extend: "Ext.window.Window",

	layout: "fit",

	requires: [
		"IceCap.controller.Window"
	],

	config: {
		inputData: null,
		lastTabIndex: 1,
		currentTabIndex: 1,
		content: null,
		keyArea: null,
		working: false,
		needFocus: 1,
		currentPosition: {
			col: 1,
			lin: 1
		},
		statusBar: null,
		main: null,
		sessionValue: "",
		cursor: null,
		timer: null,
		theme: "",
		requestCallBack: Ext.emptyFn,
		responseCallBack: Ext.emptyFn,
		enterOnDblClick: false,
		marker: null,
		markerActive: false,
		options: {},
		typeMode: 1
	},

	closable: false,
	modal: false,

	controller: "icecap.view.window",

	listeners: {
		afterrender: {
			fn: function(i_panel) {

				var i_self = i_panel;

				i_self.addKeyEvents();

				var i_content = i_self.getContent();

				if (i_content) {

					var o_inputData = i_self.getInputData();

					var a_fields = i_content.query("field[focusable]");
					var b_found = false;
					var b_error = false;

					Ext.Array.each(a_fields, function(i_field) {
						if (i_field.getTabIndex() === i_self.getNeedFocus()) {
							if (!b_error) {
								setTimeout(function() {
									i_field.focus(false, 10);
								}, 10);
							}
							b_found = true;

							return false;
						}
					});

					if (!b_found) {

						i_self.setCurrentPosition({
							col: 2,
							lin: 1
						});

						if (!b_error) {
							setTimeout(function() {
								i_self.focus(true, 10);
							}, 10);
						}
					}
				}
			},
			buffer: 100
		},
		dblclick: {
			fn: "onDblClick",
			element: "body"
		},
		click: {
			fn: "onClick",
			element: "body"
		},
		wheel: {
			fn: "onWheel",
			element: "body"
		},
		fieldoption: {
			fn: function(a_config) {

				var i_self = this;
				var i_controller = i_self.getController();

				var o_data = {};

				Ext.apply(o_data, {
					key: "ENTER"
				});

				Ext.Array.each(a_config, function(o_config) {

					var s_field = o_config.field;

					o_data[s_field] = o_config.option;

					Ext.apply(o_data, {
						csrCol: o_config.col,
						csrLin: o_config.lin
					});
				});

				i_self.setCurrentPosition({
					col: o_data.csrCol,
					lin: o_data.csrLin
				});

				i_controller.onSendData({
					func: ""
				}, o_data);
			}
		},
		key: {
			fn: function(s_value) {

				var i_self = this;
				var i_controller = i_self.getController();

				i_controller.doKeyEvent(s_value);
			}
		}

	},

	onKeyDown: Ext.emptyFn, // THIS IS NEEDED TO OVERRIDE Ext.util.Floating

	initComponent: function() {

		var i_self = this;

		var o_params = Ext.Object.fromQueryString(window.location.search);

		Ext.Object.each(o_params, function(s_key, value) {
			o_params[s_key] = Ext.decode(value, true) || value;
		});

		var o_options = Ext.apply(i_self.options, o_params.options || {});

		Ext.apply(i_self, {
			theme: i_self.theme || o_params.theme || ""
		});

		i_self.callParent(arguments);

		i_self.setTypeMode(IceCap.util.Handler.typeMode.INSERT);

		i_self.add(i_self.createLayout());

		i_self.enableBubble([
			"onAddWebContainer",
			"toWindow",
			"applicationClose"
		]);

		i_self.recreate();
	},

	createStatusBar: function() {

		var i_statusbar = Ext.widget("toolbar", {
			dock: "bottom",
			cls: "icecap-statusbar",
			height: 30,
			hidden: false,
			items: [
				{
					xtype: "label",
					itemId: "icecap-message"
				},
				"->",
				{
					xtype: "label",
					itemId: "icecap-position"
				},
				{
					xtype: "label",
					text: " INS",
					itemId: "icecap-typemode"
				}
			],
			setText: function (s_text) {

				var i_toolbar = this;

				var i_message = i_toolbar.getComponent("icecap-message");

				if (i_message) {
					i_message.setText(s_text);
				}
			}
		});

		return i_statusbar;
	},

	createLayout: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();

		return i_self.createContent();
	},

	createContent: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();
		var n_tabIndex = o_inputData.input.length > 0 ? 0 : 1;
		var b_focusable = o_inputData.input.length > 0 ? false : true;

		var i_panel = Ext.widget("panel", {
			layout: {
				type: "vbox",
				align: "stretch"
			},
			border: false,
			focusable: b_focusable,
			tabIndex: n_tabIndex,
			items: i_self.createItems()
		});

		i_self.setMain(i_panel);

		return i_panel;
	},

	createItems: function() {

		var i_self = this;

		var o_inputData = i_self.getInputData();

		var a_items = [];

		var i_center = Ext.widget("form", {
			bodyPadding: 5,
			border: false,
			layout: "absolute",
			flex: 1,
			ui: ["icecap", i_self.getTheme()].filter(Boolean).join("-")
		});

		a_items.push(i_center);

		var i_south = Ext.widget("panel", {
			layout: {
				type: "column"
			},
			border: false,
			bodyPadding: 5,
			defaults: {
				margin: "0 5 5 0"
			},
			ui: ["icecap", i_self.getTheme()].filter(Boolean).join("-")
		});

		a_items.push(i_south);

		i_self.setContent(i_center);
		i_self.setKeyArea(i_south);

		var i_statusbar = i_self.createStatusBar();

		i_self.addDocked(i_statusbar);

		i_self.setStatusBar(i_statusbar);

		i_self.updateMessage(o_inputData);

		return a_items;
	},

	createFields: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();

		var a_fields = [];

		var n_tabIndex = 1;
		var n_focus = o_inputData.focus;

		var n_inputWidth = o_inputData.width || 80;

		var n_inputHeight = o_inputData.height || 24;

		var b_fixed = IceCap.util.Handler.getFixed();

		var a_sliced = Ext.Array.from(o_inputData.input || []);

		a_sliced = a_sliced.concat(Ext.Array.from(o_inputData.fields || []));

		Ext.Array.each(a_sliced, function(o_config, n_index) {

			var n_default = IceCap.util.Handler.getDefaultWidth();
			var n_width = n_default;

			var o_meta = o_config.metaData || {};

			var o_field = {
				inputType: "text",
				xtype: "textfield"
			};

			if (o_config.len > 1) {
				n_width = ((o_config.len - 1) * IceCap.util.Handler.getColSize()) + n_default;
			}

			Ext.apply(o_field, i_self.applyFFW(o_config));

			var s_type = o_field.xtype || "textfield";
			var n_x = IceCap.util.Handler.getX(o_config.col) + (IceCap.util.Handler.getColSize() / 8);
			var n_y = IceCap.util.Handler.getY(o_config.lin) + 3;
			var b_password = o_config.atr == "27";

			if (o_field.readOnly) {

				o_field.xtype = s_type = "displayfield";

				Ext.apply(o_field.fieldStyle, {
					"font": "normal " + IceCap.util.Handler.getTextSize() + "px / " + IceCap.util.Handler.getColSize() + "px Lucida Console",
					"white-space": "pre",
					"margin-top": IceCap.util.Handler.getMargin() + "px !important",
					"min-height": "0px !important"
				});

				Ext.apply(o_field, {
					style: {
						padding: "1px 0px !important"
					}
				});

				Ext.apply(o_field, {
					fieldCls: ["x-form-display-field iceCap-output-" + (o_config.atr || 20), i_self.getTheme()].filter(Boolean).join("-")
				});
			}

			if (o_config.len > n_inputWidth) {
				var n_temp = n_inputWidth - o_config.col;
				n_width = ((n_temp - 1) * IceCap.util.Handler.getColSize()) + n_default;
			}

			if (o_config.hasOwnProperty("colsize") && o_config.hasOwnProperty("rows")) {

				var s_data = o_config.dta || "";

				o_field.xtype = s_type = "textarea";
				o_field.height = IceCap.util.Handler.getTextSize() * o_config.rows;
				o_field.preventScrollbars = true;

				var a_split = s_data.split("\n");

				var s_result = "";

				var n_split = a_split.length;

				Ext.Array.each(a_split, function (s_part, n_index) {

					var regex = new RegExp(".{" + o_config.colsize + "}", "g");

					var a_match = s_part.match(regex);

					if (a_match) {
						s_result += s_part;
					}
					else {
						if (n_index != n_split - 1) {
							s_result += s_part + "\n";
						}
						else {
							s_result += s_part;
						}
					}
				});

				o_config.dta = s_result;


				o_field.getSubmitValue = function () {

					var i_field = this;

					var s_value = i_field.stripReturns(i_field.getRawValue());

					var regex = new RegExp(".{1," + o_config.colsize + "}", "g");

					var a_split = s_value.match(regex) || [];

					return a_split.join("\n");
				};

			}

			var o_listeners = Ext.apply({}, o_meta.listeners);
			delete o_meta.listeners;

			var a_field = ["icecap-field", i_self.getTheme()];

			var a_pass = [b_password ? "iceCap-input-password" : "iceCap-input-" + (o_config.atr || 20)];

			a_pass.push(i_self.getTheme());

			var a_cls = [a_field.filter(Boolean).join("-"), a_pass.filter(Boolean).join("-")];

			var s_cls = a_cls.join(" ");

			if (isNaN(n_x)) {
				n_x = IceCap.util.Handler.getColSize();
			}

			if (isNaN(n_y)) {
				n_y = IceCap.util.Handler.getLineHeight();
			}

			Ext.Object.merge(o_field, {
				width: n_width,
				enforceMaxLength: true,
				maxLength: o_config.len,
				minWidth: IceCap.util.Handler.getDefaultWidth(),
				tabIndex: !o_field.readOnly ? n_tabIndex : -1,
				focusable: !o_field.readOnly,
				lin: o_config.lin,
				col: o_config.col,
				emuId: o_config.id,
				name: o_config.id,
				itemId: o_config.id,
				value: o_config.dta || "",
				inputAttrTpl: "spellcheck=false",
				x: n_x,
				y: n_y,
				cls: s_cls,
				enableKeyEvents: true,
				selectOnFocus: false,
				listeners: {
					afterrender: {
						fn: function(i_field) {

							if (i_field.inputEl && !i_field.metaField) {

								i_field.inputEl.setMinHeight(0);

								if (i_field.xtype == "textfield") {
									i_field.inputEl.setHeight(IceCap.util.Handler.getDefaultHeight());
								}
								if (i_field.xtype == "textarea") {
									i_field.inputEl.dom.setAttribute("rows", o_config.rows);
								}

								i_field.inputEl.set({
									autocomplete: "off"
								});

								if (i_field.getTabIndex() === i_self.getNeedFocus()) {

									i_field.focus(false, 10, function () {
										var o_inputData = i_self.getInputData();

										var s_value = i_field.getValue();
										var n_col = i_field.col;
										var n_diff = 0;

										if (n_col < o_inputData.csrCol) {
											n_diff = o_inputData.csrCol - n_col;
											i_field.inputEl.el.dom.selectionEnd = i_field.inputEl.el.dom.selectionStart = i_field.inputEl.el.dom.selectionStart + n_diff;
										}

										var o_position = {
											col: o_inputData.csrCol,
											lin: o_inputData.csrLin
										}

										i_self.setCurrentPosition(o_position);

										var i_cursor = i_self.getCursor();
										i_cursor.moveCursor(o_position);
										i_cursor.hide();
									});
								}
							}
						}
					},
					change: {
						fn: function(i_field, value) {

							var s_direction = i_field.inputEl.el.dom.selectionDirection;

							var n_curpos = s_direction == "forward" ? i_field.inputEl.el.dom.selectionEnd : i_field.inputEl.el.dom.selectionStart;

							if (value.length == i_field.maxLength && n_curpos == i_field.maxLength) {
								i_self.doNavigationKey(1, true);
							}
						}
					},
					focus: {
						fn: function(i_field) {

							var n_tabIndex = i_field.getTabIndex();
							i_self.setCurrentTabIndex(n_tabIndex);

							var o_position = {
								col: i_field.col,
								lin: i_field.lin
							};
							i_self.setCurrentPosition(o_position);

							var i_cursor = i_self.getCursor();
							i_cursor.moveCursor(o_position);
							i_cursor.hide();
						}
					},
					keyup: {
						fn: function(i_field, i_event) {

							if (i_field.xtype === "textfield") {
								var s_direction = i_field.inputEl.el.dom.selectionDirection;

								var n_start = s_direction == "forward" ? i_field.inputEl.el.dom.selectionEnd : i_field.inputEl.el.dom.selectionStart;
								var n_add = i_field.value.length * IceCap.util.Handler.getColSize();

								if (n_start != i_field.value.length) {
									n_add = n_start * IceCap.util.Handler.getColSize();
								}

								var o_input = Ext.apply({
									x: i_field.getLocalX() + n_add,
									y: i_field.getLocalY()
								});

								var o_position = IceCap.util.Handler.toColAndLine(o_input);
								i_self.setCurrentPosition(o_position);

								var i_cursor = i_self.getCursor();
								i_cursor.moveCursor(o_position);
							}
						}
					},
					keydown: {
						fn: function(i_field, i_event) {

							var i_regext = new RegExp("^\\w{1}$");

							var value = i_field.getValue();

							if (i_event.ctrlKey) {

								if (i_event.keyCode == Ext.event.Event.DOWN ||
									i_event.keyCode == Ext.event.Event.UP) {

									i_event.stopEvent();

									var n_y = IceCap.util.Handler.getLineHeight();

									if (i_event.keyCode == Ext.event.Event.UP) {
										n_y = -IceCap.util.Handler.getLineHeight();
									}

									var o_input = Ext.apply({
										x: i_field.getLocalX(),
										y: i_field.getLocalY() + n_y
									});

									var o_position = IceCap.util.Handler.toColAndLine(o_input);
									i_self.setCurrentPosition(o_position);

									var i_cursor = i_self.getCursor();
									i_cursor.moveCursor(o_position);

									var a_inputs = i_self.query("field[focusable]");

									var o_box = Ext.apply({
										width: IceCap.util.Handler.getColSize(),
										height: 1
									}, o_input);

									var b_intersect = false;

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

										var o_bound = i_field.el.dom.getBoundingClientRect();

										var o_rect = Ext.apply({}, {
											x: i_field.getLocalX(),
											y: i_field.getLocalY(),
											width: o_bound.width,
											height: o_bound.height
										});

										if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
											b_intersect = true;
											i_field.focus(false, 10);
											return false;
										}
									});

									if (!b_intersect) {

										i_field.blur();
										i_cursor.show();

										i_self.focus(true, 10);
									}
								}
								if (i_event.keyCode == Ext.event.Event.LEFT ||
									i_event.keyCode == Ext.event.Event.RIGHT) {

									var i_inputEl = i_field.inputEl;
									var n_selection = i_event.keyCode == Ext.event.Event.LEFT ? i_inputEl.dom.selectionStart : i_inputEl.dom.selectionEnd;

									if (n_selection == 0 && i_event.keyCode == Ext.event.Event.LEFT) {

										i_event.stopEvent();

										var n_x = IceCap.util.Handler.getLineHeight();

										var o_input = Ext.apply({
											x: i_field.getLocalX() - (n_x / 2),
											y: i_field.getLocalY()
										});

										var o_position = IceCap.util.Handler.toColAndLine(o_input);
										i_self.setCurrentPosition(o_position);

										var i_cursor = i_self.getCursor();
										i_cursor.moveCursor(o_position);

										var a_inputs = i_self.query("field[focusable]");

										var o_box = Ext.apply({
											width: IceCap.util.Handler.getColSize(),
											height: 1
										}, o_input);

										var b_intersect = false;

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

											var o_bound = i_field.el.dom.getBoundingClientRect();

											var o_rect = Ext.apply({}, {
												x: i_field.getLocalX(),
												y: i_field.getLocalY(),
												width: o_bound.width,
												height: o_bound.height
											});

											if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
												b_intersect = true;
												i_field.focus(false, 10);
												return false;
											}
										});

										if (!b_intersect) {

											i_field.blur();
											i_cursor.show();

											i_self.focus(true, 10);
										}
									}
									else if (n_selection == value.length && i_event.keyCode == Ext.event.Event.RIGHT) {

										i_event.stopEvent();

										var n_x = IceCap.util.Handler.getColSize();

										n_x = n_x * (i_field.maxLength + 2);

										var o_input = Ext.apply({
											x: i_field.getLocalX() + n_x,
											y: i_field.getLocalY()
										});

										var o_position = IceCap.util.Handler.toColAndLine(o_input);
										i_self.setCurrentPosition(o_position);

										var i_cursor = i_self.getCursor();
										i_cursor.moveCursor(o_position);

										var a_inputs = i_self.query("field[focusable]");

										var o_box = Ext.apply({
											width: IceCap.util.Handler.getColSize(),
											height: 1
										}, o_input);

										var b_intersect = false;

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

											var o_bound = i_field.el.dom.getBoundingClientRect();

											var o_rect = Ext.apply({}, {
												x: i_field.getLocalX(),
												y: i_field.getLocalY(),
												width: o_bound.width,
												height: o_bound.height
											});

											if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
												b_intersect = true;
												i_field.focus(false, 10);
												return false;
											}
										});

										if (!b_intersect) {

											i_field.blur();
											i_cursor.show();

											i_self.focus(true, 10);
										}
									}
								}
							}
							else {

								if (i_field.xtype == "textarea") {

									if (i_event.keyCode == Ext.event.Event.UP) {

										var s_value = i_field.stripReturns(i_field.getRawValue());

										var regex = new RegExp("^.*\n$|.{1," + o_config.colsize + "}", "gm");

										var a_lines = s_value.match(regex) || [];
										var a_newLines = s_value.split("\n");

										var n_start = i_field.inputEl.dom.selectionStart;

										var n_row = 1;
										var n_char = 0;
										var b_last = i_field.inputEl.dom.selectionStart == i_field.inputEl.dom.value.length;

										Ext.Array.each(a_lines, function (s_part, n_index) {

											n_char += s_part.length;

											if (n_char <= n_start) {
												n_row++;
											}
											else {
												return false;
											}
										});

										if (n_row > 1) {

											i_event.stopEvent();

											n_char = 0;
											var n_left = 0;
											var n_current = 0;
											var n_nextLine = 0;

											n_start = n_start - (a_newLines.length - 1);

											Ext.Array.each(a_lines, function (s_part, n_index) {

												n_char += s_part.length;

												var n_count = 0;

												n_left = 0;

												while (n_count < s_part.length) {
													n_count++;
													if (n_current < n_start) {
														n_current++;
														n_left++;
													}
												}

												if (n_char < n_start) {
													n_row++;
												}
												else {
													n_nextLine = n_index - 1;
													return false;
												}
											});

											if (n_nextLine == -1) {
												n_nextLine = 0;
											}

											var n_newPos = 0;

											Ext.Array.each(a_lines, function (s_part, n_index) {

												if (n_index < n_nextLine) {
													n_newPos += s_part.length;
												}
												else {
													n_newPos += n_left;
													return false;
												}
											});

											i_event.stopEvent();
											i_field.inputEl.dom.setSelectionRange(n_newPos, n_newPos);
										}
									}

									if (i_event.keyCode == Ext.event.Event.DOWN) {

										var s_value = i_field.stripReturns(i_field.getRawValue());
										var n_length = s_value.length;

										var regex = new RegExp("^.*\n$|.{1," + o_config.colsize + "}", "g");
										var n_pos = i_field.inputEl.dom.selectionStart;

										var a_lines = s_value.match(regex) || [];
										var a_newLines = s_value.split("\n");

										if (a_lines.length < o_config.rows && a_newLines.length < o_config.rows) {

											i_event.stopEvent();

											s_value = s_value + "\n";

											i_field.setValue(s_value);

											i_field.inputEl.dom.setSelectionRange(s_value.length, s_value.length);
										}
										else {
											var n_row = 1;
											var n_char = 0;
											var n_left = 0;
											var n_current = 0;
											var n_nextLine = 0;

											Ext.Array.each(a_lines, function (s_part, n_index) {

												n_char += s_part.length;

												var n_count = 0;

												n_left = 0;

												while (n_count < s_part.length) {
													n_count++;
													if (n_current < n_pos) {
														n_current++;
														n_left++;
													}
												}

												if (n_char <= n_pos) {
													n_row++;
												}
												else {
													n_nextLine = n_index + 1;
													return false;
												}
											});

											if (n_nextLine < a_lines.length || n_nextLine < a_newLines.length) {

												var n_newPos = 0;

												Ext.Array.each(a_lines, function (s_part, n_index) {

													if (n_index < n_nextLine) {
														n_newPos += s_part.length;
													}
													else {
														n_newPos += n_left + (a_newLines.length - 1);
														return false;
													}
												});

												if (n_row < o_config.rows) {
													i_event.stopEvent();
													i_field.inputEl.dom.setSelectionRange(n_newPos, n_newPos);
												}
											}
										}

									}
								}
								else if (i_field.xtype == "textfield") {

									var n_typeMode = i_self.getTypeMode();

									if (n_typeMode == IceCap.util.Handler.typeMode.OVERWRITE) {

										var s_direction = i_field.inputEl.el.dom.selectionDirection;

										var n_curpos = s_direction == "forward" ? i_field.inputEl.el.dom.selectionEnd : i_field.inputEl.el.dom.selectionStart;

										var s_key = String.fromCharCode(i_event.keyCode);

										if (n_curpos < value.length && i_regext.test(s_key)) {

											var a_values = value.split("");
											a_values[n_curpos] = i_event.browserEvent.key;
											value = a_values.join("");
											i_field.setValue(value);
											i_field.inputEl.el.dom.selectionEnd = i_field.inputEl.el.dom.selectionStart = n_curpos + 1;
											i_event.stopEvent();
										}
									}
								}
							}
						}
					}
				}
			}, o_meta);

			var o_clone = Ext.clone(o_field);

			if (o_meta.xtype) {

				o_clone.metaField = true;

				o_clone.xtype = s_type = o_meta.xtype;

				delete o_clone.cls;
				delete o_clone.fieldStyle;
				delete o_clone.inputType;
			}

			if (o_config.type == "grid") {

				Ext.apply(o_clone, {
					xtype: s_type = "iceCapGrid",
					gridConfig: o_config.grid,
					width: IceCap.util.Handler.getColSize() * n_inputWidth
				});

				delete o_clone.cls;
				delete o_clone.fieldStyle;
				delete o_clone.inputType;
				delete o_clone.listeners;
				delete o_clone.grid;

				Ext.apply(o_clone, {
					listeners: {
						fieldoption: {
							fn: function(a_config) {
								i_self.fireEvent("fieldoption", a_config);
							}
						},
						key: {
							fn: function(s_key) {
								i_self.fireEvent("key", s_key);
							}
						}
					}
				});
			}

			var i_field = Ext.widget(s_type, o_clone);

			var o_on = {};

			Ext.Object.each(o_listeners, function(s_key, value) {

				if (Ext.isFunction(value)) {
					o_on[s_key] = Ext.apply({}, {
						fn: value,
						scope: i_self
					});
				}
				else if (Ext.isObject(value)) {
					value.scope = i_self;
					o_on[s_key] = value;
				}
			});

			i_field.on(o_on);

			if (!i_field.readOnly) {
				n_tabIndex++;
			}

			if (o_config.atr == 25) {

				if (o_field.readOnly) {
					i_field.addCls("icecap-invalid");
				}
				else {
					i_field.markInvalid(true);
				}
			}

			a_fields.push(i_field);
		});

		n_tabIndex = n_tabIndex - 1;
		i_self.setLastTabIndex(n_tabIndex);
		i_self.setCurrentTabIndex(o_inputData.focus);

		if (n_focus) {
			i_self.setNeedFocus(n_focus);
		}

		return a_fields;
	},

	createLabels: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();

		var a_fields = [];

		var b_fixed = IceCap.util.Handler.getFixed();

		Ext.Array.each(o_inputData.output, function(o_config) {

			var s_value = o_config.dta;

			if (o_config.len) {
				if (o_config.ta == "right" && !o_config.off) {
					//o_config.col += o_config.len - o_config.dta.length;
				}

				if (o_config.off) {
					s_value = Ext.String.leftPad(s_value, s_value.length + o_config.off);
				}

				var n_x = Math.ceil((IceCap.util.Handler.getX(o_config.col) + (IceCap.util.Handler.getColSize() / 2)));
				var n_y = IceCap.util.Handler.getY(o_config.lin);

				if (isNaN(n_x)) {
					n_x = IceCap.util.Handler.getColSize();
				}

				if (isNaN(n_y)) {
					n_y = IceCap.util.Handler.getLineHeight();
				}

				var o_field = {
					value: s_value,
					htmlEncode: true,
					x: n_x,
					y: n_y,
					cls: ["icecap-field", i_self.getTheme()].filter(Boolean).join("-")
				};

				var o_style = {
					"font": "normal " + IceCap.util.Handler.getTextSize() + "px Segoe UI,Segoe,DejaVu Sans,Trebuchet MS,Verdana,sans-serif",
					"margin-top": "7px !important",
					"min-height": "0px !important"
				};

				if (b_fixed) {

					Ext.apply(o_field, {
						fieldCls: ["x-form-display-field iceCap-output-" + (o_config.atr || 20), i_self.getTheme()].filter(Boolean).join("-")
					});

					Ext.apply(o_style, {
						"font": "normal " + IceCap.util.Handler.getTextSize() + "px / " + IceCap.util.Handler.getColSize() + "px Lucida Console",
						"white-space": "pre",
						"margin-top": "7px !important",
						"min-height": "0px !important"
					});
				}

				Ext.apply(o_field, {
					fieldStyle: o_style
				});

				if (o_config.atr == "27") {
					o_field.hidden = true;
				}

				var i_field = Ext.widget("displayfield", o_field);

				if (o_config.atr == 23) {

					//i_field.addCls("icecap-invalid");
				}

				a_fields.push(i_field);
			}
		});

		return a_fields;
	},

	createKeys: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();
		var a_disabledKey = o_inputData.disabledKeys || [];
		var a_buttons = [];

		var i_template = Ext.create("Ext.XTemplate",
			"{id}={text}"
		);

		Ext.Array.each(o_inputData.keys, function(o_config) {

			var s_template = i_template.apply(o_config);

			var o_found = Ext.Array.findBy(a_disabledKey, function(o_disabled) {
				return o_config.id == o_disabled.id;
			});

			o_found = o_found || {
				showKey: 1
			};

			var b_hidden = Number(o_found.showKey) < 1  || false;

			var i_button = Ext.widget("button", {
				text: s_template,
				key: o_config.id,
				focusable: false,
				cls: "icecap-key",
				hidden: b_hidden,
				handler: function(i_button) {
					i_self.doKeyEvent(i_button.key);
				}
			});

			a_buttons.push(i_button);
		});

		return a_buttons;
	},

	createMetaFields: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();

		var a_fields = [];
		var n_tabIndex = i_self.getLastTabIndex() || 1;

		var a_sliced = Ext.Array.slice(o_inputData.metaFields || []);

		var n_inputHeight = o_inputData.height || 24;

		var n_inputWidth = o_inputData.width || 80;

		Ext.Array.each(a_sliced, function(o_config) {

			var n_x = o_config.x;
			var n_y = o_config.y;

			if (!o_config.x) {
				n_x = Math.ceil((IceCap.util.Handler.getX(o_config.col) + (IceCap.util.Handler.getColSize() / 2)));
			}

			if (!o_config.y) {
				n_y = IceCap.util.Handler.getY(o_config.lin);
			}

			if (isNaN(n_x)) {
				n_x = IceCap.util.Handler.getColSize();
			}

			if (isNaN(n_y)) {
				n_y = IceCap.util.Handler.getLineHeight();
			}

			var o_listeners = o_config.listeners || {};

			Ext.apply(o_config, {
				tabIndex: n_tabIndex,
				keys: Ext.Array.slice(o_inputData.keys),
				theme: i_self.getTheme(),
				x: n_x,
				y: n_y,
				listeners: {
					fieldoption: {
						fn: function(a_config) {
							i_self.fireEvent("fieldoption", a_config);
						}
					},
					key: {
						fn: function(o_config) {
							i_self.fireEvent("key", o_config);
						}
					}
				}
			});

			n_tabIndex++;

			o_config.xtype = o_config.xtype || o_config.type || "textfield";

			o_config.name = o_config.id;

			delete o_config.id;

			var i_field = Ext.widget(o_config.xtype, o_config);

			var o_on = {};

			Ext.Object.each(o_listeners, function(s_key, value) {

				if (Ext.isFunction(value)) {
					o_on[s_key] = Ext.apply({}, {
						fn: value,
						scope: i_self
					});
				}
				else if (Ext.isObject(value)) {
					value.scope = i_self;
					o_on[s_key] = value;
				}
			});

			i_field.on(o_on);

			a_fields.push(i_field);
		});

		i_self.setLastTabIndex(n_tabIndex);

		return a_fields;
	},

	doCreateFromData: function(o_inputData) {

		var i_self = this;
		i_self.fireEvent("toWindow", i_self, o_inputData);
	},

	recreate: function() {

		var i_self = this;

		var i_content = i_self.getContent();
		var i_keyArea = i_self.getKeyArea();
		var i_main = i_self.getMain();
		var o_inputData = i_self.getInputData();

		var b_fixed = o_inputData.hasOwnProperty("fixed") ? o_inputData.fixed : true;

		IceCap.util.Handler.setFixed(b_fixed);

		i_self.suspendLayouts();

		if (o_inputData.title) {
			i_self.setTitle(o_inputData.title);
		}

		var a_created = i_self.createFields();
		a_created = a_created.concat(i_self.createLabels());
		a_created = a_created.concat(i_self.createMetaFields());
		a_created.push(i_self.createCursor());
		a_created.push(i_self.createMarker());

		i_content.removeAll(true);
		i_keyArea.removeAll(true);

		i_content.add(a_created);

		var a_keys = i_self.createKeys();
		i_keyArea.add(a_keys);

		i_keyArea[a_keys.length === 0 ? "hide" : "show"]();

		var a_fields = i_self.query("field[focusable]");
		var a_grids = i_content.query("gridpanel");

		var b_found = false;
		var b_error = false;

		if (o_inputData.msgBox && o_inputData.msgBox.text) {
			b_error = true;
			i_self.onError(o_inputData);
		}

		if (a_fields.length > 0) {

			b_found = Ext.Array.findBy(a_fields, function (i_field) {
				return i_field.getTabIndex() === i_self.getNeedFocus();
			});

			if (!b_found) {
				b_found = true;
				var i_field = a_fields[0];
				if (!b_error) {
					setTimeout(function () {
						i_field.focus(false, 10);
					}, 10);
				}
			}
		}

		if (!b_found) {

			i_self.setCurrentPosition({
				col: 2,
				lin: 1
			});

			if (!b_error) {

				if (a_grids.length == 0) {
					setTimeout(function() {
						i_self.focus(true, 10);
					}, 10);
				}
			}
		}

		i_self.updateMessage(o_inputData);

		if (!b_found && !b_error) {
			setTimeout(function() {
				i_main.focus(true, 10);
			}, 10);
		}

		var i_marker = i_self.getMarker();
		i_marker.setInUse(i_self.getMarkerActive());

		IceCap.util.Handler.setFixed(true);

		i_self.resumeLayouts(true);
	},

	getCurrentField: function() {

		var i_self = this;

		var a_fields = i_self.query("field[focusable]");
		var n_tabIndex = i_self.getCurrentTabIndex();
		var i_controller = i_self.getController();

		var i_content = i_self.getContent();
		var i_currentField;

		Ext.Array.each(a_fields, function(i_field) {
			if (i_field.getTabIndex() === n_tabIndex && i_field.hasFocus) {
				i_currentField = i_field;
				return false;
			}
		});

		return i_currentField;
	},

	doKeyEvent: function(s_value, i_event) {

		if (i_event) {
			i_event.stopEvent();
		}

		var i_self = this;

		var i_controller = i_self.getController();

		var o_inputData = i_self.getInputData() || {};

		var a_disabledKeys = Ext.Array.from(o_inputData.disabledKeys || []);

		var o_found = Ext.Array.findBy(a_disabledKeys, function (o_key) {
			return s_value == o_key.id;
		});

		if (!o_found) {
			i_controller.doKeyEvent(s_value);
		}
		else {

			Ext.Msg.show({
				title: "Error",
				msg: "Funktionstasten er ikke tilladt",
				icon: Ext.MessageBox.INFO,
				buttons: Ext.MessageBox.OK,
				fn: function () {
					i_self.findFocus();
				}
			});
		}
	},

	doNavigationKey: function(n_direction, b_tab, i_event) {

		if (i_event) {
			i_event.stopPropagation();
			i_event.preventDefault();
		}

		var i_self = this;
		var i_controller = i_self.getController();
		i_controller[b_tab ? "doTabKey" : "doArrowKeys"](n_direction);
	},

	addKeyEvents: function() {

		var i_self = this;

		var a_bindings = [
			{
				key: Ext.event.Event.PAGE_DOWN,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doKeyEvent("ROLLUP", i_event);
				}
			},
			{
				key: Ext.event.Event.PAGE_UP,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doKeyEvent("ROLLDOWN", i_event);
				}
			},
			{
				key: Ext.event.Event.ENTER,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doKeyEvent("ENTER", i_event);
				}
			},
			{
				key: Ext.event.Event.ESC,
				fn: function(n_key, i_event) {

					i_event.preventDefault();

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doKeyEvent("ATTN", i_event);
				}
			},
			{
				key: Ext.event.Event.UP,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;

					if (!i_event.target.type) {

						i_event.stopEvent();

						var i_cursor = i_self.getCursor();
						var o_position = i_self.getCurrentPosition();

						var a_inputs = i_self.query("field[focusable]");

						var n_lin = o_position.lin;

						if (n_lin < 1) {
							n_lin = 1;
						}

						var o_box = Ext.apply({
							x: IceCap.util.Handler.getX(o_position.col),
							y: IceCap.util.Handler.getY(n_lin),
							width: Math.floor(IceCap.util.Handler.getColSize()),
							height: 1
						});

						var b_intersect = false;

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

							var o_bound = i_field.el.dom.getBoundingClientRect();

							var o_rect = Ext.apply({}, {
								x: i_field.getLocalX(),
								y: i_field.getLocalY(),
								width: o_bound.width,
								height: o_bound.height
							});

							if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
								b_intersect = true;
								i_field.focus(false, 10);
								return false;
							}
						});

						if (!b_intersect) {

							o_position.lin--;

							if (o_position.lin < 1) {
								o_position.lin = 1;
							}

							i_cursor.moveCursor(o_position);
						}
						i_self.setCurrentPosition(o_position);
					}
					else {
						i_self.doNavigationKey(0, false, i_event);
					}
				}
			},
			{
				key: Ext.event.Event.DOWN,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;

					if (!i_event.target.type) {

						i_event.stopEvent();

						var i_cursor = i_self.getCursor();
						var o_position = i_self.getCurrentPosition();
						var o_inputData = i_self.getInputData();
						var o_window = o_inputData.window;

						var a_inputs = i_self.query("field[focusable]");

						var n_lin = o_position.lin + 2;

						if (n_lin > o_window.height) {
							n_lin = o_window.height;
						}

						var o_box = Ext.apply({
							x: IceCap.util.Handler.getX(o_position.col),
							y: IceCap.util.Handler.getY(n_lin),
							width: Math.floor(IceCap.util.Handler.getColSize()),
							height: 1
						});

						var b_intersect = false;

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

							var o_bound = i_field.el.dom.getBoundingClientRect();

							var o_rect = Ext.apply({}, {
								x: i_field.getLocalX(),
								y: i_field.getLocalY(),
								width: o_bound.width,
								height: o_bound.height
							});

							if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
								b_intersect = true;
								i_field.focus(false, 10);
								return false;
							}
						});

						if (!b_intersect) {

							o_position.lin++;

							if (o_position.lin > o_window.height) {
								o_position.lin = o_window.height;
							}

							i_cursor.moveCursor(o_position);
						}
						i_self.setCurrentPosition(o_position);
					}
					else {
						i_self.doNavigationKey(1, false, i_event);
					}
				}
			},
			{
				key: Ext.event.Event.LEFT,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;

					if (!i_event.target.type) {
						i_event.stopEvent();

						var i_cursor = i_self.getCursor();
						var o_position = i_self.getCurrentPosition();

						var a_inputs = i_self.query("field[focusable]");

						var n_col = o_position.col - 1;

						if (n_col < 1) {
							n_col = 1;
						}

						var o_box = Ext.apply({
							x: IceCap.util.Handler.getX(n_col),
							y: IceCap.util.Handler.getY(o_position.lin + 1),
							width: Math.floor(IceCap.util.Handler.getColSize()),
							height: 1
						});

						var b_intersect = false;

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

							var o_bound = i_field.el.dom.getBoundingClientRect();

							var o_rect = Ext.apply({}, {
								x: i_field.getLocalX(),
								y: i_field.getLocalY(),
								width: o_bound.width,
								height: o_bound.height
							});

							if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
								b_intersect = true;
								i_field.focus(false, 10);
								return false;
							}
						});

						if (!b_intersect) {

							o_position.col--;

							if (o_position.col < 1) {
								o_position.col = 1;
							}

							i_cursor.moveCursor(o_position);
						}

						i_self.setCurrentPosition(o_position);
					}
				}
			},
			{
				key: Ext.event.Event.RIGHT,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;

					if (!i_event.target.type) {

						i_event.stopEvent();

						var i_cursor = i_self.getCursor();
						var o_position = i_self.getCurrentPosition();
						var o_inputData = i_self.getInputData();
						var o_window = o_inputData.window;

						var a_inputs = i_self.query("field[focusable]");

						var n_col = o_position.col + 1;

						if (n_col > o_inputData.width) {
							n_col = o_inputData.width;
						}

						var o_box = Ext.apply({
							x: IceCap.util.Handler.getX(n_col),
							y: IceCap.util.Handler.getY(o_position.lin + 1),
							width: Math.floor(IceCap.util.Handler.getColSize()),
							height: 1
						});

						var b_intersect = false;

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

							var o_bound = i_field.el.dom.getBoundingClientRect();

							var o_rect = Ext.apply({}, {
								x: i_field.getLocalX(),
								y: i_field.getLocalY(),
								width: o_bound.width,
								height: o_bound.height
							});

							if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
								b_intersect = true;
								i_field.focus(false, 10);
								return false;
							}
						});

						if (!b_intersect) {

							o_position.col++;

							if (o_position.col > o_inputData.width) {
								o_position.col = o_inputData.width;
							}

							i_cursor.moveCursor(o_position);
						}

						i_self.setCurrentPosition(o_position);
					}
				}
			},
			{
				key: Ext.event.Event.TAB,
				shift: false,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doNavigationKey(1, true, i_event);
				}
			},
			{
				key: Ext.event.Event.TAB,
				shift: true,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doNavigationKey(0, true, i_event);
				}
			},
			{
				key: Ext.event.Event.NUM_PLUS,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;

					var i_field = i_self.getCurrentField();

					if (i_field) {

						var n_start = i_field.inputEl.el.dom.selectionStart;

						var s_text = i_field.getValue();

						s_text = s_text.substr(0, n_start);

						i_field.setValue(s_text);

						i_self.doNavigationKey(1, true, i_event);
					}
				}
			},
			{
				key: Ext.event.Event.HOME,
				ctrl: true,
				alt: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();

					var i_key = this;
					var i_self = i_key.target.component;

					var b_markerActive = i_self.getMarkerActive();
					var i_marker = i_self.getMarker();

					i_self.setMarkerActive(!b_markerActive);

					i_marker.toggle();
				}
			},
			{
				key: Ext.event.Event.INSERT,
				ctrl: false,
				alt: false,
				fn: function (n_key, i_event) {

					var n_type = i_self.getTypeMode();
					switch (n_type) {
						case IceCap.util.Handler.typeMode.OVERWRITE:
							n_type = IceCap.util.Handler.typeMode.INSERT;
							break;
						default:
							n_type = IceCap.util.Handler.typeMode.OVERWRITE;
							break;
					}

					i_self.setTypeMode(n_type);
				}
			}
		];

		a_bindings = a_bindings.concat(i_self.getFKeys());

		var i_keyMap = Ext.create("Ext.util.KeyMap", {
			target: i_self.getEl(),
			binding: a_bindings
		});
	},

	getFKeys: function() {

		var i_self = this;

		var a_keys = [
			{
				key: Ext.event.Event.F1,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F1", i_event);
				}
			},
			{
				key: Ext.event.Event.F2,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F2", i_event);
				}
			},
			{
				key: Ext.event.Event.F3,
				shift: false,
				alt: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F3", i_event);
				}
			},
			{
				key: Ext.event.Event.F4,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F4", i_event);
				}
			},
			{
				key: Ext.event.Event.F5,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F5", i_event);
				}
			},
			{
				key: Ext.event.Event.F6,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F6", i_event);
				}
			},
			{
				key: Ext.event.Event.F7,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F7", i_event);
				}
			},
			{
				key: Ext.event.Event.F8,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F8", i_event);
				}
			},
			{
				key: Ext.event.Event.F9,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F9", i_event);
				}
			},
			{
				key: Ext.event.Event.F10,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F10", i_event);
				}
			},
			{
				key: Ext.event.Event.F11,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F11", i_event);
				}
			},
			{
				key: Ext.event.Event.F12,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F12", i_event);
				}
			}
		];

		a_keys = a_keys.concat(i_self.getFShiftKeys());

		return a_keys;
	},

	getFShiftKeys: function() {

		var i_self = this;

		var a_keys = [
			{
				key: Ext.event.Event.F1,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F13", i_event);
				}
			},
			{
				key: Ext.event.Event.F2,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F14", i_event);
				}
			},
			{
				key: Ext.event.Event.F3,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F15", i_event);
				}
			},
			{
				key: Ext.event.Event.F4,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F16", i_event);
				}
			},
			{
				key: Ext.event.Event.F5,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F17", i_event);
				}
			},
			{
				key: Ext.event.Event.F6,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F18", i_event);
				}
			},
			{
				key: Ext.event.Event.F7,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F19", i_event);
				}
			},
			{
				key: Ext.event.Event.F8,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F20", i_event);
				}
			},
			{
				key: Ext.event.Event.F9,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F21", i_event);
				}
			},
			{
				key: Ext.event.Event.F10,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F22", i_event);
				}
			},
			{
				key: Ext.event.Event.F11,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F23", i_event);
				}
			},
			{
				key: Ext.event.Event.F12,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F24", i_event);
				}
			}
		];

		return a_keys;
	},

	createCursor: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();

		var i_cursor = Ext.widget("component", {
			cls: ["iceCap-cursor", i_self.getTheme()].filter(Boolean).join("-"),
			autoEl: {
				tag: "div",
				style: {
					"border": "1px solid red;",
					"position": "absolute !important;",
					"width": (IceCap.util.Handler.getColSize() + 0.4) + "px !important;"
				}
			},
			listeners: {
				render: {
					fn: function(i_item) {
						i_item.hide();
					}
				},
				afterrender: {
					fn: function(i_cursor) {

						var i_content = i_self.getContent();

						var a_fields = i_content.query("field[focusable]");

						if (a_fields.length === 0) {

							i_cursor.show();

							i_cursor.moveCursor({
								col: 2,
								lin: 1
							});
						}
					}
				}
			},
			moveCursor: function(o_position) {

				var i_cursor = this;
				var i_marker = i_self.getMarker();

				i_cursor.setPosition({
					x: IceCap.util.Handler.getX(o_position.col - 1) + 4,
					y: IceCap.util.Handler.getY(o_position.lin + 1) - 2
				});

				var a_pos = [
					IceCap.util.Handler.getX(o_position.col - 1) - 1,
					IceCap.util.Handler.getY(o_position.lin + 1) - 2
				];

				i_self.updateCurrentPosition(o_position);
			}
		});

		i_self.setCursor(i_cursor);

		return i_cursor;
	},

	createMarker: function() {

		var i_self = this;

		var i_marker = Ext.widget("icecap-marker", {
			hidden: true,
			active: false,
			inUse: false,
			ui: i_self.getTheme() || "default"
		});

		i_self.setMarker(i_marker);

		return i_marker;
	},

	findFocus: function() {

		var i_self = this;
		var i_main = i_self.getMain();
		var i_content = i_self.getContent();

		var a_fields = i_content.query("field[focusable]");

		if (a_fields.length > 0) {

			var i_field = a_fields[0];
			i_field.focus(false, 10);
		}
		else {
			i_main.focus(false, 10);
		}
	},

	doTimer: function() {

		var i_self = this;

		var i_timer = i_self.getTimer();
		if (!i_timer) {
			i_timer = new Ext.util.DelayedTask(function() {
				i_self.setLoading(true);
			});

			i_self.setTimer(i_timer);

			i_timer.delay(1000);
		}
	},

	cancelTimer: function() {

		var i_self = this;

		var i_timer = i_self.getTimer();

		if (i_timer) {
			i_timer.cancel();
			i_self.setTimer(null);
		}
	},

	onError: function(o_inputData) {

		var i_self = this;

		Ext.Msg.show({
			title: "Server Message",
			msg: o_inputData.msgBox.text,
			icon: Ext.MessageBox.INFO,
			buttons: Ext.MessageBox.OK,
			fn: function(btn) {
				i_self.findFocus();
			}
		});
	},

	updateMessage: function(o_inputData) {

		var i_self = this;
		var i_statusbar = i_self.getStatusBar();

		var a_messages = Ext.Array.from(o_inputData.message);

		if (a_messages.length > 0) {

			var a_result = [];

			Ext.Array.each(a_messages, function (o_message) {
				a_result.push(o_message.text);
			});

			i_statusbar.setText(a_result.join(" "));
		}
	},

	applyFFW: function(o_config) {

		var i_self = this;
		var s_hex = o_config.ffw;
		var b_fixed = IceCap.util.Handler.getFixed();

		var o_field = {
			xtype: "textfield"
		};

		var o_fieldStyle = {
			font: "normal " + IceCap.util.Handler.getTextSize() + "px Segoe UI,Segoe,DejaVu Sans,Trebuchet MS,Verdana,sans-serif",
			padding: "1px 3px 2px 3px !important"
		};

		if (b_fixed) {

			Ext.apply(o_fieldStyle, {
				font: "normal " + IceCap.util.Handler.getTextSize() + "px / " + IceCap.util.Handler.getColSize() + "px Lucida Console"
			});
		}

		if (o_config.ta) {

			Ext.apply(o_fieldStyle, {
				"text-align": o_config.ta
			});
		}

		if (s_hex) {

			var n_hex = parseInt(s_hex);

			var n_sanHex = IceCap.util.Handler.createIndicators(s_hex);

			if (n_sanHex & IceCap.util.Handler.FFW.BYPASS) {

				Ext.apply(o_field, {
					readOnly: true
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.ALPHA_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[a-z A-Z ,.-]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.NUMERIC_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[+\\-,. 0-9]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.DIGITS_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[0-9]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MOD_DATA) {

				Ext.apply(o_field, {
					listeners: {
						afterrender: {
							fn: function(i_field) {

								i_field.originalValue = 0xFF;
							},
							single: true
						}
					}
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.AUTO_ENTER) {

				// THIS WE DONT NEED IN THE MODERN WORLD...

				// Ext.apply(o_field, {
				// 	listeners: {
				// 		blur: {
				// 			fn: function(i_field) {
				// 				i_self.doKeyEvent("ENTER");
				// 			}
				// 		}
				// 	}
				// });
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MONOCASE) {

				// Ext.apply(o_fieldStyle, {
				// 	"text-transform": "uppercase"
				// });
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MAND_ENTER) {

				Ext.apply(o_field, {
					allowBlank: false
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.RA_ZEROFILL ||
				n_sanHex & IceCap.util.Handler.FFW.RA_BLANKFILL ||
				n_sanHex & IceCap.util.Handler.FFW.RA_MANDATORY) {

				Ext.apply(o_fieldStyle, {
					"text-align": "right"
				});
			}

			if (o_config.atr == "27" && n_hex & (1 << 13)) {

				Ext.apply(o_field, {
					hidden: true,
					disabled: true
				});
			}
		}

		Ext.apply(o_field, {
			fieldStyle: o_fieldStyle
		});

		return o_field;
	},

	updateCurrentPosition: function (o_position) {

		var i_self = this;
		var i_statusbar = i_self.getStatusBar();

		if (i_statusbar) {
			var i_position = i_statusbar.getComponent("icecap-position");

			if (i_position) {

				var a_result = [
					o_position.lin,
					o_position.col
				];

				i_position.setText(a_result.join("/"));
			}
		}
	},

	updateTypeMode: function (n_type) {

		var i_self = this;

		var i_statusbar = i_self.getStatusBar();

		if (i_statusbar) {

			var i_typeLabel = i_statusbar.getComponent("icecap-typemode");

			var a_mode = [
			];

			if (i_typeLabel) {

				switch (n_type) {
					case IceCap.util.Handler.typeMode.OVERWRITE:
						a_mode.push(" DEL");
						break;

					default:
						a_mode.push(" INS");
						break;
				}

				i_typeLabel.setText(a_mode.join(" "));
			}
		}
	}
});
Ext.define("IceCap.view.WebContainer", {

	extend: "Ext.Component",

	xtype: "icecap-webcontainer",

	alias: [
		"widget.icecap.view.webcontainer",
		"widget.webcontainer"
	],

	config: {
		src: ""
	},

	autoEl: {
		tag: "iframe",
		style: "height: 100%; width: 100%; border: none;",
		src: ""
	},

	listeners: {
		render: {
			fn: function (i_self) {

				var i_element = i_self.getEl();

				if (i_element) {
					i_element.dom.src = i_self.getSrc();
					i_self.setLoading(true);
				}
			}
		},
		load: {
			element: "el",
			fn: function (i_element) {

				var i_self = this.component;
				i_self.setLoading(false);
			}
		}
	},

	initComponent: function () {

		var i_self = this;
		i_self.callParent(arguments);
	},

	updateSrc: function(s_url) {

		var i_self = this;
		var i_element = i_self.getEl();

		if (i_element) {
			i_element.dom.src = s_url;
			i_self.setLoading(true);
		}
	}
});
Ext.define("IceCap.controller.Main", {
	extend: "Ext.app.ViewController",

	alias: "controller.icecap.view.main",

	listen: {
		global: {
			beforelogout: {
				fn: function() {

					var i_controller = this;
					i_controller.onBeforeLogout();
				}
			}
		}
	},

	onAfterRender: function(i_panel) {

		var i_self = i_panel;

		i_self.addKeyEvents();

		var i_content = i_self.getContent();
		var i_main = i_self.getMain();

		var o_inputData = i_self.getInputData() || {};

		if (o_inputData.window) {
			i_self.doCreateFromData(o_inputData);
		}

		if (i_content) {

			var a_fields = i_content.query("field[focusable]");

			var i_found = Ext.Array.findBy(a_fields, function (i_field) {
				return i_field.getTabIndex() == i_self.getNeedFocus();
			});

			if (i_found) {
				i_found.focus(false, 10);
			}
			else {
				i_self.setCurrentPosition({
					col: o_inputData.csrCol,
					lin: o_inputData.csrLin
				});

				i_main.focus(true, 10);
			}
		}
	},

	onSendData: function(o_config, o_data) {

		var i_controller = this;
		var o_saved = Ext.clone(o_config);
		var i_view = i_controller.getView();
		var s_session = i_view.getSessionValue();

		var o_options = i_view.getOptions();

		o_data = Ext.apply(o_data, {
			i18nLearning: false
		});

		Ext.apply(o_config, {
			session: s_session,
		});

		Ext.apply(o_config, o_options);

		if (!i_view.getWorking()) {
			i_view.setWorking(true);
			i_view.doTimer();
			IceCap.util.Handler.getData(o_config, o_data, function(o_inputData) {

				if (o_inputData.statusUpdate) {

					i_view.setWorking(false);
					i_view.updateMessage(o_inputData);
					if (o_inputData.status > -1) {
						i_view.doCreateFromData(o_inputData);
					}
					i_controller.onSendData(o_saved, o_data);
				}
				else {

					i_view.setLoading(false);
					i_view.cancelTimer();

					var f_callback = i_view.getResponseCallBack() || Ext.emptyFn;

					if (f_callback != Ext.emptyFn) {

						f_callback(o_inputData);
					}

					var o_plugin = o_inputData.plugin || Ext.apply({}, {
						require: []
					});

					var a_require = o_plugin.require || [];
					var s_run = o_plugin.run || "";

					i_controller.doRequire(a_require, function() {

						if (s_run) {
							eval(s_run);
						}

						if (o_inputData.close) {
							i_view.close();
						}
						else {

							i_view.setWorking(false);
							i_view.doCreateFromData(o_inputData);

							i_view.fireEvent("stateupdate", i_view);
						}
					});
				}
			});
		}
	},

	onWheel: function(i_event) {

		var i_controller = this;
		var i_view = i_controller.getView();

		var b_hasGrid = i_view.query("gridpanel").length > 0;
		var b_mappanel = i_view.query("icecap-gmappanel").length > 0;

		var n_delta = i_event.getWheelDelta();
		var s_event = "";

		if (n_delta > 0) {
			s_event = "ROLLDOWN";
		}
		else {
			s_event = "ROLLUP";
		}

		if (!b_hasGrid && !b_mappanel && !i_event.ctrlKey){
			i_controller.doKeyEvent(s_event);
		}
	},

	onDblClick: function(i_event) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var o_inputData = i_view.getInputData();
		var i_cursor = i_view.getCursor();

		var b_fireEvent = i_view.getEnterOnDblClick();

		if (b_fireEvent) {

			var i_body = i_view.body;
			var a_local = i_body.getOffsetsTo(window);

			var a_metaFields = o_inputData.metaFields || [];

			var o_click = Ext.apply({
				x: i_event.pageX - a_local[0],
				y: i_event.pageY - a_local[1]
			});

			var o_position = IceCap.util.Handler.toColAndLine(o_click);

			var i_currentField = i_view.getCurrentField();

			if (i_currentField) {
				o_position = Ext.apply(o_position, {
					lin: i_currentField.lin,
					col: i_currentField.col
				});
				if (i_currentField.inputEl && i_currentField.getValue()) {
					o_position = IceCap.util.Handler.toColAndLine(o_click);
				}
			}
			else {

				i_cursor.moveCursor(o_position);
				i_cursor[a_metaFields.length === 0 ? "show" : "hide" ]();
			}

			i_view.setCurrentPosition(o_position);

			if (a_metaFields.length === 0) {
				i_controller.doKeyEvent("ENTER");
			}
		}
	},

	onClick: function(i_event) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var o_inputData = i_view.getInputData();
		var i_cursor = i_view.getCursor();

		var i_body = i_view.body;
		var a_local = i_body.getOffsetsTo(window);

		var o_click = Ext.apply({
			x: i_event.pageX - a_local[0],
			y: i_event.pageY - a_local[1]
		});

		var o_position = IceCap.util.Handler.toColAndLine(o_click);

		var i_currentField = i_view.getCurrentField();

		var a_metaFields = o_inputData.metaFields || [];

		if (i_currentField) {
			o_position = Ext.apply(o_position, {
				lin: i_currentField.lin,
				col: i_currentField.col
			});
			if (i_currentField.inputEl && i_currentField.getValue()) {
				o_position = IceCap.util.Handler.toColAndLine(o_click);
			}
		}
		else {

			var a_inputs = i_view.query("field[focusable]");

			var o_box = Ext.apply({
				x: o_click.x,
				y: o_click.y,
				width: IceCap.util.Handler.getColSize(),
				height: 2
			});

			var b_intersect = false;

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

				var i_element = i_field.getEl();

				if (i_element) {
					var o_bound = i_element.dom.getBoundingClientRect();

					var o_rect = Ext.apply({}, {
						x: i_field.getLocalX(),
						y: i_field.getLocalY(),
						width: o_bound.width,
						height: o_bound.height
					});

					if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
						b_intersect = true;
						i_field.focus(false, 10);
						return false;
					}
				}
			});

			if (!b_intersect) {
				i_cursor.moveCursor(o_position);

				i_cursor[a_metaFields.length === 0 ? "show" : "hide" ]();
			}
		}

		i_view.setCurrentPosition(o_position);
	},

	doArrowKeys: function(n_direction) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var a_fields = i_view.query("[focusable]");
		var n_tabIndex = i_view.getCurrentTabIndex();
		var n_lastTabIndex = i_view.getLastTabIndex();
		var n_part = n_direction ? 1 : -1;

		var i_lastFocus;
		var i_nextFocus;

		i_lastFocus = Ext.Array.findBy(a_fields, function (i_field) {
			return i_field.getTabIndex() == n_tabIndex;
		});

		var b_found = false;

		if (i_lastFocus) {

			var n_lin = i_lastFocus.lin;
			var n_col = i_lastFocus.col;
			var n_next = n_lin + n_part;

			i_nextFocus = Ext.Array.findBy(a_fields, function(i_field) {
				return i_field.lin === n_next && i_field.col === n_col;
			});

			if (i_nextFocus) {
				b_found = true;
				n_tabIndex = i_nextFocus.getTabIndex();
				i_nextFocus.focus(false, 10);
			}
		}

		if (!b_found) {

			n_tabIndex = i_view.getCurrentTabIndex() + n_part;

			if (n_direction) {
				if (n_tabIndex > n_lastTabIndex) {
					n_tabIndex = 1;
				}
			}
			else {
				if (n_tabIndex < 1) {
					n_tabIndex = n_lastTabIndex;
				}
			}

			i_nextFocus = Ext.Array.findBy(a_fields, function (i_field) {
				return i_field.getTabIndex() == n_tabIndex;
			});

			if (i_nextFocus) {
				i_nextFocus.focus(false, 10);
			}
		}

		i_view.setCurrentTabIndex(n_tabIndex);
	},

	doTabKey: function(n_direction) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var a_fields = i_view.query("[focusable]");
		var n_tabIndex = i_view.getCurrentTabIndex();
		var n_lastTabIndex = i_view.getLastTabIndex();
		var n_part = n_direction ? 1 : -1;

		n_tabIndex = i_view.getCurrentTabIndex() + n_part;

		if (n_direction) {
			if (n_tabIndex > n_lastTabIndex) {
				n_tabIndex = 1;
			}
		}
		else {
			if (n_tabIndex < 1) {
				n_tabIndex = n_lastTabIndex;
			}
		}

		var i_found =Ext.Array.findBy(a_fields, function(i_field) {
			return i_field.getTabIndex() == n_tabIndex;
		});

		if (i_found) {
			i_found.focus(false, 10);
		}

		i_view.setCurrentTabIndex(n_tabIndex);
	},

	doKeyEvent: function(s_value) {

		var i_controller = this;
		var i_view = i_controller.getView();

		var i_currentField = i_view.getCurrentField();
		var o_position = i_view.getCurrentPosition();

		var o_data = {
			key: s_value
		};

		var i_content = i_view.getContent();
		var i_form = i_content.getForm();
		var i_collectionField = i_form.getFields();
		var o_values = {};

		Ext.Array.each(i_collectionField.items, function(i_field) {
			if (i_field.isDirty()) {
				var value = i_field.getSubmitValue ? i_field.getSubmitValue() : i_field.getValue();
				o_values[i_field.itemId || i_field.initialConfig.id || i_field.name] = value;
			}
		});

		Ext.Object.each(o_values, function(s_key, value) {
			var s_id = "ses2.win." + s_key;
			o_data[s_id] = value;
		});

		if (i_currentField) {

			o_data = Ext.apply(o_data, {
				csrLin: i_currentField.lin,
				csrCol: i_currentField.col
			});

			if (i_currentField.inputEl && i_currentField.getValue()) {
				o_data = Ext.apply(o_data, {
					csrCol: o_position.col
				});
			}
		}
		else {

			o_data = Ext.apply(o_data, {
				csrLin: o_position.lin,
				csrCol: o_position.col
			});
		}

		var f_callback = i_view.getRequestCallBack() || Ext.emptyFn;

		if (f_callback != Ext.emptyFn) {

			f_callback(o_data);
		}

		i_controller.onSendData({
			func: ""
		}, o_data);
	},

	doFKey: function(s_value) {

		var i_controller = this;

		i_controller.doKeyEvent(s_value);
	},

	doRequire: function(a_require, f_callback) {

		f_callback = f_callback || Ext.emptyFn;

		a_require = a_require || [];

		if (a_require.length > 0) {

			var a_external = Ext.Array.filter(Ext.Array.map(a_require, function(s_value) {
				if (s_value.indexOf("/") > -1) {
					return s_value;
				}
			}), function(value) {
				return value;
			});

			var a_internal = Ext.Array.filter(Ext.Array.map(a_require, function(s_value) {
				if (s_value.indexOf("/") < 0) {
					return s_value;
				}
			}), function(value) {
				return value;
			});

			if (a_external.length > 0) {
				Ext.Loader.loadScriptsSync(a_external);
			}

			if (a_internal.length > 0) {
				Ext.syncRequire(a_internal);
			}
		}

		f_callback();
	},

	onPCO: function(o_inputData) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var i_keyMap = i_view.getKeyMap();

		switch (o_inputData.cmdAct) {

			case 0:
				window.open(o_inputData.cmd, o_inputData.cmdTitle, "");
				break;
			case 1:
				var i_window = Ext.widget("window", {
					title: o_inputData.cmdTitle,
					layout: "fit",
					collapsible: true,
					items: [
						{
							xtype: "icecap-webcontainer",
							src: o_inputData.cmd
						}
					],
					width: window.outerWidth * 0.9,
					height: window.outerHeight * 0.9
				});

				i_window.show();
				break;
			default:

				i_keyMap.disable();

				var i_store = Ext.create('Ext.data.Store', {
					proxy: {
						type: 'jsonp',
						timeout: "86400000",
						url: o_inputData.cmd,
						nocache: false,
						callback: function(o_result) {
							i_keyMap.enable();
						},
						listeners: {
							exception: {
								fn: function(i_proxy, i_request, i_operation) {
									i_controller.onPCOError(i_operation.error, function() {
										i_keyMap.enable();
										i_view.findFocus();
									});
								}
							}
						}
					}
				});

				i_store.load();
				break;
		}
	},

	onPCOError: function(s_error, f_callback) {

		f_callback = f_callback || Ext.emptyFn;

		var i_template = new Ext.XTemplate(
			"<div class=\"iceCap-msg\">",
				"<h3>{header}</h3>",
				"<ul>",
					"<li>{message}</li>",
				"</ul>",
			"</div>"
		);

		var i_window = Ext.widget("window", {
			title: "PCO Error",
			width: 800,
			modal: true,
			closable: false,
			html: i_template.apply({
				header: "Error",
				message: s_error
			}),
			bodyPadding: 5,
			buttons: [{
				text: "Ok",
				tabIndex: 1,
				focusable: true,
				listeners: {
					afterrender: {
						fn: function(i_button) {
							setTimeout(function() {
								i_button.focus(false, 10);
							});
						}
					}
				},
				handler: function() {
					i_window.close();
					f_callback();
				}
			}]
		});

		i_window.show();
	},

	onDestroy: function(i_panel) {

		var s_session = i_panel.getSessionValue();

		IceCap.util.Handler.removeSession(s_session);

		Ext.Ajax.request({
			method: "POST",
			url: IceCap.util.Handler.getPath({
				session: s_session,
				func: "",
				close: "on"

			})
		});
	},

	onBeforeDestroy: function() {

		var i_controller = this;
		var i_view = i_controller.getView();

		var i_window = i_view.up("window");

		if (i_window) {
			i_window.close();
		}
	},

	onActivated: function() {

		var i_controller = this;
		var i_view = i_controller.getView();
		var i_content = i_view.getContent();
		var i_main = i_view.getMain();

		var o_currentPosition = i_view.getCurrentPosition();

		var a_inputs = i_view.query("field[focusable]");

		var o_box = Ext.apply({
			x: IceCap.util.Handler.getX(o_currentPosition.col),
			y: IceCap.util.Handler.getY(o_currentPosition.lin),
			width: IceCap.util.Handler.getColSize(),
			height: 2
		});

		var b_intersect = false;

		var o_inputData = i_view.getInputData() || {
			metaFields: []
		};

		var a_metaFields = o_inputData.metaFields || [];

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

			var i_element = i_field.getEl();

			if (i_element) {
				var o_bound = i_element.dom.getBoundingClientRect();

				var o_rect = Ext.apply({}, {
					x: i_field.getLocalX(),
					y: i_field.getLocalY(),
					width: o_bound.width,
					height: o_bound.height
				});

				if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
					b_intersect = true;
					i_field.focus(false, 10);
					return false;
				}
			}
		});

		if (!b_intersect) {
			var i_cursor = i_view.getCursor();

			if (i_cursor) {

				i_cursor.moveCursor(o_currentPosition);

				i_cursor[a_metaFields.length === 0 ? "show" : "hide" ]();
			}

			if (i_main) {
				setTimeout(function() {
					i_main.focus(true, 10);
				}, 10);
			}
		}
	},

	onApplicationClose: function() {

		var i_controller = this;
		var i_view = i_controller.getView();

		i_view.close();
	},

	onBeforeLogout: function() {

		var i_controller = this;
		var i_view = i_controller.getView();

		i_view.close();
	}
});
Ext.define("IceCap.view.Main", {

	xtype: "icecap-panel",

	extend: "Ext.panel.Panel",

	layout: "fit",

	ui: "",

	mixins: [
		"Ext.mixin.Mashup"
	],

	requiredScripts: [
		"/icecap/app/theme/default.css",
		"/icecap/app/icecap-overrides.js"
	],

	requires: [
		"IceCap.controller.Main",
		"IceCap.view.Window",
		"IceCap.view.WebContainer",
		"IceCap.util.Handler",
		"IceCap.util.Scale",
		"IceCap.util.Breakout",
		"IceCap.component.Marker",
		"IceCap.ux.form.panel.Panel4Area",
		"IceCap.ux.form.panel.TabPanel",
		"IceCap.ux.grid.editor.Main",
		"IceCap.ux.container.Column",
		"IceCap.ux.form.label.Label",
		"IceCap.ux.form.field.Field",
		"IceCap.ux.form.panel.fusionChart",
		"IceCap.ux.form.field.SimpleComboBox",
		"IceCap.ux.form.field.sqlComboBox",
		"IceCap.ux.form.field.sqlSearchBox",
		"IceCap.ux.form.field.LINKFIELD",
		"IceCap.ux.form.field.AdvancedComboBox",
		"IceCap.ux.form.field.CommandField",
		"IceCap.ux.form.field.LibraryFileDependency",
		"IceCap.ux.form.field.FileLibraryDependency",
		"IceCap.ux.form.field.RadioGroup",
		"IceCap.ux.container.CommandPanel",
		"IceCap.ux.container.GMapPanel"
	],

	config: {
		inputData: null,
		lastTabIndex: 1,
		currentTabIndex: 1,
		content: null,
		keyArea: null,
		working: false,
		needFocus: 1,
		ignoreTitle: false,
		currentPosition: {
			col: 1,
			lin: 1
		},
		activeWindow: null,
		statusBar: null,
		main: null,
		sessionValue: "",
		keyMap: null,
		cursor: null,
		timer: null,
		func: "",
		initFunc: "",
		initCmd: "",
		responseCallBack: Ext.emptyFn,
		requestCallBack: Ext.emptyFn,
		theme: "",
		scale: 14,
		enterOnDblClick: false,
		marker: null,
		markerActive: false,
		options: {},
		messageText: "",
		typeMode: 1
	},

	controller: "icecap.view.main",

	focusable: true,

	listeners: {
		afterrender: {
			fn: "onAfterRender",
			buffer: 100,
			single: true
		},
		activate: {
			fn: "onActivated",
			buffer: 10
		},
		dblclick: {
			fn: "onDblClick",
			element: "body"
		},
		click: {
			fn: "onClick",
			element: "body"
		},
		wheel: {
			fn: "onWheel",
			element: "body"
		},
		destroy: {
			fn: "onDestroy"
		},
		beforedestroy: {
			fn: "onBeforeDestroy"
		},
		applicationClose: {
			fn: "onApplicationClose"
		},
		fieldoption: {
			fn: function(a_config) {

				var i_self = this;
				var i_controller = i_self.getController();

				var o_data = {};

				Ext.apply(o_data, {
					key: "ENTER"
				});

				Ext.Array.each(a_config, function(o_config) {

					var s_field = o_config.field;

					o_data[s_field] = o_config.option;

					Ext.apply(o_data, {
						csrCol: o_config.col,
						csrLin: o_config.lin
					});
				});

				i_self.setCurrentPosition({
					col: o_data.csrCol,
					lin: o_data.csrLin
				});

				i_controller.onSendData({
					func: ""
				}, o_data);
			}
		},
		key: {
			fn: function(s_value) {

				var i_self = this;
				var i_controller = i_self.getController();

				i_controller.doKeyEvent(s_value);
			}
		}
	},

	initComponent: function() {

		Ext.tip.QuickTipManager.init();

		var i_self = this;

		var o_params = Ext.Object.fromQueryString(window.location.search);

		Ext.Object.each(o_params, function(s_key, value) {
			o_params[s_key] = Ext.decode(value, true) || value;
		});

		var o_options = Ext.apply(i_self.options, o_params.options || {});

		Ext.apply(i_self, o_options);

		i_self.callParent(arguments);

		i_self.setTypeMode(IceCap.util.Handler.typeMode.INSERT);

		i_self.add(i_self.createLayout());

		var o_data = i_self.getInputData();
		var s_sessionValue = i_self.getSessionValue();

		if (o_data) {
			if (o_data.close) {
				i_self.close();
			}
			else {

				IceCap.util.Handler.useScale(i_self.getScale());

				if (s_sessionValue) {

					IceCap.util.Handler.isSessionAlive(s_sessionValue, function(b_alive) {
						if (!b_alive) {
							i_self.setSessionValue(null);
							i_self.setInputData(null);

							i_self.doInit(function(o_data) {
								i_self.cancelTimer();
								i_self.setLoading(false);
								if (o_data.close) {
									i_self.close();
								}
								else {
									i_self.setInputData(o_data);
									i_self.doCreateFromData(o_data);
								}
							});
						}
						else {
							i_self.doCreateFromData(o_data);
						}
					});
				}
			}
		}
		else {
			i_self.doInit(function(o_data) {
				i_self.cancelTimer();
				i_self.setLoading(false);
				if (o_data.close) {
					i_self.close();
				}
				else {
					i_self.setInputData(o_data);
					i_self.doCreateFromData(o_data);
				}
			});
		}

		if (i_self.getTheme()) {
			Ext.Loader.loadScriptsSync([
				"/icecap/app/theme/" + i_self.getTheme() + ".css"
			]);
		}

		i_self.enableBubble("onAddWebContainer");
		i_self.enableBubble("stateupdate");
	},

	doInit: function(f_callback) {

		var i_self = this;

		IceCap.util.Handler.useScale(i_self.getScale());

		if (i_self.getSessionValue()) {

			IceCap.util.Handler.addSession(i_self.getSessionValue());

			IceCap.util.Handler.getData(Ext.apply({
				session: i_self.getSessionValue(),
				func: i_self.getFunc(),
				initFunc: i_self.getInitFunc() || i_self.getInitCmd() || "",
			}, i_self.getOptions()), {}, function(o_data) {
				f_callback(o_data);
			});
		}
		else {

			IceCap.util.Handler.getNewSession(function(o_result) {

				if (o_result.success) {

					i_self.setSessionValue(o_result.session);
					i_self.doTimer();
					IceCap.util.Handler.getData(Ext.apply({
						session: o_result.session,
						func: i_self.getFunc(),
						initFunc: i_self.getInitFunc() || i_self.getInitCmd() || "",
					}, i_self.getOptions()), {}, function(o_data) {
						f_callback(o_data);
					});
				}
				else {
					Ext.Msg.show({
						title: "Error",
						msg: "No session pls! retry to connect",
						buttons: Ext.Msg.OK
					});
				}
			});
		}
	},

	createLayout: function() {

		var i_self = this;

		return i_self.createContent();
	},

	createContent: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData() || {};
		var b_focusable = true;

		var i_panel = Ext.widget("panel", {
			layout: {
				type: "vbox",
				align: "stretch"
			},
			border: false,
			focusable: true,
			tabIndex: -1,
			items: i_self.createItems()
		});

		i_self.setMain(i_panel);

		var i_statusbar = i_self.createStatusBar();

		i_self.addDocked(i_statusbar);

		i_self.setStatusBar(i_statusbar);

		i_self.updateMessage(o_inputData);

		return i_panel;
	},

	createStatusBar: function() {

		var i_statusbar = Ext.widget("toolbar", {
			dock: "bottom",
			cls: "icecap-statusbar",
			height: 30,
			hidden: false,
			items: [
				{
					xtype: "label",
					itemId: "icecap-message"
				},
				"->",
				{
					xtype: "label",
					itemId: "icecap-position"
				},
				{
					xtype: "label",
					text: " INS",
					itemId: "icecap-typemode"
				}
			],
			setText: function(s_text) {

				var i_toolbar = this;

				var i_message = i_toolbar.getComponent("icecap-message");

				if (i_message) {
					i_message.setText(s_text);
				}
			}
		});

		return i_statusbar;
	},

	createItems: function() {

		var i_self = this;

		var a_items = [];

		var i_center = Ext.widget("form", {
			bodyPadding: 5,
			border: false,
			layout: "absolute",
			autoScroll: true,
			focusable: false,
			flex: 1,
			ui: ["icecap", i_self.getTheme()].filter(Boolean).join("-")
		});

		a_items.push(i_center);

		var i_south = Ext.widget("panel", {
			layout: {
				type: "column"
			},
			border: false,
			bodyPadding: 5,
			focusable: false,
			defaults: {
				margin: "0 5 5 0"
			},
			ui: ["icecap", i_self.getTheme()].filter(Boolean).join("-")
		});

		a_items.push(i_south);

		i_self.setContent(i_center);
		i_self.setKeyArea(i_south);

		return a_items;
	},

	splitFields: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();

		var a_sliced = Ext.Array.from(o_inputData.fields || []);

		var a_outputs = Ext.Array.from(o_inputData.output || []);
		var a_inputs = Ext.Array.from(o_inputData.input || []);

		Ext.Array.each(a_sliced, function(o_config) {
			if (o_config.type == "input" || o_config.type == "grid") {
				a_inputs.push(o_config);
			}
			else if (o_config.type == "output") {
				a_outputs.push(o_config);
			}
		});

		Ext.apply(o_inputData, {
			input: a_inputs,
			output: a_outputs
		});

		if (a_sliced.length > 0) {
			o_inputData.processedFields = true;
		}

		delete o_inputData.fields;

		i_self.setInputData(o_inputData);
	},

	createFields: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();

		var a_fields = [];

		var n_tabIndex = 1;
		var n_focus = o_inputData.focus;

		var n_inputWidth = o_inputData.width || 80;
		var n_inputHeight = o_inputData.height || 24;

		var b_fixed = IceCap.util.Handler.getFixed();

		var a_sliced = Ext.Array.from(o_inputData.input || []);

		Ext.Array.each(a_sliced, function(o_config, n_index) {

			var n_default = IceCap.util.Handler.getDefaultWidth();
			var n_width = n_default;

			var o_meta = o_config.metaData || {};

			var o_field = {
				inputType: "text",
				xtype: "textfield"
			};

			if (o_config.len > 1) {
				n_width = ((o_config.len - 1) * IceCap.util.Handler.getColSize()) + n_default;
			}

			Ext.apply(o_field, i_self.applyFFW(o_config));

			var s_type = o_field.xtype || "textfield";
			var n_x = IceCap.util.Handler.getX(o_config.col);
			var n_y = IceCap.util.Handler.getY(o_config.lin);
			var b_password = o_config.atr == "27";

			if (o_field.readOnly) {

				o_field.xtype = s_type = "displayfield";

				Ext.apply(o_field.fieldStyle, {
					"font": "normal " + IceCap.util.Handler.getTextSize() + "px / " + IceCap.util.Handler.getColSize() + "px Lucida Console",
					"white-space": "pre",
					"margin-top": IceCap.util.Handler.getMargin() + "px !important",
					"min-height": "0px !important"
				});

				Ext.apply(o_field, {
					style: {
						padding: "1px 0px !important"
					}
				});

				Ext.apply(o_field, {
					fieldCls: ["x-form-display-field iceCap-output-" + (o_config.atr || 20), i_self.getTheme()].filter(Boolean).join("-")
				});
			}

			if (o_config.len > n_inputWidth) {
				var n_temp = n_inputWidth - o_config.col;
				n_width = ((n_temp - 1) * IceCap.util.Handler.getColSize()) + n_default;
			}


			if (o_config.hasOwnProperty("colsize") && o_config.hasOwnProperty("rows")) {

				var s_data = o_config.dta || "";

				o_field.xtype = s_type = "textarea";
				o_field.height = IceCap.util.Handler.getTextSize() * o_config.rows;
				o_field.preventScrollbars = true;

				var a_split = s_data.split("\n");

				var s_result = "";

				var n_split = a_split.length;

				Ext.Array.each(a_split, function(s_part, n_index) {

					var regex = new RegExp(".{" + o_config.colsize + "}", "g");

					var a_match = s_part.match(regex);

					if (a_match) {
						s_result += s_part;
					}
					else {
						if (n_index != n_split - 1) {
							s_result += s_part + "\n";
						}
						else {
							s_result += s_part;
						}
					}
				});

				o_config.dta = s_result;


				o_field.getSubmitValue = function() {

					var i_field = this;

					var s_value = i_field.stripReturns(i_field.getRawValue());

					var regex = new RegExp(".{1,"+ o_config.colsize +"}", "g");

					var a_split = s_value.match(regex) || [];

					return a_split.join("\n");
				};

			}

			var o_listeners = Ext.apply({}, o_meta.listeners);
			delete o_meta.listeners;

			var a_field = ["icecap-field", i_self.getTheme()];

			var a_pass = [b_password ? "iceCap-input-password" : "iceCap-input-" + (o_config.atr || 20)];

			a_pass.push(i_self.getTheme());

			var a_cls = [a_field.filter(Boolean).join("-"), a_pass.filter(Boolean).join("-")];

			var s_cls = a_cls.join(" ");

			if (isNaN(n_x)) {
				n_x = IceCap.util.Handler.getColSize();
			}

			if (isNaN(n_y)) {
				n_y = IceCap.util.Handler.getLineHeight();
			}

			Ext.Object.merge(o_field, {
				width: n_width,
				enforceMaxLength: true,
				maxLength: o_config.len,
				minWidth: IceCap.util.Handler.getDefaultWidth(),
				tabIndex: !o_field.readOnly ? n_tabIndex : -1,
				focusable: !o_field.readOnly,
				lin: o_config.lin,
				col: o_config.col,
				emuId: o_config.id,
				name: o_config.id,
				value: o_config.dta || "",
				itemId: o_config.id,
				inputAttrTpl: "spellcheck=false",
				x: n_x,
				y: n_y,
				cls: s_cls,
				enableKeyEvents: true,
				selectOnFocus: false,
				listeners: {
					afterrender: {
						fn: function(i_field) {

							if (i_field.inputEl && !i_field.metaField) {

								i_field.inputEl.setMinHeight(0);

								if (i_field.xtype == "textfield") {
									i_field.inputEl.setHeight(IceCap.util.Handler.getDefaultHeight());
								}
								if (i_field.xtype == "textarea") {
									i_field.inputEl.dom.setAttribute("rows", o_config.rows);
								}

								i_field.inputEl.set({
									autocomplete: "off"
								});

								if (i_field.getTabIndex() === i_self.getNeedFocus()) {

									i_field.focus(false, 10, function() {
										var o_inputData = i_self.getInputData();

										var s_value = i_field.getValue();
										var n_col = i_field.col;
										var n_diff = 0;

										if (n_col < o_inputData.csrCol) {
											n_diff = o_inputData.csrCol - n_col;
											i_field.inputEl.el.dom.selectionEnd = i_field.inputEl.el.dom.selectionStart = i_field.inputEl.el.dom.selectionStart + n_diff;
										}

										var o_position = {
											col: o_inputData.csrCol,
											lin: o_inputData.csrLin
										}

										i_self.setCurrentPosition(o_position);

										var i_cursor = i_self.getCursor();
										i_cursor.moveCursor(o_position);
										i_cursor.hide();
									});
								}
							}
						}
					},
					change: {
						fn: function(i_field, value) {

							var s_direction = i_field.inputEl.el.dom.selectionDirection;

							var n_curpos = s_direction == "forward" ? i_field.inputEl.el.dom.selectionEnd : i_field.inputEl.el.dom.selectionStart;

							if (value.length == i_field.maxLength && n_curpos == i_field.maxLength) {
								i_self.doNavigationKey(1, true);
							}
						}
					},
					focus: {
						fn: function(i_field) {

							var n_tabIndex = i_field.getTabIndex();
							i_self.setCurrentTabIndex(n_tabIndex);

							var o_position = {
								col: i_field.col,
								lin: i_field.lin
							};
							i_self.setCurrentPosition(o_position);

							var i_cursor = i_self.getCursor();
							i_cursor.moveCursor(o_position);
							i_cursor.hide();
						}
					},
					keyup: {
						fn: function(i_field, i_event) {

							if (i_field.xtype === "textfield") {

								var value = i_field.getValue();

								var s_direction = i_field.inputEl.el.dom.selectionDirection;

								var n_start = s_direction == "forward" ? i_field.inputEl.el.dom.selectionEnd : i_field.inputEl.el.dom.selectionStart;
								var n_add = value.length * IceCap.util.Handler.getColSize();

								if (n_start != value.length) {
									n_add = n_start * IceCap.util.Handler.getColSize();
								}

								var o_input = Ext.apply({
									x: i_field.getLocalX() + n_add,
									y: i_field.getLocalY()
								});

								var o_position = IceCap.util.Handler.toColAndLine(o_input);
								i_self.setCurrentPosition(o_position);

								var i_cursor = i_self.getCursor();
								i_cursor.moveCursor(o_position);
							}
						}
					},
					keydown: {
						fn: function(i_field, i_event) {

							var i_regext = new RegExp("^\\w{1}$");

							var value = i_field.getValue();

							if (i_event.ctrlKey) {

								if (i_event.keyCode == Ext.event.Event.DOWN ||
									i_event.keyCode == Ext.event.Event.UP) {

									i_event.stopEvent();

									var i_main = i_self.getMain();

									var n_y = IceCap.util.Handler.getLineHeight();

									if (i_event.keyCode == Ext.event.Event.UP) {
										n_y = -IceCap.util.Handler.getLineHeight();
									}

									var o_input = Ext.apply({
										x: i_field.getLocalX(),
										y: i_field.getLocalY() + n_y
									});

									var o_position = IceCap.util.Handler.toColAndLine(o_input);
									i_self.setCurrentPosition(o_position);

									var i_cursor = i_self.getCursor();
									i_cursor.moveCursor(o_position);

									var a_inputs = i_self.query("field[focusable]");

									var o_box = Ext.apply({
										width: IceCap.util.Handler.getColSize(),
										height: 1
									}, o_input);

									var b_intersect = false;

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

										var o_bound = i_field.el.dom.getBoundingClientRect();

										var o_rect = Ext.apply({}, {
											x: i_field.getLocalX(),
											y: i_field.getLocalY(),
											width: o_bound.width,
											height: o_bound.height
										});

										if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
											b_intersect = true;
											i_field.focus(false, 10);
											return false;
										}
									});

									if (!b_intersect) {

										i_field.blur();
										i_cursor.show();

										i_main.focus(true, 10);
									}
								}
								if (i_event.keyCode == Ext.event.Event.LEFT ||
									i_event.keyCode == Ext.event.Event.RIGHT) {

									var i_inputEl = i_field.inputEl;
									var n_selection = i_event.keyCode == Ext.event.Event.LEFT ? i_inputEl.dom.selectionStart : i_inputEl.dom.selectionEnd;

									i_event.stopEvent();

									if (n_selection === 0 && i_event.keyCode === Ext.event.Event.LEFT) {

										var i_main = i_self.getMain();

										var n_x = IceCap.util.Handler.getLineHeight();

										var o_input = Ext.apply({
											x: i_field.getLocalX() - n_x,
											y: i_field.getLocalY()
										});

										var o_position = IceCap.util.Handler.toColAndLine(o_input);
										i_self.setCurrentPosition(o_position);

										var i_cursor = i_self.getCursor();
										i_cursor.moveCursor(o_position);

										var a_inputs = i_self.query("field[focusable]");

										var o_box = Ext.apply({
											width: IceCap.util.Handler.getColSize(),
											height: 1
										}, o_input);

										var b_intersect = false;

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

											var o_bound = i_field.el.dom.getBoundingClientRect();

											var o_rect = Ext.apply({}, {
												x: i_field.getLocalX(),
												y: i_field.getLocalY(),
												width: o_bound.width,
												height: o_bound.height
											});

											if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
												b_intersect = true;
												i_field.focus(false, 10);
												return false;
											}
										});

										if (!b_intersect) {

											i_field.blur();
											i_cursor.show();

											i_main.focus(true, 10);
										}
									}
									else if (n_selection == value.length && i_event.keyCode == Ext.event.Event.RIGHT) {

										var i_main = i_self.getMain();

										var n_x = IceCap.util.Handler.getColSize();

										n_x = n_x * (i_field.maxLength + 2);

										var o_input = Ext.apply({
											x: i_field.getLocalX() + n_x,
											y: i_field.getLocalY()
										});

										var o_position = IceCap.util.Handler.toColAndLine(o_input);
										i_self.setCurrentPosition(o_position);

										var i_cursor = i_self.getCursor();
										i_cursor.moveCursor(o_position);

										var a_inputs = i_self.query("field[focusable]");

										var o_box = Ext.apply({
											width: IceCap.util.Handler.getColSize(),
											height: 1
										}, o_input);

										var b_intersect = false;

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

											var o_bound = i_field.el.dom.getBoundingClientRect();

											var o_rect = Ext.apply({}, {
												x: i_field.getLocalX(),
												y: i_field.getLocalY(),
												width: o_bound.width,
												height: o_bound.height
											});

											if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
												b_intersect = true;
												i_field.focus(false, 10);
												return false;
											}
										});

										if (!b_intersect) {

											i_field.blur();
											i_cursor.show();

											i_main.focus(true, 10);
										}
									}
								}
							}
							else {

								if (i_field.xtype == "textarea") {

									if (i_event.keyCode == Ext.event.Event.UP) {

										var s_value = i_field.stripReturns(i_field.getRawValue());

										var regex = new RegExp("^.*\n$|.{1," + o_config.colsize + "}", "gm");

										var a_lines = s_value.match(regex) || [];
										var a_newLines = s_value.split("\n");

										var n_start = i_field.inputEl.dom.selectionStart;

										var n_row = 1;
										var n_char = 0;
										var b_last = i_field.inputEl.dom.selectionStart == i_field.inputEl.dom.value.length;

										Ext.Array.each(a_lines, function (s_part, n_index) {

											n_char += s_part.length;

											if (n_char <= n_start) {
												n_row++;
											}
											else {
												return false;
											}
										});

										if (n_row > 1) {

											i_event.stopEvent();

											n_char = 0;
											var n_left = 0;
											var n_current = 0;
											var n_nextLine = 0;

											n_start = n_start - (a_newLines.length - 1);

											Ext.Array.each(a_lines, function (s_part, n_index) {

												n_char += s_part.length;

												var n_count = 0;

												n_left = 0;

												while (n_count < s_part.length) {
													n_count++;
													if (n_current < n_start) {
														n_current++;
														n_left++;
													}
												}

												if (n_char < n_start) {
													n_row++;
												}
												else {
													n_nextLine = n_index - 1;
													return false;
												}
											});

											if (n_nextLine == -1) {
												n_nextLine = 0;
											}

											var n_newPos = 0;

											Ext.Array.each(a_lines, function (s_part, n_index) {

												if (n_index < n_nextLine) {
													n_newPos += s_part.length;
												}
												else {
													n_newPos += n_left;
													return false;
												}
											});

											i_event.stopEvent();
											i_field.inputEl.dom.setSelectionRange(n_newPos, n_newPos);
										}
									}

									if (i_event.keyCode == Ext.event.Event.DOWN) {

										var s_value = i_field.stripReturns(i_field.getRawValue());
										var n_length = s_value.length;

										var regex = new RegExp("^.*\n$|.{1," + o_config.colsize + "}", "g");
										var n_pos = i_field.inputEl.dom.selectionStart;

										var a_lines = s_value.match(regex) || [];
										var a_newLines = s_value.split("\n");

										if (a_lines.length < o_config.rows && a_newLines.length < o_config.rows) {

											i_event.stopEvent();

											s_value = s_value + "\n";

											i_field.setValue(s_value);

											i_field.inputEl.dom.setSelectionRange(s_value.length, s_value.length);
										}
										else {
											var n_row = 1;
											var n_char = 0;
											var n_left = 0;
											var n_current = 0;
											var n_nextLine = 0;

											Ext.Array.each(a_lines, function (s_part, n_index) {

												n_char += s_part.length;

												var n_count = 0;

												n_left = 0;

												while(n_count < s_part.length) {
													n_count++;
													if (n_current < n_pos) {
														n_current++;
														n_left++;
													}
												}

												if (n_char <= n_pos) {
													n_row++;
												}
												else {
													n_nextLine = n_index + 1;
													return false;
												}
											});

											if (n_nextLine < a_lines.length || n_nextLine < a_newLines.length) {

												var n_newPos = 0;

												Ext.Array.each(a_lines, function(s_part, n_index) {

													if (n_index < n_nextLine) {
														n_newPos += s_part.length;
													}
													else {
														n_newPos += n_left + (a_newLines.length - 1);
														return false;
													}
												});

												if (n_row < o_config.rows) {
													i_event.stopEvent();
													i_field.inputEl.dom.setSelectionRange(n_newPos, n_newPos);
												}
											}
										}

									}
								}
								else if (i_field.xtype == "textfield") {

									var n_typeMode = i_self.getTypeMode();

									if (n_typeMode == IceCap.util.Handler.typeMode.OVERWRITE) {

										var s_direction = i_field.inputEl.el.dom.selectionDirection;

										var n_curpos = s_direction == "forward" ? i_field.inputEl.el.dom.selectionEnd : i_field.inputEl.el.dom.selectionStart;

										var s_key = String.fromCharCode(i_event.keyCode);

										if (n_curpos < value.length && i_regext.test(s_key)) {

											var a_values = value.split("");
											a_values[n_curpos] = i_event.browserEvent.key;
											value = a_values.join("");
											i_field.setValue(value);
											i_field.inputEl.el.dom.selectionEnd = i_field.inputEl.el.dom.selectionStart = n_curpos + 1;
											i_event.stopEvent();
										}
									}
								}
							}
						}
					}
				}
			}, o_meta);

			var o_clone = Ext.clone(o_field);

			if (o_meta.xtype) {

				o_clone.metaField = true;

				o_clone.xtype = s_type = o_meta.xtype;

				delete o_clone.cls;
				delete o_clone.fieldStyle;
				delete o_clone.inputType;
			}

			if (o_config.type == "grid") {

				Ext.apply(o_clone, {
					xtype: s_type = "iceCapGrid",
					gridConfig: o_config.grid,
					width: IceCap.util.Handler.getColSize() * n_inputWidth
				});

				delete o_clone.cls;
				delete o_clone.fieldStyle;
				delete o_clone.inputType;
				delete o_clone.listeners;
				delete o_clone.grid;

				Ext.apply(o_clone, {
					listeners: {
						fieldoption: {
							fn: function(a_config) {
								i_self.fireEvent("fieldoption", a_config);
							}
						},
						key: {
							fn: function(s_key) {
								i_self.fireEvent("key", s_key);
							}
						}
					}
				});
			}

			var i_field = Ext.widget(s_type, o_clone);

			var o_on = {};

			Ext.Object.each(o_listeners, function(s_key, value) {

				if (Ext.isFunction(value)) {
					o_on[s_key] = Ext.apply({}, {
						fn: value,
						scope: i_self
					});
				}
				else if (Ext.isObject(value)) {
					value.scope = i_self;
					o_on[s_key] = value;
				}
			});

			i_field.on(o_on);

			if (!i_field.readOnly) {
				n_tabIndex++;
			}

			if (o_config.atr == 25) {

				if (o_field.readOnly) {
					i_field.addCls("icecap-invalid");
				}
				else {
					i_field.markInvalid(true);
				}
			}

			a_fields.push(i_field);
		});

		n_tabIndex = n_tabIndex - 1;
		i_self.setLastTabIndex(n_tabIndex);
		i_self.setCurrentTabIndex(o_inputData.focus);

		i_self.setNeedFocus(n_focus);

		return a_fields;
	},

	createLabels: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();
		var b_fixed = IceCap.util.Handler.getFixed();

		var a_fields = [];

		Ext.Array.each(o_inputData.output, function(o_config) {

			var s_value = o_config.dta || o_config.label || o_config.text;

			if (s_value) {
				if (o_config.ta == "right" && !o_config.off) {
					//o_config.col += o_config.len - o_config.dta.length;
				}

				if (o_config.off) {
					s_value = Ext.String.leftPad(s_value, s_value.length + o_config.off);
				}

				var n_x = Math.ceil((IceCap.util.Handler.getX(o_config.col) + (IceCap.util.Handler.getColSize() / 2)));
				var n_y = IceCap.util.Handler.getY(o_config.lin);

				if (isNaN(n_x)) {
					n_x = IceCap.util.Handler.getColSize();
				}

				if (isNaN(n_y)) {
					n_y = IceCap.util.Handler.getLineHeight();
				}

				var o_field = {
					value: s_value,
					htmlEncode: true,
					x: n_x - 1,
					y: n_y,
					cls: ["icecap-field", i_self.getTheme()].filter(Boolean).join("-")
				};

				var o_style = {
					"font": "normal " + IceCap.util.Handler.getTextSize() + "px Segoe UI,Segoe,DejaVu Sans,Trebuchet MS,Verdana,sans-serif",
					"margin-top": IceCap.util.Handler.getMargin() + "px !important",
					"min-height": "0px !important"
				};

				if (b_fixed) {

					Ext.apply(o_field, {
						fieldCls: ["x-form-display-field iceCap-output-" + (o_config.atr || 20), i_self.getTheme()].filter(Boolean).join("-")
					});

					Ext.apply(o_style, {
						"font": "normal " + IceCap.util.Handler.getTextSize() + "px / " + IceCap.util.Handler.getColSize() + "px Lucida Console",
						"white-space": "pre",
						"margin-top": IceCap.util.Handler.getMargin() + "px !important",
						"min-height": "0px !important"
					});
				}

				Ext.apply(o_field, {
					fieldStyle: o_style
				});

				if (o_config.atr == "27") {
					o_field.hidden = true;
				}

				var i_field = Ext.widget("displayfield", o_field);

				if (o_config.atr == 23) {

					//i_field.addCls("icecap-invalid");
				}

				a_fields.push(i_field);
			}
		});

		return a_fields;
	},

	createMetaFields: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();

		var a_fields = [];
		var n_tabIndex = i_self.getLastTabIndex() || 0;

		var a_sliced = Ext.Array.slice(o_inputData.metaFields || []);

		var n_inputHeight = o_inputData.height || 24;

		var n_inputWidth = o_inputData.width || 80;

		Ext.Array.each(a_sliced, function(o_config) {

			var n_x = o_config.x;
			var n_y = o_config.y;

			if (!o_config.x) {
				n_x = Math.ceil((IceCap.util.Handler.getX(o_config.col) + (IceCap.util.Handler.getColSize() / 2)));
			}

			if (!o_config.y) {
				n_y = IceCap.util.Handler.getY(o_config.lin);
			}

			if (isNaN(n_x)) {
				n_x = IceCap.util.Handler.getColSize();
			}

			if (isNaN(n_y)) {
				n_y = IceCap.util.Handler.getLineHeight();
			}

			var o_listeners = o_config.listeners || {};

			Ext.apply(o_config, {
				tabIndex: n_tabIndex + 1,
				keys: Ext.Array.slice(o_inputData.keys),
				theme: i_self.getTheme(),
				x: n_x,
				y: n_y,
				listeners: {
					fieldoption: {
						fn: function(a_config) {
							i_self.fireEvent("fieldoption", a_config);
						}
					},
					key: {
						fn: function(s_key) {
							i_self.fireEvent("key", s_key);
						}
					}
				}
			});

			o_config.xtype = o_config.xtype || o_config.type || "textfield";

			o_config.name = o_config.id;

			delete o_config.id;

			var i_field = Ext.widget(o_config.xtype, o_config);

			var o_on = {};

			if (!Ext.Object.isEmpty(o_listeners)) {

				Ext.Object.each(o_listeners, function(s_key, value) {

					if (Ext.isFunction(value)) {
						o_on[s_key] = Ext.apply({}, {
							fn: value,
							scope: i_self
						});
					}
					else if (Ext.isObject(value)) {
						value.scope = i_self;
						o_on[s_key] = value;
					}
				});
			}

			i_field.on(o_on);

			a_fields.push(i_field);

			n_tabIndex++;
		});

		i_self.setLastTabIndex(n_tabIndex);

		return a_fields;
	},

	createKeys: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();
		var a_disabledKey = o_inputData.disabledKeys || [];
		var a_buttons = [];

		var i_template = Ext.create("Ext.XTemplate",
			"{id}={text}"
		);

		Ext.Array.each(o_inputData.keys, function(o_config) {

			var s_template = i_template.apply(o_config);

			var o_found = Ext.Array.findBy(a_disabledKey, function(o_disabled) {
				return o_config.id == o_disabled.id;
			});

			o_found = o_found || {
				showKey: 1
			};

			var b_hidden = Number(o_found.showKey) < 1  || false;

			var i_button = Ext.widget("button", {
				text: s_template,
				key: o_config.id,
				focusable: false,
				cls: "icecap-key",
				hidden: b_hidden,
				handler: function(i_button) {
					i_self.doKeyEvent(i_button.key);
				}
			});

			a_buttons.push(i_button);
		});

		return a_buttons;
	},

	doCreateFromData: function(o_inputData) {

		var i_self = this;
		var i_controller = i_self.getController();

		i_self.setInputData(o_inputData);
		i_self.splitFields();

		var b_fixed = o_inputData.hasOwnProperty("fixed") ? o_inputData.fixed : true;

		IceCap.util.Handler.setFixed(b_fixed);

		i_self.suspendLayouts();

		if (o_inputData.window && o_inputData.window.level > 0) {
			if (i_self.rendered) {
				i_self.mask(false);
				i_self.createWindow(o_inputData);
			}
		}
		else {

			if (i_self.rendered) {
				i_self.unmask();
			}

			if (o_inputData.title) {
				i_self.doTitleUpdate(o_inputData.title);
			}

			var i_content = i_self.getContent();

			var i_keyArea = i_self.getKeyArea();
			var i_main = i_self.getMain();

			if (i_content.rendered) {
				i_content.removeAll(true);
			}

			if (i_keyArea.rendered) {
				i_keyArea.removeAll(true);
			}

			var a_inputs = o_inputData.input || [];
			var n_tabIndex = a_inputs.length > 0 ? 0 : 1;

			var b_focusable = n_tabIndex > 0;

			i_content.focusable = b_focusable;
			i_content.setTabIndex(n_tabIndex);

			i_self.setCursor(null);

			var a_created = i_self.createFields();
			a_created = a_created.concat(i_self.createLabels());
			a_created = a_created.concat(i_self.createMetaFields());

			a_created.push(i_self.createCursor());
			a_created.push(i_self.createMarker());

			if (o_inputData.processedFields) {

				var a_toAdd = Ext.Array.slice(a_created);

				var n_inputWidth = o_inputData.width || 80;
				var n_inputHeight = o_inputData.height || 24;

				var i_container = Ext.widget("container", {
					layout: "anchor",
					width: n_inputWidth * IceCap.util.Handler.getColSize(),
					height: n_inputHeight * IceCap.util.Handler.getLineHeight(),
					focusable: false,
					tabIndex: -1,
					defaults: {
						margin: "5 0 0 0"
					},
					items: a_toAdd
				});

				a_created = [i_container];
			}

			i_content.add(a_created);

			var a_fields = i_content.query("field[focusable]");
			var a_grids = i_content.query("gridpanel");

			var b_found = false;
			var b_error = false;

			if (o_inputData.msgBox && o_inputData.msgBox.text) {
				b_error = true;
				i_self.onError(o_inputData);
			}

			if (a_fields.length > 0) {
				b_found = Ext.Array.findBy(a_fields, function(i_field) {
					return i_field.getTabIndex() === i_self.getNeedFocus();
				});

				if (!b_found) {
					b_found = true;
					var i_field = a_fields[0];
					if (!b_error) {
						setTimeout(function() {
							i_field.focus(false, 10);
						}, 10);
					}
				}
			}

			if (!b_found) {

				i_self.setCurrentPosition({
					col: o_inputData.csrCol || 1,
					lin: o_inputData.csrLin || 1
				});

				if (!b_error) {

					if (a_grids.length == 0) {
						i_content.focusable = true;
						setTimeout(function() {
							i_content.focus(true, 10);
						}, 10);
					}
				}
			}

			i_self.updateMessage(o_inputData);

			var a_keys = i_self.createKeys();
			i_keyArea.add(a_keys);

			i_keyArea[a_keys.length === 0 ? "hide" : "show"]();

			if (o_inputData.cmd) {

				i_controller.onPCO(o_inputData);
				i_controller.doKeyEvent("ENTER");
			}

			var i_marker = i_self.getMarker();
			i_marker.setInUse(i_self.getMarkerActive());
		}
		IceCap.util.Handler.setFixed(true);

		i_self.resumeLayouts(true);

		var a_metafields = o_inputData.metaFields || [];

		if (
			o_inputData.input.length === 0 &&
			o_inputData.input.length === 0 &&
			a_metafields.length === 0 && o_inputData.messageText) {

				var s_message = o_inputData.messageText;

				i_self.setMessageText(s_message);

				i_self.setLoading(s_message);
				i_controller.onSendData({
					func: ""
				}, {});
			}
	},

	createWindow: function(o_inputData) {

		var i_self = this;
		var o_config = o_inputData.window;
		var i_active = i_self.getActiveWindow();
		var a_keys = o_inputData.keys || [];

		var i_content = i_self.getContent();

		var o_size = {
			width: (o_config.width * (IceCap.util.Handler.getColSize() + 1)),
			height: (o_config.height * IceCap.util.Handler.getLineHeight()) + (a_keys.length > 0 ? 105 : 70)
		};

		if (i_active) {

			i_active.setTitle(o_config.title);

			i_active.getContent().setSize(o_size);

			i_active.setInputData(o_inputData);

			i_active.recreate();
		}
		else {

			i_active = Ext.create("IceCap.view.Window", {
				title: o_config.title,
				inputData: o_inputData,
				sessionValue: i_self.getSessionValue(),
				modal: false,
				onEsc: Ext.emptyFn,
				theme: i_self.getTheme(),
				markerActive: i_self.getMarkerActive(),
				options: i_self.getOptions(),
				listeners: {
					destroy: {
						fn: function(i_window) {
							i_self.setActiveWindow(null);
						}
					},
					toWindow: {
						fn: function(i_window, o_inputData) {
							if (!o_inputData.close) {
								i_self.doCreateFromData(o_inputData);
								if (o_inputData.window && !o_inputData.window.level) {
									i_window.close();
								}
							}
							else {
								i_window.close();
								i_self.close();
							}
						}
					}
				}
			});

			i_content.add(i_active);

			i_active.getContent().setSize(o_size);

			i_active.show();
		}

		i_self.setActiveWindow(i_active);

		var i_query = Ext.get(i_self.getMaskTarget().query(".x-mask"));

		Ext.Array.each(i_query.elements, function(i_mask) {
			Ext.get(i_mask).setStyle("opacity", 0);
		});
	},

	getCurrentField: function() {

		var i_self = this;

		var a_fields = i_self.query("field[focusable]");
		var n_tabIndex = i_self.getCurrentTabIndex();
		var i_controller = i_self.getController();

		var i_content = i_self.getContent();
		var i_form = i_content.getForm();
		var o_values = i_form.getFieldValues(true);

		var i_currentField;

		Ext.Array.each(a_fields, function(i_field) {
			if (i_field.getTabIndex() === n_tabIndex && i_field.hasFocus) {
				i_currentField = i_field;
				return false;
			}
		});

		return i_currentField;
	},

	doKeyEvent: function(s_value) {

		var i_self = this;

		var i_controller = i_self.getController();

		var o_inputData = i_self.getInputData() || {};

		var a_disabledKeys = Ext.Array.from(o_inputData.disabledKeys || []);

		var o_found = Ext.Array.findBy(a_disabledKeys, function(o_key) {
			return s_value == o_key.id;
		});

		if (!o_found)  {
			i_controller.doKeyEvent(s_value);
		}
		else {

			Ext.Msg.show({
				title: "Error",
				msg: "Funktionstasten er ikke tilladt",
				icon: Ext.MessageBox.INFO,
				buttons: Ext.MessageBox.OK,
				fn: function () {
					i_self.findFocus();
				}
			});
		}
	},

	doNavigationKey: function(n_direction, b_tab) {

		var i_self = this;
		var i_controller = i_self.getController();
		i_controller[b_tab ? "doTabKey" : "doArrowKeys"](n_direction);
	},

	addKeyEvents: function() {

		var i_self = this;

		var a_bindings = [
			{
				key: Ext.event.Event.PAGE_DOWN,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doKeyEvent("ROLLUP");
				}
			},
			{
				key: Ext.event.Event.PAGE_UP,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doKeyEvent("ROLLDOWN");
				}
			},
			{
				key: Ext.event.Event.ENTER,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doKeyEvent("ENTER");
				}
			},
			{
				key: Ext.event.Event.ESC,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doKeyEvent("ATTN");
				}
			},
			{
				key: Ext.event.Event.UP,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;

					if (!i_event.target.type) {

						i_event.stopEvent();

						var i_cursor = i_self.getCursor();
						var o_position = i_self.getCurrentPosition();

						var a_inputs = i_self.query("field[focusable]");

						o_position.lin = o_position.lin - 1;

						if (o_position.lin < 1) {
							o_position.lin = 1;
						}

						var o_box = Ext.apply({
							x: IceCap.util.Handler.getX(o_position.col + 1),
							y: IceCap.util.Handler.getY(o_position.lin),
							width: IceCap.util.Handler.getColSize(),
							height: 1
						});

						var b_intersect = false;

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

							var o_bound = i_field.el.dom.getBoundingClientRect();

							var o_rect = Ext.apply({}, {
								x: i_field.getLocalX(),
								y: i_field.getLocalY(),
								width: o_bound.width,
								height: o_bound.height
							});

							if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
								b_intersect = true;
								i_field.focus(false, 10);
								return false;
							}
						});

						if (!b_intersect) {
							i_cursor.moveCursor(o_position);
						}
						i_self.setCurrentPosition(o_position);
					}
					else {
						i_self.doNavigationKey(0);
					}
				}
			},
			{
				key: Ext.event.Event.DOWN,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;

					if (!i_event.target.type) {

						i_event.stopEvent();

						var i_cursor = i_self.getCursor();
						var o_position = i_self.getCurrentPosition();
						var o_inputData = i_self.getInputData();

						var a_inputs = i_self.query("field[focusable]");

						o_position.lin = o_position.lin + 1;

						if (o_position.lin > o_inputData.height) {
							o_position.lin = o_inputData.height;
						}

						var o_box = Ext.apply({
							x: IceCap.util.Handler.getX(o_position.col + 1),
							y: IceCap.util.Handler.getY(o_position.lin),
							width: IceCap.util.Handler.getColSize(),
							height: 1
						});

						var b_intersect = false;

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

							var o_bound = i_field.el.dom.getBoundingClientRect();

							var o_rect = Ext.apply({}, {
								x: i_field.getLocalX(),
								y: i_field.getLocalY(),
								width: o_bound.width,
								height: o_bound.height
							});

							if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
								b_intersect = true;
								i_field.focus(false, 10);
								return false;
							}
						});

						if (!b_intersect) {
							i_cursor.moveCursor(o_position);
						}
						i_self.setCurrentPosition(o_position);
					}
					else {
						i_self.doNavigationKey(1);
					}
				}
			},
			{
				key: Ext.event.Event.LEFT,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;

					if (!i_event.target.type) {
						i_event.stopEvent();

						var i_cursor = i_self.getCursor();
						var o_position = i_self.getCurrentPosition();

						var a_inputs = i_self.query("field[focusable]");

						o_position.col = o_position.col - 1;

						if (o_position.col < 1) {
							o_position.col = 1;
						}

						var o_box = Ext.apply({
							x: IceCap.util.Handler.getX(o_position.col),
							y: IceCap.util.Handler.getY(o_position.lin),
							width: IceCap.util.Handler.getColSize(),
							height: 1
						});

						var b_intersect = false;

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

							var o_bound = i_field.el.dom.getBoundingClientRect();

							var o_rect = Ext.apply({}, {
								x: i_field.getLocalX(),
								y: i_field.getLocalY(),
								width: o_bound.width,
								height: o_bound.height
							});

							if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
								b_intersect = true;
								i_field.focus(false, 10);
								return false;
							}
						});

						if (!b_intersect) {
							i_cursor.moveCursor(o_position);
						}

						i_self.setCurrentPosition(o_position);
					}
				}
			},
			{
				key: Ext.event.Event.RIGHT,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;

					if (!i_event.target.type) {

						i_event.stopEvent();

						var i_cursor = i_self.getCursor();
						var o_position = i_self.getCurrentPosition();
						var o_inputData = i_self.getInputData();

						var a_inputs = i_self.query("field[focusable]");

						o_position.col = o_position.col + 1;

						if (o_position.col > o_inputData.width) {
							o_position.col = o_inputData.width;
						}

						var o_box = Ext.apply({
							x: IceCap.util.Handler.getX(o_position.col + 1),
							y: IceCap.util.Handler.getY(o_position.lin),
							width: IceCap.util.Handler.getColSize(),
							height: 1
						});

						var b_intersect = false;

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

							var o_bound = i_field.el.dom.getBoundingClientRect();

							var o_rect = Ext.apply({}, {
								x: i_field.getLocalX(),
								y: i_field.getLocalY(),
								width: o_bound.width,
								height: o_bound.height
							});

							if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
								b_intersect = true;
								i_field.focus(false, 10);
								return false;
							}
						});

						if (!b_intersect) {
							i_cursor.moveCursor(o_position);
						}

						i_self.setCurrentPosition(o_position);
					}
				}
			},
			{
				key: Ext.event.Event.TAB,
				shift: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_event.stopPropagation();

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doNavigationKey(1, true);
				}
			},
			{
				key: Ext.event.Event.TAB,
				shift: true,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_event.stopPropagation();

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doNavigationKey(0, true);
				}
			},
			{
				key: Ext.event.Event.NUM_PLUS,
				fn: function(n_key, i_event) {

					i_event.stopEvent();

					var i_key = this;
					var i_self = i_key.target.component;

					var i_field = i_self.getCurrentField();

					if (i_field) {

						var n_start = i_field.inputEl.el.dom.selectionStart;

						var s_text = i_field.getValue();

						s_text = s_text.substr(0, n_start);

						i_field.setValue(s_text);

						i_self.doNavigationKey(1, true);
					}
				}
			},
			{
				key: Ext.event.Event.HOME,
				ctrl: true,
				alt: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();

					var i_key = this;
					var i_self = i_key.target.component;

					var b_markerActive = i_self.getMarkerActive();
					var i_marker = i_self.getMarker();

					i_self.setMarkerActive(!b_markerActive);

					i_marker.toggle();
				}
			},
			{
				key: Ext.event.Event.INSERT,
				ctrl: false,
				alt: false,
				fn: function (n_key, i_event) {

					var n_type = i_self.getTypeMode();
					switch (n_type) {
						case IceCap.util.Handler.typeMode.OVERWRITE:
							n_type = IceCap.util.Handler.typeMode.INSERT;
							break;
						default:
							n_type = IceCap.util.Handler.typeMode.OVERWRITE;
							break;
					}

					i_self.setTypeMode(n_type);
				}
			}
		];

		a_bindings = a_bindings.concat(i_self.getFKeys());

		var i_keyMap = Ext.create("Ext.util.KeyMap", {
			target: i_self.getEl(),
			binding: a_bindings
		});
	},

	getFKeys: function() {

		var i_self = this;

		var a_keys = [
			{
				key: Ext.event.Event.F1,
				shift: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F1");
				}
			},
			{
				key: Ext.event.Event.F2,
				shift: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F2");
				}
			},
			{
				key: Ext.event.Event.F3,
				shift: false,
				alt: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F3");
				}
			},
			{
				key: Ext.event.Event.F4,
				shift: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F4");
				}
			},
			{
				key: Ext.event.Event.F5,
				shift: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F5");
				}
			},
			{
				key: Ext.event.Event.F6,
				shift: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F6");
				}
			},
			{
				key: Ext.event.Event.F7,
				shift: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F7");
				}
			},
			{
				key: Ext.event.Event.F8,
				shift: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F8");
				}
			},
			{
				key: Ext.event.Event.F9,
				shift: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F9");
				}
			},
			{
				key: Ext.event.Event.F10,
				shift: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F10");
				}
			},
			{
				key: Ext.event.Event.F11,
				shift: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F11");
				}
			},
			{
				key: Ext.event.Event.F12,
				shift: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F12");
				}
			}
		];

		a_keys = a_keys.concat(i_self.getFShiftKeys());

		return a_keys;
	},

	getFShiftKeys: function() {

		var i_self = this;

		var a_keys = [
			{
				key: Ext.event.Event.F1,
				shift: true,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F13");
				}
			},
			{
				key: Ext.event.Event.F2,
				shift: true,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F14");
				}
			},
			{
				key: Ext.event.Event.F3,
				shift: true,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F15");
				}
			},
			{
				key: Ext.event.Event.F4,
				shift: true,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F16");
				}
			},
			{
				key: Ext.event.Event.F5,
				shift: true,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F17");
				}
			},
			{
				key: Ext.event.Event.F6,
				shift: true,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F18");
				}
			},
			{
				key: Ext.event.Event.F7,
				shift: true,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F19");
				}
			},
			{
				key: Ext.event.Event.F8,
				shift: true,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F20");
				}
			},
			{
				key: Ext.event.Event.F9,
				shift: true,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F21");
				}
			},
			{
				key: Ext.event.Event.F10,
				shift: true,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F22");
				}
			},
			{
				key: Ext.event.Event.F11,
				shift: true,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F23");
				}
			},
			{
				key: Ext.event.Event.F12,
				shift: true,
				fn: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.doKeyEvent("F24");
				}
			}
		];

		return a_keys;
	},

	createCursor: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();

		var i_cursor = Ext.widget("component", {
			cls: ["iceCap-cursor", i_self.getTheme()].filter(Boolean).join("-"),
			autoEl: {
				tag: "div",
				style: {
					"border": "1px solid red;",
					"position": "absolute !important;",
					"width": (IceCap.util.Handler.getColSize() + 0.3) + "px !important;"
				}
			},
			listeners: {
				hide: {
					fn: function(i_cursor) {

						var i_marker = i_self.getMarker();
						//i_marker.hide();
					}
				},
				show: {
					fn: function(i_cursor) {

						var i_marker = i_self.getMarker();
						var b_active = i_marker.getInUse();

						if (b_active) {
							i_marker.show();
						}
					}
				},
				render: {
					fn: function(i_item) {
						i_item.hide();
					}
				},
				afterrender: {
					fn: function(i_cursor) {

						var i_content = i_self.getContent();

						var a_fields = i_content.query("field[focusable]");

						if (a_fields.length === 0) {

							i_cursor.show();

							i_cursor.moveCursor({
								col: o_inputData.csrCol || 1,
								lin: o_inputData.csrLin || 1
							});
						}
					}
				}
			},
			moveCursor: function(o_position) {

				var i_cursor = this;
				var i_marker = i_self.getMarker();

				i_cursor.setPosition({
					x: IceCap.util.Handler.getX(o_position.col) + 3,
					y: (IceCap.util.Handler.getY(o_position.lin + 1) - IceCap.util.Handler.getColSize()) + 3
				});

				var a_pos = [
					IceCap.util.Handler.getX(o_position.col),
					(IceCap.util.Handler.getY(o_position.lin + 1) - IceCap.util.Handler.getColSize()) + 3
				];

				i_marker.move(a_pos);

				i_self.updateCurrentPosition(o_position);
			}
		});

		i_self.setCursor(i_cursor);

		return i_cursor;
	},

	createMarker: function() {

		var i_self = this;

		var i_marker = Ext.widget("icecap-marker", {
			hidden: true,
			active: false,
			inUse: false,
			ui: i_self.getTheme() || "default"
		});

		i_self.setMarker(i_marker);

		return i_marker;
	},

	findFocus: function() {

		var i_self = this;
		var i_main = i_self.getMain();
		var i_content = i_self.getContent();

		var a_fields = i_content.query("field[focusable]");

		if (a_fields.length > 0) {

			var i_field = a_fields[0];
			i_field.focus(false, 10);
		}
		else {
			i_main.focus(false, 10);
		}
	},

	doTimer: function() {

		var i_self = this;
		var s_message = i_self.getMessageText();

		var i_timer = i_self.getTimer();
		if (!i_timer) {
			i_timer = new Ext.util.DelayedTask(function() {

				i_self.setLoading(s_message ? s_message :true);
			});

			i_self.setTimer(i_timer);

			i_timer.delay(1000);
		}
	},

	cancelTimer: function() {

		var i_self = this;

		var i_timer = i_self.getTimer();

		if (i_timer) {
			i_timer.cancel();
			i_self.setTimer(null);
		}
	},

	doTitleUpdate: function(s_value) {

		var i_self = this;
		var i_window = i_self.up("window");
		i_self.setTitle("");

		if (i_window && !i_window.isXType("ip2DomainModelEntityManager", false)) {

			if (!i_self.getIgnoreTitle()) {
				i_window.setTitle(s_value);
			}
		}
		else {
			if (!i_self.getIgnoreTitle()) {
				i_self.setTitle(s_value);
			}
		}
	},

	onError: function(o_inputData) {

		var i_self = this;

		Ext.Msg.show({
			title: "Server Message",
			msg: o_inputData.msgBox.text,
			icon: Ext.MessageBox.INFO,
			buttons: Ext.MessageBox.OK,
			fn: function(btn) {
				i_self.findFocus();
			}
		});
	},

	updateMessage: function(o_inputData) {

		var i_self = this;
		var i_statusbar = i_self.getStatusBar();

		var a_messages = Ext.Array.from(o_inputData.message);

		if (a_messages.length > 0) {

			var a_result = [];

			Ext.Array.each(a_messages, function(o_message) {
				a_result.push(o_message.text);
			});

			i_statusbar.setText(a_result.join(" "));
		}
	},

	applyFFW: function(o_config) {

		var i_self = this;
		var s_hex = o_config.ffw;
		var b_fixed = IceCap.util.Handler.getFixed();

		var o_field = {
			xtype: "textfield"
		};

		var o_fieldStyle = {
			font: "normal " + IceCap.util.Handler.getTextSize() + "px Segoe UI,Segoe,DejaVu Sans,Trebuchet MS,Verdana,sans-serif",
			padding: "1px 3px 2px 3px !important"
		};

		if (b_fixed) {

			Ext.apply(o_fieldStyle, {
				font: "normal " + IceCap.util.Handler.getTextSize() + "px / " + IceCap.util.Handler.getColSize() + "px Lucida Console",
			});
		}

		if (o_config.ta) {

			Ext.apply(o_fieldStyle, {
				"text-align": o_config.ta
			});
		}

		if (s_hex) {

			var n_hex = parseInt(s_hex);

			var n_sanHex = IceCap.util.Handler.createIndicators(s_hex);

			if (n_sanHex & IceCap.util.Handler.FFW.BYPASS) {

				Ext.apply(o_field, {
					readOnly: true
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.ALPHA_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[a-z A-Z ,.-]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.NUMERIC_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[+\\-,. 0-9]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.DIGITS_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[0-9]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MOD_DATA) {

				Ext.apply(o_field, {
					listeners: {
						afterrender: {
							fn: function(i_field) {

								i_field.originalValue = 0xFF;
							},
							single: true
						}
					}
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.AUTO_ENTER) {

				// THIS WE DONT NEED IN THE MODERN WORLD...

				// Ext.apply(o_field, {
				// 	listeners: {
				// 		blur: {
				// 			fn: function(i_field) {
				// 				i_self.doKeyEvent("ENTER");
				// 			}
				// 		}
				// 	}
				// });
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MONOCASE) {

				// Ext.apply(o_fieldStyle, {
				// 	"text-transform": "uppercase"
				// });
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MAND_ENTER) {

				Ext.apply(o_field, {
					allowBlank: false
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.RA_ZEROFILL ||
				n_sanHex & IceCap.util.Handler.FFW.RA_BLANKFILL ||
				n_sanHex & IceCap.util.Handler.FFW.RA_MANDATORY) {

				Ext.apply(o_fieldStyle, {
					"text-align": "right"
				});
			}

			if (o_config.atr == "27" && n_hex & (1 << 13)) {

				Ext.apply(o_field, {
					hidden: true,
					disabled: true
				});
			}
		}

		Ext.apply(o_field, {
			fieldStyle: o_fieldStyle
		});

		return o_field;
	},

	getState: function() {

		var i_self = this;

		var o_inputData = i_self.getInputData();
		var s_sessionValue = i_self.getSessionValue();

		var o_result = {
			appConfig: {
				inputData: o_inputData,
				sessionValue: s_sessionValue
			}
		};

		return o_result;
	},

	updateCurrentPosition: function(o_position) {

		var i_self = this;
		var i_statusbar = i_self.getStatusBar();

		if (i_statusbar) {
			var i_position = i_statusbar.getComponent("icecap-position");

			if (i_position) {

				var a_result = [
					o_position.lin,
					o_position.col
				];

				i_position.setText(a_result.join("/"));
			}
		}
	},

	updateTypeMode: function (n_type) {

		var i_self = this;

		var i_statusbar = i_self.getStatusBar();

		if (i_statusbar) {

			var i_typeLabel = i_statusbar.getComponent("icecap-typemode");

			var a_mode = [
			];

			if (i_typeLabel) {

				switch (n_type) {
					case IceCap.util.Handler.typeMode.OVERWRITE:
						a_mode.push(" DEL");
						break;

					default:
						a_mode.push(" INS");
						break;
				}

				i_typeLabel.setText(a_mode.join(" "));
			}
		}
	}
});
Ext.define("IceCap.view.Window", {
	extend: "Ext.window.Window",

	layout: "fit",

	requires: [
		"IceCap.controller.Window"
	],

	config: {
		inputData: null,
		lastTabIndex: 1,
		currentTabIndex: 1,
		content: null,
		keyArea: null,
		working: false,
		needFocus: 1,
		currentPosition: {
			col: 1,
			lin: 1
		},
		statusBar: null,
		main: null,
		sessionValue: "",
		cursor: null,
		timer: null,
		theme: "",
		requestCallBack: Ext.emptyFn,
		responseCallBack: Ext.emptyFn,
		enterOnDblClick: false,
		marker: null,
		markerActive: false,
		options: {},
		typeMode: 1
	},

	closable: false,
	modal: false,

	controller: "icecap.view.window",

	listeners: {
		afterrender: {
			fn: function(i_panel) {

				var i_self = i_panel;

				i_self.addKeyEvents();

				var i_content = i_self.getContent();

				if (i_content) {

					var o_inputData = i_self.getInputData();

					var a_fields = i_content.query("field[focusable]");
					var b_found = false;
					var b_error = false;

					Ext.Array.each(a_fields, function(i_field) {
						if (i_field.getTabIndex() === i_self.getNeedFocus()) {
							if (!b_error) {
								setTimeout(function() {
									i_field.focus(false, 10);
								}, 10);
							}
							b_found = true;

							return false;
						}
					});

					if (!b_found) {

						i_self.setCurrentPosition({
							col: 2,
							lin: 1
						});

						if (!b_error) {
							setTimeout(function() {
								i_self.focus(true, 10);
							}, 10);
						}
					}
				}
			},
			buffer: 100
		},
		dblclick: {
			fn: "onDblClick",
			element: "body"
		},
		click: {
			fn: "onClick",
			element: "body"
		},
		wheel: {
			fn: "onWheel",
			element: "body"
		},
		fieldoption: {
			fn: function(a_config) {

				var i_self = this;
				var i_controller = i_self.getController();

				var o_data = {};

				Ext.apply(o_data, {
					key: "ENTER"
				});

				Ext.Array.each(a_config, function(o_config) {

					var s_field = o_config.field;

					o_data[s_field] = o_config.option;

					Ext.apply(o_data, {
						csrCol: o_config.col,
						csrLin: o_config.lin
					});
				});

				i_self.setCurrentPosition({
					col: o_data.csrCol,
					lin: o_data.csrLin
				});

				i_controller.onSendData({
					func: ""
				}, o_data);
			}
		},
		key: {
			fn: function(s_value) {

				var i_self = this;
				var i_controller = i_self.getController();

				i_controller.doKeyEvent(s_value);
			}
		}

	},

	onKeyDown: Ext.emptyFn, // THIS IS NEEDED TO OVERRIDE Ext.util.Floating

	initComponent: function() {

		var i_self = this;

		var o_params = Ext.Object.fromQueryString(window.location.search);

		Ext.Object.each(o_params, function(s_key, value) {
			o_params[s_key] = Ext.decode(value, true) || value;
		});

		var o_options = Ext.apply(i_self.options, o_params.options || {});

		Ext.apply(i_self, {
			theme: i_self.theme || o_params.theme || ""
		});

		i_self.callParent(arguments);

		i_self.setTypeMode(IceCap.util.Handler.typeMode.INSERT);

		i_self.add(i_self.createLayout());

		i_self.enableBubble([
			"onAddWebContainer",
			"toWindow",
			"applicationClose"
		]);

		i_self.recreate();
	},

	createStatusBar: function() {

		var i_statusbar = Ext.widget("toolbar", {
			dock: "bottom",
			cls: "icecap-statusbar",
			height: 30,
			hidden: false,
			items: [
				{
					xtype: "label",
					itemId: "icecap-message"
				},
				"->",
				{
					xtype: "label",
					itemId: "icecap-position"
				},
				{
					xtype: "label",
					text: " INS",
					itemId: "icecap-typemode"
				}
			],
			setText: function (s_text) {

				var i_toolbar = this;

				var i_message = i_toolbar.getComponent("icecap-message");

				if (i_message) {
					i_message.setText(s_text);
				}
			}
		});

		return i_statusbar;
	},

	createLayout: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();

		return i_self.createContent();
	},

	createContent: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();
		var n_tabIndex = o_inputData.input.length > 0 ? 0 : 1;
		var b_focusable = o_inputData.input.length > 0 ? false : true;

		var i_panel = Ext.widget("panel", {
			layout: {
				type: "vbox",
				align: "stretch"
			},
			border: false,
			focusable: b_focusable,
			tabIndex: n_tabIndex,
			items: i_self.createItems()
		});

		i_self.setMain(i_panel);

		return i_panel;
	},

	createItems: function() {

		var i_self = this;

		var o_inputData = i_self.getInputData();

		var a_items = [];

		var i_center = Ext.widget("form", {
			bodyPadding: 5,
			border: false,
			layout: "absolute",
			flex: 1,
			ui: ["icecap", i_self.getTheme()].filter(Boolean).join("-")
		});

		a_items.push(i_center);

		var i_south = Ext.widget("panel", {
			layout: {
				type: "column"
			},
			border: false,
			bodyPadding: 5,
			defaults: {
				margin: "0 5 5 0"
			},
			ui: ["icecap", i_self.getTheme()].filter(Boolean).join("-")
		});

		a_items.push(i_south);

		i_self.setContent(i_center);
		i_self.setKeyArea(i_south);

		var i_statusbar = i_self.createStatusBar();

		i_self.addDocked(i_statusbar);

		i_self.setStatusBar(i_statusbar);

		i_self.updateMessage(o_inputData);

		return a_items;
	},

	createFields: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();

		var a_fields = [];

		var n_tabIndex = 1;
		var n_focus = o_inputData.focus;

		var n_inputWidth = o_inputData.width || 80;

		var n_inputHeight = o_inputData.height || 24;

		var b_fixed = IceCap.util.Handler.getFixed();

		var a_sliced = Ext.Array.from(o_inputData.input || []);

		a_sliced = a_sliced.concat(Ext.Array.from(o_inputData.fields || []));

		Ext.Array.each(a_sliced, function(o_config, n_index) {

			var n_default = IceCap.util.Handler.getDefaultWidth();
			var n_width = n_default;

			var o_meta = o_config.metaData || {};

			var o_field = {
				inputType: "text",
				xtype: "textfield"
			};

			if (o_config.len > 1) {
				n_width = ((o_config.len - 1) * IceCap.util.Handler.getColSize()) + n_default;
			}

			Ext.apply(o_field, i_self.applyFFW(o_config));

			var s_type = o_field.xtype || "textfield";
			var n_x = IceCap.util.Handler.getX(o_config.col) + (IceCap.util.Handler.getColSize() / 8);
			var n_y = IceCap.util.Handler.getY(o_config.lin) + 3;
			var b_password = o_config.atr == "27";

			if (o_field.readOnly) {

				o_field.xtype = s_type = "displayfield";

				Ext.apply(o_field.fieldStyle, {
					"font": "normal " + IceCap.util.Handler.getTextSize() + "px / " + IceCap.util.Handler.getColSize() + "px Lucida Console",
					"white-space": "pre",
					"margin-top": IceCap.util.Handler.getMargin() + "px !important",
					"min-height": "0px !important"
				});

				Ext.apply(o_field, {
					style: {
						padding: "1px 0px !important"
					}
				});

				Ext.apply(o_field, {
					fieldCls: ["x-form-display-field iceCap-output-" + (o_config.atr || 20), i_self.getTheme()].filter(Boolean).join("-")
				});
			}

			if (o_config.len > n_inputWidth) {
				var n_temp = n_inputWidth - o_config.col;
				n_width = ((n_temp - 1) * IceCap.util.Handler.getColSize()) + n_default;
			}

			if (o_config.hasOwnProperty("colsize") && o_config.hasOwnProperty("rows")) {

				var s_data = o_config.dta || "";

				o_field.xtype = s_type = "textarea";
				o_field.height = IceCap.util.Handler.getTextSize() * o_config.rows;
				o_field.preventScrollbars = true;

				var a_split = s_data.split("\n");

				var s_result = "";

				var n_split = a_split.length;

				Ext.Array.each(a_split, function (s_part, n_index) {

					var regex = new RegExp(".{" + o_config.colsize + "}", "g");

					var a_match = s_part.match(regex);

					if (a_match) {
						s_result += s_part;
					}
					else {
						if (n_index != n_split - 1) {
							s_result += s_part + "\n";
						}
						else {
							s_result += s_part;
						}
					}
				});

				o_config.dta = s_result;


				o_field.getSubmitValue = function () {

					var i_field = this;

					var s_value = i_field.stripReturns(i_field.getRawValue());

					var regex = new RegExp(".{1," + o_config.colsize + "}", "g");

					var a_split = s_value.match(regex) || [];

					return a_split.join("\n");
				};

			}

			var o_listeners = Ext.apply({}, o_meta.listeners);
			delete o_meta.listeners;

			var a_field = ["icecap-field", i_self.getTheme()];

			var a_pass = [b_password ? "iceCap-input-password" : "iceCap-input-" + (o_config.atr || 20)];

			a_pass.push(i_self.getTheme());

			var a_cls = [a_field.filter(Boolean).join("-"), a_pass.filter(Boolean).join("-")];

			var s_cls = a_cls.join(" ");

			if (isNaN(n_x)) {
				n_x = IceCap.util.Handler.getColSize();
			}

			if (isNaN(n_y)) {
				n_y = IceCap.util.Handler.getLineHeight();
			}

			Ext.Object.merge(o_field, {
				width: n_width,
				enforceMaxLength: true,
				maxLength: o_config.len,
				minWidth: IceCap.util.Handler.getDefaultWidth(),
				tabIndex: !o_field.readOnly ? n_tabIndex : -1,
				focusable: !o_field.readOnly,
				lin: o_config.lin,
				col: o_config.col,
				emuId: o_config.id,
				name: o_config.id,
				itemId: o_config.id,
				value: o_config.dta || "",
				inputAttrTpl: "spellcheck=false",
				x: n_x,
				y: n_y,
				cls: s_cls,
				enableKeyEvents: true,
				selectOnFocus: false,
				listeners: {
					afterrender: {
						fn: function(i_field) {

							if (i_field.inputEl && !i_field.metaField) {

								i_field.inputEl.setMinHeight(0);

								if (i_field.xtype == "textfield") {
									i_field.inputEl.setHeight(IceCap.util.Handler.getDefaultHeight());
								}
								if (i_field.xtype == "textarea") {
									i_field.inputEl.dom.setAttribute("rows", o_config.rows);
								}

								i_field.inputEl.set({
									autocomplete: "off"
								});

								if (i_field.getTabIndex() === i_self.getNeedFocus()) {

									i_field.focus(false, 10, function () {
										var o_inputData = i_self.getInputData();

										var s_value = i_field.getValue();
										var n_col = i_field.col;
										var n_diff = 0;

										if (n_col < o_inputData.csrCol) {
											n_diff = o_inputData.csrCol - n_col;
											i_field.inputEl.el.dom.selectionEnd = i_field.inputEl.el.dom.selectionStart = i_field.inputEl.el.dom.selectionStart + n_diff;
										}

										var o_position = {
											col: o_inputData.csrCol,
											lin: o_inputData.csrLin
										}

										i_self.setCurrentPosition(o_position);

										var i_cursor = i_self.getCursor();
										i_cursor.moveCursor(o_position);
										i_cursor.hide();
									});
								}
							}
						}
					},
					change: {
						fn: function(i_field, value) {

							var s_direction = i_field.inputEl.el.dom.selectionDirection;

							var n_curpos = s_direction == "forward" ? i_field.inputEl.el.dom.selectionEnd : i_field.inputEl.el.dom.selectionStart;

							if (value.length == i_field.maxLength && n_curpos == i_field.maxLength) {
								i_self.doNavigationKey(1, true);
							}
						}
					},
					focus: {
						fn: function(i_field) {

							var n_tabIndex = i_field.getTabIndex();
							i_self.setCurrentTabIndex(n_tabIndex);

							var o_position = {
								col: i_field.col,
								lin: i_field.lin
							};
							i_self.setCurrentPosition(o_position);

							var i_cursor = i_self.getCursor();
							i_cursor.moveCursor(o_position);
							i_cursor.hide();
						}
					},
					keyup: {
						fn: function(i_field, i_event) {

							if (i_field.xtype === "textfield") {
								var s_direction = i_field.inputEl.el.dom.selectionDirection;

								var n_start = s_direction == "forward" ? i_field.inputEl.el.dom.selectionEnd : i_field.inputEl.el.dom.selectionStart;
								var n_add = i_field.value.length * IceCap.util.Handler.getColSize();

								if (n_start != i_field.value.length) {
									n_add = n_start * IceCap.util.Handler.getColSize();
								}

								var o_input = Ext.apply({
									x: i_field.getLocalX() + n_add,
									y: i_field.getLocalY()
								});

								var o_position = IceCap.util.Handler.toColAndLine(o_input);
								i_self.setCurrentPosition(o_position);

								var i_cursor = i_self.getCursor();
								i_cursor.moveCursor(o_position);
							}
						}
					},
					keydown: {
						fn: function(i_field, i_event) {

							var i_regext = new RegExp("^\\w{1}$");

							var value = i_field.getValue();

							if (i_event.ctrlKey) {

								if (i_event.keyCode == Ext.event.Event.DOWN ||
									i_event.keyCode == Ext.event.Event.UP) {

									i_event.stopEvent();

									var n_y = IceCap.util.Handler.getLineHeight();

									if (i_event.keyCode == Ext.event.Event.UP) {
										n_y = -IceCap.util.Handler.getLineHeight();
									}

									var o_input = Ext.apply({
										x: i_field.getLocalX(),
										y: i_field.getLocalY() + n_y
									});

									var o_position = IceCap.util.Handler.toColAndLine(o_input);
									i_self.setCurrentPosition(o_position);

									var i_cursor = i_self.getCursor();
									i_cursor.moveCursor(o_position);

									var a_inputs = i_self.query("field[focusable]");

									var o_box = Ext.apply({
										width: IceCap.util.Handler.getColSize(),
										height: 1
									}, o_input);

									var b_intersect = false;

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

										var o_bound = i_field.el.dom.getBoundingClientRect();

										var o_rect = Ext.apply({}, {
											x: i_field.getLocalX(),
											y: i_field.getLocalY(),
											width: o_bound.width,
											height: o_bound.height
										});

										if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
											b_intersect = true;
											i_field.focus(false, 10);
											return false;
										}
									});

									if (!b_intersect) {

										i_field.blur();
										i_cursor.show();

										i_self.focus(true, 10);
									}
								}
								if (i_event.keyCode == Ext.event.Event.LEFT ||
									i_event.keyCode == Ext.event.Event.RIGHT) {

									var i_inputEl = i_field.inputEl;
									var n_selection = i_event.keyCode == Ext.event.Event.LEFT ? i_inputEl.dom.selectionStart : i_inputEl.dom.selectionEnd;

									if (n_selection == 0 && i_event.keyCode == Ext.event.Event.LEFT) {

										i_event.stopEvent();

										var n_x = IceCap.util.Handler.getLineHeight();

										var o_input = Ext.apply({
											x: i_field.getLocalX() - (n_x / 2),
											y: i_field.getLocalY()
										});

										var o_position = IceCap.util.Handler.toColAndLine(o_input);
										i_self.setCurrentPosition(o_position);

										var i_cursor = i_self.getCursor();
										i_cursor.moveCursor(o_position);

										var a_inputs = i_self.query("field[focusable]");

										var o_box = Ext.apply({
											width: IceCap.util.Handler.getColSize(),
											height: 1
										}, o_input);

										var b_intersect = false;

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

											var o_bound = i_field.el.dom.getBoundingClientRect();

											var o_rect = Ext.apply({}, {
												x: i_field.getLocalX(),
												y: i_field.getLocalY(),
												width: o_bound.width,
												height: o_bound.height
											});

											if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
												b_intersect = true;
												i_field.focus(false, 10);
												return false;
											}
										});

										if (!b_intersect) {

											i_field.blur();
											i_cursor.show();

											i_self.focus(true, 10);
										}
									}
									else if (n_selection == value.length && i_event.keyCode == Ext.event.Event.RIGHT) {

										i_event.stopEvent();

										var n_x = IceCap.util.Handler.getColSize();

										n_x = n_x * (i_field.maxLength + 2);

										var o_input = Ext.apply({
											x: i_field.getLocalX() + n_x,
											y: i_field.getLocalY()
										});

										var o_position = IceCap.util.Handler.toColAndLine(o_input);
										i_self.setCurrentPosition(o_position);

										var i_cursor = i_self.getCursor();
										i_cursor.moveCursor(o_position);

										var a_inputs = i_self.query("field[focusable]");

										var o_box = Ext.apply({
											width: IceCap.util.Handler.getColSize(),
											height: 1
										}, o_input);

										var b_intersect = false;

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

											var o_bound = i_field.el.dom.getBoundingClientRect();

											var o_rect = Ext.apply({}, {
												x: i_field.getLocalX(),
												y: i_field.getLocalY(),
												width: o_bound.width,
												height: o_bound.height
											});

											if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
												b_intersect = true;
												i_field.focus(false, 10);
												return false;
											}
										});

										if (!b_intersect) {

											i_field.blur();
											i_cursor.show();

											i_self.focus(true, 10);
										}
									}
								}
							}
							else {

								if (i_field.xtype == "textarea") {

									if (i_event.keyCode == Ext.event.Event.UP) {

										var s_value = i_field.stripReturns(i_field.getRawValue());

										var regex = new RegExp("^.*\n$|.{1," + o_config.colsize + "}", "gm");

										var a_lines = s_value.match(regex) || [];
										var a_newLines = s_value.split("\n");

										var n_start = i_field.inputEl.dom.selectionStart;

										var n_row = 1;
										var n_char = 0;
										var b_last = i_field.inputEl.dom.selectionStart == i_field.inputEl.dom.value.length;

										Ext.Array.each(a_lines, function (s_part, n_index) {

											n_char += s_part.length;

											if (n_char <= n_start) {
												n_row++;
											}
											else {
												return false;
											}
										});

										if (n_row > 1) {

											i_event.stopEvent();

											n_char = 0;
											var n_left = 0;
											var n_current = 0;
											var n_nextLine = 0;

											n_start = n_start - (a_newLines.length - 1);

											Ext.Array.each(a_lines, function (s_part, n_index) {

												n_char += s_part.length;

												var n_count = 0;

												n_left = 0;

												while (n_count < s_part.length) {
													n_count++;
													if (n_current < n_start) {
														n_current++;
														n_left++;
													}
												}

												if (n_char < n_start) {
													n_row++;
												}
												else {
													n_nextLine = n_index - 1;
													return false;
												}
											});

											if (n_nextLine == -1) {
												n_nextLine = 0;
											}

											var n_newPos = 0;

											Ext.Array.each(a_lines, function (s_part, n_index) {

												if (n_index < n_nextLine) {
													n_newPos += s_part.length;
												}
												else {
													n_newPos += n_left;
													return false;
												}
											});

											i_event.stopEvent();
											i_field.inputEl.dom.setSelectionRange(n_newPos, n_newPos);
										}
									}

									if (i_event.keyCode == Ext.event.Event.DOWN) {

										var s_value = i_field.stripReturns(i_field.getRawValue());
										var n_length = s_value.length;

										var regex = new RegExp("^.*\n$|.{1," + o_config.colsize + "}", "g");
										var n_pos = i_field.inputEl.dom.selectionStart;

										var a_lines = s_value.match(regex) || [];
										var a_newLines = s_value.split("\n");

										if (a_lines.length < o_config.rows && a_newLines.length < o_config.rows) {

											i_event.stopEvent();

											s_value = s_value + "\n";

											i_field.setValue(s_value);

											i_field.inputEl.dom.setSelectionRange(s_value.length, s_value.length);
										}
										else {
											var n_row = 1;
											var n_char = 0;
											var n_left = 0;
											var n_current = 0;
											var n_nextLine = 0;

											Ext.Array.each(a_lines, function (s_part, n_index) {

												n_char += s_part.length;

												var n_count = 0;

												n_left = 0;

												while (n_count < s_part.length) {
													n_count++;
													if (n_current < n_pos) {
														n_current++;
														n_left++;
													}
												}

												if (n_char <= n_pos) {
													n_row++;
												}
												else {
													n_nextLine = n_index + 1;
													return false;
												}
											});

											if (n_nextLine < a_lines.length || n_nextLine < a_newLines.length) {

												var n_newPos = 0;

												Ext.Array.each(a_lines, function (s_part, n_index) {

													if (n_index < n_nextLine) {
														n_newPos += s_part.length;
													}
													else {
														n_newPos += n_left + (a_newLines.length - 1);
														return false;
													}
												});

												if (n_row < o_config.rows) {
													i_event.stopEvent();
													i_field.inputEl.dom.setSelectionRange(n_newPos, n_newPos);
												}
											}
										}

									}
								}
								else if (i_field.xtype == "textfield") {

									var n_typeMode = i_self.getTypeMode();

									if (n_typeMode == IceCap.util.Handler.typeMode.OVERWRITE) {

										var s_direction = i_field.inputEl.el.dom.selectionDirection;

										var n_curpos = s_direction == "forward" ? i_field.inputEl.el.dom.selectionEnd : i_field.inputEl.el.dom.selectionStart;

										var s_key = String.fromCharCode(i_event.keyCode);

										if (n_curpos < value.length && i_regext.test(s_key)) {

											var a_values = value.split("");
											a_values[n_curpos] = i_event.browserEvent.key;
											value = a_values.join("");
											i_field.setValue(value);
											i_field.inputEl.el.dom.selectionEnd = i_field.inputEl.el.dom.selectionStart = n_curpos + 1;
											i_event.stopEvent();
										}
									}
								}
							}
						}
					}
				}
			}, o_meta);

			var o_clone = Ext.clone(o_field);

			if (o_meta.xtype) {

				o_clone.metaField = true;

				o_clone.xtype = s_type = o_meta.xtype;

				delete o_clone.cls;
				delete o_clone.fieldStyle;
				delete o_clone.inputType;
			}

			if (o_config.type == "grid") {

				Ext.apply(o_clone, {
					xtype: s_type = "iceCapGrid",
					gridConfig: o_config.grid,
					width: IceCap.util.Handler.getColSize() * n_inputWidth
				});

				delete o_clone.cls;
				delete o_clone.fieldStyle;
				delete o_clone.inputType;
				delete o_clone.listeners;
				delete o_clone.grid;

				Ext.apply(o_clone, {
					listeners: {
						fieldoption: {
							fn: function(a_config) {
								i_self.fireEvent("fieldoption", a_config);
							}
						},
						key: {
							fn: function(s_key) {
								i_self.fireEvent("key", s_key);
							}
						}
					}
				});
			}

			var i_field = Ext.widget(s_type, o_clone);

			var o_on = {};

			Ext.Object.each(o_listeners, function(s_key, value) {

				if (Ext.isFunction(value)) {
					o_on[s_key] = Ext.apply({}, {
						fn: value,
						scope: i_self
					});
				}
				else if (Ext.isObject(value)) {
					value.scope = i_self;
					o_on[s_key] = value;
				}
			});

			i_field.on(o_on);

			if (!i_field.readOnly) {
				n_tabIndex++;
			}

			if (o_config.atr == 25) {

				if (o_field.readOnly) {
					i_field.addCls("icecap-invalid");
				}
				else {
					i_field.markInvalid(true);
				}
			}

			a_fields.push(i_field);
		});

		n_tabIndex = n_tabIndex - 1;
		i_self.setLastTabIndex(n_tabIndex);
		i_self.setCurrentTabIndex(o_inputData.focus);

		if (n_focus) {
			i_self.setNeedFocus(n_focus);
		}

		return a_fields;
	},

	createLabels: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();

		var a_fields = [];

		var b_fixed = IceCap.util.Handler.getFixed();

		Ext.Array.each(o_inputData.output, function(o_config) {

			var s_value = o_config.dta;

			if (o_config.len) {
				if (o_config.ta == "right" && !o_config.off) {
					//o_config.col += o_config.len - o_config.dta.length;
				}

				if (o_config.off) {
					s_value = Ext.String.leftPad(s_value, s_value.length + o_config.off);
				}

				var n_x = Math.ceil((IceCap.util.Handler.getX(o_config.col) + (IceCap.util.Handler.getColSize() / 2)));
				var n_y = IceCap.util.Handler.getY(o_config.lin);

				if (isNaN(n_x)) {
					n_x = IceCap.util.Handler.getColSize();
				}

				if (isNaN(n_y)) {
					n_y = IceCap.util.Handler.getLineHeight();
				}

				var o_field = {
					value: s_value,
					htmlEncode: true,
					x: n_x,
					y: n_y,
					cls: ["icecap-field", i_self.getTheme()].filter(Boolean).join("-")
				};

				var o_style = {
					"font": "normal " + IceCap.util.Handler.getTextSize() + "px Segoe UI,Segoe,DejaVu Sans,Trebuchet MS,Verdana,sans-serif",
					"margin-top": "7px !important",
					"min-height": "0px !important"
				};

				if (b_fixed) {

					Ext.apply(o_field, {
						fieldCls: ["x-form-display-field iceCap-output-" + (o_config.atr || 20), i_self.getTheme()].filter(Boolean).join("-")
					});

					Ext.apply(o_style, {
						"font": "normal " + IceCap.util.Handler.getTextSize() + "px / " + IceCap.util.Handler.getColSize() + "px Lucida Console",
						"white-space": "pre",
						"margin-top": "7px !important",
						"min-height": "0px !important"
					});
				}

				Ext.apply(o_field, {
					fieldStyle: o_style
				});

				if (o_config.atr == "27") {
					o_field.hidden = true;
				}

				var i_field = Ext.widget("displayfield", o_field);

				if (o_config.atr == 23) {

					//i_field.addCls("icecap-invalid");
				}

				a_fields.push(i_field);
			}
		});

		return a_fields;
	},

	createKeys: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();
		var a_disabledKey = o_inputData.disabledKeys || [];
		var a_buttons = [];

		var i_template = Ext.create("Ext.XTemplate",
			"{id}={text}"
		);

		Ext.Array.each(o_inputData.keys, function(o_config) {

			var s_template = i_template.apply(o_config);

			var o_found = Ext.Array.findBy(a_disabledKey, function(o_disabled) {
				return o_config.id == o_disabled.id;
			});

			o_found = o_found || {
				showKey: 1
			};

			var b_hidden = Number(o_found.showKey) < 1  || false;

			var i_button = Ext.widget("button", {
				text: s_template,
				key: o_config.id,
				focusable: false,
				cls: "icecap-key",
				hidden: b_hidden,
				handler: function(i_button) {
					i_self.doKeyEvent(i_button.key);
				}
			});

			a_buttons.push(i_button);
		});

		return a_buttons;
	},

	createMetaFields: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();

		var a_fields = [];
		var n_tabIndex = i_self.getLastTabIndex() || 1;

		var a_sliced = Ext.Array.slice(o_inputData.metaFields || []);

		var n_inputHeight = o_inputData.height || 24;

		var n_inputWidth = o_inputData.width || 80;

		Ext.Array.each(a_sliced, function(o_config) {

			var n_x = o_config.x;
			var n_y = o_config.y;

			if (!o_config.x) {
				n_x = Math.ceil((IceCap.util.Handler.getX(o_config.col) + (IceCap.util.Handler.getColSize() / 2)));
			}

			if (!o_config.y) {
				n_y = IceCap.util.Handler.getY(o_config.lin);
			}

			if (isNaN(n_x)) {
				n_x = IceCap.util.Handler.getColSize();
			}

			if (isNaN(n_y)) {
				n_y = IceCap.util.Handler.getLineHeight();
			}

			var o_listeners = o_config.listeners || {};

			Ext.apply(o_config, {
				tabIndex: n_tabIndex,
				keys: Ext.Array.slice(o_inputData.keys),
				theme: i_self.getTheme(),
				x: n_x,
				y: n_y,
				listeners: {
					fieldoption: {
						fn: function(a_config) {
							i_self.fireEvent("fieldoption", a_config);
						}
					},
					key: {
						fn: function(o_config) {
							i_self.fireEvent("key", o_config);
						}
					}
				}
			});

			n_tabIndex++;

			o_config.xtype = o_config.xtype || o_config.type || "textfield";

			o_config.name = o_config.id;

			delete o_config.id;

			var i_field = Ext.widget(o_config.xtype, o_config);

			var o_on = {};

			Ext.Object.each(o_listeners, function(s_key, value) {

				if (Ext.isFunction(value)) {
					o_on[s_key] = Ext.apply({}, {
						fn: value,
						scope: i_self
					});
				}
				else if (Ext.isObject(value)) {
					value.scope = i_self;
					o_on[s_key] = value;
				}
			});

			i_field.on(o_on);

			a_fields.push(i_field);
		});

		i_self.setLastTabIndex(n_tabIndex);

		return a_fields;
	},

	doCreateFromData: function(o_inputData) {

		var i_self = this;
		i_self.fireEvent("toWindow", i_self, o_inputData);
	},

	recreate: function() {

		var i_self = this;

		var i_content = i_self.getContent();
		var i_keyArea = i_self.getKeyArea();
		var i_main = i_self.getMain();
		var o_inputData = i_self.getInputData();

		var b_fixed = o_inputData.hasOwnProperty("fixed") ? o_inputData.fixed : true;

		IceCap.util.Handler.setFixed(b_fixed);

		i_self.suspendLayouts();

		if (o_inputData.title) {
			i_self.setTitle(o_inputData.title);
		}

		var a_created = i_self.createFields();
		a_created = a_created.concat(i_self.createLabels());
		a_created = a_created.concat(i_self.createMetaFields());
		a_created.push(i_self.createCursor());
		a_created.push(i_self.createMarker());

		i_content.removeAll(true);
		i_keyArea.removeAll(true);

		i_content.add(a_created);

		var a_keys = i_self.createKeys();
		i_keyArea.add(a_keys);

		i_keyArea[a_keys.length === 0 ? "hide" : "show"]();

		var a_fields = i_self.query("field[focusable]");
		var a_grids = i_content.query("gridpanel");

		var b_found = false;
		var b_error = false;

		if (o_inputData.msgBox && o_inputData.msgBox.text) {
			b_error = true;
			i_self.onError(o_inputData);
		}

		if (a_fields.length > 0) {

			b_found = Ext.Array.findBy(a_fields, function (i_field) {
				return i_field.getTabIndex() === i_self.getNeedFocus();
			});

			if (!b_found) {
				b_found = true;
				var i_field = a_fields[0];
				if (!b_error) {
					setTimeout(function () {
						i_field.focus(false, 10);
					}, 10);
				}
			}
		}

		if (!b_found) {

			i_self.setCurrentPosition({
				col: 2,
				lin: 1
			});

			if (!b_error) {

				if (a_grids.length == 0) {
					setTimeout(function() {
						i_self.focus(true, 10);
					}, 10);
				}
			}
		}

		i_self.updateMessage(o_inputData);

		if (!b_found && !b_error) {
			setTimeout(function() {
				i_main.focus(true, 10);
			}, 10);
		}

		var i_marker = i_self.getMarker();
		i_marker.setInUse(i_self.getMarkerActive());

		IceCap.util.Handler.setFixed(true);

		i_self.resumeLayouts(true);
	},

	getCurrentField: function() {

		var i_self = this;

		var a_fields = i_self.query("field[focusable]");
		var n_tabIndex = i_self.getCurrentTabIndex();
		var i_controller = i_self.getController();

		var i_content = i_self.getContent();
		var i_currentField;

		Ext.Array.each(a_fields, function(i_field) {
			if (i_field.getTabIndex() === n_tabIndex && i_field.hasFocus) {
				i_currentField = i_field;
				return false;
			}
		});

		return i_currentField;
	},

	doKeyEvent: function(s_value, i_event) {

		if (i_event) {
			i_event.stopEvent();
		}

		var i_self = this;

		var i_controller = i_self.getController();

		var o_inputData = i_self.getInputData() || {};

		var a_disabledKeys = Ext.Array.from(o_inputData.disabledKeys || []);

		var o_found = Ext.Array.findBy(a_disabledKeys, function (o_key) {
			return s_value == o_key.id;
		});

		if (!o_found) {
			i_controller.doKeyEvent(s_value);
		}
		else {

			Ext.Msg.show({
				title: "Error",
				msg: "Funktionstasten er ikke tilladt",
				icon: Ext.MessageBox.INFO,
				buttons: Ext.MessageBox.OK,
				fn: function () {
					i_self.findFocus();
				}
			});
		}
	},

	doNavigationKey: function(n_direction, b_tab, i_event) {

		if (i_event) {
			i_event.stopPropagation();
			i_event.preventDefault();
		}

		var i_self = this;
		var i_controller = i_self.getController();
		i_controller[b_tab ? "doTabKey" : "doArrowKeys"](n_direction);
	},

	addKeyEvents: function() {

		var i_self = this;

		var a_bindings = [
			{
				key: Ext.event.Event.PAGE_DOWN,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doKeyEvent("ROLLUP", i_event);
				}
			},
			{
				key: Ext.event.Event.PAGE_UP,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doKeyEvent("ROLLDOWN", i_event);
				}
			},
			{
				key: Ext.event.Event.ENTER,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doKeyEvent("ENTER", i_event);
				}
			},
			{
				key: Ext.event.Event.ESC,
				fn: function(n_key, i_event) {

					i_event.preventDefault();

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doKeyEvent("ATTN", i_event);
				}
			},
			{
				key: Ext.event.Event.UP,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;

					if (!i_event.target.type) {

						i_event.stopEvent();

						var i_cursor = i_self.getCursor();
						var o_position = i_self.getCurrentPosition();

						var a_inputs = i_self.query("field[focusable]");

						var n_lin = o_position.lin;

						if (n_lin < 1) {
							n_lin = 1;
						}

						var o_box = Ext.apply({
							x: IceCap.util.Handler.getX(o_position.col),
							y: IceCap.util.Handler.getY(n_lin),
							width: Math.floor(IceCap.util.Handler.getColSize()),
							height: 1
						});

						var b_intersect = false;

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

							var o_bound = i_field.el.dom.getBoundingClientRect();

							var o_rect = Ext.apply({}, {
								x: i_field.getLocalX(),
								y: i_field.getLocalY(),
								width: o_bound.width,
								height: o_bound.height
							});

							if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
								b_intersect = true;
								i_field.focus(false, 10);
								return false;
							}
						});

						if (!b_intersect) {

							o_position.lin--;

							if (o_position.lin < 1) {
								o_position.lin = 1;
							}

							i_cursor.moveCursor(o_position);
						}
						i_self.setCurrentPosition(o_position);
					}
					else {
						i_self.doNavigationKey(0, false, i_event);
					}
				}
			},
			{
				key: Ext.event.Event.DOWN,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;

					if (!i_event.target.type) {

						i_event.stopEvent();

						var i_cursor = i_self.getCursor();
						var o_position = i_self.getCurrentPosition();
						var o_inputData = i_self.getInputData();
						var o_window = o_inputData.window;

						var a_inputs = i_self.query("field[focusable]");

						var n_lin = o_position.lin + 2;

						if (n_lin > o_window.height) {
							n_lin = o_window.height;
						}

						var o_box = Ext.apply({
							x: IceCap.util.Handler.getX(o_position.col),
							y: IceCap.util.Handler.getY(n_lin),
							width: Math.floor(IceCap.util.Handler.getColSize()),
							height: 1
						});

						var b_intersect = false;

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

							var o_bound = i_field.el.dom.getBoundingClientRect();

							var o_rect = Ext.apply({}, {
								x: i_field.getLocalX(),
								y: i_field.getLocalY(),
								width: o_bound.width,
								height: o_bound.height
							});

							if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
								b_intersect = true;
								i_field.focus(false, 10);
								return false;
							}
						});

						if (!b_intersect) {

							o_position.lin++;

							if (o_position.lin > o_window.height) {
								o_position.lin = o_window.height;
							}

							i_cursor.moveCursor(o_position);
						}
						i_self.setCurrentPosition(o_position);
					}
					else {
						i_self.doNavigationKey(1, false, i_event);
					}
				}
			},
			{
				key: Ext.event.Event.LEFT,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;

					if (!i_event.target.type) {
						i_event.stopEvent();

						var i_cursor = i_self.getCursor();
						var o_position = i_self.getCurrentPosition();

						var a_inputs = i_self.query("field[focusable]");

						var n_col = o_position.col - 1;

						if (n_col < 1) {
							n_col = 1;
						}

						var o_box = Ext.apply({
							x: IceCap.util.Handler.getX(n_col),
							y: IceCap.util.Handler.getY(o_position.lin + 1),
							width: Math.floor(IceCap.util.Handler.getColSize()),
							height: 1
						});

						var b_intersect = false;

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

							var o_bound = i_field.el.dom.getBoundingClientRect();

							var o_rect = Ext.apply({}, {
								x: i_field.getLocalX(),
								y: i_field.getLocalY(),
								width: o_bound.width,
								height: o_bound.height
							});

							if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
								b_intersect = true;
								i_field.focus(false, 10);
								return false;
							}
						});

						if (!b_intersect) {

							o_position.col--;

							if (o_position.col < 1) {
								o_position.col = 1;
							}

							i_cursor.moveCursor(o_position);
						}

						i_self.setCurrentPosition(o_position);
					}
				}
			},
			{
				key: Ext.event.Event.RIGHT,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;

					if (!i_event.target.type) {

						i_event.stopEvent();

						var i_cursor = i_self.getCursor();
						var o_position = i_self.getCurrentPosition();
						var o_inputData = i_self.getInputData();
						var o_window = o_inputData.window;

						var a_inputs = i_self.query("field[focusable]");

						var n_col = o_position.col + 1;

						if (n_col > o_inputData.width) {
							n_col = o_inputData.width;
						}

						var o_box = Ext.apply({
							x: IceCap.util.Handler.getX(n_col),
							y: IceCap.util.Handler.getY(o_position.lin + 1),
							width: Math.floor(IceCap.util.Handler.getColSize()),
							height: 1
						});

						var b_intersect = false;

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

							var o_bound = i_field.el.dom.getBoundingClientRect();

							var o_rect = Ext.apply({}, {
								x: i_field.getLocalX(),
								y: i_field.getLocalY(),
								width: o_bound.width,
								height: o_bound.height
							});

							if (!Ext.draw.Draw.isBBoxIntersect(o_box, o_rect)) {
								b_intersect = true;
								i_field.focus(false, 10);
								return false;
							}
						});

						if (!b_intersect) {

							o_position.col++;

							if (o_position.col > o_inputData.width) {
								o_position.col = o_inputData.width;
							}

							i_cursor.moveCursor(o_position);
						}

						i_self.setCurrentPosition(o_position);
					}
				}
			},
			{
				key: Ext.event.Event.TAB,
				shift: false,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doNavigationKey(1, true, i_event);
				}
			},
			{
				key: Ext.event.Event.TAB,
				shift: true,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;
					i_self.doNavigationKey(0, true, i_event);
				}
			},
			{
				key: Ext.event.Event.NUM_PLUS,
				fn: function(n_key, i_event) {

					var i_key = this;
					var i_self = i_key.target.component;

					var i_field = i_self.getCurrentField();

					if (i_field) {

						var n_start = i_field.inputEl.el.dom.selectionStart;

						var s_text = i_field.getValue();

						s_text = s_text.substr(0, n_start);

						i_field.setValue(s_text);

						i_self.doNavigationKey(1, true, i_event);
					}
				}
			},
			{
				key: Ext.event.Event.HOME,
				ctrl: true,
				alt: false,
				fn: function(n_key, i_event) {

					i_event.stopEvent();

					var i_key = this;
					var i_self = i_key.target.component;

					var b_markerActive = i_self.getMarkerActive();
					var i_marker = i_self.getMarker();

					i_self.setMarkerActive(!b_markerActive);

					i_marker.toggle();
				}
			},
			{
				key: Ext.event.Event.INSERT,
				ctrl: false,
				alt: false,
				fn: function (n_key, i_event) {

					var n_type = i_self.getTypeMode();
					switch (n_type) {
						case IceCap.util.Handler.typeMode.OVERWRITE:
							n_type = IceCap.util.Handler.typeMode.INSERT;
							break;
						default:
							n_type = IceCap.util.Handler.typeMode.OVERWRITE;
							break;
					}

					i_self.setTypeMode(n_type);
				}
			}
		];

		a_bindings = a_bindings.concat(i_self.getFKeys());

		var i_keyMap = Ext.create("Ext.util.KeyMap", {
			target: i_self.getEl(),
			binding: a_bindings
		});
	},

	getFKeys: function() {

		var i_self = this;

		var a_keys = [
			{
				key: Ext.event.Event.F1,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F1", i_event);
				}
			},
			{
				key: Ext.event.Event.F2,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F2", i_event);
				}
			},
			{
				key: Ext.event.Event.F3,
				shift: false,
				alt: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F3", i_event);
				}
			},
			{
				key: Ext.event.Event.F4,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F4", i_event);
				}
			},
			{
				key: Ext.event.Event.F5,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F5", i_event);
				}
			},
			{
				key: Ext.event.Event.F6,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F6", i_event);
				}
			},
			{
				key: Ext.event.Event.F7,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F7", i_event);
				}
			},
			{
				key: Ext.event.Event.F8,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F8", i_event);
				}
			},
			{
				key: Ext.event.Event.F9,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F9", i_event);
				}
			},
			{
				key: Ext.event.Event.F10,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F10", i_event);
				}
			},
			{
				key: Ext.event.Event.F11,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F11", i_event);
				}
			},
			{
				key: Ext.event.Event.F12,
				shift: false,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F12", i_event);
				}
			}
		];

		a_keys = a_keys.concat(i_self.getFShiftKeys());

		return a_keys;
	},

	getFShiftKeys: function() {

		var i_self = this;

		var a_keys = [
			{
				key: Ext.event.Event.F1,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F13", i_event);
				}
			},
			{
				key: Ext.event.Event.F2,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F14", i_event);
				}
			},
			{
				key: Ext.event.Event.F3,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F15", i_event);
				}
			},
			{
				key: Ext.event.Event.F4,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F16", i_event);
				}
			},
			{
				key: Ext.event.Event.F5,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F17", i_event);
				}
			},
			{
				key: Ext.event.Event.F6,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F18", i_event);
				}
			},
			{
				key: Ext.event.Event.F7,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F19", i_event);
				}
			},
			{
				key: Ext.event.Event.F8,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F20", i_event);
				}
			},
			{
				key: Ext.event.Event.F9,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F21", i_event);
				}
			},
			{
				key: Ext.event.Event.F10,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F22", i_event);
				}
			},
			{
				key: Ext.event.Event.F11,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F23", i_event);
				}
			},
			{
				key: Ext.event.Event.F12,
				shift: true,
				fn: function(n_key, i_event) {

					i_self.doKeyEvent("F24", i_event);
				}
			}
		];

		return a_keys;
	},

	createCursor: function() {

		var i_self = this;
		var o_inputData = i_self.getInputData();

		var i_cursor = Ext.widget("component", {
			cls: ["iceCap-cursor", i_self.getTheme()].filter(Boolean).join("-"),
			autoEl: {
				tag: "div",
				style: {
					"border": "1px solid red;",
					"position": "absolute !important;",
					"width": (IceCap.util.Handler.getColSize() + 0.4) + "px !important;"
				}
			},
			listeners: {
				render: {
					fn: function(i_item) {
						i_item.hide();
					}
				},
				afterrender: {
					fn: function(i_cursor) {

						var i_content = i_self.getContent();

						var a_fields = i_content.query("field[focusable]");

						if (a_fields.length === 0) {

							i_cursor.show();

							i_cursor.moveCursor({
								col: 2,
								lin: 1
							});
						}
					}
				}
			},
			moveCursor: function(o_position) {

				var i_cursor = this;
				var i_marker = i_self.getMarker();

				i_cursor.setPosition({
					x: IceCap.util.Handler.getX(o_position.col - 1) + 4,
					y: IceCap.util.Handler.getY(o_position.lin + 1) - 2
				});

				var a_pos = [
					IceCap.util.Handler.getX(o_position.col - 1) - 1,
					IceCap.util.Handler.getY(o_position.lin + 1) - 2
				];

				i_self.updateCurrentPosition(o_position);
			}
		});

		i_self.setCursor(i_cursor);

		return i_cursor;
	},

	createMarker: function() {

		var i_self = this;

		var i_marker = Ext.widget("icecap-marker", {
			hidden: true,
			active: false,
			inUse: false,
			ui: i_self.getTheme() || "default"
		});

		i_self.setMarker(i_marker);

		return i_marker;
	},

	findFocus: function() {

		var i_self = this;
		var i_main = i_self.getMain();
		var i_content = i_self.getContent();

		var a_fields = i_content.query("field[focusable]");

		if (a_fields.length > 0) {

			var i_field = a_fields[0];
			i_field.focus(false, 10);
		}
		else {
			i_main.focus(false, 10);
		}
	},

	doTimer: function() {

		var i_self = this;

		var i_timer = i_self.getTimer();
		if (!i_timer) {
			i_timer = new Ext.util.DelayedTask(function() {
				i_self.setLoading(true);
			});

			i_self.setTimer(i_timer);

			i_timer.delay(1000);
		}
	},

	cancelTimer: function() {

		var i_self = this;

		var i_timer = i_self.getTimer();

		if (i_timer) {
			i_timer.cancel();
			i_self.setTimer(null);
		}
	},

	onError: function(o_inputData) {

		var i_self = this;

		Ext.Msg.show({
			title: "Server Message",
			msg: o_inputData.msgBox.text,
			icon: Ext.MessageBox.INFO,
			buttons: Ext.MessageBox.OK,
			fn: function(btn) {
				i_self.findFocus();
			}
		});
	},

	updateMessage: function(o_inputData) {

		var i_self = this;
		var i_statusbar = i_self.getStatusBar();

		var a_messages = Ext.Array.from(o_inputData.message);

		if (a_messages.length > 0) {

			var a_result = [];

			Ext.Array.each(a_messages, function (o_message) {
				a_result.push(o_message.text);
			});

			i_statusbar.setText(a_result.join(" "));
		}
	},

	applyFFW: function(o_config) {

		var i_self = this;
		var s_hex = o_config.ffw;
		var b_fixed = IceCap.util.Handler.getFixed();

		var o_field = {
			xtype: "textfield"
		};

		var o_fieldStyle = {
			font: "normal " + IceCap.util.Handler.getTextSize() + "px Segoe UI,Segoe,DejaVu Sans,Trebuchet MS,Verdana,sans-serif",
			padding: "1px 3px 2px 3px !important"
		};

		if (b_fixed) {

			Ext.apply(o_fieldStyle, {
				font: "normal " + IceCap.util.Handler.getTextSize() + "px / " + IceCap.util.Handler.getColSize() + "px Lucida Console"
			});
		}

		if (o_config.ta) {

			Ext.apply(o_fieldStyle, {
				"text-align": o_config.ta
			});
		}

		if (s_hex) {

			var n_hex = parseInt(s_hex);

			var n_sanHex = IceCap.util.Handler.createIndicators(s_hex);

			if (n_sanHex & IceCap.util.Handler.FFW.BYPASS) {

				Ext.apply(o_field, {
					readOnly: true
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.ALPHA_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[a-z A-Z ,.-]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.NUMERIC_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[+\\-,. 0-9]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.DIGITS_ONLY) {

				Ext.apply(o_field, {
					maskRe: new RegExp("[0-9]")
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MOD_DATA) {

				Ext.apply(o_field, {
					listeners: {
						afterrender: {
							fn: function(i_field) {

								i_field.originalValue = 0xFF;
							},
							single: true
						}
					}
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.AUTO_ENTER) {

				// THIS WE DONT NEED IN THE MODERN WORLD...

				// Ext.apply(o_field, {
				// 	listeners: {
				// 		blur: {
				// 			fn: function(i_field) {
				// 				i_self.doKeyEvent("ENTER");
				// 			}
				// 		}
				// 	}
				// });
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MONOCASE) {

				// Ext.apply(o_fieldStyle, {
				// 	"text-transform": "uppercase"
				// });
			}

			if (n_sanHex & IceCap.util.Handler.FFW.MAND_ENTER) {

				Ext.apply(o_field, {
					allowBlank: false
				});
			}

			if (n_sanHex & IceCap.util.Handler.FFW.RA_ZEROFILL ||
				n_sanHex & IceCap.util.Handler.FFW.RA_BLANKFILL ||
				n_sanHex & IceCap.util.Handler.FFW.RA_MANDATORY) {

				Ext.apply(o_fieldStyle, {
					"text-align": "right"
				});
			}

			if (o_config.atr == "27" && n_hex & (1 << 13)) {

				Ext.apply(o_field, {
					hidden: true,
					disabled: true
				});
			}
		}

		Ext.apply(o_field, {
			fieldStyle: o_fieldStyle
		});

		return o_field;
	},

	updateCurrentPosition: function (o_position) {

		var i_self = this;
		var i_statusbar = i_self.getStatusBar();

		if (i_statusbar) {
			var i_position = i_statusbar.getComponent("icecap-position");

			if (i_position) {

				var a_result = [
					o_position.lin,
					o_position.col
				];

				i_position.setText(a_result.join("/"));
			}
		}
	},

	updateTypeMode: function (n_type) {

		var i_self = this;

		var i_statusbar = i_self.getStatusBar();

		if (i_statusbar) {

			var i_typeLabel = i_statusbar.getComponent("icecap-typemode");

			var a_mode = [
			];

			if (i_typeLabel) {

				switch (n_type) {
					case IceCap.util.Handler.typeMode.OVERWRITE:
						a_mode.push(" DEL");
						break;

					default:
						a_mode.push(" INS");
						break;
				}

				i_typeLabel.setText(a_mode.join(" "));
			}
		}
	}
});
