  var blnDoPopAway = true;
  var BLN_POPAWAY  = true;
  var allcookies   = document.cookie;
  var pos          = allcookies.indexOf("exitpop");

  function hasSentGreeting()
  {
    if (allcookies.indexOf("AMG_usage") != -1)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  function isCategoryPageOne()
  {
    var ret   = 1;
    var query = getQuery()
    i = query.indexOf('adisplay');
    //if we are on the category page then return false if not page 1
    if ((location.href.indexOf('category') != -1) && (i != -1))
    {
      pagenum = query.substring(i+9, i+10);
      if (pagenum != "1")
      {
        ret = 0;
      }
    }
    return ret;
  }

  function processOnUnload()
  {
    if (blnDoPopAway && !hasSentGreeting() && BLN_POPAWAY && isCategoryPageOne())
    {
      exit();
    }
  }

  function exit()
  {
  /*
    var URL = "http://" + location.host + "/exit.pd";
    if (window.location.href.indexOf('view') != -1)
    {
      URL = URL + "?p=view";
    }
    myCookieDays    = 7;
    SECONDS_PER_DAY = 86400;
    myMinTimeOut    = SECONDS_PER_DAY*myCookieDays;
    myDate          = new Date();
    myDate.setTime(myDate.getTime()+(myMinTimeOut*1000));
    document.cookie = "exitpop=true; expires=" + myDate.toGMTString();
    window.open(URL);
    return;
  */
  }

  function processOnClick()
  {
    setDoPopAway(false);
    setTimeout("delay()",500);
  }

  function setDoPopAway(blnValue)
  {
    blnDoPopAway = blnValue;
  }

  function delay()
  {
    return true;
  }

  function addPopAwayHandlers()
  {
    // only set the popaway handlers if we don't see the cookie
    if (pos == -1)
    {
      var browser = navigator.appName;
      if (browser=="Microsoft Internet Explorer")
      {
        document.onclick   = processOnClick;
        window.onunload    = processOnUnload;
      }
      else if (browser=="Netscape")
      {
        window.captureEvents(Event.CLICK|Event.KEYDOWN|Event.UNLOAD);
        window.onclick   = processOnClick;
        window.onunload  = processOnUnload;
      }
    }
  }

  function getQuery()
  {
	  var query = location.search.substring(1);
	  return query;
  }
