// $Id: trk.js,v 1.3 2006/08/07 19:43:55 pat Exp $
//
// may need to skip if blocked referer, e.g. "Field blocked by ProxyWay (http://www.ProxyWay.com)"
// need to watch for user bookmarking URL with tracking parameters to avoid false hits
//
// trk_done cookie forces only one visit to be recorded per browser session.
// *** currently, trk_done cookie is at hostname level. So www.bla.com and bla.com are separate hosts.
// *** BUT, visitor and visit cookies are shared for all domains using the same trk.js file. So
// *** a visitor will be tracked from houston.com to austin.com if both domains include 
// *** trk.houston.com/trk/trk.js
//

function trk_Go() {
  var c = "; "+document.cookie;

  // tests if persistent cookies are enabled in browser
  document.cookie = 'trk_ctest=1; expires=Sun, 18-Jan-2038 00:00:00 GMT';
  var cok = (document.cookie.indexOf('trk_ctest') != -1) ? '1' : '0';
  document.cookie = 'trk_ctest=; expires=Thu, 01-Jan-1970 00:00:01 GMT';

//  Track all clicks that include tracking parameters (even repeats during same session)
//  e.g. user clicks multiple links in a newsletter email or clicks a banner multiple times
//  if ( location.search.match('[&?]trk_s=') ) {
//    trk_Send('/trk/trk_event.php?u='+encodeURIComponent(location)+'&r='+encodeURIComponent(document.referrer));
//  } else  

  // Track if referrer host different than current host AND session cookie not set 
  // This will track visits without referrers such as bookmarks and blocked referrers, but will
  // not track .
  if ( (document.referrer.indexOf(location.host) == -1) && (c.indexOf('; trk_done=1') == -1) ) {
    trk_Send('/trk/trk_visit.php?u='+encodeURIComponent(location)+'&r='+encodeURIComponent(document.referrer));
    document.cookie = 'trk_done=1; path=/;';
  }

  if ( trk_uid && (c.indexOf('; trk_uid=1') == -1) ) {
    trk_Send('/trk/trk_uid.php?uid='+encodeURIComponent(trk_uid));
    document.cookie = 'trk_uid=1; path=/;';
  }  
}

// must be called when script is first loaded
function trk_GetBase()
{
  var s = document.getElementsByTagName("SCRIPT")
  var u = s[s.length-1].src;
  if ( u.indexOf('://') == -1 ) {
    u = location.href;
  }
  var h = u.match("^.+://[^/?]+");
  return (h) ? h[0] : '';
}

function trk_Send(u)
{
  document.write('<div style="visibility: visible; z-index:100;">');
  document.write('<im');
  document.write('g s'+'rc="'+trk_base+u+'" border=0 width=1 height=1 alt="" />');
  document.write('</div>');
// DEBUG CODE ///////////////////////////////////
//document.write('<HR>'+trk_base+u+'<HR>');
// DEBUG CODE ///////////////////////////////////
}

function trk_Event(type, name, value)
{
  trk_Send('/trk/trk_event.php?n='+encodeURIComponent(name)+'&t='+encodeURIComponent(type)+'&v='+encodeURIComponent(value)+'&u='+encodeURIComponent(location)+'&r='+encodeURIComponent(document.referrer));
}

// trk_uid is set in web page.
var trk_uid;
// must be called on first load
var trk_base = trk_GetBase();
trk_Go();
