ICT

Welcome guest
Login : Register : Team list : Latest posts : Help : Login to chat

» Home » Discussion Forums » Reading Topic
» ICT V1.1 » Chat system » Small bug in the JS code of the chat << Older
Only one page to show.
Camil May 21st, 2010 21:03 GMT Print this post
Offline


Topics: 154
Replies: 3406
Development


Post: #12044
PT: #1/1


Replace the existing JS code for the chat system with this:
Code
<script language='javascript'>
var sendReq = getXmlHttpRequestObject();
var receiveReq = getXmlHttpRequestObject();
var base_addr = '$this->base_addr';
   base_addr = base_addr.replace(/\&amp\;/ig, '&');
var lastMessage = 0;
var mTimer;
var nTimer;
var status_mess;
var alert_mess;
var status_orig;
var current_mess = 1;
var can_private;
var can_mod;
var skin_img;
var total_mem = new Array();
var ignore = new Array();
//Function for initializating the page.
function startChat(stat_mess,alt_mess,can_priv, skin, mod) {
   status_mess = stat_mess;
   alert_mess = alt_mess;
   can_private = can_priv;
   can_mod = mod;
   skin_img = skin;
   //Set the focus to the Message Box.
   document.getElementById('message').focus();
   status_orig = document.getElementById('p_status').innerHTML;
   document.getElementById('p_status').innerHTML = status_mess;
   //Start Recieving Messages.
   getChatText();
}
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
   if (window.XMLHttpRequest ) {
      return new XMLHttpRequest();
   } else if(window.ActiveXObject ) {
      return new ActiveXObject("Microsoft.XMLHTTP");
   } else {
      status_mess = status_orig;
      document.getElementById('p_status').innerHTML = status_mess;
   }
}
//Gets the current messages from the server
function getChatText() {
   if (lastMessage > -1 && (receiveReq.readyState == 4 || receiveReq.readyState == 0)) {
      receiveReq.open("GET", base_addr + 'a=cht&header=0&last=' + lastMessage, true);
      receiveReq.onreadystatechange = handleReceiveChat;
      receiveReq.send(null);
   }
   clearInterval(mTimer);
   mTimer = setTimeout('getChatText();',3000); //Refresh our chat in 3 seconds
}
//Add a message to the chat server.
function sendChatText() {
   if(document.getElementById('message').value == '') {
      alert(alert_mess);
      return;
   }
   var timing = new Date();
   if( nTimer > timing.getTime()-3100 ){ //will wait 3 seconds before it can send another message
      document.getElementById('message').focus();
      return;
   }
   document.getElementById('message').focus();
   if (sendReq.readyState == 4 || sendReq.readyState == 0) {
      nTimer = timing.getTime();
      sendReq.open("POST", base_addr + 'a=cht&last=' + lastMessage, true);
      sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      sendReq.onreadystatechange = handleSendChat;
      var param = 'message=' + document.getElementById('message').value;
      sendReq.send(param);
      document.getElementById('input_text').innerHTML = '<input type="text" id="message" name="message" id="message" style="width: 50em;" class="forminput" onClick="storeCaret(this);" onKeyup="storeCaret(this); javascript:expendinput();" onChange="storeCaret(this);" onFocus="storeCaret(this);" onSelect="storeCaret(this);" ONDBLCLICK="storeCaret(this);"/>';
      if(document.getElementById('expend_box').checked){
         expendinput();
      }
   }
   CurOffset;
   CurLine;
   TagStack = new Array();
}
//When our message has been sent, update our page.
function handleSendChat() {
   //Clear out the existing timer so we don't have
   //multiple timer instances running.
   clearInterval(mTimer);
   getChatText();
}
//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 member_nodes = xmldoc.getElementsByTagName("member");
      var me = xmldoc.getElementsByTagName("me");
      var message_nodes = xmldoc.getElementsByTagName("message");
      var n_messages = message_nodes.length;
      var add_lines = '';
      var lnti = ignore.length;
      for (i = 0; i < n_messages; i++) {
         var user_node = message_nodes[i].getElementsByTagName("user");
         var user_id_node = message_nodes[i].getElementsByTagName("user_id");
         var text_node = message_nodes[i].getElementsByTagName("text");
         var time_node = message_nodes[i].getElementsByTagName("time");
         var color_node = message_nodes[i].getElementsByTagName("color");
         var ign = 0;
         for(j=0;j<lnti;j++){
            if(ignore[j] == user_id_node[0].firstChild.nodeValue){
               ign = 1;
            }
         }
         if(ign == 0){
            add_lines += '<table width="98%"><tr><td align="left" colspan="3" id="chat_messages">' + text_node[0].firstChild.nodeValue + '</td></tr><tr><td><font color="' + color_node[0].firstChild.nodeValue + '">' + user_node[0].firstChild.nodeValue + '</font></td><td><font class="chat_time">' + time_node[0].firstChild.nodeValue + '</font></td><td align="center" width="10%"><input  type="image" onclick="javascript:deletemess(' + current_mess + ')" src="' + skin_img + 'postv/delete.gif" border="0" ></td></tr></table><hr /><!-- mess -->';
         }
         lastMessage = (message_nodes[i].getAttribute('id'));
         current_mess++;
      }
      if( add_lines != '' ){
         chat_div.innerHTML += add_lines;
         chat_div.scrollTop += (chat_div.offsetHeight * i);
         document.getElementById('message').focus();
      }
      var n_member = member_nodes.length;
      if(n_member>0){
         total_mem = new Array();
      }
      for (i = 0; i < n_member; i++) {
         var name_node = member_nodes[i].getElementsByTagName("name");
         var me_node = member_nodes[i].getElementsByTagName("me");
         var member_id_node = member_nodes[i].getElementsByTagName("member_id");
         var color_node = member_nodes[i].getElementsByTagName("color");
         var mem_arr = new Array(member_id_node[0].firstChild.nodeValue, name_node[0].firstChild.nodeValue, color_node[0].firstChild.nodeValue, 0);
         total_mem[i] = mem_arr;
         if(me_node.length){
            document.getElementById('moi').innerHTML = '<font color="' + color_node[0].firstChild.nodeValue + '">' + name_node[0].firstChild.nodeValue + '</font>';
         }
      }
      if(n_member>0){
         render_mem();
      }
   }
}
//Function that handles the member in the chat room.
function render_mem(){
   var n_member = total_mem.length;
   document.getElementById('n_member').innerHTML = n_member;
   var lines_m = '';
   var lines_mi = '';
   var ling = ignore.length;
   var mod;
   for (i = 0; i < n_member; i++) {
      var ing = 0;
      for(j=0; j < ling; j++){
         if(ignore[j] == total_mem[i][0]){
            ing = 1;
         }
      }
      if(ing == 0){
         if( lines_m != '' ){
            lines_m += ', ';
         }
         if( can_private == 1 ){
            lines_m += '<a href="javascript:privateMessage(' + total_mem[i][0] + ', \'' + total_mem[i][1] + '\' );"><font color=' + total_mem[i][2] + ' >' + total_mem[i][1] + '</font></a>';
         }else{
            lines_m += '<font color=' + total_mem[i][2] + ' >' + total_mem[i][1] + '</font>';
         }
      }else{
         if( lines_mi != '' ){
            lines_mi += ', ';
         }
         if( can_private == 1 ){
            lines_mi += '<a href="javascript:remIgnore(' + total_mem[i][0] + ' );"><font color=' + total_mem[i][2] + ' >' + total_mem[i][1] + '</font></a>';
         }else{
            lines_mi += '<font color=' + total_mem[i][2] + ' >' + total_mem[i][1] + '</font>';
         }
      }
      if(can_mod == 1){
         mod += '<option value="' + total_mem[i][0] + '">' + total_mem[i][1] + '</option>';
      }
   }
   if(can_private == 0){
      document.getElementById('ignore').disabled = true
   }
   if(can_mod == 1){
      document.getElementById('members_mod').innerHTML = '<select name="member">' + mod + '</select>';
   }
   document.getElementById('members').innerHTML = lines_m;
   document.getElementById('imembers').innerHTML = lines_mi;
   return;
}
//Delete a specific message in the chat_div
function deletemess( num ){
   var splitmess = document.getElementById('div_chat').innerHTML.split( '<!-- mess -->' );
   var ln = splitmess.length;
   var tot = "";
   for( i=0; i < ln; i++ ){
      if( i != (num-1) ){
         tot += splitmess[i] + '<!-- mess -->';
      }else{
         tot += '<!-- mess -->';
      }
   }
   document.getElementById('div_chat').innerHTML = tot;
   document.getElementById('message').focus();
}
//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() {
   sendChatText();
   return false;
}
//This sends a private message to someone in the chat room.
//It adds the [private=n]@name: in front of the message.
function privateMessage( id, name ){
   if(id != '' ){
      if(document.getElementById('ignore').checked){
         document.getElementById('ignore').checked = false;
         var lnt = ignore.length;
         ignore[lnt] = id;
         render_mem();
         document.getElementById('message').focus();
      }else{
         var re = new RegExp("^\\\\[private=");
         if( document.getElementById('message').value == '' || !document.getElementById('message').value.match( re ) ){
            document.getElementById('message').value = '[private=' + id + ']@' + name + ': ' + document.getElementById('message').value;
            document.getElementById('message').focus();
         }else{
            document.getElementById('message').focus();
         }
      }
   }
}
//Removes an member from the Ignore list
function remIgnore(id){
   var lnt = ignore.length;
   var temp_arr = new Array();
   var j = 0;
   for(i=0; i < lnt; i++){
      if( ignore[i] != id ){
         temp_arr[j] = ignore[i];
         j++;
      }
   }
   ignore = temp_arr;
   render_mem();
   document.getElementById('message').focus();
}
//This function is used to add more input element for more space..
function expendinput(){
   var le = document.getElementById('message').value;
   l1 = le.length;
   if(l1 > 80 || document.getElementById('expend_box').checked ){
      var text = document.getElementById('message').value;
      document.getElementById('input_text').innerHTML = '<textarea cols="75" rows="10" id="message" name="message" class="forminput" onClick="storeCaret(this);" onKeyup="storeCaret(this);" onChange="storeCaret(this);" onFocus="storeCaret(this);" onSelect="storeCaret(this);" ONDBLCLICK="storeCaret(this);">' + document.getElementById('message').value + '</textarea>';
      document.getElementById('message').focus();
      document.getElementById('message').focus();
      return true;
   }
}
</script>

The bug is with the Google Chrome browser. It did not work with it... now it does.
PM: Camil
Only one page to show. Posts in this thread ( 1 )

[ Subscriptions disabled ] [ Print Topic :: Print page]

» Home » Discussion Forums » Reading Topic


8 users online [ 7 guests, 1 member, 0 Anonymous Members ] in the last 15 minutes
jimlongo
[ Member ] [Online list]
© ICT - Infinite Core Technologies 2002-2006 All Rights Reserved
Driven by ICT - Infinite Core Technologies
Privacy Policy