var http_request;
var http_request1;
var http_request2=false;

function toPage(getval) {
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	http_request.onreadystatechange = toPageRStCh;
	http_request.open('GET', "/?httprequest=true&"+getval, true);
	http_request.send(null);
}
function toPageRStCh() {
        if (http_request.readyState == 4) {
		if (http_request.status != 200) {
			alert('Bei dem Request ist ein Problem aufgetreten.');
		} else {
			document.getElementById("clErg").innerHTML=http_request.responseText;
		}
	}
}

function aktInfoBox() {
	if (window.XMLHttpRequest) {
		http_request1 = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		http_request1 = new ActiveXObject("Microsoft.XMLHTTP");
	}
	http_request1.onreadystatechange = aktInfoBoxRStCh;
	http_request1.open('GET', "/?httprequest=true&get=infobox", true);
	http_request1.send(null);
}
function aktInfoBoxRStCh() {
        if (http_request1.readyState == 4) if (http_request1.status == 200) {
		var InfoTxt=http_request1.responseText;
		document.getElementById("InfoBox").innerHTML=InfoTxt;
		if (InfoTxt=="") setTimeout("aktInfoBox();",25000); else setTimeout("aktInfoBox();",35000);
	}
}

var ShoutBoxID=0;
var ForceFull=false;
var ShoutBoxIntervall=15;
setTimeout("forceFullRefresh();aktShoutBox();",10000);
setTimeout("if(document.getElementById('sbsec'))document.getElementById('sbsec').value='7';",100);
setTimeout("if(document.getElementById('sbsec'))document.getElementById('sbsec').value='7';",4000);
function forceFullRefresh() { ForceFull=true; setTimeout("forceFullRefresh();",(ShoutBoxIntervall+8)*3000); }
function aktShoutBox() {
	if (document.getElementById("ShoutBox")) {
		if (window.XMLHttpRequest) {
			http_request2 = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			http_request2 = new ActiveXObject("Microsoft.XMLHTTP");
		}
		http_request2.onreadystatechange = aktShoutBoxRStCh;
		var sbb="";
		if (window.location.pathname=="/shoutbox/") sbb="&sb=big";
		if (ForceFull) var sendid=0; else sendid=ShoutBoxID;
		http_request2.open('GET', "/?httprequest=true&get=shoutbox&sbid="+sendid+sbb, true);
		http_request2.send(null);
		ForceFull=false;
		setTimeout("aktShoutBox();",ShoutBoxIntervall*1000);
	}
}
function aktShoutBoxRStCh() {
        if (http_request2.readyState == 4) if (http_request2.status == 200) {
		var rtxt=http_request2.responseText;
		if (rtxt!="") {
			if (ShoutBoxID!=rtxt.substring(0,6)*1) ShoutBoxIntervall=15;
			ShoutBoxID=rtxt.substring(0,6)*1;
			rtxt=rtxt.substring(6);
			document.getElementById("ShoutBox").innerHTML=rtxt;
		}
		if (ShoutBoxIntervall<60) ShoutBoxIntervall+=5;
	}
}


var ShoutBoxIDA=999999;
setTimeout("aktShoutBoxA();",300);
function aktShoutBoxA() {
	setTimeout("aktShoutBoxA();",100);

	var sY=0;
	var mY=301;
	if (window.pageYOffset) sY=window.pageYOffset; else if (document.body.scrollTop)  sY=document.body.scrollTop;
	if (window.scrollMaxY) mY=window.scrollMaxY; else if (document.body.scrollHeight && document.body.innerHeight) mY=document.body.scrollHeight-document.body.innerHeight; else if (document.body.scrollHeight && document.body.clientHeight) mY=document.body.scrollHeight-document.body.clientHeight;

	//alert(sY+" "+mY);
	//console.log(sY,mY);

        if (http_request2==false) if ((sY>mY-300)||(ShoutBoxIDA==999999)) {
	if (document.getElementById("ShoutBoxA")) {
                if (window.XMLHttpRequest) {
                        http_request2 = new XMLHttpRequest();
                } else if (window.ActiveXObject) {
                        http_request2 = new ActiveXObject("Microsoft.XMLHTTP");
                }
                http_request2.onreadystatechange = aktShoutBoxRStChA;
                sendid=ShoutBoxIDA;
                http_request2.open('GET', "/?httprequest=true&get=shoutboxarchiv&sbmin="+sendid, true);
                http_request2.send(null);
        }}
}
function aktShoutBoxRStChA() {
        if (http_request2.readyState == 4) if (http_request2.status == 200) {
                var rtxt=http_request2.responseText;
                if (rtxt!="") {
                        xShoutBoxIDA=rtxt.substring(0,6)*1;
                        rtxt=rtxt.substring(6);
                        if (ShoutBoxIDA==999999) {
				document.getElementById("ShoutBoxA").innerHTML=rtxt;
				ShoutBoxIDA=xShoutBoxIDA;
			} else {
				//console.log(ShoutBoxIDA,xShoutBoxIDA);
				if (ShoutBoxIDA>xShoutBoxIDA) {
					document.getElementById("ShoutBoxA").innerHTML+=rtxt;
                			ShoutBoxIDA=xShoutBoxIDA;
					//cosole.log(ShoutBoxIDA);
				}
			}
		}
		http_request2=false;
        }
}



function editShoutbox(t,id) {
	txt=t.parentNode.innerHTML;
	while (txt.indexOf("<BR>")>0) txt=txt.replace(/\<BR\>/,"||");
	while (txt.indexOf("<br>")>0) txt=txt.replace(/\<br\>/,"||");
	txt=txt.replace(/<\/?[^>]+>/gi,'');
	while (txt.substring(txt.length-1,txt.length)==" ") txt=txt.substring(0,txt.length-1);
	document.getElementById("sbtxt").value=txt;
	document.getElementById("sbedit").value=id;
	document.getElementById("sbsbm").value="edit";
}

function addSearchPlugin() {
	window.external.AddSearchProvider("http://cutlist.at/suche.xml");
}

function sbNameClick(t) {
	var nsbtxt="";
	if (t.textContent) nsbtxt="@"+t.textContent+": ";
	if (t.innerText) nsbtxt="@"+t.innerText+": ";
	var st=document.getElementById("sbtxt");
	nsbtxt=nsbtxt.replace(/ \(Admin\)/,"");
	nsbtxt=nsbtxt.replace(/ \(Mod\)/,"");
	if (st.value.substring(0,1)!="@") st.value=nsbtxt+st.value; else st.value=st.value+" || "+nsbtxt;
	st.focus();
	st.value=st.value;
}
