var myName='';
var open=false;
var uc=0;
var lastID=0;
var flashTimer;
function ajaxchat_init() {
pingIM();
setInterval(pingIM,1000);
}
if(window.addEventListener) { window.addEventListener("load",ajaxchat_init,false); }
else { window.attachEvent("onload",ajaxchat_init); }
function im_kp(el, e) {
if(!e) { e=window.event; }
if(e.keyCode==13) { updateName(el); }
}
function reload_list() {
var el=document.getElementById("online_list");
loadEl(el,"http://ampletech.us/blog/wp-content/plugins/ajaxchat/ajaxchat_xml.php?action=online_list&t="+new Date().getTime(),function() { return true; });
}
function updateName(el) {
var updateReq=getXML();
updateReq.onreadystatechange=function() {
if(updateReq.readyState==4 && updateReq.status==200) {
if(updateReq.responseText.match("OK")) {
var tmp=updateReq.responseText;
tmp=tmp.split(":");
document.getElementById("myName").value=tmp[1];
myName=tmp[1];
document.getElementById("msg").focus();
reload_list();
}
else {
if(myName.length) { document.getElementById("myName").value=myName; }
alert(updateReq.responseText);
}
}
}
updateReq.open("GET","http://ampletech.us/blog/wp-content/plugins/ajaxchat/ajaxchat_xml.php?action=updateName&name="+el.value);
updateReq.send(null);
}
function blurName(el) {
if(myName.length) { el.value=myName; }
}
function listCB() {
if(open) {
setTimeout("loadEl(document.getElementById(\"online_list\"),\"http://ampletech.us/blog/wp-content/plugins/ajaxchat/ajaxchat_xml.php?action=online_list\",listCB);",5000);
}
}
function openCB() {
loadEl(document.getElementById("online_list"),"http://ampletech.us/blog/wp-content/plugins/ajaxchat/ajaxchat_xml.php?action=online_list",listCB);
el=document.getElementById("msg");
if(el) { setTimeout("document.getElementById('msg').focus();",200); } //Stupid IE hack
}
function ajaxchat_closed() {
open=false;
closeReq=getXML();
closeReq.open("GET","http://ampletech.us/blog/wp-content/plugins/ajaxchat/ajaxchat_xml.php?action=open&val=0",function() { return false; });
closeReq.send(null);
}
function ajaxchat_open() {
lastID=0;
open=true;
openReq=getXML();
openReq.open("GET","http://ampletech.us/blog/wp-content/plugins/ajaxchat/ajaxchat_xml.php?action=open&val=1",function() { return false; });
openReq.send(null);
}
function openOnline() {
if(document.getElementById("online")) {
document.getElementById("online").parentNode.removeChild(document.getElementById("online"));
ajaxchat_closed();
return;
}
ajaxchat_open();
var el=document.createElement("div");
el.style.position="fixed";
el.style.bottom="20px";
el.style.right="0px";
el.style.backgroundColor="#ffffff";
el.id="online";
el.innerHTML="

Loading..
\n";
document.body.appendChild(el);
loadEl(el,"http://ampletech.us/blog/wp-content/plugins/ajaxchat/ajaxchat_xml.php?action=online&t="+new Date().getTime(),openCB);
}
function loadEl(el, url, cb) {
if(!el || !url) { return false; }
var req=getXML();
req.onreadystatechange=function() {
if(req.readyState==4 && req.status==200) {
el.innerHTML=req.responseText;
cb();
}
};
req.open("GET",url);
req.send(null);
}
function getXML() {
try { httpreq=new XMLHttpRequest(); } catch(err) { try { httpreq=new ActiveXObject("Microsoft.XMLHTTP"); } catch(err) { try { httpreq=new ActiveXObject("Msxml2.XMLHTTP"); } catch(err) { } } }
if(!httpreq) { alert("XMLHttpRequest not supported"); return false; }
return httpreq;
}
function loadMessages(info) {
var maxid=0;
var el=document.getElementById("messages");
var i,x;
el.innerHTML+=info;
//Just in case, check to make sure no duplicates in messages div
el.scrollTop=el.scrollHeight;
for(i=0;i1) {
for(x=1;xmaxid) { maxid=tId; }
}
lastID=maxid;
}
function ajaxchat_flash() {
var el=document.getElementById("ajaxIM");
if(!el) { return false; }
if(open) {
el.style.backgroundColor="#c0c0c0";
clearInterval(flashTimer);
flashTimer=0;
return false;
}
if(el.style.backgroundColor=="" || el.style.backgroundColor=="#c0c0c0" || el.style.backgroundColor=="rgb(192, 192, 192)") {
el.style.backgroundColor="#2a4480";
}
else {
el.style.backgroundColor="#c0c0c0";
}
}
function pingIM() {
if(uc==5) { uc=0; }
if(uc==0) { loadEl(document.getElementById("online_count"),"http://ampletech.us/blog/wp-content/plugins/ajaxchat/ajaxchat_xml.php?action=online_count",function() { return true; }); }
uc++;
var pingReq=getXML();
pingReq.onreadystatechange=function() {
if(pingReq.readyState==4 && pingReq.status==200) {
if(pingReq.responseText.match("NEW:")) {
if(!open && !flashTimer) { flashTimer=setInterval(ajaxchat_flash,1000); }
var loadIt=pingReq.responseText.split("NEW:");
loadIt=loadIt[1];
loadMessages(loadIt);
}
}
}
pingReq.open("GET","http://ampletech.us/blog/wp-content/plugins/ajaxchat/ajaxchat_ping.php?lastid="+lastID+"&t="+new Date().getTime());
pingReq.send(null);
}
function msg_kp(el,e) {
if(!e) { e=window.event; }
if(e.keyCode==13 && el.value.length) { sendMsg(el.value); el.value=""; }
}
function sendMsg(txt) {
var sendReq=getXML();
sendReq.onreadystatechange=function() {
if(sendReq.readyState==4 && sendReq.status==200) {
if(sendReq.responseText.match("ERROR")) { alert("There was an error sending your message."+sendReq.responseText); return false; }
}
}
var params="msg="+encodeURI(txt);
params=params.replace("+","%2B"); //Replace any remaining plus signs, spaces should have been converted to %20, remaining plus signs are user inputted.
sendReq.open("POST","http://ampletech.us/blog/wp-content/plugins/ajaxchat//ajaxchat_xml.php?action=send",true);
sendReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
sendReq.setRequestHeader("Content-length", params.length);
sendReq.setRequestHeader("Connection", "close");
sendReq.send(params);
}