

var instanse = false;
var state;
var mes;
var file;

function Chat () {
    this.update = updateChat;
    this.send = sendChat;
}

//Updates the chat
function updateChat(){
	UpdateTimer();
}

 function doReload(){    
         httpObject = getHTTPObject();
         var randomnumber=Math.floor(Math.random()*10000);
         if (httpObject != null) {
            link = "cats/process.php?all=1&rnd="+randomnumber;
            httpObject.open("GET", link , true);
            httpObject.onreadystatechange = setAll;
            httpObject.send(null);
         }
      }
  // Change the value of the outputText field
  function setAll(){
	 if(httpObject.readyState == 4){
		var response = httpObject.responseText;
		var objDiv = document.getElementById("chat-area");
		objDiv.innerHTML = response;
		objDiv.scrollTop = objDiv.scrollHeight;
	 }
  }
  function getHTTPObject(){
	 if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
	 else if (window.XMLHttpRequest) return new XMLHttpRequest();
	 else {
		alert("Your browser does not support AJAX.");
		return null;
	 }
  }  
  function UpdateTimer() {
	 doReload();   
	 timerID = setTimeout("UpdateTimer()", 15000);
  }
//send the message
function sendChat(message, nickname)
{       
    
     $.ajax({
		   type: "POST",
		   url: "cats/process.php",
		   data: {  
		   			'function': 'send',
					'message': message,
					'nickname': nickname,
					'file': file
				 },
		   dataType: "json",
		   success: function(data){
			   UpdateTimer();
		   }
		});
	setAll();
	UpdateTimer();
}

