/*
// (c) 2008 talksuites.com
*/


String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

if (navigator.userAgent.indexOf("Safari") > 0)
{
  isSafari = true;
  isMoz = false;
  isIE = false;
}
else if (navigator.product == "Gecko")
{
  isSafari = false;
  isMoz = true;
  isIE = false;
}
else
{
  isSafari = false;
  isMoz = false;
  isIE = true;
}

/* Functions to handle browser incompatibilites */
function eventElement(event)
{
  if(isMoz)
  {
    return event.currentTarget;
  }
  else
  {
    return event.srcElement;
  }
}

function addKeyListener(element, listener)
{
  if (isSafari)
    element.addEventListener("keyup",listener,false);
  else if (isMoz)
    element.addEventListener("keyup",listener,false);
  else
    element.attachEvent("onkeyup",listener);
}

function addListener(element, type, listener)
{
  if(element.addEventListener)
  {
    element.addEventListener(type, listener, false);
  }
  else
  {
    element.attachEvent('on' + type, listener);
  }
}

function removeListener(element, type, listener)
{
  if(element.removeEventListener)
  {
    element.removeEventListener(type, listener, false);
  }
  else
  {
    element.detachEvent('on' + type, listener);
  }
}

function _navigate(url){
	window.location=url;
}

var ajax=new Array(1000);
var to=new Array(1000);

var _num_chatters=0;

var _private_chat=0;
var _preview_chat=0;

function get_ajax(url){
	if (window.XMLHttpRequest) {
		var xmlhttp = new XMLHttpRequest();
	}else{
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET", "http://"+document.location.host+url, false);
	xmlhttp.send("request");
	return xmlhttp.responseText;
}

function async_ajax(url,id){
	if(ajax[id]){
		return false;
	}
	if (window.XMLHttpRequest) {
		ajax[id] = new XMLHttpRequest();
	}else{
		ajax[id] = new ActiveXObject("Microsoft.XMLHTTP");
	}
	ajax[id].open("GET", "http://"+document.location.host+url, true);
	ajax[id].send("request");
	to[id]=setTimeout('read_data('+id+')',500);
}

function clickact(id){
	var i;
	i=document.getElementById("acts"+id);
	window.location=i.value;
}


function actions(id,acts){
	var a,i;
	a=acts.split("\n");
	document.write("<select id=\"acts"+id+"\" style=\"font-size:8pt\">");
	for(i=0;i<a.length;i++){
		b=a[i].split("\t");
		document.write("<option value=\""+b[0]+"\">"+b[1]+"</option>");
	}
	document.write("</select>&nbsp;");
	document.write("<input type=\"button\" value=\" Go \" onclick=\"clickact("+id+")\">");
}


function askform(text,id){
	var f;
	if(confirm(text)){
		f=document.getElementById(id);
		f.submit();
	}
}

function askgo(url,text){
	if(confirm(text)){
		_navigate(url);
	}
}

function askkill(id){
	var f;
	f=document.getElementById(id);
	if(confirm("Delete selected?")){
		f.submit();
	}
}

function submitform(el,id){
	el.disabled=true;
	var f;
	f=document.getElementById(id);
	f.contentEditable=false;
	f.submit();
}

function askdo(text,url){
	if(confirm(text)){
		_navigate(url);
	}
}

function view_topics(){
	var l,v;
	l=document.getElementById("topiclist");
	list=l.value;

	v=document.getElementById("topicview");
	txt=get_ajax("/publish-listtopics/?list="+list);
	v.innerHTML=txt;
}

function add_topiclist(id){
	var l,txt,list;
	l=document.getElementById("topiclist");
	list=l.value;
	txt=get_ajax("/publish-addlist/?list="+list+"&id="+id);
	l.value=txt;

	view_topics();
}

function add_topic(){
	var h;
	h=document.getElementById("id_topic_hint");
	topic=h.value;
	txt=get_ajax("/publish-topicinfo/?topic="+escape(h.value));
	x=txt.split("|");
	if(x[0]=="0"){
		if(!confirm("This tag hasn't been used in other articles yet.\r\nAre you sure that this tag has to be introduced to the knowledge base?\r\nIf not, try picking up an existing tag out of the list below, or type in another tag hint.")) return;
	}
	txt=get_ajax("/publish-gettopic/?topic="+escape(h.value));
	x=txt.split("|");
	if(x[0]=="1"){
		topic_id=x[1];
		add_topiclist(topic_id);
		h.value="";
	}else{
		alert("Error selecting the tag!");
	}
}

function remove_topic(id){
	if(!confirm("Remove this tag?")) return;
	l=document.getElementById("topiclist");
	list=l.value;
	txt=get_ajax("/publish-removelist/?list="+list+"&id="+id);
	l.value=txt;

	view_topics();
}

function topic_hint_do(){
	var h;
	h=document.getElementById("id_topic_hint");
	topic=h.value;
	txt=get_ajax("/publish-topichint/?topic="+escape(h.value));

	hd=document.getElementById("topichint");
	hd.innerHTML=txt;
}

var thtime;

function topic_hint(){
	clearTimeout(thtime);
	thtime=setTimeout("topic_hint_do()",1000);
}

function pick_topic(txt){
	h=document.getElementById("id_topic_hint");
	h.value=txt;
}


function insertAtCaret(obj, text) {
		if(document.selection) {
			obj.focus();
			var orig = obj.value.replace(/\r\n/g, "\n");
			var range = document.selection.createRange();

			if(range.parentElement() != obj) {
				return false;
			}

			range.text = text;
			
			var actual = tmp = obj.value.replace(/\r\n/g, "\n");

			for(var diff = 0; diff < orig.length; diff++) {
				if(orig.charAt(diff) != actual.charAt(diff)) break;
			}

			for(var index = 0, start = 0; 
				tmp.match(text) 
					&& (tmp = tmp.replace(text, "")) 
					&& index <= diff; 
				index = start + text.length
			) {
				start = actual.indexOf(text, index);
			}
		} else if(obj.selectionStart) {
			var start = obj.selectionStart;
			var end   = obj.selectionEnd;

			obj.value = obj.value.substr(0, start) 
				+ text 
				+ obj.value.substr(end, obj.value.length);
		}
		
		if(start != null) {
			setCaretTo(obj, start + text.length);
		} else {
			obj.value += text;
		}
	}
	
	function setCaretTo(obj, pos) {
		if(obj.createTextRange) {
			var range = obj.createTextRange();
			range.move('character', pos);
			range.select();
		} else if(obj.selectionStart) {
			obj.focus();
			obj.setSelectionRange(pos, pos);
		}
	}


function resize_chatbox(w,h){

//	if(w>1000) w=1000;
//	if(h>600) h=600;
	if(h<300) h=300;

	var ca=document.getElementById('chatarea');
	var cb=document.getElementById('chatbox');
	var ct=document.getElementById('chatters');
	var ib=document.getElementById('inputbox');
	var lib=document.getElementById('loginbox');
	var ti=document.getElementById('textinput');
	var sb=document.getElementById('smilebox');
	var clb=document.getElementById('colorbox');
	var stb=document.getElementById('setbox');
	var rl=document.getElementById('runline');
	var cu=document.getElementById('chatusers');
	var xads=document.getElementById('ads');

	caw=w+20;
	tr_x=caw-10;

	
	ca.style.width=caw+"px";
	
	_box_height=h;
//	if(xads){
	if(_private_chat){
	}else{
		h-=70;
	}
//	}else{
//	}

	ca.style.height=_box_height+"px";
	ca.style.display="block";


	_coll=document.getElementById("coll");
	
	if(_private_chat){
		cbw=w-10;
		cb.style.width=cbw+"px";
		ct.style.visibility="hidden";
		cu.style.visibility="hidden";
	}else if(_preview_chat){
		if(_coll){
			_coll.style.display="none";
		}

		cbw=w-10;
		cb.style.width=cbw+"px";
		ct.style.display="none";

		ctx=w-120;
		cu.style.left=ctx+"px";

	}else{
		_coll.style.display="block";
		ct.style.display="block";
		_coll.style.top=((h-110)/2+20)+"px";
		if(!_ulcoll){
			cbw=w-90-10-15;
			cb.style.width=cbw-5+"px";
			ctx=cbw+25;
			ct.style.left=ctx+"px";
			_coll.style.left=(ctx-8)+"px";
			ct.style.visibility="visible";
		}else{
			cbw=w-20;
			cb.style.width=cbw+"px";
			ct.style.visibility="hidden";
			_coll.style.left=(cbw+22)+"px";
		}

		ctx=w-120;
		cu.style.left=ctx+"px";

	}



	var cbh=h-110;
	cb.style.height=cbh+"px";

	ct.style.height=cbh+"px";

	var ibt=h-65;
	var rlt=h-55;
	var rrt=h-20;

	ibw=w-22;
	ebw=w-165-10;
	rlw=w-4;
	ib.style.width=ibw+"px";
	ib.style.top=ibt+"px";

	lib.style.width=ibw+"px";
	lib.style.top=ibt+"px";


	ti.style.width=ebw+"px";

	if(rl){
		rl.style.width=rlw+"px";

		rl.style.top=rrt+"px";
	}

	sbl=w-400;
	sb.style.left=sbl+"px";
	sbl-=90;
	stb.style.left=sbl+"px";
	sbl+=100+40;
	clb.style.left=sbl+"px";

	if(xads){
		xads.style.width=(caw-20)+"px";
		xads.style.top=(h)+"px";
		xads.style.zIndex=1000;
	}

		
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function auto_resize(){
	var w=$("maindiv").offsetWidth;
	var h=$("maindiv").offsetHeight;
	setTimeout('auto_resize()',2000);
	if(w==_last_w) return;

	if(w>950){
		createCookie("narrow",0,1);
	}else{
		createCookie("narrow",1,1);
	}

	resize_chatbox(w-20,h-_reduce_height);
	_last_w=w;
}

function resolve(id){
	resid.push(id);
	if(restime){
		clearTimeout(restime);
	}
	restime=setTimeout("do_resolve()",250);

}

function rewrite_link(text){
	var x=text.split("|");
	var id=x[0];
	var t=x[1];

	var c=document.getElementsByTagName("A");
	for(var i=0;i<c.length;i++){
		var o=c.item(i);
		if(o){
			if(o.id){
			if(o.id==id){
				o.innerHTML=t;
			}
			}
		}
	}
}

var last_resid=0;
var last_resurl="";

function do_resolve(){
	if(resid.length==0) return;

	var id=resid.pop();

	lst_resid=id;

	var o=document.getElementById(id);
	if(!o) return;

	var url=o.href;

	last_resurl=url;

	var url="/fast.php?pp=resolve&key="+id+"&url="+escape(url);
//	alert(url);
	async_ajax(url,3);	
	restime=0;
}

function retry_resolve(){
	resid.unshift(last_resid);
}

function set_status(txt){
	return;
}

var _return_url="";
var _spect=0;
var _no_preview=false;

function re_login_success(){
	var ib,dl;
	dl=document.getElementById('def_login');
	if(dl)
	dl.style.display="black";
	dl=document.getElementById('msg_login');
	if(dl)
	dl.style.display="none";

	ib=document.getElementById('loginbox');
	if(ib)
	ib.style.display="none";

	ib=document.getElementById('inputbox');
	if(ib)
	ib.style.display="block";

	_preview_chat=0;
	_last_w/=2;
	auto_resize();
}

function re_login_failure(){
	dl=document.getElementById('msg_login');
	dl.style.display="block";
	dl.innerHTML="Failed to re-login. Will attempt again in 5 seconds...";
	setTimeout("re_login()",5000);
}

function re_login(){
	var dl;
	if(ajax[6]) return;
	dl=document.getElementById('def_login');
	dl.style.display="none";
	dl=document.getElementById('msg_login');
	dl.style.display="block";
	dl.innerHTML="Timeout kick occurred. Trying to re-login...";

//	alert("/fast.php?pp=command2&start=relogin&key="+room+"&sk="+_def_id);
	async_ajax("/fast.php?pp=command2&start=relogin&key="+room+"&sk="+_def_id,6);
}

function view_only_mode(reason){
	var ib,dl;
	if(_preview_chat) return;
	ib=document.getElementById('inputbox');
	if(ib){
		ib.style.display="none";
	}

	ib=document.getElementById('loginbox');
	if(ib){
		ib.style.display="block";
	}

	if(reason==1){
		re_login();
	}else{

		if(_no_preview){
			_navigate(_return_url);
		}

	}

	_preview_chat=1;
	_last_w/=2;
	auto_resize();
}

function read_data(id){
	var t=document.getElementById("chatbox");
	var x;
	if(id==2){
	}
	if(ajax[id].readyState==4){
		if(id==1){
			txt=ajax[1].responseText;
//debug(txt+"("+_def_id+","+lastmsg+")");
			x=txt.split("|");
			ajax[1]=0;
			if(x[2]==37){
				_return_url="/room/"+x[4]+"/";
				_spect=1;
				view_only_mode(x[5]);
				x[1]="10000";
			}
			if(parseInt(x[0])>0){
				load_messages();
			}
			clearTimeout(_reload);
			
			_refresh_time=1000;
			if(x[1]){
				_refresh_time=parseInt(x[1]);
			}
			if(_refresh_time<500) _refresh_time=1000;
			if(_spect){
				if(_refresh_time>30000) _refresh_time=30000;
			}else{
				if(_refresh_time>5000) _refresh_time=5000;
			}
			if(isNaN(_refresh_time)) _refresh_time=1000;
			_reload=setTimeout("checkmsg()",_refresh_time);
		}
		if(id==2){
			t=document.getElementById("chatters");
			var tx=ajax[2].responseText;
			
			x=tx.split("|");
			_num_chatters=x[0];
			
			var uc=document.getElementById("id_users");
			uc.innerHTML=x[0];

			x.shift();
			if(!nm_shown){
				t.innerHTML=x.join('|');
			}
			
			
			ajax[2]=0;
			clearTimeout(_listload);
			_listload=setTimeout("refresh_list()",10000);

		}
		if(id==3){
			text=ajax[3].responseText;
//			alert(text+" "+last_resurl);
			if(text==""){
				retry_resolve();
			}else{
				rewrite_link(text);
			}
			
			ajax[3]=0;
			if(restime){
				clearTimeout(restime);
			}
			restime=setTimeout("do_resolve()",250);
		}
		if(id==4){
			var newmsg=ajax[4].responseText;
			add_messages(newmsg);
			ajax[4]=0;

		}
		if(id==5){
			var tp=ajax[5].responseText;
			set_topics(tp);
			ajax[5]=0;

		}
		if(id==6){
			var txp=ajax[6].responseText;
			if(txp=="OK"){
				re_login_success();
			}else{
				re_login_failure();
			}
			ajax[6]=0;
		}

	}else{
		setTimeout('read_data('+id+')',250);
	}
}

function reduce_elements(t){
	var e=t.lastChild;
	if(!e) return t;
	var i=200;
	var ne;
	do{
		ne=e.previousSibling;
		if(i<0){
			t.removeChild(e);
		}
		i--;
		e=ne;
		
	}while(e);
	return t;
}

var _own_msg;

function replace_own(t){
	var e=t.lastChild;
	if(!e) return t;
	var i=20;
	do{
		if(i<0){
			break;
		}
		if(!e) break;
		if(e.id){
		if(e.id.substr(0,7)=="own_msg"){
			var l=_own_msg.length;
			for(var k=0;k<l;k++){
				var item=_own_msg[k];
				var x=item.split('|');
				if(x[0]==e.id){
					x.shift();
					e.innerHTML=x.join('|');
					run_scripts(e);
				}
			}
		}
		}
		
		e=replace_own(e);
		i--;
	}while(e=e.previousSibling);
	return t;
}

function set_topics(tp){
	x=tp.split('|');
	topic=x[0];
	x.shift();
	line=x.join("|");

	l=40-line.length;
	for(var i=0;i<l;i++){
		line+="&nbsp;";
	}
	
	set_runline(line);

	var t=document.getElementById("id_topic");
	if(t){
		t.innerHTML=topic;
		restart_runline();
	}
}

var _chunk_time=new Array();
var _chunk_text=new Array();

function add_messages(newmsg){
	var x=newmsg.split('|');
	lastmsg=x[0];
	x.shift();
	var l=x.length;
	var text="";
	var otext="";
	var part=1;
	for(var i=0;i<l;i++){
		var s=x[i];
		if(s=="*PART*"){
			part=2;
			continue;
		}
		if(part==1){
			text+=x[i]+'|';
		}
		if(part==2){
			otext+=x[i]+'|';
		}
	}
	text=text.substr(0,text.length-1);
	otext=otext.substr(0,otext.length-1);

	var hcb=document.getElementById("hcb");
	hcb.innerHTML=otext;

	var own=new Array();
	var e=hcb.firstChild;
	do{
		if(!e) break;

		own.push(e.innerHTML);
	}while(e=e.nextSibling);

	_own_msg=own;

	var cb=document.getElementById("chatbox");
	var t=cb;
	t=reduce_elements(t);
	t=replace_own(t);


	x=text.split('|');
	var ctime=0;

	x.shift();
	l=x.length;

	var ctext="",ctime=0,xx;

	for(i=0;i<l;i++){
		s=x[i];
		if(s.substr(0,5)=="time_"){
			if(ctime>0){
				ctext=ctext.substr(0,ctext.length-1);
				_chunk_time.push(ctime);
				_chunk_text.push(ctext);
				ctext="";
			}
			xx=s.split('_');
			ctime=xx[1];
			continue;
		}
		ctext+=x[i]+'|';
	}
	ctext=ctext.substr(0,ctext.length-1);
	if(ctext!='|'){
		_chunk_time.push(ctime);
		_chunk_text.push(ctext);
	}

	add_chunk();

	scroll_down(cb);
        refresh_list();

}

function force_scroll(cb){
	cb.scrollTop = cb.scrollHeight*2;
}

function scroll_down(cb){
	o=document.getElementById("chatbox");
	var h=o.offsetHeight;
	var diff=(cb.scrollHeight-cb.scrollTop)-h;
	if(diff<(h*.25)){
		force_scroll(cb);
	}
}

function run_scripts(e){
	var c=new Array();
	c=findElements(c,e,"SCRIPT");
	while(c.length){
		var ce=c.pop();
		eval(ce.innerHTML);
	}
}

function add_chunk(){
	var t=document.getElementById("chatbox");
	if(_chunk_time.length<1) return;
	var ctext=_chunk_text.shift();
	var ctime=_chunk_time.shift();
	
	var e=document.createElement("div");
	e.innerHTML=ctext;
        
	if(!isMoz){
		run_scripts(e);
	}
	t.appendChild(e);
	
	if(ctime<0) ctime=10;
	if(ctime>1000) ctime=1000;
	setTimeout("add_chunk()",ctime);

	scroll_down(t);
}


function dotalk(){
	clearTimeout(_reload);
	t=document.getElementById("textinput");
	var s=t.value;
	s=s.trim();
	if(s.length==0){
		t.value="";
		return;
	}
	d=document.getElementById("talkform");

	var now = new Date();     
	var ms = now.valueOf(); 

	var ct=document.getElementById("id_cms");
	ct.value=ms;
	var sms=ms.toString();
	sms=sms.substr(sms.length-6,6);

	var e=document.createElement("div");
	e.innerHTML="<p class=\"ownms\" id=\"own_msg_"+sms+"\"><b>"+_def_nick+"</b>: "+s+"</p>";

	cb=document.getElementById("chatbox");
	cb.appendChild(e);
	force_scroll(cb);

	d.submit();

	_msgt++;
	if(_msgt>5) _msgt=1;

	d.target="msgtarg_"+_msgt;

	clearTimeout(_reload);
	_reload=setTimeout("checkmsg()",500);
	t.value="";
}

function findElements(c,e,tag){
	var a=e.firstChild;
	if(!a) return c;
	do{
		if(a.tagName==tag){
			c.push(a);
		}
		c=findElements(c,a,tag);
	}while(a=a.nextSibling);
	return c;
}

function load_messages(){
	var pc="";
	if(_private_chat){
		pc=_private_chat;
	}
	async_ajax("/fast.php?pp=newmsg2&key="+room+"&sk="+_def_id+"&start="+lastmsg+"&ss="+pc,4);
}

function set_runline(txt){
	for(i=2;i<=7;i++){
		f=document.getElementById("runline_"+i);
		f.innerHTML=txt;
	}
}

function refresh_list(){
	if(_private_chat || _preview_chat) return;

	async_ajax("/fast.php?pp=loadlist2&key="+room+"&sk="+_def_id,2);
}

function checkmsg(){
	var pc="";
	if(_private_chat){
		pc=_private_chat;
	}

	do_resolve();

	async_ajax("/fast.php?pp=checkmsg2&key="+room+"&sk="+_def_id+"&start="+lastmsg+"&ss="+pc,1);
}

function autosend(e){
	if(!e.keyCode) return;

	closesmbx();
	closesset();
	closesclr();

	if(e.keyCode==13){
		dotalk();
	}
}

function closepws(){
	closesmbx();
	closesset();
	closesclr();
}

function smileys(){
	closesclr();
	closesset();
	t=get_ajax("/listsmileys/");
	e=document.getElementById("smilebox");
	d=document.getElementById("smilediv");
	d.innerHTML=t;
	locate_box(e);
	e.style.visibility="visible";
}

function sets(){
	closesclr();
	closesmbx();
	e=document.getElementById("setbox");
	locate_box(e);
	e.style.visibility="visible";
}

function colors(){
	closesmbx();
	closesset();
	e=document.getElementById("colorbox");
	d=document.getElementById("colordiv");
	locate_box(e);
	e.style.visibility="visible";
}

function closesmbx(){
	d=document.getElementById("smilebox");
	d.style.visibility="hidden";
	t=document.getElementById("textinput");
	t.focus();
}

function closesclr(){
	d=document.getElementById("colorbox");
	d.style.visibility="hidden";
	t=document.getElementById("textinput");
	t.focus();
}

function closesset(){
	d=document.getElementById("setbox");
	d.style.visibility="hidden";
	t=document.getElementById("textinput");
	t.focus();
}

function insert_smiley(txt){
	
	t=document.getElementById("textinput");
	insertAtCaret(t,txt);
	t.focus();
	closesmbx();
}

_rlt=0;

var _ticks=0;

var err="";
function runline(){
	e=document.getElementById("runline");
	f=document.getElementById("runline_1");
	n=document.getElementById("runline_2");

	if(e.scrollLeft>=(f.scrollWidth+2)){
		e.scrollLeft=0;
		f.innerHTML=n.innerHTML;
		_ticks++;
		if(_ticker_repeats){
			if(_ticks>=_ticker_repeats){
				clearTimeout(_rlt);
				return;
			}
		}
	}else{
		e.scrollLeft=e.scrollLeft+3;
	}

	if(_rlt){
		clearTimeout(_rlt);
	}

	_rlt=setTimeout('runline()',60);


}

function restart_runline(){
	_ticks=0;
	_rlt=setTimeout('runline()',60);
}



var hn=new Array();
var nm_shown=false;
function hide_nick_menu_do(id){
	e=document.getElementById("nick_menu_"+id);
	e.style.visibility="hidden";
	nm_shown=false;
}

function hide_nick_menu(id){
	hn[id]=setTimeout("hide_nick_menu_do("+id+")",700);
}

function nick_menu(id){
	clearTimeout(hn[id]);
	e=document.getElementById("nick_menu_"+id);
	e.style.visibility="visible";
	nm_shown=true;
}


function private_close(){
	if(_private_chat){
//		_navigate("/privateclose/");
	}
}

function find_rule(name){
	var ss=document.styleSheets;
	var l=ss.length;
	var r;
	for(var i=0;i<l;i++){
		if(ss[i].cssRules){
			r=ss[i].cssRules;
		}else{
			r=ss[i].rules;
		}
		if(!r) continue;
		var rl=r.length;
		for(var k=0;k<rl;k++){
			var rr;
			rr=r[k];
			if(rr.selectorText == name) return rr;
		}
	}
	return false;
}

function redraw(o){
	v=o.value;
	ca=document.getElementById('chatarea');
	var t;
	if(o.id=='id_background'){

		t=document.getElementById('chatarea');
		t.style.backgroundColor=v;

	}
	t=document.getElementById('chatbox');
	var t2=document.getElementById('inputbox');
	var t3=document.getElementById('chatters');
	if(o.id=='id_cabg'){
		t.style.backgroundColor=v;
		t2.style.backgroundColor=v;
		t3.style.backgroundColor=v;
	}
	if(o.id=='id_color'){
		ca.style.color=v;
		t.style.color=v;
		t2.style.color=v;
		t3.style.color=v;
	}
	var rule;
	if(o.id=='id_smes'){
		rule=find_rule('.server');
		if(rule){
			rule.style.color=v;
		}
	}
	if(o.id=='id_omes'){
		rule=find_rule('.ownms');
		if(rule){
			rule.style.color=v;
		}
	}

	if(o.id=='id_linkc'){
		rule=find_rule('.linkc');
		if(rule){
			rule.style.color=v;
		}
	}

	if(o.id=='id_oline'){
		t.style.borderColor=v;
		t2.style.borderColor=v;
		t3.style.borderColor=v;
	}
	if(o.id=='id_font'){
		ca.style.fontFamily=v;
		t.style.fontFamily=v;
		t2.style.fontFamily=v;
		t3.style.fontFamily=v;
	}
	if(o.id=='id_fontsize'){
		ca.style.fontSize=v+"pt";
		t.style.fontSize=v+"pt";
		t2.style.fontSize=v+"pt";
		t3.style.fontSize=v+"pt";
	}

}

function update_topics(){
	async_ajax("/fast.php?pp=topic2&key="+room+"&sk="+_def_id,5);	
}

function kick(id){
	if(confirm("Kick this user?")){
		get_ajax("/fast.php?pp=command2&start=kick&key="+room+"&ss="+id+"&sk="+_def_id);
	}
}

function ban(id){
	txt=prompt("Ban this user? Input the ban term in minutes:","15");
	if(!txt) return;
	get_ajax("/fast.php?pp=command2&start=ban&key="+room+"&ss="+id+"&sk="+_def_id+"&sd="+txt);
}

function mute(id){
	txt=prompt("Mute this user? Input the mute term in minutes:","15");
	if(!txt) return;
	get_ajax("/fast.php?pp=command2&start=mute&key="+room+"&ss="+id+"&sk="+_def_id+"&sd="+txt);
}

function ignore(id){
	if(confirm("Ignore this user?")){
		get_ajax("/fast.php?pp=command2&start=ignore&key="+room+"&ss="+id+"&sk="+_def_id);
	}
}

function unignore(id){
	if(confirm("Un-ignore this user?")){
		get_ajax("/fast.php?pp=command2&start=unignore&key="+room+"&ss="+id+"&sk="+_def_id);
	}
}

function unmute(id){
	if(confirm("Un-mute this user?")){
		get_ajax("/fast.php?pp=command2&start=unmute&key="+room+"&ss="+id+"&sk="+_def_id);
	}
}

function get_location(obj){
	var p=obj;
	var rv=new Array();
	rv[0]=0;rv[1]=0;
	do{
		rv[0]+=p.offsetLeft;
		rv[1]+=p.offsetTop;
	}while(p=p.offsetParent);
	return rv;
}

function private_chat(url,id){
	var h=350;
	if(id=="embed") h=250;
	window.open(url,"private"+id,"left=50,top=50,width=500,height="+h+",menubar=no,toolbar=no,location=no,status=no,resizable=yes,scrollbars=no",false);
}

function _popup(url,id){
	var h=500;
	window.open(url,"popup"+id,"left=50,top=50,width=800,height="+h+",menubar=no,toolbar=no,location=no,status=no,resizable=yes,scrollbars=yes",false);
}

function debug(txt){
	var d=new Date();
	var date=d.toString();
	var x=date.split(' ');
	date=x[3];
	var o=document.getElementById("debug");
	if(!o) return;
	o.value=o.value+date+": "+txt+'\r\n';
	o.scrollTop = o.scrollHeight*2;
}

function locate_box(e){
	var w=document.getElementById("smiles");
	var l=get_location(w);
//alert(l[0]+" / "+l[1]);
	e.style.left=(l[0]-e.offsetWidth-50)+"px";
	e.style.top=(l[1]-e.offsetHeight-20)+"px";
	hidewmenu_ex();
}

function wmenu(){
	clearTimeout(_hwm);
	var m=document.getElementById("wmenu");
	var w=document.getElementById("wrench");
	var l=get_location(w);
	m.style.display="block";
	m.style.left=(l[0]-8)+"px";
	m.style.top=(l[1]-m.offsetHeight)+"px";
}

var _hwm=0;
function hidewmenu_ex(){
	var m=document.getElementById("wmenu");
	m.style.display="none";
}

function hidewmenu(){
	clearTimeout(_hwm);
	_hwm=setTimeout("hidewmenu_ex()",500);
}

var _ulcoll=false;

function collexp(){
	var c=document.getElementById("collimg");
	if(_ulcoll){
		c.src="/i/arrow.gif";

		_ulcoll=false;
	}else{
		c.src="/i/arrowl.gif";
		_ulcoll=true;
	}

	_last_w=0;
	auto_resize();
	t=document.getElementById("textinput");
	t.focus();
}

var _hm=new Array();

function pop_menu(id){
	clearTimeout(_hm[id]);
	var m=document.getElementById("pmenu_"+id);
	var e=document.getElementById("mitem_"+id);
	if(m && e){
		var a=get_location(e);
		m.style.display="block";
		m.style.left=a[0]+"px";
		m.style.top=(a[1]+e.offsetHeight)+"px";
	}

}

function hide_menu_do(id){
	var m=document.getElementById("pmenu_"+id);
	if(m){
		m.style.display="none";
	}
}

function hide_menu(id){
	var m=document.getElementById("pmenu_"+id);
	if(m){
		_hm[id]=setTimeout("hide_menu_do("+id+")",500);
	}
}

function update_url(){
	a=document.getElementById("id_e_name");
	b=document.getElementById("id_e_url");

	t=a.value;
	re= / /g;
	b.value=t.replace(re,"_");
}

ajax[1]=0;
ajax[2]=0;
ajax[3]=0;
ajax[4]=0;
ajax[5]=0;
ajax[6]=0;
