function agilentSurvey(rURL, sLocale, iSessionId)
{
var lscafreq =0000000;
   
   if (selectuser())
    {
   
  if (findCookie())
   {
  if(notdirectedtoACSsurvey())
  {
   survey(rURL, sLocale, iSessionId);
   }
       }
     }
//1.function to check if user allows client side js
//2.selecting the user for the survey
function selectuser()
{
 
var Denominator = 100000;
var rnd = Math.floor(Math.random() * Denominator) + 1;
if (rnd < lscafreq) 
 {
 return true;
    }
}
	
function setCookie (name, value, expires) 
{
	if (expires) 
	{
		document.cookie = name + "=" + escape (value) + "; expires=" + expires.toGMTString() +  "; path=";  
	} else { 
		document.cookie = name + "=" + escape (value) + "; path=";  
	}
}


function getCookie (name) 
{

	var dcookie = document.cookie;
	var cname = name + "=";
	var clen = dcookie.length;        
	var cbegin = 0;                   
	while (cbegin < clen) 
	{           
		var vbegin = cbegin + cname.length;                    
		if (dcookie.substring(cbegin, vbegin) == cname) 
		{      
			 var vend = dcookie.indexOf (";", vbegin);           
			 if (vend == -1) vend = clen;                        
				return unescape(dcookie.substring(vbegin, vend));   
		}                                                      
		cbegin = dcookie.indexOf(" ", cbegin) + 1;             
		if (cbegin == 0) break;                                
	 }                                                         
	 return null;                                              
} 

function findCookie()
{
      
	 var expdate = new Date ();     
				
	 expdate.setTime (expdate.getTime() + (1000 * 60 * 1 * 1 * 1)); 

	 setCookie ("cookie_test", "true", expdate); 
	  
	 var persistentCookieEnabled=(getCookie("cookie_test") == "true")? true : false; 

	 if (persistentCookieEnabled) 
	 { 
		return true;
	 }else{
		return false;
	 }
}
//4.The user has not been directed to the invitation page previously - i.e. the user does not have a ACSSurvey cookie. 
function notdirectedtoACSsurvey()
{
        var cookieName = "ACSsurvey";
        var allcookies = document.cookie;
  	if (allcookies.indexOf(cookieName) < 0) {
		return true
	}
	else {
		return false
	}
}
//if all the criterias match
//1.function to set the cookie
function setACSCookie(){
// variable expiration date cookie - only modify daysToExpiration
 /*var daysToExpiration = 90; // Set days till expiration
 var exp = new Date(); // Creates date variable exp
 var currentTime = exp.getTime(); //returns current time in msecs
 var expireTime = currentTime + (daysToExpiration*24*60*60*1000); // adds variable expiration time to current time
 exp.setTime(expireTime); 
  // creates expiration date exp for cookie
// Set the domain to the same as the page the script is on
 document.cookie = "ACSsurvey=1;expires="+exp+";path=/;domain=.agilent.com";*/
var cookieDate = new Date();
cookieDate.setTime(cookieDate.getTime() + 1000 * 60 * 60 * 24 * 90);
var name = "ACSsurvey";
var value = "1";
document.cookie = name+"="+escape(value)+";expires=" + cookieDate.toGMTString()+";path=/; domain=.agilent.com";
 
}
//2.Redirect the user to the invitation url
 
function decode(str) {
     var result = "";
 
     for (var i = 0; i < str.length; i++) {
          if (str.charAt(i) == "+") result += " ";
          else result += str.charAt(i);
 
          return unescape(result);
     }
}
 
function survey(rURL,sLocale,iSessionId)
	{ 
		//alert("survey");
		setACSCookie(); // Sets cookie indicating the person has been selected
		 // Gets full URL for currrent web page making survey request
		//var optinURL = "http://acomd1.cos.agilent.com:6666/cgi-bin/paramlinktest.pl"; // Sets location of Opt-In request
		//var optinURL = "http://survey7.maritz.com/x097/agilent.php";
		var optinURL = "http://survey7.maritz.com/x097/agilent.php";
		var additionalInformation = "BU=lsca&LOCALE=" + sLocale + "&Session=" + iSessionId + "&REF_URL="+rURL;
 
		if(optinURL.indexOf('?') > -1)
			optinURL = optinURL+"&"+additionalInformation;
		else
			optinURL = optinURL+"?"+additionalInformation;
		//alert(optinURL);
		//2.Redirect the user to the invitation url
		//window.open(optinURL); 
		//alert(optinURL);
		location.href=optinURL;
	}
}



