//Popmatic v26
// (C) 2004-2010 Virtual Surveys Ltd
//
//IMPORTANT - change these variables' values for each survey

var demomode = 1; //1 = live, 2 = demo
var i = 0;

var popUpType = 1; //1 = popamatic, 2 = interstital
var interstitial_type = 1; //1 = link-based, 2 = redirect-based

//NEVER use a hyphen (-) in name below
var vslgSurveyName = "PGI_2681";  //E.g. name+survey

var triggerLocation = new Array();
var vslgDefaultLocationNumber = 1;  //Default

var vsltest, vslregexp;

vslregexp = /\/en-GB\//i;
vsltest = vslregexp.test(document.location.href)
if (vsltest == true) vslgDefaultLocationNumber = 1;

vslregexp = /\/en-US\//i;
vsltest = vslregexp.test(document.location.href)
if (vsltest == true) vslgDefaultLocationNumber = 2;

vslregexp = /\/en\//i;
vsltest = vslregexp.test(document.location.href)
if (vsltest == true) vslgDefaultLocationNumber = 3;

vslregexp = /\/zh-CN\//i;
vsltest = vslregexp.test(document.location.href)
if (vsltest == true) vslgDefaultLocationNumber = 4;

vslregexp = /\/ja\//i;
vsltest = vslregexp.test(document.location.href)
if (vsltest == true) vslgDefaultLocationNumber = 5;

vslregexp = /\/it\//i;
vsltest = vslregexp.test(document.location.href)
if (vsltest == true) vslgDefaultLocationNumber = 6;

vslregexp = /\/de\//i;
vsltest = vslregexp.test(document.location.href)
if (vsltest == true) vslgDefaultLocationNumber = 7;

vslregexp = /\/fr\//i;
vsltest = vslregexp.test(document.location.href)
if (vsltest == true) vslgDefaultLocationNumber = 8;

var vslgLang
if (vslgDefaultLocationNumber == 1 || vslgDefaultLocationNumber == 2 || vslgDefaultLocationNumber == 3) vslgLang = 9; // UK / US / India
if (vslgDefaultLocationNumber == 4) vslgLang = 4; // China
if (vslgDefaultLocationNumber == 5) vslgLang = 17; // Japan
if (vslgDefaultLocationNumber == 6) vslgLang = 16; // Italy
if (vslgDefaultLocationNumber == 7) vslgLang = 7; // Germany
if (vslgDefaultLocationNumber == 8) vslgLang = 12; // France

var vslgPopupHeight = 540;
var vslgPopupWidth = 650;
var vslgIntervalURL = 'http://survey.euro.confirmit.com/isa/HMCYGPGAHXJXBHABPDEBCDDRJDMAGPBF/2861_PGI/job2861interval.js'; // use job number in name, note different domain to client
//var vslgPopupURL = 'interstitial_page_example.htm'; //Interstitial + Exit Example
var vslgPopupURL = 'http://survey.euro.confirmit.com/wix/p383406102.aspx';
var vslgCookiePersistence = 90*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 surveyLater, currentTime, surveyLaterStart;

var vslgPu; //global variable for popup window itself, init to undefined
var vslgSWidth = 0; 
var vslgSHeight = 0; 
var vslgWWidth = 0; 
var vslgWHeight = 0;

var vslgOS = 3;
var vslgCook = 3;

getScreenDimensions();
getOS();
cookiesEnabled();

var vslgrandomnum = Math.floor(Math.random()*1000);

var vslgHitSample = false;

/*
if (demomode == 1){

  if (!window.onload){

    window.onload = function(){

      attachRemoteScript();

    }

  }else{

    curr_onload = window.onload;

    window.onload = function() {

      curr_onload();
      attachRemoteScript();

    }
  }

} else {

  var dropdown = document.createElement("select")
  dropdown.id = "locationSelector";

  var option2 = new Array();

  for (var i=0; i < triggerLocation.length; i++){

    option2[i] = document.createElement("option");
    if (triggerLocation[i].slocation) option2[i].innerHTML = "Location: " + triggerLocation[i].locNumber + " - URL: " + triggerLocation[i].slocation;
    if (triggerLocation[i].stitle) option2[i].innerHTML = "Location: " + triggerLocation[i].locNumber + " - Page Title: " + triggerLocation[i].stitle;
    option2[i].value = triggerLocation[i].locNumber;
    dropdown.appendChild(option2[i]);

  }

  document.body.appendChild(dropdown)

}
*/

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 vslfCheckIntervalFile() {

  try {
    setSampleIntervalRemote();

    if (vslgInterval != 0) {

      vslgHitSample = ((vslgrandomnum + 1) * vslgInterval /1000 <= 1) && vslgCook != "2" && vslfGetCookie(vslgSurveyName) != 1;

    }
    clearInterval(vslgIntervalID);

    if (vslgHitSample) {

      fixLinks();
    }

  }
  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);

  //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);

}

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,this);
    }
  } else {

    targetElement.curr_on = targetElement.onclick;
    targetElement.onclick = function() {
      vslfDoPop(vslgDefaultLocationNumber,this);
      this.curr_on();
    }
  }

  //Rewrite link text for screenreader
  //if (targetElement.innerHTML){ 
  //  targetElement.innerHTML = targetElement.innerHTML + '<span style="position: absolute; left: -3000px"> - This link will take you to a survey invite before this page</span>';
  //}

  var titleText;
  if (popUpType == 1) titleText = "This link also opens a new window";
  if (popUpType == 2) titleText = "This link will take you to a page asking you to participate in a survey";

  // check to see if the element has already got a title attribute
  if (targetElement.title != ""){

    targetElement.title = targetElement.title + "." + titleText;

  } else {

    targetElement.title = titleText;

  }
}

function fixLinks(){

  if (popUpType == 1 || (popUpType == 2 && interstitial_type == 1)){

    // iterate through all anchor links on the page
    var docPs = document.getElementsByTagName("a");
  
    for(var i=0;i<docPs.length;i++){
  
      if (popUpType == 1 && (docPs[i].href)){
  
        attachDoPop(docPs[i]);
  
      }
  
      if (popUpType == 2 && interstitial_type == 1 && (docPs[i].href && (docPs[i].protocol == "http:" || docPs[i].protocol == "https:" ) && !docPs[i].target && !docPs[i].onclick)){
  
        attachDoPop(docPs[i]);
  
      }
    }
  
    // iterate through all input tags on the page
    docPs = document.getElementsByTagName("input");
  
    for(i=0;i<docPs.length;i++){
  
      if (popUpType == 1 && (docPs[i].type == "submit" || docPs[i].type == "button")){
  
        attachDoPop(docPs[i]);
  
      }
    }
  }

  if (popUpType == 2 && interstitial_type == 2) {

    vslfDoPop(vslgDefaultLocationNumber);

  }
}


// takes mandatory location number argument; -1 to use default
function vslfDoPop(uC, pLink) {

  var vslFullUrl = vslgPopupURL+"?c="+uC+"&l="+vslgLang+"&w="+vslgSWidth+"&h="+vslgSHeight+"&wW="+vslgWWidth+"&wH="+vslgWHeight+"&k="+vslgCook+"&os="+vslgOS+"&si="+vslgInterval+"&pt="+popUpType;
  
  if (uC == -1) {
    uC = vslgDefaultLocationNumber;
  }

  if (vslgHitSample && vslfGetCookie(vslgSurveyName) != 1) {

    if (popUpType == 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();}
    }

    if (popUpType == 2 && interstitial_type == 1){

      pLink.href = vslgPopupURL+"?oh="+pLink.href+"&c="+vslgDefaultLocationNumber;

      var expdate = new Date();
      expdate.setTime(expdate.getTime() + (vslgCookiePersistence));
      vslfSetCookie(vslgSurveyName,"1",expdate,"/");

    }

    if (popUpType == 2 && interstitial_type == 2){

      var expdate = new Date();
      expdate.setTime(expdate.getTime() + (vslgCookiePersistence));
      vslfSetCookie(vslgSurveyName,"1",expdate,"/");

      window.location = vslgPopupURL+"?oh="+window.location.href+"&c="+vslgDefaultLocationNumber;

    }
  }
}

function getScreenDimensions(){

  // 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;
  }

}

function getOS(){

  // operating system
  if (navigator.userAgent.match(/Windows/)) vslgOS=1;
  if (navigator.userAgent.match(/Macintosh/)) vslgOS=2;
  
}

function cookiesEnabled(){

  vslfSetCookie("cookieenabledcheck","1");
  if (vslfGetCookie("cookieenabledcheck") == 1){

    vslgCook = 1;

  } else {

    vslgCook = 2;

  }
}

//function fivePageClicksCookie(){
//
//  if (vslgCook != "2"){
//    surveyLater = vslfGetCookie("popSurvey_clicks");
//    if (surveyLater){
//      surveyLater++;
//      vslfSetCookie("popSurvey_clicks",surveyLater)
//    } else {
//      vslfSetCookie("popSurvey_clicks","1")
//    }
//
//  }
//}

