/*****************************************************************************************************************
Div overlay v1
Code developed by TNS Global Interactive dept
Developer: Dominic Claxton
TNS October 2007


Div overlay JavaScript for dynamic overlaying of informational adds or prompts

Current setup allows for: 
delayed popup, 
minimum pages visited before popup, 
leaving site popup.

Currenlty tested to work in: 
IE 5,6,7
FireFox 2
Netscape 7,8
Safari 3 on the PC (apart from the close browser popup)
******************************************************************************************************************/


//PARAMETERS - TO EDIT

//x and y position where div will appear on the page
D_Div_xpos = 30;
D_Div_ypos = 100;

//Life of the cookie in days
D_cookieLife = 14;

//Sets the number range for the randomnum which only shows an overlay for x number of people
// It is worth noting that this random number generator is only mimicing a random number
//So the numbers below equal a 1 in 2 chance of the overlay being shown to the respondent
D_NumberRange = 1;
D_triggerNum = 1;


/*
These two parameter below work control the timed delay div popup and the x number of pages visited popup.
If one is turned off then the other should be turned on - and viceversa
*/
//This turns on/off the time delay on=yes  off=no
D_turnOnTimeDelay = "yes";
//Turns on/off the page visited function. Will show the overlay when x number of pages have been visited on=yes off=no
D_turnOnPageVisit = "no";


//The delay time in seconds when the overlay will show
D_delayTime = 4;

//The number of pages you must visit in order to trigger the overlay
D_pages2visit = 1;

//Enter the domains where this popupcode will be served, preceded by a |
D_domainCheckers = "|visitlanecounty.org";


//The cookie name
D_check_name = "181579cookie";

//This turns on/off the onclose/on exit script on=yes off=no
//It is currently not recommended to use this function because of the different results it returns in different browsers. 
//However, please seek advise from Global interactive if interested
D_turnOnCloseBrowser = "no";

/*=========================================================================================================================================================================================================================================================================================================================================*/
var cssNode = document.createElement('link'); 
cssNode.setAttribute('rel', 'stylesheet'); 
cssNode.setAttribute('type', 'text/css'); 
cssNode.setAttribute('href', 'http://svsiso.mysurvey.com/181579/181579control.css'); 
document.getElementsByTagName('head')[0].appendChild(cssNode); 

 /*                                                                 DIV overlay text - TO EDIT
=========================================================================================================================================================================================*/

D_divText = "<center><font color=black><B>Thank you for visiting!<BR>Please help us enhance our site</B></font></center> <P>&nbsp;<P><BR><font color=#5d5d5d size=1>This survey may appear in a new browser window, <BR>please allow pop-ups from this site. </font><BR><font color=5d5d5d  size = 1>We have commissioned </font><a href=\"/\" onclick=\"window.open('http://www.tns-us.com/privacy/','','top=5,left=5,height=600,width=700,scrollbars,toolbar=yes');return false;\"><font color= 5d5d5d size=1>TNS Custom Research,</font></a> <font color=5d5d5d size=1><BR>to conduct the survey on our behalf.</font>"

//For use in on site exit - CURRENTLY NOT USED
/*
D_popupText = " \n\n\n\n\n\n\n\n\n WE VALUE YOUR OPINION! \n\nWe hope you have enjoyed your stay and found what you were looking for.\nTo help us improve our service we would like to ask you a couple of questions. \n\nClick 'Ok' to close and leave or 'Cancel' to take the survey.\n\nThankyou.\n\n\n\n\n\n\n\n\n\n";
*/

//Button text
D_AcceptText="<B> </B>";
D_DeclineText="<u>Close</u> ";

//URL link to your survey
D_AcceptLink="http://online.tns-global.com/wix/p422472527.aspx";
                                               
//survey image
D_surveyWindow="http://svsiso.mysurvey.com/181579/181579inv.jpg";

/*=====================================================================================================================================================================
                                                             DO NOT EDIT THE CODE BELOW THIS 
=======================================================================================================================================================================*/
D_urlon = window.location.href;


function D_submitButton(){
window.onbeforeunload = D_voidedFunction;       
}


function D_setWindowrefresh(){  
        //Need to do domain checking to see if the link you have clicked on is with the same domain or not
    var D_domainSplit = D_domainCheckers.split("|");
    var D_getDomain = this.href.split("/");
        var D_JavaScriptMatch = this.href.match("java");
        var D_HashMatch = this.href.match("#");
    var D_getDomainOn = D_getDomain[2]; 
        var D_showpopup = true;
        //
        if(D_getDomain.length > 1){
      for(var n = 0; n < D_domainSplit.length; n++){
                if( D_getDomainOn == D_domainSplit[n] ){
                        D_getDomain = "";//make the getDomain equal nothing
                        var D_showpopup = false;
                        break;
                }
          }
        }
        
        if(D_JavaScriptMatch != null){
                var D_showpopup = false;
        }
        if(D_HashMatch != null){
                var D_showpopup = false;
        }
        
        if(D_showpopup == true && D_turnOnCloseBrowser == "yes"){
                //If you want the popup to appear then do this
                D_turnOnCloseBrowser = "no";
                //If the link is going outside and isn't part of the list of domains you specify then it will call the Div overlay to appear
                var D_cookie_value = ">"+D_domainCheckers+">no>"+D_pages2visit+">no>"+D_delayTime+">"+D_urlon+">"+D_Addone+">no"; 
                D_Set_Cookie( '181579cookie', D_cookie_value, D_cookieLife, '/', '', '' );
        }else{
                window.onbeforeunload = D_voidedFunction;
        }
        
}

function D_voidedFunction(){
//over-rides the original window.onbeforeunload so that it doesn't prompt on link click!
}


function D_confirmExit(){
        D_MoveDiv();
        return D_popupText; 
}
  
function D_CreateDiv(){
document.write("<div id=\"GIS_popupInvite\"></div><div  id=\"GIS_InviteImageClass\"><img class=\"GIS_img\"  src=\""+D_surveyWindow+"\"></div><div id=\"GIS_InviteText\">"+D_divText+"<br/></div><div id=\"GIS_InviteLink\"><a href=\""+D_AcceptLink+"\" target=\"_blank\" onClick=\"D_hideDiv();\">"+D_AcceptText+"</a><br/><br/><a href=\"javascript:D_hideDiv();\">"+D_DeclineText+"</a></div><div id=\"GIS_InviteButton1\"><a href=\""+D_AcceptLink+"\" target=\"_blank\" onClick=\"D_hideDiv();\"><img class=\"GIS_img\" src=\"http://svsiso.mysurvey.com/181579/181579butt.png\"></a></div><div id=\"GIS_InviteButton2\"><a href=\"javascript:D_hideDiv();\"><img class=\"GIS_img\" src=\"http://svsiso.mysurvey.com/181579/181579closebutt.png\"></a></div><div id=\"GIS_closeButton\"><a href=\"javascript:D_hideDiv();\"><img class=\"GIS_img\" src=\"http://svsiso.mysurvey.com/181579/181579closebutt.png\"></a></div>");
}

function D_MoveDiv(){
D_element = document.getElementById("GIS_InviteImageClass");

D_element.style.top = (D_Div_ypos+"px");
D_element.style.left = (D_Div_xpos+"px");
//
var D_element2 = document.getElementById("GIS_InviteText");
var D_newypos = D_Div_ypos+140;
var D_newxpos = D_Div_xpos+33;
D_element2.style.top = (D_newypos+"px");
D_element2.style.left = (D_newxpos+"px");
//
var D_element2 = document.getElementById("GIS_InviteLink");
var D_newypos2 = D_newypos+100;
var D_newxpos2 = D_newxpos+20;
D_element2.style.top = (D_newypos2+"px");
D_element2.style.left = (D_newxpos2+"px");
//
var D_element3 = document.getElementById("GIS_InviteButton1");
var D_newypos3 = D_newypos+55;
var D_newxpos3 = D_newxpos+125;
D_element3.style.top = (D_newypos3+"px");
D_element3.style.left = (D_newxpos3+"px");
//
var D_element4 = document.getElementById("GIS_InviteButton2");
var D_newypos4 = D_newypos+130;
var D_newxpos4 = D_newxpos+415;
D_element4.style.top = (D_newypos4+"px");
D_element4.style.left = (D_newxpos4+"px");
//
var D_element6 = document.getElementById("GIS_closeButton");
var D_newypos6 = D_newypos-160;
var D_newxpos6 = D_newxpos+410;
D_element6.style.top = (D_newypos6+"px");
D_element6.style.left = (D_newxpos6+"px");
//
var D_element2 = document.getElementById("GIS_popupInvite");
D_element2.style.top = "0px";
D_element2.style.left = "0px";

}

function D_hideDiv(){
        var D_element = document.getElementById("GIS_InviteImageClass");
    D_element.style.top = "-2000px";
        var D_element1 = document.getElementById("GIS_popupInvite");
    D_element1.style.top = "-3000px";
        var D_element2 = document.getElementById("GIS_InviteText");
    D_element2.style.top = "-2000px";
        var D_element3 = document.getElementById("GIS_InviteLink");
    D_element3.style.top = "-2000px";
        var D_element4 = document.getElementById("GIS_InviteButton1");
    D_element4.style.top = "-2000px";
        var D_element5 = document.getElementById("GIS_InviteButton2");
    D_element5.style.top = "-2000px";
        var D_element6 = document.getElementById("GIS_closeButton");
    D_element6.style.top = "-2000px";
}

//====================================================================================================================================================

function D_showTimeOutDiv(){
        D_MoveDiv();
}

function D_Set_Cookie( D_name, D_value, D_expires, D_path, D_domain, D_secure ) {
// set time, it's in milliseconds
var D_today = new Date();
D_today.setTime( D_today.getTime() );
//if the expires variable is set, make the correct expires time, the current script below will set it for x number of days, to make it for hours, 
//delete * 24, for minutes, delete * 60 * 24
 if ( D_expires ){
         D_expires = D_expires * 1000 * 60 * 60 * 24; 
         }
  var D_expires_date = new Date( D_today.getTime() + (D_expires) );
  document.cookie = D_name + "=" +escape( D_value ) +( ( D_expires ) ? ";expires=" + D_expires_date.toGMTString() : "" ) + ( ( D_path ) ? ";path=" + D_path : "" ) + ( ( D_domain ) ? ";domain=" + D_domain : "" ) +( ( D_secure ) ? ";secure" : "" );

}


function D_Get_Cookie() {
        //D_check_name, D_domainCheckers, D_pages2visit, D_turnOnPageVisit, D_turnOnTimeDelay, D_delayTime, D_turnOnCloseBrowser
        // first we'll split this cookie up into name/value pairs
        // note: document.cookie only returns name=value, not the other components
        var D_a_all_cookies = document.cookie.split( ';' );
        var D_a_temp_cookie = '';
        var D_cookie_name = '';
        var D_cookie_value = '';
        var D_b_cookie_found = "none"; // set boolean t/f default f
        
        for ( i = 0; i < D_a_all_cookies.length; i++ ){
                // now we'll split apart each name=value pair
                D_a_temp_cookie = D_a_all_cookies[i].split( '=' );
                // and trim left/right whitespace while we're at it
                D_cookie_name = D_a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
                //==========================================================================================================================================
                // if the extracted name matches passed check_name
                
                if ( D_cookie_name == D_check_name ){
                        var D_b_cookie_found = "present";
                        var D_cookie_value = unescape(D_a_temp_cookie[1]);
                        break;
                }
            
        }
        //===========================================================================================================================================
        //Here is where the cookie is first set. So all the variables must be set here.
        if ( D_b_cookie_found == "none" ){
                var D_cookie_value = ">"+D_domainCheckers+">"+D_turnOnPageVisit+">"+D_pages2visit+">"+D_turnOnTimeDelay+">"+D_delayTime+">"+D_urlon+">1>"+D_turnOnCloseBrowser;
                D_Set_Cookie( '181579cookie', D_cookie_value, D_cookieLife, '/', '', '' );
        }
        //===========================================================================================================================================
        D_CreateDiv();
        //Extract the variables
                    var D_temp = D_cookie_value.split(">");
                        D_domainCheckers = D_temp[1];
                        D_turnOnPageVisit = D_temp[2];
                        D_pages2visit = D_temp[3];
                        D_turnOnTimeDelay = D_temp[4];
                        D_delayTime = D_temp[5];
                        D_cookieUrlOn = D_temp[6];
                        D_IncrementNum = D_temp[7];
                        D_turnOnCloseBrowser = D_temp[8];
                        //We do another split on the domaincheckers variable
                        D_domainSplit = D_domainCheckers.split("|");
                        
                        //if the url is different then you have gone to a different page so increment the cookie by one
                        D_Addone = parseInt(D_IncrementNum)+1;
                        if(D_urlon != D_cookieUrlOn && D_turnOnPageVisit == "yes"){
                         var D_cookie_value = ">"+D_domainCheckers+">"+D_turnOnPageVisit+">"+D_pages2visit+">"+D_turnOnTimeDelay+">"+D_delayTime+">"+D_urlon+">"+D_Addone+">"+D_turnOnCloseBrowser;
                          D_Set_Cookie( '181579cookie', D_cookie_value, D_cookieLife, '/', '', '' );
                         //If the person has visited three different pages then do this
                
                            if(D_Addone==D_pages2visit){
                              var D_cookie_value = ">"+D_domainCheckers+">no>"+D_pages2visit+">no>"+D_delayTime+">"+D_urlon+">"+D_Addone+">no"; 
                                  D_Set_Cookie( '181579cookie', D_cookie_value, D_cookieLife, '/', '', '' );
                                 D_MoveDiv();
                            }
                        
                        }
                   var D_ran_unrounded=Math.random()*D_NumberRange;
           var D_ran_number=Math.ceil(D_ran_unrounded); 
          
         if(D_ran_number == D_triggerNum){
                        //if D_turnOnTimeDelay =  "yes"
            D_refreshpage();
          }
}       

var D_incrm = 0;
function D_refreshpage(){
  if(D_turnOnTimeDelay == "yes"){ 
  //set the randomNum function which only lets x number of people in
    D_incrm =D_incrm+1;
    if(D_incrm == D_delayTime){
       D_MoveDiv();
       var D_cookie_value = ">"+D_domainCheckers+">no>"+D_pages2visit+">no>"+D_delayTime+">"+D_urlon+">"+D_Addone+">no"; 
       D_Set_Cookie( '181579cookie', D_cookie_value, D_cookieLife, '/', '', '' );
    }else{
    window.setTimeout('D_refreshpage()', 1000); 
    }
 }
}
//====================================================================================================================================
//Dynamic link convert (for use in the on page exit) - currently turned off
//====================================================================================================================================
/*
var MySiteDomain='yourdomain.com';

function XBrowserAddHandlerPops(target,eventName,handlerName) { 
  if ( target.addEventListener ) {// if Mozilla
    target.addEventListener(eventName, function(e){target[handlerName](e);}, false);
  } else if ( target.attachEvent ) {//if IE
    target.attachEvent("on" + eventName, function(e){target[handlerName](e);});
  } else {
    var originalHandler = target["on" + eventName];
    if ( originalHandler ) {
      target["on" + eventName] = function(e){originalHandler(e);target[handlerName](e);};
    } else {
      target["on" + eventName] = target[handlerName];
    }
  }
}


function LinkConvert(){
        //Adds onclick to 'a' tag
        var anchors = document.getElementsByTagName('a');
        for(var y=0; y<anchors.length; y++){
                        anchors[y].clickhandler=setWindowrefresh;
                        XBrowserAddHandlerPops(anchors[y],"click","clickhandler");
        }
    //Adds onclick code to input tag
    var anchors2 = document.getElementsByTagName('input');
        for(var yy=0; yy<anchors2.length; yy++){
                //If its a button do this
                if(anchors2[yy].type == "button"){
                        var getCurrentOnClick = anchors2[yy].attributes['onclick'].value;
                        getCurrentOnClick.toLowerCase();
                        var addOverRide = false;
                        var domainSplit = domainCheckers.split("|");
                        for(var r = 0;r < domainSplit.length;r++){
                                var DomainString = domainSplit[r];
                                var domainMatch = getCurrentOnClick.match(DomainString);
                                var httpMatch = getCurrentOnClick.match("http");
                                //if there is no domain and no http match then the link is internal
                                if(domainMatch == null && httpMatch == null){
                anchors2[yy].addit = getCurrentOnClick;
                            anchors2[yy].onclick = function(){  submitButton(); eval(this.addit);};
                                //alert(" first = "+anchors2[yy].addit+" getCurrentOnClick= "+getCurrentOnClick+"  DomainString= "+DomainString);
                                }else{
                                 //If there is a domain match and an http match then the link is internal       
                                 if( domainMatch > -1 && httpMatch > -1){
                 anchors2[yy].addit = getCurrentOnClick;
                             anchors2[yy].onclick = function(){  submitButton(); eval(this.addit);};
                                 //alert(" second = "+anchors2[yy].addit+" getCurrentOnClick= "+getCurrentOnClick+"  DomainString= "+DomainString);
                             }
                                 //otherwise don't change anything!
                                }

                           
                                
                        }
                   
                                
                }

    }
        
                var form = document.forms[yy];
                var tags = document.getElementsByTagName('form');
                var loop = tags.length;
                for (var i = 0; i < loop; i++) {
                        var tags2 = tags[i].getElementsByTagName('input');
                var loop2 = tags2.length;
                        for (var ii = 0; ii < loop2; ii++) {
                                if(tags2[ii].type == "submit"){
                                tags2[ii].clickhandler=submitButton;
                    XBrowserAddHandlerPops(tags2[ii],"click","clickhandler");
                                }
                        }
        }
                
}

//calls the dynamic link convert function

if(D_turnOnCloseBrowser == "yes"){
        window.onbeforeunload = confirmExit;
}
*/
//=============================================================================================
//Start things off!
D_Get_Cookie();
//It only does this function if you have switched on the on browser exti code. - CURRENTLY TURNED OFF
/*
XBrowserAddHandlerPops(window,"load","LinkConvert");
*/










