function formIsValid() {
	// check to make sure a valid username has been entered
	if ( document.login.username.value == '' ) {
		alert('Please input a username.');
		return false;
	}

	return true;
}

// a small poupup window to show who's in the chat at the current time
function showInfo() {
	// the size of the popup window
	var width = 400;
	var height = 300;


	// the x,y position of the popup window
	// NOTE: this formula will auto-center the popup on the screen
	var y = (screen.height - height) / 2;
	var x = (screen.width - width) / 2;

	var session_inst = 1;
	if(document.login.session_inst != undefined)
	{
	 session_inst = document.login.session_inst.value;
	}
	var url = 'http://jiwang.la/plugins/chat/info.php?session_inst=' + session_inst;
	var options = 'width=' + width + ',height=' + height + ',top=' + y + ',left=' + x + ',resizable';

	// open the info window as a popup, instead of embedded in webpage
	window.open( url, 'info', options );
}

function basicLogin() {
	if (formIsValid()) document.login.submit();
}
function changeConf(chat,instances,selectchat,selectinst)
{

	var i;
	var start = 0;
	var index;
	var newObj;
	var str;
	var end;

	while( 0 < 1)
	{
		if ( chat.indexOf(selectchat.value,start) != -1)
			if ( chat.charAt(chat.indexOf(selectchat.value,start)+1) == "|" )
			{
				index = chat.indexOf(selectchat.value,start);
				break;
			}
			else
				start = chat.indexOf(selectchat.value,start)+1;
		else
			break;
	}


	for( i = selectinst.length ; i >= 0 ; i-- )
		selectinst.remove(i);

	start = 0;
	var k;
	k = 0;
	index = index + 2;
	for( i = index ; i < chat.indexOf(";",index) ; i++ )
	{
		end = chat.indexOf(",",index);
		if ( end > chat.indexOf(";",index) || end == -1 )
			end = chat.indexOf(";",index);


		start = instances.indexOf(chat.substring(index,end));
		if (start != -1)
		{
			newObj = document.createElement("OPTION");
			start = start + 2;
			newObj.text = instances.substring(start,instances.indexOf(";",start));
			newObj.value = chat.substring(index,end);

			if ( navigator.userAgent.indexOf("Firefox") != -1)
				selectinst.add(newObj,null);
			else
				selectinst.add(newObj,k);
			k++;
		}
		else
			break;


		if (chat.charAt(chat.substring(index,end).length+index) == ";")
			break;


		index = chat.indexOf(",",index) + 1;

	}
}

function popupLogin() {
	// check to make sure a valid username has been entered
	if (!formIsValid()) return;

	var username = document.login.username.value;
	var password = document.login.password.value;
	var lang = document.login.lang.value;

	// the size of the popup window
	var width = 800;
	var height = 600;

	// the x,y position of the popup window
	// NOTE: this formula will auto-center the popup on the screen
	var y = (screen.height - height) / 2;
	var x = (screen.width - width) / 2;

	var session_inst = 1;
	if(document.login.session_inst != undefined)
	{
	 session_inst = document.login.session_inst.value;
	}

	var url = 'http://jiwang.la/plugins/chat/flashchat.php?username=' + username + '&password=' + password + '&lang=' + lang+'&session_inst='+session_inst;
	var options = 'width=' + width + ',height=' + height + ',top=' + y + ',left=' + x + ',resizable';

	// open the chat window as a popup, instead of embedded in webpage
	window.open( url, 'chat', options );
}


