
var current_page = document.URL;
var http_string = "http";
if (current_page.indexOf("https", 0) != -1)
	{var http_string = "https";}

// SECTION: Extract Source Code From The URL *******************************************************************************
function extractSourceCodeFromURL()
{
	var sourcecodeOut = '';
	var url = window.location;
	URLAsString = new String(url);
	start = URLAsString.indexOf("srccd");
	
	if (start != -1)
	{
		if (URLAsString.indexOf("&", start) != -1)
			{end = URLAsString.indexOf("&", start);}
		else 
			{end = URLAsString.length;}
		var sourcecodeOut = (URLAsString.substring(start+6, end));
	}
	
	return sourcecodeOut;
}
// END OF SECTION: Extract Source Code From The URL ************************************************************************

// SECTION: Check to See If Source Cookie Exists In the Local Cookie *******************************************************
function SourceCodeCookieExists()
{
	var variable_out = '0';
	if (document.cookie != "")
	{
		var getLocalCookie = unescape(document.cookie);
		var SourceCodeLocation = getLocalCookie.indexOf("wTrackerSourceCode=");
		if (SourceCodeLocation != -1)
			{var variable_out = '1';}
	}
	return variable_out;
}
// END OF SECTION: Check to See If Source Cookie Exists In the Local Cookie ************************************************

// SECTION: Extract Source Code From The Cookie ****************************************************************************
function extractSourceCodeFromCookie()
{
	// will return the value of the Cookie Named "wTrackerSourceCode" if it doesn't exist returns empty string
	var sourceCodeOut = '';
	var exists = SourceCodeCookieExists();
	
	if (exists == 1)
	{
		var getLocalCookie = unescape(document.cookie);
		var SourceCodeLocation = getLocalCookie.indexOf("wTrackerSourceCode=");
		var start = SourceCodeLocation + 19;  
		var end = getLocalCookie.indexOf(";", start);  
		
		if (end == -1)
		{
			end = getLocalCookie.length;
		}
			
		var sourcecodeOut = getLocalCookie.substring(start, end);
	}
		
	return sourcecodeOut;
}
// END OF SECTION: Extract Source Code From The Cookie *********************************************************************

// SECTION: Check to See If Source Cookie Exists In the Local Cookie *******************************************************
function DomainCookieExists()
{
	var variable_out = '0';
	if (document.cookie != "")
	{
		var getLocalCookie = unescape(document.cookie);
		var SourceCodeLocation = getLocalCookie.indexOf("wTrackerReferringDomain=");
		if (SourceCodeLocation != -1)
			{var variable_out = '1';}
	}
	return variable_out;
}
// END OF SECTION: Check to See If Source Cookie Exists In the Local Cookie ************************************************

// SECTION: Extract Domain From The Cookie *********************************************************************************
function extractDomainFromCookie()
{
	// will return the value of the Cookie Named "wTrackerSourceCode" if it doesn't exist returns empty string
	var domainOut = '';
	var exists = DomainCookieExists();
	
	if (exists == 1)
	{
		var getLocalCookie = unescape(document.cookie);
		var SourceCodeLocation = getLocalCookie.indexOf("wTrackerReferringDomain=");
		var start = SourceCodeLocation + 24;  
		var end = getLocalCookie.indexOf(";", start);  
		
		if (end == -1)
		{
			end = getLocalCookie.length;
		}
			
		var domainOut = getLocalCookie.substring(start, end);
	}
		
	return domainOut;
}
// END OF SECTION: Extract Domain From The Cookie **************************************************************************

// SECTION: Check To See If The SourceCode Passed In Matches The Local Cookie (excluding empty) ****************************
function checkSourceCodeMatch(sourcecode_in)
{
	var matches = 0;
	var sourceCodeInCookie = extractSourceCodeFromCookie();
	
	if (sourceCodeInCookie != -1 && sourceCodeInCookie == sourcecode_in)
		{var matches = 1;}
	return matches;
}
// END OF SECTION: Check To See If The SourceCode Passed In Matches The Local Cookie (excluding empty) *********************

// SECTION: Get Refering Page Information **********************************************************************************
function extractReferringDomain()
{
	var referrer = document.referrer;
	referrerDomainAsString = new String(referrer); 
	referringDomainOut = 	"noReferrer";
		
	if (referrerDomainAsString != "") 
	{	
		startDomain = referrerDomainAsString.indexOf("//");
		endDomain = referrerDomainAsString.indexOf("/", 7); 
		if (referrerDomainAsString.indexOf("/", 7) != -1) 
			{referringDomainOut = referrerDomainAsString.substring(startDomain + 2, endDomain);}
		else 
			{referringDomainOut = referrerDomainAsString.substring(startDomain + 2, referrerDomainAsString.length);}
	}
	
	return referringDomainOut;
}
// END OF SECTION: Get Refering Page Information ***************************************************************************


// SECTION: Set Local Domain Cookie; Set The Local Cookie If They Don't Have The "Domain Cookie" or the "Source Cookie" ****

var wTrackerSourcecodeInURL = extractSourceCodeFromURL(); // GETS SOURCE CODE FROM URL

// set test cookie if no cookies - will help us determine if the user has cookies on
if (!document.cookie.length)
{ 
	var expireDate1 = new Date();
	expireDate1.setTime(expireDate1.getTime() + 14400000);//set expiration date to 4 hours
	document.cookie = "wTrackerTestCookie=on" + "; expires=" + expireDate1.toGMTString() + "; path=/";		
}

if (document.cookie.length > 0)
{
	var SourceCodeCookieIsDefined = SourceCodeCookieExists(); //RETURNS 1 IF EXISTS, ELSE 0
	var DomainCookieIsDefined = DomainCookieExists(); //RETURNS 1 IF EXISTS, ELSE 0
	var matches_existing_srccd = checkSourceCodeMatch(wTrackerSourcecodeInURL); //
	var rand5 = Math.random();//Random Value In Image Name Prevents Image From Being Cached
	var url = window.location; //TESTING PURPOSES
	URLAsString = new String(url); //TESTING PURPOSES
	
	if ((wTrackerSourcecodeInURL != "" && matches_existing_srccd == 0) || (wTrackerSourcecodeInURL == "" && SourceCodeCookieIsDefined == 0 && DomainCookieIsDefined == 0))
	{	
		var SCExpireDate = new Date();	
		SCExpireDate.setTime(SCExpireDate.getTime() + 14400000);	
		document.cookie = "wTrackerVisitDate=today" + "; expires=" + SCExpireDate.toGMTString() + "; path=/";
		document.cookie = "wTrackerSourceCode=" + escape(wTrackerSourcecodeInURL) + "; expires=" + SCExpireDate.toGMTString() + "; path=/"; 
		document.cookie = "wTrackerClientId=" + escape(clientidvar) + "; expires=" + SCExpireDate.toGMTString() + "; path=/"; 
		
		if (DomainCookieIsDefined == 1)
			{var wTrackerVisitorDomain = extractDomainFromCookie();}
		else
			{var wTrackerVisitorDomain = extractReferringDomain();}

		document.cookie = "wTrackerReferringDomain=" + escape(wTrackerVisitorDomain) + "; expires=" + SCExpireDate.toGMTString() + "; path=/"; 
		
		document.write("<img src=" + http_string + "://www.1st-chicago-web-design.com/wTracker/wTracker_pwim_log_data.cfm?log_type=visitor&clientID=" + clientidvar + "&srccd=" + wTrackerSourcecodeInURL + "&wTrackerOn=" + wTrackerOn + "&domain=" + wTrackerVisitorDomain + "&rand5=" + rand5 + " width=1 height=1>");
	} 
}
// END OF SECTION: Set Local Domain Cookie If They Don't Have The "Domain Cookie" or the "Source Cookie" ******************


// SECTION: Determine If Success Image Should Be Written To Page And With What Data ****************************************
try
	{
	// wTrackerLogLead needs to be defined and have a value of 1 in order to log the sales; need to 
	// have the double check because of Mac Safari (variable is defined with value undefined)
	var logOrder = wTrackerLogLead;
	if (wTrackerLogLead != 1)
		{var logOrder = '';}
	}
catch(e)
	{var logOrder = '';}


if (logOrder != '')
{
	var wTrackerWriteSuccess = 1;
	var wTrackerSuccessSourceCode = '';
	var wTrackerSuccessDomain = '';
	
	if (document.cookie.length > 0)
	{	
	  	var allcookies = unescape(document.cookie);
		var find_success_today = allcookies.indexOf("wTrackerSuccessToday=");
		var SourceCodeCookieIsDefined = SourceCodeCookieExists(); //RETURNS 1 IF EXISTS, ELSE 0
		var DomainCookieIsDefined = DomainCookieExists(); //RETURNS 1 IF EXISTS, ELSE 0
		var SourceCodeInCookie = extractSourceCodeFromCookie();
		var DomainInCookie = extractDomainFromCookie();
	
		if (find_success_today == -1)
		{
			if (SourceCodeCookieIsDefined == 1)
				{var wTrackerSuccessSourceCode = extractSourceCodeFromCookie();}
			
			if (DomainCookieIsDefined == 1)
				{var wTrackerSuccessDomain = extractDomainFromCookie();}
			
			// set success cookie to expire in 2 minutes(this should stop page refreshing, and allow for other orders to be logged)
			var successCookieExpireDate = new Date();	
			successCookieExpireDate.setTime(successCookieExpireDate.getTime() + 120000);	
			document.cookie = "wTrackerSuccessToday=today" + "; expires=" + successCookieExpireDate.toGMTString() + "; path=/";
		}
		else
			{
				wTrackerWriteSuccess = 0;
			}
	}
	
	if (wTrackerWriteSuccess == 1)
	{
		var rand4 = Math.random();//Random Value In Image Name Prevents Image From Being Cached
		document.write("<img src=" + http_string + "://www.1st-chicago-web-design.com/wTracker/wTracker_pwim_log_data.cfm?log_type=lead&clientID=" + clientidvar + "&salesamount=" + salesamountvar + "&units=" + unitsvar + "&orderID=" + orderidvar + "&rand4=" + rand4 + "&srccd=" + wTrackerSuccessSourceCode + "&domain=" + wTrackerSuccessDomain  + "&wTrackerOn=" + wTrackerOn + " width=1 height=1>");
	}
}	
// END OF SECTION: Determine If Success Image Should Be Written To Page And With What Data *********************************


