
// XHTML live Chat
// author: alexander kohlhofer
// version: 1.0
// http://www.plasticshore.com
// http://www.plasticshore.com/projects/chat/
// please let the author know if you put any of this to use
// XHTML live Chat (including this script) is published under a creative commons license
// license: http://creativecommons.org/licenses/by-nc-sa/2.0/

// initiates the two objects for sending and receiving data
var httpReceiveChat = getHTTPObject();
var httpSendChat = getHTTPObject();
var httpReceiveChatID = getHTTPObject();


var GetChaturl = "getChatData.php";
var SendChaturl = "sendChatData.php";
var lastID = 1; //initial value will be replaced by the latest known id
var oldlastID =1;
window.onload = initJavaScript;

function receiveChatID() 
{
  	httpReceiveChatID.open("GET",GetChaturl, true);
    httpReceiveChatID.onreadystatechange = handlehHttpReceiveChatID; 
    try
    {
      httpReceiveChatID.send(null);
    }
    catch (e) 
    {
    }
}



function initJavaScript() 
{
	if (document.forms['chatForm'] )
  { 
    document.forms['chatForm'].elements['chatbarText'].setAttribute('autocomplete','off'); //this non standard attribute prevents firefox' autofill function to clash with this script
  }
	receiveChatID(); //initiates the first data query
  
  setTimeout('doitww();',4000); //executes the next data query in 4 seconds
}

function doitww()
{
	receiveChatID();
  setTimeout('doitww();',4000); //executes the next data query in 4 seconds  
}

//initiates the first data query
function receiveChatText() 
{
  	httpReceiveChat.open("GET",GetChaturl + '?data=1&lastID=' + lastID , true);
    httpReceiveChat.onreadystatechange = handlehHttpReceiveChat; 
  	httpReceiveChat.send(null);
    lastID = oldlastID ;
}




function handlehHttpReceiveChatID() 
{
  if (httpReceiveChatID.readyState == 4) 
  {               
    if (lastID != httpReceiveChatID.responseText )       
    {
      oldlastID = httpReceiveChatID.responseText ;
      receiveChatText();      
    }
    else
    {
    
    }
    
  }
}


//deals with the servers' reply to requesting new content
function handlehHttpReceiveChat() 
{
  if (httpReceiveChat.readyState == 4) 
  {  
    document.getElementById("chatoutput").innerHTML =  httpReceiveChat.responseText;
    //setTimeout('receiveChatID();',4000); //executes the next data query in 4 seconds
  }
}



//stores a new comment on the server
function sendComment() {
	currentChatText = document.forms['chatForm'].elements['chatbarText'].value;
	if (currentChatText != '' & (httpSendChat.readyState == 4 || httpSendChat.readyState == 0)) {		
		param = 'c='+ currentChatText;	
		httpSendChat.open("POST", SendChaturl, true);
		httpSendChat.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  	httpSendChat.onreadystatechange = handlehHttpSendChat;
  	httpSendChat.send(param);
  	document.forms['chatForm'].elements['chatbarText'].value = '';
	} 
}

//deals with the servers' reply to sending a comment
function handlehHttpSendChat() 
{
  if (httpSendChat.readyState == 4) 
  { 
    if (httpReceiveChatID )
    {            
        receiveChatID() ;           
    }  
  }  
}





//initiates the XMLHttpRequest object
//as found here: http://www.webpasties.com/xmlHttpRequest
function getHTTPObject() {
  var xmlhttp;
  /*@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;
}



