/*------------------------------------------------------------------------------------------------------------------------------------------/
 * Name:			Analytics Manager Configuration File
 * Version: 		0.2
 * Application: 	Intraposte: CPC Drug Plan
 * Last Modified: 	2009/02/25
 * Description: 	The Analytics Manager config file is used to initialize all of the analytics attributes which are passed to Omniture. 
 *------------------------------------------------------------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------------------------------------------------------------------/
 * parseUri 1.2.1
 * (c) 2007 Steven Levithan <stevenlevithan.com>
 * MIT License
 * ----------------------
 * splits any well-formed URI into the following parts (all are optional):
 * source,protocol,authority,userInfo,user,password,host,port,relative,path,directory,file,query,anchor
 *------------------------------------------------------------------------------------------------------------------------------------------*/

/*-- Start Custom Functions ----------------------------------------------------------------------------------------------------------------*/
function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

/********************************************************************************************************
fSetDefaultVars 1.2
Last Modified: 	2008/10/15
----------------------
Parses the "directory" portion of the URL to provide a the following default analyticsData attributes:
- analyticsData.hierarchy, analyticsData.pageName, analyticsData.siteSection, analyticsData.siteSection2, analyticsData.siteSection3, analyticsData.siteSection4
- Checks for application directories and sets the appropriate analyticsData.app attribute.
*********************************************************************************************************/
function fSetDefaultVars(_sDelimiter){
	/* Split the Directories and add each section into a unique siteSection variable. */ 
	var _sDirectories= analyticsPath.replace(/(^\/)|(\/$)/g,"");	//Removes starting and trailing "/" and creates an array of values.
	_sDirectories= _sDirectories.replace(/.(aspx|shtml|html|htm|asp)/g,"").replace(/_/g," ").replace(/\/dfc/g,""); // Remove parts of URL that are not required.
	
	/* Application Segments */
	if(uriFileName.match("tier1")){
		 _sDirectories= _sDirectories.replace("tier1","single tier");
		 analyticsData.appRequestType="single tier";
		 analyticsData.appState="form";
		 if(uriFileName.match("info"))	analyticsData.appState="info";				   
		  _sDirectories+= ("/" + analyticsData.appState);
		  analyticsData.webForm="cpc drug plan"; // Application Name
	}
	
	if(uriFileName.match("tier3")){
		 _sDirectories= _sDirectories.replace("tier3","three tier");	
		 analyticsData.appRequestType="three tier";
		 analyticsData.appState="form";
		 if(uriFileName.match("info"))	analyticsData.appState="info";				   
		  _sDirectories+= ("/" + analyticsData.appState);
		 analyticsData.webForm="cpc drug plan";	// Application Name
	}

	_sDirectories= _sDirectories.split("/");		// Create an array of all directories in the URL.

	// Set local varaibles
	var _aLen= _sDirectories.length;
	var _sSiteSection="";
	var _sQueryString= uriQueryString;
	var _sQueryKey= parseUri(analyticsURL).queryKey;
	var _sAnchor= uriAnchor;
	
	//Loop through each directory in the URL
	for (x=0; x<_aLen; x++){
	var _sDirectory= _sDirectories[x];
	
		if(x==0){
			_sSiteSection= _sDirectory;
			analyticsData.siteSection= _sSiteSection;
		}else{		
			/* Set Remaining Site Sections */
			_sSiteSection += analyticsData.delimiter + _sDirectory;
			analyticsData["siteSection" + (x+1)] = _sSiteSection;
		}
		
	}
	
	/* Set the Page Name attribute */
	analyticsData.pageName= _sSiteSection;
	analyticsData.hierarchy= analyticsData.pageName.replace(/ > /g,",");
}


/********************************************************************************************************
addBannerID(bannerID) 1.0
Last Modified: 	2008/06/04
----------------------
An instance of the function is called for each banner displayed on the page. Tracks banner impressions.
*********************************************************************************************************/
function addBannerID(bannerID){
	var _bannerID= bannerID;
	if(_bannerID){
		analyticsData.banners += "," + 	bannerID;
	}		
}

/********************************************************************************************************
replaceValue(oldValue,newValue)
Last Modified: 	2008/10/31
----------------------
Replaces an analytics value with a more intuitive value for reporting.
*********************************************************************************************************/
function replaceValue(oldValue,newValue){
	analyticsData.pageName= analyticsData.pageName.replace(oldValue,newValue);
	analyticsData.siteSection= analyticsData.siteSection.replace(oldValue,newValue);
	analyticsData.siteSection2= analyticsData.siteSection2.replace(oldValue,newValue);
	analyticsData.siteSection3= analyticsData.siteSection3.replace(oldValue,newValue);
	analyticsData.siteSection4= analyticsData.siteSection4.replace(oldValue,newValue);
	analyticsData.hierarchy= analyticsData.hierarchy.replace(oldValue,newValue);
}

/********************************************************************************************************
takeYear(theDate)
Last Modified: 	2008/01/05
----------------------
Retrieves and stores the current year.
*********************************************************************************************************/
/* Get the year for the getTimeParting Plug-in */
function takeYear(theDate) {
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}
/*-- End Custom Functions --------------------------------------------------------------------------------------------------------------------*/

/*-- Set Global Variables -------------------------------------------------------------------------------------------*/
var analyticsData = new Object();				// Initialize the analytics data object. 
var analyticsURL= location.href.toLowerCase();	// Obtain the full URL

/* Set global variables which use the parseUri function */ 
var analyticsPath= parseUri(analyticsURL).path.replace(/(^\/)|(\/$)/g,"");		// Obtain the URL path
var uriDomain= parseUri(analyticsURL).host;
var uriDirectories= parseUri(analyticsURL).directory;
var uriFileName= parseUri(analyticsURL).file;
var uriQueryString= parseUri(analyticsURL).query;
var uriAnchor= parseUri(analyticsURL).anchor;

/*-- End Global Variables -------------------------------------------------------------------------------------------*/

/*-- Set Analytics Data Attributes -----------------------------------------------------------------------------------------------------------*/
var omniToday = new Date();
analyticsData.year = takeYear(omniToday);					// Current Year
analyticsData.siteName="intraposte";
analyticsData.prefix = analyticsData.siteName + ": ";		// Prefix added to the beginning of each variable to designate as cpc.ca vs. epost.ca
analyticsData.delimiter= " > ";								// Default delimter for all delimited variable values.
analyticsData.domain=document.domain.toLowerCase();			// Domain Name
analyticsData.style="standard";								// User experience (standard vs. basic)
analyticsData.loginStatus="anonymous";						// Loging Status (anonymous vs. registered)

/* Initialize Default metrics */
// Only set if not set using fSetDefaultVars
if(!analyticsData.hierarchy)		analyticsData.hierarchy="";		// Default Hierarchy
if(!analyticsData.fileName)			analyticsData.fileName="";		// Default File Name
if(!analyticsData.pageName)			analyticsData.pageName="";		// Default Page Name
if(!analyticsData.basicPageName)	analyticsData.basicPageName="";	// Basic Page Name
if(!analyticsData.siteSection)		analyticsData.siteSection="";	// Default Site Section
if(!analyticsData.siteSection2)		analyticsData.siteSection2="";	// Default Site Section 2
if(!analyticsData.siteSection3)		analyticsData.siteSection3="";	// Default Site Section 3
if(!analyticsData.siteSection4)		analyticsData.siteSection4="";	// Default Site Section 4	
if(!analyticsData.language)			analyticsData.language="en";		// Language Variable, set in Omniture.ascx
if(!analyticsData.loginguid)		analyticsData.loginguid="";		// Captures the user id of a user in a registered status. 
if(!analyticsData.banners)			analyticsData.banners="";		// Initialize the banners attribute. 

fSetDefaultVars(analyticsData.delimiter);						// Sets all default traffic variables

/* Initialize Application Attributes */
if(!analyticsData.webForm) analyticsData.webForm="";							//Web Form
if(!analyticsData.appRequestType) analyticsData.appRequestType="";				//Application Request Type
if(!analyticsData.appState) analyticsData.appState="";					//Application State 

/* Initialize Application Events */
analyticsData.products="";	//Commerce Products
analyticsData.appInitiate= false;	//Application Initation
analyticsData.appCommit= false;	//Application Commitment
analyticsData.appInfoNotFound= false;	//Information Not Found
analyticsData.appError= false;	//Application Error
analyticsData.appComplete= false;	//Application Success
analyticsData.appCallToAction= false;	//Application Call to Action

/* Initialize Search Attributes */
analyticsData.searchQuery="";	//Sets the search terms entered into the website search application. 
analyticsData.searchType="";	//Specifices the specific type of search (basic vs. advanced)

/* Initialize Error Handling Attributes */
analyticsData.errorType="";	//Sets the type of error.
analyticsData.errorMsg="";	//Set the error message displayed to the user. 
analyticsData.formData="";	//Captures a colon delimited list of values entered into the form at the time of error. 

/*-- End Set Analytics Data Attributes -------------------------------------------------------------------------------------------*/

/*-- Clear Global Objects --------------------------------------------------------------------------------------------------------*/
void(parseUri);		//Clear the parseUri object.
/*-- End Clear Global Objects ----------------------------------------------------------------------------------------------------*/

/* Set analytics on or off */
analyticsData.track="on";

/*------------------------------------------------------------------------------------------------------------------------------------------/
 * Name:			Canada Post Employee Drug Plan - Analytics Variables
 * Version: 		0.3
 * Last Modified: 	2009/03/17
 * Description: 	Sets default analytics variables for the Employee Drug Plan Application.
 *------------------------------------------------------------------------------------------------------------------------------------------*/
if(analyticsData.track=="on"){	
	replaceValue("benefitseap","cpc drug plan"); // User friendly value for benefits section
	if (analyticsData.appRequestType){	
		analyticsData.webForm+= analyticsData.delimiter + analyticsData.appRequestType;	//Add the appication segment to the webform name.
	}
}
