
function parseAnalyticsCookie() {
    // inspiration from http://stackoverflow.com/questions/1688657/how-do-i-extract-google-analytics-campaign-data-from-their-cookie-with-javascript
    // readCookie is from // http://www.quirksmode.org/js/cookies.html
    // utmcsr = utm_source
    // utmccn = utm_campaign
    // utmcmd = utm_medium
    // utmctr = utm_term
    // utmcct = utm_content
    var values = {};
    var cookie = readCookie("__utmz");
    if (cookie) {
        var z = cookie.split('.');
        if (z.length >= 4) {
            var y = z[4].split('|');
            for (i = 0; i < y.length; i++) {
                var pair = y[i].split("=");
                values[pair[0]] = pair[1];
            }
        }
    }
    return values;
}



//
// Script posted on http://cutroni.com/blog/2009/03/18/updated-integrating-google-analytics-with-a-crm/comment-page-1/
// 
// This is a function that I "borrowed" from the urchin.js file.
// It parses a string and returns a value.  I used it to get
// data from the __utmz cookie
//
function _uGC(l, n, s) {
    if (!l || l == "" || !n || n == "" || !s || s == "") return "-";
    var i, i2, i3, c = "-";
    i = l.indexOf(n);
    i3 = n.indexOf("=") + 1;
    if (i > -1) {
        i2 = l.indexOf(s, i); if (i2 < 0) { i2 = l.length; }
        c = l.substring((i + i3), i2);
    }
    return c;
}

function gaCookies() {
    // 
    // Get the __utmz cookie value. This is the cookies that 
    // stores all campaign information. 
    // 
    var z = _uGC(document.cookie, '__utmz=', ';');
    // 
    // The cookie has a number of name-value pairs. 
    // Each identifies an aspect of the campaign. 
    // 
    // utmcsr  = campaign source 
    // utmcmd  = campaign medium 
    // utmctr  = campaign term (keyword) 
    // utmcct  = campaign content  
    // utmccn  = campaign name 
    // utmgclid = unique identifier used when AdWords auto tagging is enabled 
    // 
    // This is very basic code. It separates the campaign-tracking cookie 
    // and populates a variable with each piece of campaign info. 
    // 
    this.source = _uGC(z, 'utmcsr=', '|');
    this.medium = _uGC(z, 'utmcmd=', '|');
    this.term = _uGC(z, 'utmctr=', '|');
    this.content = _uGC(z, 'utmcct=', '|');
    this.campaign = _uGC(z, 'utmccn=', '|');
    this.gclid = _uGC(z, 'utmgclid=', '|');
    // 
    // The gclid is ONLY present when auto tagging has been enabled. 
    // All other variables, except the term variable, will be '(not set)'. 
    // Because the gclid is only present for Google AdWords we can 
    // populate some other variables that would normally 
    // be left blank. 
    // 
    if (this.gclid != "-") {
        this.source = 'google';
        this.medium = 'cpc';
    }

    // Data from the custom segmentation cookie can also be passed 
    // back to your server via a hidden form field 
    var csegment = _uGC(document.cookie, '__utmv=', ';');
    if (csegment != '-') {
        var csegmentex = /[1-9]*?\.(.*)/;
        csegment = csegment.match(csegmentex);
        this.csegment = csegment[1];
    } else {
        this.csegment = '(not set)';
    }

    //
    // One more bonus piece of information.  
    // We're going to extract the number of visits that the visitor
    // has generated.  It's also stored in a cookie, the __utma cookis
    // 
    var a = _uGC(document.cookie, '__utma=', ';');
    var aParts = a.split(".");

    //this.nVisitor = a;
    this.domainhash = aParts[0]; // Domain Hash
    this.visitorid = aParts[1]; // Visitor ID
    this.firstvisit = aParts[2]; // Initial visit time stamp
    this.previousvisit = aParts[3]; // Previous visit time stamp
    this.currentvisit = aParts[4]; // Current visit time stamp
    this.totalvisits = aParts[5]; // number of visits
}


function populateHiddenFields(e, f) {
    var cookie = new gaCookies();

    if (cookie.term != "-") {
        $(e).hide();
        //$(f).value = cookie.source + ' ' + cookie.medium + ' - ' + cookie.term;
        $(f).remove();
        $(e).append("<input type='hidden' name='" + f.replace("#", "") + "' id='" + f.replace("#", "") + "' value='" + cookie.source + ' ' + cookie.medium + ' - ' + cookie.term + "' />");
    }

    // Inject the GA values into hidden fields
    $(e).append("<input type='hidden' name='ga_source' id='ga_source' value='" + cookie.source + "' />");
    $(e).append("<input type='hidden' name='ga_medium' id='ga_medium' value='" + cookie.medium + "' />");
    $(e).append("<input type='hidden' name='ga_term' id='ga_term' value='" + cookie.term + "' />");
    $(e).append("<input type='hidden' name='ga_content' id='ga_content' value='" + cookie.content + "' />");
    $(e).append("<input type='hidden' name='ga_campaign' id='ga_campaign' value='" + cookie.campaign + "' />");
    $(e).append("<input type='hidden' name='ga_csegment' id='ga_csegment' value='" + cookie.csegment + "' />");
    $(e).append("<input type='hidden' name='ga_visitorid' id='ga_visitor' value='" + cookie.visitorid + "' />");
    $(e).append("<input type='hidden' name='ga_firstvisit' id='ga_firstvisit' value='" + cookie.firstvisit + "' />");
    $(e).append("<input type='hidden' name='ga_previousvisit' id='ga_previousvisit' value='" + cookie.previousvisit + "' />");
    $(e).append("<input type='hidden' name='ga_currentvisit' id='ga_currentvisit' value='" + cookie.currentvisit + "' />");
    $(e).append("<input type='hidden' name='ga_totalvisits' id='ga_totalvisits' value='" + cookie.totalvisits + "' />");

//    f.source.value = cookie.source;
//    f.medium.value = cookie.medium;
//    f.term.value = cookie.term;
//    f.content.value = cookie.content;
//    f.campaign.value = cookie.campaign;
//    f.segment.value = cookie.csegment;
//    f.numVisits.value = cookie.nVisits;
//    f.visitor.value = cookie.nVisitor;

//    alert('source=' + cookie.source);
//    alert('medium=' + cookie.medium);
//    alert('term=' + cookie.term);
//    alert('content=' + cookie.content);
//    alert('campaign=' + cookie.campaign);
//    alert('custom segment=' + cookie.csegment);
//    alert('visitor=' + cookie.nVisitor);
//    alert('number of visits=' + cookie.nVisits);
//    alert('glcid=' + cookie.gclid);

    return false;
}
