lastmess='';
function replaceText(text,textarea)
{
try{
//Attempt to create a text range (IE).
if(typeof(textarea.caretPos)!="undefined"&&textarea.createTextRange){
var caretPos=textarea.caretPos;
caretPos.text=caretPos.text.charAt(caretPos.text.length-1)==' '?text+' ':text;
caretPos.select();}
//Mozilla text range replace.
else if(typeof(textarea.selectionStart)!="undefined"){
var begin=textarea.value.substr(0,textarea.selectionStart);
var end=textarea.value.substr(textarea.selectionEnd);
var scrollPos=textarea.scrollTop;
textarea.value=begin+text+end;
if(textarea.setSelectionRange){
textarea.focus();
textarea.setSelectionRange(begin.length+text.length,begin.length+text.length);}
textarea.scrollTop=scrollPos;}
//Just put it on the end.
else{
textarea.value+=text;
textarea.focus(textarea.value.length-1);}
}catch(e){alert('Вероятно вы не авторизовались, поэтому Вы не имеете права отправлять сообщения.');}
}
function InsertSmiles(str){
ImagePath="<img src=\"http://www.secundant.com/_graph/images/smiles/";
str=str.replace(/0\:\-\)/g,ImagePath+"aa.gif\" alt=\"0:-)\" title=\"0:-)\" />");
str=str.replace(/8\)/g,ImagePath+"ai.gif\" alt=\"8)\" title=\"8)\" />");
str=str.replace(/\:\)/g,ImagePath+"ab.gif\" alt=\":)\" title=\":)\" />");
str=str.replace(/\:\(/g,ImagePath+"ac.gif\" alt=\":(\" title=\":(\" />");
str=str.replace(/\;\)/g,ImagePath+"ad.gif\" alt=\";)\" title=\";)\" />");
str=str.replace(/\:P/g,ImagePath+"ae.gif\" alt=\":P\" title=\":P\" />");
str=str.replace(/\:D/g,ImagePath+"ag.gif\" alt=\":D\" title=\":D\" />");
str=str.replace(/\:\-\[/g,ImagePath+"ah.gif\" alt=\":-[\" title=\":-[\" />");
str=str.replace(/\:\`\(/g,ImagePath+"ak.gif\" alt=\":`(\" title=\":`(\" />");
str=str.replace(/\:\-X/g,ImagePath+"al.gif\" alt=\":-X\" title=\":-X\" />");
str=str.replace(/\:\|/g,ImagePath+"an.gif\" alt=\":|\" title=\":|\" />");
str=str.replace(/\:\-\//g,ImagePath+"ao.gif\" alt=\":-/\" title=\":-/\" />");
str=str.replace(/]\:\&gt\;/g,ImagePath+"aq.gif\" alt=\"]:>\" title=\"]:>\" />");
str=str.replace(/\*KISSED\*/g,ImagePath+"as.gif\" alt=\"*KISSED*\" title=\"*KISSED*\" />");
str=str.replace(/\*KISSING\*/g,ImagePath+"aw.gif\" alt=\"*KISSING*\" title=\"*KISSING*\" />");
str=str.replace(/\?\?\?/g,ImagePath+"huh.gif\" alt=\"???\" title=\"???\" />");
str=str.replace(/\@\}\-\&gt\;\-\-/g,ImagePath+"ax.gif\" alt=\"@}->--\" title=\"@}->--\" />");
str=str.replace(/\*THUMBS UP\*/g,ImagePath+"ay.gif\" alt=\"*THUMBS UP*\" title=\"*THUMBS UP*\" />");
str=str.replace(/\*DRINK\*/g,ImagePath+"az.gif\" alt=\"*DRINK*\" title=\"*DRINK*\" />");
str=str.replace(/\%\)/g,ImagePath+"be.gif\" alt=\"%)\" title=\"%)\" />");
str=str.replace(/\*HELP\*/g,ImagePath+"bc.gif\" alt=\"*HELP*\" title=\"*HELP*\" />");
str=str.replace(/\*SOS\*/g,ImagePath+"bc.gif\" alt=\"*HELP*\" title=\"*HELP*\" />");
str=str.replace(/\*ROFL\*/g,ImagePath+"bj.gif\" alt=\"*ROFL*\" title=\"*ROFL*\" />");
str=str.replace(/\*YAHOO\*/g,ImagePath+"bp.gif\" alt=\"*YAHOO*\" title=\"*YAHOO*\" />");
str=str.replace(/\*BYE\*/g,ImagePath+"bs.gif\" alt=\"*BYE*\" title=\"*BYE*\" />");
str=str.replace(/\*HI\!\*/g,ImagePath+"bt.gif\" alt=\"*HI!*\" title=\"*HI!*\" />");
str=str.replace(/\*KILL ALL\*/g,ImagePath+"fb.gif\" alt=\"*KILL ALL*\" title=\"*KILL ALL*\" />");
str=str.replace(/\*SUICIDE1\*/g,ImagePath+"fo.gif\" alt=\"*SUICIDE1*\" title=\"*SUICIDE1*\" />");
str=str.replace(/\*SUICIDE2\*/g,ImagePath+"ga.gif\" alt=\"*SUICIDE2*\" title=\"*SUICIDE2*\" />");
str=str.replace(/\*NOSPAM\*/g,ImagePath+"gj.gif\" alt=\"*NOSPAM*\" title=\"*NOSPAM*\" />");
str=str.replace(/\*LAW\*/g,ImagePath+"hm.gif\" alt=\"*LAW*\" title=\"*LAW*\" />");
str=str.replace(/\*WALL\*/g,ImagePath+"bu.gif\" alt=\"*WALL*\" title=\"*WALL*\" />");
//censor
str=str.replace(/бзд|хуй|хуе|хуё|пизд|ебан|ёбан|херн|херо|бля|ебат|уебо|уёбо|пидор|сука|сучар|сучка|сучек|сучёк|сучок|педри|пизд|fuck/gi,"###");
return str;
}

function doLoad(value1,value2){
try{
if(document.getElementById('mess').value==value1){
vvod=document.getElementById('mess').value;
document.getElementById('mess').value='';}
knopka=document.getElementById('btn_chat');}catch(e){}
try{
knopka.disable=true;
knopka.value='Отправка...';}catch(e){}
pole=document.getElementById('result');
chatol=document.getElementById('chato');
try{
JsHttpRequest.query('http://www.secundant.com/chatengine.htm',{'mymess':value1,'time':value2},
function(result,errors){
if(result["error"]==''){
message=result["message"].replace(lastmess,'');
lastmess=message;
message=InsertSmiles(message);
document.getElementById('time').value=result["time"];
if(result["usersonline"]!=''){chatol.innerHTML='Сейчас смотрят чат: '+result["usersonline"];}
else{chatol.innerHTML='<i>Сейчас никто не следит за чатом</i>';}
if(result["message"]!=''){pole.innerHTML=pole.innerHTML+message;}
if(document.getElementById('mess').value==value1){document.getElementById('mess').value='';}
pole.scrollTop=90000;
try{
document.getElementById('notice').innerHTML=result["notice"];
if((ChatUserName.length>0)&&(document.getElementById('neednotice').checked))
{flag=false;
for(loop=0;loop<message.length-ChatUserName.length;loop++)
{if(ChatUserName==message.substring(loop,loop+ChatUserName.length)){flag=true;break;}}
if(flag){playSound();flag=false;}
}}catch(e){}
}
else{
if(document.getElementById('mess').value==value1){document.getElementById('mess').value=vvod;}alert(result["error"]);}
},false);
knopka.disabled=false;
knopka.value='Отправить';
}catch(e){}
}

window.onload=function(){
document.getElementsByTagName('form')[0].onkeypress=function(e){13==(window.event || e).keyCode && doLoad(document.getElementById('mess').value,document.getElementById('time').value)}
}

function updatePage(){
doLoad('',document.getElementById('time').value);
document.getElementById('chatform')[0].onkeypress=function(e){13==(window.event||e).keyCode&&doLoad('',document.getElementById('time').value)}
setTimeout(updatePage,10000);
}
