// ############################################################################
// #
// #       Copyright liegt bei:  
// #       Cuculus GmbH
// #       Ehrenbergstr. 11
// #       98693 Ilmenau
// #       Tel. 03677 / 66 85 30 
// #       http://www.cuculus.net 
// ############################################################################
// #
// #  Diese Software ist das Eigentum der Cuculus GmbH und nach dem 
// #  Urheberrecht gesch�tzt - es  handelt sich NICHT um Freeware. Jede Nutzung 
// #  der Software ohne eine g�ltige Lizenzvereinbarung, wird zivil- und 
// #  strafrechtlich verfolgt.
// ############################################################################

//
//  Variablen und Konstanten
//

// chat-URL
var chatURL = 'http://wlan-weimar.de/scripts/chat.php' ;

// Aktualisierungsintervall in Sekunden
var update_intervall = 5 ;

//
// XMLHttpRequest-Instanz
//

var xmlHttp = false;

try {

    xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
    
} catch(e) {
    
    try {
        xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
        xmlHttp  = false;
    }
}

if (!xmlHttp  && typeof XMLHttpRequest != 'undefined') {
	
    xmlHttp = new XMLHttpRequest();
}


// Aktualisiere Nachrichten alle X Sekunden
setInterval( "zeige_nachrichten()" , update_intervall*1000 );


// chat laden
function zeige_nachrichten() {
	
 	if (xmlHttp) {
 	
 		var timestamp = new Date().getTime() ;
     	xmlHttp.open('GET' , chatURL + '?action=show_messages&chat_room='+document.chatFormular.chat_room.value+'&zeitstempel=' + timestamp , true) ;
     	xmlHttp.onreadystatechange = function () {
        
    	    if (xmlHttp.readyState == 4) {
        		document.getElementById("chat_textfenster").innerHTML = xmlHttp.responseText ;
                        var chatdiv = document.getElementById("chat_textfenster");
		        chatdiv.scrollTop = chatdiv.scrollHeight;
			}
    	}
    
        xmlHttp.send(null) ;
 	}
}


// chat speichern
function speichere_nachricht() {

	if (xmlHttp) {

		// Timestamp f�r Nocache    
    	var timestamp = new Date().getTime() ;
    	
    	// Post absetzen
    	xmlHttp.open('POST', chatURL) ;
   	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1') ;

		// Textfeld leeren und Focus setzen
		xmlHttp.onreadystatechange = function () {
        
    	    if (xmlHttp.readyState == 4) {
        		
       		document.chatFormular.neue_nachricht.value = '' ;
					document.chatFormular.neue_nachricht.focus() ;
	
				// Box direkt neu laden			
				zeige_nachrichten() ;
			}
    	}
    	
    	// Parameter schicken
    	xmlHttp.send('action=save_message&chat_room='+document.chatFormular.chat_room.value+'&username='+document.chatFormular.username.value+'&nachricht='+document.chatFormular.neue_nachricht.value+'&zeitstempel='+timestamp+'&session='+document.chatFormular.session_id.value) ;
	}
}

