// Nugged's Ajax chat engine.
// (c) Andrew Nugged, 2007-2008. mailto:nugged@ladoshki.com
// free code distribution prohibited!

function PrintChatVersion() { document.write('v1.10'); }

var aChat = new Array();
// 0 - id
// 1 - time
// 2 - user
// 3 - text
// 4 - extra info
var aPriv = new Array();
// 0 - id
// 1 - time
// 2 - from
// 3 - to
// 4 - text
// 5 - extra info
var aUsers = new Array();
// 0 - id
// 1 - name
// 2 - status
// 3 - extra info
// 4 - gender
var aRooms = new Array();
// 0 - id
// 1 - name
// 2 - online
// 3 - rights
// 4 - extra info

var aConfig = new Array();

var last_priv_user = '';
var last_priv_user_from = '';
var last_chat_user_from = '';

var prev_message = '';
var typoactivity = null;

var current_room = 0;
var refresh_rooms = 1;
var refresh_users = 1;
var vertical_mode = false;

var bDebug = true;

var sleepTimer = 0;
var sleepWish = 0;

var r_request = new Object();
var r_url = null;

var max_messages = null;
var last_msg_id = null;
var last_pvt_id = null;
var room_timestamp = 0;

var txt_runner = '[--»»»---»»»-]';
var send_mode = false;

var prev_key_enter = false;
var r_timeout = null;

var req_timeout_id = null;
var cancel_timeout_id = null;

var ch_kicked = false;
var ch_stop = false;

var b_mouseover_chat = false;
var b_mouseover_privat = false;
var b_firstrun = true;

if( typeof(nugged_common_ver) == 'undefined' || nugged_common_ver < 1 )
{
	document.write('<script language="JavaScript" type="text/javascript" src="http://files.ladoshki.com/data/common/js/nugged_common.js"></script>');
}

if( typeof(nugged_chat_lib_ver) == 'undefined' || nugged_chat_lib_ver < 1 )
{
	document.write('<script language="JavaScript" src="/s/chat_lib.js" type="text/javascript"></script>');
}

function sendMessage()
{
	if(ch_kicked || ch_stop)
	{
		FullStop();
		return;
	}

	if(send_mode
	   || !document.getElementById('chat_send')
	   || !document.getElementById('chat_message')
	   || document.getElementById('chat_send').disabled
	   || document.getElementById('chat_message').value == '')
	{
		return;
	}

	var el_msg = document.getElementById('chat_message');
	var msg = escape(el_msg.value).replace(/\+/g,'%2B');
	if(is_opera && msg.length>7000)
	{
		alert('Message too long for Opera. Optimal is 7000 bytes (1166 cyrillic symbols!). Your message has '
			+ msg.length + ' bytes in encoded for web-send format.');
		return;
	}
	else if(is_firefox && msg.length>7000)
	{
		alert('Message too long for firefox. Optimal is 7000 bytes (1166 cyrillic symbols!). Your message has '
			+ msg.length + ' bytes in encoded for web-send format.');
		return;
	}
	else if(is_ie && msg.length>1800)
	{
		alert('Message too long for IE. Optimal is 1800 bytes (300 cyrillic symbols!). Your message has '
			+ msg.length + ' bytes in encoded for web-send format.');
		return;
	}

	send_mode = true;
	document.getElementById('chat_send').disabled = true;
	document.getElementById('chat_send').value = 'sending';
	ScrollChatDown(true);

	var el_name = document.getElementById('chat_privname');
	var name = escape(el_name.value).replace(/\+/g,'%2B');
	prev_message = el_msg.value;
	el_msg.value = '';

	document.getElementById('chat_status').innerHTML = '<b>preparing</b>';

	if(r_request.v)
	{
		r_request.v.abort();
		r_request = new Object();
	}



	typoactivity = null;
	var mm = get_maxmsg();
	r_url = '/do/engine?mode=chat&room=' + current_room
		+ ( refresh_rooms ? '&getrooms=' + refresh_rooms : '&rts=' + room_timestamp )
		+ '&getusers=' + refresh_users
		+ ( last_msg_id ? '&after=' + last_msg_id : '&get=' + mm )
		+ ( last_pvt_id ? '&pvtafter=' + last_pvt_id : '&pvtget=' + mm )
		+ ( name ? '&private=' + name : '')
		+ '&msg=' + msg;
	request_url(r_url,request_callback,r_request,true);
	el_msg.value = '';
	if(!document.getElementById('chat_preferpriv').checked)
		el_name.value = '';
	el_msg.focus();
}

function ReplyTo(u,forced, noreverse)
{
	var e = document.getElementById('chat_privname');
	var e1 = document.getElementById('chat_privateon');

	if(forced==true || (noreverse==true || e1.checked) && e.value != u)
	{
		e.value = u;
		last_priv_user = u;
	}
	else
	{
		e.value = '';
		PutInText('[b]' + u + '[/b]: ');
	}
	document.getElementById('chat_message').focus();
}

function RefreshChat()
{
	var mm = get_maxmsg();
	while( aChat.length > mm && (b_firstrun || !b_mouseover_chat) )
		aChat.shift();

	var old_scroll = document.getElementById('chat_messages').scrollTop;

	var s = '';
	for(var i=0; i < aChat.length; i++)
	{
		if(aChat[i][4] == 'bot')
		{
			s += '<p class="pch_bot"><span class="msgdate">'
				+ aChat[i][1] + '</span>&nbsp;'
				+ ( aChat[i][2] ? '<a class="li" href="#" onclick="ReplyTo(\''
					+ aChat[i][2] + '\'); return false;">'
					+ aChat[i][2] + '</a>&nbsp;' : '')
				+ aChat[i][3]
				+ '</p>';
		}
		else if(aChat[i][4] == 'pch_medo')
		{
			s += '<p class="pch"><span class="msgdate">'
				+ aChat[i][1] + '</span>&nbsp;<span class="ch_me">*&nbsp;<b><a href="#" onclick="ReplyTo(\''
				+ aChat[i][2] + '\'); return false;">'
				+ aChat[i][2] + '</a></b>&nbsp;'
				+ aChat[i][3]
//				+ (aChat[i][4] ? '&nbsp;<small style="color: #888888;">' + aChat[i][4] + '</small>' : '')
				+ '</span></p>';
			last_chat_user_from = aChat[i][2];
		}
		else if(aChat[i][4] == 'pch_tome')
		{
			s += '<p class="pch_tome"><span class="msgdate">'
				+ aChat[i][1] + '</span>&nbsp;<b><a href="#" class="li" onclick="ReplyTo(\''
				+ aChat[i][2] + '\'); return false;">'
				+ aChat[i][2] + '</a></b>:&nbsp;'
				+ aChat[i][3]
//				+ (aChat[i][4] ? '&nbsp;<small style="color: #888888;">' + aChat[i][4] + '</small>' : '')
				+ '</p>';
			last_chat_user_from = aChat[i][2];
		}
		else if(aChat[i][4] == 'pch_self')
		{
			s += '<p class="pch_self"><span class="msgdate">'
				+ aChat[i][1] + '</span>&nbsp;<b>'
				+ aChat[i][2] + '</b>:&nbsp;'
				+ aChat[i][3]
//				+ (aChat[i][4] ? '&nbsp;<small style="color: #888888;">' + aChat[i][4] + '</small>' : '')
				+ '</p>';
		}
		else

		{
			s += '<p class="pch"><span class="msgdate">'
				+ aChat[i][1] + '</span>&nbsp;<b><a href="#" class="li" onclick="ReplyTo(\''
				+ aChat[i][2] + '\'); return false;">'
				+ aChat[i][2] + '</a></b>:&nbsp;'
				+ aChat[i][3]
//				+ (aChat[i][4] ? '&nbsp;<small style="color: #888888;">' + aChat[i][4] + '</small>' : '')
				+ '</p>';
			last_chat_user_from = aChat[i][2];
		}
	}

	var e = document.getElementById('chat_messages');
	if(e) e.innerHTML = s;

	if(!b_firstrun && b_mouseover_chat)
		document.getElementById('chat_messages').scrollTop = old_scroll;
	else
		ScrollChatDown(b_firstrun);

}

function RefreshPrivate()
{
	var mm = get_maxmsg();
	while( aPriv.length > mm && (b_firstrun || !b_mouseover_chat) )
		aPriv.shift();

	var old_scroll = null;
	if(!b_firstrun && b_mouseover_privat)
		old_scroll = document.getElementById('chat_private_msgs').scrollTop;

	var s = '';
	for(var i=0; i < aPriv.length; i++)
	{
		if(aPriv[i][5] == 'bot')
		{
			s += '<p class="pprv_bot"><span class="msgdate">'
				+ aPriv[i][1] + '</span>&nbsp;'
				+ ( aPriv[i][2] ? '<a class="li" href="#" class="li" onclick="ReplyTo(\''
					+ aPriv[i][2] + '\',true); return false;">'
					+ aPriv[i][2] + '</a>&nbsp;' : '')
				+ ( aPriv[i][3] ? '»&nbsp;<a class="li" href="#" class="li" onclick="ReplyTo(\''
					+ aPriv[i][3] + '\',true); return false;">'
					+ aPriv[i][3] + '</a>&nbsp;' : '')
				+ aPriv[i][4]
				+ '</p>';
		}
		else if(aPriv[i][2])
		{
			s += '<p class="pprv"><span class="msgdate">'
				+ aPriv[i][1] + '</span>&nbsp;'
				+ '<b><a href="#" class="li" onclick="ReplyTo(\''
				+ aPriv[i][2] + '\',true); return false;">'
				+ aPriv[i][2] + '</a></b>'
				+ ( aPriv[i][3] ? 'для <b><a href="#" class="li" onclick="ReplyTo(\''
					+ aPriv[i][3] + '\',true); return false;">'
					+ aPriv[i][3] + '</a></b>' :
						(aPriv[i][2] ? '' : 'себе') )
				+ ':&nbsp;'
				+ aPriv[i][4]
				+ '</p>';
			last_priv_user_from = aPriv[i][2];
		}
		else
		{
			s += '<p class="pprv_self"><span class="msgdate">'
				+ aPriv[i][1] + '</span>&nbsp;'
				+ ( aPriv[i][3] ? 'для <b><a href="#" class="li" onclick="ReplyTo(\''
					+ aPriv[i][3] + '\',true); return false;">'
					+ aPriv[i][3] + '</a></b>' :
						(aPriv[i][2] ? '' : 'себе') )
				+ ':&nbsp;'
				+ aPriv[i][4]
//				+ (aPriv[i][5] ? '&nbsp;<small style="color: #888888;">' + aPriv[i][5] + '</small>' : '')
				+ '</p>';
		}
	}
	var e = document.getElementById('chat_private_msgs');
	if(e) e.innerHTML = s;

	if(old_scroll)
		document.getElementById('chat_private_msgs').scrollTop = old_scroll;
	else
		ScrollPrivatDown(b_firstrun);
}

function ShowEUIL(pos,uid)
{
	var e = document.getElementById('extra_userinfo');
	var ew = e.style.width.substr(0,e.style.width.indexOf('px')-1);
	e.style.left = (pos[0]+15) + 'px';
	e.style.top = (pos[1]+35) + 'px';

	if(window.innerWidth && (pos[0]+ew) > window.innerWidth)
	{
		e.style.left = (window.innerWidth - ew - 15) + 'px';
	}
	if (document.body.scrollWidth && (pos[0]+ew) > document.body.scrollWidth)
	{
		e.style.left = (document.body.scrollWidth - ew - 15) + 'px';
	}
	e.innerHTML = aUsers[uid][3];
	e.style.display = '';
}

function HideEUIL()
{
	document.getElementById('extra_userinfo').style.display = 'none';
}

function RefreshUsers()
{
	var s = '';
	for(var i=0; i < aUsers.length; i++)
	{
		var u_name = '';
		s += '<p class="puser';
		if(aUsers[i][1].substr(0,1) == '?')
		{
			s += '_anonym';
			u_name = '<a href="#" onclick="ReplyTo(\''
				+ aUsers[i][1] + '\', null, true); return false;" '
				+ '>'
				+ aUsers[i][1]
//				+ aUsers[i][1].substr(1)
				+ '</a>';
		}
		else
		{
			u_name = '<a href="#" onclick="ReplyTo(\''
				+ aUsers[i][1] + '\', null, true); return false;" '
				+ '>'
				+ aUsers[i][1] + '</a>'
				+ '<a target="_blank" href="http://forum.ladoshki.com/profile.php?mode=viewprofile&u='
				+ aUsers[i][0] + '" '
				+ (aUsers[i][3] ? 'onMouseOver="ShowEUIL(findPosition(this),' + i + ');" onMouseOut="HideEUIL();"' : '')
				+ '>'
				+ (
				   	aUsers[i][4] == 'm' ? '<img border=0 align=absmiddle hspace=3 width=11 height=15 src="http://f.ladoshki.com/templates/subSilver/images/icon_male.gif">' :
				   	aUsers[i][4] == 'f' ? '<img border=0 align=absmiddle hspace=3 width=11 height=15 src="http://f.ladoshki.com/templates/subSilver/images/icon_female.gif">' :
				'&nbsp;i')
				+ '</a>';
		}
		s += '">'
			+ (aUsers[i][2] == 'm' ? '@&nbsp;' :
					aUsers[i][2] == 'r' ? '-&nbsp;' : '')
			+ u_name
			+ '</p>';
	}
	var e = document.getElementById('chat_users');
	if(e) e.innerHTML = s;
}

function findPosition(oLink)
{
	if (oLink.offsetParent)
	{
		for (var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent)
		{
			posX += oLink.offsetLeft;
			posY += oLink.offsetTop;
		}
		return [posX, posY];
	}
	else
	{
		return [oLink.x, oLink.y];
	}
}

function ChangeRoom(r)
{
	if(confirm('Желаете перейти в другую комнату?'))
	{
		current_room = r;
		refresh_rooms = 1;
		refresh_users = 1;
		last_msg_id = null;
		b_firstrun = true;
		aUsers = [];
		aRooms = [];
		aChat = [];

		if(req_timeout_id)
			clearTimeout(req_timeout_id);
		if(r_request.v)
			r_request.v.abort();

		var mm = get_maxmsg();
		r_url = '/do/engine?mode=chat&room=' + current_room
			+ ( refresh_rooms ? '&getrooms=' + refresh_rooms : '&rts=' + room_timestamp )
			+ '&getusers=' + refresh_users
			+ ( last_pvt_id ? '&pvtafter=' + last_pvt_id : '&pvtget=' + mm )
			+ ( typoactivity ? '&typac=1' : '' )
			+ '&get=' + mm;
		current_room = 0;
		request_url(r_url,request_callback,r_request,true);
	}
}

function RefreshRooms()
{
	var s = '';
	for(var i=0; i < aRooms.length; i++)
	{
		// readonly,inviteonly,hidden,disabled,noanonym,roanonym
		if((aRooms[i][3]).indexOf('hidden')>=0)
		{
			s += '<p class="proom_hidden">'
				+ '<a href="#" onclick="ChangeRoom(' + aRooms[i][0] + '); return false;">'
				+ aRooms[i][1] + '</a>'
				+ '&nbsp;[' + aRooms[i][2] + ']</p>';
		}
		else if((aRooms[i][3]).indexOf('invite')>=0)
		{
			s += '<p class="proom_invite">'
				+ '<a href="#" onclick="ChangeRoom(' + aRooms[i][0] + '); return false;">'
				+ aRooms[i][1] + '</a>'
				+ '&nbsp;[' + aRooms[i][2] + ']</p>';
		}
		else if((aRooms[i][3]).indexOf('readonly')>=0)
		{
			s += '<p class="proom_readonly">'
//				+ '<a href="http://chat.ladoshki.com/do/engine?room=' + aRooms[i][0]
//				+ '" onclick="return true; ChangeRoom(' + aRooms[i][0] + '); return false;">'
				+ '<a href="#" onclick="ChangeRoom(' + aRooms[i][0] + '); return false;">'
				+ aRooms[i][1] + '</a>'
				+ '&nbsp;[' + aRooms[i][2] + ']</p>';
		}
		else if((aRooms[i][3]).indexOf('disabled')>=0)
		{
			s += '<p class="proom_disabled">' + aRooms[i][1] + '</p>';
		}
		else
//		if(!aRooms[i][3] || aRooms[i][3] == 'noanonym')
		{
			s += '<p class="proom_normal">'
				+ '<a href="#" onclick="ChangeRoom(' + aRooms[i][0] + '); return false;">'
				+ aRooms[i][1] + '</a>'
				+ '&nbsp;[' + aRooms[i][2] + ']</p>';
		}
	}
	var e = document.getElementById('right_column_rooms_sub');
	if(e) e.innerHTML = s;
}

function ProcessChatMsg(msg)
{
	var error = null;
	var aMsg = msg.split('\t');
	var takeFocus = false;

	if(aMsg.length > 2)
	{
		var global_error = aMsg.pop();
		if(global_error)
		{
			document.getElementById('info_errors_sub').innerHTML = global_error;
		}
		var global_warning = aMsg.pop();
		if(global_warning)
		{
			document.getElementById('info_warnings_sub').innerHTML = global_warning;
		}

		while(aMsg.length>0)
		{
			var command = null;
			var subcommand = null;
			var maincommand = null;

			command = aMsg.shift();

			if(command.indexOf(' ')>=0)
			{
				maincommand = command.substr(0,command.indexOf(' '));
				subcommand = command.substr(command.indexOf(' ')+1);
				// if .... subcommand parser
				// else
					error += '<br>Error: unknown subcommand from server: [' + subcommand + ']/' + aMsg.length;


				if(bDebug && document.getElementById('chat_debug'))
					document.getElementById('chat_debug').innerHTML += ' S' + subcommand;

			}
			else
				maincommand = command;

			if(maincommand == 's')
			{
				aMsg = [];
				// NO OPERATION: stop & skip...
			}
			else if(maincommand == 'm' && aMsg.length >= 5)
			{
				if(!last_msg_id || parseInt(aMsg[0]) > last_msg_id)
				{
					aChat.push([ parseInt(aMsg[0]), aMsg[1], aMsg[2], aMsg[3], aMsg[4] ]);
					last_msg_id = parseInt(aMsg[0]);
					takeFocus = true;
				}
				if(send_mode)
					MakeSound('msgout');
				else if(aMsg[4] == 'bot' && (aMsg[3].indexOf('logged out')>=0 || aMsg[3].indexOf('leaves')>=0))
					MakeSound('usrout');
				else if(aMsg[4] == 'bot' && aMsg[3].indexOf('enters')>=0)
					MakeSound('usrin');
				else
					MakeSound('msgin');
				aMsg.splice(0,5);
				RefreshChat();
			}
			else if(maincommand == 'pm' && aMsg.length >= 6)
			{
				if(!last_pvt_id || parseInt(aMsg[0]) > last_pvt_id)
				{
					aPriv.push([ parseInt(aMsg[0]), aMsg[1], aMsg[2], aMsg[3], aMsg[4], aMsg[5] ]);
					last_pvt_id = parseInt(aMsg[0]);
					TogglePrivate(true);
					takeFocus = true;
				}
				aMsg.splice(0,6);
				RefreshPrivate();
				MakeSound('msgin');
			}
			else if(maincommand == 'b' && aMsg[0]*5 < aMsg.length)
			{
				for(var i=1; i<aMsg[0]*5; i+=5)
					if(!last_msg_id || parseInt(aMsg[i]) > last_msg_id)
					{
						aChat.push([ parseInt(aMsg[i]), aMsg[i+1], aMsg[i+2], aMsg[i+3], aMsg[i+4] ]);
						last_msg_id = parseInt(aMsg[i]);
						takeFocus = true;
					}
				MakeSound('msgin');
				aMsg.splice(0,aMsg[0]*5+1);
				RefreshChat();
			}
			else if(maincommand == 'pb' && aMsg[0]*6 < aMsg.length)
			{
				for(var i=1; i<aMsg[0]*6; i+=6)
					if(!last_pvt_id || parseInt(aMsg[i]) > last_pvt_id)
					{
						aPriv.push([ parseInt(aMsg[i]), aMsg[i+1], aMsg[i+2], aMsg[i+3], aMsg[i+4], aMsg[i+5] ]);
						last_pvt_id = parseInt(aMsg[i]);
						TogglePrivate(true);
						takeFocus = true;
					}
				aMsg.splice(0,aMsg[0]*6+1);
				RefreshPrivate();
				MakeSound('msgin');
			}
			else if(maincommand == 'f' && aMsg[0]*5 < aMsg.length)
			{
				aChat = new Array();
				for(var i=1; i<aMsg[0]*5; i+=5)
				{
					aChat.push([ parseInt(aMsg[i]), aMsg[i+1], aMsg[i+2], aMsg[i+3], aMsg[i+4] ]);
					last_msg_id = parseInt(aMsg[i]);
					takeFocus = true;
				}
				aMsg.splice(0,aMsg[0]*5+1);
				RefreshChat();
				MakeSound('msgin');
			}
			else if(maincommand == 'pf' && aMsg[0]*6 < aMsg.length)
			{
				aPriv = new Array();
				for(var i=1; i<aMsg[0]*6; i+=6)
				{
					aPriv.push([ parseInt(aMsg[i]), aMsg[i+1], aMsg[i+2], aMsg[i+3], aMsg[i+4], aMsg[i+5] ]);
					last_pvt_id = parseInt(aMsg[i]);
					TogglePrivate(true);
					takeFocus = true;
				}
				aMsg.splice(0,aMsg[0]*6+1);
				RefreshPrivate();
				MakeSound('msgin');
			}
			else if(maincommand == 'sm' && aMsg.length >= 5)
			{
				aChat.push([ parseInt(aMsg[0]), aMsg[1], aMsg[2], aMsg[3], aMsg[4] ]);
				aMsg.splice(0,5);
				RefreshChat();
				takeFocus = true;
				MakeSound('msgin');
			}
			else if(maincommand == 'popup' && aMsg.length >= 5)
			{
				var popup_text = aMsg[3];
				aChat.push([ parseInt(aMsg[0]), aMsg[1], aMsg[2], aMsg[3], aMsg[4] ]);
				aMsg.splice(0,5);
				RefreshChat();
				takeFocus = true;
				MakeSound('msgin');
				alert(popup_text);
			}
			else if(maincommand == 'sp' && aMsg.length >= 6)
			{
				aPriv.push([ parseInt(aMsg[0]), aMsg[1], aMsg[2], aMsg[3], aMsg[4], aMsg[5] ]);
				aMsg.splice(0,6);
				RefreshPrivate();
				MakeSound('msgin');
				takeFocus = true;
			}
			else if(maincommand == 'u' && aMsg[0]*5 < aMsg.length)
			{
				refresh_users = 0;
				aUsers = [];
				for(var i=1; i<aMsg[0]*5; i+=5)
					aUsers.push([ parseInt(aMsg[i]), aMsg[i+1], aMsg[i+2], aMsg[i+3], aMsg[i+4]  ]);
				aMsg.splice(0,aMsg[0]*5+1);
				RefreshUsers();
			}
			else if(maincommand == 'r' && aMsg[0]*5 < aMsg.length+1)
			{
				refresh_rooms = 0;
				aRooms = [];
				for(var i=1; i<aMsg[0]*5; i+=5)
					aRooms.push([ parseInt(aMsg[i]), aMsg[i+1], aMsg[i+2], aMsg[i+3], aMsg[i+4] ]);
				aMsg.splice(0,aMsg[0]*5+1);
				room_timestamp = aMsg.shift();
				RefreshRooms();
			}
			else if(maincommand == 'stm' && aMsg.length >= 5) // STOP + message
			{
				aChat.push([ parseInt(aMsg[0]), aMsg[1], aMsg[2], aMsg[3], aMsg[4] ]);
				RefreshChat();
				aMsg = [];
				FullStop();
				document.getElementById('chat_status').innerHTML = '<b>stopped by server</b>';
				takeFocus = true;
				MakeSound('msgin');
			}
			else if(maincommand == 'st' && aMsg.length >= 5) // STOP.
			{
				aMsg = [];
				FullStop();
				document.getElementById('chat_status').innerHTML = '<b>stopped by server</b>';
				MakeSound('msgin');
			}
			else if(maincommand == 'refresh')
			{
				if(last_msg_id || last_pvt_id)
				{
					location.reload(true);
				}
			}
			else if(maincommand == 'ru' || maincommand == 'ra')
			{
				refresh_users = 1;
			}
			else if(maincommand == 'rr' || maincommand == 'ra')
			{
				refresh_rooms = 1;
			}
			else if(maincommand == 'rs'|| maincommand == 'ra')
			{
				last_msg_id = null;
				last_pvt_id = null;
//				aChat = [];
//				aPriv = [];
			}
			else if(maincommand == 'rsc'|| maincommand == 'ra')
			{
				last_msg_id = null;
//				aChat = [];
			}
			else if(maincommand == 'rsp'|| maincommand == 'ra')
			{
				last_pvt_id = null;
//				aPriv = [];
			}
			else if(maincommand == 'e' && aMsg.length >= 5)
			{
				aChat.push([ parseInt(aMsg[0]), aMsg[1], aMsg[2], aMsg[3], aMsg[4] ]);
				RefreshChat();
				aMsg = [];
				FullStop();
				MakeSound('msgin');
				document.getElementById('chat_status').innerHTML = '<b>stopped</b>';
				if(confirm('Выход из чата выполнен. Закрыть окно броузера?'))
					closeWindow();
				return;
			}
			else if(maincommand == 'k' && aMsg.length >= 5)
			{
				aChat.push([ parseInt(aMsg[0]), aMsg[1], aMsg[2], aMsg[3], aMsg[4] ]);
				RefreshChat();
				aMsg = [];
				ch_kicked = true;
				FullStop();
				MakeSound('msgin');
				document.getElementById('chat_status').innerHTML = '<b>kicked</b>';
				closeWindow();
				return;
			}

			else if(maincommand == 'bn' && aMsg.length >= 5)
			{
				aChat.push([ parseInt(aMsg[0]), aMsg[1], aMsg[2], aMsg[3], aMsg[4] ]);
				RefreshChat();
				aMsg = [];
				ch_kicked = true;
				FullStop();
				MakeSound('msgin');
				document.getElementById('chat_status').innerHTML = '<b>banned</b>';
				if(aChat.length>1)
					closeWindow();
				return;
			}
			else if(maincommand == 'go' && aMsg.length >= 5)
			{
//				aChat.push([ parseInt(aMsg[0]), aMsg[1], aMsg[2], aMsg[3], aMsg[4] ]);
//				RefreshChat();
				FullStop();
				location.href = aMsg[3];
				aMsg = [];
			}
			else if(maincommand.substr(0,4) == 'req_' && aMsg.length >= 5)
			{
				var popup_text = aMsg[3];
				aMsg.splice(0,5);
				var tmp_name = null;
				MakeSound('msgin');
				document.getElementById('NewUserReg_info').innerHTML = popup_text;
				document.getElementById('NewUserReg').style.display = '';
				document.getElementById('NewUserReg_name').focus();
				document.getElementById('NewUserReg_kun').value = maincommand.substr(4);
			}
			else if(maincommand == 't' && aMsg.length >= 5)
			{
				var e = document.getElementById('room_name');
				if(e) e.innerHTML = '<small><b>' + aMsg[0] + '</b> | ' + aMsg[1]
					+ ' <a class="lb" target="_blank" href="http://forum.ladoshki.com/profile.php?mode=viewprofile&u='
					+ aMsg[2] + '">' + aMsg[3] + '</a> установил топик:</small><br /><big><b>' + aMsg[4] + '</b></big>';
				aMsg.splice(0,5);
				MakeSound('msgin');
			}
			else
			{
				error += '<br>Error: unknown command from server: [' + maincommand + ']/' + aMsg.length;
				aMsg = [];
			}

			if(bDebug && document.getElementById('chat_debug'))
				document.getElementById('chat_debug').innerHTML += ' ' + maincommand;

		}

	}
	else
		error += '<br>Error in server answer: malformed block of data [' + maincommand + ']/' + aMsg.length;

	if(error)
	{
		aChat.push([ -1, '', '', error, '' ]);
		RefreshChat();
		MakeSound('msgin');
	}

	var tm = get_timer();
	if(tm && !r_request.v && !ch_stop)
	{
		if(req_timeout_id)
			clearTimeout(req_timeout_id);

		var mm = get_maxmsg();
		r_url = '/do/engine?mode=chat&room=' + current_room
			+ ( refresh_rooms ? '&getrooms=' + refresh_rooms : '&rts=' + room_timestamp )
			+ '&getusers=' + refresh_users
			+ ( typoactivity ? '&typac=1' : '' )
			+ ( last_msg_id ? '&after=' + last_msg_id : '&get=' + mm )
			+ ( last_pvt_id ? '&pvtafter=' + last_pvt_id : '&pvtget=' + mm )
		req_timeout_id = setTimeout('request_url(r_url,request_callback,r_request,true)', tm);
	}
	b_firstrun = false;

	if(takeFocus && document.getElementById('focus_on_nm').checked)
	{
		window.focus();
		document.forms.chatForm.chat_message.focus();
	}
}

function ScrollPrivatDown(forced)
{
	if(forced || !b_mouseover_privat)
	{
		var e = document.getElementById('chat_private_msgs');
		e.scrollTop = e.scrollHeight; // + 26;
	}
}

function ScrollChatDown(forced)
{
	if(forced || !b_mouseover_chat)
	{
		var e = document.getElementById('chat_messages');
		e.scrollTop = e.scrollHeight; // + 26;
	}
}

function ChatHeight_percent2px(percents,additive)
{
	return parseInt( Math.floor( (getBrowserHeight()-(is_opera ? 212 : 225))*(percents/100)+additive ))+'px';
}

function TogglePrivateVertical(forced)
{
	var e2=document.getElementById('chat_private_msgs');
	if(forced==true)
	{
		ToggleDisplay('chat_private_msgs',true)
		ScrollChatDown(true);
		ScrollPrivatDown(true);
	}
	else if(forced==false)
	{
		ToggleDisplay('chat_private_msgs',false)
		b_mouseover_privat=false;
	}
	else
	{
		if(ToggleDisplay('chat_private_msgs'))
		{
			if(e2.style.width=='50%')
			{
				e2.style.width='30%';
			}
			else if(e2.style.width=='30%')
			{
				e2.style.width='50%';
			}
			ScrollChatDown(true);
			ScrollPrivatDown(true);
		}
		else
		{
			b_mouseover_privat=false;
		}
	}
}

function TogglePrivate(forced)
{
	if(vertical_mode)
	   return TogglePrivateVertical(forced);

	var e1=document.getElementById('chat_messages');
	var e2=document.getElementById('chat_private');
	var e3=document.getElementById('chat_private_msgs');
	if(forced==true)
	{
		if(e1.style.height=='100%')
		{
			e1.style.height='75%';
			e2.style.height='25%';
			e3.style.height = ChatHeight_percent2px(25,-14);
			ToggleDisplay('chat_private',true)
			ScrollChatDown(true);
			ScrollPrivatDown(true);
		}
	}
	else if(forced==false)
	{
		ToggleDisplay('chat_private',false)
		e1.style.height='100%';
		e2.style.height='25%';
		e3.style.height = ChatHeight_percent2px(25,-14);
		b_mouseover_privat=false;
	}
	else
	{
		if(e1.style.height=='100%')
		{
			e1.style.height='75%';
			e2.style.height='25%';
			e3.style.height = ChatHeight_percent2px(25,-14);
			ToggleDisplay('chat_private',true)
			ScrollChatDown(true);
			ScrollPrivatDown(true);
		}
		else if(e1.style.height=='75%')
		{
			e1.style.height='50%';
			e2.style.height='50%';
			e3.style.height = ChatHeight_percent2px(50,-14);
			ToggleDisplay('chat_private',true)
			ScrollChatDown(true);
			ScrollPrivatDown(true);
		}
		else
		{
			ToggleDisplay('chat_private',false)
			e1.style.height='100%';
			e2.style.height='25%';
			e3.style.height = ChatHeight_percent2px(25,-14);
			b_mouseover_privat=false;
		}
	}
}

function changeChatSize()
{
	document.getElementById('chat_mainbox').style.height = ChatHeight_percent2px(100,0);
	document.getElementById('right_column').style.height = (getBrowserHeight()-93)+'px';
	if(vertical_mode)
		document.getElementById('chat_private_msgs').style.height = ChatHeight_percent2px(100,0);
	else
		document.getElementById('chat_private_msgs').style.height = ChatHeight_percent2px(25,-14);
	ScrollChatDown(true);
	ScrollPrivatDown(true);
}

function KeyboardListener(ek)
{
	var stop_propagation = false;
	var clear_box = false;

	var key = null;
	var kctrl = null;
	var kshift = null;
	var kalt = null;

	ek = ek || window.event;

	var e_msg = document.getElementById('chat_message');

//	if(1) // enabled only in input field, if'1' just for future config options, (Nugged hopes)... :)
//	{
//		var element;
//
//		if(ek.target)
//			element = ek.target;
//		else if(ek.srcElement)
//			element = ek.srcElement;
//
//		if(element.nodeType == 3)
//			element = element.parentNode;
//
//		if(element.tagName != 'INPUT' && element.tagName != 'TEXTAREA')
//			return;
//	}

	if(ek.keyCode)
		key = ek.keyCode;
	else if(ek.which)
		key = ek.which;

	kctrl = ek.ctrlKey;
	kshift = ek.shiftKey;
	kalt = ek.altKey;

	if(!kshift && kalt && kctrl)
	{
		if(key==66)
		{
			OverlapText('[b]','[/b]');
			stop_propagation = true;
		}
		else if(key==73)
		{
			OverlapText('[i]','[/i]');
			stop_propagation = true;
		}
		else if(key==85)
		{
			OverlapText('[u]','[/u]');
			stop_propagation = true;
		}
	}

//	if(key==10)
//	{
//		sendMessage();
//		prev_key_enter = false;
//		return false;
//	}
//	else

	if(bDebug && kshift && kctrl && kalt && key!=13)
	{
		if(bDebug && document.getElementById('chat_debug'))
			document.getElementById('chat_debug').innerHTML += ' k:' + key;
	}

	if(!kalt && !kshift && kctrl) // keys with control key only
	{
		switch(key)
		{
			case 0x25: // right
				break;
			case 0x27: // left
				break;
			case 0x26: // up
				var tmp = e_msg.value;
				e_msg.value = prev_message;
				prev_message = tmp;
				stop_propagation = true;
				e_msg.focus();
				break;
			case 0x28: // dAwn
				break;
			case 0x24: // WTF?
				break;
		}
	}

	if(kalt && !kshift && kctrl && key>=49 && key<=55)
	{
		var e = document.getElementById('chat_privname');
		if(key==49) // 1
		{
			PutInText('[b]' + last_chat_user_from + '[/b]: ');
			e.value = '';
		}
		else if(key==50) // 2
		{
			e.value = last_chat_user_from;
		}
		else if(key==51) // 3
		{
			PutInText('[b]' + last_priv_user_from + '[/b]: ');
			e.value = '';
		}
		else if(key==52) // 4
		{
			e.value = last_priv_user_from;
		}
		else if(key==53) // 5
		{
			if(e.value)
			{
				PutInText('[b]' + last_priv_user + '[/b]: ');
				e.value = '';
			}
			else
			   e.value = last_priv_user;
		}
		stop_propagation = true;
	}

	if(!kalt && key==13)
	{
		if( !kshift && !kctrl && getChecked('userconfig_send2enter'))
		{
			if(prev_key_enter)
			{
				sendMessage();
				prev_key_enter = false;
				stop_propagation = true;
				clear_box = true;
			}
			else
				prev_key_enter = true;
		}

		if( 	!kshift && !kctrl && getChecked('userconfig_sendenter') ||
				kshift && !kctrl && getChecked('userconfig_sendSenter') ||
				!kshift && kctrl && getChecked('userconfig_sendCenter'))
		{
			sendMessage();
			stop_propagation = true;
			clear_box = true;
		}
	}
	else
		prev_key_enter = false;

	if(stop_propagation)
	{
		ek.cancelBubble = true;
		ek.returnValue = false;
		if(ek.stopPropagation)
		{
			ek.stopPropagation();
			ek.preventDefault();
		}

		if(e_msg && clear_box)
			e_msg.value = '';
		return false;
	}
}

function ChatStart(vertical)
{
	vertical_mode = vertical;
	r_timeout = 'userconfig_refresh';
	max_messages = 'userconfig_qty';
	changeChatSize();
	attachActionTo('resize',changeChatSize);

	var chm_e = document.getElementById('chat_message');
	if(chm_e.addEventListener) chm_e.addEventListener('keydown', KeyboardListener, false);
	else if(chm_e.attachEvent) chm_e.attachEvent('onkeydown', KeyboardListener);
	else chm_e['onkeydown'] = KeyboardListener;

	LoadConfig();
//	try
//	{
//		document.getElementById('chat_message').onkeydown=KeyboardListener;
//		if(!document.all)
//		{
//			body.onKeyPress=KeyboardListener;
//			document.captureEvents(Event.KEYDOWN);
//		}
//	}
//	catch(e)
//	{
//		void(null);
//	}
	var mm = get_maxmsg();
	r_url = '/do/engine?mode=chat&room=' + current_room
		+ ( refresh_rooms ? '&getrooms=' + refresh_rooms : '&rts=' + room_timestamp )
		+ '&getusers=' + refresh_users
		+ '&pvtget=' + mm
		+ ( typoactivity ? '&typac=1' : '' )
		+ '&get=' + mm;
	request_url(r_url,request_callback,r_request,true);
	document.forms.chatForm.chat_message.focus();
}

function UserExitRequest()
{
	if(r_request && r_request.v)
		r_request.v.abort();
	if(req_timeout_id)
		clearTimeout(req_timeout_id);
	if(cancel_timeout_id)
		clearTimeout(cancel_timeout_id);
	r_url = '/do/engine?mode=chat&logout=1';
	r_request = new Object();
	request_url(r_url,request_callback,r_request,true);
}

function FullStop()
{
	r_timeout = 0;
	ch_stop = true;
	if(r_request && r_request.v)
		r_request.v.abort();
	r_request = new Object();
	if(req_timeout_id)
		clearTimeout(req_timeout_id);
	if(cancel_timeout_id)
		clearTimeout(cancel_timeout_id);

	document.getElementById('chat_send').disabled = true;
	document.getElementById('chat_send').value = 'stopped';
}



function get_maxmsg()
{
	if(typeof(max_messages) == 'number')
	{
		if(!max_messages>0)
			max_messages = 10;
		return max_messages;
	}
	else if(document.getElementById(max_messages))
	{
		var e = document.getElementById(max_messages);
		if(e.value)
		{
			if(!parseInt(e.value)>0)
				e.value = 10;
			return parseInt(e.value);
		}
		else if(e.innerHTML)
		{
			if(!parseInt(e.innerHTML)>0)
				e.innerHTML = 10;
			return parseInt(e.innerHTML);
		}
	}
	return null;
}

function get_timer()
{
	if(typeof(r_timeout) == 'number')
	{
		if(r_timeout > 0)
			return r_timeout;
	}
	else if(document.getElementById(r_timeout))
	{
		var e = document.getElementById(r_timeout);
		if(e.value)
		{
			if(!parseInt(e.value)>0)
				e.value = 8;
			return parseInt(e.value)*1000;
		}
		else if(e.innerHTML)
		{
			if(!parseInt(e.innerHTML)>0)
				e.innerHTML = 8;
			return parseInt(e.innerHTML)*1000;
		}
	}
	return null;
}

function request_callback()
{
	if(ch_kicked || ch_stop)
	{
		FullStop();
		return;
	}
//	if(typoactivity != null && typoactivity != document.getElementById('chat_message').value)
	if(document.getElementById('chat_message').value)
	{
		typoactivity = document.getElementById('chat_message').value;
	}
	else
	{
		typoactivity = false;
	}

	if(r_request.v)
	{
		var req = r_request.v;
		if(req && req.readyState == 4)
		{
			r_request = new Object();
			if(req.status == 200)
			{
				ProcessChatMsg(req.responseText);
				if(send_mode && !ch_stop)
				{
					document.getElementById('chat_status').innerHTML = '<b>sent</b>';
					ScrollChatDown(true);
				}
			}
			else
			{
				if(send_mode)
					document.getElementById('chat_status').innerHTML = '<b>send error:</b> ' + req.statusText;
			}

			if(send_mode && !ch_stop)
			{
				document.getElementById('chat_send').disabled = false;
				document.getElementById('chat_send').value = 'send';
			}
			send_mode = false;
		}
		else if(req && req.readyState == 1)
		{
			if(send_mode)
				document.getElementById('chat_status').innerHTML = '<b>opening</b>';
		}
		else if(req && req.readyState == 2)
		{
			if(send_mode)
				document.getElementById('chat_status').innerHTML = '<b>sending</b>';
		}
		else if(req && req.readyState == 3)
		{
			if(send_mode)
				document.getElementById('chat_status').innerHTML = '<b>confirming</b>';
		}
	}
}

function rotate_runner()
{
	txt_runner = '[' + txt_runner.substr(12,1) + txt_runner.substr(1,11) + ']';
}

function request_url(url,request_action,ro,do_timeout)
{
	if(bDebug && document.getElementById('chat_debug'))
		document.getElementById('chat_debug').innerHTML = '<a href="'+url+'">ajax call</a>';

	if(ro.v)
	{
		alert('Error: request object not cleared after last request.');
	}

	rotate_runner();

	document.getElementById('chat_status').innerHTML = txt_runner;

	if(req_timeout_id)
	{
		clearTimeout(req_timeout_id);
		req_timeout_id = null;
	}

	var req = null;
	if (window.XMLHttpRequest)
	{
		try { req = new XMLHttpRequest(); } catch (e){}
	} else if (window.ActiveXObject) {
		try { req = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e)
		{
			try { req = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e){}
		}
	}

	if(req)
	{
		req.onreadystatechange = request_action;
		req.open("GET", url, true);
		req.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
		req.send(null);
		ro.v = req;
		if(do_timeout)
		{
			clearTimeout(cancel_timeout_id);
			cancel_timeout_id = setTimeout("cancel_request(r_url,request_callback,r_request)", 30000);
		}
	}
}

function cancel_request(url,request_action,obj)
{
	var req = new Object();
	document.getElementById('chat_status').innerHTML = '';
	if(cancel_timeout_id)
	{
		clearTimeout(cancel_timeout_id);
		cancel_timeout_id = null;
	}
	else
	{
		document.getElementById('chat_status').innerHTML +=
			'<b style="color: red;">error/bug:</b> no cancel_timeout_id | ';
	}

	if(obj)
	{
		if(obj.v)
		{
			req = obj.v;
			req.abort();
			obj = new Object();
			r_request = new Object();
		}
		else
		{
			document.getElementById('chat_status').innerHTML += '<b style="color: red;">error/bug:</b> no obj.v | ';
			obj = new Object();
		}
	}
	else
	{
		document.getElementById('chat_status').innerHTML +=
			'<b style="color: red;">error/bug:</b> no obj.v & no obj | ';
	}

	if(!send_mode)
	{
		request_url(r_url,request_callback,r_request,true);
		document.getElementById('chat_status').innerHTML += '<b style="color: red;">request timed out:</b> retrying';
	}
	else
	{
		if(confirm('Отправить сообщение не удалось - недоступен сервер.\nПопробовать ещё раз?\n(но возможен и дубль)'))
		{
			request_url(r_url,request_callback,r_request,true);
			document.getElementById('chat_status').innerHTML = 'retrying';
		}
		else
		{
			if(!document.getElementById('chat_message').value)
			{
				document.getElementById('chat_message').value = prev_message;
			}
			r_url = '/do/engine?mode=chat&room=' + current_room
				+ ( refresh_rooms ? '&getrooms=' + refresh_rooms : '&rts=' + room_timestamp )
				+ '&getusers=' + refresh_users
				+ ( last_pvt_id ? '&pvtafter=' + last_pvt_id : '&pvtget=' + mm )
				+ ( typoactivity ? '&typac=1' : '')
				+ '&get=' + mm;
			request_url(r_url,request_callback,r_request,true);
			document.getElementById('chat_status').innerHTML += ' <b style="color: red;">canceled by user</b>';
			document.getElementById('chat_send').disabled = false;
			document.getElementById('chat_send').value = 'send';
			send_mode = false;
		}
	}
}

function closeWindow()
{
	if (is_firefox || is_gecko)
	{
		window.open('','_parent','');
		window.close();
	}
	else if(is_ie)
	{
		this.focus();
		self.opener = this;
		self.close();
	}
	else
		window.close();
}

function UpdateConfig()
{
	aConfig = new Array();
	aConfig['u_refresh'] = parseInt(document.getElementById('userconfig_refresh').value);
	aConfig['u_qty'] = parseInt(document.getElementById('userconfig_qty').value);
	aConfig['privateon'] = (document.getElementById('chat_privateon').checked ? 1 : 0);
	aConfig['preferpriv'] = (document.getElementById('chat_preferpriv').checked ? 1 : 0);
	aConfig['focusonm'] = (document.getElementById('focus_on_nm').checked ? 1 : 0);
	aConfig['sound'] = (document.getElementById('userconfig_sound').checked ? 1 : 0);
	aConfig['sendenter'] = (document.getElementById('userconfig_sendenter').checked ? 1 : 0);
	aConfig['send2enter'] = (document.getElementById('userconfig_send2enter').checked ? 1 : 0);
	aConfig['sendCenter'] = (document.getElementById('userconfig_sendCenter').checked ? 1 : 0);
	aConfig['sendSenter'] = (document.getElementById('userconfig_sendSenter').checked ? 1 : 0);
	aConfig['rccon'] =
		(document.getElementById('right_column_config_sub').style.display != '' ? 1 : 0);
	var coo = '';
	for (var i in aConfig)
	{
		coo += (coo ? ',' : '') + i + ":" + aConfig[i];
	}
	encodeCookie('config',coo,365);
}


function LoadConfig()
{
	var config = decodeCookie('config');
	if(config)
	{
		var ca=config.split(',');
		for(var i=0;i < ca.length;i++)
		{
			if(ca[i].indexOf(':'))
			{
				aConfig[ ca[i].substr(0,ca[i].indexOf(':')) ] = ca[i].substr(ca[i].indexOf(':')+1);
//				alert( [
//					'YES:',
///					ca[i].substr(ca[i].indexOf(':')+1),
//					ca[i].substr(0,ca[i].indexOf(':'))
//					]);
			}
			else
			{
				aConfig[ ca[i] ] = '';
//				alert(['NO:',ca[i],aConfig[ ca[i] ]]);
			}

		}
		document.getElementById('userconfig_refresh').value = parseInt(aConfig['u_refresh'] ? aConfig['u_refresh'] : 8);
		document.getElementById('userconfig_qty').value = parseInt(aConfig['u_qty'] ? aConfig['u_qty'] : 50);
		document.getElementById('chat_privateon').checked = (aConfig['privateon']=='1' ? true : false);
		document.getElementById('chat_preferpriv').checked = (aConfig['preferpriv']=='1' ? true : false);
		document.getElementById('focus_on_nm').checked = (aConfig['focusonm']=='1' ? true : false);
		document.getElementById('userconfig_sound').checked = (typeof(aConfig['sound'])=='undefined' ? true : aConfig['sound']=='1' ? true : false);
		document.getElementById('userconfig_sendenter').checked = (aConfig['sendenter']=='1' ? true : false);
		document.getElementById('userconfig_send2enter').checked = (aConfig['send2enter']=='1' ? true : false);
		document.getElementById('userconfig_sendCenter').checked = (aConfig['sendCenter']=='1' ? true : false);
		document.getElementById('userconfig_sendSenter').checked = (aConfig['sendSenter']=='1' ? true : false);
		ToggleDisplay('right_column_config_sub', aConfig['rccon']=='1' ? false : true );
	}
	if(document.getElementById('userconfig_refresh').value<5)
	{
		document.getElementById('userconfig_refresh').value = 8;
	}
}

function decodeCookie(name)
{
	var cn=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(cn)==0) return c.substring(cn.length,c.length);
	}
	return null;
}

function encodeCookie(n, v, days)
{
	var e = '';
	if(days)
	{
		var dt=new Date();
		dt.setTime(dt.getTime()+(days*24*60*60*1000));
		e='; expires='+dt.toGMTString();
	}
	document.cookie=n+"="+v+e+"; path=/";
}

function clearCookie(name)
{
	document.cookie = name + '=; path=/; expires=Mon, 01-Jan-2001 00:00:00 GMT';
}

function MakeSound(action)
{
	if(getChecked('userconfig_sound'))
	{
		if(!b_firstrun)
		{
			var ding = '';
			if(action == 'msgin')
			{
				ding = 'IncomingMessage';
			}
			else if(action == 'msgout')
			{
				ding = 'OutgoingMessage';
			}
			else if(action == 'usrin')
			{
				ding = 'UserOnline';
			}
			else if(action == 'usrout')
			{
				ding = 'UserOffline';
			}

			if(navigator.appName=='Microsoft Internet Explorer')
			{
				document.all.DingerIE.src = '/i/'+ding+'.wav';
				/* способ 2:
				var e = document.getElementById('Dinger');
				if(e)
				{
					document.all.sound.src = url;
					e.innerHTML = '<bgsound src="/i/'+ding+'.wav" />';
					alert([e.innerHTML,'<bgsound src="/i/'+ding+'.wav" />']);
				}
				способ 3 типа для всех, но фиг знаеть:
				<object
					classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95">
					<param name="FileName" value="liar.wav" />
					</object>

				вариации:
				<object width="115" height="45"
				classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
				codebase="http://www.apple.com/qtactivex/qtplugin.cab">
				<param name="src" value="file.mp3">
				<param name="autoplay" value="false">
				</object>

				вариации 2:
				<object data="file.mp3" type="audio/mpeg" width="115" height="45">
					<param name="autoplay" value="false">
				</object>
				*/
			}
			else if(navigator.appName=='Opera' && parseFloat(navigator.appVersion)>9.49)
			{
				var e = document.getElementById('Dinger');
				if(e) e.innerHTML = '<embed src="/i/'+ding+'.wav" autostart="true" loop="false" width="0" height="0" mastersound />';
			}
			else if(navigator.appName=='Netscape' || navigator.appName=='Opera')
			{
				if(document.getElementById(ding))
				{
					try
					{
						if(typeof document.getElementById(ding).Play == "function")
							document.getElementById(ding).Play();
						else if(typeof document.getElementById(ding).play == "function")
							document.getElementById(ding).play();
						else if(typeof document.getElementById(ding).Run == "function")
							document.getElementById(ding).Run();
						else if(typeof document.getElementById(ding).run == "function")
							document.getElementById(ding).run();
					}
					catch(e) {
				  	alert(['play',typeof document.getElementById(ding).play,
							 'Play',typeof document.getElementById(ding).Play,
							 'Run',typeof document.getElementById(ding).Run,
							 'run',typeof document.getElementById(ding).run,
							 'DoPlay',typeof document.getElementById(ding).DoPlay,
							 'embeds play',typeof document.embeds[ding].play,
							 'embeds Play',typeof document.embeds[ding].Play]);
					}
				}
			}
		}
		else if(navigator.appName=='Netscape' || navigator.appName=='Opera' && parseFloat(navigator.appVersion)<9.50)
		{
			var e = document.getElementById('Dinger');
			if(e) e.innerHTML = '<embed id="IncomingMessage" name="IncomingMessage" src="/i/IncomingMessage.wav" autostart="false" width="0" height="0" enablejavascript="true" type="audio/x-wav" /><embed id="OutgoingMessage" name="OutgoingMessage" src="/i/OutgoingMessage.wav" autostart="false" width="0" height="0" enablejavascript="true" type="audio/x-wav" /><embed id="UserOnline" name="UserOnline" src="/i/UserOnline.wav" autostart="false" width="0" height="0" enablejavascript="true" type="audio/x-wav" /><embed id="UserOffline" name="UserOffline" src="/i/UserOffline.wav" autostart="false" width="0" height="0" enablejavascript="true" type="audio/x-wav" />';
		}
	}

}

function NewUserReg_done()
{
	var tmp_name = '';
	if(document.getElementById('NewUserReg_name')
		&& document.getElementById('NewUserReg_name').value)
			tmp_name = document.getElementById('NewUserReg_name').value;

	if(tmp_name != '' && tmp_name.length > 2 )
	{
		encodeCookie(document.getElementById('NewUserReg_kun').value,tmp_name);
		document.getElementById('NewUserReg').style.display = 'none';
	}
}

