    var apikey = "49112de41e952cc75bb6af0842536f6c";
    var sitedomain = "bridalpartytees.com";
    var sitedomainproper = "BridalPartyTees.com";

    var loginWidget = null;
    var logoutWidget = null;
    var postDesignWidget = null;
    var itemTypeSource = null;
    var designIdSource = null;
    var designNameSource = 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 postImageExists = 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." + sitedomain + "/xd_receiver_ssl.htm"
    }
    else
    {
        receiverlink = "http://www." + sitedomain + "/xd_receiver.htm"
    }

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

        function()
        {
            FB.init( apikey, 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 deisgn name will be fetched from during a
    //      Facebook posting (if it's even used)
    function setDesignNameSource( widget )
    {
        designNameSource = 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;
            apply_tax();
        }
        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 postToFacebook( imageExists )
    {
        postImageExists = imageExists;

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

            FB.Connect.requireSession( loginDone );
        }
    }

    function openFeedDialog()
    {
        var caption;

        //----- Set the caption text to either the 'made a design', 'shared a design', or design name type of post
        if ( postType == 0 )
        {
            caption = "{*actor*} Made a Custom " + itemTypeSource.value + " on " + sitedomainproper + "!";
        }
        else if ( postType == 1 )
        {
            caption = "{*actor*} Shared a Custom " + itemTypeSource.value + " on " + sitedomainproper + "!";
        }
        else
        {
            caption = designNameSource.value;
        }

        var imageUrl;

        if ( postImageExists )
        {
            imageUrl = "http://static." + sitedomain + "/images/design/" + designIdSource.value + "_0_swatch.gif";
        }
        else
        {
            imageUrl = "http://www." + sitedomain + "/cgi-bin/showimg.cgi?did=" + designIdSource.value + "&swatch=1";
        }

        //----- Instantiate the facebook attachment data
        var attachment = {
            "name" : sitedomainproper,
            "href" : "http://www." + sitedomain,
            "caption" : caption,
            "media" : [ {
                    "type" : "image",
                    "src" : imageUrl,
                    "href" : "http://www." + sitedomain + "/design/" + designIdSource.value
                } ]
        };

        var actionLinks = [{ "text": "Customize It", "href" : "http://www." + sitedomain + "/design/" + designIdSource.value }];
 
        //----- Open the dialog
        FB.Connect.streamPublish( null, attachment, actionLinks, null, null, postDone, false, null );
    }

    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;
        }
    }

