var TestLength=26;
var lastClicked;

function getCookieVal(offset)
{
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
      Endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}


function GetCookie (name)
{
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen)
   {
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg)
         return getCookieVal (j);
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0)
   break;
   }
   return null;
}

function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

function InitPop()
{
   lastClicked = 'N/A';

   for(i=0;i<document.links.length;i++)
   {
      if (document.links[i].href.substr(document.links[i].href.length-1, document.links[i].href.length) != '#')
         document.links[i].onclick = lastClick;
   }
   
   for(i=0;i<document.forms.length;i++)
   {
//      alert('formstart ' + document.forms[i].length);
      for(j=0;j<document.forms[i].length;j++)
      {
//          alert(document.forms[i].elements[j]);
         document.forms[i].elements[j].onclick = lastClick;
      }
   }
}

function lastClick()
{
   lastClicked = this;
}

function PopUpInit()
{
//   alert('Name=*' + lastClicked.name + '*');

   if (lastClicked == 'N/A')
      PopUp();
   else if ((lastClicked.href.substr(0, TestLength) == document.location.href.substr(0, TestLength)) || (lastClicked.href.substr(0, 25) == 'https://secure.invers.nl/') || (lastClicked.name == ''))
      {
//         alert('nopop ' + lastClicked.name);
      }
   else
      PopUp();
}

function PopUp()
{
   var ExPopIdx = GetCookie('EP_EXPOP_Idx');
   var Shown = GetCookie('EP_EXPOP_Shown' + ExPopIdx);

   if (Shown != 1)
   {

      var enabled="";
      var exp = new Date(); 
      exp.setTime(exp.getTime() + (60*1000)); // valid: one minute (60seconds*1000 hundreds)
      SetCookie ('testcookie', '1', exp);
      Enabled = GetCookie('testcookie');

      if (Enabled)
      {
         window.open('exit_popup.php', '', 'WIDTH=1,HEIGHT=1');
      }
      else
      {
         window.open('exit_popup.php?CookieTestFailed=True', '', 'WIDTH=1,HEIGHT=1');
      }

      var exp = new Date(); 
      exp.setTime(exp.getTime() + (1000*60*60*24*30)); // valid: one minute (60seconds*1000 hundreds)
      SetCookie ('EP_EXPOP_Shown' + ExPopIdx, '1', exp);


/*
      document.cookie = 'test';
      if (document.cookie.indexOf('test') > -1)
      {
         window.open('exit_popup.php', '', 'WIDTH=1,HEIGHT=1');
      }
      else
      {
         window.open('exit_popup.php?CookieTestFailed=True', '', 'WIDTH=1,HEIGHT=1');
      }
*/

   }
}