﻿//******************************************************************
//单元内容: Ajax 客户端脚本
//修改日期: 2006-01-03
//作者:     李 焕然
//单元说明: 这是实现 Ajax 中 Client 临时用户所有的接口方法
//******************************************************************

var ToUsername = "";
var ToNickname = "";
var ToMobile   = "";

//播放消息声音
function doPlayMessage() {
	var wmp	= document.getElementById("WMP");
	
	try {
		wmp.FileName = "Sound/Msg.wav";
	} catch (ex) {
		wmp.url = "Sound/Msg.wav";
	}
}

//播放上线声音
function doPlayGlobal() {
	var wmp	= document.getElementById("WMP");
	
	try {
		wmp.FileName = "Sound/Global.wav";
	} catch (ex) {
		wmp.url = "Sound/Global.wav";
	}
}

//设置焦点
function focusInFrame() {
	document.frames["FCKeditorIn___Frame"].focus();
}

//发送消息
function sendMessage() {

	if ((ToUsername == "") && (ToNickname == "")) {
		window.alert(str_SelectUser_alert);
		return false;
	} 
	
	if ((document.getElementById("EMAIL_DIV").style.display == "") && ((document.getElementById("Email").value == "")||(document.getElementById("Email").value == str_Email_Input)) ) {
		window.alert(str_Email_Select);
		return false;
	}

	var oEditor = FCKeditorAPI.GetInstance('FCKeditorIn');
	var ContentHTML = oEditor.GetXHTML(true); 
	var ContentText = oEditor.GetXHTML(false);
	
	if (ContentText != "") {
		var Command = "[MESSAGE]";
		var Content = ContentHTML;
		
		document.frames["FCKeditorIn___Frame"].document.frames["eEditorArea"].document.body.innerHTML = "";
		document.frames["FCKeditorIn___Frame"].focus();

		//显示内容
		//如果离线, 显示了电子邮件框
		if (document.getElementById("EMAIL_DIV").style.display == "") {
			var inputEmail	= document.getElementById("Email");
			var Email = inputEmail.value;

			document.frames["Out_Frame"].document.body.innerHTML += "<font color=#339966>" + Nickname + "(" + Email + ")" + " " + getNowTime() + "</font><br>";
		} else {
			document.frames["Out_Frame"].document.body.innerHTML += "<font color=#339966>" + Nickname + " " + getNowTime() + "</font><br>";
		}

		//document.frames["Out_Frame"].document.body.innerHTML += "<font color=#339966>" + Nickname + " " + getNowTime() + "</font><br>";
		document.frames["Out_Frame"].document.body.innerHTML += "<div class='Message'>" + Content + "</div>";
		document.frames["Out_Frame"].document.body.scrollTop = document.frames["Out_Frame"].document.body.scrollHeight;	

		//显示内容
		//如果离线, 显示了电子邮件框
		if (document.getElementById("EMAIL_DIV").style.display == "") {
			var inputEmail	= document.getElementById("Email");
			var Email = inputEmail.value;

			LHRSoft.WebModules.LiveICQ.Ghost.AjaxObject.SendMessage(Username, Nickname + "(" + Email + ")", ToUsername, ToNickname, Command, Content, callback_sendMessage);
		} else {
			LHRSoft.WebModules.LiveICQ.Ghost.AjaxObject.SendMessage(Username, Nickname, ToUsername, ToNickname, Command, Content, callback_sendMessage);
		}
	};
}

function callback_sendMessage(res) {
	if (res.value == 0) {
	}
}

//请求对话
function Connection(AUserId, AUsername, ANickname, AMobile, AState, ASex, AMode) {
	
	ToUserId = AUserId;
	ToUsername = AUsername;
	ToNickname = ANickname;
	ToMobile   = AMobile;
					
	var Command = "[CONNECTION]";	
	var Content = "";
	LHRSoft.WebModules.LiveICQ.Ghost.AjaxObject.SendMessage(Username, Nickname, ToUsername, ToNickname, Command, Content, null);
			
	var tu	= document.getElementById("tableUsers");
	tu.style.display = "none";
	var of	= document.getElementById("Out_Frame");
	of.style.display = "";
			
	var rf	= document.frames["Read_Frame"];
	rf.readMessage();
			
	focusInFrame();
	
	
	//如果离线, 就提示用户不会马上回复		
	if (AState == "0") {
		document.frames["Out_Frame"].document.body.innerHTML += "<div><img src='images/Hint.png'>&nbsp;<font color=green>" + ANickname + str_Hint_Offline + "</font></div><br>";
		document.frames["Out_Frame"].document.body.scrollTop = document.frames["Out_Frame"].document.body.scrollHeight;	
		
		document.getElementById("EMAIL_DIV").style.display = "";
	} else if (AState == "1") {
		document.frames["Out_Frame"].document.body.innerHTML += "<div><img src='images/Hint.png'>&nbsp;<font color=green>" + ANickname + str_Hint_Online + "</font></div><br>";
		document.frames["Out_Frame"].document.body.scrollTop = document.frames["Out_Frame"].document.body.scrollHeight;	
		
		//document.getElementById("PHONE_DIV").style.display = "";
		if (AMode == "0") { document.getElementById("btnSendVideo").style.display = "" };
	} else if (AState == "2") {
		document.frames["Out_Frame"].document.body.innerHTML += "<div><img src='images/Hint.png'>&nbsp;<font color=green>" + ANickname + str_Hint_Dnd + "</font></div><br>";
		document.frames["Out_Frame"].document.body.scrollTop = document.frames["Out_Frame"].document.body.scrollHeight;	
		
		if (AMode == "0") { document.getElementById("btnSendVideo").style.display = "" };
	}
	var Img	= document.getElementById("Img");
	Img.src = "images/" + ASex + AState + ".jpg";
	Img.style.display = "";
/*	
	if (AMode == "0") {
	
	} else if (AMode == "1") {	//电话模式 则将输入和其他什么的隐藏掉
		document.getElementById("trEditIn").style.display = "none";
		document.getElementById("btnSend").style.display = "none";
		document.frames["Out_Frame"].document.body.innerHTML += "<div><img src='images/Hint.png'>&nbsp;<font color=red>" + str_Phone_Select + "</font></div><br>";
		document.frames["Out_Frame"].document.body.scrollTop = document.frames["Out_Frame"].document.body.scrollHeight;	
	}
*/
	// 查询欢迎语
	var res = LHRSoft.WebModules.LiveICQ.Ghost.AjaxObject.ReadGreetings(ToUsername);
	
	try 
	{	
		if(res != null && res.value != null && res.value.Tables != 0 && res.value.Tables.length == 1) 
		{
			for(var i=0; i<res.value.Tables[0].Rows.length; i++) 
			{
				document.frames["Out_Frame"].document.body.innerHTML += "<font color=#3366ff>" + ToNickname + " " + getNowTime() + "</font><br>";
				document.frames["Out_Frame"].document.body.innerHTML +="<div class='Message'>" + res.value.Tables[0].Rows[i].Text + "</div>";
				document.frames["Out_Frame"].document.body.scrollTop = document.frames["Out_Frame"].document.body.scrollHeight;
							
				doc.getElementById("State").innerText = stringMessages;
				//播放声音提示
				doPlayMessage();
			}	
		}
	} catch (ex) {

	}	
}

// 欢迎语
function callback_readGreeting(res) 
{			
	try 
	{	
		if(res != null && res.value != null && res.value.Tables != 0 && res.value.Tables.length == 1) 
		{
			for(var i=0; i<res.value.Tables[0].Rows.length; i++) 
			{
				document.frames["Out_Frame"].document.body.innerHTML += "<font color=#3366ff>" + ToNickname + " " + getNowTime() + "</font><br>";
				//document.frames["Out_Frame"].document.body.innerHTML +="<div class='Message'>" + res.value.Tables[0].Rows[i].text + "</div>";
				document.frames["Out_Frame"].document.body.scrollTop = document.frames["Out_Frame"].document.body.scrollHeight;
							
				doc.getElementById("State").innerText = stringMessages;
				//播放声音提示
				doPlayMessage();
			}	
		}
	} catch (ex) {

	}
}

//关闭对话
function DisConnection() {
	if ((ToUsername == "") && (ToNickname == "")) {
		return false;
	} 
	
	var Command = "[DISCONNECTION]";	//断开连接的命令
	var Content = "";
	var res = LHRSoft.WebModules.LiveICQ.Ghost.AjaxObject.SendMessage(Username, Nickname, ToUsername, ToNickname, Command, Content);
}

//初始化
function Init() {	
	document.frames["Out_Frame"].document.write("<html><head><Style> body { font-family: Verdana, Arial; font-size: 12px; padding: 5px 5px 5px 5px; margin: 0px; border-style: none; background-color: #ffffff; } .Message { padding-left: 16px; }</Style></head><body></body></html>")
	document.frames["Out_Frame"].document.close();
	
	//显示欢迎语
	// document.frames["Out_Frame"].document.body.innerHTML += "<div><img src='images/Hint.png'>&nbsp;<font color=green>" + str_Hint + "</font></div>";
	document.frames["Out_Frame"].document.body.innerHTML += "<div><img src='images/Hint.png'>&nbsp;<font color=green>" + str_Hint_Userid + "</font><font color=red>" + Username + ".</font></div>";
	document.frames["Out_Frame"].document.body.scrollTop = document.frames["Out_Frame"].document.body.scrollHeight;	
}

//电话处理函数
function callMe() {	
	var inputPhone	= document.getElementById("Phone");
	var Phone = inputPhone.value;
	
	if (Phone == "") {	
		window.alert(str_Phone_Select);
		return false;
	};	
	//第一个是主叫号码
	var res = LHRSoft.WebModules.LiveICQ.Ghost.AjaxObject.CallMe(AccountId, ToMobile, Phone, Username, Nickname, ToUsername, ToNickname);
	//显示内容
	if (res.value == 0) {
		//显示内容
		document.frames["Out_Frame"].document.body.innerHTML += "<img src='images/phone.gif'>&nbsp;<font color=Red>" + str_Phone_Black + "</font><br>";
		document.frames["Out_Frame"].document.body.scrollTop = document.frames["Out_Frame"].document.body.scrollHeight;
	} else {
		//显示内容
		document.frames["Out_Frame"].document.body.innerHTML += "<img src='images/phone.gif'>&nbsp;<font color=Red>" + str_Phone_Called + "</font><br>";
		document.frames["Out_Frame"].document.body.scrollTop = document.frames["Out_Frame"].document.body.scrollHeight;
	}

}
