var ChatTranslit = false; // вкл/выкл транслита
var ChatOnOff = true;	// вкл/выкл чата
var nIE = (IE && parseFloat(navigator.appVersion.substring(navigator.appVersion.indexOf("MSIE ") + 5))>=6);
var opened_win;

var map_en = new Array('j/o','j/e','sx','Sx','SX','sX','shh','Shh','w','W',"jo","Jo",'JO','zh','Zh','ZH','ch','Ch','CH','sh','Sh','SH','je','Je','JE','ju','Ju','JU','ja','Ja','JA','a','A','b','B','v','V','g','G','d','D','e','E','z','Z','i','I','j','J','k','K','l','L','m','M','n','N','o','O','p','P','r','R','s','S','t','T','u','U','f','F','h','H','x','X','c','C','##','#','y','Y',"''","'")
var map_ru = new Array('йо','йе','сх','Сх','СХ','сХ','щ','Щ','щ','Щ','ё','Ё','Ё','ж','Ж','Ж','ч','Ч','Ч','ш','Ш','Ш','э','Э','Э','ю','Ю','Ю','я','Я','Я','а','А','б','Б','в','В','г','Г','д','Д','е','Е','з','З','и','И','й','Й','к','К','л','Л','м','М','н','Н','о','О','п','П','р','Р','с','С','т','Т','у','У','ф','Ф','х','Х','х','Х','ц','Ц','Ъ','ъ','ы','Ы','Ь','ь')

var obj = {};
obj.sm = sm;
var oPopup, smile_n=1;
if (nIE) {
	oPopup = window.createPopup();
	if (!oPopup) nIE = false;
	else {
		var oPopBody = oPopup.document.body;
		oPopBody.style.backgroundColor = "#E0D9D1";
		oPopBody.style.border = "solid black 1px";
		oPopBody.style.margin = "0";
		oPopBody.style.padding = "0";
		oPopBody.ondragstart = function() { return false }
	}
}

var chat_history = [];
var chat_val = 1;
var chat_mess;
var chat_length = 0;

var sort_key = ["me", "clan", "trade","noranks","noshowaggr"];

function convert(str) {
	for(var i=0;i<map_en.length;++i) while(str.indexOf(map_en[i])>=0) str = str.replace(map_en[i],map_ru[i]);
	return str;
}
function translate(s) {	// translates latin to russian
	var strarr = new Array();
	strarr = s.split(' ');
	for (var k=0;k<strarr.length;k++) {
		if (strarr[k].indexOf("//") < 0 && strarr[k].indexOf('@') < 0 && strarr[k].indexOf("www.") < 0 && !(strarr[k].charAt(0)==":" && strarr[k].charAt(strarr[k].length-1)==":")) {
			if ((k<strarr.length-1)&&(strarr[k]=="to" || strarr[k]=="private")&&(strarr[k+1].charAt(0)=="[")) {
				while ( (k<strarr.length-1) && (strarr[k].charAt(strarr[k].length-1)!="]") ) k++;
			} else { strarr[k] = convert(strarr[k]) }
		}
	}
	return strarr.join(' ');
}

function sw_translit(nw)
{
	var o = $('b___translit');

	if (!nw)
	{
		ChatTranslit = ! ChatTranslit;
	}

	if (ChatTranslit)
	{
		o.src = 'i/b___translit_on.gif';
		o.alt = o.title = mes_ChatTranslitOn;
	}
	else
	{
		o.src = 'i/b___translit_off.gif';
		o.alt = cmd_form.title = mes_ChatTranslitOff;
	}

	SetCookie('translit', ChatTranslit ? 1 : 0);
}

function doCmd(s) {
	if (s.charAt(0)==" ") {

		if (cmd_form && cmd_form.style.visibility == "visible") {
			cmd_form.text.focus();
			cmd_form.text.value = cmd_form.text.value + ((cmd_form.text.value.length>0 && cmd_form.text.value.charAt(cmd_form.text.value.length - 1)!=" ") ? " " : "") + s.substring(1);
		}
	} else {
		subm(s);
	}
	if (nIE) oPopup.hide();
	return false;
}

function set_SWin(dx, checked)
{
	if (checked)
	{
		split_win = Number(split_win) + dx;
	}
	else
	{
		split_win = Number(split_win) - dx;
	}

	SetCookie('split_win', split_win);

	if (split_win == 0)
	{
		temp_chat1_parent_container_height = chat1_parent_container.offsetHeight;

		chat1_parent_container.style.height = chat_list_parent_container_height;
		chat2_parent_container.style.height = '0px';
		chat2_parent_container.style.display = 'none';

		winheight = 100;

		SetCookie('winheight', winheight);
	}
	else if (split_win > 0 && split_win < 255)
	{
		if (temp_chat1_parent_container_height == 0)
		{
			var h1 = Math.floor(chat_list_parent_container.offsetHeight / 2);
			var h2 = chat_list_parent_container_height - h1;

			chat1_parent_container.style.height = h1 + 'px';
			chat2_parent_container.style.height = h2 + 'px';

			winheight = h1 * 100 / chat_list_parent_container_height;

			SetCookie('winheight', winheight);
		}
		else
		{
			chat1_parent_container.style.height = (temp_chat1_parent_container_height) + 'px';
			chat2_parent_container.style.height = (chat_list_parent_container_height - temp_chat1_parent_container_height) + 'px';

			winheight = temp_chat1_parent_container_height * 100 / chat_list_parent_container_height;

			SetCookie('winheight', winheight);
		}

		chat2_parent_container.style.display = '';
	}
}

function set_filter(dx)
{
	if (dx == 128 && !word_filter_array.length && !set_word_filter())
	{
		return false;
	}

	if (dx & chat_filter)
	{
		chat_filter = Number(chat_filter) & (0xFFFF - Number(dx));
	}
	else
	{
		chat_filter = Number(chat_filter) | Number(dx);
	}

	SetCookie('chat_filter', chat_filter);
}

function set_system_filter(dx)
{
	if (dx & chat_system_filter)
	{
		chat_system_filter = Number(chat_system_filter) & (0xFFFFFFFF - Number(dx));
	}
	else
	{
		chat_system_filter = Number(chat_system_filter) | Number(dx);
	}




	if (chat_system_filter == 32506032)
	{
		if (((1<<0) & chat_filter))
		{
			set_filter(1);
		}
	}
	else
	{
		if (!((1<<0) & chat_filter))
		{
			set_filter(1);
		}
	}

	SetCookie('chat_system_filter', chat_system_filter);
}

function set_word_filter()
{
	var s = prompt(mes_word_filter_prompt, word_filter_text);
	if (!s) return false;
	word_filter_text = s;
	word_filter_array = SplitWordFilter(word_filter_text);
	SetCookie("word_filter", s);
	return Boolean(word_filter_array.length);
}

// фильтр системных сообщений
function set_system_filter_win()
{
	var p;
	var s1='<tr><td colspan="3" width="300" align="center"><b>'+mes_show_sysmessages+'</b></td></tr>';
	var uniqueArr = [];

	for (var i=0; i<msys.length; i++)
	{
		p = 1<<i;
		/*
		if (exists(msys2[i], uniqueArr))
		{
			continue;
		}
		*/
		var str=msys[i];
		str = str.split("%").join("");
		str = str.substr(0,30) + "…"
		uniqueArr.push(str);

		s1 += '<tr><td nowrap="nowrap" style="padding:2px 2px 2px 10px">'+
		'<input onclick="'+(nIE ? 'parent' : 'window.opener')+'.set_system_filter('+p+')" type="checkbox" '+(p&chat_system_filter?'checked="checked"':'')+'/></td><td align="left">'+str+'</td></tr>';
	}

	OpenPopUp("system_filter", s1, 300, msys.length*27, true, true);
}

function set_sort(key, value) {
	if (sort_obj[key] != value) { resetlist = true; resortlist = true; };
	sort_obj[key] = value;
	SetCookie("sort_obj_"+key, sort_obj[key]);
	CreateList();
}


function set_scans()
{
	scan_f = !scan_f;
	SetCookie('scan_f', scan_f ? 1 : 0);
}

function rt()
{
	if (!GetCookie) {return;} //когда отменяем prompt из списка слов в мозилле теряется top

	ChatTranslit = (GetCookie('translit') == 1);

	chat_filter = parseInt(GetCookie('chat_filter'));
	if (!chat_filter || !/^\d+$/.test(chat_filter)) chat_filter = 127;


	chat_system_filter = parseInt(GetCookie('chat_system_filter'));
	//if (!chat_system_filter || !/^\d+$/.test(chat_system_filter)) chat_system_filter = 101122047;
	if (!chat_system_filter || !/^\d+$/.test(chat_system_filter)) {
		//для наглядности покажем все что включаем
		//chat_system_filter = ((1<<msys.length) - 1);
		chat_system_filter = (1<<3) | (1<<4) | (1<<3) | (1<<10) | (1<<13) | (1<<15) | (1<<16) | (1<<19)| (1<<20)| (1<<21)| (1<<22)| (1<<23) | (1<<24) | (1<<27) | (1<<28) | (1<<29) | (1<<30) | (1<<31);
		SetCookie('chat_system_filter', chat_system_filter);

	}


	for (var i=0; i<sort_key.length; i++)
	{
		sort_obj[sort_key[i]] = Number(GetCookie("sort_obj_"+sort_key[i]) || 0);
	}

	sort_obj.all = Number(GetCookie("sort_obj_all") || 0);

	sw_translit(true);

	var o = $('b___translit');

	if (no_translit) // выключить кнопку транслита
	{
		o.style.display = 'none';
	}

	if (lang != 'ru') // у нерусских убираем счетчик
	{
		o = $('b__mailru');
		o.style.display = 'none';
	}

	o = $('b___ok');
	o.alt = o.title = mes_addtext;

	o = $('b___smile');
	o.alt = o.title = mes_smile;

	o = $('b___clear');
	o.alt = o.title = mes_clear;

	o = $('b___cmd');
	o.alt = o.title = mes_addcmd;

	o = $('b___sort');
	o.alt = o.title = mes_sort;

	o = $('b__exit');
	o.alt = o.title = mes_exit;

	o = $('b___filter');
	o.alt = o.title = mes_filter;

	o = $('b___scan');
	o.alt = o.title = '';

	light_lamp(0);
}

function subm(s)
{
	if (!s)
	{
		s = cmd_form.text.value;
		cmd_form.text.value = '';
		cmd_form.text.focus();
	}

	if (s != "") {

		if (s=="//scrolloff") noScroll = true;
		else if (s=="//scrollon") noScroll = false;
		else if (s=="//smartscroll") {
			pconf.smartScroll=!pconf.smartScroll;
			SetCookie('smartScroll',pconf.smartScroll);
			if (pconf.smartScroll) {
					S("  "+mes_smart_scroll_on, 15, true);
			} else {
					S("  "+mes_smart_scroll_off, 15, true);
			}
		}
		else if (s=="//away") doAway();
		else if (s=="//font+") {
			if (Number(pconf.fontSize)<=24) {
				pconf.fontSize=Number(pconf.fontSize)+1;
				setFonts();
			}
		}
		else if (s=="//font-") {
			if (Number(pconf.fontSize)>6) {
				pconf.fontSize=Number(pconf.fontSize)-1
				setFonts();
			}
		}
		else {
			add_history(s);

			if (s.toLowerCase().indexOf("//мясо")>-1) {
				if (aprilparty) {
					s="//meat";
				} else {
					return false;
				}
			}
			var i, j, now_time = (new Date()).getTime(), c = 0;
			while ((i=s.indexOf("to [", c))>=0 && (j=s.indexOf("]", i))>=0) {
				away_stamp[s.substring(i+4, j).toLowerCase()] = now_time+5*60000;
				c = j+1;
			}
			c = 0;
			while ((i=s.indexOf("private [", c))>=0 && (j=s.indexOf("]", i))>=0) {
				away_stamp[s.substring(i+9, j).toLowerCase()] = now_time+5*60000;
				c = j+1;
			}
			if (ChatTranslit) s=translate(s);

			tz.sendChat(s);
		}
	}
	return false;
}

function Sz(name)
{
	if (cmd_form && cmd_form.style.visibility == "visible")
	{
		cmd_form.text.focus();
		cmd_form.text.value = cmd_form.text.value + (cmd_form.text.value.charAt(cmd_form.text.value.length - 1) != " " ? " " : "") + ':'+name+': ';
	}

	if (nIE) oPopup.hide();
}

function td1(n, d) {
	return '<td style="background:#555;font-size:9pt;font-weight:bold;cursor:pointer;cursor:hand; '+(n==d?"background-color:#eeeeee;":"")+'" '+(n==d?'':('onclick="'+(nIE ? 'parent' : 'window.opener')+'.smiles('+d+')" onMouseOver="this.style.backgroundColor=\'white\'" onMouseOut="this.style.backgroundColor=\'#555555\'"'))+'>'+d+'</td>';
}

function divPopUpClose()
{
	var popup = $('popup');
	popup.style.display = 'none';
	popup.innerHTML = '';

	CEvent.remove(document, 'mousedown', divPopUpClose);
	CEvent.remove(window, 'resize', divPopUpClose);
}

function divPopUpOpen(content, x, y, width, height)
{
	var popup = $('popup');

	popup.innerHTML = '<table width="100%">' + content + '</table>';
	popup.style.left = x + 'px';
	popup.style.top = y + 'px';
	popup.style.width = width + 'px';
	popup.style.height = height + 'px';
	popup.style.display = 'block';

	CEvent.add(document, 'mousedown', divPopUpClose);
	CEvent.add(window, 'resize', divPopUpClose);
}

function divPopUpOpenLeftBottom(content, x, y, width, height,autosize)
{
	//alert([content, x, y, width, height,autosize])
	var popup = $('popup');

	popup.innerHTML = '<table>' + content + '</table>';
	popup.style.left = x + 'px';
	popup.style.top = y + 'px';
	popup.style.display = 'block';
	if (!autosize) 	{
		popup.style.width = width + 'px';
		popup.style.height = height + 'px';
	}
	//повтор для рефреша данных
	popup.style.top = (y-popup.clientHeight) + 'px';
	CEvent.add(document, 'mousedown', divPopUpClose);
	CEvent.add(window, 'resize', divPopUpClose);

}

function OpenPopUp(name, content, width, height, notReal, notHand, notRight)
{
	content = '<table width="100%" style="font-size:12pt;">'+content+'</table>'

	var x = document.body.clientWidth - width;
	var y = document.body.clientHeight - height;

	if (nIE) {
		y-=30;
		var oPopBody = oPopup.document.body;
		oPopBody.innerHTML = content;

		if (notReal) {
			oPopup.show((notRight?0:x), y, width, height, document.body);
		} else {
			oPopup.show(0,0, 100,0);
			var realHeight = oPopBody.scrollHeight;
			var realWidth = oPopBody.scrollWidth;
			oPopup.hide();
			oPopup.show((notRight?0:document.body.clientWidth-realWidth), -realHeight, realWidth, realHeight, document.body);
		}
	} else {

		if (opened_win) opened_win.close();
		opened_win = window.open("", "win", 'left='+(notRight?0:x)+',top='+y+',height='+height+',width='+width+',modal');
		opened_win.document.open();
		opened_win.focus();
		opened_win.document.writeln('<body bgcolor="#E0D9D1" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0">'+content+'</body>');
		opened_win.document.close();
	}
}

function smiles(n) {
	smile_n = (n = n || smile_n);
	var s1='<tr align=center>'+
		td1(n,1)+td1(n,2)+td1(n,3)+td1(n,4)+
		'</tr><tr><td align="center" colspan="4" style="padding:3px">';
	var path="";
	if(!nIE) path=location.href.substr(0,location.href.lastIndexOf('/'))+'/';
	for (var i=0; i<sm.length; i+=4) if(sm[i+3] == n)
		s1 += '<img src="'+path+'i/smile/'+sm[i]+'.gif" width="'+sm[i+1]+'" height="'+sm[i+2]+'" border="0" alt="'+sm[i]+'" title="'+sm[i]+'" onclick="'+(nIE ? 'parent' : 'window.opener')+'.Sz(\''+sm[i]+'\')" onMouseOver="this.style.backgroundColor=\'white\'" onMouseOut="this.style.backgroundColor=\'\'" /> ';
	s1 += '</td></tr>';
	OpenPopUp("smile", s1, 290, 265, true, false, false, true);
}
function checkbox_event(i,checkbox) {
	if (nIE) {
		//is IE
		var cb1=checkbox.document.getElementById("sw_filter_cb1_"+i)
		var cb2=checkbox.document.getElementById("sw_filter_cb2_"+i)
	} else {
		//is Mozilla
		var elt=checkbox.parentNode.nextSibling.nextSibling;
		var cb1=checkbox;
		var cb2=elt.firstChild
	}
	if (!cb1.checked) {
		cb2.disabled=true

	}
	if (cb1.checked) {
		if (cb2.disabled ) {
			cb2.disabled =false;
		}
	}


}
function sw_filter() { // фильтр чата
	var s1='<tr><td colspan="3" align="center"><b>'+mes_chat_filter+'</b></td></tr><tr><td style="font-size:11px" nowrap="nowrap">'+mes_chat_filter_frame1+'</td><td colspan="2" style="font-size:11px; padding: 2px 5px 2px 2px" nowrap="nowrap" align="right" ><nobr>'+mes_chat_filter_frame2+'</nobr></td></tr>', p;
	var arr = mes_filter_cmd;

	for (var i=0; i<arr.length; i++) {
		p = 1<<i;
		s1 += '<tr><td '+
		'nowrap="nowrap" style="padding:2px 2px 2px 10px">'+'<input id="sw_filter_cb1_'+i+'" onclick="'+(nIE ? 'parent' : 'window.opener')+'.set_filter('+p+'); '+(nIE ? 'parent' : 'window.opener')+'.checkbox_event('+i+',this);" type="checkbox" '+(p&chat_filter?'checked="checked"':'')+' /></td>'
		+'<td align="center" ><nobr>'+arr[i]+'</nobr></td>'+
		'<td align="center" style="padding:2px 10px 2px 2px" ><input id="sw_filter_cb2_'+i+'" '+((p&chat_filter)?'':'disabled="true"')+' onclick="'+(nIE ? 'parent' : 'window.opener')+'.set_SWin('+p+',this.checked)" type="checkbox" '+(p&split_win?'checked="checked"':'')+' /></td></tr>';
	}
	s1+='<tr><td colspan="3"><hr/></td></tr>';


	if (mes_word_filter) {
		p = 128;
		s1 += '<tr><td '+
		'nowrap="nowrap" style="padding:2px 2px 2px 10px">'+
		'<input onclick="'+(nIE ? 'parent' : 'window.opener')+'.set_filter('+p+')" type="checkbox" '+(p&chat_filter?'checked="checked"':'')+'/></td><td align="left">'+mes_word_filter+' (<a href="#" onclick="'+(nIE ? 'parent' : 'window.opener')+'.set_word_filter()">'+mes_word_filter_set+'</a>)</td></tr>';
	}
	s1+='<tr><td colspan="3"><hr/></td></tr>';
	if (mes_system_filter) {
		s1 += '<tr><td '+
		'nowrap="nowrap" style="padding:2px 2px 2px 10px" colspan="3">'+
		mes_system_filter + ' (<a href="#" onclick="'+(nIE ? 'parent' : 'window.opener')+'.set_system_filter_win(); return false;">'+mes_system_filter_set+'</a>)</td></tr>';
	}

	OpenPopUp("filter", s1, 360, 330, true, true);
}
function exists(elt, arr)
{
	for (var i in arr)
	{
		if (elt == arr[i])
		{
			return true;
		}
	}

	return false;
}
function cmd() { // командная строка

	var s1='';

	for (var i=0, n=0; i<chat_cmd.length; i++)
	{
		if (chat_cmd[i].charAt(0)!="#")
		{
			n++;

			s1 += '<tr><td style="cursor:pointer;" onclick="'+(nIE ? 'parent.doCmd(\''+chat_cmd[i]+'\')' : 'window.opener.doCmd(\''+chat_cmd[i]+'\');window.close();')+'" '+
			'onMouseOver="this.style.backgroundColor=\'white\'" onMouseOut="this.style.backgroundColor=\'\'" nowrap="nowrap">'+mes_chat_cmd[i]+'</td></tr>';
		}
	}

	OpenPopUp("cmd", s1, 415, 24*(n), true, true);
}
function sw_sort() {
	var s1 = '<tr><td '+
		'onMouseOver="this.style.backgroundColor=\'white\'" onMouseOut="this.style.backgroundColor=\'\'" nowrap="nowrap"> &nbsp; '+
		(mes_sort_by||"Sort by")+': <select onchange="'+(nIE ? 'parent' : 'window.opener')+'.set_sort(\'all\', this.value)">';
	var arr = mes_sort_cmd || ['name', 'level', 'clan'];
	for (var i=0; i<arr.length; i++) {
		s1 += '<option '+(i==sort_obj.all?'selected="selected"':'')+'value="'+i+'">'+arr[i]+' &nbsp;</option>';
	}
	s1 += '</select>&nbsp;</td></tr>';
	arr = mes_sort_val || ['show your name always on top', 'show your clan always on top', 'show merchants always on top',"don't show the ranks"];
	for (i=0; i<arr.length; i++) {
		s1 += '<tr><td '+
		'onMouseOver="this.style.backgroundColor=\'white\'" onMouseOut="this.style.backgroundColor=\'\'" nowrap="nowrap">'+
		'<input onclick="'+(nIE ? 'parent' : 'window.opener')+'.set_sort(\''+sort_key[i]+'\', this.checked?1:0)" type="checkbox" '+(sort_obj[sort_key[i]]?'checked="checked"':'')+' />'+arr[i]+' &nbsp;</td></tr>';
	}

	OpenPopUp("sorting", s1, 300, 140, true, true);
}
// показываем список сканирующих
function show_scans() {
	if (!isVip) return;
	var s1 = '<tr><td align="center" style="padding:0 5px 0 5px">'+mes_time+'</td><td align="center" style="padding:0 5px 0 5px;border:1px solid black;border-width:0 1px 0 1px">'+mes_pers+'</td><td align="center" style="padding:0 5px 0 5px">'+mes_count+'</td></tr>';
	if (scans.length>0) {
		for (var i=0; i<scans.length; i++) {
			s1 += '<tr><td align="center" style="padding:0 5px 0 5px"><font class="date">'+scans[i].date+'</font></td>'+
			'<td align="center" style="padding:0 5px 0 5px;border:1px solid black;border-width:0 1px 0 1px;cursor:pointer;cursor:hand" nowrap="nowrap" '+
			'onclick="'+(nIE ? 'parent' : 'window.opener' )+'.Info(\''+scans[i].login+'\')" '+
			'onMouseOver="this.style.backgroundColor=\'white\'" onMouseOut="this.style.backgroundColor=\'\'">'+
			'<b>'+scans[i].login+'</b></td>'+
			'<td align="center" style="padding:0 5px 0 5px">'+scans[i].count+'</td></tr>';
		}
	} else { s1 += '<tr>'; for (var i=0; i<3; i++) s1 += '<td align="center">-</td>'; s1 += '</tr>' }
	s1 += '<tr><td colspan="3" style="border-top:1px solid black" nowrap="nowrap"><input onclick="'+(nIE ? 'parent' : 'window.opener')+'.set_scans()" type="checkbox" '+(scan_f?'checked="checked"':'')+' />'+mes_scan+'</td></tr>';
	OpenPopUp("scans", s1, 250, 200, true, true, true);
	//divPopUpOpenLeftBottom(s1, 0, document.body.offsetHeight - cmd_container.offsetHeight, 200, 100,true);
	light_lamp(1);

}

// показываем историю
function show_history() {
	if (chat_length<=0) return;
	var s1='', s2, k, j, cl;
	for (var i=0; i<chat_length; i++) {
		k = 0;
		s2 = '<table width="100%"><tr><td nowrap="nowrap">';
		if (chat_history[i].length<=5) s2+=chat_history[i][0];
		else
		for (j=1; j<chat_history[i].length; j++, k++) {
			if (k==4) { k=0; s2+='</td></tr><tr><td nowrap="nowrap">' };
			s2 += chat_history[i][j]+"</b> ";
		}
		s2 += "</td></tr></table>";

		s1 += '<tr><td '+(chat_val==i?'style="background-color:#D0C0B0;"':'')+' onclick="setHistory('+i+');divPopUpClose();" '+
		'onMouseOver="this.style.backgroundColor=\'white\'" onMouseOut="this.style.backgroundColor=\''+(chat_val==i?'#D0C0B0':'')+'\'" nowrap="nowrap">'+s2+'</td></tr>';
	}
	divPopUpOpenLeftBottom(s1, 0, document.body.offsetHeight - cmd_container.offsetHeight, 200, 100,true);
}
// добавляем в историю сообщение
function add_history(txt) {
	var post=[], c=0, i, j, k, s;
	post[0] = '';
	while ((i=txt.indexOf("to [", c))>=0 && (j=txt.indexOf("]", i))>=0) {
		s = txt.substring(i, j+1);
		if (txt.indexOf(s)>=c) { post[post.length]=s; post[0]+=s+" "; }
		c = j+1;
	}
	c = 0;
	while ((i=txt.indexOf("private [", c))>=0 && (j=txt.indexOf("]", i))>=0) {
		s = txt.substring(i, j+1);
		if (txt.indexOf(s)>=c) { post[post.length]=s; post[0]+=s+" "; }
		c = j+1;
	}
	if (post.length<=1) return;
	c = true;
	for (i=0; i<chat_length; i++) {
		if (chat_history[i].length!=post.length) continue;
		for (j=0; j<post.length && c; j++)
		for (k=j; k<post.length && c; k++)
			if (chat_history[i][j]==post[k]) c = false;
		if (!c) {
			chat_length--;
			for (j=i; j<chat_length; j++) chat_history[j] = chat_history[j+1];
			c=true;
		}
	}
	if (chat_length>=10) {
		chat_length--;
		for (i=0; i<chat_length; i++) chat_history[i] = chat_history[i+1];
	}
	chat_history[chat_length]=post;
	chat_val = (++chat_length);
	if (nIE) oPopup.hide();
}
function clearc() {

	if (cmd_form.text.value == '') {
		if (split_win<127)
		if (confirm(mes_clearchat1)) {
			cache = [];
			ClearChat1();
		}

		if (split_win>0)
		if (confirm(mes_clearchat2)) {
			cache = [];
			ClearChat2();
		}
	} else { cmd_form.text.value=''; }
	cmd_form.text.focus();
}

function do_exit() // выход
{
	if (confirm(mes_exitgame)) getFlashElement().gameLogOut();
}

function gKey2(evt)
{
	evt = (evt) ? evt : event;

	var charCode = evt.charCode || evt.keyCode || evt.which || 0;

	if (IE && event.ctrlKey && ((event.keyCode == 10)||(event.keyCode == 13)))
	{
		if (subm()) cmd_form.submit();
	}
	else if (charCode == 9)
	{
		return false;
	}
	else if (charCode == 38 || charCode == 40 && chat_length) // отлавливаем верх и низ
	{
		var val;

		if (charCode == 38 && chat_val > 0) val = chat_val - 1;
		else if (charCode == 40 && chat_val < chat_length) val = chat_val + 1;
		else val = chat_val;
		setHistory(val);
		if (val < chat_length) show_history();
	}

	return true;
}

function setHistory(val)
{
	if (nIE) oPopup.hide();

	var cur = (chat_val < chat_length ? chat_history[chat_val][0] : '');

	if (chat_val==chat_length)
	{
		chat_mess = cmd_form.text.value;
	}
	else if (cur+chat_mess!=cmd_form.text.value)
	{
		var i;

		if ((i=cmd_form.text.value.indexOf(cur, 0))>=0)
		{
			chat_mess = cmd_form.text.value.substring(0, i) + cmd_form.text.value.substring(i + cur.length);
		}
		else
		{
			chat_mess = cmd_form.text.value;
		}
	}

	chat_val = val;
	cmd_form.text.value = (chat_val < chat_length ? chat_history[chat_val][0] : '') + chat_mess;
	cmd_form.text.focus();
}

function traceBits(num) {
	var s = num+':\n';
	for (var i =0; i<30;i++) {
		if ((1<<i) && num) {
			s+=i+ " —   true\n"
		} else {
			s+=i+ " —false\n"
		}
	}
	alert(s);
}