
function surveywindow(){
  var url = window.location.href;
  var surveyUrl = "";
  var visits = 2; //The initial value must be set to 2.
  //--arrays--
  
  //-- Urls that can trigger a redirect to the surveys. For every Trigger url there must be a Target Url 
  var triggerUrls = new Array();
  triggerUrls[0] = "/eng/lifeevents/job.shtml";
  triggerUrls[1] = "/eng/audiences/seniors/index.shtml";
  triggerUrls[2] = "/fra/vie/emploi.shtml";
  triggerUrls[3] = "/fra/auditoires/aines/index.shtml";
  triggerUrls[4] = "/eng/about/scpublications.shtml";
  triggerUrls[5] = "/fra/ausujet/scpublications.shtml";
  

  //-- Number of times a user must visit the page before triggering a redirect to the Target Url
  var triggerValues = new Array();
  triggerValues[0] ="4";
  triggerValues[1] ="2";
  triggerValues[2] ="2";
  triggerValues[3] ="2";
  triggerValues[4] ="2";
  triggerValues[5] ="2";

  //-- Urls to redirect to. Index values (ie. [0]) MUST match index values of Trigger Urls.
  var targetUrls = new Array();
  targetUrls[0]="/eng/lifeevents/invitation.shtml";
  targetUrls[1]="/eng/lifeevents/invitation_seniors.shtml";
  targetUrls[2]="/fra/vie/invitation.shtml";
  targetUrls[3]="/fra/vie/invitation_aines.shtml";
  targetUrls[4]="/eng/online/questionnaire/invitation_pub.shtml";
  targetUrls[5]="/fra/enligne/questionnaire/invitation_pub.shtml";
  
  //-- End of arrays--

  //--loop through the triggerUrls to see if they match current url  
  for (i=0;i<triggerUrls.length;i++){     
   if(url.indexOf(triggerUrls[i])>-1){   
//--Cookie Test
    //see if a cookie exists
    if(getCookie( triggerUrls[i])){
      visits = getCookie( triggerUrls[i]);       
      //--redirect if number of visits equals trigger value for this page (INDEX).
      if(visits==triggerValues[i]){
        //reset cookie to 1
        setCookie( triggerUrls[i], 1, 5);
        //alert("Redirecting to: "+targetUrls[i]);
        window.location = targetUrls[i];
      }
      //--add to visit value and reset cookie if visist is less than trigger value for this page.
      if(visits<triggerValues[i]){
        //add one to visit count - must turn var into int first.
        visits = parseInt(visits)+1;
        setCookie( triggerUrls[i], visits, 5);       
        //document.write("Resetting cookie to: "+visits);
      }         
    }else{
      //set a cookie to keep track of how many visits - cookie set to last 5 days.
      //the initial value must be set to 2.
      visits = parseInt(visits);
      setCookie( triggerUrls[i], visits, 5);
      //document.write("Creating New Cookie Set to: "+visits);
    }
//--End Cookie test  
   }
  }
}

//-- Cookie functions. Copied from: http://www.w3schools.com/JS/js_cookies.asp - March 19 2010.
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}
//-- End Cookie Functions --

//--Created by Michel Gervais.
function rand_no_surveywindow(){
  var url = window.location.href;
  //--arrays--
  
  //-- Urls that can trigger a redirect to the surveys. For every Trigger url there must be a Target Url 
  var triggerUrls = new Array();
  triggerUrls[0] = "/eng/online/mysca.shtml";
  triggerUrls[1] = "/fra/enligne/mondsc.shtml";
  triggerUrls[2] = "/eng/online/mysca_byp.shtml";
  triggerUrls[3] = "/fra/enligne/mondsc_adp.shtml";

  //-- triggerUrls[2] = "/eng/isp/cpp/postrtrben/main.shtml";
  //-- triggerUrls[3] = "/fra/psr/rpc/prestpostrtr/principale.shtml";
  
  //-- Upper scale of the random number generated  before triggering a redirect to the Target Url
  //-- NOTE NUMBER MUST BE 1 MORE THEN WHAT YOU REQUIRE.. IE  1 TO 10  SHOULE BE Math.floor(Math.random()*11
  var triggerValues = new Array();
  triggerValues[0] = Math.floor(Math.random()*183);
  triggerValues[1] = Math.floor(Math.random()*183);
  triggerValues[2] = Math.floor(Math.random()*32);
  triggerValues[3] = Math.floor(Math.random()*32);
  //-- triggerValues[2] = Math.floor(Math.random()*4);
  //-- triggerValues[3] = Math.floor(Math.random()*4);

  
  //-- Urls to redirect to. Index values (ie. [0]) MUST match index values of Trigger Urls.
  var targetUrls = new Array();
  targetUrls[0]="/eng/online/questionnaire/invitation2_mysca.shtml";
  targetUrls[1]="/fra/enligne/questionnaire/invitation2_mondsc.shtml";
  targetUrls[2]="/eng/online/questionnaire/invitation3_mysca.shtml";
  targetUrls[3]="/fra/enligne/questionnaire/invitation3_mondsc.shtml";
  //--   targetUrls[2]="/eng/online/questionnaire/invitation-c51.shtml";
  //-- targetUrls[3]="/fra/enligne/questionnaire/invitation-c51.shtml";
  
  
  var otherUrls = new Array();
  otherUrls[0]="https://srv136.services.gc.ca/sc/msca-mdsc/portal-portail/pub/msca/home_e.html"
  otherUrls[1]="https://srv136.services.gc.ca/sc/msca-mdsc/portal-portail/pub/msca/home_f.html"
  //-- End of arrays--

  //--loop through the triggerUrls to see if they match current url  
  for (i=0;i<triggerUrls.length;i++){
   if(url.indexOf(triggerUrls[i])>-1){	   
	
  //--Check to see if triggerValue equals 1 if it does Redirect.
  //--alert(triggerValues[i]);
	   
		if(triggerValues[i] == 1) 
		{
			if(i!=2 && i!=3)
			{	
				var quest = getCookie(triggerUrls[i])
				if (quest!=null && quest!=""){	}
				else{
				setCookie(triggerUrls[i], 1, 1);
				window.location = targetUrls[i];
				}
			}
			else{	
				window.location = targetUrls[i];
			}
		}
		//else{
		//	window.location = otherUrls[i]
		//}
   }
  }
}

