function flip_login_form_visibility() {
	flip_visibility("overlay");
	flip_visibility("login_form");
  }


function show_login_form(post_login_callback) {
	flip_login_form_visibility() ;
	showOverlay();
	btn = document.getElementById("login_button");
    btn.loggedinCallback = post_login_callback;
}


/**
 * If user already logged in, then the callback will be called immediately with user id as its parameter.
   Otherwise user will be asked to log in using his password, if he successfully logs in, then the callback will be called
 */
function secure_async_call(post_login_callback) {
	
}

function secure_async_call(post_login_callback) {
	UserAuthenticator.authenticate( function (user) {
		if (user) {
			//alert("logged in as" + result);
			post_login_callback(user);
		} else {
			// popup login box
			show_login_form(post_login_callback);
		}
	});
}

function logout() {
	UserAuthenticator.logout( function () {
		alert("You have been logged out");
	});
}


function submit_password(btn) {
	u = document.getElementById("username");
	p = document.getElementById("password");
	c = document.getElementById("rememberMe");
	UserAuthenticator.login(u.value, p.value, c.checked, function(result) {
		if (result) {
			flip_login_form_visibility() ;
			clearLoginError();
			btn.loggedinCallback(u.value);
		} else {
			showLoginError();
		}
	}) ;
}


function showLoginError() {
	// show login failed
	msg = document.getElementById("loginErrMsg");
	flipDisplayState(msg,true);
	// hide login needed
	msg = document.getElementById("loginNeededMsg");
	flipDisplayState(msg,false);
}

function clearLoginError() {
	// show login failed
	msg = document.getElementById("loginErrMsg");
	flipDisplayState(msg,false);
	// hide login needed
	msg = document.getElementById("loginNeededMsg");
	flipDisplayState(msg,true);
}