var titlei=0;
var sound=0;
var color='#ffffff';
var autoscroll=0;


function startchat(){
config("0",0,0);
getChat(1);
setInterval('getChat(0)', 500);
}

function sendMsg(id) {
var Ausgabe = document.getElementById( "Ausgabe");
var http = null;
if (window.XMLHttpRequest) {
http = new XMLHttpRequest();
} else if (window.ActiveXObject) {
http = new ActiveXObject("Microsoft.XMLHTTP");
}


if (http != null) {
document.getElementById("submit").value="sende...";
http.open("POST", "index.php?getchat", true);
http.onreadystatechange = ausgeben;
http.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded");

var wert=document.Chat.message.value;
http.send('a='+wert+'&color='+color);
}

function ausgeben() {

if (http.readyState == 4) {
document.Chat.message.value="";
if( http.responseText.length !=1){
Ausgabe.innerHTML =http.responseText;
if(autoscroll==0){
Ausgabe.scrollTop = Ausgabe.scrollHeight;}
}
document.getElementById("submit").value="Abschicken";
}
}
}

function getChat(nr) {
Ausgabe=document.getElementById("Ausgabe");
var httpg = null;
if (window.XMLHttpRequest) {
httpg = new XMLHttpRequest();
} else if (window.ActiveXObject) {
httpg = new ActiveXObject("Microsoft.XMLHTTP");
}


if (httpg != null) {
httpg.open("POST", "index.php?getchat", true);
httpg.onreadystatechange = ausgeben2;
httpg.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded");
httpg.send("o="+nr);

}

function ausgeben2() {
if (httpg.readyState == 4 && httpg.responseText.length !=1) {
Ausgabe.innerHTML =httpg.responseText;
if(autoscroll==0 || nr==1){
Ausgabe.scrollTop = Ausgabe.scrollHeight;}
if(nr==0){
if(titlei!=0){clearInterval(titleinter);}
if(sound!=0){document.getElementById("sound").innerHTML='<embed src="/include/sounds/newmessage.wav" autostart="true" loop="false" hidden="true" height="0" width="0">';}
blinktitle();
titleinter= setInterval('blinktitle()',500);
}
}
}
}


function getOnline() {
var online = document.getElementById( "online" );
var httpg = null;
if (window.XMLHttpRequest) {
httpg = new XMLHttpRequest();
} else if (window.ActiveXObject) {
httpg = new ActiveXObject("Microsoft.XMLHTTP");
}


if (httpg != null) {
httpg.open("POST", "index.php?onlinechat", true);
httpg.onreadystatechange = ausgeben2;
httpg.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded");
httpg.send();
}

function ausgeben2() {
if (httpg.readyState == 4 && online.innerHTML.length != httpg.responseText.length) {
online.innerHTML =httpg.responseText;
}
}
}



function setOnline() {
var httpg = null;
if (window.XMLHttpRequest) {
httpg = new XMLHttpRequest();
} else if (window.ActiveXObject) {
httpg = new ActiveXObject("Microsoft.XMLHTTP");
}


if (httpg != null) {
httpg.open("POST", "/index.php?setonline", true);
httpg.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded");
httpg.send();
}
}

function touser(uid){
document.Chat.message.value="/say "+uid+" "+document.Chat.message.value;
}

function  entersend (event)  {
 var key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

if(key==13){sendMsg();}
}

function blinktitle(){
if(titlei%2==0){
document.title="[@ ]Neue Nachricht";
}
if(titlei%2!=0){
document.title="[ @]Neue Nachricht";
}
titlei=parseInt(titlei)+1;
if(titlei==10){
clearInterval(titleinter);
document.title="TDF Clan :: Chat";
titlei=0;
}
}

function config(setcolor,setsound,setautoscroll) {
var http = null;
if (window.XMLHttpRequest) {
http = new XMLHttpRequest();
} else if (window.ActiveXObject) {
http = new ActiveXObject("Microsoft.XMLHTTP");
}


if (http != null) {
http.open("POST", "index.php?chatconfig", true);
http.onreadystatechange = getconfig;
http.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded");

var wert=document.Chat.message.value;
http.send('color='+setcolor+'&sound='+setsound+'&autoscroll='+setautoscroll);
}

function getconfig() {
if (http.readyState == 4) {
var configs=http.responseText.split(',');
color=configs[0];
if(configs[0]!=""){
document.getElementById("message").style.color=configs[0];}
sound=configs[1];
if(configs[1]==0){
document.sound.src="include/images/icons/sound_mute.png";}
else{
document.sound.src="include/images/icons/sound.png";}
autoscroll=configs[2];
if(configs[2]==0){
document.autoscroll.src="include/images/icons/bullet_arrow_down.png";}
else{
document.autoscroll.src="include/images/icons/bullet_arrow_bottom.png";}

}
}
}

function user(setstatus) {
var http = null;
if (window.XMLHttpRequest) {
http = new XMLHttpRequest();
} else if (window.ActiveXObject) {
http = new ActiveXObject("Microsoft.XMLHTTP");
}


if (http != null) {
http.open("POST", "index.php?chatuserconfig", true);
http.onreadystatechange = getuserconfig;
http.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded");

var wert=document.Chat.message.value;
http.send('status='+setstatus);
}

function getuserconfig() {
if (http.readyState == 4) {
}
}
}