// Get the value in the cookie with the name provided.
function getCookieValue(cookieName)
{
   var cookieValue = document.cookie;
   var cookieStartsAt = cookieValue.indexOf(' ' + cookieName + '=');
   if (cookieStartsAt == -1)
   {
      cookieStartsAt = cookieValue.indexOf(cookieName + '=');
   }

   if (cookieStartsAt == -1)
   {
      cookieValue = null;
   }
   else
   {
      cookieStartsAt = cookieValue.indexOf('=', cookieStartsAt) + 1;
      var cookieEndsAt = cookieValue.indexOf(';', cookieStartsAt);
      if (cookieEndsAt == -1)
      {
         cookieEndsAt = cookieValue.length;
      }
      cookieValue = unescape(cookieValue.substring(cookieStartsAt,
         cookieEndsAt));
   }

   return cookieValue;
}

// Function: regularTraffic
// Creates an image object to send a request to the traffic server.
function regularTraffic(uuid, release, trafficSite)
{
	// Configuration variables.
	//var trafficSite = '//localhost/SEMTrafficCollector';
	//var version = 1;
	//var account = '{69ece8bf-5ad2-43a3-904c-0693c82fb8cb}';
	var nhp = 'http:';

	// Local variables.
	var query,rf,sr,request,i;

	rf=document.referrer;
	sr=document.location.search;

	if(top.document.location==document.referrer || (document.referrer == '' && top.document.location != ''))
	{	
		rf=top.document.referrer;sr=top.document.location.search
	}

	if(location.href.substr(0,6).toLowerCase()=='https:')
		nhp='https:'; 

	query='uuid='+uuid+
	    '&release='+release+
        '&referer='+escape(rf);

	//alert(top.document.referrer);
	//alert(document.referrer);

	// Create a trafic url with the appropriate site and query string information.
	request = nhp+trafficSite+'/image.tfc?'+query;

	// Debug statement.
	//alert(request);
	//document.write(request);
	
	// Create an image object an set the source to be the custom url. 
	i = new Image();
	i.src = request;	
}

// Check the cookie for the uid.  (semws_uid)
// If it doesn't exist, create it.
// Create a traffic request and send it.
// - if the cookie doesn't already exist, 
// - - Send all the information on the query string of the request.
// - - (account, uid, referer, version, cookie support, browser version, color palette,
// - - screen resolution,  
// - else
// - - Send only the relevant information (account, uuid, referer, version)

// Configuration variables.
var trafficSite = '//www.websourcedtraffic.com';
var release = '2';
// var account = [accountGUID];
var cookieName = 'uuid.websourcedtraffic.com';

// Check to make sure that the uuid global variable is defined.
if (typeof uuid == 'undefined')
{
    if (!self.uuid)
    {
        uuid = "";
    }
} 

// If the uuid variable is empty, get it from the cookie.
if (uuid == "")
{
    uuid = getCookieValue(cookieName);
}
if (uuid == null)
{
    // Call the initial traffic script which creates the cookie and creates the initial traffic tag.
	var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = "http://www.websourcedtraffic.com/semws_tfc_init.js";
    document.getElementsByTagName('head')[0].appendChild(script);
}
else
{
	// Create a regular traffic request.
	regularTraffic(uuid, release, trafficSite);
}
