		var gecko = (navigator.userAgent.toLowerCase().indexOf("gecko") >= 0);
		var opera = (navigator.userAgent.toLowerCase().indexOf("opera") >= 0);
		var ie = (navigator.userAgent.toLowerCase().indexOf("msie") >= 0);
		var safari = (navigator.userAgent.toLowerCase().indexOf("safari") >= 0);

		var browserVersion = parseFloat(navigator.appVersion);


		/* Preload any image which is used as a button, which is used in the Stylesheets */
		if(document.styleSheets) {
			// Do that no longer, as it a) won't work as expected and b) is not neccesary
			var peloadStyleSheetImages = false;
			for(var j = 0; j < document.styleSheets.length && peloadStyleSheetImages; j++) {
				var ss = document.styleSheets[j];
				var rules = ss.cssRules || ss.rules; 
				var preload = new Array();
				
				for(var i = 0; i < rules.length; i++) {
					if(!rules[i].style.backgroundImage) {
						continue;
					}

					var text = rules[i].style.backgroundImage.toLowerCase();
					var url = text.replace("url(", "").replace(")", "");
					
					if(url.toLowerCase().indexOf("button") == -1) {
						continue;
					}
					
					var key = preload.length;
					preload[key] = new Image();
					preload[key].src = url;
				}
			}
		}

		var ButtonList = [];
		function addButton(_name, _align, _action, _cName, _cId) {
			if(!_cName) {
				_cName = false;
			}
			
			ButtonList[ButtonList.length] = {
				name: _name,
				align: _align,
				action: _action,
				cname: _cName,
				cid: _cId
			};
		}
		
		function addItemToButtonLine(_source, _align) {
			ButtonList[ButtonList.length] = {
				source: _source,
				align: _align
			};
		}
		
		function moveButtonsToButtonLine() {
			if(!window.moveButtons || !moveButtons) {
				return true;
			}

			var inputs = document.getElementsByTagName("input");
			for(var i = 0; i < inputs.length; i++) {
				var elem = inputs[i];

				if(!elem.form) continue;
				
				if(elem.type == "button") {
					var moveIt = elem.getAttribute("moveThisButton");
					if(!moveIt) {
						moveIt = true;
						forceMove = false;
					} else if(moveIt == "true") {
						moveIt = true;
						forceMove = true;
					} else if(moveIt == "false") {
						moveIt = false;
					}
					
					var id = elem.id;

					if((elem.form.name != "mainForm" && moveIt == true && forceMove == false) || moveIt == false) {
						continue;
					}

					var action = elem.getAttribute("onClick").toString();
					var regexp = /function (anonymous|onclick)\(\)\s*\{\s*(.+)\s*\}/i;
					if(regexp.exec(action.replace("\r\n", " ").replace("\r", " ").replace("\n", " "))) {
						action = RegExp.$2;
					}

					if(action.indexOf('this.form') >= 0) {
						var formlink = "document." + (elem.form.name || "document.mainForm");
						action = action.replace("this.form", formlink);
					}
					addButton(elem.value, "right", action, elem.className, id);
					elem.style.display = "none";
					elem.id = "";
				}
			}
		}
		
		function printButtons() {
			var left = '';
			var right = '';
	
			for(var i = 0; i < ButtonList.length; i++) {
				var Button = ButtonList[i];
				
				if(Button.source) {
					ButtonHTML = Button.source;
				} else {
					var ButtonHTML = '<input type="button" value="' + Button.name + '" name="' + Button.name + '" onClick="' + Button.action + '"';
					if(Button.cname) {
						ButtonHTML += ' class="' + Button.cname + '"';
					} 
					if(Button.cid) {
						ButtonHTML += ' id="' + Button.cid + '"';
					}
					ButtonHTML += '/>';
				}
	
				if(Button.align == 'left') {
					left += ButtonHTML + "&nbsp;";
				} else {
					right +=  "&nbsp;" + ButtonHTML;
				}
			}
			
			if(left.length >= 6) {
				left = left.substr(0, left.length - 6)
			}
			if(right.length >= 6) {
				right = right.substr(6, right.length);
			}
			
			left = left == '' ? '&nbsp;' : left;
			right = right == '' ? '&nbsp;' : right;

			var ButtonsHTML = '<form name="dummyButtonForm" style="margin: 0px" onSubmit="return false;"><table border="0" cellpadding="0" cellspacing="0" style="height: 40px; width: 100%; padding-left: 10px; padding-right: 10px"><tr>';
				ButtonsHTML += '<td align="left">' + left + '</td>';
				ButtonsHTML += '<td align="right">' + right + '</td>';
				ButtonsHTML += '</tr></table></form>';
	
			try {
				var ButtonsContainer = document.getElementById("buttonLine");
					ButtonsContainer.innerHTML = ButtonsHTML;
					ButtonsContainer.style.display = "block";
			} catch(e) {}
		}

/*
	This script has no real license, but I got the OK to use it commercial
	It's been done by 
		- Peter Müller - http://fumle.dk/
		- Lumpio- (name and url unknown - find him in IRC QuakeNet #javascript)
*/

// It's a debugging thingamajig O_o

String.prototype.repeat = function(n) {
	var r = "", i;
	for (i = 0; i < n; i++) {
		r += this;
	}
	return r;
};

var DebugHandler = function(title, pos) {
	this.entries = [];
	this.curItem = null;
	this.title = title || null;
	this.pos = pos || "ne";
	
	this.open();
}

DebugHandler.styleInserted = false;
DebugHandler.indent = "    ";
DebugHandler.css = "\
.debugger {\
	position: absolute;\
	border: 1px solid #000;\
	padding: 3px;\
	background-color: #EEE;\
	width: 250px;\
	-moz-border-radius: 4px;\
	font-family: sans-serif;\
	font-size: 0.8em;\
	z-index: 20000;\
}\
.debugger.nw {\
	top: 10px;\
	left: 10px;\
}\
.debugger.ne {\
	top: 10px;\
	right: 10px;\
}\
.debugger.sw {\
	bottom: 10px;\
	left: 10px;\
}\
.debugger.se {\
	bottom: 10px;\
	right: 10px;\
}\
.debugger.fold {\
	opacity: 0.3;\
}\
.debugger.fold:hover {\
	opacity: 1.0;\
}\
.debugger h2 {\
	font-size: 0.9em;\
	margin: 2px 0;\
}\
.debugger .toolbox {\
	margin: 2px 0;\
}\
.debugger .toolbox a {\
	display: block;\
	float: right;\
	margin-right: 1px;\
	width: 13px;\
	height: 13px;\
	color: #000;\
	text-decoration: none;\
	background-image: url(data:image/gif;base64,R0lGODlhRQAaAKIAAJkAAO7u7t3d3czMzLu7u5mZmWZmZv///yH5BAEAAAcALAAAAABFABoAAAP/OLrc/jDKyYK9OOt9B++f5YVKaJJZkY2fp2pqWcxz8F50bd25iKm3gGsT26VEw9/x+OIJcVCfbclyIqfKrLIJHXGx1egTGpQtcTpjk+bjcpNAI8uIMWvpaLYzjPfSX3NxHUlieHiCU35ZfFhjjTcyPTlAkmRdjUqBmIpRJSefKyZzmE9BXaOgJ6ihKCQUr7CxsQS0tba3uLm6u7y9tgLAwcLDxMXBBMbHycDIy7TL0M7DBsPNycgAxdnPBt3dAtTC3t/A4eDezOLgwsjm6s3ZwvHc0/X14ebU1vnK6+oC1uIJEEjvXzB35fzxa4YvIcCDENMFyyYQYDt7B8klxIfuVmHDhhfXgRxW0aK/iCjLjTupD6U5hg7DWRtI81jIlCcdioQI898+lAEn2jyHbhw1oyyT3nynzh08ksxmRpsq8RrGdCWBbZNKNRrXatC+svNFtqxZswkAADs%3D);\
	-moz-outline: none;\
}\
.debugger .toolbox a.clear {\
	width: 33px;\
	background-position: 0 0;\
}\
.debugger .toolbox a.clear:hover {\
	background-position: 0 -13px;\
}\
.debugger .toolbox a.fold {\
	background-position: -32px 0;\
}\
.debugger .toolbox a.fold:hover {\
	background-position: -32px -13px;\
}\
.debugger.fold .toolbox a.fold {\
	background-position: -44px 0;\
}\
.debugger.fold .toolbox a.fold:hover {\
	background-position: -44px -13px;\
}\
.debugger.se .toolbox a.fold, .debugger.sw .toolbox a.fold {\
	background-position: -44px 0;\
}\
.debugger.se .toolbox a.fold:hover, .debugger.sw .toolbox a.fold {\
	background-position: -44px -13px;\
}\
.debugger.fold.se .toolbox a.fold, .debugger.sw .toolbox a.fold {\
	background-position: -32px 0;\
}\
.debugger.fold.se .toolbox a.fold:hover, .debugger.sw .toolbox a.fold {\
	background-position: -32px -13px;\
}\
.debugger .toolbox a.close {\
	background-position: -56px 0;\
}\
.debugger .toolbox a.close:hover {\
	background-position: -56px -13px;\
}\
.debugger.ie .toolbox a {\
	font-size: 0.7em;\
	background-color: #EEE;\
	border: 1px solid #666;\
	text-align: center;\
}\
.debugger.ie .toolbox a:hover {\
	background-color: #BBB;\
}\
.debugger ul {\
	list-style: none;\
	padding: 2px;\
	height: 300px;\
	margin: 0;\
	overflow: auto;\
	background-color: #FFF;\
	border: 1px solid #AAA;\
	font-family: monospace;\
}\
.debugger ul li {\
	white-space: pre;\
	padding: 2px;\
	border-top: 1px solid #CCC;\
}\
.debugger.fold ul {\
	display: none;\
}\
";

// Yes, the next line is an abomination. But this is the way to get a styleblock in the head section in IE apparently...
document.write('<style type="text/css">' + DebugHandler.css + '</style>');

DebugHandler.isIE = navigator.userAgent.toLowerCase().indexOf("msie") != -1;

DebugHandler.prototype = {
	open: function() {
		var e;
		if (!this.el) {
			this.el = document.createElement("div");
			this.el.className = "debugger " + this.pos + (DebugHandler.isIE ? " ie" : "");
			
			this.toolbox = document.createElement("div");
			this.toolbox.className = "toolbox";
			var obj = this;
			
			(function(c, t, f, ietext) {
				var e = document.createElement("a");
				e.obj = obj;
				e.className = c;
				e.title = t;
				e.onclick = f;
				e.href = "#";
				if (DebugHandler.isIE) {
					e.appendChild(document.createTextNode(ietext));
				}
				obj.toolbox.appendChild(e);
				obj["btn" + c] = e;
				return arguments.callee;
			})
			("close", "Close", this.close, "X")
			("fold", "Fold/unfold", this.toggle, "_")
			("clear", "Clear log", this.clear, "CLEAR");

			this.el.appendChild(this.toolbox);
			
			e = document.createElement("h2");
			e.appendChild(document.createTextNode(this.title || "Debug log"));
			this.el.appendChild(e);
	
			this.list = document.createElement("ul");
			if (this.pos == "se" || this.pos == "sw") {
				this.el.insertBefore(this.list, this.el.firstChild);
			} else {
				this.el.appendChild(this.list);
			}
	
			document.body.appendChild(this.el);
		}
	},

	clear: function() {
		obj = this.obj || this;
		obj.entries = [];
		obj.curItem = null;
		var l = document.createElement("ul");
		obj.list.parentNode.replaceChild(l, obj.list);
		obj.list = l;
		return false;
	},

	close: function() {
		obj = this.obj || this;
		obj.clear();
		obj.el.parentNode.removeChild(obj.el);
		obj.el = null;
		return false;
	},

	fold: function() {
		this.el.className = "debugger " + this.pos + " fold" + (DebugHandler.isIE ? " ie" : "");
	},

	unfold: function() {
		this.el.className = "debugger " + this.pos + (DebugHandler.isIE ? " ie" : "");
	},

	toggle: function() {
		obj = this.obj || this;
		(obj.el.className.indexOf("fold") == -1) ? obj.fold() : obj.unfold();
		return false;
	},

	setWidth: function(w) {
		this.el.style.width = w + "px";
	},

	setHeight: function(h) {
		this.el.style.height = h + "px";
	},

	write: function(s, c) {
		if (!this.curItem) {
			this.writeln(s, c);
			this.curItem = this.entries[this.entries.length - 1];
		} else {
			this.curItem.firstChild.nodeValue += s;
		}
	},

	writeln: function(s, c) {
		var e;

		e = document.createElement("li");
		e.appendChild(document.createTextNode(s));
		if (typeof c != "undefined") {
			e.style.color = c;
		}
		
		this.curItem = null;
		this.entries.push(e)
		this.list.insertBefore(e, this.list.firstChild);
	},

	print_r: function(o, d) {
		var i, r;
		if (typeof d == "undefined") {
			d = 0;
		}
		if (typeof o == "string") {
			return "\"" + o + "\"";	
		} else if (o === null) {
			return "null";
		} else if (o instanceof Array) {
			r = "[\n";
			for (i = 0; i < o.length; i++) {
				if (typeof(o[i]) == "function") {
					continue;
				}
				if (r != "[\n") {
					r += ",\n";
				}
				r += DebugHandler.indent.repeat(d + 1) + this.print_r(o[i], d + 1);
			}
			return r + "\n" + DebugHandler.indent.repeat(d) + "]";
		} else if (typeof o == "object") {
			r = "{\n";
			for (i in o) {
				if (typeof(o[i]) == "function") {
					continue;
				}
				if (r != "{\n") {
					r += ",\n";
				}
				r += DebugHandler.indent.repeat(d + 1) + i + ": " + this.print_r(o[i], d + 1);
			}
			if (r == "{\n") {
				return "{ }";
			} else {
				return r + "\n" + DebugHandler.indent.repeat(d) + "}";
			}
		} else {
			return o.toString();
		}
	}
};


var DraggableWindowModalScreen = null;

function DraggableWindow(name, title, top, left, width, height, settings) {
	this.DOM = null;
	this.TitleBar = null;
	this.TitleBarText = null;
	this.CloseButton = null;
	this.ContentBox = null;
	this.HidedSelectFields = [];

	this.ClassName = "DraggableWindow";

	this.Name = name;
	this.Title = title || "";
	this.Top = top || 0;
	this.Left = left || 0;
	this.Width = width || 400;
	this.Height = height || 300;
	this.Settings = settings || { };
	
	this.IsDragging = false;
	this.IsShown = false;
	
	this.Init();
}

DraggableWindow.prototype = {
	Init : function() {
		this.CloseButton = document.createElement("div");
		this.CloseButton.className = "CloseButton";
		
		this.TitleBarText = document.createElement("div");
		this.TitleBarText.className = "TitleBarText";
		this.TitleBarText.innerHTML = this.Title;
	
		this.TitleBar = document.createElement("div");
		this.TitleBar.className = "TitleBar";
		this.TitleBar.appendChild(this.TitleBarText);
		this.TitleBar.appendChild(this.CloseButton);
		
		this.ContentBox = document.createElement("div");
		this.ContentBox.className = "ContentBox";
		this.ContentBox.IsAppended = false;
		
		DraggableWindowModalScreen = document.createElement("div");
		DraggableWindowModalScreen.className = "ModalScreen";
		if(window.ie && window.getWindowSize) {
			DraggableWindowModalScreen.style.width = getWindowSize("width");
		}
		
		this.DOM = document.createElement("div");
		this.DOM.className = this.ClassName;
		this.DOM.appendChild(this.TitleBar);
		this.DOM.id = this.Name;
		
		this.Repaint();
		this.RegisterEvents();
	},
	RegisterEvents : function() {
		var that = this;

		Events.add(this.TitleBar, "mousedown", function(e) {
			that._OnInitDrag(e);
		});
		Events.add(document, "mouseup", function(e) {
			that._OnFinishDrag(e);
		});
		Events.add(document, "mousemove", function(e) {
			that._OnDrag(e);
		});
		Events.add(this.CloseButton, "click", function(e) {
			that.Close();
		});
	},
	Open : function(parent) {
		parent = parent || document.body;
		parent.appendChild(this.DOM);
		if(this.Settings.IsModal) {
			document.body.appendChild(DraggableWindowModalScreen);
		}		
		this.IsShown = true;
		this.Repaint();

		if(this.ContentBox.IsAppended == false) {
			this.RepaintContentBox();
			this.DOM.appendChild(this.ContentBox);
			this.ContentBox.IsAppended = true;
		}
		
		if(window.ie) {
			this._HideSelectFields();
		}
		if(this.OnOpen) {
			this.OnOpen();
		}
	},
	Close : function() {
		if(this.OnClose) {
			this.OnClose();
		}
		this.DOM.parentNode.removeChild(this.DOM);
		if(this.Settings.IsModal) {
			DraggableWindowModalScreen.parentNode.removeChild(DraggableWindowModalScreen);
		}
		this.IsShown = false;
		
		if(window.ie) {
			this._ShowSelectFields();
		}
	},
	MoveTo : function(top, left) {
		this.Top = top;
		this.Left = left;
		this.Repaint();
	},
	MoveBy : function(x, y) {
		this.Top += parseInt(y);
		this.Left += parseInt(x);
		this.Repaint();
	},
	ResizeTo : function(width, height) {
		this.Width = width;
		this.Height = height;
		this.Repaint();
		this.RepaintContentBox();
	},
	Repaint : function() {
		this.DOM.style.top = this.Top;
		this.DOM.style.left = this.Left;
		this.DOM.style.width = this.Width;
		this.DOM.style.height = this.Height;
	},
	RepaintContentBox : function() {
		this.ContentBox.style.height = this.DOM.offsetHeight - this.TitleBar.offsetHeight;
	},
	SetContent : function(content) {
		if(typeof(content) == "string") {
			this.ContentBox.innerHTML = content;
		} else {
			try {
				while(this.ContentBox.hasChildNodes()) {
					this.ContentBox.removeChild(this.ContentBox.firstChild);
				}
				this.ContentBox.appendChild(content);
			} catch(e) { }
		}
	},
	
	// Available Events
	_OnInitDrag : function(e) {
		this.IsDragging = true;
	},
	_OnFinishDrag : function(e) {
		this.IsDragging = false;
	},
	_OnDrag : function(e) {
		if(this.IsDragging == false) {
			return;
		}

		var diffX = Mouse.actual.x - Mouse.last.x;
		var diffY = Mouse.actual.y - Mouse.last.y;

		var moveByX = diffX;
		var moveByY = diffY;

		if(this.Left + this.Width + diffX > getWindowSize("width")) {
			moveByX = getWindowSize("width") - this.Left - this.Width;
		} else if(this.Left + moveByX < 0) {
			moveByX = -this.Left;
		}
		if(this.Top + this.Height + 1 + diffY > getWindowSize("height")) {
			moveByY = getWindowSize("height") - this.Top - (this.Height + 1);
		} else if(this.Top + moveByY < 0) {
			moveByY = -this.Top;
		}

		this.MoveBy(moveByX, moveByY);
	},
	_FormIsInWindow : function(form) {
		var node = form;
		for(var node = form; node.parentNode; node = node.parentNode) {
			if(node == this.DOM) {
				return true;	
			}
		}
		return false;
	},
	_HideSelectFields : function() {
		var elements = document.getElementsByTagName("select");
		for(var i = 0; i < elements.length; i++) {
			var elem = elements[i];
			if(elem.style.display != "none" && elem.style.visibility != "hidden" && this._FormIsInWindow(elem.form) == false) {
				this.HidedSelectFields[this.HidedSelectFields.length] = elem;
				elem.style.visibility = "hidden";
			}
		}
	},
	_ShowSelectFields : function() {
		for(var i = 0; i < this.HidedSelectFields.length; i++) {
			var elem = this.HidedSelectFields[i];
			elem.style.visibility = "visible";
		}
		this.HidedSelectFields = [];
	}
}

function GetDraggableWindow(titleStringId, headerStringId, contentName, formName, submitAction, cancelAction, onLoadCallback) {
	var width = 700;
	var height = 500;
	var top = 100;
	var left = 100;

	var content = document.getElementById(contentName);
	content.parentNode.removeChild(content);
	content.style.display = "block";
	
	var windowObj = new DraggableWindow(getString(headerStringId), getString(titleStringId), 100, 100, 700, 500, { IsModal : true });
	
	var http = new HTTP();	
	var url = "/basket/index.php?tan=" + encodeURI(document.mainForm.tan.value) + 
		"&orderid=" + encodeURI(document.mainForm.orderid.value) + 
		"&uniqueid=" + encodeURI(document.mainForm.uniqueid.value) + 
		"&task=dialog&layoutOnly=1" +
		"&formName=" + encodeURI(formName) +
		"&submitAction=" + encodeURI(submitAction) +
		"&cancelAction=" + encodeURI(cancelAction) + 
		"&header=" + encodeURI(headerStringId);
	
	var loadingText = document.createTextNode(getString(24302));
	windowObj.SetContent(loadingText);

	var response = new Response(function() {
		windowObj.SetContent(this.getText());
		window.setTimeout(function() {
			document.getElementById(formName + "Content").appendChild(content);
			renderButtons();
			
			if(onLoadCallback) {
				onLoadCallback();
			}
		}, 100);
	});
	http.post(url, response);
	
	return windowObj;
}

var Events = {
	_events: new Array(),
	add: function(obj, type, fn) {
		if((typeof type).toLowerCase() == "string") {
			if (obj.addEventListener) {
				obj.addEventListener(type, fn, false);
			} else if(obj.attachEvent) {
				obj["e" + type + fn] = fn;
				obj[type + fn] = function() {
					if(obj["e" + type + fn]) {//this is needed for errors caused through double-inclusion of buttons.js and leaved here, because it shouldn't make any problems and it's also needed if Events.js is a second time included,...
						obj["e" + type + fn](window.event);
					}
				}
				obj.attachEvent("on" + type, obj[type + fn]);
			}
			this._events[this._events.length] = {
				obj: obj,
				type: type,
				fn: fn
			}
		} else {
			var types = type;
			for(var i = 0; i < types.lengths; i++) {
				var type = types[i];

				if (obj.addEventListener) {
					obj.addEventListener(type, fn, false);
				} else if(obj.attachEvent) {
					obj["e" + type + fn] = fn;
					obj[type + fn] = function() {
						obj["e" + type + fn](window.event);
					}
					obj.attachEvent("on" + type, obj[type + fn]);
				}
				this._events[this._events.length] = {
					obj: obj,
					type: type,
					fn: fn
				}
			}
		}
	},
	remove: function(obj, type, fn) {
		if (obj.removeEventListener) {
			obj.removeEventListener(type, fn, false);
		} else if(obj.detachEvent) {
			obj.detachEvent("on" + type, obj[type + fn]);
			obj[type + fn] = null;
			obj["e" + type + fn] = null;
		}
		for(var i = this._events.length; i >= 0 ; i--) {
			if(this._events[i]) {
				var ev = this._events[i];
				if(ev.obj == obj && ev.type == type && ev.fn == fn) {
					this._events.splice(i,1);
				}
			}
		}
	},
	cleanup: function() {
		for(var i = 0; i < this._events.length; i++) {
			var evt = this._events[i];
			if(evt) {
				this.remove(evt.obj, evt.type, evt.fn);
			}
		}
	},			
	target: function(e) {
		var target = e.target || e.srcElement;				
		if (target.nodeType == 3) {
			target = target.parentNode;
		}				
		return target;
	},
	stop: function(e) {
		e.cancelBubble = true;
		if(e.stopPropagation) e.stopPropagation();
	},
	debug: function() {
		for(var elemid in this._events) {
			var ev = this._events[elemid];
			debug(ev.obj + " - " + ev.type + " - " + ev.fn);
		}			
	}
}
Events.add(window, "unload", function() {
	Events.cleanup();
});


