function SetServerData(key,value1,value2)
{
	var doc=new ActiveXObject("Msxml2.DOMDocument");
	var root=doc.createElement("root");			
	var k=doc.createElement("k");
	k.text=key;	
	root.appendChild(k);
	if (value1!=null)
	{
		var v1=doc.createElement("v1");
		v1.text=value1;	
		root.appendChild(v1)	
	}
	if (value2!=null)
	{
		var v2=doc.createElement("v2");
		v2.text=value2;
		root.appendChild(v2)	
	}
	
	var v3=doc.createElement("v3");
	v3.text=_wkEntry;
	root.appendChild(v3);
	
	var v4=doc.createElement("v4");
	v4.text=_wktraceID;
	root.appendChild(v4);
		
	doc.documentElement=root;		
	var req = new ActiveXObject("Msxml2.XMLHTTP");	
	req.open("POST","/FrontWeb/GetData.aspx", false);		
	req.send(doc);
	return req.responseText;
}

function ShowAlert(msg)
{
	//Create Dialog Div
	alert(msg);
}

function OnHeart()
{	
	if (_wkHeart==0)
	{		
		SetServerData(1);
	}
	_wkHeart=0;
}

function QueryCSName(v)
{
	var v=SetServerData(7,v);
	var n=parseInt(v.substring(0,1),10);
	if (n==0)
	{
		return v.substring(1);
	}else
	{
		ShowAlert(v.substring(1));
	}
}

var inviteMessage="";
function OnActionHeart()
{
	var v=SetServerData(2);	
	
	_wkHeart=1;
	_wkTimeout=window.setTimeout("OnActionHeart()",2000);
	if (v==null || v=="")
	{	
		return;
	}
	
	//get invite message
	var message=v.substring(1);
	
	if(message!=null && message!="")
	{
		inviteMessage=message;
	}

	var n=parseInt(v.substring(0,1),10);
		
	//1 invite 2 accept 3 reject
	if (n==1)
	{		
		if (document.body==undefined)
		{
			attachEvent("onload",ShowInviteDialog);
		}
		else
		{
			ShowInviteDialog();	
		}	
	}else if (n==2)
	{
		// show dialog chat
		if (document.body==undefined)
		{
			attachEvent("onload",ShowChatDialog);	
		}else
		{
			ShowChatDialog();
		}
	}else
	{
		//dialog reject
		if (n!=0)
		{
			ShowAlert(v.substring(1));
		}		
	}
}

function CheckCS()
{
	var v=SetServerData(6);
	var v1=v.substring(0,2);
	return v1;
}

function OnChartHeart()
{
	var v=SetServerData(4);
	_wkHeart=1;
	_wkTimeout=window.setTimeout("OnChartHeart()",2000);	
	if (v!=null && v!="")
	{
		ReceiveMessage(v.substring(0,19),v.substring(19));
	}	
}

function InviteCS()
{
	var v=SetServerData(6);
	var v1=v.substring(0,2);
	var v2=v.substring(2);
	if (v!=null && v!="" && v1!="00")
	{
		ShowAlert(v2);
		return;
	}
	v=SetServerData(5,"1");
	var n=parseInt(v.substring(0,1),10);
	if (n==2)
	{
		ShowChatDialog();
	}
	else
	{
		if (n!=0)
		{
			ShowAlert(v.substring(1));
		}		
	}
}

function AcceptInvite()
{
	closemsgdialog();
	var v=SetServerData(5,"2");
	var n=parseInt(v.substring(0,1),10);
	if (n==2)
	{
		ShowChatDialog();
	}else	
	{	
		if (n!=0)
		{
			ShowAlert(v.substring(1));
		}
	}
}

function RejectInvite()
{
	closemsgdialog();
	var v=SetServerData(5,"3");
	var n=parseInt(v.substring(0,1),10);
	if (n==2)
	{
		ShowChatDialog();
	}else
	{
		if (n!=0)
		{
			ShowAlert(v.substring(1));
		}
	}
}

function CloseChat()
{
	var v=SetServerData(5,"4");	
	window.clearTimeout(_wkTimeout);
	_wkTimeout="";
	OnActionHeart();
	closemsgdialog();
}

function ShowInviteDialog()
{
	//Show Invite Dialog
	WriteInviteDialogToUI();
}

function ShowChatDialog()
{
	//Show Chat Dialog
	WriteChatDialogToUI();
		
	window.clearTimeout(_wkTimeout);
	_wkTimeout="";
	OnChartHeart();
}

function ReceiveMessage(time,msg)
{
	//1 visitor 2 cs	
	AddMsg(msg,time,2);
}

//get ChatDialog ad
function GetChatAD()
{
	var v=SetServerData(8);
	var adCode;
	var n=parseInt(v.substring(0,1),10);
	if(n==0)
	{
		adCode=v.substring(1);
		if(adCode=="" || adCode==null)
		{
			adCode="<div style='border:1px solid #cfcfcf;background-color:#FFFFFF;padding:1px;margin-top:3px; width:124px;margin-left:5px;'><a href='#' target='_blank'><img src='/FrontWeb/images/link0.GIF'  border='0'></a></div>";
			adCode=adCode+"<div style='border:1px solid #cfcfcf;background-color:#FFFFFF;padding:1px;margin-top:3px; width:124px;margin-left:5px;'><a href='#' target='_blank'><img src='/FrontWeb/images/link1.GIF'  border='0'></a></div>";
			adCode=adCode+"<div style='border:1px solid #cfcfcf;background-color:#FFFFFF;padding:1px;margin-top:3px; width:124px;margin-left:5px;margin-bottom:4px;'><a href='#' target='_blank'><img src='/FrontWeb/images/link2.GIF' border='0'></a></div>";
		}
	}
	return adCode;
}

//frame and fill
function CreateMainFrame(str,width,height,bClose)
{	
	var msgw=width;//Width
	var msgh=height;//Height
	
	//create main div
	var msgObj=document.createElement("div")
	msgObj.style.position = "absolute";
	msgObj.style.left = parseInt((document.body.offsetWidth-msgw)/2);
	msgObj.style.top = parseInt((document.body.offsetHeight-msgh)/2);
	msgObj.style.width = msgw + "px";
	msgObj.style.height =msgh + "px";
	msgObj.style.zIndex = zIndex;
	zIndex=zIndex+1;
	var tb=document.createElement("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" height=\"100%\"></table>");
	var tBody=document.createElement("<tbody></tbody>");
	
	var trTitle=document.createElement("<tr style='cursor:move;' onmousedown='onmsgmousedown()' onmousemove='onmsgmousemove()' onmouseup='onmsgmouseup()'></tr>");
	var tdTitleLeft=document.createElement("<td width='15'></td>");
	tdTitleLeft.innerHTML="<img src='/FrontWeb/images/dialog_tl.gif'/>";
	
	var tdTitleImage=document.createElement("<td width='35px' align='center' valign='middle' nowrap background='/FrontWeb/images/dialog_tm.gif'></td>");
	tdTitleImage.innerHTML="<img src='/FrontWeb/images/dialog_ico.gif'/>&nbsp;";
	
	var tdTitle=document.createElement("<td width='"+(msgw-80)+"' background='/FrontWeb/images/dialog_tm.gif' height='35px' nowrap></td>");
	var tdTitleClose=document.createElement("<td width='15' nowrap valign='middle' background='/FrontWeb/images/dialog_tm.gif' style='cursor:hand;'></td>");
	tdTitleClose.innerHTML="<img src='/FrontWeb/images/dialog_close.gif'/>";
	if (bClose)
	{
		tdTitle.innerHTML="<font color='white'><b>"+QueryCSName()+"</b></font>";
		tdTitleClose.attachEvent("onclick",closemsgdialog);
	}else
	{		
		tdTitle.innerHTML="<font color='white'><b>"+QueryCSName(1)+"</b></font>";
		tdTitleClose.attachEvent("onclick",CloseChat);
	}
	var tdTitleRight=document.createElement("<td width='15'></td>");
	tdTitleRight.innerHTML="<img src='/FrontWeb/images/dialog_tr.gif'/>";
	
	trTitle.appendChild(tdTitleLeft);
	trTitle.appendChild(tdTitleImage);	
	trTitle.appendChild(tdTitle);
	trTitle.appendChild(tdTitleClose);
	trTitle.appendChild(tdTitleRight);		
	tBody.appendChild(trTitle);
	
	var trContent=document.createElement("<tr></tr>");
	var tdContent=document.createElement("<td height='100%' colspan=\"5\" width='100%'></td>");	
	tdContent.innerHTML=str;
	trContent.appendChild(tdContent);
	
	tBody.appendChild(trContent);	
	tb.appendChild(tBody);	

	msgObj.appendChild(tb);	
	var frm=document.createElement("<iframe src=\"javascript:false\" style=\"Z-INDEX:-1; FILTER:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0); LEFT:0px; VISIBILITY:inherit; WIDTH:"+msgw+"px; POSITION:absolute; TOP:0px; HEIGHT:"+msgh+"px\"></iframe>");
	msgObj.appendChild(frm);
	document.body.appendChild(msgObj);
}

var zIndex=1000;
var x1,y1;
var x2,y2;
var msgDiv,msgObj;
function msgGetDiv(o)
{
	var o1=o;
	while(o1 && o1.tagName!="DIV")
	{
		o1=o1.parentElement;
	}	
	return o1;
}

function onmsgmousedown()
{
	msgObj=event.srcElement;	
	x1=event.x;
	y1=event.y;	
	msgDiv=msgGetDiv(msgObj);
	if (msgDiv)
	{
		x2=msgDiv.style.pixelLeft;
		y2=msgDiv.style.pixelTop;
	}
	msgObj.setCapture();
}
function onmsgmouseup()
{
	if (msgObj)
	{
		msgObj.releaseCapture();
		msgObj=null;
	}
}
function onmsgmousemove()
{
	if (msgObj)
	{		
		msgDiv.style.left=x2+(event.x-x1);
		msgDiv.style.top=y2+(event.y-y1);		
	}
}
function closemsgdialog(o)
{
	var v=o;
	if (event)
	{
		var v=msgGetDiv(event.srcElement);		
	}
	if (v)
	{
		document.body.removeChild(v);
	}
}


//dialog invite
function WriteInviteDialogToUI()
{
	var v=QueryCSName();
	var str="";
	var w=380;//dialog width
	var h=230;//dialog height	
	str="<table width='100%' height='100%' style='border:1px solid #c51100' background='/FrontWeb/images/kf.JPG'>";
	str=str+"<tr>";
	str=str+"<td width='129' rowspan='3'></td>";
	str=str+"<td height='140' colspan='5'>";
	str=str+"<table width='88%' height='100' border='0' align='center' cellpadding='0' cellspacing='0'>";
	str=str+"<tr>";
	str=str+"<td height='40' style='font-size:15px;font-family:ºÚÌå;color:#CC0033;font-weight:bold; font-style:italic;text-align:center; '><font color='#CC0033'>\u6B22\u8FCE\u60A8\u5149\u4E34\u4E07\u79D1\u7F51\u4E0A\u9500\u552E\u4E2D\u5FC3</font></td>";
	str=str+"</tr>";
	str=str+"<tr>";
	str=str+"<td height='60' valign='top' style='padding-top:5px;font-size:12px;color:#CC0033;line-height:120%; word-break:break-all;padding-left:10px;font-weight:bold;'>"+inviteMessage+"</td>";
	str=str+"</tr>";
	str=str+"</table>";
	str=str+"</td>";
	str=str+"</tr>";
	str=str+"<tr>";
	str=str+"<td width='39' height='21'></td>";
	str=str+"<td width='93'><img src='/FrontWeb/images/bt_accept0ut.gif' onmouseout=\"this.src='/FrontWeb/images/bt_accept0ut.GIF'\" onmouseover=\"this.src='/FrontWeb/images/bt_acceptOver.GIF'\" style='cursor:hand;' onclick='javascript:AcceptInvite();'/></td>";
	str=str+"<td width='0'></td>";
	str=str+"<td width='93'><img src='/FrontWeb/images/bt_rejectOut.gif' onmouseout=\"this.src='/FrontWeb/images/bt_rejectOut.GIF'\" onmouseover=\"this.src='/FrontWeb/images/bt_reject0ver.GIF'\" style='cursor:hand;' onclick='javascript:RejectInvite();'/></td>";
	str=str+"<td width='39'></td>";
	str=str+"</tr>";
	str=str+"<tr>";
	str=str+"<td height='21' colspan='5'></td>";
	str=str+"</tr>";
	str=str+"</table>";
	CreateMainFrame(str,w,h,true);
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//the chat dialog ui
function WriteChatDialogToUI()
{
	if (document.all("MessageBox")!=undefined)
	{
		return;
	}		
	var chatW=600;//dialog width
	var chatH=500;//dialog height
	var str="";	
	str=str+"<table cellpadding='0' cellspacing='0' border='0' width='100%'>";
	str=str+"<tr>";
	str=str+"<td height='450' background='/FrontWeb/images/dialog_midbg.gif' >";
	str=str+"<table width='97%' height='100%' border='0' align='center' cellpadding='0' cellspacing='0'>";
	str=str+"<tr>";
	str=str+"<td height='8' colspan='3'></td>";
	str=str+"</tr>";
	str=str+"<tr>";
	str=str+"<td width='76%' rowspan='2' valign='top' style='background-color:#FFFFFF;'><div id='MessageBox' style='background-color:transparent;border:1px solid #cfcfcf; overflow:auto;overflow-x:hidden;word-break:break-all;width:100%; height:100%;' align='left'></div></td>";
	str=str+"<td width='1%' rowspan='2'></td>";
	str=str+"<td width='23%' height='126' valign='top' style='background-color:#FFFFFF;border:1px solid #cfcfcf;padding:3px;text-align:Center;'><img src='/FrontWeb/images/right_pic.GIF' width='120' height='120'></td>";
	str=str+"</tr>";
	str=str+"<tr>";
	str=str+"<td width='23%' height='175' valign='top' style='background-color:#FFFFFF;border:1px solid #cfcfcf;text-align:Center;'>";
	//str=str+"<div style='border:1px solid #cfcfcf;background-color:#FFFFFF;padding:1px;margin-top:3px; width:124px;margin-left:5px;'><a href='#'><img src='/FrontWeb/images/link0.GIF'  border='0'></a></div>";
	//str=str+"<div style='border:1px solid #cfcfcf;background-color:#FFFFFF;padding:1px;margin-top:3px; width:124px;margin-left:5px;'><a href='#'><img src='/FrontWeb/images/link1.GIF'  border='0'></a></div>";
	//str=str+"<div style='border:1px solid #cfcfcf;background-color:#FFFFFF;padding:1px;margin-top:3px; width:124px;margin-left:5px;margin-bottom:4px;'><a href='#'><img src='/FrontWeb/images/link2.GIF' border='0'></a></div>";
	str=str+GetChatAD();//insert ad to ChatDialog
	str=str+"</td>";
	str=str+"</tr>";
	str=str+"<tr>";
	str=str+"<td height='8' align='center'><img src='/FrontWeb/images/mid_line.GIF' width='34' height='2'></td>";
	str=str+"<td height='8' colspan='2'></td>";
	str=str+"</tr>";
	str=str+"<tr>";
	str=str+"<td height='29' colspan='3' style='border:1px solid #cfcfcf;padding-left:5px;' background='/FrontWeb/images/dialog_bg11.GIF'><img src='/FrontWeb/images/bt_clear_out.GIF' alt='\u6E05\u9664\u8BB0\u5F55' onmouseout=\"this.src='/FrontWeb/images/bt_clear_out.GIF'\" onmouseover=\"this.src='/FrontWeb/images/bt_clear_over.GIF'\" style='cursor:hand;' onclick='javascript:ClearMsg();'></td>";
	str=str+"</tr>";
	str=str+"<tr>";
	str=str+"<td colspan='3' height='1'style='border-left:1px solid #cfcfcf;background-color:#ffffff;font-size:1px;'>&nbsp;</td>";
	str=str+"</tr>";
	str=str+"<tr>";
	str=str+"<td rowspan='2' style='border-left:1px solid #cfcfcf;border-right:1px solid #cfcfcf;background-color:#ffffff;' valign='top'><textarea name='SendMagBox' id='SendMagBox' style='background-color:#ffffff;border:1px solid #d7d5d6; overflow:auto; width:100%; height:100%;word-break : normal;' onkeydown='OnChatKeyDown()' ></textarea></td>";
	str=str+"<td rowspan='4'>&nbsp;</td>";
	str=str+"<td height='45' align='center'><img src='/FrontWeb/images/bt_send.GIF' alt='\u6309Enter\u952E\u53D1\u9001' style='cursor:hand;' onclick='javascript:SendMessage();' border='0'></td>";
	str=str+"</tr>";
	str=str+"<tr>";
	str=str+"<td height='45'  align='center'><img src='/FrontWeb/images/bt_exit.gif' alt='\u53D6\u6D88' border='0' style='cursor:hand;' onclick='javascript:closemsgdialog();'></td>";
	str=str+"</tr>";
	str=str+"<tr>";
	str=str+"<td style='border-left:1px solid #cfcfcf;border-right:1px solid #cfcfcf;border-bottom:1px solid #cfcfcf;background-color:#ffffff;'>&nbsp;</td>";
	str=str+"<td height='35' rowspan='2'  align='right'><img src='/FrontWeb/images/dialog_logo.GIF'>&nbsp;&nbsp;&nbsp;&nbsp;</td>";
	str=str+"</tr>";
	str=str+"<tr>";
	str=str+"<td>&nbsp;</td>";
	str=str+"</tr>";
	str=str+"</table>";
	str=str+"</td>";
	str=str+"</tr>";
	str=str+"<tr>";
	str=str+"<td height='15'><img src='/FrontWeb/images/dialog_footbg.gif' width='600' height='15' /></td>";
	str=str+"</tr>";
	str=str+"</table>";

	CreateMainFrame(str,chatW,chatH,false);
}

//close the chat dialog
function CloseChatDialog(o)
{	
	window.clearTimeout(_wkTimeout);
	_wkTimeout="";
	OnActionHeart();
	closemsgdialog(o);
}

function SendMessage()
{
	var send=document.getElementById("SendMagBox");
	if(AddMsg(send.value,new Date().toLocaleTimeString(),1))
	{
		send.value="";		
	}else
	{
		CloseChatDialog(send);
	}	
}

//mode 1 visitor;2 cs
function AddMsg(message,sendTime,mode)
{
	WriteChatDialogToUI();
	var msg="";		
	
	if(mode==1)
	{	
		if(message!="")
		{
			var v=SetServerData(3,message);
			if (v!=null && v!="")
			{		
				ShowAlert(v.substring(19));	
				return false;
			}else
			{
				msg = "<font color = 'red'>\u60A8\u8BF4: "+new Date().toLocaleTimeString()+"</font><br>&nbsp;&nbsp;&nbsp;" + message+"<br>"				
			}
		}
		else
		{
			ShowAlert("\u4E0D\u80FD\u53D1\u9001\u7A7A\u6D88\u606F\uFF01");			
		}
	}
	else
	{
		msg = "<font color = 'blue'>\u4E07\u79D1\u5BA2\u670D\u8BF4: "+sendTime.substring(11)+"</font><br>&nbsp;&nbsp;&nbsp;" + message+"<br>"
	}
	
	if(msg!="")
	{
		MessageBox.innerHTML += msg;
		MessageBox.scrollTop=MessageBox.scrollHeight;
	}	
	return true;
}

//clear the msg
function ClearMsg()   
{  
	MessageBox.innerHTML="";
}

//send message
function OnChatKeyDown()
{ 
	if(event.keyCode == 13)   
	{   
		SendMessage();
		window.event.returnValue=false;
	}  	
}

function ShowInvite()
{	
	var btn = document.createElement("<INPUT TYPE='button'  value='ShowInvite' onclick='ShowInviteDialog()'>")
	var btn2 = document.createElement("<INPUT TYPE='button' value='Chat' onclick='ShowChatDialog()'>")
	var btn3 = document.createElement("<INPUT TYPE='button' value='Invite' onclick='InviteCS()'>")
    document.body.insertBefore(btn);
    document.body.insertBefore(btn2);
    document.body.insertBefore(btn3);
}
//attachEvent("onload",ShowInvite);