// ===============================
// Track Event and Redirect
// -------------------------------
// Event click is recorded then the user is reidrected to a specified url

function _trackEvent(category, action, optional_label, url, optional_value){

	// send tracking information to GA account 1
	var ga_result = 1; //pageTracker._trackEvent(category, action, optional_label, optional_value);
	
	//sends trackcing information to GA account 2
	if (pageTracker2){
	var ga_result2 = pageTracker2._trackEvent(category, action, optional_label, optional_value);
	}
	
	// redirects user to apply URL
	if(ga_result && ga_result2){
		window.location = url
	}

}

// ===============================
// Initial Referral
// -------------------------------
// Checks to see if the user is a new visitor or a returning visitor
// If the user is new, it stores a cookie of the initial referral source


function _uGC(l,n,s) {
   // used to obtain a value form a string of key=value pairs
   if (!l || l=="" || !n || n=="" || !s || s=="") return "undefinded";
   var i,i2,i3,c="undefinded";
   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 checkFirst(){
   // check if this is a first time visitor
   newVisitor = 0;
   var myCookie = " " + document.cookie + ";";
   var searchName = "__utma=";
   var startOfCookie = myCookie.indexOf(searchName)
   if (startOfCookie == -1) {   // i.e. first time visitor
	  newVisitor = 1;
   }
}

function grabReferrer(){
   // grab campaign and referrer info from the _utmz cookie
   if (newVisitor) {
	  var z = _uGC(document.cookie, "__utmz=", ";");
	  GA_source = _uGC(z,"utmcsr=", "|");
	  //urchin_medium = _uGC(z,"utmcmd=", "|");
	  //urchin_term = _uGC(z,"utmctr=", "|");
	  //urchin_content = _uGC(z,"utmcct=", "|");
	  //urchin_campaign = _uGC(z,"utmccn=", "|");
	  //var gclid = _uGC(z,"utmgclid=","|");
	
	
	  //pageTracker._setVar(urchin_term);
	 pageTracker._setCustomVar(
	  1,                // This custom var is set to slot #1
	  "Initial_Referral",    // The name of the custom variable
	  GA_source,        // The value of the custom variable 
	  1                 // Sets the scope to visitor-level
	 );

	  //pageTracker._setVar(urchin_term);
	 pageTracker2._setCustomVar(
	  1,                // This custom var is set to slot #1
	  "Initial_Referral",    // The name of the custom variable
	  GA_source,        // The value of the custom variable 
	  1                 // Sets the scope to visitor-level
	 );



   }
}
