// Popmatic v23
// (C) 2004-2006 Virtual Surveys Ltd
//
//IMPORTANT - change these variables' values for each survey
var vslgSurveyName = "COOP_survey_2322";  //E.g. name+survey

var vslgDefaultLocationNumber = 3;  //Default
//if (document.location == "http://www.google.com/") var vslgDefaultLocationNumber = 1;
//if (document.title == "Google") vslgDefaultLocationNumber = 2;

var lang=9;			      //Default to English
var vslgPopupHeight = 540;
var vslgPopupWidth = 650;
var vslgIntervalURL = 'http://survey.euro.confirmit.com/isa/HMCYGPGAHXJXBHABPDEBCDDRJDMAGPBF/2322_CoOp/job2322interval.js'; // use job number in name, note different domain to client
//var vslgPopupURL = 'popupchild23.htm';
var vslgPopupURL = 'http://survey.euro.confirmit.com/wix/p149105201.aspx';
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 vslgIntervalID;
var vslgIntervalAttempts = 0; // DO NOT CHANGE

var vslgPu; //global variable for popup window itself, init to undefined
var vslgSWidth = 0; var vslgSHeight = 0; var vslgWWidth = 0; var vslgWHeight = 0; //geometry

// 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;
if (navigator.userAgent.match(/Windows/)) vslgOS=1;
if (navigator.userAgent.match(/Macintosh/)) vslgOS=2;

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;

}

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" : "");
}


// takes mandatory location number argument; -1 to use default
function vslfDoPop(uC) {

  var vslFullUrl = vslgPopupURL+"?c="+uC+"&l="+lang+"&w="+vslgSWidth+"&h="+vslgSHeight+"&wW="+vslgWWidth+"&wH="+vslgWHeight+"&k="+vslgCook+"&os="+vslgOS+"&si="+vslgInterval;

  if (uC == -1) {
    uC = vslgDefaultLocationNumber;
  }
  if (vslgHitSample && vslfGetCookie(vslgSurveyName) != 1) {

    if (vslgPu != undefined && !vslgPu.closed && vslgPu.location) {

      vslgPu.location.href = vslFullUrl;

    } else {

      vslgPu=window.open(vslFullUrl,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();}
  }
} // end vslfDoPop()

function attachDoPop(targetElement){

  // FOR SPECIFIC SITES ONLY checks to see if the element has a target attribute and rewrite it to self
  // if (targetElement.target)
  // {
  //   targetElement.target = "_self";
  // }

  // check to see if the element has already got an onclick event handler
  if (!targetElement.onclick)
  {
    targetElement.onclick = function() {
      vslfDoPop(vslgDefaultLocationNumber);
    }
  } else {

    targetElement.curr_on = targetElement.onclick;
    targetElement.onclick = function() {
      vslfDoPop(vslgDefaultLocationNumber);
      this.curr_on();
    }
  }

  // check to see if the element has already got a title attribute
  if (targetElement.title != ""){

    targetElement.title = targetElement.title + ". This link also opens a new window";

  }else{

    targetElement.title = "This link also opens a new window";

  }
}

function fixLinks(){

  // Useful to get a specigic button/element working if it has an id
  if (document.getElementById("buttonNext")) {

    websiteButton = document.getElementById("buttonNext");
    websiteButton.onclick = function() {

      vslfDoPop(vslgDefaultLocationNumber);
      curr_on();

    }
  }

  // iterate through all anchor links on the page
  var docPs = document.getElementsByTagName("a");

  for(var i=0;i<docPs.length;i++){

//    if (docPs[i].href){
    if (docPs[i].href && docPs[i].href == 'https://applications.co-operativebank.co.uk/apps/funnel.faces?media=799/11/99626') {
      attachDoPop(docPs[i]);

    }
  }

  // iterate through all imagemap area links on the page
  docPs = document.getElementsByTagName("input");

  for(i=0;i<docPs.length;i++){

    if (docPs[i].href){

      attachDoPop(docPs[i]);

    }
  }
} // end of fixLinks function
    
function fixPage(h,w,u) {

  popH = h;
  popW = w;
  popUrl = u;
  fixLinks();

}

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 browserVer = parseInt(navigator.appVersion);

    var popH = 0;
    var popW = 0;
    var popUrl = "";
    var original = "";

    // IMPORTANT: next line sets height, width and location of survey respectively
    fixPage(pH,pW,pU);
  }
}

function vslfCheckIntervalFile() {

  try {
    setSampleIntervalRemote();
    if (vslgInterval != 0) {

      vslgHitSample = ((vslgrandomnum + 1) * vslgInterval /1000 <= 1) && vslgCook != "2" && vslfGetCookie(vslgSurveyName) != 1;

    }
    clearInterval(vslgIntervalID);
    vslfCheck(vslgPopupHeight,vslgPopupWidth,vslgPopupURL);
  }
  catch (e) {

    vslgIntervalAttempts++;
    if ((vslgIntervalAttempts * vslgIntervalRetry) >  vslgIntervalTimeout) {

      clearInterval(vslgIntervalID);

    }
  }
}

function setSampleInterval(remoteInterval) {

  vslgInterval = remoteInterval;

}

function attachRemoteScript(){

  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);
  vslgIntervalID = setInterval("vslfCheckIntervalFile()",vslgIntervalRetry);

}

if (!window.onload){

  window.onload = function(){

    attachRemoteScript();

  }

}else{

  curr_onload = window.onload;

  window.onload = function() {

    curr_onload();
    attachRemoteScript();

  }
}

