saveMe();
function setCookie (name, value, expires, path, domain, secure)
{
  var curCookie = name + "=" + escape(value) + (expires ? "; expires=" + expires : "") + (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
  document.cookie = curCookie;
}

function getCookie (name) 
{
  var prefix = name + '=';
  var c = document.cookie;
  var nullstring = '';
  var cookieStartIndex = c.indexOf(prefix);
  if (cookieStartIndex == -1)
    return nullstring;
  var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
  if (cookieEndIndex == -1)
    cookieEndIndex = c.length;
  return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function fixDate (date) 
{
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function setAdcode(pi_strName, pi_strRef, pi_iNow)
{
  var t_strVal = getCookie(pi_strName);
  if ( "" == t_strVal || !t_strVal )
  {
    var sUrl = window.location.href;
    var iPos = sUrl.indexOf(pi_strRef);
    if ( iPos > 0 )
    {
      iPos = iPos + pi_strRef.length;
      t_strVal = sUrl.substring(iPos);
      if ( !("" == t_strVal ) )
      {
        var t_iTime = new Date;
        var t_strTimeName = pi_strName + 'time';
        t_iTime = t_iTime.getTime() / 1000;
        t_iTime = parseInt( t_iTime);

        setCookie(pi_strName, t_strVal, pi_iNow, "/", ".joyfax.com", "");
        setCookie(t_strTimeName, t_iTime,pi_iNow, "/", ".joyfax.com", "");
      }
    }
  }
}

function saveMe () 
{
  var now = new Date();
  fixDate(now);
  now.setTime(now.getTime() + 5 * 365 * 24 * 60 * 60 * 1000);
  now = now.toGMTString();

  var _ftime = getCookie("_ftime");
  if ( "" == _ftime || !_ftime ) // Saved _ftime is empty
  {
    var _ftime = new Date();
    _ftime = _ftime.getTime() / 1000;
    _ftime = parseInt( _ftime);
    setCookie("_ftime", _ftime, now, "/", ".joyfax.com", "");
    
    var _ref = document.referrer;
    setCookie("_ref", _ref, now, "/", ".joyfax.com", "");
  }
  
  setAdcode("_adc", "ref=", now);
  setAdcode("_adca", "refa=", now);
  setAdcode("_adcd", "refd=", now);
  setAdcode("_adci", "refi=", now);
}