// 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/




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

function initJavaScript() {
	document.forms['chatForm'].elements['chatbarText'].setAttribute('autocomplete','off'); //this non standard attribute prevents firefox' autofill function to clash with this script
	checkStatus(''); //sets the initial value and state of the input comment
	checkName(); //checks the initial value of the input name
	receiveChatText(); //initiates the first data query
	receiveChatters(); //initiates the first data query
	checkAlertMode();
}

//initiates the first data query
function receiveChatText() {
	if (httpReceiveChat.readyState == 4 || httpReceiveChat.readyState == 0) {
  	httpReceiveChat.open("GET",GetChaturl + '?lastID=' + lastID + '&rand='+Math.floor(Math.random() * 1000000), true);
    httpReceiveChat.onreadystatechange = handlehHttpReceiveChat; 
  	httpReceiveChat.send(null);
	}
}

//initiates the first data query
function receiveChatters() {
	if (httpReceiveChatters.readyState == 4 || httpReceiveChatters.readyState == 0) {
		var cName = document.forms['chatForm'].elements['name'].value;
  		httpReceiveChatters.open("GET",GetChattersurl + '?name=' + cName + '&rand='+Math.floor(Math.random() * 1000000), true);
   		httpReceiveChatters.onreadystatechange = handlehHttpReceiveChatters; 
  		httpReceiveChatters.send(null);
	}
}


//deals with the servers' reply to requesting new content
function handlehHttpReceiveChatters() {
  if (httpReceiveChatters.readyState == 4) {
    var res = httpReceiveChatters.responseText.split('---'); //the fields are seperated by ---
    var chatters = '';
   
    for(j=0;j < res.length;j++) { //goes through the result one message at a time
	chatters += res[j] + '    ';
    }
    insertChatters(chatters); //inserts the new content into the page
    setTimeout('receiveChatters();',20000); //executes the next data query in 4 seconds
  }
}

//deals with the servers' reply to requesting new content
function handlehHttpReceiveChat() {
  if (httpReceiveChat.readyState == 4) {
    results = httpReceiveChat.responseText.split('---'); //the fields are seperated by ---
    if (results.length > 2) {
	    for(i=0;i < (results.length-1);i=i+3) { //goes through the result one message at a time
	    	insertNewContent(results[i+1],results[i+2]); //inserts the new content into the page
	    }
	    lastID = results[results.length-4];
	    if (alertMode) {
		suspendTimers();
		alert("New messages received! \nThis will turn off message alert.");
		alertMode = false;
		checkAlertMode();
		resumeTimers();
	    }
    }
    setTimeout('receiveChatText();',4000); //executes the next data query in 4 seconds
  }
}

//inserts the new content into the page
function insertChatters(names) {
	document.getElementById("chatters").innerHTML = names;
}

//inserts the new content into the page
function insertNewContent(liName,liText) {
	insertO = document.getElementById("outputList");
	oLi = document.createElement('li');
	oSpan = document.createElement('span');
	oSpan.setAttribute('className','name'); //for IE's sake
	oSpan.setAttribute('class','name');
	oName = document.createTextNode(liName+': ');
	oText = document.createTextNode(liText);
	oSpan.appendChild(oName);
	oLi.appendChild(oSpan);
	oLi.appendChild(oText);
	insertO.insertBefore(oLi, insertO.firstChild);
}

//stores a new comment on the server
function sendComment() {
	currentChatText = document.forms['chatForm'].elements['chatbarText'].value;
	if (currentChatText != '' & (httpSendChat.readyState == 4 || httpSendChat.readyState == 0)) {
		currentName = document.forms['chatForm'].elements['name'].value;
		param = 'n='+ currentName+'&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 = '';
	} else {
		setTimeout('sendComment();',1000);
	}
}

//deals with the servers' reply to sending a comment
function handlehHttpSendChat() {
  if (httpSendChat.readyState == 4) {
  	receiveChatText(); //refreshes the chat after a new comment has been added (this makes it more responsive)
  }
}


//does celver things to the input and submit
function checkStatus(focusState) {
	currentChatText = document.forms['chatForm'].elements['chatbarText'];
	oSubmit = document.forms['chatForm'].elements['submit'];
	if (currentChatText.value != '' || focusState == 'active') {
		oSubmit.disabled = false;
	} else {
		oSubmit.disabled = true;
	}
}

//autoasigns a random name to a new user
function checkName() {
	currentName = document.forms['chatForm'].elements['name'];
	if (currentName.value == '') {
		currentName.value = 'guest_'+ Math.floor(Math.random() * 10000);
	}
}


//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;
}

function checkAlertMode() {
	if (alertMode) {
		document.getElementById("alert").innerHTML = "<p><strong>ON</strong> [<a href=\"javascript:setAlert(false)\"><i>Turn Off</i></a>]</p>";
	} else {
		document.getElementById("alert").innerHTML = "<p><strong>OFF</strong> [<a href=\"javascript:setAlert(true)\"><i>Turn On</i></a>]</p>";
	}
}

function setAlert(a) {
	alertMode = a;
	checkAlertMode();
}

function suspendTimers() {
	clearTimers();
	msgTimeOut = setTimeout('receiveChatText();',60000);
	chattersTimeOut = setTimeout('receiveChatters();',60000);
}

function resumeTimers() {
	clearTimers();
	msgTimeOut = setTimeout('receiveChatText();',4000);
	chattersTimeOut = setTimeout('receiveChatters();',20000);
}

function clearTimers() {
	clearTimeout(msgTimeOut);
	clearTimeout(chattersTimeOut);
}

// initiates the two objects for sending and receiving data
var httpReceiveChat = getHTTPObject();
var httpReceiveChatters = getHTTPObject();
var httpSendChat = getHTTPObject();
var alertMode = false;
var msgTimeOut;
var chattersTimeOut;
