var fromUserId;
var toUserId;
this.fromUserId = fromUserId;
this.toUserId = toUserId;

function openChatWindow(username,fromUserId,toUserId){
	this.fromUserId = fromUserId;
	this.toUserId = toUserId;
	
	createWindow(username,fromUserId,toUserId,0);
	var userChatWindowId = document.getElementById("chatwindow_" + this.toUserId);
	if(userChatWindowId.style.visibility == "hidden"){
		winInit();
		if (winList["chatwindow_" + toUserId]) winList["chatwindow_" + toUserId].open(); return false;
	}
	
}

function doSend(fromUserId,toUserId){
	//alert('called in');
	var toUserId	=	(toUserId?toUserId:0);
	var fromUserId	=	(fromUserId?fromUserId:0);
	var chatText 	=	(document.getElementById("chatText" + toUserId)?document.getElementById("chatText" + toUserId).value:document.getElementById("chatText" + fromUserId).value);

	if(chatText.length > 0 && toUserId > 0){
		ajax.sendrequest("POST","mirrorOneUp.php?reqFile=ajax/save.php", { chatText : chatText , toUserId:toUserId,fromUserId:fromUserId}, "isDoSend", "");
		if(document.getElementById("chatText" + toUserId)){
			document.getElementById("chatText" + toUserId).value = "";
		}else{
			document.getElementById("chatText" + fromUserId).value = "";
		}
	}
	
}

function isDoSend(msg){
	if(msg.success == 1){
		//alert('in cond is do send');
		//doDisplayMessages(msg.fromUserId,msg.toUserId);
		doDisplayMessages(msg.toUserId,msg.fromUserId);
	}
}

function doDisplayMessages(fromUserId,toUserId){
	var toUserId = (toUserId?toUserId:0);
	var fromUserId = (fromUserId?fromUserId:0);

	var userChatWindowId = (document.getElementById("userChatWindow" + toUserId)?document.getElementById("userChatWindow" + toUserId):document.getElementById("userChatWindow" + fromUserId));

	if(userChatWindowId){
		ajax.sendrequest("GET","mirrorOneUp.php?reqFile=ajax/displayMessages.php", {toUserId:toUserId,fromUserId:fromUserId}, "",userChatWindowId);
		return 1;
	}
}

function chat_CheckForOnLinkMessage(){
	var flag = 1;
	ajax.sendrequest("POST","mirrorOneUp.php?reqFile=ajax/checkMessageSend.php", {flag:flag}, "isChat_CheckForOnLinkMessage", "");
	
	var userChatWindowId = (document.getElementById("userChatWindow" + this.toUserId)?document.getElementById("userChatWindow" + this.toUserId):document.getElementById("userChatWindow" + this.fromUserId));

	if(userChatWindowId){
		var objDiv = userChatWindowId;
		objDiv.scrollTop = objDiv.scrollHeight;	
	}
}

function isChat_CheckForOnLinkMessage(msg){
	if(msg.to_user_id && msg.from_user_id && msg.success == 1 && !document.getElementById("chatwindow_" + msg.from_user_id)){
		createWindow(msg.from_user_name,msg.from_user_id,msg.to_user_id,1);
		winInit();
		
		var userChatWindowId = document.getElementById("chatwindow_" + msg.from_user_id);
		if(userChatWindowId){
			if(userChatWindowId.style.visibility == "hidden" && msg.success == 1){
				///sound
				if (winList["chatwindow_" + msg.from_user_id]) winList["chatwindow_" + msg.from_user_id].open();
				doDisplayMessages(msg.from_user_id,msg.to_user_id);	
			}
		}
	}else{
		doDisplayMessages(msg.from_user_id,msg.to_user_id);
	}
	
	this.fromUserId = (msg.from_user_id?msg.from_user_id:this.fromUserId);
	this.toUserId = (msg.toUserId?msg.toUserId:this.toUserId);
	
	window.setTimeout(chat_CheckForOnLinkMessage, 1000, true);
}

