﻿// JScript File


//======== This is for Close Opened_ChatWindow ======//
        function clos(e)
		{
		    var obj;
		    var parentDIV;
		    var mainDiv;
		    try
		    {
		        if (window.event) 
		        {
                    obj = window.event.srcElement;
                    parentDIV = obj.parentNode;
                    mainDiv=parentDIV.parentNode;
                    var divTitle = obj.previousSibling.innerHTML;
                    isClose(divTitle);
                    document.getElementById("divChat").removeChild(mainDiv);
                } 
                else if (e)
                {
                    obj = e.target;
                    parentDIV = obj.parentNode;
                    mainDiv=parentDIV.parentNode;
                    var divTitle = obj.previousSibling.innerHTML;
                    isClose(divTitle);
                    document.getElementById("divChat").removeChild(mainDiv);
                }
                else return;
            }
            catch(err)
            {
                alert('Error : ' + err);
            }
		}
		
		//======== This is for Update the Opened Chat Window status in dataBase after close ======//
		function isClose(otherName)
        {
             if(document.getElementById("lblMySelf") != null)
			 {
			      LoggedInUser = document.getElementById("lblMySelf").innerHTML;
			 }
			 else if(document.getElementById("ctl00_hidUserName") != null)
			 {
			      LoggedInUser = document.getElementById("ctl00_hidUserName").value;
			 }
			 else
			 {
			      return false;
			 }
			 var queryString = "uName=" + LoggedInUser + "&otherName=" + otherName;
             //alert(queryString);
             var url = "ChatWindowStatusUpdate.aspx";
             var pars = queryString;
             var myAjax = new Ajax.Request(url, { method: 'post', parameters: pars, onSuccess: showAction }); 
        }
        function showAction(originalRequest)
        {
           //alert('Hello');
           var x=originalRequest.responseText;
           //alert(x);
        }

            //======== This is for Create a New Chat Window ======//
            function NewOpenChatWindow(userName)
			{
			    try
			    {
			        var LoggedInUser;
			        
			        var  x=document.getElementById("ChatMainWindow_" + userName);
                    if(x==null)
                    {
			            if(document.getElementById("lblMySelf") != null)
			            {
			                LoggedInUser = document.getElementById("lblMySelf").innerHTML;
			            }
			            else if(document.getElementById("ctl00_hidUserName") != null)
			            {
			                LoggedInUser = document.getElementById("ctl00_hidUserName").value;
			            }
			            else
			            {
			                return false;
			            }
			            var queryString = userName + "&un=" + LoggedInUser;
				        ParentElement=document.getElementById("divChat");
        				
				        //To Add a Main DIV
				        var Divelement=document.createElement("DIV");
				        Divelement.setAttribute('id','ChatMainWindow_'+userName);
				        Divelement.style.styleFloat = "right"; //=== This is for IE =====//
				        Divelement.style.cssFloat = "right";   //=== This is for Mozilla =====//
			            ParentElement.appendChild(Divelement);
        			    			    
				        //To Add a Title Area 
				        var divTitleArea=document.createElement('DIV');
				        divTitleArea.setAttribute('id','TitleArea');
				        divTitleArea.style.Align='left';
				        Divelement.appendChild(divTitleArea);
        				
    				    //To Add a Title Area Icon
				        var chatWindowTitleLeft=document.createElement('DIV');
				        chatWindowTitleLeft.setAttribute('id','chatWindowTitleLeft');
				        chatWindowTitleLeft.appendChild(document.createTextNode(' ')); 
				        //chatWindowTitleLeft.style.Align='left';
				        divTitleArea.appendChild(chatWindowTitleLeft);
        				
    				    //To Add a Title Area UserName
				        var chatWindowTitleBg=document.createElement('DIV');
				        chatWindowTitleBg.setAttribute('id','chatWindowTitleBg');
				        chatWindowTitleBg.appendChild(document.createTextNode(userName));
				        chatWindowTitleBg.style.Align='left';
				        divTitleArea.appendChild(chatWindowTitleBg);
        				
    				    //To Add a Title Area Cross Button
				        var chatWindowTitleRight=document.createElement('DIV');
				        chatWindowTitleRight.setAttribute('id','chatWindowTitleRight');
				        chatWindowTitleRight.onclick= clos;
				        chatWindowTitleRight.style.cursor='pointer';
				        divTitleArea.appendChild(chatWindowTitleRight);
        				
				        //To Add a  TEXTAREA for Chat
				        var DivMessages=document.createElement('IFRAME');
				        DivMessages.setAttribute('id','iframe1');
				        DivMessages.setAttribute('align','left');
				        DivMessages.setAttribute('src',"ChatWindow.aspx?pf=" + queryString);
				        DivMessages.setAttribute('style','overflow: hidden; border: 1px solid #5B5B5B; left:0px; height:325px; width:240px;');
				        DivMessages.setAttribute('frameborder','0');
				        DivMessages.style.width="238px";
				        DivMessages.style.height="323px";
				        Divelement.appendChild(DivMessages);
                    }
                }
                catch(err)
                {
                    alert('Error : '+err);
                }
			}

