// Popmatic v23
// (C) 2004-2006 Virtual Surveys Ltd
//
//IMPORTANT - change these variables' values for each survey
var vslgSurveyName = "Vodafone_Survey_1573";  //E.g. name+survey
var vslgDefaultLocationNumber = 1;    //E.g. trigger location number
var lang=9;			      //Default to English
var vslgPopupHeight = 540;
var vslgPopupWidth = 650;
var vslgIntervalURL = 'http://survey.euro.confirmit.com/isa/HMCYGPGAHXJXBHABPDEBCDDRJDMAGPBF/1580_Vodafone/1580_Vodafone_interval_1.js'; // use job number in name, note different domain to client
//var vslgPopupURL = 'popupchild23.htm';
var vslgPopupURL = 'http://survey.euro.confirmit.com/wix/p29282233.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 chnaged by external file
var vslgDebug = false;
var vslgIntervalID;
var vslgIntervalAttempts = 0; // DO NOT CHANGE

vslfDLog("Started - v23b");

//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;
  }
}
vslfDLog("vslgCook ="+vslgCook);

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" : "");
}


// takes mandatory location number argument; -1 to use default
function vslfDoPop(uC)
{
  vslfDLog("vslfDoPop() - trigger c="+uC);
  if (uC == -1) {uC = vslgDefaultLocationNumber;}
  vslfDLog(" rewritten trigger to c="+uC);
  if (vslgHitSample)
  {
    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;
    }
    else
    {
      vslgPu=window.open(vslgPopupURL+"?c="+uC+"&l="+lang+"&w="+vslgSWidth+"&h="+vslgSHeight+"&wW="+vslgWWidth+"&wH="+vslgWHeight+"&k="+vslgCook+"&os="+vslgOS,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 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()
    {
      // WEBSITE-specific change text in "buttonNext"
      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)
        {

          // FOR SPECIFIC SITES ONLY checks to see if the element has a target attribute and rewrite it to self
          // if (docPs[i].target)
          // {
          //   docPs[i].target = "_self";
          // }

          // check to see if the element has already got an onclick event handler
          if (!docPs[i].onclick)
          {
            docPs[i].onclick = function()
                               {
                                 vslfDoPop(vslgDefaultLocationNumber);
                               };
          }
          else
          {
            var curr_on = docPs[i].onclick;
            docPs[i].onclick = function()
                               {
                                 vslfDoPop(vslgDefaultLocationNumber);
                                 curr_on();
                               };
          }

          // check to see if the element has already got a title attribute
          if (docPs[i].title != "")
          {
            docPs[i].title = docPs[i].title + ". This link also opens a new window";
          }
          else
          {
            docPs[i].title = "This link also opens a new window";
          }
        }
      } //end anchor links iteration


      // iterate through all input links on the page to capture search
      docPs = document.getElementsByTagName("input");
      for(i=0;i<docPs.length;i++)
      {
        if (docPs[i].alt == "Search")
        {

          // FOR SPECIFIC SITES ONLY checks to see if the element has a target attribute and rewrite it to self
          // if (docPs[i].target)
          // {
          //   docPs[i].target = "_self";
          // }

          // check to see if the element has already got an onclick event handler
          if (!docPs[i].onclick)
          {
            docPs[i].onclick = function()
                               {
                                 vslfDoPop(vslgDefaultLocationNumber);
                               };
          }
          else
          {
            curr_on = docPs[i].onclick;
            docPs[i].onclick = function()
                               {
                                 vslfDoPop(vslgDefaultLocationNumber);
                                 curr_on();
                               };
          }

          // check to see if the element has already got a title attribute
          if (docPs[i].title != "")
          {
            docPs[i].title = docPs[i].title + ". This link also opens a new window";
          }
          else
          {
            docPs[i].title = "This link also opens a new window";
          }
        }
      } //end area 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;
      vslfDLog("and now vslgInterval = "+vslgInterval+", vslgHitSample = "+vslgHitSample);
    }
    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);
                    vslgIntervalID = setInterval("vslfCheckIntervalFile()",vslgIntervalRetry);
                  }
}
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);
                    vslgIntervalID = setInterval("vslfCheckIntervalFile()",vslgIntervalRetry);
                  };
}
