
var SPACER_GIF	= "/res/common/spacer.gif";
var BLANK_HTML	= "/res/common/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 = /\$PRODUCTID/g;
	var re6 = /\$PRODUCT/g;
	var re7 = /\$WSCOEMPATH/g;

	var topWindow = getTopWindow();

	url = url.replace(re1, topWindow.theLanguage);
	url = url.replace(re2, topWindow.theCountry);
	url = url.replace(re3, topWindow.theOEM);
	url = url.replace(re4, topWindow.theScriptDir);
	url = url.replace(re5, topWindow.productId || topWindow.theProductId);
	url = url.replace(re6, topWindow.theProduct);
	url = url.replace(re7, "/wsc/res/oem/" + topWindow.wlName);
	return url;
}

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;
		wnd.document.write("<script type='text/javascript' src='" + url + "'></"+"script>");
	}
}

// Unterschied zu IMPORT_JS:
// Scripte werden im Head angehangen, duerfen also kein document.write o.ae. beinhalten
// Ausserdem wird callback aufgerufen, sobald alle Scripte da sind
// Desweiteren kann auch ein Array uebergeben werden

IMPORT_JS_LIB = function(urls, callback, wnd) {
	wnd = (wnd || window);
	if (typeof(urls) == "string"){
		urls = [urls];
	}
	var url = formatURL(urls[0]);
	var script = wnd.document.createElement("script");

	var callbackFN = function( skipCheck ) {
							if ( script.importDone)
								return;

							// skipCheck is here if script.readyState is not there anymore
							// on a later class ( IE 7 had a problem with it )
							if ( !skipCheck && script.readyState && script.readyState != "loaded" && script.readyState != "complete")
								return;
							script.importDone = true; // dont call me twice
							var urls2 = urls.slice(1);
							if (urls2.length == 0) { // all imports done?
								if (typeof(callback) == "function")
									callback();
								return;
							}
							IMPORT_JS_LIB(urls2, callback, wnd);
					  };

	if (wnd.document.getElementById(url) == null) {
		// import the file
		var head = getHeadElement(wnd.document);

		script.setAttribute("language", "javascript");
		script.setAttribute("src", url);
		script.id = url;
		script.setAttribute("cm_src", url);

		script.onload=callbackFN;
		script.onreadystatechange=callbackFN;

		head.appendChild(script);
	} else {
		// file is already loaded, just call the callback func
		script.importDone = false;
		callbackFN(true);
	}
};

function getHeadElement(doc) {
	doc = (doc || document);

    var list = doc.getElementsByTagName("head");

    if (list && list[0]) {
		return list[0];
	}

	var head = doc.createElement("head");
	// assume html element is always there
	var html = doc.getElementsByTagName("html")[0];

	html.insertBefore(head, html.firstChild);
	return head;
}


IMPORT_CSS = function(url, wnd) {
	wnd = wnd || window;
	var doc = wnd.document;
	var isXHTML = doc.doctype && doc.doctype.publicId && doc.doctype.publicId.search(/\-\/\/W3C\/\/DTD XHTML.*/) > -1
	var head = doc.getElementsByTagName("head");
	url = (url.constructor == Array) ? url : [ url ];
	for ( var i = 0 ; i < url.length ; i++ ){
		var currentUrl = formatURL(url[i]);
		if (!head || !head[0]) {
			doc.write("<link type='text/css' rel='stylesheet' href='" + currentUrl + "'>"+(isXHTML ? "</link>" : ""));
		} else {
			var link = doc.createElement("link");
			link.setAttribute("type", "text/css");
			link.setAttribute("rel", "stylesheet");
			link.setAttribute("href", currentUrl);
			head[0].appendChild(link);
		}
	}
}

IMPORT_I18N = function(namespace) {
	IMPORT_JS("/res/i18n/$LANGUAGE/$COUNTRY/" + namespace + ".js");
	//IMPORT_JS("/app/generated/res/i18n/$LANGUAGE/$COUNTRY/" + namespace + ".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, fbwnd) {
	// call this method to get the main-WsC-window handle
	if (!wnd)
		wnd = this;

	//

	try {
		var test = wnd.document.location.href;
	} catch (e) {
		return (fbwnd || null);
	}

	if (typeof(wnd.__theOneAndOnlyTopWindow__) != 'undefined' && wnd.__theOneAndOnlyTopWindow__ != null)
		return wnd.__theOneAndOnlyTopWindow__;

	if (!wnd || wnd.parent == wnd) {

		try {
			if (wnd && !wnd.opener && wnd.dialogArguments && wnd.dialogArguments.opener)
				wnd.opener = wnd.dialogArguments.opener;
		} catch (e) {
		}

		if (wnd && wnd.opener && !wnd.opener.closed && wnd.opener != wnd) {
			var ret = getTopWindow(wnd.opener, wnd);
			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, wnd);
		if (ret) {
			wnd.__theOneAndOnlyTopWindow__ = ret;
			return ret;
		}
		return wnd;
	}
}

		function ensureWindowSize(width, height, center, exact) {


			var client 	= window.top.document.documentElement;
			var aWidth 	= window.screen.availWidth;
        	var aHeight	= window.screen.availHeight;

			var appPosX = (typeof(window.screenLeft) != "undefined")?window.screenLeft:window.screenX;
			var appPosY = (typeof(window.screenTop) != "undefined")?window.screenTop:window.screenY;

			var byX = 0;
			var byY = 0;

			// first: move the window top/left so it can grow to the right;
			if (appPosX + width > aWidth)
				byX = Math.max(0, (width - (aWidth - appPosX) + 20));
			if (appPosY + height > aHeight)
				byY = Math.max(0, (height - (aHeight - appPosY) + 40));

			if (exact) {
				try {
					window.top.moveBy(-byX, -byY);
				} catch (e) {}

				if (width != client.clientWidth || height != client.clientHeight) {
					try {
						window.top.resizeTo(width, height);
					} catch (e2) {}
					if (width != client.clientWidth || height != client.clientHeight) {
						width = width + (width - client.clientWidth);
						height = height + (height - client.clientHeight);
						try {
							window.top.resizeTo(width, height);
						} catch (e2) {}
					}
				}

			} else {
				if (client.clientWidth < width || client.clientHeight < height) {
					try {
						window.top.moveBy(-byX, -byY);
					} catch (e) {}
				
					byX = 0;
					byY = 0;
					if (client.clientWidth < width)
						byX = width - client.clientWidth ;
					if (client.clientHeight < height)
						byY = height - client.clientHeight ;
					try {
						window.top.resizeBy(byX, byY);
					} catch (e2) {}
	
					byX = 0;
					byY = 0;
	
					if (client.clientWidth < width)
						byX = width - client.clientWidth ;
					if (client.clientHeight < height)
						byY = height - client.clientHeight ;
					try {
						window.top.resizeBy(byX, byY);
					} catch (e2) {}
				}
       		}

			if (center) {
				// move again since with and height has changed
				var aWidth       = window.screen.availWidth;
        		var aHeight      = window.screen.availHeight;

        		var imoveLeft    = Math.round((aWidth - width) / 2);
        		var imoveTop     = Math.round((aHeight - height) / 2);
				try {
        			window.top.moveTo(imoveLeft, imoveTop);
        		} catch (e2) {}
        	}

		}



//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;

//	for (pt in src.prototype) {
//		target.prototype["_base" + pt]=src.prototype[pt];
//	}
}

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) {
	var topWindow = getTopWindow();
	if (topWindow.g_clientCheck.isIE55up || topWindow.g_clientCheck.isMoz1up) {
		if (typeof(array.push) == "function")
			array.push(value);
	} else {
		array[array.length] = value;
	}
}

Array.pop = function(array) {
	var topWindow = getTopWindow();
	if (topWindow.g_clientCheck.isIE55up || topWindow.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) {
	var topWindow = getTopWindow();
	if (topWindow.g_clientCheck.isIE55up || topWindow.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) {
	var topWindow = getTopWindow();
	if (topWindow.g_clientCheck.isIE55up || topWindow.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;
}


