

      var ChathttpObject = null;
      var chatLink = "";
      var chatTimerID = 0;
      var nickName = "<?php echo $user_nick; ?>";
      var alterText = "";


      // Get the HTTP Object
      function getChathttpObject(){
         if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
         else if (window.XMLHttpRequest) return new XMLHttpRequest();
         else {
            alert("Dein Browser unterstützt leider kein AJAX.");
            return null;
         }
      }   

      // Change the value of the outputText field
      function setChatOutput(){
         if(ChathttpObject.readyState == 4){
            var response = ChathttpObject.responseText;
            var objDiv = document.getElementById('chatbox');
            objDiv.innerHTML = response;
            objDiv.scrollTop = objDiv.scrollHeight;
            var inpObj = document.getElementById('msg');
            inpObj.focus();
         }
      }

      // Change the value of the outputText field
      function setChatAll(){
         if(ChathttpObject.readyState == 4){
            var response = ChathttpObject.responseText;
            var objDiv = document.getElementById('chatbox');
            if (alterText != response)
		{
		        objDiv.innerHTML = response;
			objDiv.scrollTop = objDiv.scrollHeight;
		}
	    alterText = response;
         }
      }

      // Implement business logic    
      function chat_send(){    
         ChathttpObject = getChathttpObject();
         if (ChathttpObject != null) {
            chatLink = "chat_message.php?all=1&msg="+document.getElementById('msg').value;
            document.getElementById('msg').value = "";
            ChathttpObject.open("GET", chatLink , true);
            ChathttpObject.onreadystatechange = setChatOutput;
            ChathttpObject.send(null);
         }
      }

      // Implement business logic    
      function doChatReload(){    
         ChathttpObject = getChathttpObject();
         var randomnumber=Math.floor(Math.random()*10000);
         if (ChathttpObject != null) {
            chatLink = "chat_message.php?all=1&rnd="+randomnumber;
            ChathttpObject.open("GET", chatLink , true);
            ChathttpObject.onreadystatechange = setChatAll;
            ChathttpObject.send(null);
         }
      }

      function chatUpdateTimer() {
         doChatReload();   
         chatTimerID = setTimeout("chatUpdateTimer()", 500);
      }
    
    
      function chat_keypressed(e){
         if(e.keyCode=='13'){
            chat_send();
         }
      }
