if (typeof Error == 'undefined') {
	Error = {};
	
	Error.debug = function (obj, sep)
	{
		var message = "";
		var type = typeof obj;
		if (! sep) {
			sep = "";
		}
		message += "(" + type + ")";
		if (type === "object" || type === "array") {
			message += "\n{\n";
			var sep2 = sep + "    ";
			for (var key in obj) {
				//message += sep2 + ((type === "array") ? "[" + key + "]" : "->" + key) + " = " + Error.debug(obj[key], sep2) + "\n";
				message += sep2 + ((type === "array") ? "[" + key + "]" : "->" + key) + " = " + obj[key] + "\n";
			}
			message += "}";
		} else {
			message += " " + obj;
		}
		return message;
	}
}


if (typeof System == 'undefined' || ! System.defined) {
	System = {'defined': true};
	
	System.debugMessage = function (message) {
		messageWindow = window.top;
		if (! isset(messageWindow.frameworkMessage)
		|| ! isset(messageWindow.frameworkMessage.length)) {
			messageWindow.frameworkMessage = [];
		}
		messageWindow.frameworkMessage.push(message);
		if (! messageWindow.debugWindow) {
			var
			dialogWidth = screen.availWidth * 0.50
			dialogHeight = screen.availHeight,
			dialogLeft = screen.availWidth - dialogWidth,
			dialogTop = 0;
			messageWindow.debugWindow = true;
			if (! isset(window.showModelessDialog)) {
				window.attachEvent("onload", new Function('var w = window.open('
				+ '"' + SERVER_BASE + INDEX_NAME + '_dialogs/debug.php", "debugDialog", "'
				+ 'width=' + dialogWidth + 'px, height=' + dialogHeight + 'px, '
				+ 'left=' + dialogLeft + 'px, top=' + dialogTop + 'px, '
				+ 'resizable, scrollbars, dependent, modal, dialog"); '
				+ 'w.dialogArguments = messageWindow; return true;'));
			} else {
				window.attachEvent("onload", new Function('showModelessDialog('
				+ '"' + SERVER_BASE + INDEX_NAME + '_dialogs/debug.php", messageWindow, "'
				+ 'dialogWidth: ' + dialogWidth + 'px; '
				+ 'dialogHeight: ' + dialogHeight + 'px; '
				+ 'dialogLeft: ' + dialogLeft + 'px; '
				+ 'dialogTop: ' + dialogTop + 'px; center: no; dialogHide: yes; '
				+ 'edge: raised; help: no; resizable: yes; scroll: auto; '
				+ 'status: no; unadorned: no;"); return true;'));
			}
		} else if (messageWindow.debugWindow !== true) {
			messageWindow.debugWindow.Refresh();
		}
	}
	
	System.errorMessage = function (container) {
		var message = (container.length) ? (container[container.length-1].innerHTML) : (container.innerHTML);
		var rgx = new RegExp('^<br\s*\/?><b>([^<]*)<\/b>:\s*([^\s].*[^\s])\s*in\s*<b>([^<]*)<\/b>.*<b>([^<]*)<\/b><br\s*\/?>$', 'i');
		var res = rgx.exec(message);
		if (rgx != null && res != null) {
			title = res[1];
			title = title.toLowerCase();
			if (title === 'fatal error') {
				title = 'system error';
				endline = 'Script was terminated';
			} else {
				title = 'system ' + title;
				endline = '';
			}
			message = '<table class="error" cellspacing="0" cellpadding="0" border="0" width="100%" height="100%" align="center">'
			+ '<tr valign="top"><th height="10" colspan="2"><p><b>' + title + ':</b> ' + res[2] + '</p></th></tr>'
			+ '<tr valign="top"><td height="10"><p><b>File:&nbsp;</b></p></td><td width="100%"><p>' + res[3] + '</p></td></tr>'
			+ '<tr valign="top"><td height="10"><p><b>Line:&nbsp;</b></p></td><td width="100%"><p>' + res[4] + '</p></td></tr>'
			+ '<tr valign="top"><td height="10" colspan="2"><p>' + endline + '</p></td></tr></table>';
		} else {
			if (message.substr(1, 3) == '<br') {
				message = message.substr(message.indexOf('>'));
			}
			message = '<table class="error" cellspacing="0" cellpadding="0" border="0" width="100%" height="100%" align="center">'
			+ '<tr valign="top"><th height="10" colspan="2"><p>' + message + '</p></th></tr></table>';
		}
		System.debugMessage(message);
	}
	
	System.__error = function (e) {
		Dialogs.error(e.name + ': ' + e.message);
	}
	
	System.parentWindow = function (w) {
		if (! isset(w)) {
			w = window;
		}
		w = w.top;
		if (w.opener) {
			return w.opener;
		}
		if (w.dialogArguments && w.dialogArguments.caller) {
			return w.dialogArguments.caller;
		}
		return window.parent;
	}
	
	System.reloadWindow = function (w) {
		if (! isset(w)) {
			w = window;
		}
		//w = w.top;
		if (w.name && w.name !== '[object]') {
			w.open(w.location.href, w.name);
		} else {
			w.location = w.location.href;
		}
	}
	
	System.closeWindow = function (w) {
		if (! isset(w)) {
			w = window;
		}
		w = w.top;
		if (! isset(w.opener)) {
			w.opener = w;
		}
		w.close();
	}
	
	System.returnValue = function (v, w) {
		if (! isset(w)) {
			w = window;
		}
		w = w.top;
		w.returnValue = v;
	}
	
	System.openLink = function (url, target, params, win) {
		if (! isset(win)) {
			win = window;
		}
		if (target == null || target == '') {
			target = win.name;
		}
		if (target == '' || target === '[object]') {
			target = '_self';
		}
		if (params) {
			return win.open(url, target, params);
		} else {
			return win.open(url, target);
		}
	}
}

