﻿var sendReqPing = getXmlHttpRequestObjectPing();
var receiveReqPing = getXmlHttpRequestObjectPing();
var lastMessagePing = 0;
var mTimerPing;

popupWins = new Array();

mTimerPing = setTimeout('startChatPing();',5000);
startChatPing();

function startChatPing() {
	//document.getElementById('txt_message').focus();
	
	getChatTextPing();
	//alert("Test123456");
	//getInvitationPing();
					
}		
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObjectPing() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        //document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object.  Consider upgrading your browser.';
    }
}

function getChatTextPing() {
    if (receiveReqPing.readyState == 4 || receiveReqPing.readyState == 0)
     { 
        var myRnd=parseInt(Math.random()*99999999);
        receiveReqPing.open("GET", 'Chat_PingServer.aspx?uid=' + document.getElementById('ucHdnToUserId').value + '&myrnd=' + myRnd, true);
		receiveReqPing.onreadystatechange = handleReceiveChatPing; 
		receiveReqPing.send(null);
	}			
}

function getInvitationPing() 
{
    if (receiveReqPing.readyState == 4 || receiveReqPing.readyState == 0) {
        var myRnd=parseInt(Math.random()*99999999);
		receiveReqPing.open("GET", 'Chat_AjaxGetInvitationPing.aspx?uid=' + document.getElementById('ucHdnToUserId').value + '&myrnd=' + myRnd, true);
		receiveReqPing.onreadystatechange = handleReceiveInvitePing; 
		receiveReqPing.send(null);
	}			
}

function handleReceiveChatPing() {
    
	if (receiveReqPing.readyState == 4) 
	{
	
        //var chat_div = document.getElementById('div_chat');
		var xmldoc = receiveReqPing.responseXML;
		var message_nodes = xmldoc.getElementsByTagName("message"); 
		var n_messages = message_nodes.length;
		
		var url;
		var winprops;
		var myname;
		
		var windowHandle_closed = true;
		
		//alert(n_messages);
		if(n_messages > 0)
		{   
		    		    
		   var strUserid = message_nodes[0].getElementsByTagName("userid")[0].firstChild.nodeValue;
		   var strChannelid = message_nodes[0].getElementsByTagName("channelid")[0].firstChild.nodeValue;
		    
		   // Added by Aparna

		   winprops = 'width=750,height=580,toolbar=0,status=0,menubar=0,scrollbars=1';
   
           url='../source/TextChat_Popup.aspx?Id=' + strUserid + '&cid=' + strChannelid;
           if (typeof(popupWins[strChannelid])!="object" ) {
                           
                popupWins[strChannelid]=window.open(url,strChannelid,winprops);
                if (window.focus) 
                {
                     popupWins[strChannelid].focus();
                }                
            }
           else {
                if (!popupWins[strChannelid].closed){}
                else {
                    popupWins[strChannelid]=window.open(url,strChannelid,winprops);
               }
            }
            
            // Added by Aparna        
           
            /*
            var strUserid = message_nodes[0].getElementsByTagName("userid")[0].firstChild.nodeValue;
		    var strChannelid = message_nodes[0].getElementsByTagName("channelid")[0].firstChild.nodeValue;		    
            checkWindows(strChannelid);
            popUp('../source/TextChat_Popup.aspx?Id=' + strUserid + '&cid=' + strChannelid + '','' + strChannelid + '','295','300','no');
            */
		}
		
		mTimerPing = setTimeout('getChatTextPing();',5000); //Refresh our chat in 5 seconds
    }
}

function checkWindows(strChannelid) 
{
     var result="";	 
	 try
		 {
	          if(eval("window."+strChannelid+".document"))
	          {
	             result+="window "+strChannelid+" is open\n"
	          }
//	          else
//	          {
//	            result+="window "+strChannelid+" is NOT open\n"
//	          }
	
	     }
	     catch(e)
		 {
		 	result+="window "+strChannelid+" is NOT open\n"
		 }
	
	 //if(result)
	 alert(result)
}

function popUp(url, myname, w, h, scroll) 
{ 
    var winl = (screen.width - w) / 2;
    var wint = ((screen.height - h) / 2)-20;
    winprops = 'height=300,width=200,top=200,left=200,scrollbars=no,toolbar=0, status=0, resizable=0,menubar=0'
    eval(myname+" = window.open(url, myname, winprops)");
//    if (parseInt(navigator.appVersion) >= 4) 
//    { 
//        eval(myname+".window.focus()"); 
//    }
}

function handleReceiveInvitePing() {
    
	if (receiveReqPing.readyState == 4) 
	{
		var xmldoc = receiveReqPing.responseXML;
		var message_nodes = xmldoc.getElementsByTagName("message"); 
		var n_messages = message_nodes.length;
		if(n_messages > 0)
		{
		    alert("Success");
		}
		
		//mTimerPing = setTimeout('startChatPing();',5000); //Refresh our chat in 5 seconds
    }
}
