document.toggleLogin = function() {
	var loginBox = document.getElementById("login_div");
	if (loginBox == null) {
		document.loginActivated();
		$(function() {
			$('<div id="login_div" title="Login"><span id="login_label">Login:</span><input id="login_login_input" type="text" cols="30" onkeypress="{if (event.keyCode==13) $(\'#login_password_input\').focus();}"><span id="password_label">Password:</span><input id="login_password_input" type="password" cols="30" onkeypress="{if (event.keyCode==13) document.doLogin();}"><span id="login_links"><a href="#" onclick="document.doLogin()">Login</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="document.toggleLogin()">Cancel</a></span><span id="login_error"></span></div>').dialog({
				modal: true,
				draggable: false,
				resizable: false,
				width: 220,
				height: 150,
				close: function(ui,ev) { var loginDiv = document.getElementById("login_div"); loginDiv.parentNode.removeChild(loginDiv); }
			});
		});
	} else {
		$(function() {
			$("#login_div").dialog('close');
			document.loginDeactivated();
			if (loginBox.parentNode != null) {
				loginBox.parentNode.removeChild(loginBox);
			}
		});
	}
}

document.loginActivated = function() {
}

document.loginDeactivated = function() {
}

document.doLogin = function() {
	var loginBox = document.getElementById("login_div");
	if (loginBox != null) {
		var loginInput = document.getElementById("login_login_input");
		var loginPassword = document.getElementById("login_password_input");
		var login = loginInput.value;
		var password = loginPassword.value;
		var loginError = document.getElementById("login_error");
		if (loginError.childNodes.length == 1) {
			loginError.removeChild(loginError.childNodes[0]);
		}
		if (login == "") {
			loginError.appendChild(document.createTextNode("No username set."));
			return;
		}
		if (password == "") {
			loginError.appendChild(document.createTextNode("No password set."));
			return;
		}
		$.post("/login.jsp", {login:login, password:password}, function(data) {
			if (data.result == true) {
				$(function() {
					$("#login_div").dialog('close');
					var loginBox = document.getElementById("login_div");
					if (loginBox != null) {
						loginBox.parentNode.removeChild(loginBox);
					}
					document.doForumLogin(data.forumUser, data.forumPassword);
				});
			} else {
				document.getElementById("login_error").appendChild(document.createTextNode(data.errorMessage));
			}
		}, "json");
	}
}

document.doLogout = function() {
	
	$.get("/logout.jsp", { }, function(data) {
		if (data.result == true) {
			$(function() {
				document.doForumLogout(data.sessionId);
			});
		}
	}, "json");
}

document.doForumLogout = function(sessionId) {
	$.get('/forum/index.php?action=logout;sesc=' + sessionId, { }, function() {
		forumLoggedOut = true;
		document.updateLogoutState();
	});
}


document.doForumLogin = function(forumUser, forumPassword) {
	$.post("/forum/index.php", {action:"login2", user: forumUser, 'skeleton_passwrd': forumPassword}, function(data2) {
		window.location.reload();
	});
}	

document.doRegistrationApproveForumLogin = function(forumUser, forumPassword) {
	$.post("/forum/index.php", {action:"login2", user: forumUser, 'skeleton_passwrd': forumPassword}, function(data2) {
		// nothing to do here
	});
}

document.updateLogoutState = function() {
	window.location.reload();
}