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 += '
| '+usr_node[0].firstChild.nodeValue + ' |