function chatData() {
	this.init();
}

chatData.prototype.init = function()
{
  this.timeStamp      = 0;                            //default it set to 0 it will get the current server timestamp upon 1st request
  this.interval       = 3000;                         //interval is 3 sec
  this.getUrl         = 'chat.php?action=getmssg';    //url to get the mssg only
  this.postUrl        = 'chat.php?action=sendmssg';   //url to send a new mssg
  this.httpReqObject  = this.httpObject();            //Create new Ajax object
	this.timerID        = null;                         //The timer id that refres to the interval
	
	this.send( false, false );                          //Get the current timestamp and optional data
	this.start();                                       //Start the service and get new messages each interval
	
}

/* start the service */
chatData.prototype.start = function()
{
  var thisObject = this;
	clearInterval(this.timerID);
	this.timerID = setInterval(function() { thisObject.send( false, false ); }, this.interval);
}

/* stop the service */
chatData.prototype.stop = function()
{
  clearInterval(this.timerID);
}

/* reset the service, same as start */
chatData.prototype.reset = function()
{
  this.start();
}

/* Send a request to the server */
chatData.prototype.send = function( nickname, message )
{
  var thisObject = this;

	if (this.httpReqObject !== null && (this.httpReqObject.readyState === 4 || this.httpReqObject.readyState === 0)) {
  	
  	/* the params need to be urlEncoded */
  	var postData = 'timestamp=' + encodeURIComponent(this.timeStamp);

  	if ( nickname == false || message == false ) {
  	  this.httpReqObject.open( 'POST', this.getUrl, true );
	  } else {
  	  this.reset(); //reset the interval (send request are not done in frequent intervals so the normal interval to get the messages is getting reset)
  	  postData+= '&nickname=' + encodeURIComponent(nickname) + '&mssg=' + encodeURIComponent(message);
  	  this.httpReqObject.open( 'POST', this.postUrl, true );
	  }
		this.httpReqObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		this.httpReqObject.onreadystatechange = function() { thisObject.responseHandler(); };
		this.httpReqObject.send(postData);
  	
	} else {
  	/* HTTP object not ready try it 500 milisec later again */
		setTimeout(function() { thisObject.send( nickname, message ); }, 500);
	}
}

/* 
 * The response handler 
 * You need to change the code here if you need to change the actions on the response
 * For now its a simple handler to extract the timestamp and the message and it will
 * put the output in the div
 */
chatData.prototype.responseHandler = function() {

  if (this.httpReqObject !== null && this.httpReqObject.readyState === 4) {
  	var response = this.httpReqObject.responseXML; /* get the XML response */
	
  	/* get the root node */
  	var dataNode = response.getElementsByTagName("data")[0];
  	
  	/* get and set the latest timestamp */
  	this.timeStamp = dataNode.attributes.getNamedItem("timestamp").value;
  	
  	/* find the divObject */
  	chatTextObj = document.getElementById('chatText');
  	
  	/* loop the data node for mssg nodes */
  	for( var x = 0; dataNode.childNodes[x]; x++ ) {
    	if ( dataNode.childNodes[x].nodeName == "mssg" ) {
      	/* add the mssg to the div */
    	  chatTextObj.innerHTML+= "<br /><b>" + dataNode.childNodes[x].attributes.getNamedItem("nickname").value + "&nbsp;(" + dataNode.childNodes[x].attributes.getNamedItem("timestamp").value + "):&nbsp;</b>" + dataNode.childNodes[x].attributes.getNamedItem("mssg").value;
    	  /* scroll down div so the latest text is always visible */
    	  chatTextObj.scrollTop = chatTextObj.scrollHeight - chatTextObj.clientHeight;
  	  }
  	}
	}
};


/* 
 * create a object regardles to the browser type 
 */
chatData.prototype.httpObject = function() {
	var xmlhttp = false;
	
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	@else
	xmlhttp = false;
	@end @*/
	
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	    try {
	    	xmlhttp = new XMLHttpRequest();
	    } catch (e) {
	    	xmlhttp = false;
	    }
	}
	return xmlhttp;
};

function toggleLayer(whichLayer)
{
if (document.getElementById)
{
// this is the way the standards work
var style2 = document.getElementById(whichLayer).style;
style2.display = style2.display? "":"block";
}
else if (document.all)
{
// this is the way old msie versions work
var style2 = document.all[whichLayer].style;
style2.display = style2.display? "":"block";
}
else if (document.layers)
{
// this is the way nn4 works
var style2 = document.layers[whichLayer].style;
style2.display = style2.display? "":"block";
}
}
