// Bill Burleigh, Laramie River Ranch, www.LRRanch.com
// Copyright 1998, 2002
//
// We use cookies to track which marketing sources send us guests.
// You can have each site that links to you add a "tag".
// Ex) http://www.lrranch.com/?DRA instead of the usual http://www.lrranch.com
// This tag is recorded in a cookie on the visitor's machine.
// If no tag is used, the referring URL is recorded instead.
//
// Later when the guest visits your brochure request page and asks
// for a brochure this info is pulled out of the cookie and sent
// along with the brochure request.
//
// You can then know what sites led to the guest asking for your brochure.
function mySetCookie(name, value, expire) {
   value = unescape(value);
   document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}
function myGetCookie(Name) {
   var search = Name + "=";
   if (document.cookie.length > 0) {
      offset = document.cookie.indexOf(search);
      if (offset != -1) {
         offset += search.length;
         end = document.cookie.indexOf(";", offset);
         if (end == -1)
            end = document.cookie.length
         return unescape(document.cookie.substring(offset, end))
      }
   }
}
var calledURL      = "";
var i              = 0;
var tagPos         = 0;
var tag            = "";
var origTag        = "";
var expireDate     = new Date();
// Check to see if there is a previous cookie
origTag = myGetCookie("OriginalLinkSource");
if (origTag != null) {
   // The cookie exists from a previous visit, leave it alone
}
else {
   // The cookie does not exist, create one
   // Capture the URL that pulled up this page
   calledURL = document.URL;
   // Determine if a tag has been appended to the URL that pulled up this page by searching for the "?"
   i = 1;
   while (i < calledURL.length) {
      if (calledURL.substring(i,i+1) == "?")
         tagPos = i;
      i++;
   }
   if (tagPos > 0){
      // If there is a tag, record that as the "OriginalLinkSource"
      tag = calledURL.substring(tagPos+1,calledURL.length);   
   }
   else {
      // If there was no tag, record the entire referring URL in "OriginalLinkSource"
      tag = document.referrer;   
   }
   // Expire in two years...
   expireDate.setTime(expireDate.getTime()+720*24*60*60*1000); 
   mySetCookie("OriginalLinkSource",tag,expireDate);
}