﻿/*
Chat script 1.1
By: Nguyen Duc Huy

Sync chat info: 3s/time
  - Chat box list state
  - Chat with
  - Chat text
Send chat text immediately

*/

//-------------Chat script ---------------
var _currentChatPad;
var _mouseonimotpad = false;
var _caretPos;
var _getChatRef = 0;
var _chatTimeKey = '';
var _alowSendChat = true;
var _alowchatsound = true;
var _chatScrollKey=true; //alow scroll chatbox
var _currentChatWithArr = new Array;
var _userOnlineTimer;
var _chatTimer;
var _httpRequestLock = false;
//var _postChat = true;

var _chatboxstate=false;
var _oldElement;
//


String.prototype.generateChatString = function(){  
    var str = this.replace(/&nbsp;/g,'&amp;nbsp;').replace(/&lt;/g,'&amp;lt;').replace(/&gt;/g,'&amp;gt;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\n/g,'<br>');
    str = str.replace(/\:\)&gt;-/gi,'<img src=\'images/emotions/67.gif\'>');  
    str = str.replace(/\^:\)\^/,'<img src=\'images/emotions/77.gif\'>');
    str = str.replace(/:\)\]/gi,'<img src=\'images/emotions/81.gif\'>');      
    str = str.replace(/\:\-\?\?/gi,'<img title=\'\r\n\' src=\'images/emotions/87.gif\'>');
    str = str.replace(/\@\}\;\-/gi,'<img title=\'\r\n\' src=\'images/emotions/53.gif\'>');
    str = str.replace(/\:\(\|\)/gi,'<img title=\'\r\n\' src=\'images/emotions/51.gif\'>');
    str = str.replace(/\~X\(/gi,'<img title=\'~ X (\r\nat wits\' end\' src=\'images/emotions/83.gif\'/>');
	str = str.replace(/3:-O/gi,'<img title=\'3 : - O\r\ncow\' src=\'images/emotions/50.gif\'/>');	
    str = str.replace(/&lt;\):\)/gi,'<img title=\'< ) : )\r\ncowboy\' src=\'images/emotions/48.gif\'>');
    str = str.replace(/&gt;:P/gi,'<img title=\'> : P\r\nphbbbbt\' src=\'images/emotions/47.gif\'>');
    str = str.replace(/\(:\|/gi,'<img title=\'( : |\r\nyawn\' src=\'images/emotions/37.gif\'>');
    str = str.replace(/&lt;:-P/gi,'<img title=\'< : - P\r\nparty\' src=\'images/emotions/36.gif\'>');
    str = str.replace(/\/:\)/gi,'<img title=\'/ : )\r\nraised eyebrows\' src=\'images/emotions/23.gif\'>');
    str = str.replace(/:\(\(/gi,'<img title=\'\r\n\' src=\'images/emotions/20.gif\'>');
    str = str.replace(/&gt;:\)/gi,'<img title=\'\r\n\' src=\'images/emotions/19.gif\'>');    
    str = str.replace(/&gt;:D&lt;/gi,'<img title=\'\r\n\' src=\'images/emotions/6.gif\'>');
    str = str.replace(/:\)\)/g,'<img title=\'\r\n\' src=\'images/emotions/21.gif\'>');
    str = str.replace(/\\:d\//gi,'<img src=\'images/emotions/69.gif\'>');     
    str = str.replace(/;;\)/g,'<img title=\'\r\n\' src=\'images/emotions/5.gif\'>');    
    str = str.replace(/:-P/gi,'<img title=\'\r\n\' src=\'images/emotions/10.gif\'>');
    str = str.replace(/:p/gi,'<img title=\'\r\n\' src=\'images/emotions/10.gif\'>');
    str = str.replace(/:\)/g,'<img title=\'\r\n\' src=\'images/emotions/1.gif\'>');
    str = str.replace(/:\(/g,'<img title=\'\r\n\' src=\'images/emotions/2.gif\'>');
    str = str.replace(/;\)\)/g,'<img title=\'\r\n\' src=\'images/emotions/71.gif\'>');
    str = str.replace(/;\)/g,'<img title=\'\r\n\' src=\'images/emotions/3.gif\'>');
    str = str.replace(/:D/gi,'<img title=\'\r\n\' src=\'images/emotions/4.gif\'>');
    str = str.replace(/:-\//gi,'<img src=\'images/emotions/7.gif\'>');
    str = str.replace(/:x/gi,'<img title=\'\r\n\' src=\'images/emotions/8.gif\'>');
    str = str.replace(/:\"&gt;/gi,'<img title=\'\r\n\' src=\'images/emotions/9.gif\'>');
    str = str.replace(/:-\*/gi,'<img title=\'\r\n\' src=\'images/emotions/11.gif\'>');
    str = str.replace(/\=\(\(/gi,'<img title=\'\r\n\' src=\'images/emotions/12.gif\'>');
    str = str.replace(/:-O/gi,'<img title=\'\r\n\' src=\'images/emotions/13.gif\'>');
    str = str.replace(/X\(/gi,'<img title=\'\r\n\' src=\'images/emotions/14.gif\'>');
    str = str.replace(/:&gt;/gi,'<img title=\'\r\n\' src=\'images/emotions/15.gif\'>');
    str = str.replace(/B-\)/gi,'<img title=\'\r\n\' src=\'images/emotions/16.gif\'>');
    str = str.replace(/:-SS/gi,'<img title=\'\r\n\' src=\'images/emotions/42.gif\'>');    
    str = str.replace(/#:-S/gi,'<img title=\'\r\n\' src=\'images/emotions/18.gif\'>');    
    str = str.replace(/:-S/gi,'<img title=\'\r\n\' src=\'images/emotions/17.gif\'>');    
    str = str.replace(/:\|/gi,'<img title=\'\r\n\' src=\'images/emotions/22.gif\'>');    
    str = str.replace(/\=\)\)/gi,'<img title=\'\r\n\' src=\'images/emotions/24.gif\'>');
    str = str.replace(/O:-\)/gi,'<img title=\'\r\n\' src=\'images/emotions/25.gif\'>');
    str = str.replace(/:-B/gi,'<img title=\'\r\n\' src=\'images/emotions/26.gif\'>');
    str = str.replace(/\=;/gi,'<img title=\'\r\n\' src=\'images/emotions/27.gif\'>');
    str = str.replace(/I-\)/gi,'<img title=\'\r\n\' src=\'images/emotions/28.gif\'>');
    str = str.replace(/8-\|/gi,'<img title=\'\r\n\' src=\'images/emotions/29.gif\'>');
    str = str.replace(/L-\)/gi,'<img title=\'\r\n\' src=\'images/emotions/30.gif\'>');
    str = str.replace(/:-\&/gi,'<img title=\'\r\n\' src=\'images/emotions/31.gif\'>');
    str = str.replace(/:-\$/gi,'<img title=\'\r\n\' src=\'images/emotions/32.gif\'>');
    str = str.replace(/\[-\(/gi,'<img title=\'\r\n\' src=\'images/emotions/33.gif\'>');
    str = str.replace(/:O\)/gi,'<img title=\'\r\n\' src=\'images/emotions/34.gif\'>');
    str = str.replace(/8-\}/gi,'<img title=\'\r\n\' src=\'images/emotions/35.gif\'>');    
    str = str.replace(/\=P\~/gi,'<img title=\'\r\n\' src=\'images/emotions/38.gif\'>');
    str = str.replace(/:-\?/gi,'<img title=\'\r\n\' src=\'images/emotions/39.gif\'>');
    str = str.replace(/\#-o/gi,'<img title=\'\r\n\' src=\'images/emotions/40.gif\'>');
    str = str.replace(/\=D&gt;/gi,'<img title=\'\r\n\' src=\'images/emotions/41.gif\'>');
    str = str.replace(/\@-\)/gi,'<img title=\'\r\n\' src=\'images/emotions/43.gif\'>');
    str = str.replace(/:\^o/gi,'<img title=\'\r\n\' src=\'images/emotions/44.gif\'>');
    str = str.replace(/:-w/gi,'<img title=\'\r\n\' src=\'images/emotions/45.gif\'>');
    str = str.replace(/:-&lt;/gi,'<img title=\'\r\n\' src=\'images/emotions/46.gif\'>');
    str = str.replace(/:\@\)/gi,'<img title=\'\r\n\' src=\'images/emotions/49.gif\'>');
    str = str.replace(/X_X/gi,'<img title=\'\r\n\' src=\'images/emotions/109.gif\'>');
    str = str.replace(/\%\-\(/gi,'<img title=\'\r\n\' src=\'images/emotions/88.gif\'>');
    str = str.replace(/\~\O\)/gi,'<img title=\'\r\n\' src=\'images/emotions/57.gif\'>');
    str = str.replace(/8-X/gi,'<img title=\'\r\n\' src=\'images/emotions/59.gif\'>');
    str = str.replace(/:-L/gi,'<img title=\'\r\n\' src=\'images/emotions/62.gif\'>');
    str = str.replace(/\[-O\!/gi,'<img title=\'\r\n\' src=\'images/emotions/63.gif\'>');
    str = str.replace(/\$-\)/gi,'<img title=\'\r\n\' src=\'images/emotions/64.gif\'>');
    str = str.replace(/:-\"/gi,'<img title=\'\r\n\' src=\'images/emotions/65.gif\'>');
    str = str.replace(/b-\(/gi,'<img title=\'\r\n\' src=\'images/emotions/66.gif\'>');
    str = str.replace(/\[-X/gi,'<img title=\'\r\n\' src=\'images/emotions/68.gif\'>');
    str = str.replace(/&gt;:\//gi,'<img title=\'\r\n\' src=\'images/emotions/70.gif\'>');
    str = str.replace(/:-@/gi,'<img title=\'\r\n\' src=\'images/emotions/76.gif\'>');
    str = str.replace(/:-j/gi,'<img title=\'\r\n\' src=\'images/emotions/78.gif\'>');
    str = str.replace(/:-c/gi,'<img title=\'\r\n\' src=\'images/emotions/82.gif\'>');
    str = str.replace(/:-h/gi,'<img title=\'\r\n\' src=\'images/emotions/84.gif\'>');
    str = str.replace(/:-t/gi,'<img title=\'\r\n\' src=\'images/emotions/85.gif\'>');
    str = str.replace(/8-&gt;/gi,'<img title=\'\r\n\' src=\'images/emotions/86.gif\'>');
    str = str.replace(/:\!\!/gi,'<img title=\'\r\n\' src=\'images/emotions/91.gif\'>');
    str = str.replace(/\\m\//gi,'<img title=\'\r\n\' src=\'images/emotions/92.gif\'>');
    str = str.replace(/:-q/gi,'<img title=\'\r\n\' src=\'images/emotions/93.gif\'>');
    str = str.replace(/:-bd/gi,'<img title=\'\r\n\' src=\'images/emotions/94.gif\'>');
    str = str.replace(/\^#\(\^/gi,'<img title=\'\r\n\' src=\'images/emotions/95.gif\'>');
    str = str.replace(/-splitpage-/gi,'\|\*\|\?\|');
    str = str.replace(/-splitmessinf-/gi,'\|messinf\|');
    str = str.replace(/-splitmesssplit-/gi,'\|messsplit\|');
    return str;
}

function initChat(){
    //Create chat box list---------------------
    var chatBoxList = document.createElement('div');chatBoxList.setAttribute('id','chatBoxList');  
    chatBoxList.style.zIndex = 1000;_winMainDiv.appendChild(chatBoxList);
    chatBoxList.style.display = 'none';
    str =  '<DIV><DIV class="class0">';
    str += '        <DIV class="listhead" onclick="if(this.parentNode.className==\'class0\'){this.parentNode.className=\'class1\'}else{this.parentNode.className=\'class0\'}">&nbsp;&nbsp;Friends</DIV>';
    str += '        <DIV class="listitems" id="chatUserList">Try to load user list...</DIV>';
    str += '</DIV></DIV>';
    str += '<DIV><DIV class="class0">';
    str += '        <DIV class="listhead" onclick="if(this.parentNode.className==\'class0\'){this.parentNode.className=\'class1\'}else{this.parentNode.className=\'class0\'}">&nbsp;&nbsp;References</DIV>';
    str += '        <DIV class="listitems" id="chatReferences">';
    str += '            ...';
    str += '        </DIV>';
    str += '</DIV></DIV>';
    str += '<DIV><DIV class="class0">';
    str += '        <DIV class="listhead" onclick="if(this.parentNode.className==\'class0\'){this.parentNode.className=\'class1\'}else{this.parentNode.className=\'class0\'}">&nbsp;&nbsp;Session information</DIV>';
    str += '        <DIV class="listitems" id="chatUserInfoList">Try to load information...</DIV>';
    str += '</DIV></DIV>'; 
    
    
    chatBoxList.innerHTML = str;
    //----Create chat box---------------------
    chatBox = document.createElement('div');
    chatBox.setAttribute('id','chatBox'); 
    chatBox.style.zIndex = 1000;
    chatBox.style.display = 'none';  
    _winMainDiv.appendChild(chatBox);
    str = ' <table cellpadding=0 cellspacing=0 height="56">';
    str += '    <tr valign="top" style="height:56px">';
    str += '        <td><DIV class="chatboxhead_img" id="friendImgDiv"><img alt="" id="friendAvatar" height="55" src="css/img/useroffline.bmp" /></DIV><input type="hidden" id="TxtChatWithNick" /></td>';
    str += '        <td><DIV class="chatboxhead_nick" id="chatwithnickDiv"></DIV><div class="chatboxhead_cmd">test</div></td>';
    str += '        <td><DIV class="chatboxhead_minimizebtt" onclick="hideChatBox()"></DIV></td>';
    str += '        <td><DIV class="chatboxhead_closebtt" onclick="turnOffChatBox()"></DIV></td></tr>';
    //str += '    <tr valign="top" style="height:56px"><td colspan="4"><div class="chatboxhead_cmd">test</div></td></tr>';
    str += '</Table>';
    str += '<DIV id="chatContentParent"><DIV></DIV>';
    str += '</DIV>';    
    str += '<table cellpadding=0 cellspacing=0><tr><td colspan=2><div id="bttEmoticon" class="normal" onmouseover="this.className=\'hover\'" onmouseout="this.className=\'normal\';_mouseonimotpad=false" onclick="showImoticonPad()"></td></tr><tr><td><DIV class="talkicon"></DIV></td><td>';
    str += '<textarea id="chatText" maxlength="250" onkeydown="if(event.keyCode==13){postChat(this.value)}"></textarea></td></tr></table>';
    chatBox.innerHTML = str;
    //----Create imoticons box---------------------
    emoticonBox = document.createElement('div');
    emoticonBox.setAttribute('id','emoticonsParentDiv'); 
    emoticonBox.style.zIndex = 1000;  
    emoticonBox.style.display = 'none';  
    _winMainDiv.appendChild(emoticonBox);
    str = '<DIV id="emoticonsContent" onmouseover="_mouseonimotpad=true" onmouseout="_mouseonimotpad=false">';
    str += '<Table><tr>';
    str += '<td><img alt=":&#41;" title=":&#41;\r\nhappy" class="imot" src="images/emotions/1.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':\)\')" /></td>';
    str += '<td><img alt="" title=":D\r\nbig grin" class="imot" src="images/emotions/4.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':D\')" /></td>';
    str += '<td><img alt="" title=":P\r\ntongue" class="imot" src="images/emotions/10.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':P\')" /></td>';
    str += '<td><img alt="" title=";;&#41;\r\nbatting eyelashes" class="imot" src="images/emotions/5.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\';;\)\')" /></td>';
    str += '<td><img alt="" title=":&#41;&#41;\r\nlaughing" class="imot" src="images/emotions/21.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':\)\)\')" /></td>';
    str += '<td><img alt="" title=":&gt;\r\nsmug" class="imot" src="images/emotions/15.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':&gt;\')" /></td>';
    str += '<td><img alt="" title="B-&#41;\r\ncool" class="imot" src="images/emotions/16.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'B-\)\')" /></td>';
    str += '<td><img alt="" title="&gt;:&#41;\r\ndevil" class="imot" src="images/emotions/19.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'\>:\)\')" /></td>';
    str += '<td><img alt="" title=";&#41;&#41;\r\nhee hee" class="imot" src="images/emotions/71.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\';\)\)\')" /></td>';
    str += '<td><img alt="" title=";&#41;\r\nwinking" class="imot" src="images/emotions/3.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\';\)\')" /></td>';
    str += '</tr><tr>';
    str += '<td><img alt="" title=":&#40;\r\nsad" class="imot" src="images/emotions/2.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':\(\')" /></td>';
    str += '<td><img alt="" title="X(\r\nangry" class="imot" src="images/emotions/14a.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'X(\')" /></td>';
    str += '<td><img alt="" title="&gt;:P\r\nphbbbbt" class="imot" src="images/emotions/47.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'\>:P\')" /></td>';
    str += '<td><img alt="" title=":-/\r\nconfused" class="imot" src="images/emotions/7.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':-\/\')" /></td>';
    str += '<td><img alt="" title="&#91;-&#40;\r\nno talking" class="imot" src="images/emotions/33.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'\[-\(\')" /></td>';
    str += '<td><img alt="" title="&#40;:|\r\nyawn" class="imot" src="images/emotions/37.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'\(:\|\')" /></td>';
    str += '<td><img alt="" title="=&#40;&#40;\r\nbroken heart" class="imot" src="images/emotions/12.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'=((\')" /></td>';
    str += '<td><img alt="" title=":-S\r\nworried" class="imot" src="images/emotions/17.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':-S\')" /></td>';
    str += '<td><img alt="" title=":-O\r\nsurprise" class="imot" src="images/emotions/13.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':-O\')" /></td>';
    str += '<td><img alt="" title=":\^o\r\nliar" class="imot" src="images/emotions/44.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':\^o\')" /></td>';
    str += '</tr><tr>';
    str += '<td><img alt="" title=":|\r\nstraight face" class="imot" src="images/emotions/22.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':|\')" /></td>';
    str += '<td><img alt="" title="=;\r\ntalk to the hand" class="imot" src="images/emotions/27.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'=;\')" /></td>';
    str += '<td><img alt="" title=":&#x22;&gt;\r\nblushing" class="imot" src="images/emotions/9.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':&quot;&gt;\')" /></td>';
    str += '<td><img alt="" title=":-*\r\nkiss" class="imot" src="images/emotions/11.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':-*\')" /></td>';
    str += '<td><img alt="" title=":-\&\r\nsick" class="imot" src="images/emotions/31.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':-\&\')" /></td>';
    str += '<td><img alt="" title=":-\$\r\ndon\'t tell anyone" class="imot" src="images/emotions/32.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':-\$\')" /></td>';
    str += '<td><img alt="" title="\=P\~\r\ndrooling" class="imot" src="images/emotions/38.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'\=P\~\')" /></td>';
    str += '<td><img alt="" title=":-\?\r\nthinking" class="imot" src="images/emotions/39.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':-\?\')" /></td>';
    str += '<td><img alt="" title="\@-&#41;\r\nhypnotized" class="imot" src="images/emotions/43.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'\@-\)\')" /></td>';
    str += '<td><img alt="" title="/:&#41;\r\nraised eyebrows" class="imot" src="images/emotions/23.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'/:\)\')" /></td>';
    str += '</tr><tr>';
    str += '<td><img alt="" title="\=D&gt;\r\napplause" class="imot" src="images/emotions/41.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'\=D\>\')" /></td>';
    str += '<td><img alt="" title=":x\r\nlove struck" class="imot" src="images/emotions/8.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':x\')" /></td>';
    str += '<td><img alt="" title=">:D<\nbig hug" class="imot" src="images/emotions/6a.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'\>:D\<\')" /></td>';
    str += '<td><img alt="" title="O:-&#41;\r\nangel" class="imot" src="images/emotions/25a.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'O:-\)\')" /></td>';
    str += '<td><img alt="" title="#:-S\r\nwhew!" class="imot" src="images/emotions/18a.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'#:-S\')" /></td>';
    str += '<td><img alt="" title="\#-O\r\nd\'oh" class="imot" src="images/emotions/40a.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'\#-o\')" /></td>';
    str += '<td><img alt="" title=":-SS\r\nnail biting" class="imot" src="images/emotions/42a.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':-SS\')" /></td>';
    str += '<td><img alt="" title="\\:D/\ndancing" class="imot" src="images/emotions/69a.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'\\\&#92;:d\/\')" /></td>';
    str += '<td><img alt="" title=":&#40;&#40;\r\ncrying" class="imot" src="images/emotions/20a.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\':\(\(\')" /></td>';
    str += '<td><img alt="" title="8-&#125;\r\nsilly" class="imot" src="images/emotions/35a.gif" onmouseover="this.className=\'imot_hover\'" onmouseout="this.className=\'imot\'" onclick="insertToChatText(\'8-\}\')" /></td>';
    str += '</tr></Table>';
    str += '</DIV>';
    emoticonBox.innerHTML = str;
    updateSessionInfo();
    getOffLineMessage();
    //setTimeout("startSyncChat()", 300);    
    
}
function getOffLineMessage(){
    var requestBody = 'act=getOffLineMessage';
    var wRequest = new Sys.Net.WebRequest();
    var myurl = 'ajax/chat1.aspx';
    wRequest.set_url(myurl);
    wRequest.set_httpVerb("POST");
    wRequest.add_completed(getOffLineMessageComplete);
    wRequest.set_body(requestBody);
    wRequest.get_headers()["Content-Length"] = requestBody.length; 
    wRequest.invoke();
}
function getOffLineMessageComplete(executor, eventArgs){
    if(executor.get_responseAvailable()){        
        var datareturn = executor.get_responseData();
        var messArr; var mess;
        if(datareturn.indexOf("|*|?|")>-1){            
            datareturn = datareturn.replace('|*|?|','');            
            messArr = datareturn.split('|messsplit|');
            var myVirWin = new virtualWin();
            myVirWin.title='Offline messages';
            myVirWin.height=230;myVirWin.width=400;myVirWin.top = 100;myVirWin.left = 300;
            var str = '<DIV class="offlinemessdiv">';
            for(i=0;i<messArr.length-1;i++){
                mess = messArr[i].split('|messinf|');
                str += '<DIV style="cursor:pointer" onclick="chatWithFriend(\''+mess[0]+'\',\''+mess[7]+'\',\''+mess[8]+'\')">';
                str += '<span class="friend">' + mess[7] + '</span>(' + mess[4] + '): ';
                str += mess[3].generateChatString() + '</DIV>';
            }
            str += '</DIV>'
            myVirWin.body=str;
            myVirWin.init();
        }
    }
}
function createChatPad(friendid,friendname,chatindex,avatar){
    //chatindex use to place where chatter on screen
    var chatPad;if(chatindex<0)chatindex=0;
    if(!$id('chatPad_'+friendid)){
        chatPad = document.createElement('div');chatPad.setAttribute('id','chatPad_'+friendid);chatPad.className = 'chatpad';            
        chatPad.style.right = 258 + chatindex*127 + 'px';_winMainDiv.appendChild(chatPad);                
    }else{chatPad = $id('chatPad_'+friendid)}
    chatPad.innerHTML = '<table cellpadding=0 cellspacing=0><tr><td><div class="nick" onclick="if(_alowSendChat)switchChatWithFriend(\''+friendid+'\',\''+friendname+'\',\''+avatar+'\')">' + friendname + '</div></td><td><div class="close" onmouseover="this.className=\'closehover\'" onmouseout="this.className=\'close\'" onclick="turnOffChatPad(\''+friendid+'\');"></div></td></tr></table>';
    return chatPad;
}
function createChatContent(friendid){
    var chatcontent = document.createElement('div');
    chatcontent.setAttribute('id','chatContent_'+friendid);
    chatcontent.className = 'chatContent';
    chatcontent.style.zIndex = 1000;
    _winMainDiv.appendChild(chatcontent);
    return chatcontent;
}
function scrollChatBox(friendid){    //Chua chuan
    if($id('TxtChatWithNick').value==friendid){
        var newscrollto; var currscrollto; var chatcontentheight; var deltascroll;
        //currscrollto = $id('chatContentParent').scrollTop;
        chatcontentheight = getHeight($id('chatContent_'+$id('TxtChatWithNick').value));
        deltascroll =  chatcontentheight - 203 + 70;
        $id('chatContentParent').scrollTop = deltascroll;
    }
}
function hideChatBox(){
    if($id('chatBox')){
        try{
            var friendid = $id('TxtChatWithNick').value;    
            $id('chatBox').style.display='none';//hidde chat box
            setChatBoxState(0);  
            $id('chatPad_'+friendid).className = 'chatpad';
        }catch(e){}; 
        try{//get value before change value
            _oldElement = $id('chatContent_'+friendid);
            _oldElement.style.display = 'none';
            _winMainDiv.appendChild(_oldElement);
        }catch(e){};      
    }
}
function switchChatWithFriend(friendid,friendname,avatar){ 
    var chatcontent;    
    if(_chatboxstate){ //off chat pad  
        try{_currentChatPad.className = 'chatpad'}catch(e){} 
        _currentChatPad = $id('chatPad_'+friendid);  
        //try to move chatContent_nick out of chat box
        try{
            _oldElement = $id('chatContent_'+$id('TxtChatWithNick').value);
            _oldElement.style.display = 'none';
            _winMainDiv.appendChild(_oldElement);
        }catch(e){};
        //append specific chatcontent to chat box
        if($id('chatContent_'+friendid)){                
            chatcontent = $id('chatContent_'+friendid);
            chatcontent.style.display = 'block';                
            $id('chatContentParent').appendChild(chatcontent);
        }else{                
            chatcontent = createChatContent(friendid);
            $id('chatContentParent').appendChild(chatcontent);
        }        
        //
        if($id('TxtChatWithNick').value==friendid){
            setChatBoxState(0);
            $id('chatBox').style.display = 'none';             
            _currentChatPad.className = 'chatpad';
        }else{                    
            $id('TxtChatWithNick').value = friendid;    //change value after get for oldElement
            $id('chatwithnickDiv').innerHTML = friendname;
            $id('chatwithnickDiv').onclick = function(){                
                document.location = 'SU04.aspx?id='+encode64(friendid);
            }            
            $id('chatText').value='';
            $id('chatText').focus();            
            _currentChatPad.className = 'chatpad_current';
            $id('chatBox').style.right= parseInt(getStyle(_currentChatPad,"right")) + 'px'; 
        }
        _chatboxstate=false;
    }else{  //on chat pad
        setChatBoxState(1);
        if($id('TxtChatWithNick').value!=friendid){
            try{
                _oldElement = $id('chatContent_'+$id('TxtChatWithNick').value);//get value before change value
                _oldElement.style.display = 'none';
                _winMainDiv.appendChild(_oldElement);
            }catch(e){}; 
            $id('TxtChatWithNick').value = friendid;
            $id('chatwithnickDiv').innerHTML = friendname;
            $id('chatwithnickDiv').onclick = function(){
                document.location = 'SU04.aspx?id='+encode64(friendid);                
            } 
        }
        if($id('chatContent_'+friendid)){            
            chatcontent = $id('chatContent_'+friendid);
            chatcontent.style.display = 'block';            
            $id('chatContentParent').appendChild(chatcontent);
        }else{
            chatcontent = createChatContent(friendid);  
            $id('chatContentParent').appendChild(chatcontent);          
        }
        try{_currentChatPad.className = 'chatpad';}catch(e){} 
        _currentChatPad = $id('chatPad_'+friendid);       
        $id('chatBox').style.right= parseInt(getStyle(_currentChatPad,"right")) + 'px';
        _currentChatPad.className = 'chatpad_current'; 
        $id('chatBox').style.display = 'block';        
        $id('chatText').focus();$id('chatText').select();
        _chatboxstate=true;
    }
    $id('friendAvatar').src = 'images/Avatar/'+avatar;
    //Set current friend have chat pad turn on    
    setCurrentChat(friendid);   
}
function turnOffChatPad(friendid){
    if($id('chatBox')){
        _currentChatWithArr.clean(friendid);
        try{_winMainDiv.appendChild($id('chatContent'+friendid))}catch(ex){};
        try{
            if($id('TxtChatWithNick').value==friendid){
                $id('chatBox').style.display='none'; 
                setChatBoxState(0);
                _chatboxstate = false;
            }
        }catch(e){}
        //setChatBoxState(0);
        removeCurrentChater(friendid);   //ajax
        _winMainDiv.removeChild($id('chatPad_'+friendid));
        try{_winMainDiv.removeChild($id('chatContent_'+friendid))}catch(e){};
        try{$id('chatBox').removeChild($id('chatContent_'+friendid))}catch(e){} 
        alignChatPad();               
    }
}
function alignChatPad(){
    try{
        for(i=0;i<_currentChatWithArr.length;i++){
            $id('chatPad_'+_currentChatWithArr[i]).style.right = 258 + i*127 + 'px';
        }        
    }catch(ex){}
}

function turnOffChatBox(){
    if($id('chatBox')){
        _chatboxstate = false;
        var friendid = $id('TxtChatWithNick').value;
        _currentChatWithArr.clean(friendid);
        try{_winMainDiv.appendChild($id('chatContent'+friendid))}catch(ex){};
        $id('chatBox').style.display='none';
        setChatBoxState(0);
        removeCurrentChater(friendid); 
        try{_winMainDiv.removeChild($id('chatPad_'+friendid))}catch(ex){};
        alignChatPad();
        try{document.removeChild($id('chatContent_'+friendid))}catch(ex){}
    }
}
function turOnChatBox(uid,uname){
    if($id('chatBox')){
        setChatBoxState(1);//switch chat box list state to show
        if($id('chatBox').style.display!='block'){$id('chatBox').style.display='block'};
        $id('chatwithnickDiv').innerHTML = uname;
        $id('TxtChatWithNick').value = uid;
        $id('chatwithnickDiv').onclick = function(){
            document.location = 'SU04.aspx?id='+encode64(uid);
        } 
        _chatboxstate = true;
    }
}


//Ajax function
function updateSessionInfo(){
    if(_alowSendChat){
        //_alowSendChat = false;
        xmlHttp2=GetXmlHttpObject();
        if (xmlHttp2==null){alert ("Your browser does not support AJAX!");return false;} 
        var url="actflag=updateSessionInfo";
        with(xmlHttp2){
            open("POST","ajax/getSessionInfo.aspx",true);
            onreadystatechange=updateSessionInfoComplete;
            setRequestHeader("Content-type", "application/x-www-form-urlencoded"); setRequestHeader("Content-length", url.length); setRequestHeader("Connection", "close"); send(url);
        }
    }    
}
function updateSessionInfoComplete(){
    if(xmlHttp2.readyState==4){    
        //_alowSendChat = true    
        var chatUserInfoList = $id('chatUserInfoList');
        var res = xmlHttp2.responseText;        
        if(res!=''){
            chatUserInfoList.innerHTML = res;
        }else{
            chatUserInfoList.innerHTML = 'Session infomation not update';
        }
    }
}

//Client function
function insertToChatText(txt){
    var str; var str1; var str2;str = $id('chatText').value;str1 = str.substring(0,_caretPos); str2 = str.substring(_caretPos,str.length); $id('chatText').value = str1 + txt + str2; _mouseonimotpad = false; hideImoticonPad(); _caretPos += txt.length; setCaretPosition($id('chatText'), _caretPos);
}
function showImoticonPad(){
    _caretPos=getCaretPosition($id('chatText'));
    var emoticonsParentDiv = $id('emoticonsParentDiv');
    if(getStyle(emoticonsParentDiv,"display")=="none"){emoticonsParentDiv.style.display = 'block'}
    else{emoticonsParentDiv.style.display = 'none'};
    _mouseonimotpad = _mouseonimotpad? false:true;
    emoticonsParentDiv.style.right = parseInt(getStyle($id('chatBox'),"right")) - 15 + 'px';
}
function hideImoticonPad(){
    if(!_mouseonimotpad)$id('emoticonsParentDiv').style.display = 'none';
}

//run when user click on user icon at the right bottom
function switchChatBoxList(){
    var chatboxlist = $id('chatBoxList');
    if(getStyle(chatboxlist,"display")=='none'){
        chatboxlist.style.display = 'block';
        setFriendListState(1);
        //try{updateFriendList()}catch(e){}
    }else{
        chatboxlist.style.display = 'none';
        setFriendListState(0);
    }
}
function setFriendListState(val){
    if(_alowSendChat){
        //_alowSendChat = false;
        xmlHttp2=GetXmlHttpObject();
        if (xmlHttp2==null){alert ("Your browser does not support AJAX!");return false;} 
        var url="act=updateFriendListState&val=" + val;
        with(xmlHttp2){
            open("POST","ajax/chat1.aspx",true);
            onreadystatechange=setFriendListStateComplete;
            setRequestHeader("Content-type", "application/x-www-form-urlencoded"); setRequestHeader("Content-length", url.length); setRequestHeader("Connection", "close"); send(url);
        }
    }    
}
function setFriendListStateComplete(){
    if(xmlHttp2.readyState==4){    
        //_alowSendChat = true    
        var res = xmlHttp2.responseText;        
        if(res=='Session Timeout'){
            //if (window.confirm('Your session have been terminated\nPlease re-login to continue')) showLoginForm();
        }
    }
}
function startSyncChat(){
    syncChat(); 
    setTimeout("startSyncChat()", 4000);
}
function syncChat(){    
    if(_alowSendChat){
        //_alowSendChat = false;
        xmlHttp2=GetXmlHttpObject();
        if (xmlHttp2==null){alert ("Your browser does not support AJAX!");return false;} 
        var url="act=syncchat&chatTimeKey="+_getChatRef;
        with(xmlHttp2){
            open("POST","ajax/chat1.aspx",true);
            onreadystatechange=syncChatComplete;
            setRequestHeader("Content-type", "application/x-www-form-urlencoded"); setRequestHeader("Content-length", url.length); setRequestHeader("Connection", "close"); send(url);
        }
    } 
}
function syncChatComplete(){
    if(xmlHttp2.readyState==4){ 
        //_alowSendChat = true;    
        var res = xmlHttp2.responseText;        
        if(res=='Session Timeout'){
            _alowSendChat = false;
            if (window.confirm('Your session have been terminated\nPlease re-login to continue')){
                showLoginForm();
            }
        }else{            
            var resArr = res.split('|Ø|');
            if(resArr.length == 7){
                //sync main chat win: resArr[0]
                var chatboxlist = $id('chatBoxList');
                chatboxlist.style.display = resArr[0];
                //1.*****sync friend list in main chat win: resArr[1]
                if(resArr[1]!=''){
                    var listStr;var temArr = resArr[1].split("|u|");
                    $id('chatUserList').innerHTML = '';
                    for(i=0;i<temArr.length-1;i++){
                        var userArr = temArr[i].split('|');           //userArr[0]: userid;userArr[1]: User name; userArr[2]: avatar; userArr[3]: state
                        $id('chatUserList').innerHTML +=  '<DIV onclick="chatWithFriend(\''+userArr[0]+'\',\''+userArr[1]+'\',\''+userArr[2]+'\')" class="friend" onmouseover="this.className=\'friend_hover\'" onmouseout="this.className=\'friend\'"><table cellpadding="0" cellspacing="0"><tr><td><img height="20" src="images/Avatar/'+userArr[2]+'"/></td><td><DIV class="name' + userArr[3] + '">' + userArr[1] + '</DIV></td></tr></table></DIV>';
                    }
                }
                //2.****sync current chat with friends, who have chat pad on screen: resArr[2]
                var numfriend = 0;
                var temChater = new Array;
                var currentfriendid;
                if(resArr[2]!=''){
                    var temArr = resArr[2].split("|u|");   
                    numfriend = temArr.length-1;
                    //create chat pad
                    for(i=0;i<temArr.length-1;i++){
                        var userArr = temArr[i].split('|');
                        var fid = userArr[0];
                        var fname = userArr[1];
                        var avatar = userArr[2];                    
                        var isCurrentChat = userArr[3];             
                        temChater.push(fid);
                        if(!_currentChatWithArr.content(fid)){_currentChatWithArr.push(fid)};
                        if(!$id('chatPad_'+fid)){
                            var chatPad = createChatPad(fid,fname,i, avatar);
                        }
                        var chatcontent;
                        
                        //sync chat box state to on 
                        if(isCurrentChat=='1'){   //chabox have to show on this friend 
                            currentfriendid = fid;
                            if(_chatboxstate){  //chatbox is showing
                                //Make sure chat box have to show on this friend                          
                                if($id('TxtChatWithNick').value!=fid){ //current chat box not show for this friend
                                    try{    //get value before change value
                                        _oldElement = $id('chatContent_'+$id('TxtChatWithNick').value);
                                        _oldElement.style.display = 'none';
                                        _winMainDiv.appendChild(_oldElement);
                                    }catch(e){}; 
                                    $id('TxtChatWithNick').value = fid;
                                    $id('chatwithnickDiv').innerHTML = fname;   
                                    $id('chatwithnickDiv').onclick = function(){
                                        document.location = 'SU04.aspx?id='+encode64(fid);
                                    }                               
                                }                                 
                            }else{  //chatbox is hidding, show it and active chatpad  
                                try{
                                    _oldElement = $id('chatContent_'+$id('TxtChatWithNick').value);//get value before change value
                                    _oldElement.style.display = 'none';
                                    _winMainDiv.appendChild(_oldElement);
                                }catch(e){}; 
                                $id('chatBox').style.display='block';
                                $id('chatwithnickDiv').innerHTML = fname;
                                $id('TxtChatWithNick').value = fid;    
                                $id('chatwithnickDiv').onclick = function(){
                                    document.location = 'SU04.aspx?id='+encode64(fid);
                                }                   
                                _chatboxstate = true;
                                //test
                                if($id('chatContent_'+fid)){            
                                    chatcontent = $id('chatContent_'+fid);
                                    chatcontent.style.display = 'block';            
                                    $id('chatContentParent').appendChild(chatcontent);
                                }else{
                                    chatcontent = createChatContent(fid);  
                                    $id('chatContentParent').appendChild(chatcontent);          
                                }                                                       
                                $id('friendAvatar').src = 'images/Avatar/'+avatar;
                            }                            
                            _currentChatPad = $id('chatPad_'+fid); 
                            _currentChatPad.className = 'chatpad_current';
                            $id('chatBox').style.right= parseInt(getStyle(_currentChatPad,"right")) + 'px';
                        }                    
                    }                
                }
                //remove chat pad
                for(i=0;i<_currentChatWithArr.length;i++){
                    var friendid = _currentChatWithArr[i];
                    if(!temChater.content(friendid)){                    
                        _currentChatWithArr.clean(friendid);
                        try{if($id('TxtChatWithNick').value==friendid){$id('chatBox').style.display='none'; _chatboxstate=false;}}catch(e){}//hidde chat box
                        try{_winMainDiv.removeChild($id('chatPad_'+friendid));}catch(ex){}
                        try{_winMainDiv.removeChild($id('chatContent_'+friendid))}catch(ex){}
                        alignChatPad();
                    }
                }
                //3.****sync chat box state to off/hide: resArr[3]
                if(resArr[3]=='0'){
                    try{
                        if(_chatboxstate)_currentChatPad.className = 'chatpad'
                    }catch(e){}
                    
                    try{
                        _oldElement = $id('chatContent_'+$id('TxtChatWithNick').value);
                        _oldElement.style.display = 'none';
                        _winMainDiv.appendChild(_oldElement);
                    }catch(e){};
                    $id('chatBox').style.display = 'none';
                    _chatboxstate=false;
                }
                //4.****Sync chat text receive: resArr[4], chatRef = resArr[5]
                _getChatRef = resArr[5];
                var friendArr;
                var messfromid;
                var messfromname;
                var messtoid;
                var friendstate;
                var textchat;
                var chatcontent_id;
                var classfrom;
                var nickfrom;
                var messisfromfriend;
                var isRead;
                var chatPad;
                var numchater;
                var padstr;
                var messtime;
                var avatar;
                if(resArr[4]!=''){//if have incoming mess
                    var messArr = resArr[4].split("|messsplit|");
                    for(i=0;i<messArr.length-1;i++){
                        friendArr = messArr[i].split("|messinf|"); 
                        messfromid = friendArr[0];
                        messfromname = friendArr[1];
                        friendstate = parseInt(friendArr[2]);
                        messtoid = friendArr[3];
                        textchat = friendArr[4];
                        messtime = friendArr[5];
                        messisfromfriend = parseInt(friendArr[6]);
                        isRead = parseInt(friendArr[7]);
                        avatar = friendArr[8];
                        if(messisfromfriend){                        
                            if($id('chatContent_'+messfromid)){ //if chatContent_id is exist
                                chatcontent_id = $id('chatContent_'+messfromid);
                            }else{
                                chatcontent_id = createChatContent(messfromid);
                            }
                            if(!isRead){
                                var temchatboxstate = _chatboxstate;
                                if(!$id('chatPad_'+messfromid)){                                
                                    var chatPad = createChatPad(messfromid,messfromname,numfriend, avatar);
                                    chatWithFriend(messfromid,messfromname,avatar);                                
                                }
                                if(!temchatboxstate){
                                    hideChatBox();
                                    $id('chatPad_'+messfromid).className = 'chatpad_attention';
                                }
                                _chatboxstate = temchatboxstate;
                                if($id('chatPad_'+messfromid).className =='chatpad'){$id('chatPad_'+messfromid).className = 'chatpad_attention'};
                                //if(_alowchatsound){playSound('sound/snd05.wav')};
                                if(!_documentfocus){newMessComingAlerts(messfromid+':'+textchat)};
                            }
                            classfrom = 'friend';
                            nickfrom = messfromid;
                            chatcontent_id.innerHTML += '<div><span class="'+ classfrom +'">'+nickfrom+': </span>' + textchat.generateChatString() + '</div>';
                            if(_chatboxstate){
                                scrollChatBox(messfromid);
                            }else{
                                chatcontent_id.style.display='none';
                            }
                        }else{
                            classfrom = 'me';nickfrom = 'Me';
                            if($id('chatContent_'+messtoid)){
                                chatcontent_id = $id('chatContent_'+messtoid);                        
                            }else{
                                chatcontent_id = createChatContent(messtoid);
                            }
                            chatcontent_id.innerHTML += '<div><span class="'+ classfrom +'">'+nickfrom+': </span>' + textchat.generateChatString() + '</div>';
                            if(_chatboxstate){
                                scrollChatBox(messtoid);
                            }
                        }
                    }
                }
                //5.****Sync number of friend request: resArr[6]
                if(resArr[6]=='0'){
                   try{$id('friendrequestpad').style.display = 'none'}catch(e){}                   
                }else{
                    var friendrequestpad;
                    if($id('friendrequestpad')){
                        friendrequestpad = $id('friendrequestpad');
                    }else{
                        friendrequestpad = document.createElement('span');
                        friendrequestpad.setAttribute('id','friendrequestpad');
                        document.body.appendChild(friendrequestpad);
                        if(friendrequestpad.addEventListener){   //For FF
                            friendrequestpad.addEventListener('click',function(e){checkFriendRequest();},true);                
                        }else{
                            friendrequestpad.attachEvent("onclick", function(e){checkFriendRequest();});
                        }                         
                    }
                    friendrequestpad.style.display = 'block';
                    friendrequestpad.innerHTML = resArr[6]; 
                }
                //Con tiep
            }           
        }
    }
}

function chatWithFriend(fid,fname,avatar){
    if(_alowSendChat){
        //_alowSendChat = false;
        if(!_currentChatWithArr.content(fid)){_currentChatWithArr.push(fid)};
        xmlHttp2=GetXmlHttpObject();
        if (xmlHttp2==null){alert ("Your browser does not support AJAX!");return false;} 
        var url="act=chatwithfriend&fid="+fid+"&fname="+fname+"&avatar="+avatar;
        with(xmlHttp2){
            open("POST","ajax/chat1.aspx",true);
            onreadystatechange=chatWithFriendComplete;
            setRequestHeader("Content-type", "application/x-www-form-urlencoded"); setRequestHeader("Content-length", url.length); setRequestHeader("Connection", "close"); send(url);
        } 
    }   
}
function chatWithFriendComplete(){
    if(xmlHttp2.readyState==4){    
        //_alowSendChat = true;    
        var res = xmlHttp2.responseText;        
        if(res!='Session Timeout'){
            var resArr = res.split('|');
            var isnew = parseInt(resArr[0]);
            var fid = resArr[1];
            var fname = resArr[2];
            var avatar = resArr[3];
            var numchater = resArr[4];
            _chatboxstate = true; 
            if(!$id('chatPad_'+fid)){ 
                var chatPad = createChatPad(fid,fname,numchater, avatar); 
            }
            var chatcontent;
            if($id('TxtChatWithNick').value!=fid){
                try{
                    _oldElement = $id('chatContent_'+$id('TxtChatWithNick').value);//get value before change value
                    _oldElement.style.display = 'none';
                    _winMainDiv.appendChild(_oldElement);
                }catch(e){}; 
                $id('TxtChatWithNick').value = fid;
                $id('chatwithnickDiv').innerHTML = fname;
                $id('chatwithnickDiv').onclick = function(){
                    document.location = 'SU04.aspx?id='+encode64(fid);
                }
            }
            if($id('chatContent_'+fid)){            
                chatcontent = $id('chatContent_'+fid);
                chatcontent.style.display = 'block';            
                $id('chatContentParent').appendChild(chatcontent);
            }else{
                chatcontent = createChatContent(fid);  
                $id('chatContentParent').appendChild(chatcontent);          
            }
            try{_currentChatPad.className = 'chatpad';}catch(e){} 
            _currentChatPad = $id('chatPad_'+fid);       
            $id('chatBox').style.right= parseInt(getStyle(_currentChatPad,"right")) + 'px';
            _currentChatPad.className = 'chatpad_current'; 
            $id('chatBox').style.display = 'block';
            $id('friendAvatar').src = 'images/Avatar/'+avatar;
            $id('chatText').focus();$id('chatText').select();
        }
    }
}
function setChatBoxState(val){
    //_alowSendChat = false;
    xmlHttp2=GetXmlHttpObject();
    if (xmlHttp2==null){alert ("Your browser does not support AJAX!");return false;} 
    var url="act=setchatboxstate&val="+val;
    with(xmlHttp2){
        open("POST","ajax/chat1.aspx",true);
        onreadystatechange=setChatBoxStateComplete;
        setRequestHeader("Content-type", "application/x-www-form-urlencoded"); setRequestHeader("Content-length", url.length); setRequestHeader("Connection", "close"); send(url);
    }
}
function setChatBoxStateComplete(){
    if(xmlHttp2.readyState==4){    
        _alowSendChat = true;
    }
}
function setCurrentChat(fid){    
        //_alowSendChat = false;
        xmlHttp2=GetXmlHttpObject();
        if (xmlHttp2==null){alert ("Your browser does not support AJAX!");return false;} 
        var url="act=setcurrentchat&fid="+fid;
        with(xmlHttp2){
            open("POST","ajax/chat1.aspx",true);
            onreadystatechange=setCurrentChatComplete;
            setRequestHeader("Content-type", "application/x-www-form-urlencoded"); setRequestHeader("Content-length", url.length); setRequestHeader("Connection", "close"); send(url);
        }    
}
function setCurrentChatComplete(){
    if(xmlHttp2.readyState==4){    
        _alowSendChat = true;
    }
}
function removeCurrentChater(friendid){
    //_alowSendChat = false;
    xmlHttp2=GetXmlHttpObject();
    if (xmlHttp2==null){alert ("Your browser does not support AJAX!");return false;} 
    var url="act=removeCurrentChater&fid="+friendid+"&chatref="+_getChatRef;
    with(xmlHttp2){
        open("POST","ajax/chat1.aspx",true);
        onreadystatechange=removeCurrentChaterComplete;
        setRequestHeader("Content-type", "application/x-www-form-urlencoded"); setRequestHeader("Content-length", url.length); setRequestHeader("Connection", "close"); send(url);
    }
}
function removeCurrentChaterComplete(){
    if(xmlHttp2.readyState==4){    
        _alowSendChat = true;
    }
}

function postChat(txt){    
    if(txt.alltrim()!=''){
        //_alowSendChat = false;        
        //ajax post chat
        var requestBody = 'act=postchat&txt='+txt.generatePostData();
        requestBody += '&fid='+$id('TxtChatWithNick').value;    
        var myurl = 'ajax/chat1.aspx';        
        var wRequest = new Sys.Net.WebRequest();
        wRequest.set_url(myurl);
        wRequest.set_httpVerb("POST");
        wRequest.add_completed(postChatComplete);
        wRequest.set_body(requestBody);
        wRequest.get_headers()["Content-Length"] = requestBody.length;wRequest.invoke();
    }else if(txt.alltrim()!=''){
        alert('Can not send many message in second\nRefresh page to avoid error');        
    }
}
function postChatComplete(executor, eventArgs){
    if(executor.get_responseAvailable()){
        _alowSendChat = true;
        if(executor.get_responseData()=='posted'){ 
            $id('chatText').value = '';
            syncChat();
        }else{
            //alert(executor.get_responseData())
        }
    }    
}
function addAsFriend(fid){
    xmlHttp2=GetXmlHttpObject();
    if (xmlHttp2==null){alert ("Your browser does not support AJAX!");return false;} 
    var url="act=addasfriend&fid="+fid;
    with(xmlHttp2){
        open("POST","ajax/chat1.aspx",true);
        onreadystatechange=addAsFriendComplete;
        setRequestHeader("Content-type", "application/x-www-form-urlencoded"); setRequestHeader("Content-length", url.length); setRequestHeader("Connection", "close"); send(url);
    } 
}
function addAsFriendComplete(){
    if(xmlHttp2.readyState==4){  
        var res = xmlHttp2.responseText;
        if(res=='addfriendcomplete'){$id('bttAddAsFriend').style.display='none';alert('Add friend request have beed sent');}
        else{alert(res)}
     } 
}
var _mouseonrequestDiv = false;
function checkFriendRequest(){
    var requestDiv;
    if($id('requestDiv')){
        requestDiv = $id('requestDiv');
    }else{
        requestDiv = document.createElement('div');
        requestDiv.setAttribute('id','requestDiv');
        document.body.appendChild(requestDiv);
        if(requestDiv.addEventListener){   //For FF
            requestDiv.addEventListener('mouseover',function(e){_mouseonrequestDiv=true},true); 
            requestDiv.addEventListener('mouseout',function(e){_mouseonrequestDiv=false},true);                
        }else{  //IE
            requestDiv.attachEvent("onmouseover", function(e){_mouseonrequestDiv=true});
            requestDiv.attachEvent("onmouseout", function(e){_mouseonrequestDiv=false});
        }
    }
    requestDiv.style.display = 'block';
    xmlHttp2=GetXmlHttpObject();
    if (xmlHttp2==null){alert ("Your browser does not support AJAX!");return false;} 
    var url="act=checkFriendRequest";
    with(xmlHttp2){
        open("POST","ajax/chat1.aspx",true);
        onreadystatechange=checkFriendRequestComplete;
        setRequestHeader("Content-type", "application/x-www-form-urlencoded"); setRequestHeader("Content-length", url.length); setRequestHeader("Connection", "close"); send(url);
    }
}
function checkFriendRequestComplete(){
    if(xmlHttp2.readyState==4){    
        var res = xmlHttp2.responseText; 
        if(res!=''){
            $id('requestDiv').innerHTML = res;
            $id('requestDiv').style.height= Math.min(300, getHeight($id('requestDivBody')) + 5 ) + 'px';
        }else{
            $id('requestDiv').innerHTML = 'No request';
        }
    }
}
function confirmAddFriend(fid){
    xmlHttp2=GetXmlHttpObject();
    if (xmlHttp2==null){alert ("Your browser does not support AJAX!");return false;} 
    var url="act=confirmAddFriend&fid="+fid;
    with(xmlHttp2){
        open("POST","ajax/chat1.aspx",true);
        onreadystatechange=confirmAddFriendComplete;
        setRequestHeader("Content-type", "application/x-www-form-urlencoded"); setRequestHeader("Content-length", url.length); setRequestHeader("Connection", "close"); send(url);
    } 
}
function confirmAddFriendComplete(){
    if(xmlHttp2.readyState==4){  
        var res = xmlHttp2.responseText.split('|');        
        if(res[0]=='confirmcomplete'){
            $id('friendrequestDiv_'+res[1]).style.display='none';
            //alert( res[1] + 'have been add to your friend list');
            $id('requestDiv').style.height= Math.min(300, getHeight($id('requestDivBody')) + 5 ) + 'px';}
            if(getHeight($id('requestDivBody'))<30){
                $id('requestDiv').style.display = 'none';                
            }
        //else{alert(res[0])}
     } 
}
function denyAddFriend(fid){

}
