/*
//TO USE THIS UNCOMPRESSED CODE, YOU NEED TO USE THESE GLOBAL VARIABLES!
//Set all 'global' variables that need setting
var session='<? echo session_id(); ?>'; //Session ID
var time=<? echo timemicro(); ?>; //Microtime
var pmstring='PM > '; //The string prepended to the name of all PM 'channels'
var msgstring=': '; //The string between a name and a message
var curchannel='main'; //The channel currently visible
var channelarray=new Array(); //An array of all open channels
var path='/chat'; //The path for the script.
//The XMLHttpRequest storage variables
var reqs; //SEND
var reqg; //GET
var reqc; //CHAN
//Keep some browsers from firing events multiple times
var comps=false;
var compg=false;
var compc=false;
var keepgoing=true; //Is set to false if there is a server side 403 error
var problem="There was a problem retrieving messages:\n";
*/

//General function for creating XMLHttpRequest Object
function newcom(pn)
	{
	if (window.XMLHttpRequest && ((!window.opera || window.opera.version()>8) || !pn))
		{
		return new XMLHttpRequest();
		}
	else if (window.ActiveXObject)
		{
		return new ActiveXObject("Microsoft.XMLHTTP");
		}
	else
		{
		return false;
		}
	}

//The following are all functions for loading and processing the server side pages.
//There is a set of two function for each page (sendmsg.php,getmsg.php,chan.php)
function load_s(url,postdata)
	{
	comps=false;
	if (reqs=newcom(true))
		{
		reqs.onreadystatechange = process_s;
		reqs.open("POST", url, true);
		reqs.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		reqs.send(postdata);
		}
	else
		{
		iframeload('s',url,postdata);
		}
	}

function process_s()
	{
	if (reqs.readyState == 4 && !comps)
		{
		comps=true;
		if (reqs.status == 200 || reqs.status == 403)
			{
			eval(reqs.responseText);
			}
		else
			{
			alert(problem + reqs.statusText);
			}
		}
	}

function load_g(url)
	{
	compg=false;
	if (reqg=newcom(false))
		{
		reqg.onreadystatechange = process_g;
		reqg.open("GET", url, true);
		reqg.send(null);
		}
	else
		{
		iframeload('g',url);
		}
	}

function process_g()
	{
//try{
	if (reqg.readyState == 4 && !compg)
		{
		compg=true;
		if (reqg.status == 200 || reqg.status == 403)
			{
			eval(reqg.responseText);
			}
		else
			{
			alert(problem + reqg.statusText);
			}
		}
//}catch(e){}
	}

function load_c(url)
	{
	compc=false;
	if (reqc=newcom(false))
		{
		reqc.onreadystatechange = process_c;
		reqc.open("GET", url, true);
		reqc.send(null);
		}
	else
		{
		iframeload('c',url);
		}
	}

function process_c()
	{
//try{
	if (reqc.readyState == 4 && !compc)
		{
		compc=true;
		if (reqc.status == 200 || reqc.status == 403)
			{
			eval(reqc.responseText);
			}
		else
			{
			alert(problem + reqc.statusText);
			}
		}
//}catch(e){}
	}

//This function is used if there is no XMLHttpRequest method
function iframeload(frame,url,postdata)
	{
	if (postdata)
		{
		document.sendform2.action=url;
		document.sendform2.msg.value=decodeURIComponent(postdata.substring(4,postdata.length));
		document.sendform2.submit();
		}
	else
		{
		document.getElementById('dataframeid'+frame).src=url;
		}
	}

//This function calls itself in a timeout, and checks the server for new messages
function getmsg()
	{
	if (keepgoing)
		{
		url='http://'+window.location.host+path+'/getmsg.php?session='+session+''+makechannelquery()+'&t='+time;
		load_g(url);
		setTimeout('getmsg();',1000);
		}
	else
		{
		keepgoing=true;
		setTimeout('getmsg();',10000);
		}
	}

//This function is called from the form, and send a message to the server
function sendmsg(chan,themsg)
	{
	url='http://'+window.location.host+path+'/sendmsg.php?session='+session+''+makechannelquery()+'&chanto='+myescape(chan);
	load_s(url,'msg='+myescape(themsg));
	}

//This function is called from the server page getmsg.php,
//as well as various functions for updating channel content.
function addmsg(chan,name,msg)
	{
	var divid='msgbox'+chan;
	var ihtml=document.getElementById(divid).innerHTML;
	if (ihtml!='')ihtml=ihtml+'<br><!-- endmsg -->';
	document.getElementById(divid).innerHTML=ihtml+'<a class="msgname" href="#" onClick="sendmsg(\'\',\'/pm '+name+'\');return false;">'+name+'</a>'+msgstring+''+msg;
	document.getElementById(divid).scrollTop=document.getElementById(divid).scrollHeight;
	//document.getElementById('music').Play();
	if (curchannel!=chan)timeouts[0][chan]=setInterval('flashtab(\''+chan+'\');',500);
	}

//Opens a pm to user.  Called by sendmsg.php in response to /pm (name)
function mpm(chan)
	{
	if (!isTab(pmstring+chan))newTab(pmstring+chan,true);
	}

//Called from getmsg.php, puts a new msg in a pm window,
//or creates the pm window if it does not exist already for the sender
function pm(chan,from,msg)
	{
	if (isTab(pmstring+chan))
		{
		addmsg(pmstring+chan,from,msg);
		}
	else
		{
		newTab(pmstring+chan);
		if(msg)addmsg(pmstring+chan,from,msg);
		}
	}

//Opens a new channel.  Called by sendmsg.php in response to /join (channel)
function jc(chan)
	{
	newTab(chan,true);
	}

//Closes a channel.  Called by the close button.
function lc(chan)
	{
	removeTab(chan);
	}

//Adds a user to the online list.  Called by chan.php.
function oa(chan,name)
	{
	var divid='userbox'+chan;
	var ihtml=document.getElementById(divid).innerHTML;
	document.getElementById(divid).innerHTML=ihtml+'<br><a class="onlinename" href="#" onClick="sendmsg(\'\',\'/pm '+name+'\');return false;">'+name+'</a>';
	}

//Called from goTab().  Queries server for online list of current channel.
function getol(chan)
	{
	if (chan.substring(0,5)!=pmstring)
		{
		var divid='userbox'+chan;
		document.getElementById(divid).innerHTML='Users:';
		url='http://'+window.location.host+path+'/chan.php?session='+session+'&action=ol&chan='+myescape(chan);
		load_c(url);
		}
	}

//Adds notification of joining or leaving a channel, and changing of names.
//Called from getmsg.php.
function ajlc(chan,name,type)
	{
	var msg='';
	if (type=='j')
		{
		msg='- '+name+' has joined the channel -';
		}
	if (type=='l')
		{
		msg='- '+name+' has left the channel -';
		}
	if (type.substr(0,2)=='n>')
		{
		var newname=type.substr(2,type.length-2);
		msg='- '+name+' is now known as '+newname+' -';
		}
	if (curchannel==chan)getol(chan);
	var divid='msgbox'+chan;
	var ihtml=document.getElementById(divid).innerHTML;
	if (ihtml!='')ihtml=ihtml+'<br><!-- endmsg -->';
	document.getElementById(divid).innerHTML=ihtml+msg;
	document.getElementById(divid).scrollTop=document.getElementById(divid).scrollHeight;
	}

//Notifies in a PM window if the user is not signed on.  Called from getmsg.php.
function lpm(chan,name,type)
	{
	var msg='';
	if (type=='l')
		{
		msg='- '+name+' has signed off -';
		}
	if (type.substr(0,2)=='n>')
		{
		var newname=type.substr(2,type.length-2);
		msg='- '+name+' is now known as '+newname+' -';
		changeTabName(chan,pmstring+newname);
		}
	var divid='msgbox'+chan;
	var ihtml=document.getElementById(divid).innerHTML;
	if (ihtml!='')ihtml=ihtml+'<br><!-- endmsg -->';
	document.getElementById(divid).innerHTML=ihtml+msg;
	document.getElementById(divid).scrollTop=document.getElementById(divid).scrollHeight;
	}

//Called on page unLoad.  Tells server to remove user from all channels.
function leaveAllChannels()
	{
	url='http://'+window.location.host+path+'/chan.php?session='+session+'&action=lall'+makechannelquery();
	load_c(url);
	}

//Used in multiple queries to the server to indicate
//which channels you are currently viewing.
function makechannelquery()
	{
	var retstring='';
	for(var i=0;i<channelarray.length;i++)
		{
		retstring+='&chan['+i+']='+myescape(channelarray[i]);
		}
	return retstring;
	}

//Adds the names of each channel currently open
//into an array for use by other functions
function getChanArray()
	{
	var tmparr=new Array();
	var thedivs=document.getElementsByTagName("div");
	for (i=0; i< thedivs.length; i++)
		{
		if (thedivs[i].getAttribute('class')=='thebox'||thedivs[i].getAttribute('classname')=='thebox')
			{
			lastchan=thedivs[i].getAttribute('id');
			tmparr[tmparr.length]=lastchan.substring(3,lastchan.length);
			}
		}
	return tmparr;
	}

//Switch focus to another tab.  Set in its own function to save space.
function switchTab(tabname)
	{
	clearTimeout(timeouts[0][tabname]);
	var thedivs=document.getElementsByTagName("div");
	for (i=0; i< thedivs.length; i++)
		{
		if (thedivs[i].getAttribute('class')=='thebox'||thedivs[i].getAttribute('classname')=='thebox')
			{
			thedivs[i].style.display="none";
			}
		}
	var theas=document.getElementsByTagName("a");
	for (i=0; i< theas.length; i++)
		{
		if (theas[i].getAttribute('class')=='tab'||theas[i].getAttribute('classname')=='tab')
			{
			theas[i].style.backgroundColor='#dddddd';
			theas[i].style.borderColor='#aaaaaa';
			theas[i].style.zIndex="1";
			}
		}
	document.getElementById('box'+tabname).style.display="block";
	document.getElementById('tab'+tabname).style.backgroundColor="#ffffff";
	document.getElementById('tab'+tabname).style.borderColor="#000000";
	document.getElementById('tab'+tabname).style.color="#000000";
	document.getElementById('tab'+tabname).style.zIndex="3";
	document.getElementById('closediv').innerHTML='<a href="#" onClick="lc(\''+tabname+'\');return false;" onmouseover="window.status=\'Leave '+tabname+'\';return true;" onmouseout="window.status=\'\';return true;" class="close">X</a>';
	}

//Called on page load.  Makes tabs styled properly based on the curchannel.
function prepTabs()
	{
	switchTab(curchannel);
	channelarray=getChanArray();
	}

//Switches focus to another tab.
function goTab(tabname,getl)
	{
	if (curchannel!=tabname)
		{
		curchannel=tabname;
		if (getl!='no')
			{
			getol(tabname);
			}
		else if (tabname.substring(0,5)!=pmstring)
			{
			var divid='userbox'+tabname;
			document.getElementById(divid).innerHTML='Users:';
			}
		switchTab(tabname);
		document.getElementById('msgbox'+tabname).scrollTop=document.getElementById('msgbox'+tabname).scrollHeight;
		}
	}

//Returns true if tabname is the name of a tab, false otherwise
function isTab(tabname)
	{
	if (document.getElementById('box'+tabname))
		{
		return true;
		}
	else
		{
		return false;
		}
	}

//Returns true if tabname is the current tab in focus
function istopTab(tabname)
	{
	if (document.getElementById('box'+tabname).style.display!='none')
		{
		return true;
		}
	else
		{
		return false;
		}
	}

//Opens a new tab, and tells the sever of the join, and to get the online list
function newTab(tabname,focus)
	{
	if (!isTab(tabname))
		{
		var tabshtml=document.getElementById('alltabs').innerHTML;
		var channelshtml=document.getElementById('allboxs').innerHTML;
		if (focus){stylevar='';}else{stylevar=' style="display:none;"';}
		document.getElementById('allboxs').innerHTML=channelshtml+'<div id="box'+tabname+'" class="thebox"'+stylevar+'><div class="msgbox" id="msgbox'+tabname+'"></div><div class="userbox" id="userbox'+tabname+'"></div></div>';
		document.getElementById('alltabs').innerHTML=tabshtml+'<a id="tab'+tabname+'" class="tab" onClick="goTab(\''+tabname+'\');return false;">'+tabname+'</a>';
		if (focus){goTab(tabname);}
			url='http://'+window.location.host+path+'/chan.php?session='+session+'&action=j&chan='+myescape(tabname);
			load_c(url);
		}

	channelarray=getChanArray();
	}

//Remove a tab.  Gets new tab, and asks server for online list of new tab.
function removeTab(tabname)
	{
	if (isTab(tabname)&&channelarray.length>1)
		{
		var toptab=istopTab(tabname);

		var chan=document.getElementById('box'+tabname);
		chan.parentNode.removeChild(chan);
		var tab=document.getElementById('tab'+tabname);
		tab.parentNode.removeChild(tab);
		var newtab=getNewTab(tabname);
		if (toptab)goTab(newtab,'no');
			var nolvar='';
			if (newtab.substring(0,5)!=pmstring)nolvar='&nol='+myescape(newtab);

			url='http://'+window.location.host+path+'/chan.php?session='+session+'&action=l&chan='+myescape(tabname)+nolvar;
			load_c(url);
		}

	channelarray=getChanArray();
	}

function changeTabName(oldname,newname)
	{

	}

//Returns the tab to the right of the tab specified,
//or the the left if it is rightmost
function getNewTab(tabname)
	{
	var lastis=false;
	for (var i=0;i<channelarray.length;i++)
		{
		if (lastis)
			{
			return channelarray[i];
			}
		if (channelarray[i]==tabname)
			{
			lastis=true;
			if (i+1==channelarray.length)
				{
				return channelarray[i-1];
				}
			}
		}
	}

var timeouts=new Array();
timeouts[0]=new Array();
timeouts[1]=new Array();
function flashtab(tabname)
	{
	var thetab=document.getElementById('tab'+tabname);
	if (timeouts[1][tabname])
		{
		thetab.style.backgroundColor="#dddddd";
		thetab.style.color="#000000";
		timeouts[1][tabname]=false;
		}
	else
		{
		thetab.style.backgroundColor="#d0d0d0";
		thetab.style.color="#ff0000";
		timeouts[1][tabname]=true;
		}
	}


function myescape(thestring)
	{
	return escape(thestring).replace(/\+/g,'%2B');
	}


/*
 * Gets by an annoying bug in Opera..
 * Unless the content has changed,
 * Opera will not resize it with the window..
 * This constitutes a change.
 * BUT, NOT IN 8.01..  FIX THIS OPERA, PLEASE! :(
 */
function operafix()
	{
	document.getElementById('operafix').innerHTML=time;
	}

if (window.opera)
	{
	setInterval("operafix()",100);
	}


//Called from body onload, fires off all neaded onload functions.
function bol()
	{
	prepTabs();
	getmsg();
	url='http://'+window.location.host+path+'/chan.php?session='+session+'&action=jall'+makechannelquery()+'&nol='+myescape(curchannel);
	load_c(url);
	}

//Called from body onunload, fires off all needed onunload functions.
function boul()
	{
	leaveAllChannels();
	}