//Popmatic v26
// (C) 2004-2009 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

var vslgSurveyName = "NetAPorter_Survey_2678";  //E.g. name+survey

var triggerLocation = new Array();
var vslgDefaultLocationNumber = 97;  //Default

triggerLocation[0] = { slocation: "http://www.net-a-porter.com/", channel: "intl", locNumber: 1 };
triggerLocation[1] = { slocation: "http://www.net-a-porter.com/", channel: "am", locNumber: 2 };
triggerLocation[2] = { slocation: "http://www.theoutnet.com/", channel: "intl", locNumber: 3 };
triggerLocation[3] = { slocation: "http://www.theoutnet.com/", channel: "am", locNumber: 4 };

  for (var i=0; i<triggerLocation.length; i++){

    if ( (triggerLocation[i].slocation && document.location.toString().indexOf(triggerLocation[i].slocation) > -1) && channel && channel == triggerLocation[i].channel) vslgDefaultLocationNumber = triggerLocation[i].locNumber;

  }

var section = 97; // Any section not in list below;
var sectionNap = ["/Shop/Whats-New","/Shop/Designers","/Shop/AZDesigners","/Shop/Clothing","/Shop/Bags","/Shop/Shoes","/Shop/Accessories","/Content/Boutiques","/Shop/List","/magazine","/product/"];
var sectionOutNet = ["/Shop/Just-In","/Shop/Designers","/Shop/Clothing","/Shop/Bags","/Shop/Shoes","/Shop/Accessories","/Shop/Dress-Me","/Shop/List","/flashsales","/product/"]

if (document.location == "http://www.net-a-porter.com/") section = 1;
if (document.location == "http://www.theoutnet.com/") section = 101;

for (var i=0; i<=sectionNap.length; i++){
  if (vslgDefaultLocationNumber <= 2 && document.location.toString().indexOf(sectionNap[i]) > -1) section = i+2;
}
for (var i=0; i<=sectionOutNet.length; i++){
  if (vslgDefaultLocationNumber >= 3 && document.location.toString().indexOf(sectionOutNet[i]) > -1) section = 100+i+2;
}


var lang=9;			      //Default to English

var vslgPopupHeight = 540;
var vslgPopupWidth = 650;
var vslgIntervalURL = window.location.protocol + '//survey.euro.confirmit.com/isa/HMCYGPGAHXJXBHABPDEBCDDRJDMAGPBF/2678_Net_A_Porter/job2678interval.js'; // use job number in name, note different domain to client
var vslgPopupURL = window.location.protocol + '//survey.euro.confirmit.com/wix/p315874669.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 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 && vslgDefaultLocationNumber > 0) {

      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);

  vslgIntervalID = setInterval("vslfCheckIntervalFile()",vslgIntervalRetry);

}

function attachDoPop(targetElement){

  // 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();
    }
  }

  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="+lang+"&w="+vslgSWidth+"&h="+vslgSHeight+"&wW="+vslgWWidth+"&wH="+vslgWHeight+"&k="+vslgCook+"&os="+vslgOS+"&si="+vslgInterval+"&pt="+popUpType+"&section="+section;
  
  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;

  }
}
