// This little ballet gets around a repurposed window.onload()
function update_cs_data() {
    var hsjssp = document.getElementById('hsjssp').innerHTML;
    var hubspotutk = cookiejar.fetch('hubspotutk');
    var hsfirstvisit = cookiejar.fetch('hsfirstvisit');

    /* TODO:  If hsfirstvisit is missing, parse it from the <img> tag */

    // If the value is set, we know the page has been fully rendered.
    if (hsjssp.match(/tracking/) !== null) {
        // This will contain our subcookie names and values
        var cookie_values = {};

        // First make sure we have a value for hubspotutk
        if (hubspotutk === null) {
            hubspotutk = window.hsut;

            // If we have neither the cookie nor the raw value, something is very wrong
            if (hubspotutk === null) {
                return null;
            }

            // Remove the dashes so we're left with 32 bytes of hex
            if (hubspotutk.indexOf('-') != -1) {
                hubspotutk = hubspotutk.replace(/\-/g, '');
            }
           
            // Make sure it matches our expected pattern.
            if (hubspotutk.match(/^[0-9a-f]{32}$/) === null) {
                return null;
            }

        }

        // We have a valid code.  Add it to our subcookie list.
        cookie_values.hubspotutk = hubspotutk;

        // Also include first visit data
        if (hsfirstvisit !== null) {
            // Add the first visit value to our subcookie list
            cookie_values.hsfirstvisit = hsfirstvisit;
        }

        // Create the CS cookie, containing everything we found.
        subcookiejar.bake('hubspottrackingcookie',cookie_values,3650,'/','.cloudswitch.com');

        // Check to see if we've been refreshed here by analytics.  If so,
        // bounce back
        var hostname = window.location.hostname;
        var trigger_hostname = "www.cloudswitch.com";

        var current_page = window.location.pathname;
        var trigger_page = "/cshrefresh.html";

        var target_url = null;

        var csh_hostname_prod = "home.cloudswitch.com";
        var csh_hostname_test = "cshtest.cloudswitch.com";
        var csh_hostname_dev = "cshdev.cloudswitch.com";

        // Only bounce back if we're on WWW's CSH refresh page
        if (hostname == trigger_hostname && current_page == trigger_page) {

            // Read the GET argument
            var csh_env = gup('env');

            switch (csh_env) {
                case 'prod':
                    target_url = "https://" + csh_hostname_prod + "/csh/";
                    break;
                case 'test':
                    target_url = "https://" + csh_hostname_test + "/csh/";
                    break;
                case 'dev':
                    target_url = "https://" + csh_hostname_dev + "/csh/";
                    break;
                default:
                    target_url = "https://" + csh_hostname_prod + "/csh/";
                    break;
            }

            if (target_url !== null) {
                window.location = target_url;
            }
        }
    } else {
        // Wait a short while and check for the Hubspot-generated html ('hsjssp' SPAN) data again
        setTimeout('update_cs_data()', 200);
    }

} // End of function update_cs_data()

// Start the process
setTimeout('update_cs_data()', 200);
