var $WaitLay = null;
var $ErrorLay = null;

if (window.attachEvent) {window.attachEvent("onload", InitAjaxWait); } else { window.addEventListener('load', InitAjaxWait, false); }


function CreateAjaxObject($Successor)
{
var req = null;

try{
req = new XMLHttpRequest();
}
catch (ms){
try{
req = new ActiveXObject("Msxml2.XMLHTTP");
} 
catch (nonms){
try{
req = new ActiveXObject("Microsoft.XMLHTTP");
} 
catch (failed){
req = null;
}
} 
}
var $Success = $Successor;
req.onreadystatechange = function(){RequestReady(req, $Success);}
return req;
}

function RequestReady(Requester, SuccessFunc)
{
//if (Requester.readyState == 1) { if (window.ShowAjaxNotification) { ShowAjaxNotification(); } }

if (Requester.readyState == 4) 
	{ 	//if (window.HideAjaxNotification) { HideAjaxNotification(); }
		if (Requester.status == 200) { window[SuccessFunc](Requester); }	
				else   { window.alert("Hier ist leider gerade etwas schief gelaufen!"); }
	}
}

function InitAjaxWait()
{
$WaitLay = document.createElement("div");
$WaitLay.innerHTML = '<img src="/images/ajax.gif"><br>Einen Moment bitte...';
$WaitLay.className = "AjaxWait";

document.body.appendChild($WaitLay);
CenterWindow($WaitLay);

$ErrorLay = document.createElement("div");
$ErrorLay.innerHTML = 'dfgd';
$ErrorLay.className = "AjaxWait";
$ErrorLay.style.width = "450px";
$ErrorLay.style.height= "300px";
document.body.appendChild($ErrorLay);
}

function ShowAjaxNotification()
{
CenterWindow($WaitLay);
$WaitLay.style.visibility = "visible";
}

function HideAjaxNotification()
{
$WaitLay.style.visibility = "hidden";
}


function GetFormData(Formular)
{
var Data = "";
for ($i=0; $i < document.forms[Formular].elements.length; $i++)
	{ 	if (document.forms[Formular].elements[$i].type != 'checkbox')
				{ Data = Data + encodeURIComponent(document.forms[Formular].elements[$i].name) + '=' + encodeURIComponent(document.forms[Formular].elements[$i].value) + '&'; }
		else	{ if (document.forms[Formular].elements[$i].checked == true) Data = Data + encodeURIComponent(document.forms[Formular].elements[$i].name) + '=' + encodeURIComponent(document.forms[Formular].elements[$i].value) + '&';
				}
	}
return Data;
}

function CheckForm(Formular, CheckUrl, OptionReset)
{
OptionReset = OptionReset || 'CheckFormAnswer';

var Request = CreateAjaxObject(OptionReset);
Request.open("POST", CheckUrl, true);

Request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
Request.send(GetFormData(Formular));
}

function CheckFormAnswer(Requester)
{
$Antwort = Requester.responseText;
if ($Antwort.substr(0, 1) == 1) { window.location.href = $Antwort.substr(1); }
if ($Antwort.substr(0, 1) == 0) { $ErrorLay.innerHTML = $Antwort.substr(1);
				  CenterWindow($ErrorLay);
				  $ErrorLay.style.visibility = "visible";
 				}
}

function CloseFormError()
{
$ErrorLay.innerHTML = "";
$ErrorLay.style.visibility = "hidden";
}

function GetMail(Mail)
{
var Request = CreateAjaxObject('CheckFormAnswer');
Request.open("POST", '/check/decodemail.php', true);
Request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
Request.send('mail='+Mail);
}

function CenterWindow($MyWindow)
{
var $ScrolledX 	= window.pageXOffset || document.body.scrollLeft;
var $ScrolledY 	= window.pageYOffset || document.body.scrollTop;

var $BodyX		= document.body.clientWidth || window.innerWidth;
var $BodyY		= document.body.clientHeight || window.innerHeight;

var $PosX		= Math.floor($ScrolledX + (($BodyX - $MyWindow.offsetWidth) / 2));
var $PosY		= Math.floor($ScrolledY + (($BodyY - $MyWindow.offsetHeight) / 2));
$MyWindow.style.top = $PosY;
$MyWindow.style.left= $PosX;
}