var last_request=0;                          /* Letzter Request               */
var last_class=0;                            /* Letzte  Klasse                */
var aktiv = setInterval("loadData()", loadInterval); /* Lade Einträge alle x Sekunden */
setTimeout('loadData();',1000);              /* Lade Einträge der Shoutbox    */
var dosProtect = 0;   /* Setzen der Vaiable damit der Chat nicht zum DOS wird */


// Funktion um zur letzten Nachricht zu scrollen
function Scrolling(){
 if(document.getElementById('autoscroll')){
  if(document.getElementById('autoscroll').checked){
   var cdiv=document.getElementById('asb_contentwrap');
   cdiv.scrollTop=cdiv.scrollHeight;
}}}

// XML HttpRequest-Instanz erstellen
var xmlHttp = false;
 try{ xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP"); } /* für IE < Version 6 */
 catch(e){
     try{ xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP"); }
     catch(e){ xmlHttp  = false; }
 }
 /* für Mozilla, Opera, Safari usw. */
 if (!xmlHttp  && typeof XMLHttpRequest != 'undefined'){ xmlHttp = new XMLHttpRequest(); }

 
// Funktion zum Laden der Einträge
function loadData(){
 asb_loadingChange(true);
 if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){ /* Wenn Status entsprechend führe Aktion durch */
  xmlHttp.open('GET', webpath+'getdata.php?last_request='+last_request+'&last_class='+last_class, true); /* Führe Aktion aus */
  xmlHttp.onreadystatechange = function (){ /* Sobald sich der Zustand ändert führe Aktion aus */
   if (xmlHttp.readyState == 4){
    dosProtect = dosProtect + 1; /* Dos Variable erhöhen */
    var response=xmlHttp.responseText;

	if (response.length>0){
	 zeilen=response.split("<br />"); /* in erster Zeile steht der Timestamp -> extrahieren */
	 last_request=zeilen[0];
     last_class=zeilen[1];
	 var anzahl_zeilen=zeilen.length;
	 for (i=2;i<anzahl_zeilen;i++){
      if(reihenfolge_eintraege=='ASC')
        document.getElementById("asb_content").innerHTML+=zeilen[i]; 
      else if(reihenfolge_eintraege=='DESC') 
        document.getElementById("asb_content").innerHTML=zeilen[i]+document.getElementById("asb_content").innerHTML;
	  if(i<anzahl_zeilen-1) 
        document.getElementById("asb_content").innerHTML+='<br />';
	 }
     if(reihenfolge_eintraege=='ASC'){ /* Zum Ende Scrollen */
        setTimeout('Scrolling();',1);
        setTimeout('Scrolling();',750);
        setTimeout('Scrolling();',1500);
     }
	}

    if(dosProtect >= dosProtect_value){ /* Meldung nach 50 Aufrufen ohne Interaktion */
      var dosProtectText = '<div class="even">Ladelimit wurde erreicht.';
      var dosProtectText = dosProtectText + '<br />Bitte Seite neu laden ...</div>';
      document.getElementById("asb_content").innerHTML = dosProtectText;
      clearInterval(aktiv);
  }}}
 xmlHttp.send(null); }
 setTimeout('asb_loadingChange(false);',1000); /* asb_loading Anzeige beenden */
}




// Funktion zum Speichern der Einträge
function sendComment(){
 asb_loadingChange(true);
 if (xmlHttp){
  var txtMessage = encodeURIComponent(document.frmshoutbox.txtmessage.value);
  var txtName    = encodeURIComponent(document.frmshoutbox.txtname.value);
  if(use_captcha) var captcha_code    = encodeURIComponent(document.frmshoutbox.captcha_code.value);
  else var captcha_code='';
  if (txtMessage == '' || txtName == '')
    alert("Bitte fuelle Name und Nachricht aus");
  else{
   if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
    xmlHttp.open('POST', webpath+'setdata.php');
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send('message='+txtMessage+"&name="+txtName+"&last_request="+last_request+"&captcha_code="+captcha_code);
    
    xmlHttp.onreadystatechange = function (){ /* Sobald sich der Zustand ändert führe Aktion aus */
     if (xmlHttp.readyState == 4){
       var response=xmlHttp.responseText;
       if(response=='false') {
           alert("Bestätigungs-Code falsch!");
           document.frmshoutbox.captcha_code.value = '';
           document.frmshoutbox.captcha_code.focus();
       }
       else {
           document.frmshoutbox.txtmessage.value = ''; /* Leere das Eingabefeld */
           document.frmshoutbox.txtmessage.focus();    /* Cursor in das Textfeld setzen */
       }
       
       if(use_captcha){
           document.getElementById("captcha_pic").src=webpath+'captcha.php?'+Math.random();
           document.frmshoutbox.captcha_code.value = '';
       }
       dosProtect = 0;
       clearInterval(aktiv);
       aktiv = setInterval("loadData()", loadInterval);
       setTimeout('loadData();',1000);
   }}}
   else
    setTimeout('sendComment();',1000);
  }}
}




// Funktion zum Loeschen eines Eintrags
function delComment(id){
 asb_loadingChange(true);
 if (xmlHttp){
   if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
    xmlHttp.open('POST', webpath+'deldata.php');
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send('id='+id);
    
    xmlHttp.onreadystatechange = function (){ /* Sobald sich der Zustand ändert führe Aktion aus */
     if (xmlHttp.readyState == 4){
        dosProtect = 0;
        last_request = 0;
        last_class = 0;
        clearInterval(aktiv);
        document.getElementById("asb_content").innerHTML='';
        
        aktiv = setInterval("loadData()", loadInterval);
        setTimeout('loadData();',1000);
   }}}
   else{ /* Lade Einträge zeitversetzt und resette DOS Protect */
    setTimeout('delComment('+id+');',1000); 
 }}
}



// Funktion zum Login ohne Reload der Seite
function sendLogin(){
 asb_loadingChange(true);
 if (xmlHttp){
   var LoginName     = encodeURIComponent(document.frmlogin.loginname.value);
   var LoginPassword = encodeURIComponent(document.frmlogin.loginpassword.value);
   if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
    xmlHttp.open('POST', webpath+'login.php?type=login');
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send('usr='+LoginName+'&pw='+LoginPassword);
    
    xmlHttp.onreadystatechange = function (){ /* Sobald sich der Zustand ändert führe Aktion aus */
     if (xmlHttp.readyState == 4){
       var response=xmlHttp.responseText;
       if(response=='false') {
           alert("Benutzername oder Passwort falsch!");
           document.frmlogin.loginpassword.value = ''; /* Leere das Eingabefeld */
           document.frmlogin.loginpassword.focus();    /* Cursor in das Textfeld setzen */
       }
       else if(response=='true') {
           document.frmlogin.loginname.value = '';
           document.frmlogin.loginpassword.value = '';
           
           dosProtect = 0;
           last_request = 0;
           last_class = 0;
           clearInterval(aktiv);
           document.getElementById("asb_content").innerHTML='';
           
           aktiv = setInterval("loadData()", loadInterval);
           setTimeout('loadData();',1000);
           
           setTimeout('onLoginSuccess();',1000); /* Funktion wird im Style definiert */
       }
    }}
   }
   else{ /* Lade Einträge zeitversetzt und resette DOS Protect */
    setTimeout('sendLogin();',1000); 
 }}
}



// Funktion zum Logout ohne Reload der Seite
function sendLogout(){
 asb_loadingChange(true);
 if (xmlHttp){
  var txtMessage = encodeURIComponent(document.frmshoutbox.txtmessage.value);
  var txtName    = encodeURIComponent(document.frmshoutbox.txtname.value);
   if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
    xmlHttp.open('POST', webpath+'login.php?type=logout');
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send(null);
    
    dosProtect = 0;
    last_request = 0;
    last_class = 0;
    clearInterval(aktiv);
    document.getElementById("asb_content").innerHTML='';
    
    aktiv = setInterval("loadData()", loadInterval);
    setTimeout('loadData();',1000);
    
    setTimeout('onLogout();',1000); /* Funktion wird im Style definiert */
   }
   else
    setTimeout('sendLogout();',1000);
  }
}