﻿var isdown = 0;
function DownMe(ctrl,e) 
{
    isdown = 1;
}
function UpMe(ctrl,e)
{
    isdown = 0;
}
function MoveMe(ctrl,ev) 
{
    if(isdown == 1)
    {
        ctrl.style.left = tempX-40+'px';
        ctrl.style.top = tempY-20+'px';
    }
}



var IE = document.all?true:false;

if (!IE) document.captureEvents(Event.MOUSEMOVE);

document.onmousemove = getMouseXY;

var tempX = 0;
var tempY = 0;


function getMouseXY(e) 
{
  if (IE) 
  {
    tempX = event.clientX + document.body.scrollLeft;
    tempY = event.clientY + document.body.scrollTop;
  } 
  else 
  { 
    tempX = e.pageX;
    tempY = e.pageY;
  } 
  if (tempX < 0)
  {
    tempX = 0;
  }
  if (tempY < 0)
  {
    tempY = 0;
  }
  return true;
}




function ShowSmilee(id,val)
{
   $get('sml'+id).style.display='block';
}
function SendSmilee(key,id)
{
    var txt = $get('chtText'+id).value;
    txt = txt + key;
    $get('chtText'+id).value = txt;
    $get('sml'+id).style.display = 'none';
}
function GetChatData()
{
    var chatcount=$get('activechatcount').value;
    var URL = 'chatservice.asmx/GetChatData?tm='+new Date().getTime();
    var values= '{"count":"'+chatcount+'"}';
    AjaxCall(URL,values,ONS_GetChatData);
}



var newchatmargin=10;
var chtpredata='';
function ONS_GetChatData()
{
    if (xmlhttp.readyState == 4)
    {
        if (xmlhttp.status == 200)
        {
            var data = eval('('+xmlhttp.responseText+')');
            if(data.d != null)
            {
                window.focus();
                if($get('cht'+data.d.fid) != null)
                {
                    $get('cht'+data.d.fid).style.display = 'block';
                    if(chtpredata != data.d.data)
                    {
                        chtpredata = data.d.data;
                        var sml1='';
                        var sml2='';
                        var text=data.d.data;
                        while(text.indexOf('[:)') >= 0)
                        {
                            if(text.indexOf('[:)') >= 0)
                            {
                                var smlindex = text.indexOf('[:)');
                                var smileimg = text.substring(smlindex,smlindex + 6);
                                var smlnum = smileimg.split(':')[1];                            
                                var imgs = smlnum.indexOf(')')+1;
                                var imge = smlnum.indexOf(']');
                                smlnum = smlnum.substring(imgs,imge);
                                sml1=text.substring(0,smlindex);
                                sml2=text.substring(text.indexOf(']')+1,text.length);
                                text = sml1+'<img src="smileys/sml'+smlnum+'.gif" />'+sml2;
                            }
                            else
                            {
                                text = data.d.data;
                            }
                        }
			            $get('msgText'+data.d.fid).innerHTML = $get('msgText'+data.d.fid).innerHTML + '<div style="width:100%;border-bottom:1px solid #cccccc;margin-bottom:2px;"><b>'+data.d.name+':</b>'+text+'</div>';
			            var chatcount=$get('activechatcount').value;
			            $get('activechatcount').value = data.d.count;
			            scrollToBottom('msgText'+data.d.fid);
			        }
			        else
			        {
			            chtpredata = '';
			        }
			    }
			    else
			    {
			        var smilee = '<div style="cursor:pointer;width:190px;" onclick="ShowSmilee('+data.d.fid+')"><img src="smileys/main.gif" /> smileys </div>';
			        smilee = smilee + '<div id="sml'+data.d.fid+'" style="z-index:1000;height:120px;width:145px;background-color:#FFFFFF;position:absolute;display:none;" align="center">';
			        smilee = smilee + '<table width="100%" style="text-align:center;">';
			        
			        smilee = smilee + '<tr> <td><img src="smileys/sml1.gif" alt="[:)1]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+');" /></td>';
			        smilee = smilee + '<td><img src="smileys/sml2.gif" alt="[:)2]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+');" /></td>';
			        smilee = smilee + '<td><img src="smileys/sml3.gif" alt="[:)3]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+');" /></td>';
			        smilee = smilee + '<td><img src="smileys/sml4.gif" alt="[:)4]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+');" /></td> </tr>';
			        
			        smilee = smilee + '<tr> <td><img src="smileys/sml5.gif" alt="[:)5]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+')" /></td>';
			        smilee = smilee + '<td><img src="smileys/sml6.gif" alt="[:)6]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+');" /></td>';
			        smilee = smilee + '<td><img src="smileys/sml7.gif" alt="[:)7]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+');" /></td>';
			        smilee = smilee + '<td><img src="smileys/sml8.gif" alt="[:)8]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+');" /></td> </tr>';
			        
			        smilee = smilee + '<tr> <td><img src="smileys/sml9.gif" alt="[:)9]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+')" /></td>';
			        smilee = smilee + '<td><img src="smileys/sml10.gif" alt="[:)10]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+');" /></td>';
			        smilee = smilee + '<td><img src="smileys/sml11.gif" alt="[:)11]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+');" /></td>';
			        smilee = smilee + '<td><img src="smileys/sml12.gif" alt="[:)12]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+');" /></td> </tr>';
			        
			        smilee = smilee + '<tr> <td><img src="smileys/sml13.gif" alt="[:)13]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+')" /></td>';
			        smilee = smilee + '<td><img src="smileys/sml14.gif" alt="[:)14]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+');" /></td>';
			        smilee = smilee + '<td><img src="smileys/sml15.gif" alt="[:)15]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+');" /></td>';
			        smilee = smilee + '<td><img src="smileys/sml16.gif" alt="[:)16]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+data.d.fid+');" /></td> </tr>';
			        
			        smilee = smilee + '</table></div>';
			        
			        
			        var div1 = document.createElement('div');
                    div1.setAttribute('style','right:0;background-color:#FFFFFF;position:absolute;width:200px;height:250px;margin-right:'+newchatmargin+'px;background-image:url("images/chatBgblank.png");');
                    div1.setAttribute('id','cht'+data.d.fid);
                    div1.setAttribute('class','content');
                    
                    
                    var div1a = document.createElement('div');
                    div1a.setAttribute('style','width:100%;height:15%;');
                    div1a.setAttribute('onmousedown','DownMe(this,event)');
                    div1a.setAttribute('onmouseup','UpMe(this,event)');
                    div1a.setAttribute('onmousemove','MoveMe(this,event)');                  
                    
                    var div1b = document.createElement('div');
                    div1b.setAttribute('style','width:100%;overflow-y:scroll;height:65%;');
                    div1b.setAttribute('id','msgText' + data.d.fid);
                    
                   
                       
                    var div1c = document.createElement('textarea');
                    div1c.setAttribute('style','width:98%;height:15%;');
                    div1c.setAttribute('id','chtText'+data.d.fid);
                    div1c.setAttribute('rows','2');
                    div1c.setAttribute('cols','10');
                    div1c.setAttribute('onkeyup','SendChatData(this.value,event,'+data.d.fid+')');
                    
                     var div1d = document.createElement('div');
                    div1d.setAttribute('style','bottom:0px;width:190px;height:10%;');
                    div1d.innerHTML = smilee;
                    
                    
                    var div1a1 = document.createElement('div');
                    div1a1.setAttribute('style','float:left;color:White;padding-left:10px;');
                    div1a1.innerHTML = '<b>'+data.d.name+'</b>';
                    
                    
                    var div1a2 = document.createElement('div');
                    div1a2.setAttribute('style','float:right;font-size:20px;cursor:pointer;color:Red;font-weight:bold;height:20px;width:20px;');
                    div1a2.setAttribute('onclick','Closethis('+data.d.fid+')');
                    div1a2.innerHTML = 'X';
            
                    div1a.appendChild(div1a1);
                    div1a.appendChild(div1a2);
                    div1.appendChild(div1a);
                    div1.appendChild(div1b);
                    div1.appendChild(div1d);
                    div1.appendChild(div1c);
                    
                    document.body.appendChild(div1);
                    newchatmargin = newchatmargin + 240;
			    }
		        if(chatcount != data.d.count)
		        {
		            var URL = 'chatservice.asmx/GetMyChatList?tm='+new Date().getTime();
                    var values= '{"t":"t"}';
                    AjaxCall(URL,values,ONS_GetMyChatList);
		        }
            }
		}
    }
}


function ONS_GetMyChatList()
{
    if (xmlhttp.readyState == 4)
    {
        if (xmlhttp.status == 200)
        {
            var data = eval('('+xmlhttp.responseText+')');
            if(data.d != null)
            {
                $get('myChatListDiv').innerHTML = data.d;
            }
		}
    }
}



function SetNewWindow(id,name)
{
        
        var smilee = '<div style="cursor:pointer;width:190px;" onclick="ShowSmilee('+id+')"><img src="smileys/main.gif" /> smileys </div>';
        smilee = smilee + '<div id="sml'+id+'" style="z-index:1000;height:120px;width:145px;background-color:#FFFFFF;position:absolute;display:none;" align="center">';
        smilee = smilee + '<table width="100%" style="text-align:center;">';
        
        smilee = smilee + '<tr> <td><img src="smileys/sml1.gif" alt="[:)1]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td>';
        smilee = smilee + '<td><img src="smileys/sml2.gif" alt="[:)2]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td>';
        smilee = smilee + '<td><img src="smileys/sml3.gif" alt="[:)3]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td>';
        smilee = smilee + '<td><img src="smileys/sml4.gif" alt="[:)4]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td> </tr>';
        
        smilee = smilee + '<tr> <td><img src="smileys/sml5.gif" alt="[:)5]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td>';
        smilee = smilee + '<td><img src="smileys/sml6.gif" alt="[:)6]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td>';
        smilee = smilee + '<td><img src="smileys/sml7.gif" alt="[:)7]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td>';
        smilee = smilee + '<td><img src="smileys/sml8.gif" alt="[:)8]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td> </tr>';
        
        smilee = smilee + '<tr> <td><img src="smileys/sml9.gif" alt="[:)9]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td>';
        smilee = smilee + '<td><img src="smileys/sml10.gif" alt="[:)10]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td>';
        smilee = smilee + '<td><img src="smileys/sml11.gif" alt="[:)11]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td>';
        smilee = smilee + '<td><img src="smileys/sml12.gif" alt="[:)12]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td> </tr>';
        
        smilee = smilee + '<tr> <td><img src="smileys/sml13.gif" alt="[:)13]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td>';
        smilee = smilee + '<td><img src="smileys/sml14.gif" alt="[:)14]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td>';
        smilee = smilee + '<td><img src="smileys/sml15.gif" alt="[:)15]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td>';
        smilee = smilee + '<td><img src="smileys/sml16.gif" alt="[:)16]" style="cursor:pointer;" onclick="return SendSmilee(this.alt,'+id+');" /></td> </tr>';
        
        smilee = smilee + '</table></div>';
        
        
        
        
        var div1 = document.createElement('div');
        div1.setAttribute('style','right:0;background-color:#FFFFFF;position:absolute;width:200px;height:250px;margin-right:'+newchatmargin+'px;background-image:url("images/chatBgblank.png");');
        div1.setAttribute('id','cht'+id);
        
        var div1a = document.createElement('div');
        div1a.setAttribute('style','width:100%;height:15%;');
        div1a.setAttribute('onmousedown','DownMe(this,event)');
        div1a.setAttribute('onmouseup','UpMe(this,event)');
        div1a.setAttribute('onmousemove','MoveMe(this,event)'); 
        
        var div1b = document.createElement('div');
        div1b.setAttribute('style','width:100%;overflow-y:scroll;height:65%;');
        div1b.setAttribute('id','msgText' + id);        
        
        var div1c = document.createElement('textarea');
        div1c.setAttribute('style','position:absolute;width:98%;height:15%;');
        div1c.setAttribute('id','chtText'+id);
        div1c.setAttribute('rows','2');
        div1c.setAttribute('cols','10');
        div1c.setAttribute('onkeyup','SendChatData(this.value,event,'+id+')');
        
         var div1d = document.createElement('div');
        div1d.setAttribute('style','bottom:0px;width:190px;height:10%;');
        div1d.innerHTML = smilee;
        
        var div1a1 = document.createElement('div');
        div1a1.setAttribute('style','float:left;color:White;padding-left:10px;');
        div1a1.innerHTML = '<b>'+name+'</b>';
        
        var div1a2 = document.createElement('div');
        div1a2.setAttribute('style','float:right;font-size:20px;cursor:pointer;color:Red;font-weight:bold;height:20px;width:20px;');
        div1a2.setAttribute('onclick','Closethis('+id+')');
        div1a2.innerHTML = 'X';
        
        div1a.appendChild(div1a1);
        div1a.appendChild(div1a2);
        div1.appendChild(div1a);
        div1.appendChild(div1b);
        div1.appendChild(div1d);
        div1.appendChild(div1c);
        
        document.body.appendChild(div1);
        newchatmargin = newchatmargin + 240;
}



function ShowChatList()
{
    $get('chtlist').style.display = 'block';
    var URL = 'chatservice.asmx/GetMyChatList?tm='+new Date().getTime();
    var values= '{"t":"t"}';
    AjaxCall(URL,values,ONS_GetMyChatList);
}




function SendChatData(text,e,id)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    if(keycode == 13 && text != '' && text != '\r')
    {
        var sml1='';
        var sml2='';
        var textcht=text;
        while(textcht.indexOf('[:)') >= 0)
        {
            if(textcht.indexOf('[:)') >= 0)
            {
                var smlindex = textcht.indexOf('[:)');
                var smileimg = textcht.substring(smlindex,smlindex + 6);
                var smlnum = smileimg.split(':')[1];
                var imgs = smlnum.indexOf(')')+1;
                var imge = smlnum.indexOf(']');
                smlnum = smlnum.substring(imgs,imge);
                sml1=textcht.substring(0,smlindex);
                sml2=textcht.substring(textcht.indexOf(']')+1,textcht.length);
                textcht = sml1+'<img src="smileys/sml'+smlnum+'.gif" />'+sml2;
            }
            else
            {
                textcht = text;
            }
        }               
        $get('msgText'+id).innerHTML = $get('msgText'+id).innerHTML+'<div style="width:100%;border-bottom:1px solid #cccccc;margin-bottom:2px;"><b>me:</b>'+textcht+'</div>';
        $get('chtText'+id).value = '';
        scrollToBottom('msgText'+id);
        var URL = 'chatservice.asmx/SendChatData?tm='+new Date().getTime();
        var values= '{"text":"'+text+'","to":"'+id+'"}';
        AjaxCall(URL,values,ONS_SendChatData);
    }
}
function ONS_SendChatData()
{
    
}


