﻿        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;
        }

// 
// 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. 
// 
//Identifies a search engine, newsletter name, or other source specified in the utm_source query parameter See the “Marketing Campaign Tracking” section for more information about query parameters.

var source  = _uGC(z, 'utmcsr=', '|'); 

//A campaign medium or value of utm_medium query parameter.

var medium  = _uGC(z, 'utmcmd=', '|'); 

//Identifies the keywords used in an organic search or the value in the utm_term query parameter.

var term    = _uGC(z, 'utmctr=', '|'); 

//Campaign content or the content of a particular ad (used for A/B testing) The value from utm_content query parameter.

var content = _uGC(z, 'utmcct=', '|'); 

//Stores the campaign name or value in the utm_campaign query parameter.

var campaign = _uGC(z, 'utmccn=', '|'); 

//A unique identifier used when AdWords auto tagging is enabled This value is reconciled during data processing with information from AdWords.

var 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 (gclid !="-") { 
      source = 'google'; 
      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); 
      csegment    = csegment[1]; 
} else { 
      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(".");
var nVisits = aParts[5];




//Grab the cookie that we have created for the Google campaign

var g = _uGC(document.cookie, 'ad_campaign=', ';')




function populateHiddenFields() { 


var myTextBoxElement = document.getElementById("google_source");
myTextBoxElement.value = "my new value";
alert(myTextBoxElement.value);

      
      //Assign the google data to a hiiden text box
//      document.getElementById("google_source").value = source;
//      document.getElementById("google_term_txt").value= term;
//      document.getElementById("google_content_txt").value= content;
//      document.getElementById("google_medium_txt").value= medium;
//      document.getElementById("google_campaign_txt").value= g;
//      document.getElementById("google_segment_txt").value= csegment;
//      document.getElementById("google_visits_txt").value= nVisits;
//     
//      
//      alert('source='+document.google_source.value); 
//      alert('medium='+document.google_medium.value); 
//      alert('term='+document.term.value); 
//      alert('content='+document.content.value); 
//      alert('campaign='+document.campaign.value); 
//      alert('custom segment='+document.segment.value); 
//      alert('number of visits='+document.numVisits.value);
      
      return false; 

} 






