// Accessible Overlay
// (C) 2004-2009 Virtual Surveys Ltd
// IMPORTANT - change these variables' values for each survey

var noPagesVisited;

var vslgSurveyName = 'Learndirect_Survey_2615';  //E.g. name+survey, used in cookie
var vslgSurveyTitle = 'learndirect Survey'; // Use in title of popover


var vslgDefaultLocationNumber = 1;    //E.g. trigger location number

var businessLocations = ["/businessadvice/","/businessinfo/","/campaigns/"];

for (var i=0; i<=businessLocations.length; i++){
  if (document.location.toString().indexOf(businessLocations[i]) > -1) vslgDefaultLocationNumber = 7;
}

var vslgLang = 9; //English

var vslgPopupHeight = 540;
var vslgPopupWidth = 650;

var surveyOuterContainer, surveyInnerDiv, surveyLink, vslgSurveyURL;

var overlay_position = "centered"; // "specified" or "centered"
var persist_position = "absolute"; // use "fixed" or "absolute"

var vslgIntervalURL = 'http://author.euro.confirmit.com/isa/HMCYGPGAHXJXBHABPDEBCDDRJDMAGPBF/2615_Learndirect/job2615_Dec09interval.js';

var vslgCookiePersistence = 90*24*60*60*1000; //set to 30*24*60*60*1000 if 30 day cookie required. 10 milliseconds
var vslgBodyFontSize = 1 / 1; // as 1/(font-size of containing document, 0.7 is font-size of 70%)

var vslgBorderCol = '#369'; // colour name (blue) or hex value (#00f)
var vslgIntervalRetry = 2000; // 2000 milliseconds - time allowed for loading external interval script
var vslgIntervalTimeout = 5000; // 5 seconds
var time_before_appearing = 2000; //Time before overlay appears

//IMPORTANT don't change anything below this line------------------------------------------------

var bd, vslgObjHeight, vslgObjWidth, vslgTopPos, vslgLeftPos, background, backgroundDiv;

var sizeArray = getPageSize();

vslgObjHeight = 200;
vslgObjWidth = 400;

vslgTopPos = 50; //in px - absolute only.
vslgLeftPos = 50; //in px - absolute only

if (!window.XMLHttpRequest) persist_position = "absolute"; //IE6 and lower override

var vslgInterval = 0;     //default 0, will be changed by external file
var vslgDebug = false;
var vslgIntervalID;
var vslgIntervalAttempts = 0; // DO NOT CHANGE

if (window.screen) {
  vslgSWidth = window.screen.width; 
  vslgSHeight = window.screen.height;
}

vslgWWidth = sizeArray[0];
vslgWHeight = sizeArray[1];

var vslgOS = 3;
var vslgCook = 3;

getOS();
cookiesEnabled();

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" : "");

}

function vslfCheckIntervalFile() {

  try {

    setSampleIntervalRemote();

    if (vslgInterval != 0) {

      vslgHitSample = ((vslgrandomnum + 1) * vslgInterval /1000 <= 1) && vslgCook != "2" && vslfGetCookie(vslgSurveyName) != 1 && vslfGetCookie("Learndirect_popSurvey") >= 3 ;

    }

    clearInterval(vslgIntervalID);

    if (vslgHitSample) {

      setTimeout("vslfDoPopOver();",time_before_appearing);

    }

  } catch (e) {

    vslgIntervalAttempts++;

    if ((vslgIntervalAttempts * vslgIntervalRetry) >  vslgIntervalTimeout) {

      clearInterval(vslgIntervalID);

    }
  }
}

function setSampleInterval(remoteInterval) {

  vslgInterval = remoteInterval;

}

function vslfLoadwork() {

  surveyOuterContainer = document.getElementById("surveyouter");
  surveyInnerDiv = document.getElementById("surveywrapper");
  surveyLink = document.getElementById("surveylink");
  vslgSurveyURL = surveyLink.href;
  surveyOuterContainer.style.display = "none";
  surveyOuterContainer.style.visibility = "hidden";

  clickedLink();

  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 getPageSize(){
  
  var xScroll, yScroll;
  
  if (window.innerHeight && window.scrollMaxY) {  
    xScroll = document.body.scrollWidth;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }
  
  var windowWidth, windowHeight;
  if (self.innerHeight) {  // all except Explorer
    windowWidth = self.innerWidth;
    windowWidth2 = document.body.clientWidth; //Measurement excluding scollbars for opaque div
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowWidth2 = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowWidth2 = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }  

  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight){
    pageHeight = windowHeight;
  } else { 
    pageHeight = yScroll;
  }

  // for small pages with total width less then width of the viewport
  if(xScroll < windowWidth){  
    pageWidth = windowWidth;
  } else {
    pageWidth = xScroll;
  }

  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight,windowWidth2) 
  return arrayPageSize;

}

function getScrollXY(rvalue) {

  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }

  if (rvalue == "height") return scrOfY;
  if (rvalue == "width")  return scrOfX;

}

function vslfDoPopOver() {

  if (vslgHitSample) {

    var expdate = new Date();
    expdate.setTime(expdate.getTime() + (vslgCookiePersistence));
    vslfSetCookie(vslgSurveyName,"1",expdate,"/");

    var surveyWidth, surveyWidth2, surveyTop, surveyHeight;

    surveyLeft = vslgBodyFontSize*vslgLeftPos/16 + 'em';

    if (overlay_position == "centered" && persist_position == "absolute"){

      surveyWidth = vslgObjWidth + 'px';
      surveyWidth2 = vslgObjWidth + 'px';
      surveyHeight = vslgObjHeight + 'px';

      surveyTop =  ((sizeArray[3]/2) - (vslgObjHeight/2) + getScrollXY("height")) + 'px';
      surveyLeft = ((sizeArray[2]/2) - (vslgObjWidth/2) + getScrollXY("width")) + 'px';

    }

    if (overlay_position == "centered" && persist_position == "fixed"){

      surveyWidth2 = "100%";
    
      surveyHeightPercentage = 30;
      surveyWidthPercentage = 50;

      surveyWidth = surveyWidthPercentage + '%';
      surveyHeight = surveyHeightPercentage + '%';

      surveyLeft = (100 - surveyWidthPercentage)/2 + '%';
      surveyTop = (100 - surveyHeightPercentage)/2 + '%';

    }

    if (overlay_position == "specified" && persist_position == "fixed"){

      surveyHeight = "50%";
      surveyWidth = "50%";
      surveyWidth2 = "100%";
      surveyTop = vslgTopPos + "px";
      surveyLeft = vslgLeftPos + "px";

    }
  
    var vslgObjURL2 = "?c="+vslgDefaultLocationNumber+"&w="+vslgSWidth+"&h="+vslgSHeight+"&wW="+vslgWWidth+"&wH="+vslgWHeight+"&os="+vslgOS +"&si="+vslgInterval+"&l="+vslgLang;

    surveyOuterContainer.style.display = "block";
    surveyOuterContainer.style.visibility = "visible";

    surveyOuterContainer.style.zIndex = "9999";
    surveyOuterContainer.style.borderColor = vslgBorderCol;
    surveyOuterContainer.style.borderWidth = "2px";
    surveyOuterContainer.style.borderStyle = "solid";
    surveyOuterContainer.style.width = surveyWidth;
    if (surveyHeight) surveyOuterContainer.style.height = surveyHeight;
    surveyOuterContainer.style.position = persist_position;
    surveyOuterContainer.style.top  = surveyTop;
    surveyOuterContainer.style.left = surveyLeft;
    surveyOuterContainer.style.fontFamily = "Verdana, sans-serif";
    surveyOuterContainer.style.textAlign = "left";
    surveyOuterContainer.style.fontWeight = "bold";
    surveyOuterContainer.style.backgroundColor = "white";

    vfontsize = vslgBodyFontSize*0.75;
    vfontsize = vfontsize + 'em';
    surveyOuterContainer.style.fontSize = vfontsize;

    outerImageDiv = document.createElement("div");

    var overlayImage = document.createElement("img");
    overlayImage.src = "http://www.learndirect.co.uk/media/global/logo.gif";
    overlayImage.alt = "Learndirect";
    overlayImage.style.paddingLeft = "0.5em";
    overlayImage.style.paddingTop = "0.5em";

    overlaySkipLink  = document.createElement("a");
    overlaySkipLink.id = "skiptarget";
    overlaySkipLink.name = "skiptarget";
    overlaySkipLink.className = "skip";
    overlaySkipLink.tabIndex = "-1";

    surveyInnerDiv.style.margin = "0.5em";
   
    overlayHeaderContainer = document.createElement("div");
    surveyOuterContainer.insertBefore(overlayHeaderContainer,surveyInnerDiv);
    surveyOuterContainer.insertBefore(outerImageDiv,surveyInnerDiv);
    outerImageDiv.appendChild(overlayImage);
    outerImageDiv.appendChild(overlaySkipLink);

    overlayHeaderContainer.style.color = "#fff";
    overlayHeaderContainer.style.backgroundColor =   vslgBorderCol;
    overlayHeaderContainer.style.borderBottomColor = vslgBorderCol;
    overlayHeaderContainer.style.borderBottomStyle = "solid";
    overlayHeaderContainer.style.borderBottomWidth = "1px";
    overlayHeaderContainer.style.textAlign = "right";

    overlayTitleSpan = document.createElement("span");
    overlayCloseLink = document.createElement("a");

    overlayHeaderContainer.appendChild(overlayTitleSpan);
    overlayHeaderContainer.appendChild(overlayCloseLink);

    overlayTitleSpan.style.padding = "0 0.5em";
    var surveyTitle = document.createTextNode(vslgSurveyTitle);
    overlayTitleSpan.appendChild(surveyTitle);
    overlayTitleSpan.style.styleFloat = "left";
    overlayTitleSpan.style.cssFloat = "left";

    overlayCloseLink.style.padding = "0 0.5em";
    overlayCloseLink.title = "Close Overlay";
    overlayCloseLink.style.color = "#fff";
    overlayCloseLink.href="#";
    var closeText = document.createTextNode("Close Survey Invite");
    overlayCloseLink.appendChild(closeText);

    overlayCloseLink.onclick = function(){
			  closeSurveyDiv();
			  return false;
			}

    surveyLink.href="#";
    surveyLink.removeAttribute("target");

    overlaySkipLink.focus();
    surveyLink.onclick = function(){
      vslgPu = window.open(vslgSurveyURL + vslgObjURL2,vslgSurveyName+Math.floor(Math.random()*10e20),"height="+vslgPopupHeight+",width="+vslgPopupWidth+",resizable=yes,scrollbars=yes,toolbar=no,menubar=no");
      closeSurveyDiv();
    }

  }
}

if (!window.onload) {

  window.onload = vslfLoadwork;

} else {

  curr_onload = window.onload;
  window.onload = function()
                  {
                    curr_onload();
                    vslfLoadwork();
                  }
}

function closeSurveyDiv(){

  if (surveyOuterContainer) {

    surveyOuterContainer.style.display='none';

  }

  return false;

}

function getOS(){

  // operating system
  if (navigator.userAgent.match(/Windows/)) vslgOS=1;
  if (navigator.userAgent.match(/Macintosh/)) vslgOS=2;
  
}

function cookiesEnabled(){
  var expdate = new Date();
  expdate.setTime(expdate.getTime() + (vslgCookiePersistence));

  vslfSetCookie("cookieenabledcheck","1", expdate, "/");
  if (vslfGetCookie("cookieenabledcheck") == 1){

    vslgCook = 1;

  } else {

    vslgCook = 2;

  }
}

function clickedLink(){

  var docPs = document.getElementsByTagName("a");
  
  for(var i=0; i<docPs.length; i++) {

    if (!docPs[i].onclick) {

      docPs[i].onclick = function() {
        fivePageClicksCookie();
      }
    } else {

      docPs[i].curr_on = docPs[i].onclick;
      docPs[i].onclick = function() {

        fivePageClicksCookie();
        this.curr_on();
      }
    }
  }
}

function fivePageClicksCookie(){
  var expdate = new Date();
  expdate.setTime(expdate.getTime() + (vslgCookiePersistence));
	
  if (vslgCook != "2"){
    surveyLater = vslfGetCookie("Learndirect_popSurvey");

    if (surveyLater){
      surveyLater++;
      vslfSetCookie("Learndirect_popSurvey", surveyLater, expdate, "/")
    } else {
      vslfSetCookie("Learndirect_popSurvey","1", expdate, "/")
    }

  }
}
