var sendReq = getXmlHttpRequestObject(); var receiveReq = getXmlHttpRequestObject(); var uzytkownik = getXmlHttpRequestObject(); var lastMessage = 0; var lastUser = 0; var mTimer; var uTimer; var ch=0; //Function for initializating the page. function startChat(chat) { //Set the focus to the Message Box. ch=chat; document.getElementById('txt_message').focus(); getChatUser(ch); getChatText(ch); } //Gets the browser specific XmlHttpRequest Object function getXmlHttpRequestObject() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if(window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); } else { document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object. Consider upgrading your browser.'; } } //Gets the current messages from the server function getChatText(chat) { ch=chat; if (receiveReq.readyState == 4 || receiveReq.readyState == 0) { receiveReq.open("GET", './dzialy/chat/getChat.php?chat='+ch+'&last=' + lastMessage, true); receiveReq.onreadystatechange = handleReceiveChat; receiveReq.send(null); } } function getChatUser(ch){ if (uzytkownik.readyState == 4 || uzytkownik.readyState == 0) { uzytkownik.open("POST", './dzialy/chat/getUser.php?chat='+ch+'&last=' + lastMessage + '&action=sprawdz_user' + '&user_nazwa=' + document.getElementById('user_nazwa').value + '&id_dzial=' + ch, true); uzytkownik.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); uzytkownik.onreadystatechange = handleReceiveUser; uzytkownik.send(null); } } function sendChatText() { if(document.getElementById('txt_message').value == '') { alert("You have not entered a message"); return; } if (sendReq.readyState == 4 || sendReq.readyState == 0) { sendReq.open("POST", './dzialy/chat/getChat.php?chat='+ch+'&last=' + lastMessage, true); sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); sendReq.onreadystatechange = handleSendChat; var param = 'message=' + document.getElementById('txt_message').value; param += '&name=' + document.getElementById('user_nazwa').value; param += '&chat='+ch; sendReq.send(param); document.getElementById('txt_message').value = ''; } } function handleSendChat() { //Clear out the existing timer so we don't have //multiple timer instances running. clearInterval(mTimer); getChatText(ch); getChatUser(ch); } //Function for handling the return of chat text function handleReceiveChat() { if (receiveReq.readyState == 4) { var chat_div = document.getElementById('div_chat'); var xmldoc = receiveReq.responseXML; var message_nodes = xmldoc.getElementsByTagName("message"); var n_messages = message_nodes.length for (i = 0; i < n_messages; i++) { var user_node = message_nodes[i].getElementsByTagName("user"); var text_node = message_nodes[i].getElementsByTagName("text"); var time_node = message_nodes[i].getElementsByTagName("time"); var kolor1_node=message_nodes[i].getElementsByTagName("colo"); var kolor1=kolor1_node[0].firstChild.nodeValue; chat_div.innerHTML += '
'+user_node[0].firstChild.nodeValue + ' 
'; chat_div.innerHTML += '
' + time_node[0].firstChild.nodeValue + '
' chat_div.innerHTML += '
' +text_node[0].firstChild.nodeValue + '
'; chat_div.scrollTop = chat_div.scrollHeight; lastMessage = (message_nodes[i].getAttribute('id')); } mTimer = setTimeout('getChatText(ch);',2000); //Refresh our chat in 2 seconds } } function handleReceiveUser(){ //TA - wyswietlanie listy urzytkowników if (uzytkownik.readyState == 4) { var xmldoc = uzytkownik.responseXML; var user_div = document.getElementById('div_user'); var user_nodes = xmldoc.getElementsByTagName("usr"); var n_user = user_nodes.length; user_div.innerHTML=""; for (i = 0; i < n_user; i++) { var usr_node = user_nodes[i].getElementsByTagName("nazwa"); var kolor2_node=user_nodes[i].getElementsByTagName("colo"); var kolor2=kolor2_node[0].firstChild.nodeValue; user_div.innerHTML += '
'+usr_node[0].firstChild.nodeValue + '
'; user_div.scrollTop = user_div.scrollHeight; //lastUser = (user_nodes[i].getAttribute('id')); } uTimer = setTimeout('getChatUser(ch);',50000); } } //This functions handles when the user presses enter. Instead of submitting the form, we //send a new message to the server and return false. function blockSubmit(chat) { ch=chat; sendChatText(chat); return false; } //This cleans out the database so we can start a new chat session. function resetChat() { if (sendReq.readyState == 4 || sendReq.readyState == 0) { sendReq.open("POST", './dzialy/chat/getChat.php?chat='+ch+'&last=' + lastMessage, true); sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); sendReq.onreadystatechange = handleResetChat; var param = 'action=reset'; sendReq.send(param); document.getElementById('txt_message').value = ''; } } //This function handles the response after the page has been refreshed. function handleResetChat() { document.getElementById('div_chat').innerHTML = ''; getChatText(ch); getChatUser(ch); }