// *********************** API AJAX - functions *********************** 
// *** Testfunction

doAutoPolling=false;

function startChatAutoPolling()
{
		doAutoPolling=true;
		setInterval(chatAutoPoller,3000);
}

function stopChatAutoPolling()
{
		doAutoPolling=false;
}


function chatAutoPoller()
{
	if (doAutoPolling)
	{
		pollNewMSGs();
	}
}

// ***********************************   Poll new MSGs
function pollNewMSGs()
{
		ajaxChatObj= new ajaxChatComm();
	
		urlParam="pollNewMSGs";
		ajaxChatObj.sendAndLoadChat(urlParam,onReceiveNewMSGs,null);
		// CallBackfunction is always called with (responseText,userDefObj) !!!
}


// Call Backs Poll new MSGs
function onReceiveNewMSGs(responseText,userData)
{
	//alert(responseText);
	chatNewMSGsWindowID=responseText;
	
	
	
	winManager=new chatWindowManager();
	winManager.init("chatContainer");
	
	ajaxChatObj= new ajaxChatComm();
	
	if (chatNewMSGsWindowID!="")
	{
		// Split the string by ","
		newMSGWindowsKeys=chatNewMSGsWindowID.split(",");
		// foreach KeyVal
		for(x=0;x<newMSGWindowsKeys.length;x++ )
		{
			//alert(newMSGWindowsKeys[x]);
			// Splt KeyVal  BY ":"
			
			windowUserIdKeys=newMSGWindowsKeys[x].split(":");
			// LeftPart = window TagName, RightPart=userId
			if (windowUserIdKeys.length==2)
			{
				chatWindowId=windowUserIdKeys[0];
				userId=windowUserIdKeys[1];
				//alert("windowID:::" + chatWindowId + "---- userId:::" + userId);
				// Check if exists
				// NO -> create DIV Tag and load complete Window
				// YES -> refresh the MSGS Part
				
				//userId=106;
				//chatWindowId="chatWindow_1";
				if(!winManager.checkWindowExists(chatWindowId))
				{
					winManager.createNewChatWindow(chatWindowId,userId);
					// Load the whole chatWindow including the current MSGS
					ajaxChatObj.sendAndLoadChat("loadChatWindowHTML_windowId=" + chatWindowId + "_toUserId=" + userId,onReceiveMSGsWindow,chatWindowId);
				}
				else
				{
					// Load the MSGS only
					ajaxChatObj.sendAndLoadChat("loadNewMSGS_windowId=" + chatWindowId + "_toUserId=" + userId,onReceiveMSGsHTML,chatWindowId);
					// refresh content in wondow
				}
			}
			
		}
	}
}



function onReceiveMSGsWindow(responseText,windowId)
{
	winManager=new chatWindowManager();
	winManager.init("chatContainer");
	if (windowId!=null && windowId>0)
	{
		winManager.setChatWindowHTML(windowId,responseText);
	}
	
}

function onReceiveMSGsHTML(responseText,windowId)
{
	winManager=new chatWindowManager();
	winManager.init("chatContainer");
	
	winManager.refreshMSGs(windowId,responseText);
	
}


//***********************************   /Poll new MSGs






//***********************************   Close ChatWindow
function closeChatWindow(chatWindowId)
{
	ajaxChatObj= new ajaxChatComm();

	urlParam="closeChatWindow_windowId=" + chatWindowId;
	ajaxChatObj.sendAndLoadChat(urlParam,onReceiveCloseWindow,chatWindowId);
	// CallBackfunction is always called with (responseText,userDefObj) !!!

}

// Call Backs Close ChatWindow
function onReceiveCloseWindow(responseText,windowId)
{
	winManager=new chatWindowManager();
	winManager.init("chatContainer");
	
	winManager.removeChatWindow(windowId,1);
	
	//alert(responseText);
}
//***********************************   /Close ChatWindow




//*********************************** sendMSG
function sendMSG(windowId,userId,msg)
{
	ajaxChatObj= new ajaxChatComm();
	
	
	// Grab the MSG from input Element
	msgElement=document.getElementById("myMSG_" + windowId);
	if (msgElement)
	{
		msg=msgElement.value;
		if (msg!="")
		{
			msgElement.value="";
			// Escape the Message slashes
			
//			while(msg.search("/") != -1) 
//			{
//				msg=msg.replace("/","SSS");
//			}
//			while(msg.search("=") != -1) 
//			{
//				msg=msg.replace("=","III");
//			}
//			while(msg.search("_") != -1) 
//			{
//				msg=msg.replace("_","UUU");
//			}
			
			
			urlParam="sendMSG_windowId=" + escape(windowId) + "_toUserId=" + escape(userId) + "_msg=" + escape(encodeURLParam(msg));
			ajaxChatObj.sendAndLoadChat(urlParam,onReceiveSendMSG,windowId);
		}
		// CallBackfunction is always called with (responseText,userDefObj) !!!
	}
	else
	{
		alert("TextBox not found");
	}
	
}






//Call Backs Close ChatWindow
function onReceiveSendMSG(responseText,windowId)
{
	winManager=new chatWindowManager();
	winManager.init("chatContainer");
	
	winManager.refreshMSGs(windowId,responseText);
}

//*********************************** /sendMSG


//***********************************   create new window
function createNewWindow(userId)
{
	ajaxChatObj= new ajaxChatComm();

	urlParam="createNewWindow_toUserId=" + userId;
	ajaxChatObj.sendAndLoadChat(urlParam,onReceiveNewWindow,userId);
	// CallBackfunction is always called with (responseText,userDefObj) !!!

}


// Call Backs Poll new MSGs
function onReceiveNewWindow(responseText,userData)
{
	//alert(responseText);
	
	winManager=new chatWindowManager();
	winManager.init("chatContainer");
	
	userId=userData;
	chatWindowId=responseText;
	if(!winManager.checkWindowExists(chatWindowId))
	{
		if (chatWindowId!=null && chatWindowId>0)
			{
		winManager.createNewChatWindow(chatWindowId,userId);
		ajaxChatObj.sendAndLoadChat("loadChatWindowHTML_windowId=" + chatWindowId + "_toUserId=" + userId,onReceiveMSGsWindow,chatWindowId);
			}

	}
	
}



function onReceiveMSGsWindow(responseText,windowId)
{
	winManager=new chatWindowManager();
	winManager.init("chatContainer");
	
	winManager.setChatWindowHTML(windowId,responseText);
	
}

//***********************************   /create new window


//***********************************   closeAll Windows


function closeAllWindows()
{
	
	// remove all windows
	winManager=new chatWindowManager();
	winManager.init("chatContainer");	
	winManager.closeAllWindows();
	
	// Also close windows on Server
	
	ajaxChatObj= new ajaxChatComm();

	urlParam="closeAllWindows";
	ajaxChatObj.sendAndLoadChat(urlParam,onReceiveCloseAllWindows,null);
	// CallBackfunction is always called with (responseText,userDefObj) !!!

}

function onReceiveCloseAllWindows(responseText,userdata)
{
}
//***********************************   /closeAll Windows


//***********************************   /openFriends Windows
function openFriendsWindow()
{
	ajaxChatObj= new ajaxChatComm();

	urlParam="loadFriends";
	ajaxChatObj.sendAndLoadChat(urlParam,onReceiveOpenFriendsWindow,null);
}

function onReceiveOpenFriendsWindow(responseText,userdata)
{
	//alert(responseText);
	winManager=new chatWindowManager();
	winManager.init("chatContainer");
	
	windowTagId="chatFriendsWindow";
	if (!winManager.checkWindowExists(windowTagId))	winManager.createNewChatWindow(windowTagId,0);
	
	winManager.setChatWindowHTML(windowTagId,responseText);
	
}
