﻿var source = '';
var medium = '';
var term = '';
var content = '';
var campaign = '';
var gclid = '';
var csegment = '';
var adid = '';

function ReadCookieValues() {
    // 
    // Get the __utmz cookie value. This is the cookies that 
    // stores all campaign information.
    //
    var z = _uGC(document.cookie, '__utmz=', ';');
    
    //Populate URL Varibles
    var uSource = _uGC(document.URL, 'utm_source=', '&');
    var uMedium = _uGC(document.URL, 'utm_medium=', '&');
    var uTerm = _uGC(document.URL, 'utm_term=', '&');
    var uContent = _uGC(document.URL, 'utm_content=', '&');
    var uCampaign = _uGC(document.URL, 'utm_campaign=', '&');
    var uAdid = _uGC(document.URL, 'adid=', '&');
    //\\
    
    // 
    // 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 (used for A/B testing) 
    // 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.
    //
    if (uSource == "-")
        source = _uGC(z, 'utmcsr=', '|');
    else
        source = uSource;

    if (uMedium == "-")
        medium = _uGC(z, 'utmcmd=', '|');
    else
        medium = uMedium;

    if (uTerm == "-")
        term = _uGC(z, 'utmctr=', '|');
    else
        term = uTerm;

    if (uContent == "-")
        content = _uGC(z, 'utmcct=', '|');
    else
        content = uContent;

    if (uCampaign == "-")
        campaign = _uGC(z, 'utmccn=', '|');
    else
        campaign = uCampaign;
        
    gclid = _uGC(z, 'utmgclid=', '|');

    if (uAdid != "-") {
        adid = uAdid;
        campaign = '(paid)';
        medium = 'cpc';
    }
    // 
    // 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 (gclid != "-") {
        source = 'google';
        medium = 'cpc';
    }
    // Data from the custom segmentation cookie can also be passed 
    // back to your server via a hidden form field 
    csegment = _uGC(document.cookie, '__utmv=', ';');
    if (csegment != '-') {
        var csegmentex = /[1-9]*?\.(.*)/;
        csegment = csegment.match(csegmentex);
        csegment = csegment[1];
    } else {
        csegment = '';
    }

    populateHiddenFields();
}
function populateHiddenFields() {
    
    document.getElementById('f_ga_source').value = source;
    document.getElementById('f_ga_medium').value = medium;
    document.getElementById('f_ga_term').value = term;
    document.getElementById('f_ga_content').value = content;
    document.getElementById('f_ga_campaign').value = campaign;
    document.getElementById('f_ga_segment').value = csegment;
    document.getElementById('f_ga_gclid').value = gclid;
    document.getElementById('f_ga_adid').value = adid;
}
function InitGACode(ucode) {
    
    try {
        _uacct = ucode;
        urchinTracker();
        ReadCookieValues();
    } catch (err) { }
}