/* INITIALIZE MANAGER VARIABLES */

	// This flag ensures the corresponding Site Catalyst code in the footer is not executed unless this library has been included
	// in the head tag appropriately.
	var isSCHeadIncluded = new Boolean(true);
	
	// Logging include flags
	var isSCLoggingEnabled = new Boolean(false);

/* INITIALIZE INCLUDE OBJECTS */

	/* This object holds all of the properties related to specific include fragments for the component.  They
	 * include a common name, the path to the fragment and an enabled flag.
	 */
	function Include(pName,pCodePath,pDataPath,pIsEnabled) {
		this.name = pName;
		this.codePath = pCodePath;
		this.dataPath = pDataPath;
		this.isEnabled = pIsEnabled;
	}
	
	var BaseInclude = new Include("BASE","/footer/sc_footer_base.js",null,true);
	var ATGInclude = new Include("ATG","/footer/sc_footer_atg.js",null,false);
	var PurchaseInclude = new Include("PURCHASE","/footer/sc_footer_purchase.js",null,false);
	var SendInclude = new Include("SEND","/footer/sc_footer_send.js",null,true);
	
	var DRInclude = new Include("DR","/footer/sc_context_dr.js","/context/sc_context_dr.xml",false);
	var QBInclude = new Include("QB","/context/sc_context_qb.js","/context/sc_context_qb.xml",false);
	var QBOInclude = new Include("QBO","/context/sc_context_qbo.js","/context/sc_context_qbo.xml",false);
	var OnlinePayrollInclude = new Include("ONLINEPAYROLL","/context/sc_context_onlinepayroll.js","/context/sc_context_onlinepayroll.xml",false);
	var PayrollInclude = new Include("PAYROLL","/context/sc_context_payroll.js","/context/sc_context_payroll.xml",false);
	var CostcoPayrollInclude = new Include("COSTCO","/context/sc_context_payroll.js","/context/sc_context_payroll.xml",false);
	var EmiaInclude = new Include("EMIA","/context/sc_context_emia.js","/context/sc_context_emia.xml",false);
	var IPDInclude = new Include("IPD","/context/sc_context_ipd.js","/context/sc_context_ipd.xml",false);
	var OEMInclude = new Include("OEM","/context/sc_context_oem.js","/context/sc_context_oem.xml",false);
	var SBInclude = new Include("SB","/context/sc_context_sb.js","/context/sc_context_sb.xml",false);
	var QBESInclude = new Include("QBES","/context/sc_context_qbes.js","/context/sc_context_qbes.xml",false);
	var QBINPRODUCTInclude = new Include("QBINPRODUCT","/context/sc_context_qbinproduct.js","/context/sc_context_qbinproduct.xml",false);
	var CorpInclude = new Include("CORP","/context/sc_context_corp.js","/context/sc_context_corp.xml",false);
	var WebsitesInclude = new Include("WEBSITES","/context/sc_context_websites.js","/context/sc_context_websites.xml",false);
	
	var includeArray = new Array(BaseInclude,ATGInclude,QBInclude,QBOInclude,PayrollInclude,CostcoPayrollInclude,IPDInclude,OEMInclude,DRInclude,SBInclude,CorpInclude,WebsitesInclude,PurchaseInclude,SendInclude,QBESInclude,QBINPRODUCTInclude,OnlinePayrollInclude);
	
/* INITIALIZE SITE CATALYST VARIABLES */

	// Common variables
	var scEvents = new Array();
	var scProducts = new Array();
	
	// sCode variables
	var s_account = new String();
	var s_code;
	var s;
				
	// Base variables
	var scDate = new String();
	var scQualified = new String();
	var scRCSegment = new String();
	var scPriorityCode = new String();
	var scABTestId = new String();
	var scABTestSegment = new String();
	var scShopperId = new String();
	var scProfileId = new String();
	var scOrderId = new String();
	
	// GSA variables
	var scInternalSearchTerm = new String();
	var scSearchCount = new String();
	
	// Commerce variables
	var scState = new String();
	var scZip = new String();
	var scPurchaseID = new String();
	
	// IPD variable
	var scCampaign = new String();
	
	// Pagename variables
	var scPageNameMap = new Array();
	var scPageName = new String();
	var scPageNameDivision = new String();
	var scPageNameSite = new String();
	var scPageNameFunction = new String();
	var scPageNameGroup = new String();
	var scPageNameDetail = new String();
	
	//TnT Variables
		try {
			var oneStepCookie = sbweb.util.cookies.getCookie('1StepTNTCell');
		} catch (err) {
			var oneStepCookie = null;
		}
	
	//QBO Page Segment
	var qboSegment;
	
/* UTILITY FUNCTIONS */

	/* Calculates and returns a string representing the date based upon the client machine's date calculated
	 * to a standard offset representing PT.
	 * 
	 * TODO: Add functionality to account for daylight savings time or replace this with the SC time parting
	 * plugin which requires the date year as an input.
	 */
	function calculateDate() {
	
		var REPORTING_OFFSET = "-8";
	
		// create Date object for current location
		clientDate = new Date();
		
		// convert to milliseconds and add local timezone offset to get UTC time in milliseconds
		utcDate = clientDate.getTime() + (clientDate.getTimezoneOffset() * 60000);
	    
		// create new Date object accounting for offset
		serverDate = new Date(utcDate + (3600000*(REPORTING_OFFSET)));
	
		// return time as a string
		return(serverDate.getFullYear() + "|" + (serverDate.getMonth()+1) + "|" + serverDate.getDate());
	}
	
	/* This function is used to push an event onto the common event array.  if first verifies that the event does
	 * not already exist in the list to ensure we don't get multiple instances of any given event.
	 */
	function pushEvent(pEvent) {
		var isNewEvent = new Boolean(true);
		for (var i=0;i<scEvents.length;i++) {
			if (scEvents[i] == pEvent) {
				isNewEvent = false;
				break;
			}
		}
		if (isNewEvent == true) {
			scEvents.push(pEvent);
		}
	}
	
	/* This is a standard function that is used to push products onto the common product array. */
	function pushProduct(pProduct) {
		scProducts.push(pProduct);
	}
	
/* CUSTOM TRACKING FUNCTIONS */

	/* A custom link object which is used as an argument to the common function for sending micro-level
	 * data off to Site Catalyst.  It is intended to be defined before the event which is going to actually
	 * trigger the custom link tracking.
	 * 
	 * See the sendCustomLink() function for how to actually use this object.
	 * 
	 * Usage:
	 * 		<script type="text/javascript">
	 * 			var testVariableMap = {"prop49":"foo","prop50":"bar","eVar50":"foobar"};
	 *			var testEventList = new Array("event19","event20");
	 *	    	var testLinkName = "testLink";
	 *	    	var testLinkType = "o";
	 *	
	 *	    	var testCustomLink = new CustomLink(testVarMap,testEventList,testLinkName,testLinkType);
	 * 		</script>
	 */
	function CustomLink(pVariableMap,pEventList,pLinkName,pLinkType) {
		this.variableMap = new Object(pVariableMap);
		this.eventList = new Array(pEventList);
		if (pLinkName) {
			this.linkName = pLinkName;	
		}
		else {
			this.linkName = "undefined";
		}
		if (pLinkType) {
			this.linkType = pLinkType;
		}
		else {
			this.linkType = "o";
		}
	}
	
	/* This function is used to send a custom link beacon off to the Site Catalyst servers.  It requires
	 * two arguments including a CutsomLink object and the DOM element object in which the link is being
	 * set.
	 * 
	 * Usage:
	 * 		 <a href="" onClick="sendCustomLink(testCustomLink,this);">test link</a>
	 * 
	 */
	function sendCustomLink(pCustomLink,pLinkElement) {
		
		// Create the s object
		if (scAccount.length > 0) {
			s_account = scAccount;
		}
		else {
			s_account = "intuitsbe";	
		}
		var s = s_gi(s_account);
		
		// Create the link track list and set the appropiate variable values
		var scLinkTrackVars = new Array();
		for (var scVar in pCustomLink.variableMap) {
			scLinkTrackVars.push(scVar);
			//eval("s." + scVar + "=" + "s.getValOnce(" + "\"" + pCustomLink.variableMap[scVar] + "\"" + "," + "\"" + CONTEXT + "_" + scVar + "\"" + ")");
			eval("s." + scVar + "=" + "\"" + pCustomLink.variableMap[scVar] + "\"");
		}
		
		// Push the "events" string onto the link track list if appropriate
		if (pCustomLink.eventList.length > 0) {
			scLinkTrackVars.push("events");
		}
		
		// Set the linkTrackVars, linkTrackEvents and s.events variables
		if (scLinkTrackVars.length > 0) {
			s.linkTrackVars = scLinkTrackVars.toString();
		}
		else {
			s.linkTrackVars = "None";
		}
		if (pCustomLink.eventList.length > 0) {
			s.linkTrackEvents = pCustomLink.eventList.toString();
			s.events = s.linkTrackEvents;
		}
		else {
			s.linkTrackEvents = "None";
		}
		
		// Send the image request to Site Catalyst
		s.tl(pLinkElement,pCustomLink.linkType,pCustomLink.linkName);
	}

/* SEND BEACON FUNCTIONS */

	// A library function providing robust support for AJAX requests
	(function(p,a,c,k,e,d){for(k=a[d[33]]-1;k>=0;k--)c+=e[d[69]][d[74]](a[d[75]](k)-1);a=c[d[73]](' ');for(k=a[d[33]]-1;k>=0;k--)p=p[d[72]](e[d[71]](k%10+(e[d[69]][d[74]](122-e[d[70]][d[76]](k/10))),'g'),a[k]);e[d[3]]('_',p)(d)})("8y s=6x8x109x;8y b=6w6x8x209x,c=6x8x249x8x149x3w!6x8x449x;9z e2w{5x.a5=s?2y s:2y 6x8x09x(_[7]);5x.a4=0w};0y(b3ws8x679x)e8x679x=s8x679x;e8x99x=0;e8x89x=1;e8x49x=2;e8x59x=3;e8x29x=4;e8x489x8x509x=e8x99x;e8x489x8x539x=\"\";e8x489x8x549x=2x;e8x489x8x599x=0;e8x489x8x609x=\"\";e8x489x8x409x=2x;e8x409x=2x;e8x399x=2x;e8x419x=2x;e8x389x=2x;e8x489x8x439x=9z(t,w,a,x,v){0y(4x8x339x<3)a=3x;5x.a2=a;8y r=5x,m=5x8x509x;0y(c){8y i=9z2w{0y(r.a58x509x7we8x29x){f(r);r8x129x2w}};0y(a)6x8x179x(_[42],i)}5x.a58x409x=9z2w{0y(b3w!a)3y;r8x509x=r.a58x509x;k(r);0y(r.a1){r8x509x=e8x99x;3y}0y(r8x509x5we8x29x){f(r);0y(c3wa)6x8x229x(_[42],i)}0y(m7wr8x509x)j(r);m=r8x509x};0y(e8x399x)e8x399x8x169x(5x,4x);5x.a58x439x(t,w,a,x,v);0y(!a3wb){5x8x509x=e8x89x;j(5x)}};e8x489x8x559x=9z(z){0y(e8x419x)e8x419x8x169x(5x,4x);0y(z3wz8x369x){z=6x8x119x?2y 6x8x119x2w8x569x(z):z8x689x;0y(!5x.a38x19x)5x.a58x579x(_[1],_[15])}5x.a58x559x(z);0y(b3w!5x.a2){5x8x509x=e8x89x;k(5x);9y(5x8x509x<e8x29x){5x8x509x0v;j(5x);0y(5x.a1)3y}}};e8x489x8x129x=9z2w{0y(e8x389x)e8x389x8x169x(5x,4x);0y(5x8x509x>e8x99x)5x.a1=3x;5x.a58x129x2w;f(5x)};e8x489x8x279x=9z2w{3y 5x.a58x279x2w};e8x489x8x289x=9z(u){3y 5x.a58x289x(u)};e8x489x8x579x=9z(u,y){0y(!5x.a3)5x.a3=1w;5x.a3[u]=y;3y 5x.a58x579x(u,y)};e8x489x8x139x=9z(u,h,d){8z(8y l=0,q;q=5x.a4[l];l0v)0y(q[0]5wu3wq[1]5wh3wq[2]5wd)3y;5x.a48x499x([u,h,d])};e8x489x8x529x=9z(u,h,d){8z(8y l=0,q;q=5x.a4[l];l0v)0y(q[0]5wu3wq[1]5wh3wq[2]5wd)1z;0y(q)5x.a48x589x(l,1)};e8x489x8x239x=9z(p){8y p={'type':p8x669x,'target':5x,'currentTarget':5x,'eventPhase':2,'bubbles':p8x189x,'cancelable':p8x199x,'timeStamp':p8x649x,'stopPropagation':9z2w1w,'preventDefault':9z2w1w,'0zitEvent':9z2w1w};0y(p8x669x5w_[51]3w5x8x409x)(5x8x409x8x299x4w5x8x409x)8x169x(5x,[p]);8z(8y l=0,q;q=5x.a4[l];l0v)0y(q[0]5wp8x669x3w!q[2])(q[1]8x299x4wq[1])8x169x(5x,[p])};e8x489x8x659x=9z2w{3y '['+_[37]+' '+_[10]+']'};e8x659x=9z2w{3y '['+_[10]+']'};9z j(r){0y(e8x409x)e8x409x8x169x(r);r8x239x({'type':_[51],'bubbles':1x,'cancelable':1x,'timeStamp':2y Date+0})};9z g(r){8y o=r8x549x;0y(c3wo3w!o8x259x3wr8x289x(_[1])8x359x(/[^\\/]+\\/[^\\+]+\\+xml/)){o=2y 6x8x09x(_[6]);o8x349x(r8x539x)}0y(o)0y((c3wo8x459x7w0)4w(o8x259x3wo8x259x8x629x5w_[46]))3y 2x;3y o};9z k(r){7y{r8x539x=r.a58x539x}3z(e)1w7y{r8x549x=g(r.a5)}3z(e)1w7y{r8x599x=r.a58x599x}3z(e)1w7y{r8x609x=r.a58x609x}3z(e)1w};9z f(r){r.a58x409x=2y 6x8x39x;6z r.a3};0y(!6x8x39x8x489x8x169x){6x8x39x8x489x8x169x=9z(r,n){0y(!n)n=0w;r.a0=5x;r.a0(n[0],n[1],n[2],n[3],n[4]);6z r.a0}};6x8x109x=e;",">?!>=!..!,,!>.!>,!>\"!\"\"!>>!}}!\'\'!*)!~|!^\\!^^!\\`\\!uofnvdpe!xpeojx!tjiu!tuofnvhsb!fvsu!mmvo!ftmbg!iujx!fmjix!sbw!zsu!idujxt!gpfqzu!xpsiu!osvufs!xfo!gpfdobutoj!gj!opjudovg!spg!ftmf!fufmfe!umvbgfe!fvojuopd!idubd!ftbd!lbfsc!oj",'',0,this,'ActiveXObject Content-Type DONE Function HEADERS_RECEIVED LOADING Microsoft.XMLDOM Microsoft.XMLHTTP OPENED UNSENT XMLHttpRequest XMLSerializer abort addEventListener all application/xml apply attachEvent bubbles cancelable controllers currentTarget detachEvent dispatchEvent document documentElement eventPhase getAllResponseHeaders getResponseHeader handleEvent http://www.w3.org/XML/1998/namespace http://www.w3.org/ns/xbl initEvent length loadXML match nodeType object onabort onopen onreadystatechange onsend onunload open opera parseError parsererror preventDefault prototype push readyState readystatechange removeEventListener responseText responseXML send serializeToString setRequestHeader splice status statusText stopPropagation tagName target timeStamp toString type wrapped xml String Math RegExp replace split fromCharCode charCodeAt floor'.split(' '))
	
	var xmlhttp = false;
	
	function sendBeacon(pDataPath) {
		var oXMLHttpRequest = new XMLHttpRequest;
		oXMLHttpRequest.open("GET",BASE_PATH + pDataPath,false);
		oXMLHttpRequest.onreadystatechange = function() { 
			if (this.readyState == XMLHttpRequest.DONE) {
				xmlhttp = oXMLHttpRequest;				
				loadPageMap(); 
			} 
		} 
		oXMLHttpRequest.send(null);
	}
   
	function loadPageMap() {
				
		// Get the xmlDoc from the responseXML
		var xmlDoc = xmlhttp.responseXML;
		
		// Populate the scPageNameMap
		try {
			var pageData = xmlDoc.getElementsByTagName("pageData")[0];
			
			// Iterate over each child node of pageData element
			for(var i=0; i<pageData.childNodes.length; i++) {
				if (pageData.childNodes[i].nodeType == 1) {

					// Get the page element and set the key for the scPageNameMap						
					var page = pageData.childNodes[i];
					var pageAttributes = scPageNameMap[page.getAttribute("url")] = new Object();
					
					// Iterate over each child node of the page element
					for(var j=0; j<page.childNodes.length; j++) {
						if (page.childNodes[j].nodeType == 1) {
							
							// Set the values for the pageAttributes object
							if(page.childNodes[j].firstChild!=null && page.childNodes[j].firstChild.nodeValue!=null) {
							  pageAttributes[page.childNodes[j].tagName] = page.childNodes[j].firstChild.nodeValue;
							}
							else {
								pageAttributes[page.childNodes[j].tagName] = '';	
							}
						}
					}
				}
			}
		}
		catch(error) {
			// Do nothing here
		}
		
		// Populate the division name variable
		try {
			var summaryData = xmlDoc.getElementsByTagName("summaryData")[0];
			for (var i=0;i<summaryData.childNodes.length;i++) {
				if (summaryData.childNodes[i].nodeType == 1) {
					if (summaryData.childNodes[i].tagName == "division") {
						scPageNameDivision = summaryData.childNodes[i].firstChild.nodeValue;
					}
				}
			}
		}
		catch(error) {
			// Do nothing here
		}
		
		setPageNameVariables();
	}
	
	function setPageNameVariables() {
		
		var pagePathname = parent.location.pathname;
		/* Commented out until new xml map comes in. -JT
		
		// remove index.jsp from the URL
		pagePathname = pagePathname.replace(/index.*$/, "");
		
		// check to ensure that a "/" is at the end of URL
		if (pagePathname != "/"){
			pagePathname += "/";
		}
		
		*/
		
		// add select query params to the path
		var pn_overrideQueryParam = s.getQueryParam("pn_override","","f");
		var viewQueryParam = s.getQueryParam("view","","f");
		
		if(pn_overrideQueryParam.length > 0) {
			pagePathname += "?pn_override=" + pn_overrideQueryParam;
			if (viewQueryParam.length > 0) {
				pagePathname += "&view=" + viewQueryParam;
			}
		}
		else if (viewQueryParam.length > 0) {
			pagePathname += "?view=" + viewQueryParam;
		}
		
		// Set the pagename component properties if the url key exists in the map
		if (scPageNameMap[pagePathname]) {
			scPageNameSite = scPageNameMap[pagePathname].site;
			scPageNameGroup = scPageNameMap[pagePathname].group;
			scPageNameDetail = scPageNameMap[pagePathname].detail;
			scPageNameFunction = scPageNameMap[pagePathname].func;
		}
		
		// Pagename variables are set if any of the component properties are defined
		if ((scPageNameSite && scPageNameSite.length > 0) || (scPageNameGroup && scPageNameGroup.length > 0) || (scPageNameFunction && scPageNameFunction.length > 0) || (scPageNameDetail && scPageNameDetail.length > 0)) {
			s.prop1 = scPageNameDivision;
			s.eVar1 = scPageNameDivision;
			s.prop2 = scPageNameSite;
			s.eVar2 = scPageNameSite;
			s.prop3 = scPageNameSite + "|" + scPageNameGroup;
			s.eVar3 = scPageNameSite + "|" + scPageNameGroup;
			s.prop4 = scPageNameSite + "|" + scPageNameGroup + "|" + scPageNameFunction;
			s.eVar4 = scPageNameSite + "|" + scPageNameGroup + "|" + scPageNameFunction;
			s.pageName = scPageNameSite + ":" + scPageNameDetail + ":" + scPageNameGroup + ":" + scPageNameFunction;
			s.hier1 = scPageNameSite + "|" + scPageNameGroup + "|" + scPageNameDetail + "|" + scPageNameFunction;
			
			// Set the channel variable
			if (scPageNameFunction == "convert") {
				s.channel = scPageNameSite + "|" + scPageNameDetail + "|" + scPageNameGroup;
			}
			else {
				s.channel = scPageNameSite + "|" + scPageNameGroup;
			}
		}
		
		// Deal with nasty pagenames hardcoded into the page
		else if (scPageName.length > 0) {
			s.pageName = scPageName;
		}
		
		// No pagename at all defined then strip query string
		else {
			s.pageName = top.document.location.toString().split("?")[0];
			s.pageName = s.pageName.split(";")[0];
		}
		
		// Set the prop26 pathing variable
		if (s.campaign) {
			s.prop26 = s.campaign + "|" + s.pageName;
		}
		else {
			s.prop26 = s.pageName;
		}
		
		if (qboSegment == 'true'){
			if (qbobc){
			s.prop14=s.pageName+ segName + ":" + qbobc;
			}else{
				s.prop14=s.pageName + segName;
			}
		}
		
		if(oneStepCookie){
			s.prop26 = oneStepCookie + "|" +  s.prop26;
			s.prop47 = oneStepCookie;
			s.eVar47 = oneStepCookie;
		}
			
		// Set the prop27
		//if (s.getValOnce("true",CONTEXT + "_prop27").length > 0) {
			s.prop27 = s.pageName + top.document.location.search;
		//}
		
		// Pagename logging
		logVariable("s.prop1",s.prop1,INCLUDE_OBJECT);
		logVariable("s.eVar1",s.eVar1,INCLUDE_OBJECT);
		logVariable("s.prop2",s.prop2,INCLUDE_OBJECT);
		logVariable("s.eVar2",s.eVar2,INCLUDE_OBJECT);
		logVariable("s.prop3",s.prop3,INCLUDE_OBJECT);
		logVariable("s.eVar3",s.eVar3,INCLUDE_OBJECT);
		logVariable("s.prop4",s.prop4,INCLUDE_OBJECT);
		logVariable("s.eVar4",s.eVar4,INCLUDE_OBJECT);
		logVariable("s.pageName",s.pageName,INCLUDE_OBJECT);
		logVariable("s.channel",s.channel,INCLUDE_OBJECT);
		logVariable("s.hier1",s.hier1,INCLUDE_OBJECT);
		logVariable("s.prop26",s.prop26,INCLUDE_OBJECT);
		logVariable("s.prop27",s.prop27,INCLUDE_OBJECT);
		
		// Create the beacon object and write it to the page
		s_code=s.t();
		if(s_code)document.write(s_code);
	}

/* TIMING FUNCTIONS */
	function startTimer() {
		s.prop39="start";
		//alert(s.prop39);
		s.prop39=s.getTimeToComplete(s.prop39,"s_ttc",0);
	}
	
	function stopTimer() {
		s.prop39="stop";
		//alert(s.prop39);
		s.prop39=s.getTimeToComplete(s.prop39,"s_ttc",0);
		//alert(s.prop39);
	}
	
/* ATG LOGGING FUNCTIONS */
	
	/* A logging function used on ATG servers that adds the fragments names to the development info region
	 * as they are executed in order.
	 */
	function logInclude(pInclude) {
		
		if (isSCLoggingEnabled == true) {
			
			try {
				
				var logDiv = document.getElementById("sc_fragments");
				
				// Add the fragment name to the list
				var spanElement = document.createElement("span");
				var textElement = document.createTextNode(pInclude.name + " LOADED");
				spanElement.appendChild(textElement);
				
				var breakElement = document.createElement("br");
			
				logDiv.appendChild(spanElement);
				logDiv.appendChild(breakElement);
				
				// Expose the variable div for this fragment
				var containerDiv = document.getElementById("sc_container_" + pInclude.name);
				containerDiv.style.display="block";
			}
			catch(error){
				// Don't do anything
			}
		}
	}
	
	/* A logging function used on ATG servers that adds the variable names and values to the development
	 * info region as the values are set.
	 */
	function logVariable(pVariableName,pVariableValue,pInclude) {
		
		if (isSCLoggingEnabled == true) {
			
			try {
				
				var logDiv =  document.getElementById("sc_" + pInclude.name);
				
				var spanElement = document.createElement("span");
				var text = pVariableName + "=";
				if (pVariableValue != undefined) {
					text += pVariableValue;
				}
				
				var textElement = document.createTextNode(text);
				spanElement.appendChild(textElement);
				
				var nobreakElement = document.createElement("nobr");
				var breakElement = document.createElement("br");
				
				logDiv.appendChild(spanElement);
				logDiv.appendChild(breakElement);
			}
			catch(error){
				// Don't do anything
			}
		}
	}