//Aegis Tracking Code ver. 1.0 BETA

/*var aegis_thisDomain;

if(document.domain != 'localhost')
{
	var domArr = document.domain.split(".");
	aegis_thisDomain = domArr[domArr.length - 2].toLowerCase() + '.' + domArr[domArr.length - 1].toLowerCase();
}
else aegis_thisDomain = '';*/

//always check to see if we need to set the tracking cookie
//user could come to any page
aegis_setTrackingCookie();

//if has tracking code appended to url we want to save it
function aegis_setTrackingCookie()
{
	var aegis_tid, aegis_cid = '', aegis_pid = '', aegis_ref = '';
	var url = window.location.toString();
	var startPos, endPos;
	
	startPos = url.indexOf('aegis_tid' + '=');
	
	if(startPos != -1)
	{
		startPos += 10;
		endPos = url.indexOf('&', startPos);
		if(endPos == -1) endPos = url.length;
		
		aegis_tid = url.substring(startPos, endPos);
		
		startPos = url.indexOf('aegis_cid' + '=');
	
		if(startPos != -1)
		{
			startPos += 10;
			endPos = url.indexOf('&', startPos);
			if(endPos == -1) endPos = url.length;
			
			aegis_cid = url.substring(startPos, endPos);
		}
		
		startPos = url.indexOf('aegis_pid' + '=');
	
		if(startPos != -1)
		{
			startPos += 10;
			endPos = url.indexOf('&', startPos);
			if(endPos == -1) endPos = url.length;
			
			aegis_pid = url.substring(startPos, endPos);
		}		
	}
	else if(aegis_oldTrackingParam && aegis_oldTrackingParam != '')	//aegis_tid doesn't exist, check for old tracking
	{
		startPos = url.indexOf(aegis_oldTrackingParam + '=');
	
		if(startPos != -1)
		{
			startPos += aegis_oldTrackingParam.length + 1;
			endPos = url.indexOf('&', startPos);
			if(endPos == -1) endPos = url.length;
			
			aegis_tid = url.substring(startPos, endPos);
		}
	}
	
	if(!aegis_tid || aegis_tid == '')	//check to see if from organic serps
	{
		try
		{
			aegis_tid = aegis_getSerpTrackingCode(aegis_getRef());
		}
		catch(e)
		{
			aegis_tid = '';
		}
	}
	
	//save referrer for later
	if(aegis_getRef() != '')	//use only if referrer is not this domain
	{
		aegis_ref = document.referrer;	//raw referrer; will parse later
		if(aegis_ref == null) aegis_ref = '';
		
		/*REMOVING:DOING SERVER SIDEstartPos = url.indexOf('url=');
		if(startPos != -1)
		{
			startPos += 4;
			endPos = url.indexOf('&', startPos);
		if(endPos == -1) endPos = url.length;
		}*/
		aegis_ref = escape(aegis_ref);
	}
	
	var now = new Date();
		
	var hours, minutes, seconds;
	
	hours = now.getHours().toString();
	minutes = now.getMinutes().toString();
	seconds = now.getSeconds().toString();
	
	if(hours.length < 2) hours = '0' + hours;
	if(minutes.length < 2) minutes = '0' + minutes;
	if(seconds.length < 2) seconds = '0' + seconds;
	
	var nowString = (now.getMonth()+1).toString() + '/' + now.getDate().toString() + '/' + now.getFullYear().toString() + ' ' + hours + ':' + minutes + ':' + seconds;		
	
	now.setTime(now.getTime() + 3650 * 24 * 60 * 60 * 1000);	
	
	var aegis_uid = aegis_getCookie('aegis_uid');
		
	if(aegis_uid == null)	//set userID cookie
	{
		aegis_uid = Math.floor(Math.random()*99223372036);		
		
		aegis_setCookie('aegis_uid', aegis_uid, now, '/', aegis_domain, null);
		aegis_setCookie('aegis_first_hit', nowString.replace(' ', '+'), now, '/', aegis_domain, null);
		
		
	}	
	
	if(aegis_tid != null && aegis_tid != '')	//have something, need to save it
	{		
		aegis_setCookie('aegis_tid', aegis_tid, now, '/', aegis_domain, null);
		if(aegis_cid != null && aegis_cid != '') aegis_setCookie('aegis_cid', aegis_cid, now, '/', aegis_domain, null);
		if(aegis_pid != null && aegis_pid != '') aegis_setCookie('aegis_pid', aegis_pid, now, '/', aegis_domain, null);
	}
	if(aegis_ref != '')
	{
		aegis_setCookie('aegis_ref', aegis_ref, now, '/', aegis_domain, null);
	}
	
	//if we are tracking hits we will log here
	if(aegis_hitLogging && aegis_tid != null && aegis_tid != '')
	{
		aegis_logHit(aegis_uid, aegis_tid, aegis_ref);
	}
}

function aegis_logHit(aegis_uid, aegis_tid, aegis_ref)
{
	var proto = window.location.protocol.toLowerCase();
	
	if ('http:' != proto && 'https:' != proto) 
	{
		proto = 'http:';
	}
	
	var url = proto + '//www.searchfloor.com/AegisTracker/ClickTracker.aspx?domain=' + aegis_domain + '&aegis_uid=' + aegis_uid;
	if(aegis_tid != null && aegis_tid != '') url += '&aegis_tid=' + aegis_tid;
	if(aegis_ref != null && aegis_ref != '') url += '&aegis_ref=' + aegis_ref;
	
	var bug = new Image();
	bug.src = url;	
}





function aegis_getDomain(url) 
{
   if (url) 
   {
     domain = url.match(".?://(www\.)?([^/:]+)");
     if (domain && domain.length >= 3) 
     {
       return domain[2] ? domain[2] : '';
     }
   }
   return "";
}

function aegis_getRef()
{
	var ref = aegis_getDomain(document.referrer);
	if (ref && (ref.toLowerCase().indexOf(aegis_domain) > -1 || aegis_domain.indexOf(ref.toLowerCase()) > -1)) ref = "";
    return ref.toLowerCase();
}

function aegis_getSerpTrackingCode(ref)
{
	if(!ref || ref == '') return "";
	
	var trackingCode = '';
	
	var goog = new Array("google.", "aol.com", "myway.com", "earthlink.net", "netscape.com", "newyorktimes.com", "nytimes.com", "washingtonpost.com", "webmd.com", "comcast.net", "foxnews.com", ".mysearch.com", ".cnn.com", "peoplepc.com", "my.att.net", ".rr.com");
	var yahoo = new Array("search.yahoo.", "altavista.com", "overture.com", "alltheweb.com", "websearch.com");
	var msn = new Array("search.msn.", "search.live.", "bing.");
	var ask = new Array("ask.co", "teoma.com");
	
	for(x = 0; x < goog.length; x++)
	{
		if(ref.indexOf(goog[x]) > -1) { trackingCode = "google_organic"; break; }		
	}
	if(trackingCode == '') 
	{
	    for(x = 0; x < yahoo.length; x++)
	    {
		    if(ref.indexOf(yahoo[x]) > -1) { trackingCode = "yahoo_organic"; break; }	
	    }
	}
	if(trackingCode == '') 
	{
	    for(x = 0; x < msn.length; x++)
	    {
		    if(ref.indexOf(msn[x]) > -1) { trackingCode = "msn_organic"; break; }		
	    }
	}
	if(trackingCode == '') 
	{
	    for(x = 0; x < ask.length; x++)
	    {
		    if(ref.indexOf(ask[x]) > -1) { trackingCode = "ask_organic"; break; }		
	    }
	}
	if(trackingCode == '') 
	{
	    if(ref.indexOf('mail.yahoo.') != -1) trackingCode = "mail_yahoo";
	    else if(ref.indexOf('mail.aol.') != -1) trackingCode = "mail_aol";
	    else if(ref.indexOf('mail.live.') != -1) trackingCode = "mail_msn";
	    else if(ref.indexOf('mail.google.') != -1) trackingCode = "mail_google";
	    else if(ref.indexOf('.mail.') != -1 || ref.indexOf('.webmail.') != -1) trackingCode = "mail_unknown";
	}
	
	if(trackingCode == '') trackingCode = ref;
	
	return trackingCode;
}



function aegis_setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function aegis_getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function aegis_deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
