// Popmatic v23
// (C) 2004-2006 Virtual Surveys Ltd
//
//IMPORTANT - change these variables' values for each survey

//var expdate = new Date();
//expdate.setTime(expdate.getTime() + (vslgCookiePersistence));
//vslfSetCookie("usy46gabsosd","directgovCSA__83958045_9673",expdate,"/");

var vslgSurveyName = "DirectGov_Survey";  //E.g. name+survey
var vslgDefaultLocationNumber = 1;    //E.g. trigger location number
var lang=9;			      //Default to English

var vslgLoc = location.href;
var re = /\/cy\//;
if (re.exec(vslgLoc)) lang = 512;  //Set Welsh from URL

var vslgPopupHeight = 540;
var vslgPopupWidth = 650;
var vslgIntervalURL = 'http://collections.europarchive.org/tna/20080530131623/http://survey.euro.confirmit.com/isa/HMCYGPGAHXJXBHABPDEBCDDRJDMAGPBF/2026_DirectGov/job2026interval.js'; // use job number in name, note different domain to client
var vslgCookiePersistence = 30*24*60*60*1000; //set to 30*24*60*60*1000 if 30 day cookie required. 10 milliseconds for testing

// time allowed for loading external interval script
var vslgIntervalRetry = 500; // 500 milliseconds
var vslgIntervalTimeout = 5000; // 5 seconds

//IMPORTANT don't change anything below this line------------------------------------------------
var vslgInterval =0;     //default 0, will be changed by external file
var vslgDebug = false;
var vslgIntervalID;
var vslgIntervalAttempts = 0; // DO NOT CHANGE
var surveyLater, currentTime, surveyLaterStart;

//Randomly pick which pop-up method to use
var rand = Math.random();
var myLimit = 2;
//var method = Math.floor(rand*myLimit)+1;
//if (vslfGetCookie("Do_Survey_Later")) method = 2;

var method = 2; //Force to be interstitial all the time

var vslgPopupURL;
var vslgSurveyURL = 'http://collections.europarchive.org/tna/20080530131623/http://survey.euro.confirmit.com/wix/p81374226.aspx';
if (method == 1) vslgPopupURL = vslgSurveyURL;
if (method == 2 && lang == 9) vslgPopupURL = 'http://collections.europarchive.org/tna/20080530131623/http://www.els-services.co.uk/directgov/dgpopups/interstitial_page.htm';
if (method == 2 && lang == 512) vslgPopupURL = 'http://collections.europarchive.org/tna/20080530131623/http://www.els-services.co.uk/directgov/dgpopups/interstitial_page_cy.htm';

var vslSpeedTrapData;

//global variable for popup window itself, init to undefined
var vslgPu;
//geometry
var vslgSWidth = 0; var vslgSHeight = 0; var vslgWWidth = 0; var vslgWHeight = 0;

// size of screen
if (window.screen)
{
  vslgSWidth = window.screen.width; vslgSHeight = window.screen.height;
}

// size of browser window, excluding chrome.
if( typeof( window.innerWidth ) == 'number' )
{ //Non-IE
  vslgWWidth = window.innerWidth;
  vslgWHeight = window.innerHeight;
}
else if (document.documentElement &&
         (document.documentElement.clientWidth || document.documentElement.clientHeight) )
{ //IE 6+ in 'standards compliant mode'
  vslgWWidth = document.documentElement.clientWidth;
  vslgWHeight = document.documentElement.clientHeight;
}
else if (document.body && (document.body.clientWidth || document.body.clientHeight) )
{ //IE 4 compatible
  vslgWWidth = document.body.clientWidth;
  vslgWHeight = document.body.clientHeight;
}

// operating system
var vslgOS = 3;
vslfDLog("vslgOS="+vslgOS);
vslfDLog(navigator.userAgent);
if (navigator.userAgent.match(/Windows/)) vslgOS=1;
if (navigator.userAgent.match(/Macintosh/)) vslgOS=2;
vslfDLog("vslgOS="+vslgOS);

var vslgCook = 3;
if (navigator.appName=="Microsoft Internet Explorer")
{
  if (navigator.cookieEnabled==true)
  {
    vslgCook = 1;
  }
  else
  {
    vslgCook = 2;
  }
}

var vslgrandomnum = (new Date()).getTime();
vslgrandomnum = Math.round(Math.abs(Math.sin(vslgrandomnum) * 1000000)) % 1000;
var vslgHitSample = false;
if (vslgInterval != 0)
{
  vslgHitSample = ((vslgrandomnum + 1) * vslgInterval /1000 <= 1) && vslgCook != "2" && vslfGetCookie(vslgSurveyName) != 1;
}
vslfDLog("vslgInterval ="+vslgInterval+",  vslgHitSample ="+vslgHitSample);

function vslfGetCookieVal (offset)
{
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function vslfGetCookie (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 vslfGetCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0)
      break;
  }
  return null;
}

function vslfSetCookie (name, value)
{
  var argv = vslfSetCookie.arguments;
  var argc = vslfSetCookie.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 getSpeedTrapSessionNumber()
{
			
  var re=/directgovCSA__(\d*)_/gi;
  re.exec(document.cookie);
  return RegExp.$1;
			
}

// takes mandatory location number argument; -1 to use default
function vslfDoPop(uC, pLink)
{
  if (uC == -1) {uC = vslgDefaultLocationNumber;}

   //var oldhref = pLink.href;

  if (vslgHitSample)
  {
      if (method == 1){

        if(vslgPu != undefined && !vslgPu.closed && vslgPu.location)
        {
          vslgPu.location.href = vslgPopupURL+"?c="+uC+"&l="+lang+"&w="+vslgSWidth+"&h="+vslgSHeight+"&wW="+vslgWWidth+"&wH="+vslgWHeight+"&k="+vslgCook+"&os="+vslgOS+"&method=1&speedt="+vslSpeedTrapData;
        }
        else
        {
          vslgPu=window.open(vslgPopupURL+"?c="+uC+"&l="+lang+"&w="+vslgSWidth+"&h="+vslgSHeight+"&wW="+vslgWWidth+"&wH="+vslgWHeight+"&k="+vslgCook+"&os="+vslgOS+"&si="+vslgInterval+"&method=1&speedt="+vslSpeedTrapData,vslgSurveyName+Math.floor(Math.random()*10e20),"height="+vslgPopupHeight+",width="+vslgPopupWidth+",resizable=yes,scrollbars=yes,toolbar=no,menubar=no");
          if (!vslgPu.opener) {vslgPu.opener = self;}
          var expdate = new Date();
          expdate.setTime(expdate.getTime() + (vslgCookiePersistence));
          vslfSetCookie(vslgSurveyName,"1",expdate,"/");
        }
        if (self.focus && vslgPu) {self.focus();}

      }
      
      if (method == 2){

        pLink.href = vslgPopupURL+"?oh="+pLink.href+"&speedt="+vslSpeedTrapData;

        var expdate = new Date();
        expdate.setTime(expdate.getTime() + (vslgCookiePersistence));
        vslfSetCookie(vslgSurveyName,"1",expdate,"/");

      }

  }
} // end vslfDoPop()

function vslfCheck(pH,pW,pU)
{
  // only include those in sample,
  // Screen out those with cookies turned off (IE only check)
  // and exclude seen-befores
  if (vslgHitSample)
  {
    //var expdate = new Date();
    //expdate.setTime(expdate.getTime() + (vslgCookiePersistence));
    //vslfSetCookie(vslgSurveyName,"1",expdate,"/");
    var browserVer = parseInt(navigator.appVersion);

    var popH = 0;
    var popW = 0;
    var popUrl = "";
    var original = "";

    function fixPage(h,w,u)
    {
      popH = h;
      popW = w;
      popUrl = u;
      fixLinks();
    }

    function fixLinks()
    {

      // iterate through all anchor links on the page
      var docPs = document.getElementsByTagName("a");
      for(var i=0;i<docPs.length;i++)
      {
 
        var currentLink = docPs[i].href;
        if (docPs[i].href && docPs[i].protocol == "http:" && !docPs[i].target)
        {

          // check to see if the element has already got an onclick event handler
          if (!docPs[i].onclick)
          {
            docPs[i].onclick = function()
                               {
                                 vslfDoPop(vslgDefaultLocationNumber, this);
                               };
          }
          else
          {
            var curr_on = docPs[i].onclick;
            docPs[i].onclick = function()
                               {
                                 vslfDoPop(vslgDefaultLocationNumber, this);
                                 curr_on();
                               };
          }

          //Rewrite link text for screenreader
          if (docPs[i].innerHTML && method == 1){ 
            docPs[i].innerHTML = docPs[i].innerHTML + '<span style="position: absolute; left: -3000px"> - This link will launch a survey in a separate window</span>';
          }
          if (docPs[i].innerHTML && method == 2){ 
            docPs[i].innerHTML = docPs[i].innerHTML + '<span style="position: absolute; left: -3000px"> - This link will take you to a survey invite before this page</span>';
          }

          // check to see if the element has already got a title attribute
          if (docPs[i].title != "" && method == 1)
          {
            docPs[i].title = docPs[i].title + ". This link launch a survey in a separate window";
          }
          else
          {
            docPs[i].title = "This link launch a survey in a separate window";
          }
          if (docPs[i].title != "" && method == 2)
          {
            docPs[i].title = docPs[i].title + ". This link will take you to a page asking you to participate in a survey";
          }
          else
          {
            docPs[i].title = "This link will take you to a page asking you to participate in a survey";
          }
        }
      } //end anchor links iteration
    } // end of fixLinks function
    
    // IMPORTANT: next line sets height, width and location of survey respectively
    fixPage(pH,pW,pU);
  }
}

function vslfDLog(msg)
{
  if (vslgDebug)
    document.getElementById("debugOut").innerHTML+=msg+"<br />";
}


function vslfCheckIntervalFile()
{
  vslfDLog("vslfCheckIntervalFile(), vslgInterval = "+vslgInterval);

  try
  {
    setSampleIntervalRemote();
    if (vslgInterval != 0)
    {
      vslgHitSample = ((vslgrandomnum + 1) * vslgInterval /1000 <= 1) && vslgCook != "2" && vslfGetCookie(vslgSurveyName) != 1;
      //if (surveyLaterStart){
      //  vslgHitSample = 1;
      //}
    }
    clearInterval(vslgIntervalID);
    vslfCheck(vslgPopupHeight,vslgPopupWidth,vslgPopupURL);
  }
  catch (e)
  {
    vslfDLog("e.name = "+e.name);
    vslfDLog("e.message = "+e.message);

    vslgIntervalAttempts++;
    if ((vslgIntervalAttempts * vslgIntervalRetry) >  vslgIntervalTimeout)
    {
      clearInterval(vslgIntervalID);
    }
  }
}

function setSampleInterval(remoteInterval)
{
  vslfDLog("setSampleInterval("+remoteInterval+") called");
  vslgInterval = remoteInterval;
}

if (!window.onload)
{
  window.onload = function()
                  {
                    var remoteScript=document.createElement('script');
                    remoteScript.id = 'vsltrs';
                    remoteScript.setAttribute('type','text/javascript');
                    remoteScript.setAttribute('src',vslgIntervalURL);
                    var hd=document.getElementsByTagName('head')[0];
                    hd.appendChild(remoteScript);

                    //if (vslgCook != "2") surveyLater = vslfGetCookie("Do_Survey_Later");
                    //if (surveyLater){
                    //  currentTime = new Date().getTime();
                    //  if ( ((currentTime - surveyLater) / 1000) > 300) surveyLaterStart = 1; //5 mins have passed
                    //}
                    vslgIntervalID = setInterval("vslfCheckIntervalFile()",vslgIntervalRetry);
		    vslSpeedTrapData = getSpeedTrapSessionNumber(); 
                  }
}
else
{
  curr_onload = window.onload;
  window.onload = function()
                  {
                    curr_onload();
                    var remoteScript=document.createElement('script');
                    remoteScript.id = 'vsltrs';
                    remoteScript.setAttribute('type','text/javascript');
                    remoteScript.setAttribute('src',vslgIntervalURL);
                    var hd=document.getElementsByTagName('head')[0];
                    hd.appendChild(remoteScript);

                    //if (vslgCook != "2") surveyLater = vslfGetCookie("Do_Survey_Later");
                    //if (surveyLater){
                    //  currentTime = new Date().getTime();
                    //  if ( ((currentTime - surveyLater) / 1000) > 300) surveyLaterStart = 1; //5 mins have passed
                    //}
                    vslgIntervalID = setInterval("vslfCheckIntervalFile()",vslgIntervalRetry);
		    vslSpeedTrapData = getSpeedTrapSessionNumber(); 
                  };
}


