var logonCall, logoutCall, GetMessagesCall, SendMessageCall, GetServersCall, GetPersonCall;
var CurrentRoom, APerson, CurrentPerson, CurrentInput;// NickName, 
var datimeout;
/*
function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT"
	}
}

function getCookie(name) {
	var prefix = name + "=";
    var cookieStartIndex = document.cookie.indexOf(prefix);
    if (cookieStartIndex == -1) {
		return null;
	}
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1) {
		cookieEndIndex = document.cookie.length;
		return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
	}
}
*/
function init() {
	service.useService("./Service1.asmx?WSDL","ChatWebService");
	if (typeof daservers != 'undefined') {
		GetServersCall = service.ChatWebService.callService("GetServers"); 
	}
	CurrentInput = 1;
	if (NickName.value != "") {
		NickName.disabled = true;
		bnSelectName.disabled = true;
		if (NewRoom.value == "") {
			 CurrentRoom = "http://suyuan.verylead.cn";
		}
		else {
			 CurrentRoom = NewRoom.value;
		}
		NewRoom.value = "";
		logonCall = service.ChatWebService.callService("ReEnterRoom", NickName.value, CurrentRoom);
	}
	else {
		NickName.focus();
	}
	/*
	var daname = getCookie("NickName");
	if (daname) {
		NickName.value = daname;
		NickName.disabled = true;
		bnSelectName.disabled = true;
		if (NewRoom.value == "") {
			 CurrentRoom = "http://suyuan.verylead.cn";
		}
		else {
			 CurrentRoom = NewRoom.value;
		}
		NewRoom.value = "";
		logonCall = service.ChatWebService.callService("ReEnterRoom", NickName.value, CurrentRoom);
	}
	*/	
}

function SelectName() {
	if (NickName.value == "") return false;
//	setCookie("NickName", NickName.value)
	bnSelectName.disabled = true; 
	NickName.disabled = true;
	if (NewRoom.value == "") {
		 CurrentRoom = "http://suyuan.verylead.cn";
	}
	else {
		 CurrentRoom = NewRoom.value;
	}
	NewRoom.value = "";
	logonCall = service.ChatWebService.callService("EnterRoom", NickName.value, CurrentRoom);
}

function DoLogout() {
	window.clearTimeout(datimeout);
	logoutCall = service.ChatWebService.callService("LeaveRoom", NickName.value);
	datimeout=0;
//	deleteCookie("NickName");
	NickName.value = "";
	NickName.disabled = false;
	NickName.focus();
	bnSelectName.style.display = 'inline';
	bnSelectName.style.visibility = 'visible';
	bnSelectName.disabled = false;
	bnLogout.style.display = 'none';
	bnLogout.disabled = true;
	RoomList.disabled = true;
	NewRoom.disabled = true;
	bnCreateRoom.disabled = true;
	UserInput1.disabled = true;
	UserInput2.disabled = true;
	bnSendMsg.disabled = true;
	PersonList.disabled = true;
	ChatMsgs.disabled = true;
	IsAlerted.disabled = true;
	OptionList.disabled = true;
}

function GetPerson() {
	CurrentPerson=PersonList.value;
	if (PersonList.value) {
		GetPersonCall = service.ChatWebService.callService("GetPerson", CurrentPerson);
	}
	if (CurrentInput == 1)	
		UserInput1.focus();
	else
		UserInput2.focus();
}

function ChangeRoom() {
	CurrentRoom = RoomList.value;
	ChatMsgs.value = "";
	logonCall = service.ChatWebService.callService("ReEnterRoom", NickName.value, CurrentRoom);
}

function CreateRoom() {
	CurrentRoom = NewRoom.value;
	NewRoom.value = "";
	logonCall = service.ChatWebService.callService("ReEnterRoom", NickName.value, CurrentRoom);
}

function ChangeMessageOption() {
	if (CurrentInput == 1) {
		UserInput1.disabled = true;
		UserInput1.style.display = 'none';
		UserInput2.style.display = 'inline';
		UserInput2.style.visibility = 'visible';
		UserInput2.disabled = false;
		UserInput2.focus();
		CurrentInput = 0;
	}
	else {
		UserInput2.disabled = true;
		UserInput2.style.display = 'none';
		UserInput1.style.display = 'inline';
		UserInput1.style.visibility = 'visible';
		UserInput1.disabled = false;
		UserInput1.focus();
		CurrentInput = 1;
	}
}

function SendMsg() { 
	if (CurrentInput == 1) {
		if (UserInput1.value == "") return false;
		bnSendMsg.disabled = true;
		UserInput1.disabled = true;
		SendMessageCall = service.ChatWebService.callService("SendMessage", NickName.value, UserInput1.value, CurrentRoom, CurrentPerson);
	}
	else {
		if (UserInput2.value == "") return false;
		bnSendMsg.disabled = true;
		UserInput2.disabled = true;
		SendMessageCall = service.ChatWebService.callService("SendMessage", NickName.value, UserInput2.value, CurrentRoom, CurrentPerson);
	}
} 

function displayxml(xmlResult) {
	if (event.result.value.PersonArray) {
		var persons = new String(event.result.value.PersonArray);
		var personarray = persons.split(',');
		PersonList.length=personarray.length + 1;
		var selectedperson = 0;
		PersonList.options[0]=new Option();
		PersonList.options[0].text="Message All";
		PersonList.options[0].value="";
		for (var i=0;i < personarray.length; i++) {
			PersonList.options[i+1]=new Option();
			PersonList.options[i+1].text=personarray[i];
			PersonList.options[i+1].value=personarray[i];
			if (CurrentPerson == personarray[i]) {
				selectedperson = i+1;
			}
		}
		PersonList[selectedperson].selected = true;
	}
	if (event.result.value.RoomArray) {
		var rooms = new String(event.result.value.RoomArray);
		var roomarray = rooms.split(',');
		if (RoomList.length != roomarray.length) {
			var selectedroom = 0;
			RoomList.length=roomarray.length;
			for (var i=0; i < roomarray.length; i++) {
				RoomList.options[i]=new Option();
				RoomList.options[i].text=roomarray[i];
				RoomList.options[i].value=roomarray[i];
				if (CurrentRoom == roomarray[i]) {
					selectedroom = i;
				}
			}
			RoomList[selectedroom].selected = true;
		}
	}
	if (event.result.value.Messages) {
		if (IsAlerted.checked) {
			window.focus();
		}
		if (CurrentInput == 1) 
			UserInput1.focus();
		else
			UserInput2.focus();		
		ChatMsgs.value = ChatMsgs.value + unescape(event.result.value.Messages); // "\n" +
		ChatMsgs.scrollTop = 2000;
	}
}

function onmyresult() { 
	if (event.result.error) {
		ChatMsgs.value = "___________\n" + event.result.errorDetail.string + "\n___________";//'Fault Code: '+event.result.errorDetail.code+'\nFault String: '+event.result.errorDetail.string+'\nSOAP Data: '+event.result.errorDetail.raw + "\n" +ChatMsgs.value;
		NickName.disabled = false;
		bnSelectName.disabled = false;
		UserInput1.disabled = false; 
		UserInput2.disabled = false;
	}
	else if(logonCall==event.result.id) { 
		NickName.disabled = true;
		bnSelectName.style.display = 'none';
		bnLogout.style.display = 'inline';
		bnLogout.style.visibility = 'visible';
		bnLogout.disabled = false;
		RoomList.disabled = false;
		NewRoom.disabled = false;
		PersonList.disabled = false;
		bnCreateRoom.disabled = false;
		IsAlerted.disabled = false;
		OptionList.disabled = false;
		if (CurrentInput == 1) {
			UserInput1.value = ""; 
			UserInput1.disabled = false; 
			UserInput1.focus();
		}
		else {
			UserInput2.value = ""; 
			UserInput2.disabled = false; 
			UserInput2.focus();
		}
		bnSendMsg.disabled = false;
		ChatMsgs.disabled = false;
		var xmlResult = event.result.raw.xml;
		if (xmlResult != "" && xmlResult != null) {
			displayxml(xmlResult);
		}
		datimeout = window.setTimeout('GetMessagesCall = service.ChatWebService.callService("GetMessages", NickName.value);',3000);
	} 
	else if(GetMessagesCall==event.result.id) {
		var xmlResult = event.result.raw.xml; 
		if (xmlResult != "" && xmlResult != null) {
			displayxml(xmlResult);
		}
		datimeout = window.setTimeout('GetMessagesCall = service.ChatWebService.callService("GetMessages", NickName.value);',3000);
	}
	else if(SendMessageCall==event.result.id) {
		var xmlResult = event.result.raw.xml; 
		if (xmlResult != "" && xmlResult != null) {
			if (CurrentInput == 1) {
				UserInput1.value = ""; 
				UserInput1.disabled = false; 
				UserInput1.focus();
			}
			else {
				UserInput2.value = ""; 
				UserInput2.disabled = false; 
				UserInput2.focus();
			}
			bnSendMsg.disabled = false;
			displayxml(xmlResult);
		}
	}
	else if(GetServersCall==event.result.id) {
		var serverslist = new String(event.result.value);
		var serversarray = serverslist.split(',')
		for (var i=0;i < serversarray.length; i++) {
			daservers.value += serversarray[i] + "\n";
		}
	}
	else if (GetPersonCall==event.result.id) {
		ChatMsgs.value = ChatMsgs.value + unescape(event.result.value.NickName) + "#"  + unescape(event.result.value.CurrentRoom) + "@" + unescape(event.result.value.ServerURL) + " at " + unescape(event.result.value.LastAccessTime) + "\n";
		ChatMsgs.scrollTop = 2000;
	}
} 
