
var SPACER_GIF	= "/app/static/img/spacer.gif";
var BLANK_HTML	= "/app/static/html/blank.html";

//	128 nbsp´s für Modalfenster-Title
var TITLE_SPACES = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
TITLE_SPACES += TITLE_SPACES;
TITLE_SPACES += TITLE_SPACES;
TITLE_SPACES += TITLE_SPACES;
TITLE_SPACES += TITLE_SPACES;


function formatURL(url) {
	// You can use this variables:
	var re1 = /\$LANGUAGE/g;
	var re2 = /\$COUNTRY/g;
	var re3 = /\$OEM/g;
	var re4 = /\$SCRIPTDIR/g;
	var re5 = /\$PRODUCT/g;

	url = url.replace(re1, window.top.theLanguage);
	url = url.replace(re2, window.top.theCountry);
	url = url.replace(re3, window.top.theOEM);
	url = url.replace(re4, window.top.theScriptDir);
	url = url.replace(re5, window.top.theProduct);
	return url;
}

//if (typeof(window.top.IMPORT_JS) == 'undefined') {
	//window.top.IMPORT_JS =
	IMPORT_JS = function(url, wnd) {
		url = formatURL(url);

		if (typeof(wnd) == 'undefined')
			wnd = window;
		if (typeof(wnd.loaded_js_files) == 'undefined') {
			wnd.loaded_js_files = new Object();
			//wnd.loaded_js_files.files = new Array();
		}
		if (typeof(wnd.loaded_js_files.files) == 'undefined') {
			wnd.loaded_js_files.files = new Array();
		}
		if (typeof(wnd.loaded_js_files.files[url]) == 'undefined') {
			//alert(url);
			wnd.loaded_js_files.files[url] = true;
			document.write("<SCRIPT src='" + url + "'></"+"SCRIPT>");
		}
	}
//}
//IMPORT_JS = window.top.IMPORT_JS;

//------------------------------------------------------------------------------
//	Simuliert java.net.URLDecoder.encode()
if (typeof(encodeURIComponent) == "undefined") {
	encodeURIComponent = function(s) {
		return escape(s);
	}
}
if (typeof(decodeURIComponent) == "undefined") {
	decodeURIComponent = function(s) {
		return unescape(s);
	}
}


function jdecode(s){
	var re = /\+/g;
	s = s.replace(re, "%20");
	return decodeURIComponent(s);
}

function jencode(s) {
	var re1 = /\+/g;
	var re2 = /%20/g;
	s = encodeURIComponent(s);
	return s.replace(re1, "%2B").replace(re2, "\+");
}


function getTopWindow(wnd) {
	// call this method to get the main-WsC-window handle
	if (!wnd)
		wnd = this;
	if (typeof(wnd.__theOneAndOnlyTopWindow__) != 'undefined' && wnd.__theOneAndOnlyTopWindow__ != null)
		return wnd.__theOneAndOnlyTopWindow__;

	if (!wnd || wnd.parent == wnd) {
		if (wnd && wnd.opener && wnd.opener != wnd) {
			var ret = getTopWindow(wnd.opener);
			if (ret) {
				wnd.__theOneAndOnlyTopWindow__ = ret;
				return ret;
			}
		}
		// There is no parent and no opener, asume that wnd is the main window
		return wnd;
	} else {
		var ret = getTopWindow(wnd.parent);
		if (ret) {
			wnd.__theOneAndOnlyTopWindow__ = ret;
			return ret;
		}
		return wnd;
	}
}

function getResDescription() {
	var wnd = getTopWindow();
	return wnd.resDescription;
}
function getFeatureDescription() {
	var wnd = getTopWindow();
	return wnd.featureDescription;
}
function getFeature(feature, defaultValue) {
	var fd = getFeatureDescription();
	try {
		var ret = fd[feature];
		if (typeof(ret) != "undefined")
			return ret;
	} catch (e) {
	}
	return defaultValue;
}


function registerOpenWindow(wnd) {
	var topWnd = getTopWindow();
	var aow = topWnd.varContainer.openWindows;
	aow[aow.length] = wnd;
}

function _onSessionTimeout(wnd) {
	if (!wnd || wnd.closed)
		return;

	var timeoutUrl = "/app/static/html/wsc/sessiontimeout.html"
					+ "?UILanguage=" + getTopWindow().theLanguage
					+ "&UICountry=" + getTopWindow().theCountry;
	if (typeof(wnd.onSessionTimeout) == "function") {
		if (typeof(wnd.onSessionTimeoutCalled) == 'undefined') {
			wnd.onSessionTimeoutCalled = true;
			wnd.onSessionTimeout();
		}
	} else if (typeof(wnd.showSessionTimeout) != 'undefined' && wnd.showSessionTimeout) {
		if (typeof(wnd.dialogArguments) == "undefined") {
			wnd.document.location.replace(timeoutUrl);
		} else {
			wnd.document.open();
			wnd.document.write("<FRAMESET rows='100%'><FRAME src='" + timeoutUrl + "'></FRAMESET>");
			wnd.document.close();
		}
	}
}

function showSessionInvalidMsg(wnd) {
return;
	// Alle Fenster in der Aufruf Hierachy bekommen eine SessionInvalid Seite verpasst
	var isFirstCall = false;

	if (!wnd) {
		isFirstCall = true;
		wnd = this;
	}
	if (typeof(wnd.__theOneAndOnlyTopWindow__) != 'undefined' && wnd.__theOneAndOnlyTopWindow__ == wnd) {
		_onSessionTimeout(wnd);
		return;
	}

	if (!wnd || wnd.parent == wnd) {
		if (wnd && wnd.opener && wnd.opener != wnd) {
			showSessionInvalidMsg(wnd.opener);
		}
		_onSessionTimeout(wnd);
	} else if (wnd.parent) {
		showSessionInvalidMsg(wnd.parent);
	}
	if (isFirstCall) {
		var topWnd = getTopWindow();
		var aow = topWnd.varContainer.openWindows;
		for (var i=0; i < aow.length; i++)
			_onSessionTimeout(aow[i]);

		//throw "Session timeout";
	}
}


//utf-8 stuff
//---------------------------



// OO in JS -> target extends src
function copyBase(src, target) {
	for (pt in src.prototype) {
		target.prototype[pt]=src.prototype[pt];
	}
	target.prototype.toString=src.prototype.toString;
	target.prototype.valueOf=src.prototype.valueOf;
	target.prototype.base=src;
}

function copyObject(src, target) {
	// um z.B. Language Objekte save zu extenden,
	// dh. sollte in src eine Variable nicht gesetzt sein, bleibt die in target definierte erhalten
	for (pt in src) {
		target[pt]=src[pt];
	}
}

function safeEscape(s) {
	if (!s)
		return "";

	return s.replace(/([&<'])/g,
		function ($1) {
			switch ($1)	{
				case "&":
					return "&amp;";
				case "<":
					return "&lt;";
				case "'":
					return "\\'";
				case "\"":
					return "\\\"";
			}
		}
	);
}

//------------------------------------------------------------------------------
//	String.startsWith und endsWith wie in Java
String.prototype.startsWith = function(prefix) {
	if (!prefix)
		return true;
	if (prefix.length > this.length)
		return false;
	return this.indexOf(String(prefix)) == 0;
}

String.prototype.endsWith = function(suffix) {
	if (!suffix)
		return true;
	if (suffix.length > this.length)
		return false;
	return this.lastIndexOf(String(suffix)) == this.length - String(suffix).length;
}

//------------------------------------------------------------------------------
//	Global-Unique-ID erzeugen
function getGUID() {

	var zeroStr = "0000000000000000";

	var timeStamp = new Date().valueOf().toString(16);
	timeStamp = zeroStr.substr(0, 10 - timeStamp.length) + timeStamp;

	var random = Math.random().toString(16).substr(2);
	random = zeroStr.substr(0, 16 - random.length) + random;

	var hrb = "32668";		//	HRB der CM-AG anstelle MAC-Adresse

	var guid = hrb + "0" + timeStamp.substring(0, 2)
			+ "-" + timeStamp.substring(2, 6)
			+ "-" + timeStamp.substring(6, 10)
			+ "-" + random.substring(0, 4)
			+ "-" + random.substring(4, 16);
	guid = guid.toUpperCase();

	return guid;
}

//------------------------------------------------------------------------------
//	Eine HashMap, die vom GarbageCollector abgeräumt werden kann
function CMHashMap() {
	this.map = new Array();
}

CMHashMap.prototype.get = function(key) {
	if (typeof(this.map[key]) == "undefined" || this.map[key] == null)
		return null;
	else
		return this.map[key];
}

CMHashMap.prototype.put = function(key, value) {
	this.map[key] = value;
	return this.map[key];
}

CMHashMap.prototype.remove = function(key) {
	var value = this.get(key);
	this.map[key] = null;
	var canTrash = true;
	for (var o in this.map) {
		if (this.map[o] != null) {
			canTrash = false;
			break;
		}
	}
	if (canTrash)
		this.map = new Array();

	return value;
}

//------------------------------------------------------------------------------
//	IE 5.0 kennt kein Array.push, splice, shift
//	Beachte: Nicht array.push("x") sondern Array.push(array, "x")
Array.push = function(array, value) {
	if (window.top.g_clientCheck.isIE55up || window.top.g_clientCheck.isMoz1up) {
		if (typeof(array.push) == "function")
			array.push(value);
	} else {
		array[array.length] = value;
	}
}

Array.pop = function(array) {
	if (window.top.g_clientCheck.isIE55up || window.top.g_clientCheck.isMoz1up) {
		if (typeof(array.pop) == "function")
			return array.pop();
		else
			return null;
	} else {
		if (array.length == 0) {
			return null;
		} else {
			var value = array[array.length-1];
			array.length --;
			return value;
		}
	}
}

Array.shift = function(array) {
	if (window.top.g_clientCheck.isIE55up || window.top.g_clientCheck.isMoz1up) {
		if (typeof(array.shift) == "function")
			return array.shift();
		else
			return null;
	} else {
		if (array.length == 0) {
			return null;
		} else {
			var value = array[0];
			for (var i = 0; i < array.length-1; i ++)
				array[i] = array[i+1];

			array.length --;
			return value;
		}
	}
}

Array.unshift = function(array, value) {
	if (window.top.g_clientCheck.isIE55up || window.top.g_clientCheck.isMoz1up) {
		if (typeof(array.unshift) == "function")
			array.unshift(value);
	} else {
		for (var i = array.length-1; i >= 0; i --)
			array[i+1] = array[i];

		array[0] = value;
	}
}

function convertToText(msg) {
	msg = (msg || "");

	var re1 = /<script/ig;
	var re2 = /<\/script/ig;
	var re3 = /(\r\n)|(\n\r)/g;
	var re4 = /\n/g;

	msg = msg.replace(re1,"&lt;script");
	msg = msg.replace(re2,"&lt;/script");
	msg = msg.replace(re3,"\n");
	msg = msg.replace(re4,"<BR/>");

	return msg;
}

function revertToText(msg) {
	msg = (msg || "");

	var re1 = /&lt;script/ig;
	var re2 = /&lt;\/script/ig;
	var re3 = /<br\/>/ig;

	msg = msg.replace(re1,"<script");
	msg = msg.replace(re2,"<" + "/script");
	msg = msg.replace(re3,"\n");

	return msg;
}


