
//消息源用户的用户名
var	sourceUser;
//消息目的用户的用户名
var	destinationUser;


var sourceUrl=null;

//窗口是否拥有“焦点”
var isFocus=true;

var isInputBoxFocus=false;
var isInputBoxFocus2=false;

//布尔型变量，用于说明当前好友列表是否可见
var isBoddyListVisible = 0;
//布尔型变量，用于说明当前对话窗口是否可见
//var isChatVisible =0;

//新消息的ID初始值，即在每次收消息的时候，仅接收消息ID大于此数值的消息
var latestMsgId= 0;

//布尔型变量，用于说明是否已经向服务器发出消息请求
var requesting = 0;

//布尔型变量，用于说明是否已经向服务器查询用户列表
var newClientRequesting = 0;


//用于保存客服界面中4个窗口的消息目的用户
var sourceClientName1="NULL";
var sourceClientName2="NULL";
var sourceClientName3="NULL";
var sourceClientName4="NULL";

//向服务器查询新消息的时间间隔
var MESSAGE_REQ_TIMER=1500;

//向服务器查询用户列表的时间间隔
var CLIENT_LIST_REQ_TIMER=2000;

//向服务器发送Keepalive消息的时间间隔
var KEEPALIVE_MSG_TIMER=4000;


var sourceStatus=1;


var defaultSendMsgBkColor = "#FFFFFF";
var defaultReceiveMsgBkColor = "#C2D8F0";



var statusBk = new Array("#CCCCCC","#333333","#3366FF","#FFFFFF");
var statusPicBk = new Array("image\\head00.gif","image\\head01.gif","image\\head02.gif","image\\head03.gif");

var oClientList=new Array;
var tClientList=new Array;

//var lengthWaiting=0;

//用户临时存储用户名
//var tempUserName;

// 初始化一个xmlhttp对象
function initAjax()
{
	var ajax = false;

	try
	{
		ajax = new ActiveXObject (Msxml2.XMLHTTP);
	}
	catch (e)
	{
		try
		{
			ajax = new ActiveXObject ("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			ajax = false;
		}
	}

	if (!ajax && typeof XMLHttpRequest != 'undefined')
	{
		ajax = new XMLHttpRequest ();
	}

	return ajax;

}

var initAjax_getMessage_flag = 0;
var initAjax_getClientMessage_flag = 0;
var initAjax_submitMsg_flag = 0;
var initAjax_scoreUser_flag = 0;
var initAjax_sentMail_flag = 0;
var initAjax_getCustomerName_flag = 0;
var initAjax_submitLogin_flag = 0;
var initAjax_adminSubmitLogin_flag = 0;
var initAjax_oamSubmitLogin_flag = 0; // oam
var initAjax_sendAjaxRequest_flag = 0;

var ajax_getMessage = 0;
var ajax_getClientMessage = 0;
var ajax_submitMsg = 0;
var ajax_getCustomerName = 0;
var ajax_submitLogin = 0;
var ajax_adminSubmitLogin = 0;
var ajax_oamSubmitLogin = 0; // oam
var ajax_sendAjaxRequest = 0;

function initAjax_getMessage()
{
	if (initAjax_getMessage_flag)
		return ajax_getMessage;
	else{
		ajax_getMessage = initAjax();
		initAjax_getMessage_flag = 1;
		return ajax_getMessage;
	}
}

function initAjax_getClientMessage()
{
	if (initAjax_getClientMessage_flag)
		return ajax_getClientMessage;
	else{
		ajax_getClientMessage = initAjax();
		initAjax_getClientMessage_flag = 1;
		return ajax_getClientMessage;
	}
}

function initAjax_submitMsg()
{
	if (initAjax_submitMsg_flag)
		return ajax_submitMsg;
	else{
		ajax_submitMsg = initAjax();
		initAjax_submitMsg_flag = 1;
		return ajax_submitMsg;
	}
}

function initAjax_scoreUser()
{
	if (initAjax_scoreUser_flag)
		return ajax_scoreUser;
	else{
		ajax_scoreUser = initAjax();
		initAjax_scoreUser_flag = 1;
		return ajax_scoreUser;
	}
}

function initAjax_sentMail()
{
	if (initAjax_sentMail_flag)
		return ajax_sentMail;
	else{
		ajax_sentMail = initAjax();
		initAjax_sentMail_flag = 1;
		return ajax_SentMail;
	}
}


function initAjax_getCustomerName()
{
	if (initAjax_getCustomerName_flag)
		return ajax_getCustomerName;
	else{
		ajax_getCustomerName = initAjax();
		initAjax_getCustomerName_flag = 1;
		return ajax_getCustomerName;
	}
}

function initAjax_submitLogin()
{
	if (initAjax_submitLogin_flag)
		return ajax_submitLogin;
	else{
		ajax_submitLogin = initAjax();
		initAjax_submitLogin_flag = 1;
		return ajax_submitLogin;
	}
}

function initAjax_adminSubmitLogin()
{
	if (initAjax_adminSubmitLogin_flag)
		return ajax_adminSubmitLogin;
	else{
		ajax_adminSubmitLogin = initAjax();
		initAjax_adminSubmitLogin_flag = 1;
		return ajax_adminSubmitLogin;
	}
}

// oam
function initAjax_oamSubmitLogin()
{
	if (initAjax_oamSubmitLogin_flag)
		return ajax_oamSubmitLogin;
	else{
		ajax_oamSubmitLogin = initAjax();
		initAjax_oamSubmitLogin_flag = 1;
		return ajax_oamSubmitLogin;
	}
}

function initAjax_sendAjaxRequest()
{
	if (initAjax_sendAjaxRequest_flag)
		return ajax_sendAjaxRequest;
	else{
		ajax_sendAjaxRequest = initAjax();
		initAjax_sendAjaxRequest_flag = 1;
		return ajax_sendAjaxRequest;
	}
}

//周期性的向服务器请求新消息
//由于setInterval中定义的周期性执行的函数不能携带参数，所以通过全局变量（sourceUser、destinationUser）传递参数
function getMsg ( sourName, destName )
{
	sourceUser = sourName;
	destinationUser = destName;
	setInterval ("getMessage()",MESSAGE_REQ_TIMER);
}

//周期性的向服务器请求请求用户列表
//由于setInterval中定义的周期性执行的函数不能携带参数，所以通过全局变量（sourceUser）传递参数
function getClientMsg ( sourName )
{
	sourceUser = sourName;
	setInterval ("getClientMessage()",CLIENT_LIST_REQ_TIMER);
}


//向服务器（getmessagefromdb.php）请求新消息
function getMessage( )
{

//设置通过Ajax访问的URL
	var url = "getmessagefromdb.php?destUserName="+destinationUser+"&sourceUserName="+sourceUser+"&lastestMsgId="+latestMsgId;

//初始化Ajax对象
	var ajax;

//是否已经将“MSG_REQ”发送出去。如果为“1”，则已经发送出去；如果为“0”，则尚未发出


	if (requesting ==0)
	{

		ajax= initAjax_getMessage ();

		//使用Get方式进行请求
		ajax.open ("GET", url, true);

		//定义回调函数
		ajax.onReadyStateChange = function ()
		{
			requesting = 0;
			if (ajax.readyState == 4)
			{
				if (ajax.status == 200)
				{
					processMsg (ajax.responseXML);
				}
			}
		}

		ajax.send (null);
		requesting = 1;

	}

}

//向服务器（getclientmessagefromdb.php）请求用户列表
function getClientMessage( )
{

//设置通过Ajax访问的URL
	var url = "getclientmessagefromdb.php?destUserName="+sourceUser+"&sourceClientName1="+sourceClientName1+"&sourceClientName2="+sourceClientName2+"&sourceClientName3="+sourceClientName3+"&sourceClientName4="+sourceClientName4;

//	alert(url);
//初始化Ajax对象
	var ajax;

//是否已经将“MSG_REQ”发送出去。如果为“1”，则已经发送出去；如果为“0”，则尚未发出


	if (newClientRequesting ==0)
	{

		ajax= initAjax_getClientMessage ();

		//使用Get方式进行请求
		ajax.open ("GET", url, true);

		//定义回调函数
		ajax.onReadyStateChange = function ()
		{
			newClientRequesting = 0;
			if (ajax.readyState == 4)
			{
				if (ajax.status == 200)
				{
					processClientMsg (ajax.responseXML);

				}
			}
		}

		ajax.send (null);
		newClientRequesting = 1;

	}

}

//根据resCode，处理从服务器得到的消息
function processMsg (responseMsgXML)
{
	var resMsgXML=responseMsgXML.documentElement;
	if (resMsgXML!=null)
	{
		var msgNumber=resMsgXML.attributes[0].text;
		var resCode=resMsgXML.attributes[1].text;

		if (resCode == "200")
		{
			MM_controlSound('play','document.MM_soundController');
			for (var i=0; i<msgNumber; i++)
			{
				displayMsg(resMsgXML.childNodes[i]);
			}
			newMsgNotifier(true);
			//使窗口获得焦点
			var isParentFocus=getParentFocusStatus();
//			alert(isParentFocus);
			if (isParentFocus==false)
				{
					window.focus();
					setParentFocus();
				}
		}
		else if (resCode == "500")
		{
			displayString ("数据库连接失败！");
		}
		else if (resCode == "501")
		{
			displayString ("数据库查询操作失败！");
		}
	}

}

//清空客服界面中的用户列表
function clearTable(tableID)
{
	var tableHandle=document.getElementById(tableID);

	var rowNumber=tableHandle.rows.length-1;

	for (var i=0;i<rowNumber;i++)
	{
		tableHandle.deleteRow();
	}

}


//根据resCode，处理从服务器得到的用户列表
function processClientMsg (responseMsgXML)
{
	var resMsgXML=responseMsgXML.documentElement;
	if (resMsgXML!=null)
	{

		var resCode=resMsgXML.attributes[0].text;
		var tableHandle=document.getElementById("newClientList");

		if (resCode == "200")
		{
			var clientNum=responseMsgXML.getElementsByTagName("clientNumber").item(0).attributes[0].text;
			clearTable("newClientList");
/*
			//检查当前是否拥有焦点
			var isFocus=getFocusStatus();
//			alert(isFocus);
			if (isFocus==false)
				{
					window.focus();
					setFocus();
				}

*/
//			alert(clientNum);
			tClientList.length=0;
			var newtr;
			var newtd;
			for(var i=0; i<clientNum;i++)
			{
				newtr = tableHandle.insertRow ();
				newtd = newtr.insertCell ();
				var userName=resMsgXML.childNodes[i].attributes[0].text;
//				alert(resMsgXML.childNodes[i].attributes[0].text);
//				newtd.style.height="20 px";
//				newtd.style.background ='url("image\\newclientlistbk.gif")';
				newtd.style.height="25 px";
				newtd.style.background ='url("image\\assistantskin\\newclientlistcellbk.gif")';
				newtd.innerHTML="<a class=\"underlineText\" onclick=\"putIntoWorkspace('"+userName+"')\">"+userName+"</a>";
				tClientList[i]=userName;
			}
			if (isNewClient(oClientList,tClientList)==1)
			{
				if (getFocusStatus()==false)
				{
					window.focus();
					setFocus();
				}
				MM_controlSound('play','document.MM_soundController');
			}
			copyT2O(oClientList,tClientList);
		}
		else if (resCode == "404")
		{
			clearTable("newClientList");
		}
	}
}

//在客服界面中选择可用的窗口打开与destName标识的用户的会话，如果没有可用窗口则提示客服人员
function putIntoWorkspace(destName)
{
	if(	sourceClientName1=="NULL")
	{
		parent.frame11.location="myim.php?sourceName="+sourceUser+"&destinationName="+destName+"&extendedBar=0";
		sourceClientName1=destName;
		setTableTitle("dialog11","关闭与"+destName+"的会话");
	}
	else if(	sourceClientName2=="NULL")
	{
		parent.frame12.location="myim.php?sourceName="+sourceUser+"&destinationName="+destName+"&extendedBar=0";
		sourceClientName2=destName;
		setTableTitle("dialog12","关闭与"+destName+"的会话");
	}
	else if(	sourceClientName3=="NULL")
	{
		parent.frame21.location="myim.php?sourceName="+sourceUser+"&destinationName="+destName+"&extendedBar=0";
		sourceClientName3=destName;
		setTableTitle("dialog21","关闭与"+destName+"的会话");
	}
	else if(	sourceClientName4=="NULL")
	{
		parent.frame22.location="myim.php?sourceName="+sourceUser+"&destinationName="+destName+"&extendedBar=0";
		sourceClientName4=destName;
		setTableTitle("dialog22","关闭与"+destName+"的会话");
	}
	else
		alert("暂无可用对话窗口，请关闭某对话窗口后重试！");
}

var pageUrl = "";



function setDefaultReceiveMsgBkColor ( bkColor )
{
	defaultReceiveMsgBkColor = bkColor;
}

//在对话列表中显示收到的用户消息
function displayMsg ( displayElement )
{
//	alert(displayElement.attributes[4].text);
	switch (displayElement.attributes[4].text)
	{
		case "2":
		{
			var contents;
			var newtr;
			var newtd;
			contents = document.getElementById("contents");
			newtr = contents.insertRow ();
			newtd = newtr.insertCell ();
			var displayString;
			var timeString=getTimeString();
//			displayString = "<strong>"+ displayElement.attributes[1].text + " ("+ timeString +")</strong><BR>" + displayElement.text;
			displayString = displayElement.text;

			//以下代码是用于测试支持表情的代码
			displayString = processString(displayString);
			//以上代码是用于测试支持表情的代码


//			newtd.innerHTML="<div id=\"wrap\">"+displayString+"Success</div>";
			newtd.innerHTML=displayString;
			newtd.style.background = defaultReceiveMsgBkColor;
//			newtd.style.width = (eval(document.body.clientWidth)-10)+"px";

			if (displayElement.attributes[0].text>latestMsgId)
				latestMsgId = displayElement.attributes[0].text;
			document.all.textDisplay.scrollTop=document.all.textDisplay.scrollHeight;
			break;
		}
		case "3":
		{
//			var str_url = window.location.href;
			if (isBusinessCardVisible==false)
				toggleBusinessCard();
			if (displayElement.attributes[0].text>latestMsgId)
				latestMsgId = displayElement.attributes[0].text;
//			submitMsg(sourceUser,destinationUser,4);
			break;
		}
		case "4":
		{
			var contents;
			var newtr;
			var newtd;
			contents = document.getElementById("contents");
			newtr = contents.insertRow ();
			newtd = newtr.insertCell ();
			var displayString;
			displayString = "<strong>对方正在查看的网页为:</strong><BR><a href=\""+displayElement.text+"\" target=\"_blank\">" + displayElement.text+"</a>";
			newtd.innerHTML=displayString;
			newtd.style.background ="#C2D8F0";
			if (displayElement.attributes[0].text>latestMsgId)
				latestMsgId = displayElement.attributes[0].text;
			document.all.textDisplay.scrollTop=document.all.textDisplay.scrollHeight;
			break;
		}
		case "5":
		{
			var contents;
			var newtr;
			var newtd;
			contents = document.getElementById("contents");
			newtr = contents.insertRow ();
			newtd = newtr.insertCell ();
			var displayString;
			var timeString=getTimeString();
			displayString = destinationUser+"将与您的会话转向了"+displayElement.text+"，页面将在5秒钟后跳转...";
			newtd.innerHTML=displayString;
			newtd.style.background ="#C2D8F0";
			if (displayElement.attributes[0].text>latestMsgId)
				latestMsgId = displayElement.attributes[0].text;
			document.all.textDisplay.scrollTop=document.all.textDisplay.scrollHeight;
//			alert("OK");
//			pageUrl = "http://www.cocyber.com/im/myim.php?sourceName="+sourceUser+"&destinationName="+displayElement.text+"&extendedBar="+extendedBarStatus;
			pageUrl = "myim.php?sourceName="+sourceUser+"&destinationName="+displayElement.text+"&extendedBar="+extendedBarStatus;
			setTimeout("go2Page();",5000);
			//	alert(dUser+"将"+sUser+"转给您");
			var notifyString = "["+timeString+"] "+destinationUser+"将与"+sourceUser+"的会话转给您！";
//			alert(notifyString);
			silentSubmitMsg(sourceUser,displayElement.text,notifyString,2);
			break;
		}
		case "6":
		{
			var contents;
			var newtr;
			var newtd;
			contents = document.getElementById("contents");
			newtr = contents.insertRow ();
			newtd = newtr.insertCell ();
			var displayString;
			var timeString=getTimeString();
			displayString = "您收到了来自"+destinationUser+"的协同浏览请求。";
			newtd.innerHTML=displayString;
			newtd.style.background ="#C2D8F0";
			if (displayElement.attributes[0].text>latestMsgId)
				latestMsgId = displayElement.attributes[0].text;
			document.all.textDisplay.scrollTop=document.all.textDisplay.scrollHeight;
//			alert("OK");
			if(confirm(destinationUser+"请求与您协同浏览"+displayElement.text+"，同意请按“确定”，否则请“取消”！"))
			{
				//alert(displayElement.text);
				var popupWind=window.open(displayElement.text,"","resizable=yes, toolbar=yes, location=yes, directories=yes, status=yes, menubar=yes, scrollbars=yes" );
			    silentSubmitMsg(sourceUser,destinationUser,sourceUser+"已经同意了您发送的协同浏览"+displayElement.text+"的请求",2);
			}
			else
			{
			    silentSubmitMsg(sourceUser,destinationUser,sourceUser+"拒绝了您发送的协同浏览"+displayElement.text+"的请求",2);
			}

			break;
		}
		default:
		{
			alert("OK");
		}
	}

}

function go2Page()
{
	window.location.href= pageUrl;
}


function sendBusinessCard()
{
	toggleAdvToolBar();
	var funcMyMsg = document.getElementById("myMsgInput");
	funcMyMsg.value = "已向对方发送名片...";
	submitMsg(sourceUser,destinationUser,3);

}


function setDefaultSendMsgBkColor (bkColor)
{
	defaultSendMsgBkColor = bkColor;
}


//在对话列表中显示发送出去的消息
function displayString ( showString )
{
	var contents;
	var newtr;
	var newtd;
	contents = document.getElementById("contents");
	newtr = contents.insertRow ();
	newtd = newtr.insertCell ();
	newtd.style.background = defaultSendMsgBkColor;

	//以下代码是用于测试支持表情的代码
	showString = processString(showString);
	//以上代码是用于测试支持表情的代码

	newtd.innerHTML=showString;
	document.all.textDisplay.scrollTop=document.all.textDisplay.scrollHeight;

}


//在对话列表中显示发送出去的消息
function displayColorBkString ( showString, bkColor )
{
	var contents;
	var newtr;
	var newtd;
	contents = document.getElementById("contents");
	newtr = contents.insertRow ();
	newtd = newtr.insertCell ();
	newtd.style.background =bkColor;

	//以下代码是用于测试支持表情的代码
	showString = processString(showString);
	//以上代码是用于测试支持表情的代码

	newtd.innerHTML=showString;
	document.all.textDisplay.scrollTop=document.all.textDisplay.scrollHeight;

}


//发送Service ID为serId，源为sourName，目的为destName的消息
function submitMsg(sourName,destName,serId)
{

//设置通过Ajax访问的URL
	var myMsg = document.getElementById("myMsgInput");
	var timeString=getTimeString();

	if (myMsg.value!="")
	{
//		var sentMsg = sourName+" ["+timeString+"]: "+ myMsg.value;
		var sentMsg = "["+timeString+"] "+ sourName +": "+ myMsg.value;
//		var url = "sentmsgtodb.php?myMsg="+myMsg.value+"&msgSource="+sourName+"&msgDestination="+destName+"&msgServiceId="+serId;
		var url = "sentmsgtodb.php?myMsg="+sentMsg+"&msgSource="+sourName+"&msgDestination="+destName+"&msgServiceId="+serId;

	//初始化Ajax对象
		var ajax;
		ajax= initAjax_submitMsg ();

		//使用Get方式进行请求
		ajax.open ("GET", url, true);

		//定义回调函数
		ajax.onReadyStateChange = function ()
		{
			if (ajax.readyState == 4)
			{
				if (ajax.status == 200)
				{
					processMyMsg (ajax.responseXML);
				}
			}
		}

		ajax.send (null);
		if (serId!=4)
		{
			//displayString("<strong>"+sourName+" ("+timeString+")</strong><BR>"+myMsg.value);
			displayString(sentMsg);
		}
		myMsg.value ="";
		newMsgNotifier(false);

	}
	else
	{
		alert ("输入内容不能为空！");
	}
}

//处理消息发送的返回值
function processMyMsg (responseMsgXML)
{
	var resMsgXML=responseMsgXML.documentElement;
	//displayString(resMsgXML.childNodes[0].text);/////////
	if (resMsgXML!=null)
	{
		var resCode=resMsgXML.attributes[0].text;
		if (resCode == "203")
		{
			displayString("[对方不是处于在线状态，不一定能够立即收到您发送的消息！]");
		}
		if (resCode == "404")
		{
			displayString(resMsgXML.childNodes[0].text+"未能正确发送！");
		}
	}
}

//处理界面中的按键事件，目前仅处理“Ctrl+Enter”事件，目前仅支持IE浏览器
function keyDown(e)
{
//	if(ns4)
//	{
//	var nkey=e.which;
//	var iekey='现在是ns浏览器';
//	var realkey=String.fromCharCode(e.which);
//	}

//	if(ie4)
//	{
	var iekey=event.keyCode;
//	var nkey='现在是ie浏览器';
//	var realkey=String.fromCharCode(event.keyCode);
	if(event.keyCode==13 &&event.ctrlKey)
	{
		submitMsg(sourceUser,destinationUser,'2');
	}
}

//好友列表的显示与隐藏
function toggleBoddyList()
{
	var layer = document.getElementById("floatBoddyList");
	if ( 0 == isBoddyListVisible )
	{
		layer.style.overflow = "visible";
		isBoddyListVisible = 1;
	}
	else if (1 == isBoddyListVisible)
	{
		layer.style.overflow = "hidden";
		isBoddyListVisible = 0;
	}
}

//关闭对话窗口11
function closeDialog11()
{
	parent.frame11.location='blank.html';
	sourceClientName1="NULL";
	setTableTitle("dialog11","");
}

//关闭对话窗口12
function closeDialog12()
{
	parent.frame12.location='blank.html';
	sourceClientName2="NULL";
	setTableTitle("dialog12","");
}

//关闭对话窗口21
function closeDialog21()
{
	parent.frame21.location='blank.html';
	sourceClientName3="NULL";
	setTableTitle("dialog21","");
}

//关闭对话窗口22
function closeDialog22()
{
	parent.frame22.location='blank.html';
	sourceClientName4="NULL";
	setTableTitle("dialog22","");
}

//设置客服界面中与用户会话窗口的标题
function setTableTitle(tableHeadName,titleString)
{
	var tableHeadLine=document.getElementById(tableHeadName);
	tableHeadLine.innerText=titleString;

}

//实现客服界面中“搜索”功能
function sentSearchRequest()
{
	var wiki = document.getElementById("wiki");
	var baidu = document.getElementById("baidu");
	var google = document.getElementById("google");
	var keywords = document.getElementById("keyTopic");
	var searchstring;
	if(keywords.value=="")
		alert("请输入需要的搜索内容");
	else
	{
		if( wiki.checked==true )
		{
			searchstring ="../wiki/search.php?topic="+keywords.value+"&searchtype=false&searchtopic=true";
			window.open(searchstring, "_blank");
		}
		if( baidu.checked==true )
		{
			searchstring ="http://www.baidu.com/s?wd="+keywords.value+"&cl=3";
			window.open(searchstring, "_blank");
		}
		if( google.checked==true )
		{
			searchstring ="http://www.google.com/search?q="+encodeURI(keywords.value)+"&hl=zh-CN";
			window.open(searchstring, "_blank");
		}
	}
}
/*
function URLEncode(plaintext)
{
 // The Javascript escape and unescape functions do not correspond
 // with what browsers actually do...
 var SAFECHARS = "0123456789" +     // Numeric
     "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
     "abcdefghijklmnopqrstuvwxyz" +
     "-_.!~*'()";     // RFC2396 Mark characters
 var HEX = "0123456789ABCDEF";

 var encoded = "";
 for (var i = 0; i < plaintext.length; i++ ) {
  var ch = plaintext.charAt(i);
     if (ch == " ") {
      encoded += "+";    // x-www-urlencoded, rather than %20
  } else if (SAFECHARS.indexOf(ch) != -1) {
      encoded += ch;
  } else {
      var charCode = ch.charCodeAt(0);
   if (charCode > 255) {
       alert( "Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
              "(URL encoding only supports 8-bit characters.)\n" +
        "A space (+) will be substituted." );
    encoded += "+";
   } else {
    encoded += "%";
    encoded += HEX.charAt((charCode >> 4) & 0xF);
    encoded += HEX.charAt(charCode & 0xF);
   }
  }
 } // for

// return encoded;
};
*/

//改变会话窗口消息发送按钮的背景颜色，以标识是否有未回复的消息
function newMsgNotifier(isNotifier)
{
	var notifier = document.getElementById("newMessageNotifer");

	if (isNotifier==true)
	{
		//notifier.style.background ="#FF0000";
		notifier.style.background ='url("image\\chatskin\\newmessagebuttonbk03.gif")';

	}
	else
	{
		notifier.style.background ='url("image\\chatskin\\sendbuttonbk.gif")';
	}
}
/*
//会话窗口的显示与隐藏
function toggleChat()
{
	var layer = document.getElementById("floatChat");
	if ( 0 == isChatVisible )
	{
		layer.style.overflow = "visible";
		isChatVisible = 1;
	}
	else if (1 == isChatVisible)
	{
		layer.style.overflow = "hidden";
		isChatVisible = 0;
	}
}

//展开在用户页面中的嵌入代码
function unfoldedCodes(chatServerAddress,assistantAddress)
{
	document.write("<style type=\"text/css\">");
	document.write("#floatChat{");
	document.write("BORDER-RIGHT: medium none; ");
	document.write("BORDER-TOP: medium none; ");
	document.write("LEFT: expression(eval(document.body.scrollLeft)); ");
	document.write("BORDER-LEFT: medium none; ");
	document.write("BORDER-BOTTOM: medium none; ");
	document.write("POSITION: absolute; ");
	document.write("TOP: expression(eval(document.body.scrollTop)+20); ");
	document.write("TEXT-ALIGN: center;");
	document.write("width:30px;");
	document.write("height:30px;");
	document.write("z-index:0;");
	document.write("overflow: hidden;");
	document.write("cursor:hand;");
	document.write("}");
	document.write("</style>");

	document.write("<DIV id=\"floatChat\">");
	document.write("<table width=\"300\" height=\"300\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
	document.write("<tr>");
	document.write("<td width=\"30\" height=\"30\" onClick=\"toggleChat()\" align=\"center\"><img src=\""+chatServerAddress+"image/clientchatbar.gif\" width=\"16\" height=\"16\"></td>");
	document.write("<td width=\"270\" bgcolor=\"#E9E9E9\" align=\"center\"><a style=\"font-size:12px;font-weight:bold\">"+assistantAddress+"乐意为您服务！</a></td>");
	document.write("</tr>");
	document.write("<tr>");
	document.write("<td height=\"270\" colspan=\"3\">");
	document.write("<iframe marginwidth=\"0\" marginheight=\"0\" hspace=\"0\" vspace=\"0\" frameborder=\"0\" scrolling=\"no\"  height=\"270\" width=\"300\" name=\"clientChat\" src=\""+chatServerAddress+"customerim.php?destUserName="+assistantAddress+"\"></iframe>");
	document.write("</td>");
	document.write("</tr>");
	document.write("</table>");
	document.write("</DIV>");
}
*/

//处理来自服务器的登录响应消息
function processLoginMsg (responseMsgXML)
{
	//document.write( responseMsgXML);/////////
	var resMsgXML=responseMsgXML.documentElement;
	if (resMsgXML!=null)
	{
		var resCode=resMsgXML.attributes[0].text;
		if (resCode == "200")
		{
			displayLoginNotification("登录成功，正在转到会话页面……");
			switchtoPage("serviceassistant.php?sourceName="+sourceUser);
		}
		else if (resCode == "404")
		{
			displayLoginNotification("用户名或密码错误！");
		}
		else
			displayLoginNotification("登录失败请重试！");
	}
}

function processAdminLoginMsg (responseMsgXML)
{
	var resMsgXML=responseMsgXML.documentElement;
	if (resMsgXML!=null)
	{
		var resCode=resMsgXML.attributes[0].text;
		if (resCode == "200")
		{
			displayLoginNotification("登录成功，正在转到管理页面……");
			switchtoPage("admin.php?sourceName="+sourceUser);
		}
		else if (resCode == "404")
		{
			displayLoginNotification("用户名或密码错误！");
		}
		else
			displayLoginNotification("登录失败请重试！");
	}
}

// oam
function processOamLoginMsg (responseMsgXML)
{
	var resMsgXML=responseMsgXML.documentElement;
	if (resMsgXML!=null)
	{
		var resCode=resMsgXML.attributes[0].text;
		if (resCode == "200")
		{
			displayLoginNotification("登录成功，正在转到管理页面……");
			switchtoPage("oam.php?sourceName="+sourceUser);
		}
		else if (resCode == "404")
		{
			displayLoginNotification("用户名或密码错误！");
		}
		else
			displayLoginNotification("登录失败请重试！");
	}
}

// g_oam
function processg_OamLoginMsg (responseMsgXML)
{
	var resMsgXML=responseMsgXML.documentElement;
	if (resMsgXML!=null)
	{
		var resCode=resMsgXML.attributes[0].text;
		if (resCode == "200")
		{
			displayLoginNotification("登录成功，正在转到管理页面……");
			switchtoPage("g_oam.php?sourceName="+sourceUser);
		}
		else if (resCode == "404")
		{
			displayLoginNotification("用户名或密码错误！");
		}
		else
			displayLoginNotification("登录失败请重试！");
	}
}

//在登录界面中显示提示信息
function displayLoginNotification(notificationString)
{
	var loginNotification = document.getElementById("loginNotification");
	loginNotification.innerText=notificationString;

}


//得到用户名
//如果在Cookie中有可用用户名，则使用此用户转向与目的用户的聊天界面；
//如果在Cookie中没有可用用户名，则向服务器（getCustomerName.php）请求一个新的用户名；
function getCustomerName(assistantName)
{
	destinationUser=assistantName;

	var cookieName=getCookie("customerName");

	if (cookieName!="NULL")
	{
		noticeAssistant(cookieName,destinationUser);
//		switchtoPage ("myim.php?sourceName="+cookieName+"&destinationName="+destinationUser+"&extendedBar=1");
		switchtoPage ("myim.php?sourceName="+cookieName+"&destinationName="+destinationUser+"&extendedBar=1");
//		silentSubmitMsg(destinationUser,sourceUser,sourceUser+"，您好，欢迎您！",2);
	}
	else
	{
//是否已经将“MSG_REQ”发送出去。如果为“1”，则已经发送出去；如果为“0”，则尚未发出
//设置通过Ajax访问的URL
		var ajax;

		var url = "getcustomername.php";

//初始化Ajax对象
		ajax= initAjax_getCustomerName ();

			//使用Get方式进行请求
		ajax.open ("GET", url, true);

			//定义回调函数
		ajax.onReadyStateChange = function ()
		{
			if (ajax.readyState == 4)
			{
				if (ajax.status == 200)
				{
					sourceUser = processNameMsg (ajax.responseXML);
					if (sourceUser!="NULL")
					{
						deleteCookie("customerName");
						addCookie("customerName",sourceUser,300);
						noticeAssistant(sourceUser,destinationUser);
						//switchtoPage ("myim.php?sourceName="+sourceUser+"&destinationName="+destinationUser+"&extendedBar=1");
						switchtoPage ("myim.php?sourceName="+sourceUser+"&destinationName="+destinationUser+"&extendedBar=1");
//						silentSubmitMsg(destinationUser,sourceUser,sourceUser+"，您好，欢迎您！",2);
					}
				}
			}
		}

		ajax.send (null);
	}

}

//处理从服务器得到的用户名响应
function processNameMsg (responseMsgXML)
{
	var resMsgXML=responseMsgXML.documentElement;
	if (resMsgXML!=null)
	{

		var resCode=resMsgXML.attributes[0].text;
		if (resCode == "200")
		{
			return resMsgXML.childNodes[0].text;
		}
		else
			return "NULL";
	}
	else
		return "NULL";
}

//本页面转向destURL
function switchtoPage (destURL)
{
	window.location=destURL;
}

//添加一个名称为name，值为value，在expireSeconds秒后超时的Cookie
function addCookie(name,value,expireSeconds){
      var cookieString=name+"="+escape(value);
      //判断是否设置过期时间
      if(expireSeconds>0){
             var date=new Date();
			 var oriTime=date.toGMTString();
             date.setTime(date.getTime()+expireSeconds*1000);
             cookieString=cookieString+";expires="+date.toGMTString();
      }
      document.cookie=cookieString;
}

//得到一个名称为name的Cookie的值
function getCookie(name){
      var strCookie=document.cookie;
      var arrCookie=strCookie.split("; ");
      for(var i=0;i<arrCookie.length;i++)
	  {
		var arr=arrCookie[i].split("=");
		if(arr[0]==name)
		{
//			alert("the name is "+arr[1]);
			return arr[1];
		}
      }
      return "NULL";
}

//删除名称为name的Cookie
function deleteCookie(name){
       var date=new Date();
       date.setTime(date.getTime()-10000);
       document.cookie=name+"=NULL;expires="+date.toGMTString();
}

//向服务器（login.php）发送登录请求
function submitLogin()
{
	var assistantName = document.getElementById("assistantName");
	var assistantPassword = document.getElementById("assistantPassword");
	var url="login.php?assistantName="+assistantName.value+"&assistantPassword="+assistantPassword.value;
	sourceUser = assistantName.value;

	var ajax;

	ajax= initAjax_submitLogin ();

	//使用Get方式进行请求
	ajax.open ("GET", url, true);

	//定义回调函数
	ajax.onReadyStateChange = function ()
	{
		if (ajax.readyState == 4)
		{
			if (ajax.status == 200)
			{
				processLoginMsg (ajax.responseXML);
			}
		}
	}

	ajax.send (null);
	displayLoginNotification("正在登录，请稍候...");

}

//向服务器（adminlogin.php）发送登录请求
function adminSubmitLogin()
{
	var assistantName = document.getElementById("assistantName");
	var assistantPassword = document.getElementById("assistantPassword");
	var url="adminlogin.php?assistantName="+assistantName.value+"&assistantPassword="+assistantPassword.value;
	sourceUser = assistantName.value;

	var ajax;

	ajax= initAjax_adminSubmitLogin ();

	//使用Get方式进行请求
	ajax.open ("GET", url, true);

	//定义回调函数
	ajax.onReadyStateChange = function ()
	{
		if (ajax.readyState == 4)
		{
			if (ajax.status == 200)
			{
				processAdminLoginMsg (ajax.responseXML);
			}
		}
	}

	ajax.send (null);
	displayLoginNotification("正在登录，请稍候...");

}

//向服务器（oamlogin.php）发送登录请求
function oamSubmitLogin()
{
	var assistantName = document.getElementById("assistantName");
	var assistantPassword = document.getElementById("assistantPassword");
	var url="oamlogin.php?assistantName="+assistantName.value+"&assistantPassword="+assistantPassword.value;
	sourceUser = assistantName.value;

	var ajax;

	ajax= initAjax_oamSubmitLogin ();

	//使用Get方式进行请求
	ajax.open ("GET", url, true);

	//定义回调函数
	ajax.onReadyStateChange = function ()
	{
		if (ajax.readyState == 4)
		{
			if (ajax.status == 200)
			{
				processOamLoginMsg (ajax.responseXML);
			}
		}
	}

	ajax.send (null);
	displayLoginNotification("正在登录，请稍候...");

}

//向服务器（g_oamlogin.php）发送登录请求
function g_oamSubmitLogin()
{
	var assistantName = document.getElementById("assistantName");
	var assistantPassword = document.getElementById("assistantPassword");
	var url="g_oamlogin.php?assistantName="+assistantName.value+"&assistantPassword="+assistantPassword.value;
	sourceUser = assistantName.value;

	var ajax;

	ajax= initAjax_oamSubmitLogin ();

	//使用Get方式进行请求
	ajax.open ("GET", url, true);

	//定义回调函数
	ajax.onReadyStateChange = function ()
	{
		if (ajax.readyState == 4)
		{
			if (ajax.status == 200)
			{
				processg_OamLoginMsg (ajax.responseXML);
			}
		}
	}

	ajax.send (null);
	displayLoginNotification("正在登录，请稍候...");

}

/*
//展开在用户页面中的点对点聊天嵌入代码
function unfoldedP2PChatCodes(chatServerAddress,sourceUserAddress,destinationAddress)
{
	document.write("<table width=\"100%\" height=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
	document.write("<tr>");
	document.write("<td height=\"30\" bgcolor=\"#FFFFFF\">与我聊天吧！</td>");
	document.write("</tr>");

	document.write("<tr>");
	document.write("<td>");
	document.write("<iframe marginwidth=\"0\" marginheight=\"0\" hspace=\"0\" vspace=\"0\" frameborder=\"0\" scrolling=\"no\"  height=\"100%\" width=\"100%\" name=\"P2PChat\" src=\""+chatServerAddress+"myim.php?sourceName="+sourceUserAddress+"&destinationName="+destinationAddress+"\"></iframe>");
	document.write("</td>");
	document.write("</tr>");
	document.write("</table>");
}
*/



//通用的Ajax请求发送函数
//url为处理此请求的服务器页面地址
//requesting_status记录消息处理情况
	//如果为“-1”，则无需记录消息处理情况；
	//如果为0,则为消息请求尚未发送；
	//如果为1,则为消息已被响应
//responseProcess指明请求被正确处理后返回的responseXML的函数
//para为函数responseProcess所需的其他参数，一般para可以为数组，从而支持为responseProcess传递多个参数

function sendAjaxRequest(url,requesting_status,responseProcess,para)
{

//初始化Ajax对象
	var ajax;

//是否已经将“MSG_REQ”发送出去。如果为“1”，则已经发送出去；如果为“0”，则尚未发出
	if (requesting_status ==0||requesting_status ==-1)
	{

		ajax= initAjax_sendAjaxRequest ();

		//使用Get方式进行请求
		ajax.open ("GET", url, true);

		//定义回调函数
		ajax.onReadyStateChange = function ()
		{
			if (requesting_status == 1)
				requesting_status = 0;
			if (ajax.readyState == 4)
			{
				if (ajax.status == 200)
				{
					responseProcess (ajax.responseXML,para);
				}
			}
		}

		ajax.send (null);
		if (requesting_status == 0)
			requesting_status = 1;

	}

}



/*
var	presenceServer;
var	requestStatus =0;

//处理从服务器得到的用户名响应
function processStatus (responseMsgXML,otherPara)
{
	var resMsgXML=responseMsgXML.documentElement;
	if (resMsgXML!=null)
	{

		var resCode=resMsgXML.attributes[0].text;
		if (resCode == "200")
		{
			showStatus(resMsgXML.attributes[1].text,"assistantStatus",1,statusBk);
		}
		else
			showStatus(0,"assistantStatus",1,statusBk);
	}
	else
			showStatus(0,"assistantStatus",1,statusBk);
}

function showStatus(statusId,tableTdId,isColor,statusUrlArray)
{
	var statusCell = document.getElementById(tableTdId);
	switch (statusId)
	{
		case "0":
			if (isColor==0)
				statusCell.style.background ="url(\""+statusUrlArray[0]+"\")";
			else
				statusCell.style.background =statusUrlArray[0];
//			statusCell.innerText =statusId+"离线";
			break;
		case "1":
			if (isColor==0)
				statusCell.style.background ="url(\""+statusUrlArray[1]+"\")";
			else
				statusCell.style.background =statusUrlArray[1];
//			statusCell.innerText =statusId+"在线";
			break;
		case "2":
			if (isColor==0)
				statusCell.style.background ="url(\""+statusUrlArray[2]+"\")";
			else
				statusCell.style.background =statusUrlArray[2];
//			statusCell.innerText =statusId+"离开";
			break;
		case "3":
			if (isColor==0)
				statusCell.style.background ="url(\""+statusUrlArray[3]+"\")";
			else
				statusCell.style.background =statusUrlArray[3];
//			statusCell.innerText =statusId+"会议中...";
			break;
		default:
			if (isColor==0)
				statusCell.style.background ="url(\""+statusUrlArray[0]+"\")";
			else
				statusCell.style.background =statusUrlArray[0];
//			statusCell.innerText =statusId+"无状态";
			break;
	}
}


function getAssistantStatus (sourName,cycleTime)
{
	presenceServer="getstatus.php?assistantName="+sourName;
	sentStatusRequest();
	setInterval ("sentStatusRequest()",cycleTime);
}


function sentStatusRequest()
{
	sendAjaxRequest(presenceServer,requestStatus,processStatus,-1);
}
*/

function sentKeepaliveMsg ()
{
	setInterval ("keepaliveProcess()",KEEPALIVE_MSG_TIMER);
}

function keepaliveProcess()
{
//	alert(sourceStatus);
	keepaliveUrl="keepalive.php?assistantName="+sourceUser+"&status="+sourceStatus;
	sendAjaxRequest(keepaliveUrl,-1,processKeepalive,-1);
}

function processKeepalive(p1,p2)
{
	return 0;
}

function setStatus(newStatus)
{
	if (newStatus==0)
		alert("您已经将您的状态设为\"离线\",您将无法收到来自他人的消息！");
	sourceStatus = newStatus;
}


function sendCallRequest()
{
	var calledPartyId = document.getElementById("calledPartyId");
	if (calledPartyId.value != "" && calledPartyId.value != null)
	{
		if (calledPartyId.value==sourceUser||calledPartyId.value==sourceClientName1||calledPartyId.value==sourceClientName2||calledPartyId.value==sourceClientName3||calledPartyId.value==sourceClientName4)
			alert("您与"+calledPartyId.value+"的聊天窗口已经激活，请使用已有窗口与"+calledPartyId.value+"交谈！");
		else
			putIntoWorkspace(calledPartyId.value);
	}
	else
	{
//			alert("Popup Windows");
			oUserString= "";
  			window.showModalDialog("contactman.php?userName="+sourceUser+"&defaultPage=1", window, "dialogWidth:600px;dialogHeight:400px;scroll:no;status:no");   
			if(oUserString!="")    
			{
				calledPartyId.value = oUserString; 
				if (calledPartyId.value==sourceUser||calledPartyId.value==sourceClientName1||calledPartyId.value==sourceClientName2||calledPartyId.value==sourceClientName3||calledPartyId.value==sourceClientName4)
					alert("您与"+calledPartyId.value+"的聊天窗口已经激活，请使用已有窗口与"+calledPartyId.value+"交谈！");
				else
					putIntoWorkspace(calledPartyId.value);
			}
	}
	calledPartyId.value ="";
}

function showstatusvar()
{
	alert(sourceStatus);
}

function aboutInfo()
{
	displayString("本软件由<img src=\"../image/logo_en.gif\" width=\"162\" height=\"46\">提供。<BR>Cyber.IM为CoCyber提供的在线IM应用，详情请访问\"<a href=\"http://www.cocyber.com\" target=\"_blank\">http://www.cocyber.com</a>\"");
}

function searchBar()
{
	displayString("<table width=\"200\" height=\"50\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td height=\"25\"><table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=\"135\"><input type=\"text\" name=\"searchtopic\" class=\"searchBuddyBox\" id=\"keyTopic\"></td><td width=\"60\" align=\"center\" valign=\"middle\"><table width=\"45\" height=\"20\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"  class=\"loginButton\" ><tr><td onClick=\"sentSearchRequest()\">搜索</td></tr></table></td></tr></table></td></tr><tr><td width=\"50%\"><table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"contentText\"><tr><td width=\"33%\"><input name=\"wiki\" type=\"checkbox\" value=\"checkbox\" id=\"wiki\" checked>Wiki</td><td width=\"33%\"><input name=\"baidu\" type=\"checkbox\" value=\"checkbox\" id=\"baidu\">百度</td><td width=\"34%\"><input name=\"google\" type=\"checkbox\" value=\"checkbox\" id=\"google\">Google</td></tr></table></td></tr></table>");
}

function inputBoxBlur()
{
	isInputBoxFocus=false;
	isFocus=false;

}
function inputBoxFocus()
{
	isInputBoxFocus=true;
	isFocus=true;

}

function inputBoxBlur2()
{
	isInputBoxFocus2=false;
	isFocus=false;

}
function inputBoxFocus2()
{
	isInputBoxFocus2=true;
	isFocus=true;

}


function setFocus()
{
	isFocus=true;
}
function setBlur()
{
	isFocus=false;
}
function getFocusStatus()
{
	return isFocus;
}
///////////////////////////////////
function setParentFocus()
{
	parent.setFocus();
}
function setParentBlur()
{
	parent.setBlur();
}
function getParentFocusStatus()
{
	return parent.getFocusStatus();
}
function isWinOpen()
{
	if (sourceClientName1=="NULL"&&sourceClientName2=="NULL"&&sourceClientName3=="NULL"&&sourceClientName4=="NULL")
		return false;
	else
		return true;

}

function onLogout()
{
	if (isWinOpen())
	{
    	if(confirm("有尚未关闭的会话窗口，是否要继续注销操作？"))
		{//如果是true ，那么关闭现有会话窗口，而后转向注销页面
			if (sourceClientName1!="NULL")
				closeDialog11();
			if (sourceClientName2!="NULL")
				closeDialog12();
			if (sourceClientName3!="NULL")
				closeDialog21();
			if (sourceClientName4!="NULL")
				closeDialog22();
			location.href="logout.php";
		}
	}
	else
	{
	    if(confirm("是否要继续注销操作？"))
		{//如果是true ，那么关闭现有会话窗口，而后转向注销页面
			location.href="logout.php";
		}
	}
}


function getTimeString()
{
var d = new Date();

var vYear = d.getFullYear();
var vMon = d.getMonth() + 1;
var vDay = d.getDate();

var vHour = d.getHours();
var vMin = d.getMinutes();
var vSec = d.getSeconds();

var yearString=(vYear);
var monthString=(vMon<10 ? "0" + vMon : vMon);
var dayString=(vDay<10 ?  "0"+ vDay : vDay );

var hourString=(vHour<10 ? "0" + vHour : vHour);
var minuteString=(vMin<10 ? "0" + vMin : vMin);
var secondString=(vSec<10 ?  "0"+ vSec : vSec );

var timeString=yearString+"-"+monthString+"-"+dayString+" "+hourString+":"+minuteString+":"+secondString;

return timeString;
}

function isNewClient(oArray, tArray)
{
	var i=0;
	var j=0;
	var isNew=1;
	for (;i<tArray.length;i++)
	{
		j=0;
		isNew=1;
		for(;j<oArray.length;j++)
		{
			if(tArray[i]==oArray[j])
			{
				isNew=0;
				break;
			}
		}
		if (isNew == 1)
			break;
	}
	return isNew;
}

function copyT2O(oArray,tArray)
{
	oArray.length=0;
	var i=0;
	for (;i<tArray.length;i++)
	{
		oArray[i]=tArray[i];
	}
	tArray.length=0;
}

function noticeAssistant(sourceNameString,destNameString)
{
	var sourceUrlString = getSourceUrl();
	if (sourceUrlString != null)
	{
		sourceUrlString="临时用户访问"+sourceUrlString+" ("+getTimeString()+")";
	}
	else
		sourceUrlString="临时用户访问 ("+getTimeString()+")";

	silentSubmitMsg(sourceNameString,destNameString,sourceUrlString,"2");
}

function setSourceUrl(urlString)
{
	sourceUrl=urlString;
}

function getSourceUrl()
{
	return sourceUrl;
}


//发送Service ID为serId，源为sourName，目的为destName的消息
function silentSubmitMsg(sourName,destName,myMsg,serId)
{

//设置通过Ajax访问的URL
	var url = "sentmsgtodb.php?myMsg="+myMsg+"&msgSource="+sourName+"&msgDestination="+destName+"&msgServiceId="+serId;

//初始化Ajax对象
	var ajax;
	ajax= initAjax_submitMsg ();

	//使用Get方式进行请求
	ajax.open ("GET", url, true);

	//定义回调函数
	ajax.onReadyStateChange = function ()
	{
		if (ajax.readyState == 4)
		{
			if (ajax.status == 200)
			{
//				alert("msgSendOK");
			}
		}
	}

	ajax.send (null);
}

var isBusinessCardVisible=false;

function toggleBusinessCard()
{
	var businessCard = document.getElementById("businessCard");

	if (isBusinessCardVisible == false )
	{
		businessCard.style.width="300px";
		businessCard.style.height="140px";
		businessCard.style.overflow="visible";
		isBusinessCardVisible = true;
	}
	else
	{
		businessCard.style.width="0px";
		businessCard.style.height="0px";
		businessCard.style.overflow="hidden";
		isBusinessCardVisible = false;
	}

}

var assistantName="";
var extendedBarStatus = 0;

function setExtendedBar(newExtendedBarStatus)
{
//	alert(assistantName);
	extendedBarStatus = newExtendedBarStatus;
//	alert(assistantName);
}

var isScore = 0;

function scoreBar(score,clientName,scoreUserName)
{
//	alert (clientName+scoreUserName);
	if (isScore == 0)
	{
		if(score == 1)
		{
			displayString('您给了'+scoreUserName+'一次好评！');
			silentScoreUser(scoreUserName,1);
			silentSubmitMsg(clientName,scoreUserName,clientName+'给了您一次好评！',2)
		}
		else
		{
			displayString('您给了'+scoreUserName+'一次恶评！');
			silentScoreUser(scoreUserName,-1);
			silentSubmitMsg(clientName,scoreUserName,clientName+'给了您一次恶评！',2)
		}
		isScore = 1;
	}
	else
		displayString('您已经对'+scoreUserName+'做了一次评价！');

}

//发送Service ID为serId，源为sourName，目的为destName的消息
function silentScoreUser(destName,score)
{

//设置通过Ajax访问的URL
	var url = "scoreuser.php?destname="+destName+"&score="+score;

//初始化Ajax对象
	var ajax;
	ajax= initAjax_scoreUser ();

	//使用Get方式进行请求
	ajax.open ("GET", url, true);

	//定义回调函数
	ajax.onReadyStateChange = function ()
	{
		if (ajax.readyState == 4)
		{
			if (ajax.status == 200)
			{
//				alert("msgSendOK");
			}
		}
	}

	ajax.send (null);
}

//发送Service ID为serId，源为sourName，目的为destName的消息
function silentSentMail(mailReceiver, mailSubject, mailBody)
{

//设置通过Ajax访问的URL
	var url = "../smtp/sentmail.php?receiver="+mailReceiver+"&subject="+mailSubject+"&body="+mailBody;

alert(url);
//初始化Ajax对象
	var ajax;
	ajax= initAjax_scoreUser ();

	//使用Get方式进行请求
	ajax.open ("GET", url, true);

	//定义回调函数
	ajax.onReadyStateChange = function ()
	{
		if (ajax.readyState == 4)
		{
			if (ajax.status == 200)
			{
				alert("msgSendOK");
			}
		}
	}

	ajax.send (null);
}


//////////////////////////////////////////////////////
var oUserString="";

function refreshUser( sUser, dUser)
{
	toggleAdvToolBar();
//	发起转移的人是sUser;
//	var newDUser = prompt("请输入前转用户名","");
	
	oUserString= "";
  	window.showModalDialog("contactman.php?userName="+dUser+"&defaultPage=0", window, "dialogWidth:600px;dialogHeight:400px;scroll:no;status:no");   
  	if(oUserString!="")    
	{
		newDUser = oUserString; 
		displayString ( "已经将您与"+sUser+"的会话转向"+newDUser+"，"+sUser+"和"+newDUser+"将得到新会话提醒！" );
		silentSubmitMsg(dUser,sUser,newDUser,5);
	}
}


//此函数未被使用
function extendTextDisplay()
{
	var textDisplayDiv = document.getElementById("textDisplay");
	textDisplayDiv.style.height= (document.body.clientHeight-15-50-20-25-25)+"px" ;

}

function sentMail(eMailAddressString)
{
	var mailSubject = document.getElementById("mailTopic").value;
	var mailBody =  document.getElementById("mailContent").value;
	silentSentMail(eMailAddressString, mailSubject, mailBody);
}




function processString(iString)
{
	var re;
	var oString = iString;

	re = /\[:\)\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/01.gif\">");

	re = /\[:f\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/02.gif\">");

	re = /\[:K\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/03.gif\">");

	re = /\[:=\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/04.gif\">");

	re = /\[:0\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/05.gif\">");

	re = /\[:Z\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/06.gif\">");

	re = /\[:\(\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/07.gif\">");

	re = /\[:D\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/08.gif\">");

	re = /\[:\|\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/09.gif\">");

	re = /\[:I\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/10.gif\">");

	re = /\[:O\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/11.gif\">");

	re = /\[:Y\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/12.gif\">");

	re = /\[:X\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/13.gif\">");

	re = /\[:o\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/14.gif\">");

	re = /\[:dh\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/15.gif\">");

	re = /\[:ws\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/16.gif\">");

	re = /\[:yb\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/17.gif\">");

	re = /\[:qw\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/18.gif\">");

	re = /\[:nz\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/19.gif\">");

	re = /\[:sl\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/20.gif\">");

	re = /\[:\>\]/g;
	oString = oString.replace(re, "<img src=\"image/emotion/21.gif\">");

	return oString;

}


var isEmotionVisible=false;

//var cursorPos = 0;

function toggleEmotion()
{
//	var inputBox = document.getElementById("myMsgInput");
//	cursorPos = inputBox.value.length;
//	cursorPos = inputBox.createTextRange().text.length;
//	alert("cursorPos is "+cursorPos);

	var emotion = document.getElementById("emotionSelector");

	if (isEmotionVisible == false )
	{
		emotion.style.width="300px";
		emotion.style.height="140px";
		emotion.style.overflow="visible";
		isEmotionVisible = true;
	}
	else
	{
		emotion.style.width="0px";
		emotion.style.height="0px";
		emotion.style.overflow="hidden";
		isEmotionVisible = false;
	}
//	alert("OK");
//	alert(textAreaID.text.length);

}

///////////////////////////////////////////////////////
function insertString(textAreaID, tString)
{
	var inputBox = document.getElementById("myMsgInput");
	toggleEmotion();
	inputBox.focus();
	var r = document.selection.createRange();
	r.moveStart("character",inputBox.value.length);
//	alert(cursorPos);
//	cursorPos = 21;
//	r.moveStart("character",cursorPos);
	r.collapse(true);
	r.select();
	r.text = tString;
}

var isAdvToolBarVisible=false;


function toggleAdvToolBar()
{
//	var inputBox = document.getElementById("myMsgInput");
//	cursorPos = inputBox.value.length;
//	cursorPos = inputBox.createTextRange().text.length;
//	alert("cursorPos is "+cursorPos);

	var advToolBar = document.getElementById("advToolBar");

	if (isAdvToolBarVisible == false )
	{
		advToolBar.style.width="302px";
		advToolBar.style.height="102px";
		advToolBar.style.overflow="visible";
		isAdvToolBarVisible = true;
	}
	else
	{
		advToolBar.style.width="0px";
		advToolBar.style.height="0px";
		advToolBar.style.overflow="hidden";
		isAdvToolBarVisible = false;
	}
}



function cooperateSurfing( sUser, dUser)
{
	toggleAdvToolBar();
//	发起转移的人是sUser;
	var coURL = prompt("请输入希望与对方协同浏览的网址","");
	if (coURL!=null)
	{
		displayString ( "已经将您向"+sUser+"发送了协同浏览"+coURL+"的请求！" );
		silentSubmitMsg(dUser,sUser,coURL,6);
	}
}

function setSourceUserName (userNameString)
{
	sourceUser = userNameString;
}