﻿var request = null;
//var chatID=-1;
var listID=-1;
var cont=false;
var content0="";
var list="";
var version="1.20";
var cost0=127;
var target1=": ";
var target3="0";
var count=0;
var debug=false;
var newTotal="";
var chatMain2="";
var group=0
var timeOut=0;

function createRequest()
{
	try
	{
	request = new XMLHttpRequest();
	} 
	catch (trymicrosoft)
	{
		try
		{
			request = new ActiveXObject("MsXML2.XMLHTTP");
		} 
		catch (othermicrosoft)
		{
			try
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(failed)
			{
				request = null;
			}
		}
	}

	if(request == null)
		alert("Error creating request object");
}

function startChat()
{
	if(version==document.chat.version.value)
	{
		timeOut=0;
		createRequest();
		var url = "/s_chat.jsp?ID="+chatID+"&listID=0&userID="+document.chat.userID.value+"&userName="+encodeURI(document.chat.userName.value).replace("&", "%26").replace("+", "%2B")+"&GROUP="+group; 
		//+"&target=0&content="+encodeURI("使用者 "+document.chat.userName.value+(cont?" 離開":" 進入")+"系統.").replace("&", "%26");
//		alert(url);
		var postStr="";
		if(cont)
		{
			cont=false;
			document.getElementById("cont2").value="連線";
			clearInterval(chatLoop);
			chatSound.src='http://img.dnaxcat.net/upfile/sound1.mp3';
		}
		else
		{
			if(debug)
				alert("連線");
			cont=true;
			document.getElementById("cont2").value="離線";
			chatLoop=setInterval("keepChat()", 10000);
			request.open("GET", url, true);
			request.onreadystatechange = renewChat;
			request.send(null);
		}
	}
	else
	{
		alert("程式版本錯誤");
	}
}

function keepChat()
{
	count++;
	if(!cont)
		document.getElementById("chatList").innerHTML="1";
	createRequest();
	var url = "/s_chat.jsp?ID="+chatID+"&listID="+listID+(cont?"&userID="+document.chat.userID.value+"&userName="+encodeURI(document.chat.userName.value).replace("&", "%26").replace("+", "%2B"):"")+"&COUNT="+count+"&GROUP="+group;
	request.open("GET", url, true);
	request.onreadystatechange = renewChat;
	request.send(null);
}

function sendChat()
{
	if(document.chat.content.value=="$DEBUG")
	{
		debug=!debug;
		document.chat.content.value="";
		document.chat.content.focus();
		alert('Debug '+debug);
	}
	else if(cont)
	{
		if(document.chat.content.value!="")
		{
				content1=document.chat.content.value;
				while(content1.indexOf("</")!=-1)
					content1=content1.replace("</", "&lt;/");
				if(document.chat.admin.value=='0')
				{
					while(content1.indexOf("<")!=-1)
						content1=content1.replace("<", "&lt;");
					while(content1.indexOf(">")!=-1)
						content1=content1.replace(">", "&gt;");
				}
				content0=encodeURI(target1+content1);
				content1="Sending.......";
				while(content0.indexOf("&")!=-1)
					content0=content0.replace("&", "%26");
				while(content0.indexOf("+")!=-1)
					content0=content0.replace("+", "%2B");
				var url = "/s_chat.jsp?ID="+chatID+"&listID="+listID+"&userID="+document.chat.userID.value+"&target="+target3+"&userName="+encodeURI(document.chat.userName.value).replace("&", "%26").replace("+", "%2B")+"&content="+content0+"&GROUP="+group;

				createRequest();
				request.open("GET", url, true);
		//		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//		request.setRequestHeader("Content-length", postStr.length);
		//		request.setRequestHeader("Connection", "close");
				request.onreadystatechange = sended;

				setTimeout("request.send(null);", 1);
//				request.send(null);
				
				cost0+=1;
		}
	}
	else
		alert("尚未連線!!");
}

function sended()
{
	renewChat();
	document.chat.content.value="";
	document.chat.content.focus();
	timeOut=0;
}

function renewChat()
{
	if(debug)
			document.getElementById("chatBack").innerHTML=", "+request.readyState+document.getElementById("chatBack").innerHTML;
	if(request.readyState == 4)
	{
if(debug)	alert('renew01');
		if(request.status==200)
		{
if(debug)	alert('renew02');
			newTotal = request.responseText;
if(debug)	alert('renew021');
			chatMain2 = document.getElementById("chatMain");
if(debug)
{
	alert('renew022');
	alert(document.getElementById("chatBack").innerHTML);
	alert('renew023');
	document.getElementById("chatBack").innerHTML="<br>"+newTotal+document.getElementById("chatBack").innerHTML;
	alert('renew03');
}
			if(newTotal.lastIndexOf("$cmd")>0)
			{
if(debug)	alert('renew0');
				chatID2=newTotal.substring(newTotal.lastIndexOf("$chat")+5, newTotal.lastIndexOf("$userList"));
if(debug)	alert('renew1');
				list=newTotal.substring(newTotal.lastIndexOf("$userList")+9, newTotal.lastIndexOf("$listID"));
if(debug)	alert('renew2');
				listID2=newTotal.substring(newTotal.lastIndexOf("$listID")+7, newTotal.lastIndexOf("$cmd"));
if(debug)	alert('renew3');
				cmd=newTotal.substring(newTotal.lastIndexOf("$cmd")+4, newTotal.length);
if(debug)	alert('renew4');
				newChat=newTotal.substring(0, newTotal.indexOf("$chat"))
if(debug)	alert('renew5');
//if(debug)
//	document.getElementById("chatBack").innerHTML="listID2: "+listID2+"\nchatID2: "+chatID2+"<p>"+document.getElementById("chatBack").innerHTML;
				
				if(chatID2!=chatID && !isNaN(chatID2))
				{
if(debug)	alert('renew6');
					chatStr1= newChat + chatMain2.innerHTML;
					if(chatStr1.length>32768 && document.getElementById("chatBack")!=null)
					{
						chatIndex = chatStr1.indexOf("<TABLE", 16384);
						document.getElementById("chatBack").innerHTML = chatStr1.substring(chatIndex) + document.getElementById("chatBack").innerHTML;
						chatMain2.innerHTML = chatStr1.substring(0, chatIndex);
					}
					else
						chatMain2.innerHTML = chatStr1;
					chatID=chatID2;
					if(document.chat.soundEvent.value=="2")
					{
						if(!isNaN(listID2) && listID2!=listID && list!='')
							chatSound.src='http://img.dnaxcat.net/upfile/sound1.mp3';
						else
							chatSound.src='http://img.dnaxcat.net/upfile/sound2.mp3';
					}
					else if(newChat.indexOf(">"+document.chat.userName.value+"</")!=-1 && document.chat.soundEvent.value=="1")
						chatSound.src='http://img.dnaxcat.net/upfile/sound2.mp3';
				}
if(debug)	alert('renew7');
					
				if(listID2!=listID && !isNaN(listID2) && list!='')
				{
					if(cont)
						listID=listID2;
					document.getElementById("chatList").innerHTML=list;
				}
				document.chat.cost.value= "$"+ ((++cost0)>>cost);
				if(cmd.length>0)
					eval(cmd);
			}
			timeOut++;
			if(timeOut>90 && document.chat.admin.value!="2")
			{
				timeOut=0;
				startChat();
			}
		}
		else
			alert(request.status+"\n\n"+request.responseText);

	}
}

function target0()
{
	group=parseInt(document.chat.group.value);
	if(group>0)
		group=-group;
	else
		group=0;
	if(document.chat.gTalk.checked && group!=0)
	{
		document.getElementById("target").innerHTML="<font color=008000>"+(-group)+"</FONT> ";
		target1="><font color=008000>"+(-group)+"</FONT>: ";
		target3=group;
	}
	else
	{
		document.getElementById("target").innerHTML="全體";
		target1=": ";
		target3=0;
	}
	document.chat.content.focus();
}


function target(tar1)
{
//	alert(document.chat.gTalk.checked);
	group=parseInt(document.chat.group.value);
	if(group>0)
		group=-group;
	else
		group=0;
	if(document.chat.gTalk.checked && group!=0)
	{
		document.getElementById("target").innerHTML="<font color=008000>"+(-group)+"</FONT> ";
		target1="><font color=008000>"+(-group)+"</FONT>: ";
		target3=group;
	}
	else
	{
		document.getElementById("target").innerHTML=(tar1=="全體"?"全體":"<font color=ff8000>"+tar1+"</font>");
		target1=(tar1=="全體"?": ":"><font color=ff8000>"+tar1+"</FONT>: ");
		target3=0;
	}
	document.chat.content.focus();
}


function target2(tar1, tar2)
{
	document.getElementById("target").innerHTML="<font color=0000ff>"+tar1+"</FONT>";
	target1="><font color=0000ff>"+tar1+"</font>: ";
	target3=tar2;
	document.chat.content.focus();
}


