var GetChaturl = "http://www.inter-caffe.com/ajax/getChatData.php";
var SendChaturl = "http://www.inter-caffe.com/ajax/sendChatData.php";
var lastID = -1;
window.onload = initJavaScript;
function initJavaScript() {
document.forms['chatForm'].elements['chatbarText'].setAttribute('autocomplete','off');
checkStatus('');
checkName();
receiveChatText();
}
function receiveChatText() {
if (httpReceiveChat.readyState == 4 || httpReceiveChat.readyState == 0) {
httpReceiveChat.open("GET",GetChaturl+'?lastID='+lastID+'&rand='+Math.floor(Math.random() * 1000000), true);
httpReceiveChat.onreadystatechange = handlehHttpReceiveChat;
httpReceiveChat.send(null);
}
}
function handlehHttpReceiveChat() {
if (httpReceiveChat.readyState == 4) {
results = httpReceiveChat.responseText.split('---');
if (results.length > 2) {
for(i=0;i < (results.length-1);i=i+4) {
insertNewContent(results[i+1],results[i+2],results[i+3]);
}
lastID = results[results.length-5];
soundManager.play('poruka');
}
setTimeout('receiveChatText();',4000);
}
}
function insertNewContent(liTime,liName,liText) {
insertO = document.getElementById("outputList");
oLi = document.createElement('li');
if (liName.slice(-2) == '.*') { oLi.setAttribute('className','anon');oLi.setAttribute('class','anon'); }
oSpan = document.createElement('span');
oSpanv = document.createElement('span');
if ( liText.slice(0,3) == '88-' && liText.length == 14 ) {
liText = liText.slice(-11);
oSpanYTL = document.createElement('a');
oSpanYTIMG = document.createElement('img');
oSpanYTL.setAttribute('href','http://www.youtube.com/watch?v=' + liText);
oSpanYTL.setAttribute('title',liName);
oSpanYTL.setAttribute('class','chatyt');
oSpanYTL.setAttribute('target','_blank');
oSpanYTIMG.setAttribute('src','http://i4.ytimg.com/vi/' + liText  + '/default.jpg');
YTB = 1;
} else {
YTB = 0;
}
if ( liText.slice(0,33) == 'http://www.inter-caffe.com/lista-' || liText.slice(0,19) == 'http://go.inter.rs/' ) {
if ( liText.slice(0,33) == 'http://www.inter-caffe.com/lista-' ) {
liText = liText.match(/http:\/\/www\.inter-caffe\.com\/lista-(\d+(-\d*)?\.html(#p\d*)?)/);
} else {
liText = liText.match(/http:\/\/go.inter.rs\/(.*)/);
}
oSpanICF = document.createElement('a');
oSpanICF.setAttribute('href',liText[0]);
oSpanICF.setAttribute('title',liName);
oSpanICF.setAttribute('class','chatic');
oSpanICF.setAttribute('target','_blank');
liText = '[' + liText[1] + ']';
ICF = 1;
} else {
ICF = 0;
}
oSpan.setAttribute('className','name');
oSpan.setAttribute('class','name');
oSpanv.setAttribute('className','vreme');
oSpanv.setAttribute('class','vreme');
oVreme = document.createTextNode(liTime);
oName = document.createTextNode(liName);
if ( YTB == 1 ) {
TextYTL = document.createTextNode(liText);
TextYTIMG = document.createTextNode(liText);
}else {

oText = document.createElement('span');
oText.setAttribute('class','poruka');
oText.appendChild(document.createTextNode(liText));
// oText = document.createTextNode(liText);

}
if ( ICF == 1 ) {
TextICFL = document.createTextNode(liText);
TextICFT = document.createTextNode(liText);
}else {

oText = document.createElement('span');
oText.setAttribute('class','poruka');
oText.appendChild(document.createTextNode(liText));
// oText = document.createTextNode(liText);

}
oSpanv.appendChild(oVreme);
oLi.appendChild(oSpanv);
oSpan.appendChild(oName);
oLi.appendChild(oSpan);
if ( YTB == 1 ) {
oSpanYTL.appendChild(oSpanYTIMG);
oLi.appendChild(oSpanYTL);
}else if ( ICF == 1 ) {
oSpanICF.appendChild(oText);
oLi.appendChild(oSpanICF);
}else {
oLi.appendChild(oText);
}
insertO.insertBefore(oLi, insertO.firstChild);
}
function sendComment() {
currentChatText = document.forms['chatForm'].elements['chatbarText'].value;
if (currentChatText != '' & (httpSendChat.readyState == 4 || httpSendChat.readyState == 0)) {
currentName = document.forms['chatForm'].elements['name'].value;
param = 'n='+ currentName+'&c='+ currentChatText;
httpSendChat.open("POST", SendChaturl, true);
httpSendChat.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
httpSendChat.onreadystatechange = handlehHttpSendChat;
httpSendChat.send(param);
document.forms['chatForm'].elements['chatbarText'].value = '';
} else if (currentChatText != '') { setTimeout('sendComment();',1000); }
}
function handlehHttpSendChat() {
if (httpSendChat.readyState == 4) { receiveChatText(); }
}
function checkStatus(focusState) {
currentChatText = document.forms['chatForm'].elements['chatbarText'];
oSubmit = document.forms['chatForm'].elements['submit'];
if (currentChatText.value != '' || focusState == 'active') {
oSubmit.disabled = false;
} else {
oSubmit.disabled = true;
}
}
function checkName() {
currentName = document.forms['chatForm'].elements['name'];
}
function getHTTPObject() {
var xmlhttp;
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var httpReceiveChat = getHTTPObject();
var httpSendChat = getHTTPObject();
