﻿var IsFocus=0;
var timerWindowBlink;
var timerGetHistory;
var timerNewGuestInfo;
var oldTitle = document.title;
var opwinChatTransferCheck=0;
var StartChat=0;

function findPos(obj)
{
    var curleft = curtop = 0;
    if (obj.offsetParent) 
    {
    curleft = obj.offsetLeft;
    curtop = obj.offsetTop;
    while (obj = obj.offsetParent) 
        {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
   }
    return [curleft,curtop];
}


function CallSendMsg() 
{
   if(StartChat==1)
   {
        StartChat=0;
        joinChatRoom();
   }
   else 
   {
      ChatMsg();
   }
}

function ChatMsg()
{
   var msg=document.getElementById("txt_Message");
   var chatID=document.getElementById("hdn_ChatID");
   var Uname=document.getElementById("hdn_UserName");
   var IsVisitor=document.getElementById("hdn_IsVisitor");
   if(msg.value !="")
   {
     PageMethods.SendMsg(msg.value,Uname.value,chatID.value,IsVisitor.value,OnCallSendMsgComplete);
   }
   msg.value="";
   msg.focus();
   stopBlinkTimer();
   return false;
}


 function OnCallSendMsgComplete(result,methodName)
 {
 }
 
 
 function joinChatRoom() 
 {
   var chatID=document.getElementById("hdn_ChatID");
   var Uname=document.getElementById("hdn_UserName");
   var IsVisitor=document.getElementById("hdn_IsVisitor");
   
   PageMethods.JoinChat(Uname.value,chatID.value,IsVisitor.value,OnCalljoinChatRoomComplete);
   return false;
 }

 function OnCalljoinChatRoomComplete(result,methodName)
 {
    ChatMsg();
 }  
 
 
 
 function Leave()
 {
   
   var Uname=document.getElementById("hdn_UserName");
   PageMethods.Leave(Uname.value,OnCallSendMsgComplete);
 }
 
 function OnCallLeaveComplete(result,methodName)
 {
 }
 
 function keyPressSend(evt)
 {
    if(evt.which || evt.keyCode)
    {
       if((evt.which == 13) || (evt.keyCode == 13))
       {
          CallSendMsg(); 
           return false;    
       }
           return true;
    }
 }
 
 
 function ValidatUser()
 {
        if (document.form1.txt_UserName.value == "")
		{
			alert("Please enter username.");
			document.form1.txt_UserName.focus();
			return false;
		}
        
        if (document.form1.txt_Email.value == "")
		{
			alert("Please enter email address.");
			document.form1.txt_Email.focus();
			return false;
		}
		else
		{	var emailPat = /^[A-Za-z0-9_\-]+([.][A-Za-z0-9_\-]+)*[@][A-Za-z0-9_\-]+([.][A-Za-z0-9_\-]+)+$/
			var matchArray = document.form1.txt_Email.value.match(emailPat);
			if (matchArray == null)
			{
				alert("Please enter valid email address.");
				document.form1.txt_Email.focus();
				return false;
			}					
		}
		
		return true;
 }
 
 
    var opnchatAdminRow="0";
    function openAdminChat(chatid,WinTitle,RowID)
    { 
        opnchatAdminRow=RowID;
        PageMethods.RemoveRequest(chatid,OnCallopenAdminChatComplete);
        window.open('OperatorChatRoom.aspx?chat_id='+chatid, ''+WinTitle, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=515,height=550,left = 200,top = 150');
        return false;
    }
    
    function OnCallopenAdminChatComplete(result,methodName)
    {
        document.getElementById("tblGuestInfo").deleteRow(opnchatAdminRow);
    }
   
    
    function StartTimer()
    {
       timerNewGuestInfo=setInterval("GetNewGuestInfo()",2000);
    }
    
    function GetNewGuestInfo()
    {
        PageMethods.GetGuestList(OnCallGetGuestInfoComplete);
    }
    
    
    function OnCallGetGuestInfoComplete(result,methodName)
    {
      if( result != '' )
      {
        
        var arr=new Array();
        arr=result;
        
        if(arr.length >1)
        {
            var arrGstIds=arr[0].split(',');
            
            for(var i=1;i<arr.length;i++)
            {
                
                var gstid=arrGstIds[i];
                if(document.getElementById(gstid)==null)
                {
                  var info=arr[i];
                  InsertGuest(info);
                }
            }
        }
        
      }
    }
    
    
    function InsertGuest(guestInfo)
    {
        document.getElementById("tblGuestInfo").style.display="Block";
        var theTable =document.getElementById('tblGuestInfo');
        var rowcount=0;
        var rownum=theTable.rows.length;
        var x=document.getElementById('tblGuestInfo').insertRow(rownum);
        
        var c1=x.insertCell(0);
        text=guestInfo;
        c1.valign="top";
        c1.innerHTML=text;
        c1.align="left";
    }
    
    function ShowTransferChat(chtID,RowNo,lnkid,guestname)
    {
        var myObj = document.getElementById(lnkid);
        var pos = findPos(myObj);
        var left=pos[0];
        var top=pos[1];
        left=left-200;
        top=top;
        document.getElementById("div_TransferChat").style.display="block";
        document.getElementById("div_TransferChat").style.left=left+"px";
        document.getElementById("div_TransferChat").style.top=top+"px";
        
        document.getElementById("drp_OnlineOperators").value="";
        document.getElementById("hdn_ChatID").value=chtID;
        document.getElementById("spn_GuestName").innerHTML=guestname;
        document.getElementById("hdn_Rownum").value=RowNo;
        
    }
    
    function HideTransferChat()
    {
        document.getElementById("div_TransferChat").style.display="none";
    }
    
    function validateTransfer()
    {
        if(document.form1.drp_OnlineOperators.value=="")
        {
          alert("Please select operator.");
          document.form1.drp_OnlineOperators.focus();
          return false;
        }
        
        var ChtID=document.getElementById("hdn_ChatID").value;
        var VstName=document.getElementById("spn_GuestName").innerHTML;
        var OperatorID=document.getElementById("drp_OnlineOperators").value;
        PageMethods.TransferChat(ChtID,VstName,OperatorID,OnCallTransferChat);
        
        var rownum=document.getElementById("hdn_Rownum").value;
        document.getElementById("tblGuestInfo").deleteRow(rownum);
        HideTransferChat();
        return false;       
    }
    
   function OnCallTransferChat(result,methodName)
   {
   }
   
   
   function StartMessageHistoryTimer()
   {
       timerGetHistory= setInterval("GetChatHistory()",200);
   }
   
   function GetChatHistory()
   {
         clearInterval(timerGetHistory);
         var chtID=document.getElementById("hdn_ChatID").value;
         var lstTM=document.getElementById("hdn_LastTime").value;
         var UsrName=document.getElementById("hdn_UserName").value;
         PageMethods.ChatHistory(lstTM,chtID,UsrName,OnCallChatHistoryComplete);                  
   }
   
   
   function OnCallChatHistoryComplete(result,methodName)
   {
      if( result != '' )
      {
            var arr=new Array();
            arr=result;
    
            if( arr.length >2)
            {
                
                var rstlstTM=arr[0];
                if(rstlstTM !="")
                {
                    document.getElementById("hdn_LastTime").value=rstlstTM;
                }
                
                for(var i=2;i<arr.length;i++)
                {
                    var info=arr[i];
                    InsertChat(info);
                }
              
                var hasblink=arr[1];
                if(hasblink !="0")
                {
                    if(IsFocus==0)
                    {
                       newMessageAlerts(hasblink);
                    }
                    IsFocus=1;
                }
                
               ScrollChatDiv();
            }      
        
       }
       StartMessageHistoryTimer();    
      
   }
   
   
   function InsertChat(guestInfo)
    {
       
        document.getElementById("tblGuestInfo").style.display="Block";
        var theTable =document.getElementById('tblGuestInfo');
        var rowcount=0;
        var rownum=theTable.rows.length;
        var x=document.getElementById('tblGuestInfo').insertRow(rownum);
       
        var c=x.insertCell(0);
        text=guestInfo;
        c.valign="top";
        c.innerHTML=text;
        
    }
    
    function newMessageAlerts(uname) {
    var msg = uname+" says.....";
     timerWindowBlink = setInterval(function() {
        document.title = document.title == msg ? ' ' : msg;
    }, 1000);
    window.onfocus = function() {
        stopBlinkTimer();
        window.onfocus = null;
    };
    
}

function stopBlinkTimer()
{
    if((timerWindowBlink !=null) && (typeof timerWindowBlink  !=  'undefined'))
    {
        clearInterval(timerWindowBlink);
        document.title = oldTitle;
        IsFocus=0;
    }
}


function ScrollChatDiv()
{
    var d;
	if ((d = document.getElementById('chat')) && ('undefined' != typeof d.scrollTop))
	{
		d.scrollTop = 5000;
	}
}

var ChatReqRowID="0";
function RemoveChatRequest(chatID,RowID)
{
    if (confirm("Are you sure you want to delete this request?"))
	{
	   ChatReqRowID=RowID;
       PageMethods.RemoveRequest(chatID,OnCallRemoveChatRequestComplete);
	   return true;
	}
	return false;
    
}

function OnCallRemoveChatRequestComplete(result,methodName)
{
    document.getElementById("tblGuestInfo").deleteRow(ChatReqRowID);
}

function ChngOnlineStatus()
{
    PageMethods.ChangeOnlineStatus(OnCallChangeStatusComplete);
}

function OnCallChangeStatusComplete(result,methodName)
{
}

function OpwinShowTransferChat()
{
    var myObj = document.getElementById("lnk_TransferChat");
    var pos = findPos(myObj);
    var left=pos[0];
    var top=pos[1];
    left=left+100;
    top=top-80;
    document.getElementById("div_TransferChat").style.display="block";
    document.getElementById("div_TransferChat").style.left=left+"px";
    document.getElementById("div_TransferChat").style.top=top+"px";       
    document.getElementById("drp_OnlineOperators").value="";
    document.getElementById("spn_guestname").innerHTML=document.getElementById("hdn_GuestName").value;
}
   
function OpwinValidateTransferChat()
{
    if(document.form1.drp_OnlineOperators.value=="")
    {
      alert("Please select operator.");
      document.form1.drp_OnlineOperators.focus();
      return false;
    }
    
    var ChtID=document.getElementById("hdn_ChatID").value;
    var VstName=document.getElementById("hdn_GuestName").value;
    var OperatorID=document.getElementById("drp_OnlineOperators").value;
    PageMethods.TransferChat(ChtID,VstName,OperatorID,OnCallOpwinValidateTransferChat);   
    return false;       
}  
   
  function OnCallOpwinValidateTransferChat(result,methodName)
  {
     opwinChatTransferCheck=1;
     window.close();
  } 
  
 function UnjoinChatRoom() 
 {
   var chatID=document.getElementById("hdn_ChatID");
   var Uname=document.getElementById("hdn_UserName");
   var IsVisitor=document.getElementById("hdn_IsVisitor");
   PageMethods.UnJoinChat(Uname.value,chatID.value,IsVisitor.value,OnCallUnjoinChatRoomComplete);
 }

 function OnCallUnjoinChatRoomComplete(result,methodName)
 {
 }  
 
 
 function ProcessingImage()
{
    var myObj = document.getElementById("btn_Submit");
    var pos = findPos(myObj);
    var left=pos[0];
    var top=pos[1];
    left=left-25;
    top=top+25;
    document.getElementById("div_Processing").style.display="block";
    document.getElementById("div_Processing").style.left=left+"px";
    document.getElementById("div_Processing").style.top=top+"px";
 
}

function ValidatLeaveMessageUser()
 {
        if (document.form1.txt_FistName.value == "")
        {
	        alert("Enter the first name.");
	        document.form1.txt_FistName.focus();
	        return false;
        }
        
        if (document.form1.txt_LastName.value == "")
        {
	        alert("Enter the last name.");
	        document.form1.txt_LastName.focus();
	        return false;
        }
        
        if (document.form1.txt_Email.value == "")
        {
	        alert("Enter the email address.");
	        document.form1.txt_Email.focus();
	        return false;
        }
        else
        {	var emailPat = /^[A-Za-z0-9_\-]+([.][A-Za-z0-9_\-]+)*[@][A-Za-z0-9_\-]+([.][A-Za-z0-9_\-]+)+$/
	        var matchArray = document.form1.txt_Email.value.match(emailPat);
	        if (matchArray == null)
	        {
		        alert("Please enter valid email address.");
		        document.form1.txt_Email.focus();
		        return false;
	        }					
        }
		ProcessingImage();
        return true;
 }
  
  
        




