function rtrim(income) {
var ptrn = /((\s*\S+)*)\s*/;
return income.replace(ptrn, "$1");
}
function ltrim(income) {
var ptrn = /\s*((\S+\s*)*)/;
return income.replace(ptrn, "$1");
}
function trim(income) {
return ltrim(rtrim(income));
}
function chatreply(who){
var text = document.getElementById('text');
text.value = who+', '+text.value;
text.focus();
}
function chatsend() {
area = document.getElementById("messages");
if(trim(document.getElementById('name').value).length > 2 && trim(document.getElementById('name').value).length < 65) {
if(trim(document.getElementById('text').value).length > 0 && trim(document.getElementById('text').value).length < 513) {
JsHttpRequest.query(
'http://chat.304.ru/chat.php',
{
'do': 'add',
'topic': 1,
'author': document.getElementById('name').value,
'msg': document.getElementById('text').value
},
function(result, errors) {
if(errors) {
area.innerHTML = '
'+errors+'
'+area.innerHTML;
}
if (result.success) {
area.innerHTML = 'Сообщение отправлено. Оно будет показано после проверки модератором
'+area.innerHTML; document.getElementById('text').value = '';
}
area.scrollTop = 0;
},
true
);
}else{
area.innerHTML = 'Поле "Сообщение" обязательно к заполнению, а так же не может превышать 512 символов
'+area.innerHTML;
}
}else{
area.innerHTML = 'Имя должно быть от 3 до 64 символов
'+area.innerHTML;
}
}
lastpost = -1;
lastposttype = 1;
lastact = -1;
function chatupdate() {
area = document.getElementById("messages");
JsHttpRequest.query(
'http://chat.304.ru/chat.php',
{
'do': 'get',
'topic': 1,
'lastpost': lastpost,
'lastact': lastact },
function(result, errors) {
if(errors) {
area.innerHTML = ''+errors+'
'+area.innerHTML;
}
if (result) {
if(result.messages)
{
for(var i = 0; i < result.messages.length; i++)
{
var msg = document.getElementById('msg'+result.messages[i].id);
if(!msg) {
var buffer = '';
if(lastposttype == 1) {
buffer = '';
lastposttype=2;
}else{
buffer = '
';
lastposttype=1;
}
lastpost = result.messages[i].id;
area.scrollTop = 0;
buffer = buffer+'
'+result.messages[i].msg+'
';
area.innerHTML = buffer+area.innerHTML;
}
}
}
if(result.actions)
{
var msg;
for(var n = 0; n < result.actions.length; n++)
{
msg = document.getElementById('msg'+result.actions[n].msgid);
if(result.actions[n].action == 'del') {
if(msg) {
document.getElementById('msg'+result.actions[n].msgid).innerHTML = '';
document.getElementById('msg'+result.actions[n].msgid).style.padding = '1px';
area.scrollTop = 0;
}
}
lastact = result.actions[n].id;
}
}
}
},
true
);
}
setInterval(chatupdate, 1000);