    /*
    **   ref.js -- capture original external referrer in cookie.
    */

    function getCookie(strCookieName) {
      if (typeof document.cookie == "undefined")
	return("");
      var strAllCookies = document.cookie;
      var i, j, k = 0;
      while ((i = strAllCookies.indexOf(strCookieName + "="), j) > 0) {
        if ((i == 0) || (strAllCookies.substr((i - 1), 1) == ";"))
	  break;
	j = i + 1;
      }
      if (i < 0)
        return("");
      if ((k = strAllCookies.indexOf(";", (i + strCookieName.length))) > 0)
        return(strAllCookies.substring((i + strCookieName.length + 1), k));
      else
        return(strAllCookies.substring(i + strCookieName.length + 1));
    }

    function setCookie(strCookieName, strCookieValue) {
      var intMillisecondsToExpireDate = (180 * 24 * 60 * 60000) /* 180 days */
      var strExpireDateClause = "";
      if ((setCookie.arguments.length > 2) &&
          (setCookie.arguments[2] == "")) {
        strExpireDateClause = "";
      }
      else if ((setCookie.arguments.length > 2) &&
               (setCookie.arguments[2] != "") &&
               (Date(setCookie.arguments[2]) != "Invalid Date")) {
        strExpireDateClause = "; expires=" + validValue.arguments[2];
      }
      else {
        var objCurrDate = new Date();
        var intCurrDate = objCurrDate.valueOf();
        var objExpireDate = new Date(intCurrDate + intMillisecondsToExpireDate);
        strExpireDateClause = "; expires=" + objExpireDate.toGMTString();
      }
      document.cookie = strCookieName + "=" + strCookieValue + strExpireDateClause + "; path=/";
      return(true);
    }

    function checkCookie() {
      if ((getCookie("origRef") == "") &&
          (typeof document.referrer != "undefined") &&
          (document.referrer != "")) {
	/* No "origRef" cookie and there is a referrer. */
        var i, j;
	if (((i = document.referrer.indexOf("://")) > 0) &&
	    ((j = document.referrer.indexOf(window.location.hostname)) != (i + 3))) {
	  /* The referrer was not this host (i.e.: it was external). */
	  setCookie("origRef", 
		    escape("URL=" + escape(document.referrer) + 
		    	   "&Date=" + escape((new Date()).toGMTString()) +
			   "&VisitURL=" + escape(window.location)));
        }
      }
      return(true);
    }

    function displayCookie() {
      var strOrigRef = unescape(getCookie("origRef"));
      if (strOrigRef == "") {
	alert("No origRef cookie.");
        return(true);
      }
      /* Parse out and unescape the URL and Date */
      var i, j;
      var strURL = "";
      if (((i = strOrigRef.indexOf("URL=")) >= 0) &&
	  ((i == 0) || (strOrigRef.substr((i - 1), 1) == "&"))) {
        if ((j = strOrigRef.indexOf("&", (i + 4))) > 0) {
	  strRefURL = unescape(strOrigRef.substring((i + 4), j));
        }
	else {
	  strRefURL = unescape(strOrigRef.substring(i + 4));
        }
      }
      var strDate = "";
      if (((i = strOrigRef.indexOf("Date=")) >= 0) &&
	  ((i == 0) || (strOrigRef.substr((i - 1), 1) == "&"))) {
        if ((j =  strOrigRef.indexOf("&", (i + 5))) > 0) {
	  strDate = unescape(strOrigRef.substring((i + 5), j));
        }
	else {
	  strDate = unescape(strOrigRef.substring(i + 5));
        }
      }
      var strVisitURL = "";
      if (((i = strOrigRef.indexOf("VisitURL=")) >= 0) &&
	  ((i == 0) || (strOrigRef.substr((i - 1), 1) == "&"))) {
        if ((j = strOrigRef.indexOf("&", (i + 9))) > 0) {
	  strVisitURL = unescape(strOrigRef.substring((i + 9), j));
        }
	else {
	  strVisitURL = unescape(strOrigRef.substring(i + 9));
        }
      }
      alert("origRef cookie contents:\n\n" +
	    " getCookie Result:\n" +
            "    " + getCookie("origRef") + "\n\n" +
	    " Unparsed Cookie:\n" +
	    "    " + strOrigRef + "\n\n" +
	    " Parsed Cookie:\n" +
	    "   Ref URL = " + strRefURL + "\n" +
	    "      Date = " + strDate + "\n" +
	    " Visit URL = " + strVisitURL);
      return(true);
    }
