var popOut=; var open=false; var messageReq=false; var uc=0; function ajaxchat_init() { var jq=jQuery; var doc=document; var msgEl=doc.getElementById("msg"); var nameEl=doc.getElementById("myName"); jq(window).unload(function() { if(popOut) { sendWinCoord(); } }); pingIM(); setInterval(pingIM,1000); if(popOut) { initPopOut(); } if(msgEl && open) { msgEl.focus(); } if(nameEl && myName.length) { nameEl.value=myName; } reload_list(); } if(window.addEventListener) { window.addEventListener("load",ajaxchat_init,false); } else { window.attachEvent("onload",ajaxchat_init); } function sendWinCoord() { var jq=jQuery; var win=jq("#ac_window"); if(!win.is(':visible')) { win.show(); var pos=win.position(); win.hide(); } else { var pos=win.position(); } pos.top-=jq(window).scrollTop(); //Request must be synchronous to complete before page unloads. jq.ajax("ajaxchat_xml.php?action=sendCoord&left="+pos.left+"&top="+pos.top,{async:false}); } function im_kp(el, e) { if(!e) { e=window.event; } if(e.keyCode==13) { updateName(el); } } function reload_list() { var doc=document; var count=doc.getElementById("online_count"); var list=doc.getElementById("online_list"); var listReq=getXML(); listReq.onreadystatechange=function() { if(listReq.readyState==4 && listReq.status==200) { var data=listReq.responseText.split("::"); var listData=data[1]; var lines=listData.split("\n"); for(var i=0;i")!=-1) { var nameStart=lines[i].indexOf(""); var nameLen=lines[i].indexOf("")-(nameStart+8); var nameTmp=lines[i].substring(nameStart+8,(nameStart+8)+nameLen); if(nameTmp!=myName) { jQuery("#myName").val(nameTmp); myName=nameTmp; } } } list.innerHTML=data[1]; count.innerHTML=data[0]; } } listReq.open("GET","ajaxchat_xml.php?action=online_list&t="+new Date().getTime()); listReq.send(null); } function initPopOut() { var jq=jQuery; var pos={}; var win=jq("#ac_window"); win.draggable(); win.height(jq("#ac_window_table").height()); win.css('left',pos.left); win.css('top',pos.top); if(open) { win.fadeTo(1000,0.9,function() { }); } jq("#ac_window_title").css('cursor','move'); } //Function to toggle popping the window in and out of position, ie: to be able to drag it around. function popToggle() { var jq=jQuery; var win=jq("#ac_window"); var title=jq("#ac_window_title"); if(!popOut) { win.height(jq("#ac_window_table").height()); win.draggable(); win.animate({top: '-=30', left: '-=30'},1000,function() { }); win.fadeTo(1000,0.9,function() { }); title.css('cursor','move'); popOut=true; jq.ajax("ajaxchat_xml.php?action=popout&status=true", {async:true}); } else { win.draggable('destroy'); win.height(jq("#ac_window_table").height()); var newLeft,newTop,mainWin; mainWin=jq(window); newLeft=mainWin.width()-450; newTop=mainWin.height()-win.height()-jq("#ajaxIM")[0].offsetHeight; win.animate({left: newLeft, top: newTop},1000,function() { }); win.fadeTo(1000,1.0,function() { }); title.css('cursor','default'); popOut=false; jq.ajax("ajaxchat_xml.php?action=popout&status=false", {async:true}); } } function openOnline() { //Toggle ajaxchat window. var doc=document; var jq=jQuery; open=!open; var openReq=getXML(); openReq.open("GET","ajaxchat_xml.php?action=open&val="+(open?'1':'0')+"&t="+new Date().getTime()); openReq.send(null); var jqEl=jq("#ac_window"); var mainWin=jq(window); var el=doc.getElementById("ac_window"); var disp=el.style.display; var messages=doc.getElementById("messages"); if(disp=="none" || disp=="") { if(!popOut) { jqEl.css('left',mainWin.width()-450); jqEl.css('top',mainWin.height()-jqEl.height()-jq("#ajaxIM")[0].offsetHeight); } el.style.display="block"; jqEl.height(jq("#ac_window_table").height()); if(popOut) { jqEl.fadeTo(1000,0.9,function() { }); } if(doc.getElementById("msg")) { doc.getElementById("msg").focus(); } } else { el.style.display="none"; } if(open) { messages.scrollTop=messages.scrollHeight; } } function updateName(el) { var doc=document; var updateReq=getXML(); updateReq.onreadystatechange=function() { if(updateReq.readyState==4 && updateReq.status==200) { var updateResp=updateReq.responseText; if(updateResp.match("OK")) { var tmp=updateResp; tmp=tmp.split(":"); doc.getElementById("myName").value=tmp[1]; myName=tmp[1]; doc.getElementById("msg").focus(); reload_list(); } else { if(updateResp.match("Error: ")) { var error=updateResp.split("Error: ")[1]; alert(error); } if(myName.length) { doc.getElementById("myName").value=myName; } } } } updateReq.open("GET","ajaxchat_xml.php?action=updateName&name="+el.value); updateReq.send(null); } function blurName(el) { if(myName.length) { el.value=myName; } } function ajaxchat_closed() { open=false; closeReq=getXML(); closeReq.open("GET","ajaxchat_xml.php?action=open&val=0",function() { return false; }); closeReq.send(null); } function ajaxchat_open() { open=true; openReq=getXML(); openReq.open("GET","ajaxchat_xml.php?action=open&val=1",function() { return false; }); openReq.send(null); } 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 doc=document; var maxid=0; var el=doc.getElementById("messages"); var i,x; el.innerHTML+=info; el.scrollTop=el.scrollHeight; //Just in case, check to make sure no duplicates in messages div for(i=0;i1) { for(x=1;xmaxid) { maxid=tId; } } lastID=maxid; } function pingIM() { uc++; if(uc==10) { reload_list(); uc=0; } if(messageReq) { return false; } messageReq=getXML(); messageReq.onreadystatechange=function() { if(messageReq.readyState==4) { //Seperate block so we always clear messageReq when the request is finished, regardless of status if(messageReq.status==200) { var respTxt=messageReq.responseText; if(respTxt.length) { loadMessages(respTxt); } } messageReq=null; } } messageReq.open("GET","ajaxchat_ping.php?lastid="+lastID+"&t="+new Date().getTime()); messageReq.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","/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); }