/**
 * Funktion zum absenden eines AJAX-REQUESTS
 *
 */
var loader;
var debugmode	=	true;
var browserinfos	=	"Compatible::"+navigator.appCodeName+"|Realname::"+navigator.appName+"|Agentinfo::"+navigator.userAgent+"|Plattform::"+navigator.platform;

function makeRequest(viewtype,parameters)
{
	var Jetzt = new Date();
	Jetzt.toGMTString();
	var parameters	=	"viewtype="+viewtype+"&"+parameters;
	var newfound	=	null;
	var hideloader	=	null;
	var costumloader=	null;
	var brokenpars	=	parameters.split('&');
	for(b=0;b<brokenpars.length;b++)
	{	var brokenparm	=	brokenpars[b].split('=');
		if(brokenparm.length > 1 && brokenparm[0] == 'loader')
		{	costumloader	=	brokenparm[1];	}
		if(brokenparm.length > 1 && brokenparm[0] == 'loader' &&  brokenparm[1] == 'hide')
		{	hideloader	=	true;	}
		if(brokenparm.length > 1 && brokenparm[0] == 'new' &&  brokenparm[1] == 'true')
		{	newfound	=	true;	}
	}
	if($('ToolTip') != null)	$('ToolTip').hide();
	if($('tabedinfo') != null)	$('tabedinfo').hide();
	var loadertype	=	((hideloader == true) ? null : ((newfound == true) ? 'main' : costumloader));

	new Ajax.Request('request.php',
		{
		method: 'POST',
		parameters: parameters,
		loadertype: loadertype,
		requestHeaders:
			[
			'Content-type', 'application/x-www-form-urlencoded',
			'Charset', 'Prototype',
			'If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT',
			'Expires', Jetzt.toGMTString(),
			'Charset', 'Prototype',
			'Cache-Control', 'no-store, no-cache, must-revalidate,post-check=0, pre-check=0,max-age=1,s-maxage=1, must-revalidate',
			'Charset', 'Prototype',
			],
		onCreate: function(transport)
			{
				var loadersize	=	64;
				var loadertype	=	'o';
				var mainid		=	'body';
				var loaderid	=	this.loadertype;
				if(this.loadertype != null)
				{	var brokenloader	=	this.loadertype.split('_');
					if(brokenloader.length > 0)	loaderid	=	brokenloader[0];
					if(brokenloader.length > 1)	mainid		=	'#'+brokenloader[1];
					if(brokenloader.length > 2)	loadersize	=	brokenloader[2];
					if(brokenloader.length > 3)	loadertype	=	brokenloader[3];
					if($(loaderid) == null)
					{	
						Try.these(
							function () { $$(mainid)[0].insert({bottom: new Element('div', { 'id': loaderid})}); }
							);
					}
					loader	=	getBusyOverlay($(loaderid),{color:'white',opacity:0.75},{color:'#000', size:loadersize, type:'o', minopac: 0.0});
				}
			},
		onSuccess: function(transport) 
			{
				if(transport.responseXML != null)	doResponse(transport.responseXML);
				else if(debugmode)
					alert(transport.responseText);
			},
		onComplete: function (transport)
			{
				if(loader != '')
				{	loader.remove();	loader='';	}
			}
		}
		);
}


/**
 * Funktion zum verarbeiten eines AJAX-Response
 *
 */
function doResponse(xmldoc)
{
	var objekte			= xmldoc.getElementsByTagName('xml').item(0);
	var objekte_anzahl	= (objekte != null) ? objekte.childNodes.length : 0;
	if((objekte_anzahl > 0) && (objekte != null))
	{
		for (objx=0;objx < objekte_anzahl; objx++)
		{
			subobj	=	objekte.childNodes[objx];
			so_name		=	subobj.getAttribute('name');
			so_typ		=	subobj.getAttribute('typ');
			so_ziel		=	subobj.getAttribute('ziel');
			if(so_ziel == 'divinputbox' && $('divinputbox') == null)
				$$('body')[0].insert({bottom: new Element('div', { 'id': 'divinputbox'})});

			inhalt		=	(subobj.childNodes.length > 0) ? subobj.childNodes[0].nodeValue : '';
			if($(so_ziel) != null || so_typ == 'js')
			{
				switch(so_typ)
				{
					case "html":
						switch(so_name)
						{
							case 'bottom': $(so_ziel).insert({bottom:inhalt.stripScripts()}); break;
							case 'top': $(so_ziel).insert({top:inhalt.stripScripts()}); break;
							case 'before': $(so_ziel).insert({before:inhalt.stripScripts()}); break;
							case 'after': $(so_ziel).insert({after:inhalt.stripScripts()}); break;
							default: $(so_ziel).update(inhalt.stripScripts()); break;
						}
						inhalt.evalScripts();						break;
					case "js":
						try
						{	eval(inhalt);	}
						catch(e)
						{	alert('JS-ERROR:'+inhalt);	}
						break;
					case "form":
						
						setForm(so_ziel,subobj);
						break;
				}
			}
		}
	}
}

/**
 * Funktion zum absenden eines AJAX-REQUESTS 
 * mit Formularfeldern und base64 encoding
 *
 */

function sendFormSSL(viewtype,formname,parameter)
{	if($(formname) != null)
	{
		aList	=	$(formname).select('input[name!=""]','textarea[name!=""]').each(Element.extend);
		for(x=0;x<aList.length;x++)
		{	parameter	+= '&'+aList[x].readAttribute('name')+'='+encodeURIComponent(((aList[x].readAttribute('type') == 'checkbox') ? (((aList[x].checked == "-1") || (aList[x].checked == true)) ? 'true' : 'false') : ((aList[x].readAttribute('send') == "encode") ? '[encoded]'+Base64.encode($F(aList[x])) : $F(aList[x]))));	}
	}
	makeRequest(viewtype,parameter);	}

/**
 * Funktion zum setzen von Formularfeldern
 * Werte nur per XML-Objekt Ã¼bergebbar
 *
 */
function setForm(formname,daten)
{
	if($(formname) != null)
	{	
		aList	=	$(formname).select('input[name!=""]','textarea[name!=""]').each(Element.extend);
		for(x=0;x<aList.length;x++)
		{	
			xObj	=	daten.getElementsByTagName(aList[x].readAttribute('name'));
			if(xObj.length != 0)
			{	xVal	=	(xObj[0].firstChild == null) ? "" : xObj[0].firstChild.nodeValue;
				if (aList[x].tagName == "TEXTAREA")
				{
					aList[x].innerHTML	=	xVal;
				}
				else
				{
					aList[x].setValue((aList[x].readAttribute('type') == 'checkbox') ?  (xVal.toLowerCase() == "true") : xVal);
				}
			}
		}
	}
}

/**
 * Funktion zum Leeren eines Formulars
 *
 */
function clearForm(formname)
{
	aList	=	$(formname).select('input[name!=""]','textarea[name!=""]').each(Element.extend);
	for(x=0;x<aList.length;x++)
	{	aList[x].setValue((aList[x].readAttribute('type') == 'checkbox') ?  false : "");	}
}

/**
 * Hinzufügen eines Eventhandlers fuer den Tooltip
 *
 */

$(document).observe('mousemove', function (e)
{	if($('ToolTip') != null && $('ToolTip').empty()!= true)
	{	$('ToolTip').setStyle({left : (Event.pointerX(e) + 20)+'px',top : (Event.pointerY(e))+'px'});	}
});
$(document).observe('click', function (e)
{	var eo = e.srcElement || e.target;
	if(eo.name && eo.name != "")	Try.these(function() { return rmSingleBubbles(eo.name) });
});
$(document).observe('keyup', function (e)
{	var eo = e.srcElement || e.target;
	var kc = e.keyCode != null ? e.keyCode : e.charCode;
	if(eo.name && eo.name != "" && kc == 9)	Try.these(function() { return rmSingleBubbles(eo.name) });
});


/**
 * Funktion zum base64_encoding eines Strings
 *
 */
var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
		input = Base64._utf8_encode(input);
		while (i < input.length) {

			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 +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.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);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}

/**
 * Funktion zum ansprechen eines DOM-Objekts per id
 *
 */
function gebi(id)	{	return $(id);	}

/**
 * Funktion zum ansprechen eines DOM-Objekts per name
 *
 */
function gebn(name)
{
	all	=	$$('[name='+name+']').each(Element.extend);
	return (all.length>0) ? all[0] : null;
}

/**
 * Funktion zum Ã¤ndern der Navigation
 *
 */
function toggleNavi (moduleID) {
	$('navi').select('div.naviFiles').each(Element.hide);
	$('navi').select('div#naviFiles_'+moduleID).each(Element.show);
}

/**
 * Funktion zum Ã¤ndern der Navigation (ab version 2.5)
 *
 */
function toggleNavi2 (moduleID) {
	$('navi').select('div.files').each(Element.hide);
	$('navi').select('div#naviFiles_'+moduleID).each(Element.show);
}

function toggleSmenu (moduleID) {
	$('SmenuLine').select('div.Smenu').each(Element.hide);
	$('SmenuLine').select('div#Smenu'+ moduleID).each(Element.show);
}


/**
 * Funktion zum ein und ausblenden von Objekt per id
 *
 */
function toggleById(id) {
	$(id).toggle();	}

function initScrollableList(listid,timeoff)
{
	if($(listid) != null && $(listid).readAttribute('listcommand') != null && $(listid).select('[listid!=""]').length > 0)
	{
		SgD				=	Element.getDimensions($(listid));
		foundObjects	=	$(listid).descendants();
		Lcommand		=	$(listid).readAttribute('listcommand');
		absoluteTop		=	-99;
		hasmore			=	false;
		hastodo			=	false;
		for(x=0;x<foundObjects.length;x++)
		{
			EcO			=	Element.positionedOffset(foundObjects[x]);
			if(absoluteTop == -99) absoluteTop	= EcO.top;
			
			objTop		=	EcO.top-absoluteTop;
			EcSO		=	Element.cumulativeScrollOffset(foundObjects[x]);
			eID			=	Element.readAttribute(foundObjects[x],'listid');
			running		=	Element.readAttribute(foundObjects[x],'running');
			aktTop		=	EcSO.top+SgD.height;
			isempty		=	(Element.empty(foundObjects[x])) ? '1':'0';
			if(eID != null && EcSO != null && aktTop > objTop)
			{
				if(running == null && hastodo == false)
				{
					hastodo	=	Lcommand;
					while(hastodo.indexOf('$listid') != -1)
					{	hastodo	=	hastodo.replace('$listid',eID);	}
					Element.writeAttribute(foundObjects[x],'running',value=true);
				}
				else if(running == null)
				{	hasmore	=	true;	}
			}
			else
			{
				nichtsichtbar	=	true;
			}
		}
		if(hastodo != false) eval(hastodo);
		if(hasmore) window.setTimeout('initScrollableList(\''+listid+'\')',timeoff);
	}
}
var scrolled	=	false;
var scrolltimer	=	null;
function scrollList(id)
{
	timeoff	=	(scrollList.arguments.length>1) ? scrollList.arguments[1] : 500;
	Event.observe($(id),'scroll',function(e)
	{	scrolled=true;	window.clearTimeout(scrolltimer); scrolltimer=window.setTimeout('initScrollableList(\''+id+'\','+timeoff+')',1000);	});
	//Event.observe($(id),'mouseup',function(e)	{	if(scrolled == true) initScrollableList(id);	scrolled=false;	});
	initScrollableList(id);
}

