    var loginWidget = null;
    var logoutWidget = null;
    var postDesignWidget = null;
    var itemTypeSource = null;
    var designIdSource = null;
    var additionalTextSource = null;
    var loginRedirectUrl = null;
    var logoutRedirectUrl = null;
    var designPostRedirectUrl = null;
    var designPostSubmitForm = null;

    var connected = false;
    var postType = 1;
    var openFeedAfterLogin = false;

    var api = null;
    var firstNameSource = null;
    var lastNameSource = null;
    var citySource = null;
    var stateSource = null;
    var countrySource = null;
    var zipSource = null;

    var stateMap = {
"Alabama":"AL",
"Alaska":"AK",
"Arizona":"AZ",
"Arkansas":"AR",
"California":"CA",
"Colorado":"CO",
"Connecticut":"CT",
"Delaware":"DE",
"District of Columbia":"DC",
"Florida":"FL",
"Georgia":"GA",
"Hawaii":"HI",
"Idaho":"ID",
"Illinois":"IL",
"Indiana":"IN",
"Iowa":"IA",
"Kansas":"KS",
"Kentucky":"KY",
"Louisiana":"LA",
"Maine":"ME",
"Maryland":"MD",
"Massachusetts":"MA",
"Michigan":"MI",
"Minnesota":"MN",
"Mississippi":"MS",
"Missouri":"MO",
"Montana":"MT",
"Nebraska":"NE",
"Nevada":"NV",
"New Hampshire":"NH",
"New Jersey":"NJ",
"New Mexico":"NM",
"New York":"NY",
"North Carolina":"NC",
"North Dakota":"ND",
"Ohio":"OH",
"Oklahoma":"OK",
"Oregon":"OR",
"Pennsylvania":"PA",
"Rhode Island":"RI",
"South Carolina":"SC",
"South Dakota":"SD",
"Tennessee":"TN",
"Texas":"TX",
"Utah":"UT",
"Vermont":"VT",
"Virginia":"VA",
"Washington":"WA",
"West Virginia":"WV",
"Wisconsin":"WI",
"Wyoming":"WY"
    };

    var receiverlink;

    if ( window.location.protocol == 'https:' )
    {
        receiverlink = "https://www.customizedgirl.com/xd_receiver_ssl.htm"
    }
    else
    {
        receiverlink = "http://www.customizedgirl.com/xd_receiver.htm"
    }

    FB_RequireFeatures( ["XFBML", "Common"],

        function()
        {
            FB.init( "e16c509d6f738a83715888bb6178810d", receiverlink );
            FB.ensureInit (

                function()
                {
                    //----- Update the user's connected status
                    FB.Connect.ifUserConnected( isConnected, isNotConnected );
                }
            ); 
        }
    );

    //----- This sets the widget that will cause a Facebook login dialog to pop up when clicked.
    //      It will be displayed when the user is logged out, and hidden when the user is logged in
    function setLoginWidget( widget )
    {
        loginWidget = widget;
        loginWidget.onclick = function() { loginToFacebook(); return false; };
    }

    //----- This sets the widget that will cause a Faceboook 'post design' dialog to pop up when
    //      clicked.  It will be displayed when the user is logged in, and hidden when the user
    //      is logged out
    function setLogoutWidget( widget )
    {
        logoutWidget = widget;
        logoutWidget.onclick = function() { logoutOfFacebook(); return false; };
    }

    //----- This function sets the widget that will cause a Facebook logout to occur when clicked.
    //      It will be displayed when the user is logged in, and hidden when the user is logged out
    function setPostDesignWidget( widget )
    {
        postDesignWidget = widget;
        postDesignWidget.onclick = function() { postToFacebook(); return false; };
    }

    //----- This sets the input / hidden-input that the item type description 'i.e. ringer tee'
    //      will be fetched from during a Facebook posting
    function setItemTypeSource( widget )
    {
        itemTypeSource = widget;
    }

    //----- This sets the input / hidden-input that the item ID will be fetched from during a
    //      Facebook posting
    function setDesignIdSource( widget )
    {
        designIdSource = widget;
    }

    //----- This sets the input / hidden-input that any additional text will be fetched from during a
    //      Facebook posting
    function setAdditionalTextSource( widget )
    {
        additionalTextSource = widget;
    }

    //----- This sets the URL to redirect to after a user successfully logs in, if any
    function setLoginRedirectUrl( url )
    {
        loginRedirectUrl = url;
    }

    //----- This sets the URL to redirect to after a user successfully logs out, if any
    function setLogoutRedirectUrl( url )
    {
        logoutRedirectUrl = url;
    }

    //----- This sets a URL to redirect to after a user successfully posts a design, if any
    function setDesignPostRedirectUrl( url )
    {
        designPostRedirectUrl = url;
    }

    //----- This sets a form to submit after a user successfully posts a design, if any
    function setDesignPostSubmitForm( form )
    {
        designPostSubmitForm = form;
    }

    //----- Sets the post type.  0 = 'made a design' post type, 1 = 'shared a design' post type
    function setPostType( type )
    {
        postType = type;
    }

    function setFirstnameSource( widget )
    {
        firstNameSource = widget;
    }

    function setLastnameSource( widget )
    {
        lastNameSource = widget;
    }

    function setCitySource( widget )
    {
        citySource = widget;
    }

    function setStateSource( widget )
    {
        stateSource = widget;
    }

    function setCountrySource( widget )
    {
        countrySource = widget;
    }

    function setZipSource( widget )
    {
        zipSource = widget;
    }

    function populateFacebookInfo()
    {
        var result;

        api = FB.Facebook.apiClient;

        result = api.users_getLoggedInUser( populateFacebookInfoPart2 );
    }

    function populateFacebookInfoPart2( result, exception )
    {
        if ( result != undefined )
        {
            api.users_getInfo( [ result ], [ "first_name", "last_name", "current_location" ], populateFacebookInfoPart3 );
        }
    }

    function populateFacebookInfoPart3( result, exception )
    {
        var firstname, lastname, city, state, country, zip, statecode;

        firstname = result[0]["first_name"];
        lastname = result[0]["last_name"];
        city = result[0]["current_location"]["city"];
        state = result[0]["current_location"]["state"];
        country = result[0]["current_location"]["country"];
        zip = result[0]["current_location"]["zip"];

        statecode = stateMap[state];

        if ( ( firstname != undefined ) && ( firstNameSource != null ) && ( ( firstNameSource.value == "" ) ) )
        {
            firstNameSource.value = firstname;
        }
        if ( ( lastname != undefined ) && ( lastNameSource != null ) && ( ( lastNameSource.value == "" ) ) )
        {
            lastNameSource.value = lastname;
        }
        if ( ( city != undefined ) && ( citySource != null ) && ( ( citySource.value == "" ) ) )
        {
            citySource.value = city;
        }
        if ( ( statecode != undefined ) && ( stateSource != null ) && ( ( stateSource.value == "" ) ) )
        {
            stateSource.value = statecode;
        }
        if ( ( country != undefined ) && ( countrySource != null ) && ( ( countrySource.value == "United States" ) ) )
        {
            countrySource.value = country;
        }
        if ( ( zip != undefined ) && ( zipSource != null ) && ( ( zipSource.value == "" ) ) )
        {
            zipSource.value = zip;
        }
    }

    function isConnected( gid )
    {
        if ( loginWidget )
        {
            loginWidget.style.display = "none";
        }
        if ( logoutWidget )
        {
            logoutWidget.style.display = "inline";
        }
        if ( postDesignWidget )
        {
            postDesignWidget.style.display = "inline";
        }

        connected = true;
    }

    function isNotConnected()
    {
        if ( loginWidget )
        {
            loginWidget.style.display = "inline";
        }
        if ( logoutWidget )
        {
            logoutWidget.style.display = "none";
        }
        if ( postDesignWidget )
        {
            postDesignWidget.style.display = "none";
        }

        connected = false;
    }

    function loginToFacebook()
    {
        if ( !connected )
        {
            FB.Connect.requireSession( loginDone );
        }
    }

    function logoutOfFacebook()
    {
        if ( connected )
        {
            FB.Connect.logout( logoutDone );
        }
    }

    function setDesignPostParameters( itemType, designId, additionalText )
    {
        if ( itemTypeSource != null )
        {
            itemTypeSource.value = itemType;
        }
        if ( designIdSource != null )
        {
            designIdSource.value = designId;
        }
        if ( additionalTextSource != null )
        {
            additionalTextSource.value = additionalText;
        }
    }

    function postToFacebook()
    {
        if ( connected )
        {
            openFeedDialog();
        }
        else
        {
            openFeedAfterLogin = true;

            FB.Connect.requireSession( loginDone );
        }
    }

    function openFeedDialog()
    {
        //----- Instantiate the facebook comment data
        var commentDataMap = {
            "itemtype" : itemTypeSource.value,
            "designid" : designIdSource.value,
            "images" : new Array (
                {
                    "src" : "http://www.customizedgirl.com/cgi-bin/showimg.cgi?did=" + designIdSource.value,
                    "href" : "http://www.customizedgirl.com/design/" + designIdSource.value,
                    "height" : "130",
                    "width" : "130"
                }
            )
        };

        var templateId;

        //----- Set the template ID to either the 'made a design' or 'shared a design' type of post
        if ( postType == 0 )
        {
            templateId = "110966742409";
        }
        else
        {
            templateId = "110967267409";
        }
 
        //----- Open the dialog
        FB.Connect.showFeedDialog( templateId, commentDataMap, null, null, null, FB.RequireConnect.promptConnect, postDone, null, additionalTextSource );
    }

    function postDone()
    {
        if ( designPostRedirectUrl != null )
        {
            window.location = designPostRedirectUrl;
        }
        else if ( designPostSubmitForm != null )
        {
            designPostSubmitForm.submit();
        }
    }

    function loginDone()
    {
        if ( loginRedirectUrl != null )
        {
            window.location = loginRedirectUrl;
        }
        else if ( openFeedAfterLogin )
        {
            openFeedDialog();
        }
    }

    function logoutDone()
    {
        if ( logoutRedirectUrl != null )
        {
            window.location = logoutRedirectUrl;
        }
    }
