/**
 * dogma Library - Global function collection
 * 
 * Diese Bibliothek beinhaltet alle globalen statischen Funktionen der
 * dogma Library.
 *
 * @author Steffen Müller
 * @version 0.1
 */

// Globale Variablen //////////////////////////////////////////////////////////
dUseIE = false;
dDebugActive = false;
dDebugWindow = null;
dIncludedList = new Array();
dStartedUp = false;

// Startup ////////////////////////////////////////////////////////////////////
function dStartUp ()
{
	if (dStartedUp) return;
	dStartedUp = true;
	
    // Browserweiche
    if (navigator.appName.match(/^microsoft/i))
        dUseIE = true;
    else
        dUseIE = false;

    dInclude("jscripts/dogma/dEventManager.js");

	new dEventManager ();
	
    // Weitere StartUp Funktionen aus Subtemplates aufrufen
    if (document.dStartFunctions)
    {
    	for (var i = 0; i < document.dStartFunctions.length; i++)
    	{
    		var f = document.dStartFunctions[i];
    		f();
    	}
    }
    
	// Mini-Startup aufrufen
	if (window.dLocalStartup)
		dLocalStartup();

    if (dDebugActive)
    	dInclude("jscripts/dogma/components/dPopupManager.js", dStartUpDebug);
}

function dStartUpDebug ()
{
	new dPopupManager ();
	
   	// Debugfenster erzeugen
   	var dw = document.createElement("div");
   	dw.style["width"] = "500px";
   	dw.style["height"] = "400px";
   	dw.style["border"] = "1px solid #000";
   	dw.style["overflow"] = "scroll";
   	dw.style["background"] = "#FFF";
   	dDebugWindow = dw;
   	
   	var info = new Array()
   	info["title"] = "Debug Information";
   	info["width"] = 500;
   	info["height"] = 400;

   	document.dPop.createPopupElement("debugWindow", dw, info);
   	document.dPop.showPopup("debugWindow");
}

// File Inclusion /////////////////////////////////////////////////////////////

/**
 * Callback des XHR Objektes
 */
function dIncludeCallback (xhr, callback)
{
	if(xhr.readyState != 4) return;

	if (xhr.status != 200)
	{
		dError("AJAX Include Request failed (response status: "+xhr.status+" - "+xhr.statusText+")");
		return;
	}
	
	if (xhr.responseText.length)
	{
		if(window.execScript) window.execScript(xhr.responseText);
		else window.eval(xhr.responseText);
	}
	if (callback) callback();
}

/**
 * Lädt weitere Javascript Dateien oder Stylesheets, erkennts an Dateiendung.
 *
 * Die optionale Callback Funktion wird nur bei Skripten aufgerufen, wenn das
 * Skript geladen ist. Davor stehen die Skriptinhalte nicht zur Verfügung.
 */
function dInclude (file, callback, doc)
{
	if (!doc) doc = document;
	
	// Nicht zweimal includen
	for (i = 0; i < dIncludedList.length; i++)
	{
		if (dIncludedList[i] == file)
		{
			if (callback) callback();
			return;
		}
	}
	
	// Includen starten
	if (file.match(/js$/i))
	{
		var xhr = dGetNewXHR();
		if (!xhr) return;
		var async = (callback != null);

		xhr.open('GET', file, async);
		xhr.onreadystatechange = function () {dIncludeCallback(xhr, callback);}
		xhr.send(null);
		
		if (!async) dIncludeCallback(xhr, null);
	}
	else
	{
		var head = doc.getElementsByTagName('head')[0];
		if (!head)
		{
			dError("dInclude failed: website does not contain head tag.");
			return;
		}
		var link = doc.createElement("link");
		link.href = file;
		link.rel = "stylesheet";
		head.appendChild(link);
		dIncludedList.push(file);
	}
}

// Error Handling /////////////////////////////////////////////////////////////
function dError (msg)
{
	alert ("An error occured:\n\n"+msg);
}

function dDebug (msg)
{
	if (!dDebugActive || !dDebugWindow) return;
	
	dDebugWindow.innerHTML = msg+"<br>"+dDebugWindow.innerHTML;
}

// Common Functions ///////////////////////////////////////////////////////////
function dGetNumericStyle (element, propname)
{
    ret = element.style[propname];
    if (!ret) return 0;
    ret = parseInt(ret);
    if (isNaN(ret))
        ret = element.currentStyle[propname];
    return ret;
}

function dGetStyle (element, cssname, jsname)
{
	if (element.currentStyle)
		return element.currentStyle[jsname];
	if (window.getComputedStyle)
		return window.getComputedStyle(element,null).getPropertyValue(cssname);
	return element.style[jsname];
}

function dGetPosY (obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function dGetPosX (obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function dGetWinDims (w)
{
	if (!w) w = window;
	var x,y;
	if (w.innerHeight) // all except Explorer
	{
		x = w.innerWidth;
		y = w.innerHeight;
	}
	else if (w.document.documentElement && w.document.documentElement.clientHeight)
	{
		// Explorer 6 Strict Mode
		x = w.document.documentElement.clientWidth;
		y = w.document.documentElement.clientHeight;
	}
	else if (w.document.body) // other Explorers
	{
		x = w.document.body.clientWidth;
		y = w.document.body.clientHeight;
	}
	return new Array(x,y);
}

function dGetScrolling (w)
{
	if (!w) w = window;
	if (w.pageYOffset != null) // all except Explorer
	{
		x = w.pageXOffset;
		y = w.pageYOffset;
	}
	else if (w.document.documentElement && w.document.documentElement.scrollTop)
	{
		x = w.document.documentElement.scrollLeft;
		y = w.document.documentElement.scrollTop;
	}
	else if (w.document.body)
	{
		x = w.document.body.scrollLeft;
		y = w.document.body.scrollTop;
	}
	return new Array(x,y);
}

function dGetEvent (event)
{
	if (!event) event = window.event;
	return event;
}

function dGetTarget (event)
{
	event = dGetEvent(event);

    // Zielobjekt bestimmen
	var target = event.target;
	if (!target) target = event.srcElement;
	return target;
}

function dSetClass (el, c)
{
	el.className = c;
	el.setAttribute("class", c);
}

function dGetClass (el)
{
	var val = el.className;
	if (!val) val = el.getAttribute("class");
	return val;
}

function dSetName (el, c)
{
	el.name = c;
	el.setAttribute("name", c);
}

function dGetName (el)
{
	var val = el.name;
	if (!val) val = el.getAttribute("name");
	return val;
}

function dSetOpacity (el, val)
{
	if (val < 0) val = 0;
	if (val > 100) val = 100;
	el.style.opacity = val/100;
	el.style.filter = 'alpha(opacity=' + val + ')';
}

function dSelectboxByValue (el, val)
{
	if (!el) return;
	
	var index = 0;
	var found = false;
	for (; index < el.length; index++)
	{
		if (el.options[index].value == val)
		{
			found = true;
			break;
		}
	}
	if (found) el.selectedIndex = index;
}

// Visual Effects /////////////////////////////////////////////////////////////////////////////////
function dExecuteFadeElements(name)
{
	var fader = document["dFader"+name];
	fader.velocity = parseInt(fader.velocity) + parseInt(fader.acc);
	fader.value = parseInt(fader.value) + parseInt(fader.velocity);
	
	if ((fader.acc < 0 && fader.value <= fader.target) || (fader.acc > 0 && fader.value >= fader.target))
	{
		var hide = fader.hide;
		window.clearInterval(fader.intervalID);
		for (var i = 0; i < fader.elementarray.length; i++)
		{
			var el = fader.elementarray[i];
			el.dFading = false;
			dSetOpacity(el, fader.target);
			if (hide) el.style.display = "none";
		}
		document["dFader"+name] = null;
		return;
	}
	for (var i = 0; i < fader.elementarray.length; i++)
	{
		var el = fader.elementarray[i];
		dSetOpacity(el, fader.value);
	}
}

function dFadeElements (elementarray, from, to, hide)
{
	if (!elementarray.length) return;
	for (var i = 0; i < elementarray.length; i++)
	{
		var el = elementarray[i];
		if (el.dFading) return;
		el.dFading = true;
		dSetOpacity(el, from);
	}
	
	var name = (new Date).getTime() + "_" + (Math.random()*1000);
	document["dFader"+name] = new Object();
	document["dFader"+name].elementarray = elementarray;
	document["dFader"+name].value = from;
	document["dFader"+name].target = to;
	document["dFader"+name].velocity = 0;
	document["dFader"+name].hide = hide;
	if (to >= from) document["dFader"+name].acc = 1;
	else document["dFader"+name].acc = -1;
	
	document["dFader"+name].intervalID = window.setInterval("dExecuteFadeElements('"+name+"')", 10);
}

function dExecuteFoldBox (theid)
{
	var ab = document.getElementById(theid);
	if (ab.offsetHeight > 10)
	{
		var nh = ab.offsetHeight - ab.dVelocity;
		if (nh < 20) nh = 0;
		ab.style.height = nh + "px";
		ab.dVelocity += 1;
	}
	else
	{
		ab.style.display = "none";
		ab.style.overflow = "visible";
		window.clearInterval(document.dFoldIntervals[theid]);
		document.dFoldIntervals[theid] = null;
	}
}

function dExecuteUnfoldBox (theid, height)
{
	var ab = document.getElementById(theid);
	if (ab.offsetHeight > height-10)
	{
		ab.style.height = height + "px";
		ab.style.overflow = "visible";
		window.clearInterval(document.dFoldIntervals[theid]);
		document.dFoldIntervals[theid] = null;
	}
	else
	{
		var nh = ab.offsetHeight + ab.dVelocity;
		ab.style.height = nh + "px";
		ab.dVelocity +=1;
	}
}

function dFoldBox (id, startvel)
{
	if (!document.dFoldIntervals) document.dFoldIntervals = new Array();
	if (document.dFoldIntervals[id]) return;

	var ab = document.getElementById(id);
	ab.style.display = "block";
	ab.style.overflow = "hidden";
	if (!startvel) startvel = 0;
	ab.dVelocity = startvel;
	
	document.dFoldIntervals[id] = window.setInterval("dExecuteFoldBox('"+id+"')", 10);
}

function dUnfoldBox (id, height)
{
	if (!document.dFoldIntervals) document.dFoldIntervals = new Array();
	if (document.dFoldIntervals[id]) return;

	var ab = document.getElementById(id);
	ab.style.display = "block";
	ab.style.overflow = "hidden";
	ab.dVelocity = 0;
	
	document.dFoldIntervals[id] = window.setInterval("dExecuteUnfoldBox('"+id+"', '"+height+"')", 10);
}

// Prompt Replacement /////////////////////////////////////////////////////////////////////////////

function dPrompt (question, callback, prefill)
{
	// Find highest document
	var d = top.document;
	
	// Set Callback
	d.dPromptCallback = callback;
	
	// Create box
	var box = d.createElement("div");
	dSetClass(box, "cms_promptbox");
	box.style.zIndex = 20010;
	var q = d.createElement("span");
	q.innerHTML = question;
	box.appendChild(q);

	var i = d.createElement("input");
	i.type = "text";
	if (prefill) i.value = prefill;
	dSetClass(i, "cms_promptinput");
	box.dInput = i;
	box.appendChild(i);

	var br = d.createElement("div");
	var ok = d.createElement("input");
	ok.type = "submit";
	ok.value = "OK";
	ok.dInput = i;
	box.dOK = ok;
	br.appendChild(ok);
	var cancel = d.createElement("input");
	cancel.type = "submit";
	cancel.value = "Abbrechen";
	box.dCancel = cancel;
	br.appendChild(cancel);
	box.appendChild(br);
	d.dPromptBox = box;

	// Create shadow
	var wd = dGetWinDims (top);
	var s = d.createElement("div");
	s.style.position = "absolute";
	s.style.left = "0px";
	s.style.top = "0px";
	s.style.width = wd[0]+"px";
	s.style.height = wd[1]+"px";
	s.style.background = "#000";
	s.style.opacity = 0.6;
	s.style.filter = "alpha(opacity=60)";
	s.style.zIndex = 20000;
	d.dPromptShadow = s;
	
	// Set events
	ok.onclick = function (event)
	{
		d = top.document;
		var val = d.dPromptBox.dInput.value;
		d.dPromptBox.parentNode.removeChild(d.dPromptBox);
		d.dPromptShadow.parentNode.removeChild(d.dPromptShadow);
		d.dPromptBox = null;
		d.dPromptShadow = null;
		var cb = d.dPromptCallback;
		d.dPromptCallback = null;
		cb(val);
		return false;
	};
	cancel.onclick = function (event)
	{
		d = top.document;
		d.dPromptBox.parentNode.removeChild(d.dPromptBox);
		d.dPromptShadow.parentNode.removeChild(d.dPromptShadow);
		d.dPromptBox = null;
		d.dPromptShadow = null;
		d.dPromptCallback = null;
	};
	i.onkeydown = function (event)
	{
		d = top.document;
		if (!event) event = top.event;
		if (event.keyCode == 13)
		{
			d.dPromptBox.dOK.click();
			return false;
		}
		if (event.keyCode == 27)
		{
			d.dPromptBox.dCancel.click();
			return false;
		}
	};
	
	// Spawn both!
	d.getElementsByTagName("body")[0].appendChild(s);
	d.getElementsByTagName("body")[0].appendChild(box);
	box.style.left = parseInt((wd[0]-box.offsetWidth)/2) + "px";
	box.style.top = parseInt((wd[1]-box.offsetHeight)/2) + "px";
	i.focus();
}

// Waiter /////////////////////////////////////////////////////////////////////////////////////////

function dShowWaiter ()
{
	// Find highest document
	var d = top.document;
	
	// Create box
	var box = d.createElement("div");
	box.style.position = "absolute";
	box.style.border = "1px solid #999";
	box.style.height = "40px";
	box.style.width = "40px";
	box.style.background = "url('design/symbols/reload_flower.gif') no-repeat center #FFF";
	box.style.zIndex = 10010;

	// Create shadow
	var wd = dGetWinDims (top);
	var s = d.createElement("div");
	s.style.position = "absolute";
	s.style.left = "0px";
	s.style.top = "0px";
	s.style.width = wd[0]+"px";
	s.style.height = wd[1]+"px";
	s.style.background = "#000";
	s.style.opacity = 0.6;
	s.style.filter = "alpha(opacity=60)";
	s.style.zIndex = 10000;
	
	// Spawn both!
	d.getElementsByTagName("body")[0].appendChild(s);
	d.getElementsByTagName("body")[0].appendChild(box);
	box.style.left = parseInt((wd[0]-box.offsetWidth)/2) + "px";
	box.style.top = parseInt((wd[1]-box.offsetHeight)/2) + "px";
	document.dWaiter = box;
	box.dShadow = s;
}

function dHideWaiter ()
{
	if (!document.dWaiter) return;
	document.dWaiter.dShadow.parentNode.removeChild(document.dWaiter.dShadow);
	document.dWaiter.parentNode.removeChild(document.dWaiter);
	document.dWaiter = null;
}

// Array Functions ////////////////////////////////////////////////////////////////////////////////

function dIsInArray (array, el)
{
	for (var i = 0; i < array.length; i++)
	{
		if (array[i] == el)
			return i;
	}
	return -1;
}


// Cookie Functions ///////////////////////////////////////////////////////////////////////////////
function dCreateCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function dReadCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function dEraseCookie(name)
{
	createCookie(name,"",-1);
}

// Multimedia Functions ///////////////////////////////////////////////////////////////////////////
function dPlaySound (soundname)
{
	var sound = document.getElementById(soundname);
	if (!sound) return;
	
	try
	{
		sound.Stop();
		sound.Rewind();
	}
	catch (e) {}
	try
	{
		sound.DoPlay();
	}
	catch (e)
	{
		try
		{
			sound.Play();
		}
		catch (e) {}
	}
}


// Wrapper Functions //////////////////////////////////////////////////////////////////////////////
function dShowLinkChoice (callback, prefill)
{
	if (typeof(callback) != "string")
	{
		dError("dShowLinkChoice: the parameter callback has to be a string specifying a function name");
		return;
	}

	if (typeof(prefill) != "string")
		prefill = dBase64Encode(dSerialize(prefill));

	if (!top.document.dPop)
	{
		if (window.dPopupManager)
			top.document.dPop = new dPopupManager();
		else
		{
			dInclude("jscripts/components/dPopupManager.js", "dShowLinkChoice('"+callback+"', '"+prefill+"')");
			return;
		}
	}
		
	var cb = dBase64Encode(callback);
	winparams = 
	{
		"height": 380,
		"width": 520,
		"title": "Link auswählen"
	};
	top.document.dPop.createPopupIFrame ("cmsLinkChoice("+cb+prefill+")", "index.php?module=cmsBrowser&todo=wizard_link&callback="+cb+"&prefill="+prefill, winparams);
	top.document.dPop.showPopupModal ("cmsLinkChoice("+cb+prefill+")");
}

function dShowColorChoice (callback, prefill)
{
	if (typeof(callback) != "string")
	{
		dError("dShowColorChoice: the parameter callback has to be a string specifying a function name");
		return;
	}

	if (typeof(prefill) != "string")
		prefill = dBase64Encode(dSerialize(prefill));

	if (!top.document.dPop)
	{
		if (window.dPopupManager)
			top.document.dPop = new dPopupManager();
		else
		{
			dInclude("jscripts/components/dPopupManager.js", "dShowColorChoice('"+callback+"', '"+prefill+"')");
			return;
		}
	}
		
	var cb = dBase64Encode(callback);
	winparams = 
	{
		"height": 265,
		"width": 415,
		"title": "Farbe auswählen"
	};
	top.document.dPop.createPopupIFrame ("cmsColorChoice("+cb+prefill+")", "index.php?module=cmsEditor&todo=colorpicker&callback="+cb+"&prefill="+prefill, winparams);
	top.document.dPop.showPopupModal ("cmsColorChoice("+cb+prefill+")");
}

// UTF Functions //////////////////////////////////////////////////////////////////////////////////
function dUTFEncode (input)
{
	// In UTF umwandeln
	var utftext = "";
	for(var n=0; n<input.length; n++)
	{
		var c=input.charCodeAt(n);

		// alle Zeichen von 0-127 => 1byte
		if (c < 128) utftext += String.fromCharCode(c);
		// alle Zeichen von 127 bis 2047 => 2byte
		else if((c>127) && (c<2048))
		{
			utftext += String.fromCharCode((c>>6)|192);
			utftext += String.fromCharCode((c&63)|128);
		}
		// alle Zeichen von 2048 bis 66536 => 3byte
		else
		{
			utftext += String.fromCharCode((c>>12)|224);
			utftext += String.fromCharCode(((c>>6)&63)|128);
			utftext += String.fromCharCode((c&63)|128);
		}
	}
	return utftext;
}

function dUTFDecode (utftext)
{
	var plaintext = "";
	var i=0;
	var c=c1=c2=0;

	while(i<utftext.length)
	{
		c = utftext.charCodeAt(i);
		if (c<128)
		{
			plaintext += String.fromCharCode(c);
			i++;
		}
		else if((c>191) && (c<224))
		{
			c2 = utftext.charCodeAt(i+1);
			plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
			i+=2;
		}
		else
		{
			c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
			plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
			i+=3;
		}
	}
	
	return plaintext;
}

// Base 64 Functions //////////////////////////////////////////////////////////////////////////////
dGlobals_Base64String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function dBase64Encode(input)
{
	if (!input) return "";
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;
   
	input = dUTFEncode(input);

	do
	{
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);

		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;

		if (isNaN(chr2)) enc3 = enc4 = 64;
		else if (isNaN(chr3)) enc4 = 64;

		output = output + dGlobals_Base64String.charAt(enc1) + dGlobals_Base64String.charAt(enc2) + dGlobals_Base64String.charAt(enc3) + dGlobals_Base64String.charAt(enc4);
	} while (i < input.length);
   
	return output;
}

function dBase64Decode (input)
{
	if (!input) return "";
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	do
	{
		enc1 = dGlobals_Base64String.indexOf(input.charAt(i++));
		enc2 = dGlobals_Base64String.indexOf(input.charAt(i++));
		enc3 = dGlobals_Base64String.indexOf(input.charAt(i++));
		enc4 = dGlobals_Base64String.indexOf(input.charAt(i++));

		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;

		output = output + String.fromCharCode(chr1);

		if (enc3 != 64) output = output + String.fromCharCode(chr2);
		if (enc4 != 64) output = output + String.fromCharCode(chr3);
	} while (i < input.length);

	return dUTFDecode(output);
}

// HEX DECODING ///////////////////////////////////////////////////////////////////////////////////
			
function dGetHexValue (hex)
{
	var val = 0;
	for (i = 0; i < hex.length; i++)
	{
		var c = hex.charAt(hex.length-1-i);
		var cval = Number(c);
		if (isNaN(cval))
		{
			c = c.toLowerCase();

			if (c == "a") cval = 10;
			if (c == "b") cval = 11;
			if (c == "c") cval = 12;
			if (c == "d") cval = 13;
			if (c == "e") cval = 14;
			if (c == "f") cval = 15;
		}
		val += cval*Math.pow(16, i);
	}
	return val;
}

// Serialization / Deserialization ////////////////////////////////////////////////////////////////

function dSerialize (inp)
{
	var	c = {"\b":"b","\t":"t","\n":"n","\f":"f","\r":"r",'"':'"',"\\":"\\","/":"/"};
	var factory =
	{
		"boolean": function(){return Boolean},
		"function": function(){return Function},
		"number": function(){return Number},
		"object": function(o){return o instanceof o.constructor?o.constructor:null},
		"string": function(){return String},
		"undefined": function(){return null}
	};
	var rs = /(\x5c|\x2F|\x22|[\x0c-\x0d]|[\x08-\x0a])/g;
	var ru = /([\x00-\x07]|\x0b|[\x0e-\x1f])/g;
	var s = function(i,d){return "\\"+c[d]};
	var u = function(i,d){var n=d.charCodeAt(0).toString(16); return "\\u".concat(p[n.length],n)};

	var result = undefined;

	if (inp === null)
		result = "null";
	else if (inp !== undefined && (tmp = factory[typeof inp](inp)))
	{
		switch(tmp)
		{
			case Array:
				result = [];
				for(var	i = 0, j = 0; j < inp.length; j++)
				{
					if(inp[j] !== undefined && (tmp = dSerialize(inp[j])))
						result[i++] = tmp;
				}
				result = "["+result.join(",")+"]";
				break;
			case Boolean:
				result = String(inp);
				break;
			case Date:
				result = '"'+inp.getFullYear()+"-"+(inp.getMonth()+1)+"-"+inp.getDate()+" "+inp.getHours()+":"+inp.getMinutes()+':'+inp.getSeconds()+'"';
				break;
			case Function:
				break;
			case Number:
				result = isFinite(inp) ? String(inp) : "null";
				break;
			case String:
				result = '"'+inp.replace(rs, s).replace(ru, u)+'"';
				break;
			default:
				var	i = 0, key;
				result = new Array();
				for(key in inp)
				{
					if(inp[key] !== undefined && (tmp = dSerialize(inp[key])))
					{
						tmp = '"'+key.replace(rs, s).replace(ru, u)+'":'+tmp;
						result[i++] = tmp;
					}
				}
				result = "{"+result.join(",")+"}";
				break;
		}
	}
	return result;
}

function dUnserialize (inp)
{
	try
	{
		result = eval("("+inp+")");
	}
	catch(z)
	{
		dError("bad data");
	}
	return result;
}

// XMLHTTPRequest erstellen ///////////////////////////////////////////////////////////////////////
function dGetNewXHR ()
{
	var xmlHttp = null;
	// Mozilla, Opera, Safari sowie Internet Explorer 7
	if (typeof XMLHttpRequest != 'undefined')
	    xmlHttp = new XMLHttpRequest();
	if (!xmlHttp)
	{
	    // Internet Explorer 6 und älter
	    try
	    {
	        xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
	    }
	    catch(e)
	    {
	        try
	        {
	            xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
	        }
	        catch(e)
	        {
	            xmlHttp  = null;
	        }
	    }
	}
	if (!xmlHttp)
	{
		return null;
	}
	return xmlHttp;
}