/*
REQUIRED PARAMETERS
	wtiWebsiteId
	wtiWtrackerOn
ADDITIONAL REQUIRED PARAMETERS WHEN SALES SUCCESS
	wtiLogSale
	wtiOrderId
	wtiUnitsSold
	wtiSalesAmount
ADDITIONAL REQUIRED PARAMETERS WHEN LEAD SUCCESS
	wtiLogLead
	wtiLeadType
COOKIE VARIABLES SET
	wtcWebsiteId
	wtcVisitDate
	wtcSourceCode
	wtcReferrer
	wtcReferrerSearch
	wtcTestCookiesOn
	wtcSuccessDate
ABBREVIATION NOTES
	wti - wTracker In
	wtc - wTracker Cookie
	wto - wTracker Out
*/


// DETERMINE THE LOCATION OF THE WTRACKER LOGGING PAGE
var log_location = "http://www.1st-chicago-web-design.com/wTracker/wTracker_pwim_log_dataV3.php";
if (document.URL.indexOf("https", 0) != -1)
	{var log_location = "https://www.1st-chicago-web-design.com/wTracker/wTracker_pwim_log_dataV3.php";}


//WILL DETERMINE IF A COOKIE EXISTS
function CookieExists(tempName)
{
	var outValue;
	
	if (document.cookie.indexOf(tempName) != -1)
		{outValue = '1';}
	else
		{outValue = '0';}
		
	return outValue;
}

//WILL GET THE VALUE OF THE COOKIE; IF IT DOESN'T EXIST THE VALUE WILL BE AN EMPTY STRING
function CookieValue(tempName) 
{
	var outValue = '';
	var getLocalCookie = unescape(document.cookie);
	
	if (CookieExists(tempName) == 1)
	{
		start = getLocalCookie.indexOf(tempName);
		start = start + tempName.length + 1;
		end = getLocalCookie.indexOf(";", start);
		if (end == -1)
			{end = getLocalCookie.length;}
		
		outValue = getLocalCookie.substring(start, end);
	}
	return outValue;
}

function URLValue(tempName)
{
	var outValue = '';
	URLAsString = new String(window.location);
	
	stringLocation = URLAsString.indexOf(tempName);
	
	if (stringLocation != -1)
	{
		start = stringLocation + tempName.length + 1;
		if (URLAsString.indexOf("&", start) != -1)
			{end = URLAsString.indexOf("&", start);}
		else 
			{end = URLAsString.length;}
		var outValue = (URLAsString.substring(start, end));
	}
	return outValue;
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  var nothing;
  return nothing;
}


// CHECK TO SEE IF THE SOURCE CODE PASSED IN MATCHES THE COOKIE VALUE FOR SOURCECOE
function checkSourceCodeMatch(sourcecode_in)
{
	var myString = sourcecode_in;
	var myCookie = CookieValue('wtcSourceCode');
	var matches = 0;
	
	if (myString == myCookie)
		{var matches = 1;}
		
	return matches;
}

// GET THE REFERER
function getReferrer()
{
	referrerDomainAsString = new String(document.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;
}

//GET THE REFERER SEARCH RUN 
function getReferrerSearch()
{
	var search_string = "";
	var search_string_location;
	var ReferingDomain = document.referrer;
	lcReferingDomain = ReferingDomain.toLowerCase();
	
   // check first for the existance of a keyword variable
   var undefined;
   var search_string = getQueryVariable("keyword")
   if(search_string === undefined) {
      search_string = "";
   }
   
   if (search_string == "") {
      // couldn't get search string from current url query variable keyword
      // try referrer
	  if (lcReferingDomain.indexOf("google") != -1)
      //google stores the search string run by the user in the url variable "q"
		{
			search_string_location = lcReferingDomain.indexOf("q=");
			query_string_variable_length = 2;
		}
	  else if (lcReferingDomain.indexOf("msn") != -1)
		//msn stores the search string run by the user in the url variable "q"
		{
			search_string_location = lcReferingDomain.indexOf("q=");
			query_string_variable_length = 2;
		}
	  else if (lcReferingDomain.indexOf("yahoo") != -1)
		//msn stores the search string run by the user in the url variable "p"
		{
			search_string_location = lcReferingDomain.indexOf("p=");
			query_string_variable_length = 2;
		}
	  else
		{
         search_string_location = 0
      }
	  
	
	  if (search_string_location == 0)
		{search_string = "na: " + lcReferingDomain;}
	  else if (search_string_location == -1)
		{search_string = "unrecognized: " + lcReferingDomain;}
	  else
		{
		 start = search_string_location + query_string_variable_length;
		 if (lcReferingDomain.indexOf("&", start) != -1)
			{end = lcReferingDomain.indexOf("&", start);}
		 else 
			{end = lcReferingDomain.length;}
		 search_string = (lcReferingDomain.substring(start, end));
		}
  }    
	
	return unescape(search_string)
}

// GETS SOURCE CODE FROM URL
var urlSourceCode = URLValue('srccd'); 


// SET COOKIE IF NOT EXIST; TO HELP DETERMINE IF USER HAS COOKIES TURNED ON
if (!document.cookie.length)
{ 
	var expireDate1 = new Date();
	expireDate1.setTime(expireDate1.getTime() + 14400000);//set expiration date to 4 hours
	document.cookie = "wtcTestCookiesOn=1" + "; expires=" + expireDate1.toGMTString() + "; path=/";		
}

var varVisitorDomainSearch = getReferrerSearch()

if (document.cookie.length > 0)
{
	var matches_existing_srccd = checkSourceCodeMatch(urlSourceCode); 
	var rand5 = Math.random();  //Random Value In Image Name Prevents Image From Being Cached
	
	/*
	IF THERE IS A SOURCECODE IN THE URL AND IT DOES NOT MATCH THE USERS EXISTING SOURCECODE THEN WRITE COOKIE AND LOG AS VISITOR
	IF THERE IS NOT A SOURCECODE IN THE URL AND THE USER DOES NOT HAVE AN EXISTING SOURCECODE THEN WRITE COOKIE AND LOG AS VISITOR
	*/
	if ((urlSourceCode != "" && matches_existing_srccd == 0) || (urlSourceCode == "" && CookieExists('wtcSourceCode') == 0 && CookieExists('wtcReferrer') == 0))
	{	
		if (CookieExists('wtcReferrer') == 1)
			{
			//use the domain information we already have in the cookies
			var varVisitorDomain = CookieValue('wtcReferrer');
			var varVisitorDomainSearch = CookieValue('wtcReferrerSearch');
			}
		else
			{
			//get the domain information from the previous page
			var varVisitorDomain = getReferrer();
			var varVisitorDomainSearch = getReferrerSearch()
			}
			
		var SCExpireDate = new Date();	
		SCExpireDate.setTime(SCExpireDate.getTime() + 14400000);	
		document.cookie = "wtcVisitDate=today" + "; expires=" + SCExpireDate.toGMTString() + "; path=/";
		document.cookie = "wtcSourceCode=" + urlSourceCode + "; expires=" + SCExpireDate.toGMTString() + "; path=/"; 
		document.cookie = "wtcWebsiteId=" + clientidvar + "; expires=" + SCExpireDate.toGMTString() + "; path=/"; 
		document.cookie = "wtcReferrer=" + varVisitorDomain + "; expires=" + SCExpireDate.toGMTString() + "; path=/"; 
		document.cookie = "wtcReferrerSearch=" + varVisitorDomainSearch + "; expires=" + SCExpireDate.toGMTString() + "; path=/"; 

		use_query_string = ""; 
		use_query_string = use_query_string  + "?wtoLogType=v";
		use_query_string = use_query_string  + "&wtoWtrackerOn=" + wTrackerOn;
		use_query_string = use_query_string  + "&wtoWebsiteId=" + clientidvar;
		use_query_string = use_query_string  + "&wtoSourcecode=" + urlSourceCode;
		use_query_string = use_query_string  + "&wtoReferrer=" + escape(varVisitorDomain);
		use_query_string = use_query_string  + "&wtoReferrerSearch=" + escape(varVisitorDomainSearch);
		use_query_string = use_query_string  + "&rand5=" + rand5;
		// Comment out database update. 6/8/2009 Scott
		// document.write("<img src=" + log_location + use_query_string + "width=1 height=1>")
		
	}
}

/*
//DETERMINE IF SALE SHOULD BE LOGGED
try
{
	var logSale = wTrackerLogSale;
	if (wtiLogSale != 1) 
		{var logSale = '';}
}
catch(e)
	{var logSale = '';}
	
//DETERMINE IF LEAD SHOULD BE LOGGED
try
{
	var logLead = wTrackerLogLead;
	if (wtiLogLead != 1) 
		{var logLead = '';}
}
catch(e)
	{var logLead = '';}
*/
	
// leave that code alone, everything is a lead/sale
try
{
   var logSale = wTrackerLogLead;
   if (wTrackerLogLead != 1) 
      {var logSale = '';}
}
catch(e)
   {var logSale = '';}


   
var allcookies = unescape(document.cookie);
var SuccessInLast2Minutes = allcookies.indexOf("wtcSuccessDate=");
var rand4 = Math.random();//Random Value In Image Name Prevents Image From Being Cached

//if ((logLead != '' || logSale != '') && (SuccessInLast2Minutes == -1))
if ((logSale != '') && (SuccessInLast2Minutes == -1))
{
	// 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 = "wtcSuccessDate=today" + "; expires=" + successCookieExpireDate.toGMTString() + "; path=/";
	
	if (CookieExists('wtcReferrer') == 1)
		{
		//use the domain information we already have in the cookies
		var varVisitorDomain = CookieValue('wtcReferrer');
		var varVisitorDomainSearch = CookieValue('wtcReferrerSearch');
		}
	else
		{
		//get the domain information from the previous page
		var varVisitorDomain = getReferrer();
		var varVisitorDomainSearch = getReferrerSearch()
		}

	if (logSale != '')
	{
		//LOG SALE
		use_query_string = ""; 
		use_query_string = use_query_string  + "?wtoLogType=s";
		use_query_string = use_query_string  + "&wtoWebsiteId=" + clientidvar;
		use_query_string = use_query_string  + "&wtoSalesAmount=" + salesamountvar;
		use_query_string = use_query_string  + "&wtoUnitsSold=" + unitsvar;
		use_query_string = use_query_string  + "&wtoOrderId=" + orderidvar;
		use_query_string = use_query_string  + "&wtoSourcecode=" + CookieValue('wtcSourceCode');
		use_query_string = use_query_string  + "&wtoReferrer=" + escape(varVisitorDomain);
		use_query_string = use_query_string  + "&wtoReferrerSearch=" + escape(varVisitorDomainSearch);
		use_query_string = use_query_string  + "&wtoWtrackerOn=" + wTrackerOn;
		use_query_string = use_query_string  + "&rand4=" + rand4;
		// Remove the database update 6/9/2009 scott
		// document.write("<img src=" + log_location + use_query_string + "width=1 height=1>")
	}
 /*  
	else if (logLead != '')
	{	
		//LOG LEAD
		use_query_string = ""; 
		use_query_string = use_query_string  + "?wtoLogType=l";
		use_query_string = use_query_string  + "&wtoWebsiteId=" + clientidvar;
		use_query_string = use_query_string  + "&wtoLeadType=" + wtiLeadType;
		use_query_string = use_query_string  + "&wtoSourcecode=" + escape(CookieValue('wtcSourceCode'));
		use_query_string = use_query_string  + "&wtoReferrer=" + escape(varVisitorDomain);
		use_query_string = use_query_string  + "&wtoReferrerSearch=" + escape(varVisitorDomainSearch);
		use_query_string = use_query_string  + "&wtoWtrackerOn=" + wtiWtrackerOn;
		use_query_string = use_query_string  + "&rand4=" + rand4;
		// Remove the database update 6/9/2009 scott
		// document.write("<img src=" + log_location + use_query_string + "width=1 height=1>")
	}
 */  	
}