//
// NavTrack.js
// Developer: T.W.Ubhaus
// Created: 12/9/2004
//
// Stores in a cookie the URL of page wherein website was entered 
// and the URL of the previous site's page having link to enter site.
// 


// Domains not to be tracked as a site referrer are to be listed
// in the array below, contained in quotes while seperated by commas.

var omitDomain = new Array("http://search.atomz.com");


// Variables initialized.

var numOmits = omitDomain.length;

var presentPage = document.URL;
var previousPage = document.referrer;

var thisDomain = "http://" + document.domain;
var prevDomain = previousPage.slice(0,thisDomain.length);
var lastDomain = "";

var arrival = true;  // Unless otherwise specified in logic, presume the previous page was offsite for URL data to be passed.



// Begin general cookie functions.

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

// End general cookie functions.



// 
// Check to see if the page previous to the page this script is run on
// is from a non-omitted external site or if it was navigated to from 
// this same website.
//

if (previousPage) {

	if ( thisDomain == prevDomain )
	{    
		arrival = false;
	}
	else 				
	{

	  for (var cntOmit = 0; cntOmit < numOmits; cntOmit++) 
	  { 
	    lastDomain = previousPage.slice(0,omitDomain[cntOmit].length);
	    //alert("Does " + omitDomain[cntOmit] + " = " + lastDomain + " ?"); // uncomment for testing
	    if ( omitDomain[cntOmit] == lastDomain )
	    {
	    	arrival = false;
	    	break;
	    }
	  }
	}
	  
}



//
// If this page was navigated to from an external site then set the cookie
// with present URL and previous page's URL info.
//

	if (arrival==true) 
		{ 
		  // Only cookie stored referrer & URL info is escaped from to prevent anomalous field qualifications.
		  name = escape(document.referrer) + "&target_url=" + escape(presentPage);

		  createCookie(name);
		  //alert('Set Cookie: ' + name); // uncomment for testing
		}

//
// For this script to function on your website globally
// it is necessary that it be included on every page 
// of your website such as by this HTML...
//
//   <SCRIPT LANGUAGE="JavaScript" SRC="http://YOURWEBSITEURL/NavTrack.js"></SCRIPT>
//
//
// Data may be read from cookie and passed to a dynamicly created hyperlink.
//
// Sample JavaScript...
//
//   readCookie(name);
//   if (document.cookie) {
//     linkref = name;
//     //alert('Cookie stored data is: ' + linkref ); // uncomment for testing
//     document.write("<a href=\"http://YOURWEBSITEURL&source_url=" + linkref + "\">NAME OF LINK</a>");
//   }
//   else
//   {
//     document.write("<a href=\"http://YOURWEBSITEURL"\">NAME OF LINK</a>");
//   }
//
//
// The above JavaScript will create two values which may be referenced:
//
//   &source_url - URL of the site previous to your website.
//   &target_url - URL of the page wherein your site is entered.
//

