

	/**
	 * Determines whether the user is logged into facebook and retrieves their Facebook ID;
	 */
	function CheckFacebookLogin() {
		var uid = "";
		if (FB.Facebook.get_sessionState()) {
			uid = FB.Facebook.get_sessionState().result.uid;
		}
		
		return uid;
	}
	
	/**
	 * Logs in the user with the system via AJAX 
	 */
	function LoginWithFacebook() {
		
		FB.ensureInit(function() {
			FB.Facebook.get_sessionState().waitUntilReady(function(session) {
				var is_now_logged_into_facebook = session ? true : false;
				
				var uid = CheckFacebookLogin();
				
				if (uid != "") {
					$.post("/services/ajaxFacebook.ashx","f=LOGIN&uid=" + uid, function(data) {
						if (data == "True") {
							RefreshPage();
						}
					});
				}
	        });
	    });
	}
	
	/** 
	 * Function that runs after authenticating with Facebook:Connect in the Login module.
	 */
	function facebook_onlogin_ready(){
		LoginWithFacebook();
	}
	
	/**
	 * Function that runs after authenticating with Facebook:Connect in the Registration module
	 */
	function facebook_connect_id() {
		FB.ensureInit(function() {
			FB.Facebook.get_sessionState().waitUntilReady(function(session) {
				var is_now_logged_into_facebook = session ? true : false;
				
				var uid = CheckFacebookLogin();
				
				if (uid != "") {
					$(".facebook-id").val(uid);
				}
	        });
	    });
	}
	
	/**
	 * Reloads the current page;
	 */
	function RefreshPage() {
		window.location.reload();
	}
