// Based in the XHTML live Chat (http://www.plasticshore.com)
// This script is published under a creative commons license
// license: http://creativecommons.org/licenses/by-nc-sa/2.0/
var current_class = 'row2';
var counter = 0;
var refresh_time = 4000;
var sendTimeout, receiveTimeout, cssRules, row1_color, row2_color;
var online = true;
// time
var now = new Date();
var time = Math.round(now.getTime()/1000);
var lastID = -1; //initial value will be replaced by the latest known id
window.onload = initJavaScript;
// Initiates the two objects for sending and receiving data
var httpReceiveChat = getHTTPObject();
var httpSendChat = getHTTPObject();
// Sound switch
var play_sound = false;
function sound_on()
{
play_sound = true;
document.getElementById('reload').style.border = '5px solid #FFFF00';
}
function sound_off()
{
play_sound = false;
document.getElementById('reload').style.border = '';
}
window.onblur = sound_on;
window.onfocus = sound_off;
function Time() {
now = new Date();
return Math.round(now.getTime()/1000);
}
function resetTime() {
time = Time();
refresh_time = 4000;
}
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
checkStatus(''); //sets the initial value and state of the input comment
}
get_colors();
receiveChatText(); //initiates the first data query
// Get background colors from classes
if (!document.styleSheets)
{
return;
}
}
// Get the background colors from css classes
function get_colors()
{
var prefix;
var theRules = new Array();
if (document.styleSheets[0].cssRules)
{
theRules = document.styleSheets[0].cssRules
prefix = 'td.';
}
else if (document.styleSheets[0].rules)
{
theRules = document.styleSheets[0].rules
prefix = 'TD.';
}
for (x=0;x 2) {
// Check that the last message wasn't printed before.
if (lastID < results[results.length-5]) {
// Goes through the result one message at a time
for (i=0;i < (results.length-1);i=i+5) {
// Insert the new content into the page
if (lastID < results[i+1]) {
insertNewContent(results[i+2],results[i+3],results[i+4] ,lastID);
resetTime();
}
}
lastID = results[results.length-5];
}
}
else {
// no new comment since..
// 60 seconds
if ( refresh_time == 4000 & ((time + 60) < Time()) ) {
// set refresh to 10 seconds
refresh_time = 10000;
}
// 5 minutes
else if ( refresh_time == 10000 & ((time + 300) < Time()) ) {
// set refresh to 30 seconds
refresh_time = 30000;
}
// 30 minutes
else if ( refresh_time == 30000 & ((time + 1800) < Time()) ) {
// set refresh to 2 minutes
refresh_time = 120000;
}
// 60 minutes
else if ( refresh_time == 120000 & ((time + 3600) < Time()) ) {
// set refresh to 5 minutes
refresh_time = 300000;
}
}
setTimeout('receiveChatText();', refresh_time);
}
}
// Lets put the shouts to the table
function insertNewContent(liName, liText, liTime, last_id)
{
// Row Id
var id = 'row_'+counter;
// Get a reference to the table
var tableRef = document.getElementById("outputList");
// Insert a row in the table at row index 0
var newRow = tableRef.insertRow(0);
// Put some attributes to the row
newRow.setAttribute('id', id);
if(current_class == "row2")
{
newRow.style.backgroundColor = row1_color;
}
else
{
newRow.style.backgroundColor = row2_color;
}
// Insert 2 cells in the row
var newCell = newRow.insertCell(0);
var newCell2 = newRow.insertCell(1);
// Put some attributes to the rows
newCell.setAttribute('width', 110);
// Append a text node to the cell
newCell.innerHTML = ''+liName+'
'+liTime+'';
newCell2.innerHTML = ''+liText+'';
// Add 1 to counter
counter++;
// Fade effect
// We don't want to fade the first pack of messages
if(last_id != -1)
{
Fat.fade_element(id, 30, 2000, '#666666','#D9D9D9');
// Finally we can set the cells classes after the fade
setTimeout("classChanger('"+newRow.id+"');", 2006);
// play sound
if ( play_sound == true )
{
if (navigator.appName.indexOf ("Microsoft") !=-1)
{
document.getElementById("chatsound").Play();
setTimeout('document.getElementById("chatsound").Play();', 1500);
}
else
{
document.embeds[0].Play();
setTimeout('document.embeds[0].Play();', 1500);
}
}
return true;
}
// Finally we can set the cells classes after the fade
classChanger(newRow.id);
}
function classChanger(obj_id)
{
var obj = document.getElementById(obj_id);
obj.childNodes[0].className = current_class;
obj.childNodes[1].className = current_class;
if(current_class == "row2")
{
current_class = "row1";
}
else
{
current_class = "row2";
}
}
//stores a new comment on the server
function sendComment()
{
setTimeout("document.forms['chatForm'].submit.disabled = true;", 0);
setTimeout("document.forms['chatForm'].submit.disabled = false;", refresh_time);
//currentChatText = encodeURIComponent(document.forms['chatForm'].elements['chatbarText'].value);
currentChatText = escape(document.forms['chatForm'].elements['chatbarText'].value);
if (currentChatText != '' & (httpSendChat.readyState == 4 || httpSendChat.readyState == 0))
{
param = 'act=add&message='+ currentChatText;
httpSendChat.open("POST", ChatActionurl, true);
httpSendChat.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
httpSendChat.onreadystatechange = handlehHttpSendChat;
// Timeout Prevention
sendTimeout = setTimeout("ajaxTimeout(2);", (refresh_time/2));
httpSendChat.send(param);
document.forms['chatForm'].elements['chatbarText'].value = '';
document.forms['chatForm'].elements['chatbarText'].focus();
}
sound_off();
}
//deals with the servers' reply to sending a comment
function handlehHttpSendChat()
{
if (httpSendChat.readyState == 4)
{
// We didn't timeout
clearTimeout(sendTimeout);
setTimeout("receiveChatText();", (refresh_time/2)); //refreshes the chat after a new comment has been added (this makes it more responsive)
var error_status = httpSendChat.responseText.split('---')[2];
if(error_status == 1)
{
alert(httpSendChat.responseText.split('---')[1]);
setTimeout("sound_off();", 100);
}
}
}
//does clever 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;
}
}
//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 quoteSelection()
{
theSelection = false;
if (document.getSelection)
{
theSelection = document.getSelection();
}
else if (document.selection)
{
theSelection = document.selection.createRange().text;
}
if (theSelection)
{
// Add tags around selection
emoticon( '[quote]' + theSelection + '[/quote]\n');
document.forms['chatForm'].elements['chatbarText'].focus();
theSelection = '';
return;
}
else
{
alert(no_text_selected);
setTimeout("sound_off();", 100);
}
}
function storeCaret(textEl)
{
if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}
function emoticon(text)
{
if (document.forms['chatForm'].elements['chatbarText'].createTextRange && document.forms['chatForm'].elements['chatbarText'].caretPos)
{
var caretPos = document.forms['chatForm'].elements['chatbarText'].caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
document.forms['chatForm'].elements['chatbarText'].focus();
}
else
{
document.forms['chatForm'].elements['chatbarText'].value += text;
document.forms['chatForm'].elements['chatbarText'].focus();
}
}
function checkForm()
{
if (document.forms['chatForm'].elements['chatbarText'].value.length < 2)
{
alert(empty_message);
setTimeout("sound_off();", 100);
return false;
}
}
function resize(img)
{
if (img.width > screen.width-400)
{
img.width = (screen.width-400);
}
}