//Search Survey version of webeffective_wrapper.js file. Last code change 19 April 2007 

var businessCode = "";
var locale = "";
var surveyfreq = 0; //Set how often a user is randomly selected, out of 1000 (e.g. 10% = 100)
var tempCookieName = "searchSurveyCookie";

if(searchTermIndicator()) //only continue if user is on a search results page...
{
	if (getCookie(tempCookieName) != "true") //if temporary search survey cookies is not set, then continue
	{
		var expdate = new Date ();
		expdate.setTime (expdate.getTime() + (1000 * 60 * 60 * 1 * 1));  //set temp search survey cookie to expire in 1 hour. Only allow user to get survey on 1st search they perform.
		setCookie (tempCookieName, "true", expdate);
		var persistentCookieEnabled=(getCookie(tempCookieName) == "true")? true : false; //verify user allows setting of persistent cookies
		if (persistentCookieEnabled) 
		{ 
			if (selectuser())
			{
				if(findACSsurveycookie())
				{ //if ACS survey cookie found then no action
				}else 
				{
					searchsurvey(); //call to set ACSsurvey cookie and send user to the survey interstitial page
				}
			}
		}
    }
}// end of main program loop

//Check if search term indicator is present or not
function searchTermIndicator()
{
   	var currentPageUrl=location.href;//take the current page URL
	var result="";
	if (currentPageUrl.indexOf('k=') > -1)//if search term indicator found then return true
	{
	   return true;
	}   
}//end of searchTermIndicator()   

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;                                              
}//end of getCookie()

function setCookie (name, value, expires) 
{
	if (expires) 
	{
		document.cookie = name + "=" + escape (value) + "; expires=" + expires.toGMTString() +  "; path=/; domain=.agilent.com";  
	} else { 
		document.cookie = name + "=" + escape (value) + "; path=/";  
	}
}//end of setCookie()

function selectuser()
{
	var nth = getNTH();//Checking the BU(Business Units) and setting the frequencies
	var Denominator = 1000;
	var rnd = Math.floor(Math.random() * Denominator) + 1;//Math.floor(a):integer closest to and not greater than a
															//Math.random():pseudorandom number in the range 0 to 1
	if (rnd < nth) 
	{
		return true;
	}
}

function getNTH() 
{
	if (document.all)  
	{
		MetaTagList = document.all.tags("meta"); //getting all the meta tags
	}else if (document.documentElement) 
	{
		MetaTagList = document.getElementsByTagName("meta"); 
	}
	for (i = 0; i < MetaTagList.length; i++) 
	{
		if ( MetaTagList[i].name )
		{
			if ( MetaTagList[i].name == 'DC.Publisher')
			{
				businessCode = MetaTagList[i].content;	
			}
		}else if (MetaTagList[i].getAttributeNode) 
		{
			if(MetaTagList[i].getAttributeNode('http-equiv').value == 'Content-Language')
			{			
				MetaName = 'Content-Language';
				locale = MetaTagList[i].content;//from here we are getting country code
			}
		}
	}//end of for loop
	if(businessCode == "" && locale.indexOf("en") != -1){
		return surveyfreq; //allow survey only for English users
	} //end of general survey code
}//end of getNTH() function


//check whether ACS survey cookie is present or not
function findACSsurveycookie()
{
       	var cookieName = "ACSsurvey";
        var allcookies = document.cookie;
		if (allcookies.indexOf(cookieName) > -1)	//Cookie not found
		return true;
}//end of findACSsurveycookie()

function searchsurvey()
{
	var currentPage = location.href; //take URL of the current page
	var previousPage = document.referrer;
	var call = "";
	var previous = "";
	call = encodeURIComponent(currentPage);
	previous=encodeURIComponent(previousPage);
		  
	setACScookie(); // Sets ACSsurvey cookie indicating the person has been selected
	var optinURL = "http://cp.home.agilent.com/upload/cmc_upload/All/searchsurveypage.html"; // Sets location of Interstitial page fquest
	var indicator=getsearchTermIndicator(currentPage); //for getting search term indicator
	var additionalInformation = "q1="+previous+"&q2="+call+"&q3="+indicator;
	if(optinURL.indexOf('?') > -1)
			optinURL = optinURL+"&"+additionalInformation;
	else
			optinURL = optinURL+"?"+additionalInformation;
	//Redirect the user to the invitation url
	window.location = optinURL;//send the seach survey result to the target URL
}//end of searchsurvey()

//function to set the ASCsurvey cookie
function setACScookie(){
  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";
}//end of setACScookie()

//extract search term indicator
function getsearchTermIndicator(currentPage)
{
	var startindicator=0;
	var endindicator=0;
	var indicator="";
	var temp_indicator="";//extracting extra term from SEARCH Teram
	startindicator=currentPage.indexOf('k=')+2;
	//extracting extra term from SEARCH Term
	temp_indicator=currentPage.substring(startindicator,currentPage.length);
	if(temp_indicator.indexOf('&')>-1)
	{
		endindicator=temp_indicator.indexOf('&');
		temp_indicator=temp_indicator.substring(0,endindicator);
		if(temp_indicator.indexOf('+')>-1)
	    {
        	for (var i =0; i < temp_indicator.length; i++) 
			{
				if (temp_indicator.charAt(i) == "+") 
				{
			    	indicator += " ";
				}else indicator += temp_indicator.charAt(i);
		    }//end of for loop
            return indicator;		  
		}else
		{
			indicator=temp_indicator.substring(0,temp_indicator.length);
		    return indicator;	  
		}
	}else
	{
		if(temp_indicator.indexOf('+')>-1)
		{
			for (var i =0; i < temp_indicator.length; i++) 
			{
				if (temp_indicator.charAt(i) == "+") 
				{
					indicator += " ";
				}else indicator += temp_indicator.charAt(i);
			}//end of for loop
			return indicator;		  
		}else
		{
			indicator=temp_indicator.substring(0,temp_indicator.length);
			return indicator;	  
		}	  
	}
 }//end of getsearchTermIndicator()
