var xmlHttp

function showComment(id){
	 if( document.getElementById("txtHint"+id).innerHTML != ""){
	 document.getElementById("txtHint"+id).innerHTML ="";
	 } else {
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
		  alert ("Your browser does not support AJAX!");
		  return;
		  } 
		var url="/_ajax/showcomment.php";
		url=url+"?berid="+id;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=new Function("fx",'stateChanged("'+id+'")');;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
} 

function sendComment(id){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var id = document.getElementById(id).berid.value;
	var name = document.getElementById(id).name.value;
	var title = document.getElementById(id).titel.value;
	var kommentar = document.getElementById(id).kommentar.value;
	var captcha = document.getElementById(id).captcha.value;
	kommentar = kommentar.replace(/\n/g,"<br>");
	var url="/_ajax/showcomment.php";
	url=url+"?berid="+id;
	url=url+"&act=sv";
	url=url+"&name="+name;
	url=url+"&title="+title;
	url=url+"&kommentar="+kommentar;
	url=url+"&captcha="+captcha;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=new Function("fx",'stateChangedAdd("'+id+'")');;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function loginUser(){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
	  alert ("Your browser does not support AJAX!");
	  return;
	 } 
	var user = document.loginform.username.value;
	var pass = document.loginform.password.value;
	//document.getElementById("login").style.visibility = 'hidden';
	var url="/_ajax/checkuser.php";
	url=url+"?user="+user;
	url=url+"&pass="+pass;
	xmlHttp.onreadystatechange=new Function("fx",'loggedIn("'+user+'")');;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function logoutUser(){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
	  alert ("Your browser does not support AJAX!");
	  return;
	 } 
	var url="/_ajax/checkuser.php";
	url=url+"?act=logout";
	xmlHttp.onreadystatechange=loggedOut;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged(id) { 
	if (xmlHttp.readyState==4){ 
		document.getElementById("txtHint"+id).innerHTML=xmlHttp.responseText;
	}
}

function stateChangedAdd(id) { 
	if (xmlHttp.readyState==4){ 
		var text = document.getElementById("com"+id).innerHTML;
		var count = text.substring(12, text.length-1);
		count++;
		document.getElementById("com"+id).innerHTML="Kommentare ("+count+")";
		document.getElementById("txtHint"+id).innerHTML=xmlHttp.responseText;
	}
}


function loggedOut() { 
	if (xmlHttp.readyState==4){ 
		document.getElementById("succmsg").innerHTML = "Sie haben sich erfolgreich<br>ausgeloggt.";
		document.getElementById("formhide").style.display = "block";
	}
}

function loggedIn(user) { 
	if (xmlHttp.readyState==4){ 
		if(xmlHttp.responseText!=""){
			document.getElementById("succmsg").innerHTML = "&nbsp;angemeldet als <br>&nbsp;<b>User:</b> "+user+"<br>&nbsp;<b>Gruppe:</b> "+xmlHttp.responseText+"<br><a href='/community/home.php'>Zentrale</a> | <a href='javascript:logoutUser()'>Logout</a>";
			document.getElementById("formhide").style.display = "none";
		} else {
			document.getElementById("succmsg").innerHTML = "Login fehlgeschlagen.";
		}
	}
}

function chgTabelle(type){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
	  alert ("Your browser does not support AJAX!");
	  return;
	 } 
	var url="/_ajax/tabelle.php";
	url=url+"?type="+type;
	xmlHttp.onreadystatechange=new Function("fx",'changedTabelle("'+type+'")');
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}
function changedTabelle(type) { 
	if (xmlHttp.readyState==4){ 
		if(xmlHttp.responseText!=""){
			document.getElementById("tblarea").innerHTML = xmlHttp.responseText;
			if(type=="e"){
				document.getElementById("reit1link").innerHTML = "<b>Erste</b>";
				document.getElementById("reit2link").innerHTML = "U23";
			} else {
				document.getElementById("reit1link").innerHTML = "Erste";
				document.getElementById("reit2link").innerHTML = "<b>U23</b>";			
			}
		} else {
			document.getElementById("tblarea").innerHTML = "Fehler bei der Übertragung.";
		}
	}
}

function chgGm(id,valA,valB,valC,textA,textB,textC){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
	  alert ("Your browser does not support AJAX!");
	  return;
	 } 
	switch(id){
		case 1: val = valA;
				text = textA;
				break;
		case 2: val = valB;
				text = textB;
				break;
		case 3: val = valC;
				text = textC;
				break;
	}
	if(textA == ""){
		textA = 99;
	}
		if(textB == ""){
		textB = 99;
	}
		if(textC == ""){
		textC = 99;
	}
	var url="/_ajax/nextgame.php";
	url=url+"?id="+val;
	url=url+"&pos="+id;
	url=url+"&text="+text;
	xmlHttp.onreadystatechange=new Function("fx",'changedGame('+id+','+valA+','+valB+','+valC+','+textA+','+textB+','+textC+')');
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}
function changedGame(id,valA,valB,valC,textA,textB,textC) { 
	if (xmlHttp.readyState==4){ 
		if(xmlHttp.responseText!=""){
			document.getElementById("gmarea").innerHTML = xmlHttp.responseText;
			
			if(textA == 99){
				textA = 'FS';
			} else if (textA == 88) {
				textA = 'CUP';
			} else {
				textA = textA+'. Rd';
			}
			
			if(textB == 99){
				textB = 'FS';
			} else if (textB == 88) {
				textB = 'CUP';
			} else {
				textB = textB+'. Rd';
			}
			if(textC == 99){
				textC = 'FS';
			} else if (textC == 88) {
				textC = 'CUP';
			} else {
				textC = textC+'. Rd';
			}		
			switch(id){
				case 1: if(document.getElementById("gm1link") != null){
						document.getElementById("gm1link").innerHTML = "<B>"+textA+"</B>";
						}
						if(document.getElementById("gm2link") != null){
							document.getElementById("gm2link").innerHTML = textB;
						}
						if(document.getElementById("gm3link") != null){
							document.getElementById("gm3link").innerHTML = textC;
						}
						break;
				case 2: if(document.getElementById("gm1link") != null){
							document.getElementById("gm1link").innerHTML = textA;
						}
						if(document.getElementById("gm2link") != null){
						document.getElementById("gm2link").innerHTML = "<B>"+textB+"</B>";
						}
						if(document.getElementById("gm3link") != null){
							document.getElementById("gm3link").innerHTML = textC;
						}
						break;
				case 3: if(document.getElementById("gm1link") != null){
							document.getElementById("gm1link").innerHTML = textA;
						}
						if(document.getElementById("gm2link") != null){
						document.getElementById("gm2link").innerHTML = textB;
						}
						if(document.getElementById("gm3link") != null){
						document.getElementById("gm3link").innerHTML = "<B>"+textC+"</B>";
						}
						break;
			}
		} else {
			document.getElementById("gmarea").innerHTML = "Fehler bei der Übertragung.";
		}
	}
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	} catch (e) {
	// Internet Explorer
		try {
		    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e){
		    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
