/*Version Date: 2009-12-15*/var api_key = "8a1cbe7e0b31c80fce2abccab3b2e005";var signinFacebookPagelet = "/assets/pagelet/signin_facebook.cfm";var thisUserData;function facebook_onLogin() {	FB.Facebook.apiClient.fql_query("SELECT uid, username, name, pic_with_logo, hometown_location, locale, proxied_email FROM user WHERE uid=" + FB.Facebook.apiClient.get_session().uid, function(userData) {        thisUserData = userData;		$.ajax({			type: "GET",			url: signinFacebookPagelet,			data: "action=checkUserExist&uid=" + userData[0].uid + "&proxied_email=" + encodeURIComponent(userData[0].proxied_email),			success: function(data) {				data = $.trim(data);				if (data == "true") {					//the user id and proxy email combination already exist in the database					$.ajax({						type: "GET",						url: signinFacebookPagelet,						data: "action=signin&uid=" + userData[0].uid + "&proxied_email=" + encodeURIComponent(userData[0].proxied_email),						success: function(data) {							data = $.trim(data);							if (data == "true") {								window.location.reload(true);							}							else {								alert("An error occured while signing you into Longtable.\nAre you sure that you're signed into Facebook?");							}						}					});				}				else {					//the user id and proxy email combination doesn't already exist					//pop up a facebox to get the user's email and username					$.facebox(function() {						$.get(signinFacebookPagelet + "?action=getDetailsForm", function(data) {							data = $.trim(data);							$.facebox(data);							$("#getFacebookDetails_uid").val(userData[0].uid);							$("#mergeAccounts_uid").val(userData[0].uid);							$("#getFacebookDetails_username").val(userData[0].username);							$("#getFacebookDetails").bind("submit", function() {								if ($("#getFacebookDetails").valid()) {									$.ajax({										type: "GET",										url: signinFacebookPagelet,										data: "action=setDetails&uid=" + $("#getFacebookDetails_uid").val() + "&username=" + $("#getFacebookDetails_username").val() + "&email=" + $("#getFacebookDetails_email").val() + "&name=" + userData[0].name + "&country=" + userData[0].locale + "&proxied_email=" + encodeURIComponent(userData[0].proxied_email),										success: function(data) {											data = $.trim(data);											if (data == "true") {												//the new user was added successfully												window.location.reload(true);											}											else {												alert("An error occured while connecting your Facebook account to Longtable. Please refresh the page and try again.");											}										}									});								}							});						});					});				}			}		});	});}function merge() {    $("#mergeAccounts").validate({        rules: {            mergeAccounts_email: {                required: true,                email: true            },            mergeAccounts_password: {                required: true,                minlength: 6            }        },        messages: {            mergeAccounts_email: {                required: "We need you to provide the email address that you use with Longtable",                email: "Incorrect email address format, please check your entry"            },            mergeAccounts_password: {                required: "We need you to provide your Longtable password to merge your accounts",                minlength: "Your Longtable password is at least 6 characters"            }        }    });    var uid = thisUserData[0].uid;    var proxied_email = thisUserData[0].proxied_email;    var email = $("#mergeAccounts #mergeAccounts_email").val();    var password = $("#mergeAccounts #mergeAccounts_password").val();    if ($("#mergeAccounts").valid()) {        insertLoadingImage("merging");        $("#facebookDialog #merging").show();        $.ajax({            type: "GET",            url: signinFacebookPagelet,            data: "action=merge&uid=" + encodeURIComponent(uid) + "&proxied_email=" + encodeURIComponent(proxied_email) + "&email=" + encodeURIComponent(email) + "&password=" + encodeURIComponent(password),            success: function(data) {                data = $.trim(data);                if (data == "true") {                    //the accounts merged successfully, the user is now logged in                    $("#facebookDialog #merging").html("<div style='font-weight:bold;'>Your Longtable and Facebook accounts were merged successfully!</div><div><p>This means that you can now sign into your existing Longtable account by clicking the 'Connect with Facebook' link at the top of the page, and entering your Facebook login details.</p><p style='font-weight:bold;'>This page will reload in 10 seconds, or when you close this box.</p></div>");                    //reload the page in 10 seconds                    setTimeout("window.location.reload(true)", 10000);                    //when the user closes the facebox, also reload the page                    $("#facebox .close_image").click(function() {                        window.location.reload(true);                    });                }                else {                    //an error occured, email/password combination wrong or a session problem                    $("#facebookDialog #merging").html("<div style='font-weight:bold;'>An error occured while merging your accounts</div><div>This is most likely due to you entering your Longtable account details incorrectly. Please re-enter your email address and password and try again.</div>");                }            }        });    }}$(document).ready(function() {	$("#logOut").click(function(event) {		//sign the user out of facebook, when this function completes, the browser will continue onwards to		//the actual page to sign the user out of longtable		//grab the link that generated this event		var self = $(this);		//stop the link from re-directing the browser		event.preventDefault();		//log the user out of facebook		FB.Connect.logout(function() {            //and when the facebook function is done, direct the browser to the page in the link's "href" attribute            document.location = self.attr("href");        });	});});