var myDest="";

document.onload= new function () {
_gaUtil = new gaUtils('hcf.com.au');

// Uncomment the following line to turn on debug messages
// _gaUtil.SetDebug(true);

}

// no user servicable parts below this line
///// gaUtils object
// (c) 2009 Panalysis Pty Ltd all rights reserved www.panalysis.com
// licensed to HCF
// for alterations and support regarding this script please contact support@panalysis.com

function gaUtils(d)
{
	// private property declarations
	var bDomain ="";
	var bDebug = false;
	var bTrackExternalDomains = false;
	var bTrackLinkID = false;
	
	if(d != "")
		bDomain = d;
		
	// Public method declarations
	
	// SetBaseDomain (string domain) - sets the base domain from which to identify external links
	this.SetBaseDomain = _gaSetBaseDomain;
	
	// SetDebug(bool) - turn on alerts when sending data to Google Analytics
	this.SetDebug = _gaSetDebug;
	
	// TrackLink(string path, bool opens in same window ) - tracks the click on the link and records this as a page in Google Analytics
	this.TrackLink = _gaTrackThis; 
	
	// initialisation
	var cDomain = document.location.hostname.toLowerCase();
	// split the domain into its component parts
	var dp = cDomain.split(".");
	if(bDomain != "")
		bDomain = dp[dp.length-2] + "." + dp[dp.length-1]; // set to the current domain including country code
	
	_gaInitLinks();
	_gaTestMember();
	_gaCheckFirstCampaign();
	
	// if there is search in the URL then test for the internal search engine query
	if(document.location.pathname.indexOf('search')>0)
		_gaTestSearch();
	
	// _gaSetDebug(bool) - turns on or off debug mode
	function _gaSetDebug(val)
	{
		bDebug = val;
	}
	
	// _gaSetBaseDomain(string domain) - sets the base domain from which to determine whether a link is external or not
	function _gaSetBaseDomain(val)
	{
		bDomain = val;
		_gaInitLinks();
	}
	
	// _gaTrim - trims leading and trailing whitespace
	function _gATrim(val){ return val.replace(/^\s+|\s+$/g, '') ; }
	
	// _gaInitLinks - scan the DOM and associate an onclick handler for each link based on its content
	function _gaInitLinks()
	{
		var dTypes = new Array(".pdf",".doc",".docx",".xls",".xlsx",".exe",".zip",".rtf");

		var mL = document.getElementsByTagName("a");
		var myRegexp = /^http[s]?:\/\/(.*)/;
		for(var i=0;i<mL.length;i++)
		{
			var myRef = mL[i].href.toLowerCase();
			if(myRef == undefined)
				continue;
				
			var myMatch = myRegexp.exec(myRef);
			var lchar = myRef.substring(myRef.length-1);
			if((lchar=="#" && mL[i].onclick != "") || (myMatch != undefined && myMatch.length>1 && bDomain != undefined && myRef.indexOf(bDomain) < 0))
			{
				_gaAddEvent(mL[i],"click",function () { _gaTrackThis(this.href,false); });
			}
			else
			{
				for(x=0;x<dTypes.length;x++)
				{
					if(myRef.indexOf(dTypes[x])>-1 && self.pageTracker)
					{
						if(mL[i].target != "")
							_gaAddEvent(mL[i],"click",function () { _gaTrackThis(this.href,false); });
						else
							_gaAddEvent(mL[i],"click",function () { _gaTrackThis(this.href,false); return false;});
					}
				}
			}
		}
	}

	// _gaTrackThis(string path,bool has target attribute) - records clicks on pdf and other links. 
	// If the target is the same window, then dwell for 1/2 second before redirecting user.
	function _gaTrackThis(t,r)
	{
		myDest = t;
	
		var myMatch = myDest.match(/^http[s]?:\/\/(.*)/);
		if(myMatch && myDest.indexOf(bDomain) < 0)
		{
			t = "/outbound/" + myMatch[1];
		}
		else
			t = t.replace(/http[s]?:\/\/[^\/]*/,"");
	
		if(self.pageTracker)
		{
			if(bDebug==true)
				alert(t);
			pageTracker._trackPageview(t);
		}
		
		
		if(r==true)
		{
			setTimeout("document.location.href=myDest;",500); // delay for 1/2 second
			return false;
		}
		else
			return true;
	}

	// _getHashParam - retrieves the a query parameter from the document hash
	function _gaGetHashParam(strParam){
		var _pstr = document.location.hash.substring(1);
		var _uparams = _pstr.split("&");
		for(var i = 0; i < _uparams.length; i++){
			var np = _uparams[i].split("=");
			if(_gATrim(np[0].toLowerCase()) == strParam.toLowerCase())
			{
				return _gATrim(np[1]);
				break;
			}
		}
		return "";
	}
	
	// _gaGetParam - retrieves the a query parameter from the document URL
	function _gaGetParam(strParam){
		var _pstr = document.location.search.substring(1);
		var _uparams = _pstr.split("&");
		for(i = 0; i < _uparams.length; i++){
			var np = _uparams[i].split("=");
			if(_gATrim(np[0].toLowerCase()) == strParam.toLowerCase())
				return _gATrim(np[1]);
		}
		return "";
	}
	
	// _gaSetCookie(string Name, string Value, String Expire Days, String Domain) - sets a cookie
	function _gaSetCookie(cookieName,cookieValue,nDays,strDomain) {
		var today = new Date();
		var expire = new Date();
		var pdm = "";
		if (nDays==null || nDays==0) nDays=1;
		expire.setTime(today.getTime() + 3600000*24*nDays);
		if (strDomain && strDomain!="") 
			pdm=" domain="+strDomain+";"; 
		else if(_udn)
			pdm=" domain="+_udn+";";
		document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString() + "; path=/;" + pdm;
	}

	// _gaGetCookie - retrieves a cookie from a name
	function _gaGetCookie(strParam){
		var _ucookies = document.cookie.split(";");
		for(i = 0; i < _ucookies.length; i++){
			var np = _ucookies[i].split("=");
			if(_gATrim(np[0].toLowerCase()) == strParam.toLowerCase())
			{
				var val ="";
				for(i=1;i<np.length;i++)
				{
					if(i>1)
						val += "=";
					
					val += np[i];
				}
				return _gATrim(val);
			}
		}
		return "";
	}
	
	// _gaTestMember - checks whether there is a member_id variable in the URL that != guest or whether the variable ismember is set as a Javascript variable
	function _gaTestMember()
	{
		var utmv = _gaGetCookie('__utmv');
		if(utmv != "" && utmv.indexOf('member')>0)
			return true;
		if(utmv != "" && utmv.indexOf('provider')>0)
			return true;
		
		var mq = _gaGetParam('member_id');
		var im =0;
		var ip=0;
		try
		{
			im = (ismember==1)?1:0;
		}
		catch(err) {}
		
		try
		{
			ip = (isprovider==1)?1:0;
		}
		catch(err) {}
		
		if((mq != "" && mq.toLowerCase().indexOf('guest')<0) || im==1)
		{
			try
			{
				if(typeof(pageTracker) != "undefined")
				{
					pageTracker._setCustomVar(1,"member","yes",1);
				}
			}
			catch(err) {}
		}
		else if(ip==1)
		{
			try
			{
				if(typeof(pageTracker) != "undefined")
				{
					pageTracker._setCustomVar(1,"provider","yes",1);
				}
			}
			catch(err) {}
		}
	}
	
	function _gaTestSearch()
	{
		
		var mql = document.getElementById('ctl00_CPH_hlPagerNextTop');
		var msq = _gaGetParam("searchstring");
		
		if(mql && msq =="")
		{
			
			try
			{
				var apos = mql.href.indexOf('searchstring=');
				var query = mql.href.substr(apos+13);
				if(self.pageTracker)
				{
					pageTracker._trackPageview(document.location.pathname + "?searchstring=" + query);
				}
			}
			catch (err) {}
		}
	}
	
	// set first campaign
	function _gaCheckFirstCampaign(cname)
	{
		var utmv = _gaGetCookie('__utmv');
		if(utmv != "" && utmv.indexOf('1stcmp')>0)
			return true;
		var utmc = _gaGetParam('utm_campaign');
		if(utmc != "")
		{
			try
			{
				pageTracker._setCustomVar(2,"1stcmp",utmc,1);
			}
			catch(err) {}
		}
	}
	
	// _gaAddEvent - adds an event to the DOM. Correctly handles the pointer for "this"
	function _gaAddEvent( obj, type, fn ) {
		if ( obj.attachEvent ) {
			obj['e'+type+fn] = fn;
			obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
			obj.attachEvent( 'on'+type, obj[type+fn] );
		} else
			obj.addEventListener( type, fn, false );
	}
	
	// _gaRemoveEvent - removes an event from the DOM
	function _gaRemoveEvent( obj, type, fn ) {
		if ( obj.detachEvent ) {
			obj.detachEvent( 'on'+type, obj[type+fn] );
			obj[type+fn] = null;
		} else
			obj.removeEventListener( type, fn, false );
	}
}