    document.write("<fb:login-button size='medium' autologoutlink='true' onclick='onConnectClick();' onlogin='DelayLogin();'></fb:login-button>");
//    document.write("<fb:login-button size='medium' autologoutlink='true' onclick='onConnectClick();' onlogin='onLogin(\"\");'></fb:login-button>");

    function DelayLogin() {
      setTimeout("onLogin('');", 1000); // avoid a race condition
    }

    function onConnectClick() {
      if (GetCookie("fb") != "") { // user is logged in, so this is a logout call
        onLogout();
      }
    }

    function onLogout() {
      setTimeout("ClearFacebookFrame()", 3000); // clear the facebook frame after 3 seconds
      SetCookie("fb", "");
//    if (GetCookie("passport").indexOf("@") == -1) { // REAP login is facebook uid
        SetCookie("passport", "");
//    }
    }

    function onLogin(uid) {
      if (uid == "") { // call is from onlogin event handler
        uid = GetCookie(api_key + "_user");
        if (!iframeInitialized) {
          // would like to do
          //    GetIframe();
          // but this gives a js error on the line in GetIframe that reads:
          //    window.frames.myframe.document.location.replace(url);
          // so we reload the page instead
          document.location.replace(document.location);
        }
      }
      Set24HourCookie("fb", uid);
      if (GetCookie("passport") == "") {
        Set24HourCookie("passport", uid); // log him in using uid as his email address
      }
    }

    function ClearFacebookFrame() {
      var code = "<br><br><a href='javascript:parent.InviteMore();'>Invite your friends to join REAP</a>";
      document.getElementById("facebook").innerHTML = code;
      iframeInitialized = false;
    }

    function InitializeFacebookFrame() {
      FB_RequireFeatures(["XFBML"], function()
      {
        FB.Facebook.init(api_key, channel_path);
        FB.Facebook.get_sessionState().waitUntilReady(function()
        {
          var api = FB.Facebook.apiClient;

          // get user information

          uid = api.get_session().uid;
          onLogin(uid);
          var myinfo='last_name,first_name,hometown_location,work_history,pic_small';
          api.users_getInfo(uid, myinfo, function(result, ex) {
            ln = result[0]['last_name'];
            fn = result[0]['first_name'];
            pic = result[0]['pic_small'];

            if (++count == 2) {
              GetIframe();
            }
          });

        // get list of friends

          api.friends_get(null, function(result, ex) {
            friends = "-"
            var j = Math.floor(Math.random() * (result.length-1));
            for (i=0; i<result.length; i++) {
              var k = i - (-j);
              if (k >= result.length) {
                k -= result.length;
              }
              friends += result[k] + "-";
            }
            if (++count == 2) {
              GetIframe();
            }
          });
        });
      });
    }

    // create an iframe

    document.write("<div id='facebook'>&nbsp;</div>");

    var channel_path = "xd_receiver.htm";
    var count = 0; // so we know when both callbacks have completed
    var iframeInitialized = false;

    var user = GetCookie(api_key + "_user");
    if (user == "") { // no facebook connection so clear the facebook frame
      ClearFacebookFrame();
    }

    var friends = "";
    var ln = "";
    var fn = "";
    var pic = "";
    var uid = "";

    InitializeFacebookFrame();

    function GetIframe() {

      if (iframeInitialized) {
        return;
      }
      // create an iframe

      code = "<iframe name='myframe' frameborder='" + fb_border +
                   "' height='" + fb_height + "' width='" + fb_width + "'></iframe>";
//      code = "<iframe name='myframe' frameborder='0' height='525' width='200'></iframe>";
      document.getElementById("facebook").innerHTML = code;

      // fill in the iframe

      SetCookie("friends", friends); // to large to pass as query parameter in IE
      var url = "http://reapinfo.org/fb.php?" +
//        "friends=" + friends +
        "&fn=" + fn +
        "&ln=" + ln +
        "&pic=" + pic +
        "&uid=" + uid +
        "&reapFriendsLimit=" + fb_reapFriendsLimit +
        "&reapFriendsDirection=" + fb_reapFriendsDirection;

      window.frames.myframe.document.location.replace(url);
      iframeInitialized = true;
    }

    function Invite() {
      var friends = friendsToInvite; // too large to pass in as a function argument because of IE bug
      SetCookie("friends", friends); // too large to pass out as query parameter because of IE bug
      window.open(
        "fb_invite.html",
//        "fb_invite.html?friends=" + friends,
        "",
        "width=550,height=340,status=0,location=0,resizable=1,scrollbars=1");
    }

    function FriendsOnSite(friends) {
      window.open(
        "fb_friendsonsite.html?friends=" + friends,
        "",
        "width=550,height=340,status=0,location=0,resizable=1,scrollbars=1");
    }

    function InviteMore() {
      var fromName = fn + " " + ln;
      window.open(
        "fb_invitemore.html?fromName=" + fromName,
        "",
        "width=380,height=360,status=0,location=0,resizable=1,scrollbars=1");
    }

    function Notify(friendID, friendName) {
      FB_RequireFeatures(["XFBML"], function()
      {
        FB.Facebook.init(api_key, channel_path);
        FB.Facebook.get_sessionState().waitUntilReady(function()
        {
          var api = FB.Facebook.apiClient;
/*
          // send an email

          api.notifications_sendEmail(friendID, "email subject", "email text", "", function(result, ex) {
            alert (friendName + " has been invited");
          });
*/

          // send a facebook notification

          var message = "I think you might like to join REAP.  It is at <a href='http://reapinfo.org'>http://reapinfo.org</a>";
          api.notifications_send(friendID, message, function(result, ex) {
            alert (friendName + " has been invited");
          });

        });
      });
    }
