var blnDoPopAway = true;
var BLN_POPAWAY  = true;

var popURL  = '';
var popH    = 420;
var popW    = 500;
var popCN   = '';
var popCD   = 7;
var popPCT  = 100;
var popSCROLL  = 'yes';

function processOnUnload() {
  setTimeout("delay()",500);
  if(blnDoPopAway && BLN_POPAWAY)
  {
    popAway();
  }
}

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

function setDoPopAway(blnValue) {
  blnDoPopAway = blnValue;
}

function delay() {
  return true;
}

function popAway(URL, height, width, cookiename, cookiedays, percentage, scroll) {
  if (URL)          { popURL = URL; }
  if (height)       { popH   = height; }
  if (width)        { popW   = width; }
  if (cookiename)   { popCN  = cookiename; }
  if (cookiedays!=undefined) { popCD  = cookiedays; }
  if (percentage)   { popPCT = percentage; }
  if (scroll)       { popSCROLL = scroll; }
  
  var randPCT = Math.round(Math.random() * 100);
 
  var strWinFeatures = "resizeable=yes,scrollbars="+popSCROLL+",width="+popW+",height="+popH;
  
  // only set the cookie and pop the window if we're in the right percent
  if (randPCT <= popPCT)
  {
    if (popCN) {
      var allcookies = document.cookie;
      var pos = allcookies.indexOf(popCN);
      var expires = '';
    
      // only pop the window if we don't see the cookie
      if (pos == -1) {
        if(popCD)
        {
            var SECONDS_PER_DAY   = 86400;
            var myMinTimeOut      = SECONDS_PER_DAY*popCD;
            var myDate            = new Date();
            myDate.setTime(myDate.getTime()+(myMinTimeOut*1000));
            expires = " expires="+myDate.toGMTString()+";";
        }
            document.cookie = popCN+"=true;"+expires+" path=/;";
        popAwayWin = window.open(popURL, "popaway", strWinFeatures);
      }
    } else {
      popAwayWin = window.open(popURL, "popaway", strWinFeatures);
    }
  }
  return;
}

function addPopAwayHandlers(URL, height, width, cookiename, cookiedays, percentage, scroll) {
  if (URL)          { popURL = URL; }
  if (height)       { popH   = height; }
  if (width)        { popW   = width; }
  if (cookiename)   { popCN  = cookiename; }
  if (cookiedays!=undefined) { popCD  = cookiedays; }
  if (percentage)   { popPCT = percentage; }
  if (scroll)       { popSCROLL = scroll; }

  var browser = navigator.appName;
  var strBrowser = "";
  
  if (browser == "Microsoft Internet Explorer") {
    strBrowser = "ie";
  }
  else if (browser == "Netscape") {
    strBrowser = "ns";
  }
  else if (browser == "Opera") {
    strBrowser = "op";
  }  

  if(strBrowser=="ie") {
    document.onclick   = processOnClick;
    document.onkeydown = processOnClick;
    window.onunload    = processOnUnload;
  }
  else{
    try{
        window.captureEvents(Event.CLICK|Event.KEYDOWN|Event.UNLOAD);
    }
    catch(e){}
    try{
        window.onclick   = processOnClick;
        window.onkeydown = processOnClick;
        window.onunload  = processOnUnload;
    }
    catch(e){}
  }
}

//Check the URL of last visited page. If user came from a specific section,
//survey can be popped.
function checkReferrer(checkPath, URL, height, width, cookiename, cookiedays){
    if(document.referrer.indexOf(checkPath) != -1){
        popAway(URL, height, width, cookiename, cookiedays);
    }
}
