var pajaxelement = null;
var pajaxRequest = function(u, m, s, elementid)
{
	this.url		= u;
	this.method		= m || "GET";
	this.async		= s || true;
	this.body		= null;
	this.head		= false;
	this.elementid	= elementid || "ajaxcont";
	
	var _this = this;
	
	try {
		this.request = new XMLHttpRequest();	
	}catch(e){
		try {
			this.request = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			this.request = false;	
		}
	}

	this.doRequest = function()	{
		if(!this.url)		{
			this.onError("keine URL gesetzt");
			return false;
		}
		if(!this.method)		{
			this.method = "GET";
		}else{
			this.method = this.method.toUpperCase();
		}
		if(!this.request){
			this.onError("kein Verbindungsobjekt gesetzt");
			return false;
		}
		this.request.open(this.method, this.url, this.async);
		
		if(this.method == "POST"){
			this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
		}
		else this.request.setRequestHeader("Content-Type", "text/html; charset=utf-8");
		if(this.head){
			for(var i=0; i<this.head.length; i+=2){
				this.request.setRequestHeader(this.head[i], this.head[i+1]);
			}
		}
		this.request.onreadystatechange = this.checkState;
		this.request.send(this.body);
		return true;
	}
	
	this.onSuccess = function(txt, xml){
		document.getElementById(this.elementid).innerHTML = txt;
	}
	
	this.onError = function(msg){
		alert("Fehler: "+msg);
	}
	
	this.checkState = function(){
		if(_this.request.readyState<4){
			document.getElementById(_this.elementid).innerHTML = 'Daten werden geladen ...';
			return false;
		}
		if(_this.request.status==200 || _this.request.status == 304){
			_this.onSuccess(_this.request.responseText, _this.request.responseXML);
		}else{
			_this.onError("Fehler bei der Datenuebertragung"+_this.url);
		}
		return true;
	}
}

function pajax(url,elid,async,completepath, move,elid2){
	if(move) pajaxelement = elid;
	elid = elid || "ajaxcont";
	elid2 = elid2 || elid;
	async = async || false;
	completepath = completepath || false;
	var BasePath = decodeURIComponent( document.location.pathname.substr(1) ) ;
	BasePath = BasePath.replace( /\\/gi, '/' ) ;
	BasePath = BasePath.substring(0,BasePath.lastIndexOf('/')+1) ;
	//var elid2 = elid;
	var ajelement 		= document.getElementById(elid2).style;
	ajelement.display 	= 'block';
	if(completepath) BasePath = '';
	var pajax = new pajaxRequest('/'+BasePath+url,"GET",true,elid);
	pajax.doRequest();
	//while(pajax.request.readyState < 4) ;
}

function pajax_add(url,elid,async,completepath, move,elid2, closeadd){
	if(move) pajaxelement = elid;
	elid = elid || "ajaxcont";
	elid2 = elid2 || elid;
	closeadd = closeadd || false;
	async = async || false;
	ajax1 = document.getElementById(elid);
	ajax2 = document.getElementById(elid2);
	if(ajax2 == null){
		var ajax2 = document.createElement("div");
		ajax2 = document.body.appendChild(ajax2);
		ajax2.className = 'ajaxbackground';
		//ajax2.id = elid2;
		ajax2.id = 'ajaxbackground';
		ajax2.style.position = 'fixed';
	}else document.getElementById('ajaxbackground').style.display = 'block';
	//document.body.style.position = 'absolute';
	if(ajax1 == null){
		var newDiv = document.createElement("div");
		ajaxdiv = ajax2.appendChild(newDiv);
		ajaxdiv.className = 'ajaxbox';
		//ajaxdiv.id = elid;
		ajaxdiv.id = 'pajaxboxmain';
		ajaxdiv.style.display = 'block';
		ajaxdiv.innerHTML = '<div class="ajax_head" id="'+elid2+'"></div><div class="ajax_body" id="'+elid+'"></div>';
	}
	if(closeadd){
		var closeDiv = document.createElement("div");
		closeDiv = document.getElementById(elid2).appendChild(closeDiv);
		closeDiv.className = 'closeajax';
		closeDiv.id = 'close'+elid2;
		//closeDiv.innerHTML = '<img onclick="document.getElementById(\''+elid2+'\').style.display=\'none\'" style="cursor: pointer" src="/images/cancel.gif" alt="x" />';
		closeDiv.innerHTML = '<img onclick="document.getElementById(\'ajaxbackground\').style.display=\'none\'" style="cursor: pointer" src="/images/cancel.png" alt="x" />';
	}
	pajax(url,elid,async,completepath, move,elid2);
}

function moveit(e){
	if(pajaxelement != null){
		if (!e)	e = window.event;
	  	if(document.getElementById){
	  		var scrollx = 0;
	  		var scrolly = 0;
	  		if (navigator.appName.indexOf("Explorer") != -1){
	  			scrollx = document.documentElement.scrollLeft;
	  			scrolly = document.documentElement.scrollTop;
	  		}
	  		else{
	  			scrollx = pageXOffset;
	  			scrolly = pageYOffset;
	  		}
	  		var mousepos = e.clientY - 470;
	  		if(mousepos < 0) mousepos = 0;
		    document.getElementById(pajaxelement).style.left  = e.clientX+scrollx+  10 + "px";
		    document.getElementById(pajaxelement).style.top = mousepos+scrolly+"px";
		}else if(document.all){
			document.all.pajaxelement.style.left = e.clientX + 10;
			document.all.pajaxelement.style.top = e.clientY + 10;
		}
	}
}
function pajaxpost(formid, query){
	query = '?'+query;
	
	var BasePath = decodeURIComponent( document.location.pathname.substr(1) ) ;
	BasePath = BasePath.replace( /\\/gi, '/' ) ;
	BasePath = BasePath.substring(0,BasePath.lastIndexOf('/')+1) ;
	
	//Formular auslesen
	//EditorUpdate();
	var form = document.getElementById(formid);
	var inputs = form.getElementsByTagName('input');
	var texts = form.getElementsByTagName('textarea');
	var selects = form.getElementsByTagName('select');
	//alert(form.getElementsByTagName('input')[0].value);
	var postdata = '';
	for(i=0;i<inputs.length;i++){
		if(postdata == '') postdata = escape(inputs[i].name)+'='+escape(inputs[i].value);
		else postdata += '&'+escape(inputs[i].name)+'='+escape(inputs[i].value);
	}
	for(i=0;i<selects.length;i++){
		if(postdata == '') postdata = escape(selects[i].name)+'='+escape(selects[i].value);
		else postdata += '&'+escape(selects[i].name)+'='+escape(selects[i].value);
	}
	for(i=0;i<texts.length;i++){
		if(postdata == '') postdata = escape(texts[i].name)+'='+escape(input[i].innerHTML);
		else postdata += '&'+escape(texts[i].name)+'='+escape(texts[i].innerHTML);
	}
	
	var ajax = new pajaxRequest('/'+BasePath+'index.php'+query+'&postajax=1',"POST",true,'pajaxbox');
	//alert(escape(postdata));
	
	ajax.body=postdata;
	ajax.doRequest();
	return false;
}

function check_flash(){
	for (var ems = document.embeds, i = 0, em; em = ems[i]; i++) {
		em.setAttribute('wmode', 'transparent');
		var nx = em.nextSibling, pn = em.parentNode;
		pn.removeChild(em);
		pn.insertBefore(em, nx);
	}
}
check_flash();
//document.onmousemove = moveit;