// JavaScript Document
var divid="";
function CreateXmlHttp()
{
    //Creating object of XMLHTTP in IE
    try
    {
        XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
            XmlHttp = null;
        }
    }
    //Creating object of XMLHTTP in Mozilla and Safari
    if(!XmlHttp && typeof XMLHttpRequest != "undefined")
    {
        XmlHttp = new XMLHttpRequest();
    }
}
//
//function HandleResponse()
//{	
//    if(XmlHttp.readyState == 4)
//    {  
//        if(XmlHttp.status == 200)        {  
//			
//            document.getElementById(divid).innerHTML=XmlHttp.responseText
//        }
//        else
//        {
//            alert("There was a problem retrieving data from the server." );
//        }
//    }
//}
//function HandleResponse2()
//{	
//    if(XmlHttp.readyState == 4)
//    {
//        if(XmlHttp.status == 200)
//        {  //alert(divid);
//            document.getElementById(divid).innerHTML=XmlHttp.responseText
//        }
//        else
//        {
//            alert("There was a problem retrieving data from the server." );
//        }
//    }
//}

//@@@@@@@@@@@@@@@@@@@@@@@@		FOR CHAT APPLICATION		@@@@@@@@@@@@@@@@@@@@@@@@@@@
var xmlHttp = null;
//var timeLimit = 2;
//var x = timeLimit;
//var y = 1;

function createXmlHttp(){
	try {
		xmlHttp=new XMLHttpRequest();
 	}catch (e) {
 		try{
  			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  		}catch (e)  {
  			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
 	}
	return xmlHttp;
}

function reqMKTime(sesId){
	createXmlHttp();
	if(xmlHttp){
		var url = "ajax.php?act=getOnlineUsers";
		//xmlHttp.onreadystatechange=getAjaxTimeStamp 
		xmlHttp.onreadystatechange= function (){
			if(xmlHttp.readyState == 4){
				document.getElementById('unixTimeStamp').innerHTML = xmlHttp.responseText;
				//alert(sesId);
				checkNewMsgs2(sesId);
				//checkNewMsgs();
				//window.runtime.flash.display.maximize();
				//window.focus();
			}
		}
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
	
}
function checkNewMsgs2(sesId){
	
	createXmlHttp();
	if(xmlHttp && sesId!= ""){
		//alert(sesId);	
	//if(xmlHttp){
		var url = "ajax.php?act=getNewMsgs2";
		//alert(url);
		xmlHttp.onreadystatechange= function (){
			if(xmlHttp.readyState == 4){//
				newMsgs = xmlHttp.responseText;
				document.getElementById('unixTimeStamp2').innerHTML = newMsgs;
				//alert(newMsgs);
				var newMsgs1 = new Array();
				newMsgs1 = newMsgs.split("#_#");
				for(i=0;i<newMsgs1.length;i++){
					if(newMsgs1[i] != ""){
						//alert(newMsgs1[i]);
						newwindow=window.open('chat.php?chatId='+newMsgs1[i], newMsgs1[i],'location=0,status=0,scrollbars=0,width=500,height=535,menubar=0,titlebar=no,resizable=0,toolbar=0');
						//newwindow.focus();
					}
				}
			}
		}
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
}
function getChatHistory(sesId){
	createXmlHttp();
	if(xmlHttp){
		var url = "ajax.php?act=getChatHistory&randSession="+sesId;
		xmlHttp.onreadystatechange= function (){
			if(xmlHttp.readyState == 4){
				chatHistoty =  xmlHttp.responseText;
				if(chatHistoty.lastIndexOf('#@@@_@@@#')>=0){
					arr_chat = chatHistoty.split('#@@@_@@@#');
				}
				document.getElementById('mainDiv').innerHTML = arr_chat[0]+'<br><br><a id="showLink" href="javascript:void()"></a>';
				document.getElementById('UserStat').innerHTML = arr_chat[1];
				document.getElementById('loginStat').value = arr_chat[2];
				
				//document.getElementById('mainDiv').innerHTML = chatHistoty+'<br><br><a id="showLink" href="javascript:void()"></a>';
				//document.getElementById('mainDiv').innerHTML = xmlHttp.responseText+'<br><br><a id="showLink" href="javascript:void()"></a>';
				//document.getElementById('showLink').focus();
				//document.getElementById('txtChat').focus();
				
				//window.focus();
			}else{
				//document.getElementById('mainDiv').innerHTML = tableData;	
			}
		}
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
	//checkOnlineUser(sesId);
	
}
function checkOnlineUser(sesId){
	createXmlHttp();
	if(xmlHttp){
		var url = "ajax.php?act=getToUser&randSession="+sesId;
		xmlHttp.onreadystatechange= function (){
			if(xmlHttp.readyState == 4){
				document.getElementById('UserStat').innerHTML = xmlHttp.responseText;
			}
		}
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
}
function getAjaxTimeStamp(){
	if(xmlHttp.readyState == 4){
		document.getElementById('unixTimeStamp').innerHTML = xmlHttp.responseText;
	}
}
function enableSession(act,sesId)
{
	x = x-y;
	//alert("act: "+act+"\nsesId: "+sesId+"\nx: "+x);
    setTimeout("enableSession('"+act+"','"+sesId+"')", 1000);
	document.getElementById("clockTime").innerHTML = x;
	if(x==0){
		reqMKTime(sesId);	
//		if(act == 'getChatHistory'){
//			getChatHistory(sesId);
//		}else{
//			if(act == 'getLoginStatus'){
//				reqMKTime();
//			}
////			var newMsgs1 = act.split("#_#");
////			//alert(newMsgs1[0]+"_"+newMsgs1[1]);
////			if(newMsgs1[0] == 'getLoginStatus'){
////				reqMKTime();
////			}
////			if(newMsgs1[1] == 'getNewMsgs'){
////				//checkNewMsgs();
////			}
//		}
		x=timeLimit;
	}
}
function enableSession1(act,sesId){
	x = x-y;
//	data = document.getElementById('mainDiv').innerHTML+'<br><br><a id="showLink" href="javascript:void()"></a>';
//		document.getElementById('showLink').focus();
//		document.getElementById('txtChat').focus();
   setTimeout("enableSession1('"+act+"','"+sesId+"')", 1000);
	document.getElementById("clockTime").innerHTML = x;
	if(x==0){
		//alert(sesId);
		getChatHistory(sesId);
		//checkOnlineUser(sesId);
		x=timeLimit;
	}
}
function checkOnlineUser_clock(sesId){
	x = x-y;
   setTimeout("checkOnlineUser_clock('"+sesId+"')", 1000);
	//document.getElementById("clockTime").innerHTML = x;
	if(x==0){
		checkOnlineUser(sesId);
		x=timeLimit;
	}
}
function checkNewMsgs(){
	//alert(xmlHttp);
	createXmlHttp();
	if(xmlHttp){
		var url = "ajax.php?act=getNewMsgs";
		xmlHttp.onreadystatechange= function (){
			if(xmlHttp.readyState == 4){
				newMsgs = xmlHttp.responseText;
				if(newMsgs != ""){
					var newMsgs1 = new Array();
					newMsgs1 = newMsgs.split("#_#");
					for(i=0;i<newMsgs1.length;i++){
						//alert(newMsgs1[i]);
						var newwindow = '';
						if(newMsgs1[i] != ""){
							//alert("aasdfadsfads");
							newwindow = newMsgs1[i];
							if (!newwindow.closed && newwindow.location) {
								newwindow.focus();
								alert(newwindow + "opend");
								//newwindow.location.href = url;
							}
							else {
								newwindow=window.open('chat.php?chatId='+newMsgs1[i], newMsgs1[i],'location=0,status=0,scrollbars=0,width=500,height=520,menubar=0,titlebar=no,resizable=0,toolbar=0');
								if (!newwindow.opener) newwindow.opener = self;
							}
							if (window.focus) {newwindow.focus()}
							
							//window.open ('chat.php?chatId='+newMsgs1[i], newMsgs1[i],'location=0,status=0,scrollbars=0,width=500,height=520,menubar=0,titlebar=no,resizable=0,toolbar=0');
						}
					}
					document.getElementById('unixTimeStamp2').innerHTML = newMsgs;
				}
			}
		}
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
}
function getAction(chatId){
	if(document.getElementById('loginStat').value == 'Y'){
		Chat = document.getElementById('txtChat').value;
		chatId1 = chatId;
		if(Chat!=""){
			divid="unixTimeStamp";
			var resquestUrl="ajax.php"; 
			dispName = document.frmTemp.dispName.value;
			createXmlHttp();
			if(xmlHttp){
				xmlHttp.onreadystatechange = HandleResponse;
				xmlHttp.open("POST", resquestUrl,  true);
				xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
				xmlHttp.send('Chat='+Chat+'&dispName='+dispName+'&chatId='+chatId);                   
				//xmlHttp.send('dispName='+dispName);                   
			}
			document.getElementById('txtChat').value = "";
		}
	}
}
function HandleResponse(){
	//alert(xmlHttp.readyState);
	if(xmlHttp.readyState == 4){
		//alert(chatId1);
		//window.open ('chat.php?chatId='+chatId1,chatId1,'location=0,status=0,scrollbars=0,width=500,height=500,menubar=0,titlebar=no,resizable=0,toolbar=0');
		document.getElementById('mainDiv').innerHTML = xmlHttp.responseText+'<br><br><a id="showLink" href="javascript:void()"></a>';
		document.getElementById('showLink').focus();
		document.getElementById('txtChat').focus();
	}
}
function forButton(){
	Chat = document.getElementById('txtChat').value;
	//alert(Chat);
	getAction();
}
