﻿//// JScript File

var _winHandler;
var objTimer;
//This function will open the Instant Message window
function ShowInstantMessagePopUp()
{
    var _queryString;
    var width=screen.width;
	var height=screen.height;
	var top= height/2-200;
	var left= width/2-200;
    var Path; 
    var PageName;
    var toUserIDs="",toUserID="",fromUserID="";
    
    if(document.getElementById("hidInstantMessage") != null )
	{
	    if (document.getElementById("hidInstantMessage").value  != "")
	    {
	        alert(document.getElementById("hidInstantMessage").value); 
	        //document.getElementById("hidInstantMessage").value = "";
	        return false;
	    }
	}
	
	if(document.getElementById("hidFromUserID") != null && document.getElementById("hidToUserID") != "")
    {
    
        //get the FromUserID
        fromUserID = document.getElementById("hidFromUserID").value;
        //get the ToUserID
        toUserIDs = document.getElementById("hidToUserID").value;
    	
    	Path = self.location.href;
        
        PageName = Path.substring(Path.lastIndexOf("/")+1 ,Path.length);
       
        var AryUserid = toUserIDs.split(",");
              
        for (iIndex = 0; iIndex < AryUserid.length; iIndex++)
        {               
            toUserID = AryUserid[iIndex];
            
            if (toUserID != "")
            {
                if ((PageName  == "MyHomePage.aspx") || ((PageName  == "HomePage.aspx")))
                {   
                    _queryString = "Webpages/IM/InstantMessage.aspx?FromUserID=" + fromUserID + "&ToUserID=" + toUserID;
                }
                else
                {
                    _queryString = "../IM/InstantMessage.aspx?FromUserID=" + fromUserID + "&ToUserID=" + toUserID;
                }
               
                //_winHandler = window.open(_queryString,"InstantMessage_" + fromUserID + "_" + toUserID,"height=430,width=487,top=" + top + ",left=" + left + ",toolbars=0,scrollbars=0,resizable=0");     
                _winHandler = window.open(_queryString,"InstantMessage_" + fromUserID + "_" + toUserID,"height=375,width=525,toolbars=0,scrollbars=0,resizable=0");     
             
            }
        }                 
     }       
}

var xmlHttpFetchObject;
var xmlHttpFetchChatObject;

function GetXmlHttpObject()
{ 
    var objXMLHttp;
    if (window.XMLHttpRequest)
    {
        objXMLHttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return objXMLHttp;
}
// JScript File

//function CheckForChatMessage(_offlineFlag,_chatMsg)
//{
//    var _toUserName; 
//    if (_offlineFlag == "yes")
//    {
//        if (document.getElementById("hidToUserName") != null)
//        {
//            document.getElementById("hidOfflineMsgSet").value = "yes"; 
//            _toUserName = document.getElementById("hidToUserName").value; 
//            document.getElementById("divChatMsg").innerHTML = document.getElementById("divChatMsg").innerHTML + _toUserName + " is now offline....";
//            document.getElementById("divChatMsg").scrollTop = document.getElementById("divChatMsg").scrollHeight;
//            
//            //clearTimeout(TimerID);            
//        }   
//    }
//    else if (_offlineFlag == "no")
//    {      
//           document.getElementById("divChatMsg").innerHTML = _chatMsg;
//           document.getElementById("divChatMsg").scrollTop = document.getElementById("divChatMsg").scrollHeight;
//           document.getElementById("txtChatMsg").focus();           	
//    }           
//}

function UpdateWindowArray()
{
    if (arrWindow != null)
    {
        for (var key in  arrWindow)
        {
            if (key != null)
            {
                var objWindow = arrWindow[key];
                //Enable the timer on the opened window
                if (objWindow != null)
                {    
                    objWindow.UpdateParentData();       
                }
            }
        }
   }     
}

function FetchData()
{ 
    var Path,PageName;
    var _queryString;

    xmlHttpFetchObject=GetXmlHttpObject();
    if (xmlHttpFetchObject==null)
    {
        alert("Browser does not support HTTP Request.");
        return;
    } 
    
    Path = self.location.href;

    PageName = Path.substring(Path.lastIndexOf("/")+1 ,Path.length);    
   
    if ((PageName  == "MyHomePage.aspx") || (PageName  == "HomePage.aspx") || PageName.indexOf("PublicProfile.aspx") >= 0)
    {   
        _queryString = "Webpages/IM/ProcessAction.aspx";
    }
    else
    {
        _queryString = "../IM/ProcessAction.aspx";
    }       
    
    xmlHttpFetchObject.onreadystatechange=stateChangedFetchData;
    xmlHttpFetchObject.open("GET",_queryString,true);
    xmlHttpFetchObject.send(null);
    return false;
}

function stateChangedFetchData() 
{ 
    var data,msgData,ChatLog="";
    var Message="",toUserid,responseData,FriendData;
    var fromUserId,key;
    var windowHandler;
    var OnlineFriends,PrevOnlineFriends;
    var OnlineUsers,PrevOnlineUsers;
    
    var updateFlag;
    
    if(xmlHttpFetchObject==null)
    {
        return;
    }
    
    if (xmlHttpFetchObject.readyState==4 )
    {   
       //Send the data to the flash program
       var virtualdata=xmlHttpFetchObject.responseText;
       
    
       
       if(virtualdata!="")
       {
             //Check if Online Friends are present for this user           

            FriendData = virtualdata.split("%%^%%");
            if (FriendData != null)
            {
                if (FriendData[0] != null)
                {   
                    var objDiv  = document.getElementById("TopFrame1_pnlIMPopUp");
                    if (objDiv != null)
                    {
                        if (objDiv.style.display != 'none')
                        {  
                            OnlineFriends = FriendData[0].replace("OnlineFriendList=", "");
                            OnlineUsers = FriendData[1].replace("OnlineUserList=", "");
//                                    if (OnlineFriends != "" )
//                                    { 
                             //Here check if the Online friend list is updated 
                             // OnlineFriends is the updated one and PrevOnlineFriends is previous list
                             PrevOnlineFriends = document.getElementById("TopFrame1_hidPrevOnlineFriendsList").value;
                             
                             PrevOnlineUsers = document.getElementById("TopFrame1_hidPrevOnlineUsersList").value;
                              
                             updateFlag = CheckWhetherUpdated(OnlineFriends,PrevOnlineFriends);
                             
                             //If the online friend list has updated, then no need to check for the online users list
                             if (updateFlag == false)
                             {
                                updateFlag = CheckWhetherUpdated(OnlineUsers,PrevOnlineUsers);
                             }  
                             
                             if (updateFlag == true)
                             {
                                 //document.getElementById("TopFrame1_hidActiveChatUsers").value = OnlineFriends + "," + OnlineUsers;
                                 CheckForOfflineMessage(OnlineFriends + "," + OnlineUsers);
                                 var btnGetFriends = document.getElementById("TopFrame1_btnGetFriends");
                                
                                 if (btnGetFriends != null)
                                 {                        
                                        var _onlineFriends = document.getElementById("TopFrame1_hidOnlineFriendsList");
                                        if (_onlineFriends != null)
                                        {
                                            _onlineFriends.value = OnlineFriends;
                                        }
                                        
                                        var _onlineUsers = document.getElementById("TopFrame1_hidOnlineUsersList");
                                        if (_onlineUsers != null)
                                        {
                                            _onlineUsers.value = OnlineUsers;
                                        }
                                        var is_Safari = navigator.userAgent.indexOf('Safari');
                                        if(is_Safari != -1)
                                        {                                           
                                            btnGetFriends.click();
                                        }
                                        else
                                        {
                                            //Fire the click event
                                            if (document.all)
                                            {
                                                // IE
                                                btnGetFriends.click();
                                            }
                                            else
                                            {
                                                // FireFox 
                                                var e = document.createEvent("MouseEvents");
                                                e.initEvent("click", true, true);
                                                btnGetFriends.dispatchEvent(e);
                                            }
                                        }                                        
                                 }//End If : if (btnGetFriends != null)
                                 
                             }//End IF : if (updateFlag == true )
                               
                        }//End If : if (objDiv.style.display != 'none')
                       
                    }//End If : if (objDiv != null)

                   virtualdata = virtualdata.replace(FriendData[0]+ "%%^%%" , "");
                   virtualdata = virtualdata.replace(FriendData[1]+ "%%^%%" , "");
                }//End If : if (FriendData[0] != null)
                
            }//End If :  if (FriendData != null)    

            if (virtualdata != "")
            {
            //%%$%% is the delimeter for user
            responseData = virtualdata.split("%%$%%");
            if (responseData != null)
            {
                for (count= 0; count < responseData.length ; count++)
                {
                    //##$$@$$## is the delimeter for particular user data
                    data = virtualdata.split("##$$@$$##");
                   
                    if (data[0] != null)
                    {   
                       toUserid  = data[0];
                       toUserid = toUserid.replace("toUserID=","");               
                    }
                    if (data[1] != null)
                    {  
                        ChatLog = data[1].replace("ChatLog=" ,"");
                    }
                    if (data[2] != null)
                    {  
                        OnlineFriends = data[1].replace("OnlineFriends=" ,"");
                    }
                             
                    //Check if for this user window is open or not in the arrayWindow
                    fromUserId = document.getElementById("PageFooter_hUserId").value;
                    
                    key = "IM-" + fromUserId + "-" + toUserid;
                    
                    if (arrWindow[key] != null)
                    {
                        //Window is open
                        windowHandler = arrWindow[key];
                    }
                    else
                    {
                        key = "IM-" + toUserid + "-" + fromUserId;
                        if (arrWindow[key] != null)
                        {
                            //Window is open
                            windowHandler = arrWindow[key];
                        }    
                    }
                                         
                    if (windowHandler != null)
                    {
                        var ChatMsgDiv = windowHandler.document.getElementById('divChatMsg');
                        
                        var ChatMsgTxt = windowHandler.document.getElementById('txtChatMsg');
                         
                        if (ChatLog != "" )
                        {
                            //Window is open, so update the Chat Log
                            if (ChatMsgDiv != null)
                            {
                                ChatMsgDiv.innerHTML = ChatLog;
                                ChatMsgDiv.scrollTop = ChatMsgDiv.scrollHeight;
                                //windowHandler.blur();
                            }
                            if (ChatMsgTxt != null)
                            {
                               ChatMsgTxt.focus();           	
                            }
                        }
                    }
                    else
                    {
                        if (ChatLog != "")
                        {
                            //Window is not open, so open
                            ShowInstantMessageWin(toUserid);                     
                        }
                    }
                }    
            }
         }       
      }
      setTimeout("FetchData()",5000);       
    }     
} 

//This function will open the Instant Message window
function ShowInstantMessageWin(toUserID)
{
    var _queryString;
    var width=screen.width;
	var height=screen.height;
	var top= height/2-200;
	var left= width/2-200;
    var Path; 
    var PageName;
    var fromUserID="";    
    
	if(document.getElementById("PageFooter_hUserId") != null && toUserID != "")
    {   
        //get the FromUserID
        fromUserID = document.getElementById("PageFooter_hUserId").value;
       
       //Check if window is already open in the arrWindow array of Parent window
       var key = "IM-" + fromUserID + "-" + toUserID;

        if(document.getElementById("PageFooter_hidInstantMessage") != null )
        {
            if (document.getElementById("PageFooter_hidInstantMessage").value  != "")
            {
                alert(document.getElementById("PageFooter_hidInstantMessage").value); 
                document.getElementById("PageFooter_hidInstantMessage").value = "";
                return false;
            }
        }
        //Window is not open, so open the IM window
        Path = self.location.href;
        PageName = Path.substring(Path.lastIndexOf("/")+1 ,Path.length);
    	
        if (toUserID != "")
        {
            if ((PageName  == "MyHomePage.aspx") || ((PageName  == "HomePage.aspx")))
            {   
                _queryString = "Webpages/IM/InstantMessage.aspx?FromUserID=" + fromUserID + "&ToUserID=" + toUserID;
            }
            else
            {
                _queryString = "../IM/InstantMessage.aspx?FromUserID=" + fromUserID + "&ToUserID=" + toUserID;
            }
            //window.open(_queryString,"InstantMessage_" + fromUserID + "_" + toUserID,"height=430,width=487,top=" + top + ",left=" + left + ",toolbars=0,scrollbars=0,resizable=0");     
            window.open(_queryString,"InstantMessage_" + fromUserID + "_" + toUserID,"height=375,width=525,toolbars=0,scrollbars=0,resizable=0");     
        }
    }
 }
 
 function CheckForOpenIMWindow(toUserID,AllowedMsg)
 {
    // alert("AllowedMsg"+AllowedMsg);
    
    //It means AllowedMsgFlag is false then user is not allowed to send this message
    if(AllowedMsg==0)
    {
        //alert("Sorry, you do not have access to send Instant Message to this user.");
        ShowInformationMessageBox("Sorry, you do not have access to send Instant Message to this user.");
        return;
    }
     
    if(document.getElementById("PageFooter_hUserId") != null && toUserID != "")
    { 
        var windowHandler;
        //Check if for this user window is open or not in the arrayWindow
        var fromUserID = document.getElementById("PageFooter_hUserId").value;
        
        var key = "IM-" + fromUserID + "-" + toUserID;
        //check if window is alreay opened
        if (arrWindow[key] != null)
        {
            //Window is open
            windowHandler = arrWindow[key];
        }
        else
        {
            key = "IM-" + toUserID + "-" + fromUserID;
            if (arrWindow[key] != null)
            {
                //Window is open
                windowHandler = arrWindow[key];
            }    
       }
       if (windowHandler == null)
       {
           //Open the window 
           ShowInstantMessageWin(toUserID);    
       }
    }
 }
 
 //This function will compare OnlineFriends and PrevOnlineFriends list and will identify if the list is updated
 function CheckWhetherUpdated(OnlineFriends,PrevOnlineFriends)
 {
    var matchFound; 
    //If PrevOnlineFriends is blank and OnlineFriends is not blank means the new list is updated
    if ((PrevOnlineFriends == "") && (OnlineFriends != ""))
    {
        return true;
    }
    
    if ((PrevOnlineFriends != "") && (OnlineFriends == ""))
    {
        return true;
    }
        
    if ((PrevOnlineFriends == "") && (OnlineFriends == ""))
    {
        return false;
    }
 
    //Both are not blank, so compare them one by one
    var AryPrevOnlineFriends , AryOnlineFriends;
    var lenPrevOnlineFriends , lenOnlineFriends;
    
    AryPrevOnlineFriends = PrevOnlineFriends.split(",");
    
    AryOnlineFriends = OnlineFriends.split(",");
    
    if ((AryPrevOnlineFriends != null) && (AryOnlineFriends != null))
    {
        lenPrevOnlineFriends = AryPrevOnlineFriends.length;
        lenOnlineFriends = AryOnlineFriends.length;
        if (lenOnlineFriends != lenPrevOnlineFriends)
        {
            return true;
        }
            
        var count1,count2;
        
        for (count1 = 0 ; count1 < lenPrevOnlineFriends; count1++)
        {
            matchFound = 0 ;
            for (count2 = 0 ; count2 < lenOnlineFriends; count2++)
            {
                if (AryPrevOnlineFriends[count1] == AryOnlineFriends[count2])
                {
                    matchFound = 1;
                    break;                
                }
            }
            if (matchFound == 0)
            {
                //for one id in PrevOnlineFriends there is not match present in OnlineFriends, so return true
                return true;
            }
        }
        
        return false;
        
    }//End If : if ((AryPrevOnlineFriends != null) && (AryOnlineFriends != null))    
 }
 
function CheckForMessageWin()
{
    //Check if the chat is updated by someone
    var objHidden = document.getElementById("hidUpdateChatLogFlag");
    if(objHidden != null && objHidden != "")
    {
        if(objHidden.value=="yes")
        {
           //alert('yes');
           objHidden.value="";
           document.getElementById("divChatMsg").innerHTML = document.getElementById("hidCurrentChatLog").value ;
           document.getElementById("divChatMsg").scrollTop = document.getElementById("divChatMsg").scrollHeight;
        }       
        document.getElementById("txtChatMsg").focus();           	
    }          
}

function CheckForOfflineMessage(onlineUserList)
{
    var aryOnlineUserList=new Array();
    var objWindow;
    var flag,count;
    aryOnlineUserList = onlineUserList.split(",");

    if (arrWindow != null)
    {
        for (var key in  arrWindow)
        {
            if (key != null)
            {  
                 var AryUserId = key.split("-");
                 if (AryUserId != null)
                 {
                     var toUserid = AryUserId[2];
                     flag = false;
                     for (count = 0; count < aryOnlineUserList.length; count++)
                     {
                        if (aryOnlineUserList[count] == toUserid)
                        {
                            flag = true;
                            break;
                        }
                     }
                     if (flag == false)
                     {
                        //User is now offline
                        objWindow = arrWindow[key]; 
                        
                    
                        
                        if (objWindow != null)
                        {  // alert(objWindow.divChatMsg.innerHTML);
                            // var ChatMsgDiv = objWindow.document.getElementById('divChatMsg');
                             if(objWindow.divChatMsg != null)
                             {  
                                objWindow.divChatMsg.innerHTML =  objWindow.divChatMsg.innerHTML + "User is now offline ....";
                             }
                        }                    
                     }
                 }
            }
        }
    }  
}

