<!--
alertWidth=300; // Breite des Fensters
alertHeight=200; // Höhe des Fensters
xAlertStart=300;
yAlertStart=100;
alertTitle="<p align='center'><b>JavaScript Alert !!!</b></p>";
alertText="<p align='center'>Hallo, ich bin eine Dialogbox !<br /><br />Im Internet Explorer 5+ lasse ich mich sogar verschieben.</p>";

// Firefox detection
//debugger;
var isFF = navigator.userAgent.indexOf("Firefox") >= 0 || navigator.appName=="Netscape";

// Dialogs are normally modal - only one popup is allowed for this script
var currentOpenDialog = null;
var backlayer = null;
var frontlayer = null;
var messagecontent = null;
var popupDefaultTop = 250;    // Height-calculation is very difficult ;) so use this parameter to start at the best default top position
var maxheight = 600;
var requestSpecificDivIdParameter = "__request_element_id";
var requestExecCodeParameter = "__request_execcode";
var requestVarDump = "__request_vardump";

function closeDialog ( ignoreCloseScript ) {
	hideDialog ( ignoreCloseScript );	
}


function hideDialog ( ignoreCloseScript ) {
	if ( currentOpenDialog != null )
	{
		currentOpenDialog.style.display="none";
		if ( $("_blank") )
		{
			var blank = $("_blank" );
			if ( blank.parentNode )
				blank.parentNode.removeChild ( blank );
		}
	}
			
	currentOpenDialog = null;
	
	enableControls ( );
	
	frontlayer.style.display="none";
	backlayer.style.display="none";
	
	if ( !ignoreCloseScript && frontlayer.closescript != null )
	{
		runscript ( frontlayer.closescript );
		frontlayer.closescript = null;
	}
}

function runscript ( scripttxt )
{
	if ( scripttxt == "reload" )
		//window.location.href = window.location.href;
		window.location.reload ( );
	else
		eval ( scripttxt );
}

function alertAlternative() {
	alert("Ihr Browser unterstützt keine Popup-Dialoge.");
}

//Nur für IE 5+, NN6+ und Opera 5+
ie5=(document.getElementById && document.all && document.styleSheets)?1:0;
nn6=(document.getElementById && !document.all)?1:0;
op5=(document.getElementById&&document.all&&!document.styleSheets)?1:0;
ie6=false;

function InitBacklayer ( )
{
	if ( backlayer != null )
		return;
		
	// Backgroundgey erstellen
	backlayer = document.createElement ( "div" );
	backlayer.className = "messagebackground";
	backlayer.onmousemove='checkMove()';
	backlayer.onmouseup='clearMove()';
	backlayer.style.display='none';

	document.body.appendChild ( backlayer );
//	document.body.insertBefore( backlayer, document.body.childNodes[0] );
}

function InitFrontLayer ( )
{
	if ( frontlayer != null )
	{
		return;
	}
	
	frontlayer = document.getElementById ( "messagefront" );
	frontlayer.style.display='none';
	frontlayer.origWidth = frontlayer.style.width;
	
	messagecontent = document.getElementById ( "messagecontent" );
	
/*	frontlayer = document.createElement ( "div" );
	frontlayer.className = "messagefront";
	frontlayer.innerHtml = 'Testinhalt';

	document.body.appendChild ( frontlayer );	
*/
}

function showBackground ()
{
	InitBacklayer ( );
	if ( backlayer != null )
		backlayer.style.display = 'block';
}

function hideBackground ()
{
	if ( backlayer != null )
		backlayer.style.display = 'none';
}

// Box anzeigen
function showDialog ( divIdToShow )
{
	readDialogAttributes ( $(divIdToShow), null );
	showAlert ( $(divIdToShow) );
}

function showAlert ( divToAlert, displaymode ) {

	if ( currentOpenDialog != null && currentOpenDialog != divToAlert )
	{
		closeDialog ( true );
	}
			
	InitFrontLayer ( );
	showBackground ( );
	
	if ( !displaymode )
		displaymode = 'block';
		
	if ( currentOpenDialog == null )
	{
		disableControls ( );
//			alertAlternative ( );
		
		moveStatus = 0;
		currentOpenDialog = divToAlert;
		currentOpenDialog.style.display = displaymode;
		currentOpenDialog.style.visibility = "visible";
		currentOpenDialog.style.position = 'relative';

		currentOpenDialog.style.top = "0px";
		currentOpenDialog.style.left = "0px";
		currentOpenDialog.style.overflow = "visible";
		
		frontlayer.style.display = 'block';
		
		//messagecontent.style.overflow='visible';

		if ( currentOpenDialog.parentNode != messagecontent )
		{
			messagecontent.appendChild ( currentOpenDialog );
		}
		
		if ( ie6 || ie5 )
		{
			// Reactivate disabled ddown controls in focus area
			for ( i = 0; i < messagecontent.getElementsByTagName("select").length; i++ )
				messagecontent.getElementsByTagName("select")[i].style.visibility = "visible";
		}

		if ( currentOpenDialog.popuptitle )
			$("messagetitletext").update ( currentOpenDialog.popuptitle );
		else if ( $('_blank') &&  $('_blank').popuptitle )
			$("messagetitletext").update ( $('_blank').popuptitle );
		else
			$('messagetitletext').update ( "Details" );
			
		if ( currentOpenDialog.popupWidth )
		{
			frontlayer.style.width = currentOpenDialog.popupWidth;
		}
		else if ( $('_blank') && $('_blank').popupWidth )
		{
			frontlayer.style.width = $('_blank').popupWidth;
		}
		//messagecontent.offsetWidth = ( parseInt(frontlayer.origWidth) - 12 ) + "px";
		
		frontlayer.focus();
		frontlayer.blur();

	}
	
	if ( currentOpenDialog )
		centerFocusDialog ( );
}

var disablelist;

function disableControls ()
{
	if ( !( ie5 ) )
		return;

	disablelist = document.getElementsByTagName("select");

	for ( i = 0; i < document.getElementsByTagName("select").length; i++ )
	{
		document.getElementsByTagName("select")[i].style.visibility = "hidden";
	}
}

function enableControls ()
{
	if ( !( ie5 ) )
		return;
		
	disablelist = document.getElementsByTagName("select");

	for ( i = 0; i < document.getElementsByTagName("select").length; i++ )
	{
		document.getElementsByTagName("select")[i].style.visibility = "visible";
	}
}

function centerFocusDialog ()
{
	xAlert=xAlertStart, yAlert=yAlertStart;
	if ( frontlayer != null )
	{
		var width = frontlayer.offsetWidth;
		var wwidth = ( ie5 ? document.body.clientWidth : window.innerWidth );
		
		if ( !width ) width = wwidth / 4;
		var scrollLeft = 0;
		
		if ( document.documentElement )
			scrollLeft += document.documentElement.scrollLeft;
		else if ( document.body )
			scrollLeft += document.body.scrollLeft;
		else if ( window.pageXOffset )
			scrollLeft += window.pageYOffset;
			
		frontlayer.style.left = ( ( wwidth - width ) / 2 ) + scrollLeft + "px";

		if ( !frontlayer.offsetHeight && popupDefaultTop ) 
		{
			frontlayer.style.top = popupDefaultTop + "px";
		}
		else
		{
			var height = frontlayer.offsetHeight;
			var wheight = Math.max ( $('footer').offsetTop + $('footer').offsetHeight, ( document.documentElement.clientHeight || window.innerHeight ) );
			
			if ( maxheight && height > maxheight )
			{
				height = maxheight;
				currentOpenDialog.style.height = maxheight + "px";
			}
			else
				height = (height ? height : wheight / 4 );
				
			var scrollTop = 0;
			
			if ( document.documentElement )
				scrollTop += document.documentElement.scrollTop;
			else if ( document.body )
				scrollTop += document.body.scrollTop;
			else if ( window.pageXOffset )
				scrollTop += window.pageYOffset;
				
			frontlayer.style.top = Math.min ( popupDefaultTop, ( ( wheight - height ) / 2 - ( wheight - height ) * 0.2) ) + "px";
			backlayer.style.height = wheight + "px";
			window.scrollTo ( 0, 0 );
		}
		xAlert = parseInt(frontlayer.style.left);
		yAlert = parseInt(frontlayer.style.top);
		
	}
	else if(ie5) {
		currentOpenDialog.style.left=xAlert+document.body.scrollLeft;
		currentOpenDialog.style.top=yAlert+document.body.scrollTop;
	}
	else if(nn6||op5) {
		currentOpenDialog.style.left=xAlert+window.pageXOffset;
		currentOpenDialog.style.top=yAlert+window.pageYOffset;
	}
	moveStatus = 0;
}

// ------------------------- Drag Window stuff ------------------------------ 
var dragStartX = 0, dragStartY = 0;
var mousePosX = 0, mousePosY = 0;

var dragElement = null;
document.onmousemove = checkMove;
document.onmouseup = clearMove;

function startDrag ( element ) {
	dragElement = frontlayer;
	
	if ( dragElement )
	{
		dragStartX = mousePosX - dragElement.offsetLeft;
		dragStartY = mousePosY - dragElement.offsetTop;
	}
}


function checkMove ( event ) 
{
	mousePosX = document.all ? window.event.clientX : event.pageX;
	mousePosY = document.all ? window.event.clientY : event.pageY;

	if(dragElement != null) {
		dragElement.style.left = (mousePosX - dragStartX) + "px";
		dragElement.style.top = (mousePosY - dragStartY) + "px";
	}
}

function clearMove ( ) 
{
	dragElement = null;
}


// ---------------------- Autoupdate content automation -------------------------
var showLoading = null;
var loading = 0;

function prepareContentpanel ( updatePanelId, title )
{
	if ( $(updatePanelId) == null )
	{
		var mc = document.createElement ( "div" );
		mc.id = updatePanelId;
		mc.style.display = "none";
		
		if ( title )
			mc.title = title;
	
		$('__requestloader').appendChild ( mc );
	}
}

function removePanel ( targetPanel )
{
	if ( $(targetPanel) )
	{
		$(targetPanel).update ( );
		$(targetPanel).parentNode.removeChild ( $(targetPanel) );			
	}
}

function updatePanel ( panelid, url )
{
	if ( !url )
		url = window.location.href;
		
	url += "&" + requestSpecificDivIdParameter + "=" + panelid;
	
	new Ajax.Updater ( panelid, url );
}

function sendCommand ( panelid, commandurl, reloadurl, commandData )
{
	if ( !panelid )
		panelid = "_blank";
	
	if ( !commandurl )
		commandurl = window.location.href;

	if ( !reloadurl )
		reloadurl =	window.location.href;
		
	if ( !commandurl.toLowerCase().indexOf ( "http") == 0 )
		commandurl = "./" + commandurl;
		
	commandurl += "&" + requestExecCodeParameter + "=1" ;
	
	var showDebug = commandData && commandData.event && commandData.event.ctrlKey;
	if ( showDebug )
	{
		alert ( "Command to: " + commandurl + " for panel " + panelid );
		clipboardData.setData ( "text", commandurl );
	}	
	
	new Ajax.Request ( commandurl, {
		onSuccess: function ( response ) { 
			//if ( response.responseText == "STATUS: OK" ) 
			
			if ( showDebug )
			{
				alert ( "Response: '" + response.responseText + "' loading: " + reloadurl + "&" + requestSpecificDivIdParameter + "=" + panelid );
				clipboardData.setData ( "text", reloadurl + "&" + requestSpecificDivIdParameter + "=" + panelid );
			}	
			
			if ( commandData.onsuccessscript )
			{
				runscript ( commandData.onsuccessscript );
			}
			else if ( reloadurl && panelid )
			{
				new Ajax.Updater ( panelid, reloadurl + "&" + requestSpecificDivIdParameter + "=" + panelid, {
				onSuccess: function ( newResponse ) {
					if ( showDebug )
						alert ( "Got: " + newRespone.responseText );
					
					$(panelid).update (newRespone.responseText); 
					}
				} ); 
			}

			
		},
		onError: function ( response ) {
			if ( commandData.onerrorscript )
				eval ( commandData.onerrorscript );
			else if ( DEBUG )
				alert ( "Command failed" );
		}
	});
}

function refreshDialog ( url )
{
	if ( !url.toLowerCase().indexOf ( "http") == 0 )
		url = "./" + url;
	
	new Ajax.Updater ( currentOpenDialog.id, url );
}

// Opens a dialog by a given URL
function updateDialog( loadpanelId, url, popupData )
{	
	if ( !loadpanelId )
		loadpanelId = "_blank";	
		
	InitFrontLayer();
	if ( popupData.onCloseScript != null && frontlayer.closescript == null )
		frontlayer.closescript = popupData.onCloseScript;
		
	var params = new Object ();
	
	if ( loadpanelId != "_blank" )
		url += "&" + requestSpecificDivIdParameter + "=" + loadpanelId;

	// To avoid double ids remove possible current instance
	$('__requestloader').update ( );
	
	if ( !url.toLowerCase().indexOf ( "http") == 0 )
		url = "./" + url;
	
	var showContent = popupData.event && popupData.event.ctrlKey;
	if ( showContent )
	{
		alert ( "Calling url: " + url );
		clipboardData.setData ( "text", url );
	}

	new Ajax.Request ( url, { 
		onSuccess : function ( transport ) { 
			loading=0;
			if ( showContent ) 
				alert ( "New content: \r\n" + transport.responseText ); 
				
			removePanel ( $(loadpanelId) );
			
			if ( transport.responseText.startsWith ( "//JScript" ) )
			{
				return;
			}
			
			// Thinking the requested panel is in the response stream
			$('__requestloader').update ( transport.responseText ); 
			
			// No, the panel isn't there - create new one and get rid of the old catcher
			if ( !$(loadpanelId) )
			{
				$('__requestloader').update ();
				prepareContentpanel ( loadpanelId );
				$(loadpanelId).update ( transport.responseText ); 
			}
				
			readDialogAttributes ( $(loadpanelId), popupData );
			
			showAlert ( $(loadpanelId) ); 
			
			// Clear fetcher place
			$('__requestloader').update ();
		},
		onFailure : function ( response ) { loading=0; alert ( "Error. requesting: " + url + ": " + responst.responseText ); showDialog ( 'messagefailed' ); }
	});
	
	loading=1;
	window.setTimeout( "if ( loading==1 && currentOpenDialog == null ) showDialog ( 'messageloading' )", 3000 );
}

// Sends an form in the current dialog and refreshes it's data afterwards
function formUpdateDialog ( loadpanelId, mergeWithFormId, serviceName, postbackData )
{
	// Prepare request data
	var params = new Object();
	
	if ( serviceName )
		params.service = serviceName;
		
	if ( !postbackData )
		postbackData = new Object ();
		
	if ( postbackData.argument )
		params.value = postbackData.argument;
		
	if ( postbackData.mod )
		params.mod = postbackData.mod;
		
	if ( postbackData.doc ) 
		params.doc = postbackData.doc;
		
	if ( !loadpanelId )
		loadpanelId = "_blank";	
	
	if ( loadpanelId != "_blank" )
		params[requestSpecificDivIdParameter] = loadpanelId;

	// To avoid double ids remove possible current instance
	$('__requestloader').update ();

	var showDebug = postbackData.event && postbackData.event.ctrlKey;
	if ( showDebug )
	{
		alert ( "Calling with form data: " + $('_popupform').action + " Service: " + ( params.service ? params.service : "" ) + ( params.mod ? " Mod: " + params.mod : "" ) );
		clipboardData.setData ( "text", $('_popupform').action );
		params[requestVarDump] = 1;
	}		
	
	$('_popupform').request ( {
		parameters: params,
 		onSuccess: function ( transport )
		{ 
			loading=0;
			if ( showDebug ) 
				alert ( "New content: \r\n" + transport.responseText ); 
			
			if ( transport.responseText.startsWith ( "//JScript" ) )
			{
				return;
			}
				
			removePanel ( $(loadpanelId) );

			// Expecting the requested panel in the response stream
			$('__requestloader').update ( transport.responseText ); 
			
			// No, the panel isn't there - create new one and fill it
			if ( !$(loadpanelId) )
			{
				$('__requestloader').update ( );
				prepareContentpanel ( loadpanelId );
				$(loadpanelId).update ( transport.responseText ); 
			}
			
			readDialogAttributes ( $(loadpanelId), postbackData );
			
			showAlert ( $(loadpanelId) ); 
			
			// Clear fetcher place
			$('__requestloader').update ();
			
		},
		onFailure: function () { loading=0; hideDialog(); alert ( "Bitte versuchen Sie diese Aktion etwas später." ); }
	});

	loading=1;
	window.setTimeout( "if ( loading && currentOpenDialog == null ) showDialog ( 'messageloading' )", 3000 );		
}


function readDialogAttributes ( targetNode, resultNode )
{

	if ( resultNode && resultNode.title )
		targetNode.popuptitle = resultNode.title;
	else if ( resultNode && resultNode.popuptitle )
		targetNode.popuptitle = resultNode.popuptitle;
		
	else if ( readAttribute ( targetNode, "title" ) )
		targetNode.popuptitle = readAttribute ( targetNode, "title" );
		
	else if ( readAttribute ( targetNode, "popuptitle" ) )
		targetNode.popuptitle = readAttribute ( targetNode, "popuptitle" );

	
	if ( resultNode && resultNode.width )
		targetNode.width = resultNode.width;
		
	else if ( readAttribute ( targetNode, "width" ) )
		targetNode.width = readAttribute ( targetNode, "width" );
}

function readAttribute ( node, name )
{
	if ( isFF )
	{
		if ( node.attributes && node.attributes[name] )
			return node.attributes[name].value;
		else
			return null;
	}
	else
		return node[name];
}

//-->