
/*------------------------------------------------------------------------------------------------------------------------------------------/
 * 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*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};
/*-- End Custom Functions --------------------------------------------------------------------------------------------------------------------*/

/*-- Set Global Variables -------------------------------------------------------------------------------------------*/
var analyticsData = new Object();				// Initialize the analytics data object. 
var analyticsURL= location.href.toLowerCase();	// Obtain the full URL

var omniToday = new Date();
analyticsData.year = takeYear(omniToday);		// Current Year

/* 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).queryKey;
var uriQuery= parseUri(analyticsURL).query;
var uriAnchor= parseUri(analyticsURL).anchor;

/*-- End Global Variables -------------------------------------------------------------------------------------------*/

/********************************************************************************************************
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.
	
	/* Set default language */
	var _sFileName= uriFileName;
	analyticsData.language="en";
	if(_sFileName.match(/(-f)/g))	analyticsData.language="fr";

	// Remove directory references to language
	_sDirectories= _sDirectories.replace(/(\/en|\/fr|\/en\/|\/fr\/)/g,"");
	_sDirectories= _sDirectories.replace(/(-e|-f)/g,"").replace(/.(aspx|shtml|html|htm|do|aspx|asp)/g,"").replace(/_/g," ");
	_sDirectories= _sDirectories.replace(/aboutus/g,"about us");
	
	//Add default name if no filename exists
	if (_sFileName==""){
		_sDirectories += "/default";
	}
	
	// Create an array of all directories in the URL.
	_sDirectories= _sDirectories.split("/");
	
	var _aLen= _sDirectories.length;
	var _sSiteSection="";
	var _sQueryString= uriQueryString;
	var _sQueryKey= parseUri(analyticsURL).queryKey;
	var _sAnchor= uriAnchor;

	// Initialize the directories loop variable.
	var x=0;
	
	if(_sDirectories[x]=="" || _sDirectories[x]=="default") {
		switch (uriFileName){
			case "default.aspx":
					analyticsData.pageName="homepage"; 
					analyticsData.siteSection="homepage";
					analyticsData.hierarchy= analyticsData.pageName;
				break;
			case "language.aspx":
			default:
					analyticsData.pageName="language"; 
					analyticsData.siteSection="language";
					analyticsData.hierarchy= analyticsData.pageName;
				break;
		}
	}else{
		//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{	 
				// Append Site Section with next folder in directory structure
				if(x==1){
					switch(_sSiteSection){
					case "personal":
					case "business":
					case "about us":
						analyticsData.siteSection += analyticsData.delimiter + _sDirectory;
					}
				}
				
				/* Set Remaining Site Sections */
				 _sSiteSection += analyticsData.delimiter + _sDirectory;
				
				if(_aLen==2){
					analyticsData["siteSection" + (x+1)] = _sSiteSection;
				}else{
					analyticsData["siteSection" + (x)] = _sSiteSection;
				}
			}
		
		}
		
	/* Set the Page Name attribute */
	analyticsData.pageName= _sSiteSection;
		
	/* Add the query string value if applicable. */
	/* Applicable Query String Values: prid=, archiveyear=, letter=, JobID=, stamp=, pid=, r= , maintenance=, np= */
		if (_sQueryString) {			
			//Holiday: Recipies and Activities
			if (_sQueryKey.r) analyticsData.pageName += _sDelimiter + "r=" + _sQueryKey.r;
			if (_sQueryKey.pid) analyticsData.pageName += _sDelimiter + "pid=" + _sQueryKey.pid;
			
			//Maintenance Pages
			if (_sQueryKey.maintenance) analyticsData.pageName += _sDelimiter + "maintenance=" + _sQueryKey.maintenance;
			
			//Pages Not Found Referrer
			if (_sQueryKey.np) analyticsData.pageName += _sDelimiter + "np=" + _sQueryKey.np;	
		}

		analyticsData.hierarchy= analyticsData.pageName.replace(/ > /g,",");
	}
}

/********************************************************************************************************
fSetBusinessSegment 1.0
Last Modified: 	2008/06/03
----------------------
Parses the first directory of URL to determine the Business Segment
- If the business segment is not personal,business, or coporate the default value is common. 
*********************************************************************************************************/
function fSetBusinessSegment(_sDelimiter){
	// String the starting and trailing "/" and select the first folder in the list. 
	var _sBusinessSegment= uriDirectories.replace(/(^\/)|(\/$)/g,"").split("/").slice(0,1).toString();
	
	switch(_sBusinessSegment)
		{
			case "personal":
			case "business":
				return _sBusinessSegment;
			break;
			
			case "aboutus":
				_sBusinessSegment="about us";
				return _sBusinessSegment;
			default:
				return "common";
			break;			
		}	
}

/********************************************************************************************************
addBannerID 1.0
Last Modified: 	2008/06/04
----------------------
Adds a bannerID to an array of banners located on a single page. Used for tracking banner impressions.
*********************************************************************************************************/
function addBannerID(bannerID){
	var _bannerID= bannerID;
	if(_bannerID){
		analyticsData.banners += "," + 	bannerID;
	}		
}

/********************************************************************************************************
replaceValue(oldValue,newValue) 1.0
Last Modified: 	2008/10/31
----------------------
Replaces the 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;
}

/*-- Set Analytics Data Attributes -----------------------------------------------------------------------------------------------------------*/
analyticsData.siteName="cpc.ca";
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.
fSetDefaultVars(analyticsData.delimiter);					// Sets all default traffic variables
analyticsData.segment= fSetBusinessSegment();				// Business Segement
analyticsData.domain=document.domain.toLowerCase();			// Domain Name
analyticsData.style="standard";								// User experience (standard vs. basic)
analyticsData.loginStatus="anonymous";						// Loging Status (anonymous vs. registered)

/* Ensure all default metrics initialized if not sot in 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.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="";							// 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. 

/* Set Application Attributes */
analyticsData.app="";								//Application or Web Form Name
analyticsData.appRequestType="";					//Application Request Type
analyticsData.appState="";							//Application State
analyticsData.appInitiate= false;					//Specifies whether the current state in the initiation of an application. 
analyticsData.appCommit= false;						//Specifies whether the current application state is the commitment to an application. 
analyticsData.appRequestType= "";					//Sets the application segment. Value differs by application. 
analyticsData.productSku="";						//Captures the sku of a product in commerce based applications. 
analyticsData.messagegridIDs="";					//Sets the id of each message grid to track banner message grids. 
analyticsData.searchQuery="";						//Sets the search terms entered into the website search application. 
analyticsData.searchType="";						//Specifices the specific type of search (basic vs. advanced)

/* Set 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. 


/* Legacy 404 Pages */
analyticsData.pageNotFound="";
if (uriQueryString.np) analyticsData.pageNotFound= uriQueryString.np;
/*-- End Set Analytics Data Attributes -------------------------------------------------------------------------------------------*/

/* Set analytics on or off */
analyticsData.track="on";

/*--Start Custom Analytics Plug-ins --------------------------------------------------
1. Create a new case in the fCustomAnalytics() function to include the URL path to the new form or campaign.
2. Create and call a new function which sets the custom analytics variables.
-------------------------------------------------------------------------------------*/
function fCustomAnalytics(){
	switch(uriDirectories){
		case "/business/campaigns/sorted/sortednews/":
		case "/business/campaigns/sorted/forms/":
				fSortedDirectNews();	//QC 1592 - Sorted Direct News - 2009/02/21
			break;
		case "/business/prodserv/mdm/valueofmail/forms/":
				fValueOfMail();			//QC 1247 - Value of Mail Campaign - 2009/02/21
			break;
	}
}

// Use this function to track standard 2 page web forms. 
function fTrackForm(sFormName){
	/* Custom Form Tracking */
	if (uriDirectories.match("forms")) {
		analyticsData.webForm= sFormName;
		switch (uriFileName){
			case "default-e.asp":
			case "default-f.asp":
					analyticsData.appInitiate=true;
				break;
			case "thankyou-e.asp":
			case "thankyou-f.asp":
					analyticsData.appCommit=true;
					analyticsData.appCallToAction=true;
				break;
		}
	}
}


/* QC 1592 - Sorted Direct News - 2009/02/21 */
function fSortedDirectNews(){
	replaceValue("mdm","direct mail");
	fTrackForm("dm > sorted magazine > form");
}

/* QC 1247 - Value of Mail Campaign - 2009/02/21*/
function fValueOfMail(){					
	replaceValue("mdm","direct mail");
	replaceValue("valueofmail","value of mail");
	
	if (uriDirectories.match("forms")) {
		analyticsData.webForm= "dm > value of mail > form";
		switch (uriFileName){
			case "default-e.asp":
			case "default-f.asp":
					analyticsData.appInitiate=true;
				break;
			case "info-e.asp":
			case "info-f.asp":
					analyticsData.appCommit=true;
				break;
			case "thankyou-e.asp":
			case "thankyou-f.asp":
					analyticsData.appCallToAction=true;
				break;
		}
		
		//Information Not Found Event
		if(uriQueryString.error==-1){
			analyticsData.appInitiate=false;
			analyticsData.appCommit=false;
			analyticsData.appCallToAction=false;
			analyticsData.appInformationNotFound=true;
		}
	}
}

function fSetOpinionLab(){
	analyticsData.olCard="www";		//Default Comment Card
	
	if (uriDirectories.match("/cpc2/addrm/hh/")) {
		analyticsData.olCard="householder";
	}
}

/* Call Custom Analytics */
if (analyticsData.track=="on"){
	fCustomAnalytics();	
	fSetOpinionLab();
}

/*--End Custom Analytics Plug-ins --------------------------------------------------

/*-- Clear Global Objects --------------------------------------------------------------------------------------------------------*/
void(parseUri);		//Clear the parseUri object.
/*-- End Clear Global Objects ----------------------------------------------------------------------------------------------------*/

/*-- OpinionLab - Start --------------------------------------------------------------------------------------------------------*/
/* OnlineOpinion (S3tS v3.1) */
/* This product and other products of OpinionLab, Inc. are protected by U.S. Patent No. 6606581, 6421724, 6785717 B1 and other patents pending. */
var custom_var,_sp='%3A\\/\\/',_rp='%3A//',_poE=0.0, _poX=0.0,_sH=screen.height,_d=document,_w=window,_ht=_w.location.href,_hr=_d.referrer,_tm=(new Date()).getTime(),_kp=0,_sW=screen.width;
/* Add www to domain if it does not exist */
if(_ht.match(/\/\/canadapost.ca/g)){
	_ht= _ht.replace(/\/\/canadapost.ca/g,"//www.canadapost.ca");
}else if(_ht.match(/\/\/postescanada.ca/g)){
	_ht= _ht.replace(/\/\/postescanada.ca/g,"//www.postescanada.ca");
}
function _fC(_u){_aT=_sp+',\\/,\\.,-,_,'+_rp+',%2F,%2E,%2D,%5F';_aA=_aT.split(',');for(i=0;i<5;i++){eval('_u=_u.replace(/'+_aA[i]+'/g,_aA[i+5])')}return _u};function O_LC(olLoc,olRef){if(!olLoc){olLoc=_ht;}if(!olRef){olRef=_hr;}_w.open('https://secure.opinionlab.com/ccc01/comment_card.asp?time1='+_tm+'&time2='+(new Date()).getTime()+'&prev='+_fC(escape(olRef))+'&referer='+_fC(escape(olLoc))+'&height='+_sH+'&width='+_sW+'&custom_var='+custom_var,'comments','width=535,height=192,screenX='+((_sW-535)/2)+',screenY='+((_sH-192)/2)+',top='+((_sH-192)/2)+',left='+((_sW-535)/2)+',resizable=yes,copyhistory=yes,scrollbars=no');};function _fPe(){if(Math.random()>=1.0-_poE){O_LC();_poX=0.0}};function _fPx(){if(Math.random()>=1.0-_poX)O_LC()};window.onunload=_fPx;function O_GoT(_p){_d.write('<a href=\'javascript:O_LC()\'>'+_p+'</a>');_fPe()}

/*
* Load Opinion Lab comment card
*/
function openCC(domain){
	  
	  if(!domain){
		domain= "www";	//Set default domain to www
	  }
	  	  
	  olLoc= window.location.href;
	  olRef= document.referrer;
	  
	  if(parseUri(olLoc).port){
	  	fullDomain= parseUri(olLoc).host + ":" + parseUri(olLoc).port;
	  }else{
		 fullDomain= parseUri(olLoc).host; 
	  }
	  
	  switch(analyticsData.language){
	  	case "French":	
			olLoc= olLoc.replace(fullDomain,domain + ".postescanada.ca");
	  		olRef= olRef.replace(fullDomain,domain + ".postescanada.ca");
			break;
		default:
			olLoc= olLoc.replace(fullDomain,domain + ".canadapost.ca");
	  		olRef= olRef.replace(fullDomain,domain + ".canadapost.ca");
			break;
	  }
	  
	  _lG='';
      _sp='%3A\\/\\/';
      _rp='%3A//'+_lG;

	  O_LC(olLoc,olRef);	//Open the OpinionLab Comment Card
}
/*-- OpinionLab - End ----------------------------------------------------------------------------------------------------*/


