/*
	JS-Funktionen für update parts

	29.03.2007, mJ:	loadXMLFunc() mit function als Argument

	28.05.2008, mJ:	loadXMLDocScript(), 
					führt Javascript im nachgeladenen Teil aus

*/


//	"Konstanten" (Kann MSIE 6 aber nicht)

var		RSS_LOADED	=    4,
		RS_OK		=  200,
		TIMEOUT		= 1000;


//	Globale Variablen für Request/Callback-Kommunikation

var IS_IE = false;
var REQ;
var	UPDATE_URL;

var INTO_DIV;



function launchJavascript( responseText ) {
// RegExp from prototype.sonio.net
	var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:</'+'script>)';
			
	var match    = new RegExp(ScriptFragment, 'img');
	var scripts  = responseText.match(match);
	
	if(scripts) {
		var js = '';
		for(var s = 0; s < scripts.length; s++) {
			var match = new RegExp(ScriptFragment, 'im');
//			js += scripts[s].match(match)[1];
			var part = scripts[s].match(match)[1];
//			alert( part );
			eval( part );
		}
		
//		alert( js );
		
//		eval(js);
	}
}				


function updateCallback()
{
	if( REQ.readyState == RSS_LOADED ) {
		if( REQ.status == RS_OK ) {
//			alert( "callback: " + REQ.readyState );
// 			content = document.getElementById( "contentdiv" );
// 			content.innerHTML = REQ.responseText;
			if( INTO_DIV && INTO_DIV.innerHTML ) {
				INTO_DIV.innerHTML = REQ.responseText;
			} else {
				alert( "Failed to put '" + REQ.responseText + "' into '" + INTO_DIV + "'" );
			}
		//	alert( "REQ:" + REQ );
		} else {
			alert(	 "There was a problem retrieving the XML data:\n" +	REQ.statusText );
		}
	}
}


// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file

function loadXMLDoc( url, id, pre_message )
{
	// branch for native XMLHttpRequest object
	div = document.getElementById( id );
	if( div ) {
		if( pre_message ) {
			div.innerHTML = pre_message;
		}
	} else {
		alert( "Div id '" + id + "' not found!" );
		return;
	}	
	INTO_DIV = div;	
	
	if( window.XMLHttpRequest ) {
		REQ = new XMLHttpRequest();
		REQ.onreadystatechange = updateCallback;
		REQ.open( "GET", url, true );
		REQ.send( null );
	// branch for IE/Windows ActiveX version
	} else if( window.ActiveXObject ) {
		IS_IE = true;
		REQ = new ActiveXObject( "Microsoft.XMLHTTP" );
		if( REQ ) {
			REQ.onreadystatechange = updateCallback;
			REQ.open( "GET", url, true );
			REQ.send();
		}
	}
}


function updateCallbackScript()
{
	if( REQ.readyState == RSS_LOADED ) {
		if( REQ.status == RS_OK ) {
//			alert( "callback: " + REQ.readyState );
// 			content = document.getElementById( "contentdiv" );
// 			content.innerHTML = REQ.responseText;
			if( INTO_DIV && INTO_DIV.innerHTML ) {
				INTO_DIV.innerHTML = REQ.responseText;
				launchJavascript( REQ.responseText );
			} else {
				alert( "Failed to put '" + REQ.responseText + "' into '" + INTO_DIV + "'" );
			}
		//	alert( "REQ:" + REQ );
		} else {
			alert(	 "There was a problem retrieving the XML data:\n" +	REQ.statusText );
		}
	}
}


// Javascript executing version

function loadXMLDocScript( url, id, pre_message )
{
	// branch for native XMLHttpRequest object
	div = document.getElementById( id );
	if( div ) {
		if( pre_message ) {
			div.innerHTML = pre_message;
		}
	} else {
		alert( "Div id '" + id + "' not found!" );
		return;
	}	
	INTO_DIV = div;	
	
	if( window.XMLHttpRequest ) {
		REQ = new XMLHttpRequest();
		REQ.onreadystatechange = updateCallbackScript;
		REQ.open( "GET", url, true );
		REQ.send( null );
	// branch for IE/Windows ActiveX version
	} else if( window.ActiveXObject ) {
		IS_IE = true;
		REQ = new ActiveXObject( "Microsoft.XMLHTTP" );
		if( REQ ) {
			REQ.onreadystatechange = updateCallbackScript;
			REQ.open( "GET", url, true );
			REQ.send();
		}
	}
}


var	UPDATE_FUNC;

function functionCallback()
{
	if( REQ.readyState == RSS_LOADED ) {
		if( REQ.status == RS_OK ) {
			if( UPDATE_FUNC ) {
				UPDATE_FUNC( REQ.responseText );
			}
		} else {
			alert(	 "There was a problem retrieving the XML data:\n" +	REQ.statusText );
		}
	}
}


function loadXMLFunc( url, func )
{
	// branch for native XMLHttpRequest object
	if( window.XMLHttpRequest ) {
		REQ = new XMLHttpRequest();
		UPDATE_FUNC = func;
		REQ.onreadystatechange = functionCallback;
		REQ.open( "GET", url, true );
		REQ.send( null );
	// branch for IE/Windows ActiveX version
	} else if( window.ActiveXObject ) {
		IS_IE = true;
		REQ = new ActiveXObject( "Microsoft.XMLHTTP" );
		if( REQ ) {
			UPDATE_FUNC = func;
			REQ.onreadystatechange = functionCallback;
			REQ.open( "GET", url, true );
			REQ.send();
		}
	}
}


