//***************************************************************************************
// *  Page Name   :  SEOCommonJSFunctions.js                               
// *  Description :  A common JS file used by different modules in the Careers application	                                                    
// *													  			          
//***************************************************************************************
// *  REVISION HISTORY:                                                    
// *                                                                        
// *  Date Created :					                                    
// *  Author       :														
// *                                                                        
// *  REVISION TRAIL:                                                      
// *                                                                        
// *  DATE		  BY	SIR NO			DESCRIPTION
// *  12/13/05    ARMC	SIR-IDAQAWLG	Open External Window bug on ATS and Careers HP    
// *  01/04/06    PBMD	SIR-IDAHOHUC	Drop Down issues on Home Page 5 Template
// *  08/25/06	  PBMD	SIR-IDAHOZQC	Careers ASG changes
// *  11/17/06	  EVC	SIR-IDAJA2XD	Hidden PageTAS Link
// *  12/03/07	  SGS	SIR-835			Search Control Update	
// *  07/22/08    RPSD  SIR-1290		Recruiting Campaign Tracking Development  
// *  08/29/08    RPSD  SIR-1282		Omniture Effort Release 2
// *  09/24/08    RPSD  SIR-1282		Omniture Effort Release 3
// *  11/06/08    RPSD	RMT 24			Omniture Form Start Enhancement Careers Part 1
// *  12/04/08    RPSD	RMT 34			Omniture Campaign Tags on E-mail a Friend Report
// *  05/08/09	  JMCB	RMT 239			Omniture - Tracking video/ audio nstore files
// *  05/12/09    GVG   RMT 210			Omniture Custom Hierarchy
// *  05/14/09    RPSD  RMT 204			Frameset detection fix for Careers and DigitalForum
// *  06/04/09	  PBDL	RMT 238			Omniture: Form page activity - Show Referrer Page
// *  06/04/09	  PBDL	RMT 237			Omniture: Opt-out and Opt-in cookie
//***************************************************************************************

	//Frameset detection fix
	if (top.location != self.location){
		top.location = self.location
	}

	//Get URL of current page without query strings
	var pageURL;
	pageURL = window.location.protocol + "//" + window.location.host + window.location.pathname;
	pageURL = escape(pageURL.toLowerCase());
	
		
	//External js file used by Careers\NewUserControl\Footer.ascx
	var name= "ACNRC"; 
	var allcookies = this.document.cookie; 
	var pos = allcookies.indexOf(name); 
	var start = allcookies.indexOf(this.name + '=');
	start += this.name.length +1;
	
	var end = allcookies.indexOf(';', start);

	if (end ==-1) end =allcookies.length;
	
	var cookieval = allcookies.substring(start, end);
	var domain = document.domain;
	var path = " / "; 
	var secure;
	var expires= new Date(); 
	var current= new Date(); 
	var x;
	var upper_limit = 9000000000000000;
	var y;
	var mh = current.getMonth()+1; 
	var de = current.getDate(); 
	var yr = current.getFullYear(); 
	var hs = current.getHours(); 
	var mn = current.getMinutes(); 
	var ss = current.getSeconds(); 
	var ap;  

	expires.setFullYear(expires.getFullYear()+9); 
	current.setFullYear (current.getFullYear()); 
             
    function RandomNumber(upper_limit) 
	{ 
		return Math.round(upper_limit * Math.random()); 
	}
		
	function cookie(id, output, expires, path) 
	{ 
		x=("ACNUSERID=") + id; 
		x = ("ACNRC") + "=" + ("ACNVISIT=") + output + "&" + x + "; expires=" + expires.toGMTString(); 
		x =  x + "; path=" + path;
		document.cookie = x;
	}
	
	function visittime(mh, de, yr, hs, mn, ss,ap) 
	{ 
		if(hs < 12){ 
			ap = "AM"
		}
		else {
			ap = "PM"
		}

		switch(hs)
		{
			case 00:
				hs="12";
			break; 
			case 01:
				hs="1";
			break; 	
			case 02:
				hs="2";
			break; 	
			case 03:
				hs="3";
			break; 	
			case 04:
				hs="4";
			break; 	
			case 05:
				hs="5";
			break; 	
			case 06:
				hs="6";
			break; 	
			case 07:
				hs="7";
			break; 	
			case 08:
				hs="8";
			break; 	
			case 09:
				hs="9";
			break; 	    	      
			case 10:
				hs="10";
			break; 	 
			case 11: 
				hs="11";
			break; 	
			case 12: 
				hs="12";
			break; 	
			case 13: 
				hs="1";
			break; 	
			case 14: 
				hs="2";
			break; 	
			case 15: 
				hs="3";
			break; 	
			case 16: 
				hs="4";
			break; 	
			case 17: 
				hs="5";
			break; 	
			case 18: 
				hs="6";
			break; 	
			case 19: 
				hs="7";
			break; 	
			case 20: 
				hs="8";
			break; 	
			case 21: 
				hs="9";
			break; 	
			case 22: 
				hs="10";
			break; 	
			case 23: 
				hs="11";
			break; 	
			default:
				hs=hs; 
			break; 
		}
	
		switch(mn)
		{ 
			case 0:
				mn="00";
			break; 	
			case 1:
				mn="01";
			break; 	
			case 2:
				mn="02";
			break; 	
			case 3:
				mn="03";
			break; 	
			case 4:
				mn="04";
			break; 	
			case 5:
				mn="05";
			break; 	
			case 6:
				mn="06";
			break; 	
			case 7:
				mn="07";
			break; 	
			case 8:
				mn="08";
			break; 	
			case 9:
				mn="09";
			break; 		
			default:
				mn=mn;
			break; 
		} 

		switch(ss)
		{ 
			case "0":
				ss="00";
			break; 	
			case 1:
				ss="01";
			break; 	
			case 2:
				ss="02";
			break; 	
			case 3:
				ss="03";
			break; 	
			case 4:
				ss="04";
			break; 	
			case 5:
				ss="05";
			break; 	
			case 6:
				ss="06";
			break; 	
			case 7:
				ss="07";
			break; 	
			case 8:
				ss="08";
			break; 	
			case 9:
				ss="09";
			break; 		
			default:
				ss=ss;
			break; 
		} 		
		
		output = mh + "/" + de + "/" + yr + "+" + hs + ":" + mn + ":" + ss + "+" + ap; 
	}
	

	if(pos != -1)	
	{
		var broken_cookie = cookieval.split("&");
		var cookie1 = broken_cookie[0];
		var cookie2 = broken_cookie[1];
		var cookie1_check = cookie1.split("=");
		var cookie2_check = cookie2.split("=");
		var id = cookie2_check[1];
		var a = cookie2_check[1].length;
		var output = cookie1_check[1];
		
		if ((cookie1_check[0] == "ACVIST") || (cookie2_check[0] == "ACUSERID")) 
		{
			x=cookie(id, output, expires, path);
		}
		
		if (a == 0)
		{	
			var f=visittime(mh, de, yr, hs, mn, ss,ap);
			var num=RandomNumber(upper_limit); 
			var rn=current.getTime();
			var id = num + rn; 
			x=cookie(id, output, expires, path);
		}
	}
	
	if(pos == -1)
	{
		var f=visittime(mh, de, yr, hs, mn, ss,ap);
		var num=RandomNumber(upper_limit); 
		var rn=current.getTime();
		var id = num + rn; 
		x=cookie(id, output, expires, path);
	}

	function footerBG()
	{
		var footerID;
		footerID = "FooterTable";
		var section = document.getElementById(footerID); 
		var cn = "bgMain";
		section.className =  cn;
	}
		
	//taken from Homepagepopup.aspx
	function printThis()
	{
		if (window.print) 
			{ window.print()} 
		else alert('To print this page press Ctrl-P on your keyboard.')
	}
		
		
	//taken from Searchboxnew.aspx
	function selectvalue()
	{ 
		if(document.all.sv.value =="") 
		{
			document.all.gs.site.value = "CAR_global";
			document.all.gs.search_main.value = "";
			document.all.gs.search_in.value = "main_locations";	
			document.all.gs.searchby.value = "Global";
		}
		else
		{
			var SearchValue = document.all.sv.value;			
			if(SearchValue.length > 16) {
				document.all.gs.site.value = "CAR_" + (SearchValue.substr(0,16)).toLowerCase();
			}
			else {
				document.all.gs.site.value = "CAR_" + document.all.sv.value.toLowerCase();
			}
			
			document.all.gs.search_main.value = "section"; //global = ""
			document.all.gs.search_in.value = "main";	//global = main_locations
			document.all.gs.searchby.value = document.all.gs.OptionText.value; //global = ""
		}
	}		
	
	function checkForEnter(keyCode) 
	{
		if(event.keyCode == 13) {
			selectvalue();
		}
	}
		
	//taken from UIFooter.xslt
	function GMS()
	{
		if ((navigator.appVersion.indexOf("Macintosh") != -1))
		{
	    	alert('Macintosh users can view the complete Global Meritocracy Statement at http://careeers.accenture.com/gms.html.');
		}
		else
		{
	    	alert('Accenture does not discriminate on the basis of race, religion, color, sex, age, non-disqualifying physical or mental disability, national origin or any other basis covered by local law. All matters related to employment are decided on the basis of qualifications, merit and business need.');
	 	}
	}

	//taken from DropDownMenu.aspx				
	function Go(ItemID)
	{
		var temp = ItemID;
		
		if (temp.match("..external..") == "..external..")
		{		
			window.open(temp.replace(/..external../,""));	
		}
		else				
		{
			self.location = temp;
		}		
	}
						
	
	function openPrintFriendly(GUID)
	{
		popWindow('/Careers/ASPX/DisplayPrintFriendly.aspx?GUID=' + GUID,'560','760','');
	}

	//Opens the Email to a Colleague Form in a new popup window		
	function EmailToFriend(CurrentChannel)
	{
		popWindow('/xd/tsPrintMail/email_friend_full-New2.asp?id=acc&country=' + CurrentChannel + '&pageURL=' + pageURL,'500','630','');
	}
					

	//All spaces, punctuation, accented characters, and any other non-ASCII characters 
	//are replaced with %xx encoding, where xx is equivalent to the hexadecimal number 
	//representing the character. For example, a space is returned as "%20." 
	function URLencode(sStr) 
	{
		return escape(sStr).
			replace(/\+/g, '%2B').
				replace(/\"/g,'%22').
					replace(/\'/g, '%27'). 
						replace(/\//g,'%2F');
		//%27 - Single Qoute
		//%22 - Double Qoute
		//%2B - + sign
		//%2F - / sign
	}
	
	function trim(str)
	{
		return str.replace(/^\s*|\s*$/g,"");
	}

		
	//Returns true if the parameter passed is not undefined and has a length greater than 0
	function IsValidParam(param)
	{
		if (param == undefined){				
			return false;				
		}
		
		if(trim(param).length > 0){
			return true;
		}				
	
		return false;		
	}
		
	//Calls Hitbox.asp passing hitURL and redirectURL as query strings.
	//The target parameter specifies whether the Hitbox.asp will open in current window 
	//or opens in a new window.
	function RedirectHitbox(hitURL, redirectURL, target)
	{						
		if(IsValidParam(hitURL) && IsValidParam(redirectURL))
		{
			var encodedHitURL = URLencode(hitURL);
			var encodedRedirectURL = URLencode(redirectURL);
			var strURL;
			
			strURL = '/Careers/Asp/Hitbox.asp?hitURL=' + encodedHitURL + '&redirectURL=' + encodedRedirectURL;

			if(typeof target != 'undefined')
			{
				if(target.toLowerCase() == 'external'){
					window.open(strURL);
				}			
				else {
					window.location=strURL;
				}
			}
			else {
				window.location=strURL;
			}					
		}
	}

	//Retrieves value of cookie if exists, otherwise returns null
	function GetCookie(cookieName)
	{
		//Use regular expression to find the cookie name and value
		var results = document.cookie.match ('(^|;) ?' + cookieName + '=([^;]*)(;|$)' );

		if (results)
			return unescape(results[2]);
		else
			return null;
	}

	//Create a cookie and set its attributes (client-side)
	function SetCookie(name, value, expireYear, expireMonth, expireDay, domain, secure)
	{
		var strCookie = name + "=" + escape(value) + "; path=/";
			
		//optional attributes	
		if (expireYear)
		{
			//Date format in paramater should be YYYY-MM-DD
			var expires = new Date(expireYear, expireMonth-1, expireDay);
			strCookie += "; expires=" + expires.toGMTString();
		}
			
		if (domain)
			strCookie += "; domain=" + escape(domain);
			
		if (secure)
			strCookie += "; secure";
		
		document.cookie = unescape(strCookie);
	}
		
	//Omniture onClick JS function to handle variable assignments for embedded
	//video and audio attachments
	function trackOmnitureCustomLinks(eventType,url)
	{
		if (typeof(s_account)!='undefined')
		{
			
			var s=s_gi(s_account);
			
			//Media file URL
			url = url.toLowerCase();

			//Variable for the Print Article
			var printURL = url;

			//gets the posting URL containing the link
			var postingURL = window.location.pathname;
			postingURL = escape(postingURL.toLowerCase());
			postingURL = postingURL.substring(postingURL.indexOf("/")+1);
			
			//gets the domain of the media url		
			if (url.indexOf("://") != -1)
			{
				var mediaDomain = url.substring(url.indexOf("://")+3)
				mediaDomain = mediaDomain.substring(0,mediaDomain.indexOf("/"));
			}
			
			//compares the media url doamin from the posting url domain
			if ((mediaDomain == domain) || (mediaDomain == undefined))
			{
				//gets the file name of the media url
				url = url.substring(url.lastIndexOf("/")+1);
			}
			
			switch(eventType)
			{
				case 'event6':
					//Override tracked variables
					s.linkTrackVars = 'eVar16,events';
					s.linkTrackEvents = 'event6';
					
					//Set custom event variable 16 to the link URL
					s.eVar16 = url + " | " + postingURL;
													
					//Set site event variable for embedded video link
					s.events = 'event6';
					
					//Send request to populate the Custom Link Report	
					s.tl(true,'o','Watch the Video');	
					
					//Reset variables to empty to avoid double hits
					s.linkTrackVars = "None";
					s.linkTrackEvents = "None";
					s.eVar3 = "";
					s.events = "";									
					break;    
					
				case 'event7':		
					//Override tracked variables
					s.linkTrackVars = 'eVar17,events';
					s.linkTrackEvents = 'event7';
					
					//Set custom event variable 17 to the link URL
					s.eVar17 = url + " | " + postingURL;
									
					//Set site event variable for embedded audio link
					s.events = 'event7';
					
					//Send request to populate the Custom Link Report	
					s.tl(true,'o','Listen to Audio');
					
					break;  
				
				case 'event28':
				
					//Override tracked variables
					s.linkTrackVars = 'eVar32,events';
					s.linkTrackEvents = 'event28';
					
					//Set custom event variable 32 to the referral page
					s.eVar32 = printURL + ' | print article';
					
					//Set site event variable for print article tracking
					s.events = 'event28';
					
					//Send request to populate the Custom Link Report	
					s.tl(true,'o','Print Article');
					
					break;
			}
		}
	}	
	
	//OnFocus function which determines if the Form Start Event would
	//be tracked in Omniture
	function trackOmnitureFormStart()
	{
		if (typeof(s_account)!='undefined'){
		//Get Referrer URL value from cookie data
		//Track form start if Form Start cookie does not yet exist or 
		//has a value of false
		if((GetCookie("trackFormStart")== null) || (GetCookie("trackFormStart") == "false"))
		{
			//Get Form URL and Referrer URL value from cookie
			var formURL = GetCookie("OmnitureURL");
			var RefURL
			
			if((GetCookie("ReferrerURL")== null) || (GetCookie("ReferrerURL") == ""))
			{
				//Set the default Referrer URL value for empty cookies.
				RefURL = "no referrer page";
			}	
			else
			{
				RefURL = GetCookie("ReferrerURL");
			}
				
			//Ensure Form Start event would not be triggered when cookies
			//are disabled or user goes back to form after submission
			if (formURL)
			{
				//Create or Update cookie with value of True
				SetCookie("trackFormStart", "true", "", "", "", "", "");
					
				var s=s_gi(s_account);
				
				//Override tracked variables
				s.linkTrackVars = 'eVar15,events';
				s.linkTrackEvents = 'event3';
				
				//Set the Form Start URL to custom event variable 15
				s.eVar15 = RefURL + " | " + formURL;
				
				//Set site event variable for Form Start tracking
				s.events = 'event3';
				
				//Send the http request to Omniture
				s.tl(true,'o',"Form Start View");					
			
				//Ensure that eVar15 variable will not be usable/reported
				//with Form Abandonment event
				s.eVar15 = undefined;
			}
		}		
		}
	}	
	
	// Function that will set the value of Custom Hierarchy Level 1, 2 and 3.
	function SetPageHierarchy(propVal)
	{
		if (typeof(s_account)!='undefined') 
			{
			if (typeof(s)!='undefined') 
				{
				var levelCollection
				var levelHierarchy
				var propItem
				var propValCollection
				var level1 = '';
				var level2 = '';
				var level3 = '';
				var pagecnt = 0;

				// Check for the value of s.prop15, 16 and 17.
				s.linkTrackVars='prop15,prop16,prop17,hier2';
				propValCollection = propVal.split("|")

				// Split the custom property value.
				for (propItem in propValCollection)
				{
					levelCollection = propValCollection[propItem].split(";");
					
					// Set the value of s.prop15
					if (levelCollection[0])
					{
						if (level1 != trim(levelCollection[0]))
						{
							s.prop15 = trim(levelCollection[0]).toLowerCase();
						}
						
						level1 = trim(levelCollection[0]).toLowerCase();
						
						if (level1.length > 0)
						{
							levelHierarchy = trim(levelCollection[0]).toLowerCase();
						}
						else
						{
							levelHierarchy = "No Level 1";
						}	
					}
					else
						{
							levelHierarchy = "No Level 1";
						}			
					
					// Set the value of s.prop16
					if (levelCollection[1])
					{
						if (level2 != trim(levelCollection[1]))
						{
							s.prop16 = trim(levelCollection[1]).toLowerCase();
						}
						level2 = trim(levelCollection[1]).toLowerCase();
						
						if (level2.length > 0)
						{
							levelHierarchy = levelHierarchy + "/" + trim(levelCollection[1]).toLowerCase();
						}
						else
						{
							levelHierarchy = levelHierarchy + "/" + "No Level 2";
						}	
					}
					else
						{
							levelHierarchy = levelHierarchy + "/" + "No Level 2";
						}				
					
					//	Set the value of s.prop17
					if (levelCollection[2])
					{
						if (level3 != trim(levelCollection[2]))
						{
							s.prop17 = trim(levelCollection[2]).toLowerCase();
						}
						level3 = trim(levelCollection[2]).toLowerCase();
						
						if (level3.length > 0)
						{
							levelHierarchy = levelHierarchy + "/" + trim(levelCollection[2]).toLowerCase();
						}
						else
						{
							levelHierarchy = levelHierarchy + "/" + "No Level 3";
						}	
					}
					else
						{
							levelHierarchy = levelHierarchy + "/" + "No Level 3";
						}	
						
					s.hier2=levelHierarchy.toLowerCase();
					
					// Count the number of pages visited
					pagecnt = ++pagecnt;
					if (pagecnt > 1)
					{
						s.tl(true,'o','Channel Hierarchy');
					}

					//Reset variables to empty to avoid double hits
					s.prop15 = undefined;
					s.prop16 = undefined;
					s.prop17 = undefined;
					s.hier2 = undefined;
					levelHierarchy = '';
				}
				s.linkTrackVars='prop21,prop22,prop23,prop24';
			}
		}
	}
