Rtp = function() {
	return {
		silkIcon: function(icon) {
			return RTP_SHARED_URL + 'images/silk_icons/' + icon + '.png';
		},
		alert: function(msg, title, icon) {
			if (Ext && Ext.Msg) {
	  		Ext.Msg.alert(title || '', msg);
		  	Ext.Msg.setIcon(icon || Ext.Msg.INFO);
		  } else {
				msg = msg.replace(/<br>/g, '\n');
				if (title) msg = title + '\n\n' + msg;
				alert(msg);
			}
		},
		alertWarning: function(msg, title) {
			this.alert(msg, title, (Ext && Ext.Msg) ? Ext.Msg.WARNING : null);
		},
		request: function(options) {
			var options = options || {};
			options.requestOptions = {
				callback: options.callback,
				success: options.success,
				failure: options.failure,
				scope: options.scope
			};
			options.callback = this.requestCallback;
			delete options.success;
			delete options.failure;
			options.scope = this;
			var urlSep = options.url.match(/\?/) ? '&' : '?';
			options.url += urlSep + 'rtp_is_ajax=1';
			var transactionId = Ext.Ajax.request(options);
			return transactionId;
		},
		requestCallback: function(options, success, response) {
			var result = this.handleResponse(success, response);
			if (typeof result == 'boolean') result = {success: result};
			var args = [result, options, response];
			Ext.callback(options.requestOptions.callback, options.requestOptions.scope, args);
			Ext.callback(options.requestOptions[result.success !== false ? 'success' : 'failure'], options.requestOptions.scope, args);
		},	
		handleResponse: function(success, response, options) {
			options = options || {};
		  if (success == false) {
				this.alertWarning(t('Could not establish a connection to the server. Please check that you are connected to the internet.'));
		    return false;
		  }
			var result = null;
		  try {
			  result = Ext.util.JSON.decode(response.responseText);
		  } catch(e) {
		    if (options.silent !== true) {
					this.alertWarning(t('An error occurred while reading data from the server.')+(this.debugOn === true ? '<br><br>Response:<br>'+response.responseText : ''));
			  }
		    return false;
		  }
			if (result == null) {
				return false;
			}
		  if (result.success === false && result.errorMessage && options.silent !== true) {
		    this.alert(result.errorMessage, result.errorTitle || '');
		  }
		  return result;
		},
		//Diverse metoder, som evt. skal placeres andre steder
		treeIconCls: function(icon) {
			return '\" style="background-image: url('+icon+');';
		},
		treeExpandAndCollapse: function(tree, startNode) {
			if (!tree.rendered) {
				tree.on('afterrender', function() {
					this.treeExpandAndCollapse(tree, startNode);
				}, this);
			}
			tree.isExpandingAndCollapsing = true;
			var animate = tree.animate;
			tree.animate = false;
			this.treeExpandAndCollapseNode(startNode || tree.root);
			tree.animate = animate;
			tree.isExpandingAndCollapsing = false;
		},
		treeExpandAndCollapseNode: function(node) {
			var expanded = node.expanded;
			if (!expanded) node.expand();
			Ext.each(node.childNodes, function(child) {
				this.treeExpandAndCollapseNode(child);
			}, this);
			if (!expanded) node.collapse();
		},
		setFormFieldVisible: function(field, v) {
			if (!field.rendered) {
				field.on('render', this.setFormFieldVisible.createDelegate(this, arguments));
				return;
			}
			var el = field.el.up('.x-form-item');
			if (el) el.setVisible(v ? true : false)
		},
		setFormFieldDisplayed: function(field, v) {
			if (!field.rendered) {
				field.on('render', this.setFormFieldDisplayed.createDelegate(this, arguments));
				return;
			}
			var el = field.el.up('.x-form-item');
			if (el) el.setDisplayed(v ? true : false);
		},
		formOnChange: function(form, fn, scope) {
			if (typeof form.getForm == 'function') {
				if (!form.rendered) {
					form.on('afterrender', function() {
						this.formOnChange(form, fn, scope);
					}, this, {delay: 1});
					return;
				}
	  		form = form.getForm();
	  	}
			form.items.each(function(item) {
				var xtype = item.xtype;
				if (xtype == 'textfield' || xtype == 'datefield' || xtype == 'spinnerfield') {
					item.el.on('keydown', function(e) {
						if (e.getKey() == e.ENTER) {
							Ext.callback(fn, scope);
						}
					});
				}
				if (xtype == 'combo' || xtype == 'datefield') {
					item.on('select', fn, scope);
				}
				if (xtype == 'checkbox') {
					item.on('check', fn, scope);
				}
				if (xtype == 'spinnerfield') {
					item.on('spin', fn, scope);
				}
			});
		},
		windowMatchFormSize: function(win) {
			var winWidth = win.getWidth();
			var winHeight = win.getHeight();
			var formEl = win.body.child('form');
			formWidth = formEl.getWidth();
			formHeight = formEl.getHeight();
			var childWrapper = Ext.DomHelper.insertFirst(formEl, {tag: 'div', cls: 'wrapwrapwww', style: 'overflow: auto;'}, true);
			var child;
			while (child = formEl.dom.childNodes[1]) {
				childWrapper.dom.appendChild(child);
			}
			childWrapper.createChild({tag: 'div', cls: 'clear-element'});
			childWrapper.setSize(0, 0);
			var childWidth = childWrapper.dom.scrollWidth;
			var childHeight = childWrapper.dom.scrollHeight;
			while (child = childWrapper.dom.firstChild) {
				formEl.dom.appendChild(child);
			}
			childWrapper.remove();
			var extraWidth = 20; //For error icons
			win.setWidth(winWidth - formWidth + formEl.getPadding('rl') + childWidth + extraWidth);
			win.setHeight(winHeight - formHeight + formEl.getPadding('tb') + childHeight);
		},
		onImageLoaded: function(img, callback, scope) {
			if (img.complete) {
		  	Ext.callback(callback, scope);
				return;
		  }
			var c = 0;
			var interval = window.setInterval(function() {
				if (img.complete) {
					Ext.callback(callback, scope);
					window.clearInterval(interval);
				} else if (c > 50) {
					window.clearInterval(interval);
				}
				c++;
			}, 100);
		}
	}
}();