// Gather Source Data - (c)2009 Ty Roden,Buildtelligence Web Solutions
var z = _uGC(document.cookie, '__utmz=', ';');
// 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
//
var source  = _uGC(z, 'utmcsr=', '|');
var medium  = _uGC(z, 'utmcmd=', '|');
var term    = _uGC(z, 'utmctr=', '|');
var content = _uGC(z, 'utmcct=', '|');
var campaign = _uGC(z, 'utmccn=', '|');
var gclid   = _uGC(z, 'utmgclid=', '|');
//

if (document.referrer) {
   var sourcereferrer = document.referrer;
}

if (gclid !="-") {
   source = 'google';
   medium = 'cpc';
}

function _uGC(l,n,s) {
   // used to obtain a value from a string of key=value pairs
   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;
}

// Attempt ga.js cookie poll, if manual variables on URL, overwrite.

function writeCookie(cname, value, days){ // standard cookie routine to writeout
  if(days){
    (t=new Date()).setTime(new Date().getTime()+days*24*60*60*1000);
    var e='; expires='+t.toGMTString();
  }else{
    var e='';
  }
  document.cookie=cname+"="+value+e+"; path=/";
}

function varlookup(name, url) { // optional- pass URL otherwise parse current
	if (!url) url = window.location.href;
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var results = new RegExp("[\\?&]"+name+"=([^&#]*)").exec(url);
	if( results == null ) return null;		// return null if not found
	else // decodeURIComponent doesn't recognize '+' as encoding for space
		return decodeURIComponent(results[1].replace(/\\+/g," "));
}

function setFormField( idRef, val ) {
  if(!document.getElementById ) return;
  var inputObj = document.getElementById( idRef );
  if( inputObj ) {
    inputObj.value = val;
  }
}

function setFields() {
   setFormField( 'Source', source );
   setFormField( 'Medium', medium );
   setFormField( 'Keyword', term );
   setFormField( 'source', source );
   setFormField( 'medium', medium );
   setFormField( 'keyword', term );
}

if(varlookup('utm_source') != null){
    var source = varlookup("utm_source"); // setup the var + store
    writeCookie('thatsource', source, '31');
}
if(varlookup('utm_medium') != null){
    var medium = varlookup("utm_medium");
    writeCookie('thatmedium', medium, '31');
}
if(varlookup('utm_term') != null){
    var keyword = varlookup("utm_term");
    writeCookie('thatkeyword', keyword, '31');
}
if(varlookup('source') != null){
    var source = varlookup("source"); // setup the var + store
    writeCookie('thatsource', source, '31');
}
if(varlookup('medium') != null){
    var medium = varlookup("medium");
    writeCookie('thatmedium', medium, '31');
}
if(varlookup('keyword') != null){
    var keyword = varlookup("keyword");
    writeCookie('thatkeyword', keyword, '31');
}

function readCookie(name)
{
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return null;
}

if (readCookie('thatsource') != null) {
    source = readCookie('thatsource');
}else{
    if(source != null) {writeCookie('thatsource', source, '31');}
}
if (readCookie('thatmedium') != null) {
    medium = readCookie('thatmedium');
}else{
    if(medium != null) {writeCookie('thatmedium', medium, '31');}
}
if (readCookie('thatkeyword') != null) {
    term = readCookie('thatkeyword');
}else{
    if(term != null) {writeCookie('thatkeyword', term, '31');}
}
if (readCookie('thatreferrer') != null) {
    sourcereferrer = readCookie('thatreferrer');
}else{
    if(sourcereferrer != null) {writeCookie('thatreferrer', sourcereferrer, '31');}
}


