/*
File: Cookie-referrer.js

Author: Tom Dahm
Source: BridgePose Search Engine Marketing (http://www.bridgepose.com)
Requires: pcpcookielib.js

Script to determine lead sources.  Captures HTTP_REFERER on first visit and saves this info in a cookie.  

Later script Get-referrer.js retrieves this cookie and writes a hidden form field containing its info.

Also logs date of first visit, if you want to track lag between visit and lead conversion.

(C) Tom Dahm, 2006.  Feel free to use this code, but please leave this comment block in. This code must not be sold, either alone or as part of an a
pplication, without the consent of the author.

*/
//!!!!!!!!!!!!!!!!
//alert("executing")
//!!!!!!!!!!!!!!!!

userCookie = new cookieObject("leadtracking", 365, null, "referrer", "date")
if (userCookie.found) {

//!!!!!!!!!!!!!!!!
//alert("repeat visitor")
//referrer = userCookie.get("referrer")
//alert(referrer)
//!!!!!!!!!!!!!!!!

   // Repeat visitor. Do nothing.
} else {
//!!!!!!!!!!!!!!!!
//alert("NEW visitor")
//!!!!!!!!!!!!!!!!
   // New visitor

   // Get any "source=" code from the URL

   var url = document.URL
   sourceregex = /(source=[^$]+)/
   if ((matchArray = url.match(sourceregex))) {
      source = matchArray[1]
   }
   else if (document.referrer) {
      // Get referring site
      source = document.referrer 
   } else {
      source = "Unknown"
   }
   userCookie.put ("referrer", source)

   // Get current date
   vdate = new Date()
   userCookie.put ("date",  vdate.toGMTString())
   userCookie.write()
}


