// This is a simple AJAX Request Library (sajax)
// Feel free to copy and use it
//
// The Return from server side skript should be:
// <customtagname>options</customtagname>CONTENT
var sajaxResponse = "";
var sajaxLoaderActive = false;

//HTTP handler
function createRequestObject() 
{
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	else
		ro = new XMLHttpRequest();
	return ro;
}

var http = createRequestObject();

function sndReq(tg,idx,args) 
{
	//http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	//alert(args);
	http.open('post', 'index.php?tg='+tg+'&idx='+idx+args);
	http.onreadystatechange = handleResponse;
	http.send(null);
}

function sndFormReq(args) 
{
	http.open('post', 'index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
	http.send(args);
}

function sajaxSendForm(fname, container)
{
	if(sajaxLoaderActive) return false;
	f = $(fname);
	//alert (f.elements.length);
	argstr = Form.serialize(f);
	//alert($(fn));
	sajaxLoaderActive = true;
	if(container == null || container == 'undefined' || container == '')
		container = "sajaxPopupContent";
	$(container).innerHTML = $("loaderImage").innerHTML;
	centerSajaxPopup();
	sndFormReq(argstr);
}

function handleResponse() 
{
	if(http.readyState == 4)
	{
		sajaxResponse = http.responseText;
		setContent();
	}
}

// Den Inhalt des Containers parsen und setzen
function setContent()
{
	sajaxOutputContainer = null;
	output_string = sajaxResponse;
	var jsrun = new Array();
	//alert (output_string);
	
	// Wohin mit dem Output
	target_search = /<TARGET>(.+)<\/TARGET>/gi;
	target_match = sajaxResponse.match(target_search);
	//alert (target_match);
	if (target_match)
	{
		target_string = RegExp.$1;
		sajaxOutputContainer = $(target_string);
		output_string = output_string.replace(target_match,"");
	}
	
	// Gibt es Actions
	action_search = /<ACTION>(.+)<\/ACTION>/gi;
	action_match = sajaxResponse.match(action_search);
	//alert(action_match);
	if (action_match)
	{
		action_string = RegExp.$1;
		//alert(action_string);
		output_string = output_string.replace(action_match,"");
		actions = action_string.split("|*|");
		for(i = 0; i < actions.length; i++)
		{
			ac = actions[i].split(":");
			if(ac[0] == "js") jsrun[i] = ac[1];
		}
	}
	
	if(sajaxOutputContainer == null || sajaxOutputContainer == "undefined")
		alert("No output target specified!");
	else
		sajaxOutputContainer.innerHTML = output_string;
		
	sajaxResponse = "";
	sajaxLoaderActive = false;
	
	// Soll danach eine JS Funktion ausgefuehrt werden?
	for(i = 0; i < jsrun.length; i++)
	{
		//alert(jsrun[i]);
		eval(jsrun[i]);
	}
	
	if($("sajaxPopupContainer").style.display != "none")
		centerSajaxPopup();
}

function switchContainer(name, tg, idx, args, force)
{
	if(sajaxLoaderActive) return false;
	if($(name+"_container").style.display == "none" || force == "open")
	{
		$(name+"_container").style.display = "";
		if($(name+"_openImage")) $(name+"_openImage").style.display = "none";
		if($(name+"_closeImage")) $(name+"_closeImage").style.display = "";
		$(name+"_content").innerHTML = $("loaderImage").innerHTML;
		sajaxLoaderActive = true;
		sndReq(tg,idx,args);
	}
	else
	{
		$(name+"_container").style.display = "none";
		if($(name+"_openImage")) $(name+"_openImage").style.display = "";
		if($(name+"_closeImage")) $(name+"_closeImage").style.display = "none";
	}
}

function showSajaxPopup(tg, idx, args)
{
	if(sajaxLoaderActive) return false;
	$("sajaxPopupContainer").style.display = "";
	$("sajaxPopupContent").innerHTML = $("loaderImage").innerHTML;
	sajaxLoaderActive = true;
	sndReq(tg,idx,args);
	centerSajaxPopup();
}

function centerSajaxPopup()
{
	wd = $("sajaxPopupContainer").offsetWidth;
	ht = $("sajaxPopupContainer").offsetHeight;
	var x,y,xo,yo;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth; y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth; y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth; y = document.body.clientHeight;
	}
	
	if (self.pageYOffset) // all except Explorer
	{
		xo = self.pageXOffset; yo = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		xo = document.documentElement.scrollLeft; yo = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		xo = document.body.scrollLeft; yo = document.body.scrollTop;
	}
	
	l = Math.round((x - wd) / 2) + xo; t = Math.round((y - ht) / 2) +yo;
	
	$("sajaxPopupContainer").style.top = t + "px";
	$("sajaxPopupContainer").style.left = l + "px";
}

function closeSajaxPopup()
{
	$("sajaxPopupContainer").style.display = "none";
}

function sajaxReloadWindow()
{
	self.location.reload();
}
