var GetChaturl = "getChatData.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
	getPageif();
	getPageif1();
}

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

//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];
    }
    setTimeout('receiveChatText();',4000); //executes the next data query in 4 seconds
  }
}

//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.innerHTML += jal_apply_filters(liText);
	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;
}


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



var x = 10000;
/* clears default values from input boxes */
function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}



function jal_apply_filters(s) {
   return filter_smilies(make_links((s)));
}


var smilies =  [[':\\)',   'smile.gif'],
      [':\\-\\)',   'smile.gif'],
      [':D',      'biggrin.gif'],
      [':\\(',   'sad.gif'],
      [':o',      'splat.gif'],
      [':eek:',   'eek.gif'],
      [':s',      'confused.gif'],
      [':lol:',   'lol.gif'],
      [':cool:',   'cool.gif'],
      [':mad:',   'mad.gif'],
      [':razz:',   'razz.gif'],
      [':\\$',   'redface.gif'],
      [':evil:',   'evil.gif'],
      [':tongue:','tongue.gif'],
      [':cry:',   'cry.gif'],
      [':roll:',   'rolleyes.gif'],
      [':wink:',   'wink.gif'],
      [':!:',      'exclaim.gif'],
      [':\\?:',   'question.gif'],
      [':shocked:',   'shocked.gif'],
      [':>:',   '04.gif'],
      [':t',   'thumbup.gif'],
      [':mrgreen:',   'mrgreen.gif']]; 

function make_links (s) {

   var re = /((http|https|ftp):\/\/[^ ]*)/gi;
   text = s.replace(re,"<a href=\"$1\" target=new>&laquo;link&raquo;</a>");
   return text;
}

function filter_smilies(s) {
   for (var i = 0; i < smilies.length; i++) {
      var search = smilies[i][0];
      var replace = '<img border=0 src="turquoise/' +
smilies[i][1] + '" class="wp-smiley" alt="' +
smilies[i][0].replace(/\\/g, '') + '" />';
      re = new RegExp(search, 'gi');
      s = s.replace(re, replace);
   }

   return s;
};















function Toggle(item) {
   obj=document.getElementById(item);
   visible=(obj.style.display!="none")
   if (visible) {
     obj.style.display="none";
   } else {
      obj.style.display="block";
   }
}










function getPageif(){
setTimeout("getPageif()",10000); //executes the next data query in 4 seconds
var xmlhttp1=false; //Clear our fetching variable
        try {
                xmlhttp1 = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
        } catch (e) {
                try {
                        xmlhttp1 = new
                        ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
            } catch (E) {
                xmlhttp1 = false;
                        }
        }
        if (!xmlhttp1 && typeof XMLHttpRequest!='undefined') {
                xmlhttp1 = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
        }
        var file1 = 'checks/whos_on.php'; //This is the path to the file we just finished making *

    xmlhttp1.open('GET', file1, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp1.onreadystatechange=function() {
        if (xmlhttp1.readyState==4) { //Check if it is ready to recieve data
                var content1 = xmlhttp1.responseText; //The content data which has been retrieved ***
                if( content1 ){ //Make sure there is something in the content variable
                      document.getElementById('who').innerHTML = content1; //Change the inner content of your div to the newly retrieved content ****
                }
        }
        }
        xmlhttp1.send(null) //Nullify the XMLHttpRequest
return;
}




function getPage22(action){
var xmlhttp=false; //Clear our fetching variable
        try {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
        } catch (e) {
                try {
                        xmlhttp = new
                        ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
            } catch (E) {
                xmlhttp = false;
                        }
        }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
        }
        var file = 'checks/who_info.php?id=' + action; //This is the path to the file we just finished making *

    xmlhttp.open('GET', file, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
                var content = xmlhttp.responseText; //The content data which has been retrieved ***
                if( content ){ //Make sure there is something in the content variable
                      document.getElementById('con').innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
                }
        }
        }
        xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}




function getPage23(action){
var xmlhttp=false; //Clear our fetching variable
        try {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
        } catch (e) {
                try {
                        xmlhttp = new
                        ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
            } catch (E) {
                xmlhttp = false;
                        }
        }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
        }
        var file = 'checks/who_message.php?id=' + action; //This is the path to the file we just finished making *

    xmlhttp.open('GET', file, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
                var content = xmlhttp.responseText; //The content data which has been retrieved ***
                if( content ){ //Make sure there is something in the content variable
                      document.getElementById('con1').innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
                }
        }
        }
        xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}




function getPage24(action){
var xmlhttp=false; //Clear our fetching variable
        try {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
        } catch (e) {
                try {
                        xmlhttp = new
                        ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
            } catch (E) {
                xmlhttp = false;
                        }
        }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
        }
        var file = 'checks/who_message.php'; //This is the path to the file we just finished making *

    xmlhttp.open('GET', file, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
                var content = xmlhttp.responseText; //The content data which has been retrieved ***
                if( content ){ //Make sure there is something in the content variable
                      document.getElementById('con1').innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
                }
        }
        }
        xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}




function CheckAvatar(field) {

	if (window.XMLHttpRequest) {
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		AJAXForms = new ActiveXObject("Microsoft.XMLHTTP");
	}

  AJAXForms.onreadystatechange = processChange;
  AJAXForms.open("GET", "checks/checkavatar.php?op=ajax&field=avatar&id=" + field.value);
  LastField = 'avatar';
  AJAXForms.send(null);
}





















function getPageif1(){
setTimeout("getPageif()",10000); //executes the next data query in 4 seconds
var xmlhttp1=false; //Clear our fetching variable
        try {
                xmlhttp1 = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
        } catch (e) {
                try {
                        xmlhttp1 = new
                        ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
            } catch (E) {
                xmlhttp1 = false;
                        }
        }
        if (!xmlhttp1 && typeof XMLHttpRequest!='undefined') {
                xmlhttp1 = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
        }
        var file1 = ''; //This is the path to the file we just finished making *

    xmlhttp1.open('GET', file1, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp1.onreadystatechange=function() {
        if (xmlhttp1.readyState==4) { //Check if it is ready to recieve data
                var content1 = xmlhttp1.responseText; //The content data which has been retrieved ***
                if( content1 ){ //Make sure there is something in the content variable
                      document.getElementById('blah').innerHTML = content1; //Change the inner content of your div to the newly retrieved content ****
                }
        }
        }
        xmlhttp1.send(null) //Nullify the XMLHttpRequest
return;
}





