// gautils.js - Developed by Panalysis Pty Ltd www.panalysis.com
// version 1.01 16/04/2009

var _gaDTypes = new Array(".pdf",".doc",".xls",".zip",".rtf");
var _gaDelay = 500; // the delay to capture clicks to external links. Set to 0 to prevent any delay
var _gaBaseURL = "xylo.com.au";
var _gaRedirectDGM = true;
var _gaCustomerCookieName = "XyloT"; // set this to the customer login cookie
var _gaBaseDir = "/tracking/"; // the directory to record any page views 

// general tracking utility. Used to track email links, PDF links, other sites, etc.
function _gaTrackThis(u,t,o)
{
	_gaMyDest = u;
	if(t=="o")
	{
		u = u.replace(/http[s]?:\/\//,"");
		u = _gaBaseDir + "outbound/" + u;
	}
	else if(t=="d")
	{
		var re1 = new RegExp("http[s]?://[^\/]*?(\/.*)");
		var m = re1.exec(u);
		if(m)
			u = m[1];
	}
	else if (t=="e")
	{
		var re1 = new RegExp("mailto:(.*)");
		var m = re1.exec(u);
		if(m)
			u = _gaBaseDir + "email/" + m[1];
	}
	else if (o != "")
	{
		u = o; // record the overriding URL
	}

	if(self.urchinTracker)
	{
		urchinTracker(u);
	}
	else if(self.pageTracker)
	{
		pageTracker._trackPageview(u);
	}

	if(t =="e" || _gaDelay==0)
	{
		return true;
	}
	else
	{
		setTimeout("_gaSendToDest()",_gaDelay); // delay for 1/2 second
		return false;
	}
	
}

// redirect the visitor to their final destination
function _gaSendToDest()
{
	document.location.href=_gaMyDest;
}

// set up the link tracking by looping through the document and then examining each link
function _gaTrackLinks()
{
	var hashVal ="";

	if(! self.pageTracker && ! self.urchinTracker)
		return true;

	var mL = document.getElementsByTagName("a");
	
	for(var i=0;i<mL.length;i++)
	{
		var bTDL = true;
		try { 
			if (mL[i].hash)
			{
				var th = mL[i].hash.substring(1);
				if(th>0 && th<1000 && document.location.pathname.indexOf('faq')>0)
				{
					mL[i].onclick = function () { _gaTrackThis('','',_gaBaseDir + 'faqs/' + this.text); return false;};
				}
			}
		}
		catch(err){}
		var re1 = new RegExp("http[s]?:\/\/([^\/]*?)(\/.*)");
		var re2 = new RegExp("mailto:(.*)");
		var m = re1.exec(mL[i].href.toLowerCase());
		var m2 = re2.exec(mL[i].href.toLowerCase());

		if(m)
		{
			if(m[1].indexOf('online.xylo.com.au')>=0)
				mL[i].onclick = function () { _gaTrackThis(this.href,'',_gaBaseDir + 'login-button-click'); return false;};
			else if(m[1].indexOf(_gaBaseURL)<0)
			{
				mL[i].onclick = function () { _gaTrackThis(this.href,'o',''); return false;};
				bTDL = false;
			}
			
		}
		else if (m2)
		{
			mL[i].onclick = function () { _gaTrackThis(this.href,'e','');  return true;};
			bTDL = false;
		}

		if(bTDL == true)
		{
			for(x=0;x<_gaDTypes.length;x++)
			{
				if(mL[i].href.toLowerCase().indexOf(_gaDTypes[x])>-1)
				{
					mL[i].onclick = function () { _gaTrackThis(this.href,'d',''); return false;};
				}
			}
		}
	}
}

// extract any hash parameters by name. 
function _gaGetHashParam(strParam){

	var _pstr = document.location.hash.substring(1);
	var _uparams = _pstr.split("&");
	for(i = 0; i < _uparams.length; i++){
		var np = _uparams[i].split("=");
		if(trim(np[0].toLowerCase()) == strParam.toLowerCase())
			return trim(np[1]);
	}
	return "";
}

// set cookies
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(self._udn)
		pdm=" domain="+_udn+";";
	else
		pdm = " domain=." + _gaBaseURL;
	document.cookie = cookieName+"="+cookieValue + ";expires="+expire.toGMTString() + "; path=/;" + pdm;
}

// get a cookie by name
function _gaGetCookie(strParam){
	if(! document.cookie)
		return;
		
	var _ucookies = document.cookie.split(";");
	var re = new RegExp("([^=]*)=(.*)");
	for(i = 0; i < _ucookies.length; i++){
		
		var np = re.exec(_ucookies[i]);
		
		if(trim(np[1].toLowerCase()) == strParam.toLowerCase())
			return trim(np[2]);
	}
	return "";
}

// track banner impressions
function _gaTrackBanner(bName,gName)
{
	var bPath=_gaBaseDir + 'banners/';
	_gaRecordGoal(bPath,bName,gName);
	_gaSetCookie('cbanner',bName);
}

// set a goal action where the visitor has a banner in their cbanner cookie
function setBannerGoal(gName)
{
	if (! gName)
		return;
	if(_gaGetCookie('cbanner'))
		_gaTrackBanner(_gaGetCookie('cbanner'),gName);
}

// _gaRecordGoal - records a URL into Google Analytics for tracking goals such as 
// form completion events. The URL will be appended with the name of the 
// banner, form, calculator or other function used by the visitor
function _gaRecordGoal(uPath,uName,uGName)
{
	if(! uName || ! uPath)
		return;
	try{
		
		if(uGName)
		{
			if(self.urchinTracker)
				urchinTracker(uPath + uName + " - " + uGName);
			else
				pageTracker._trackPageview(uPath + uName + " - " + uGName);
		}
		else
		{
			if(self.urchinTracker)
				urchinTracker(uPath);
			else
				pageTracker._trackPageview(uPath + uName);
		}
		
	}catch(e){}
}

function trim(val){ return val.replace(/^\s+|\s+$/g, '') ; }

// ************************** Start Main ********************************/
// If the query string MID=google exists then redirect to the same URL 
// but with the Google Analytics tracking code in place
// If the campaigns can be tagged by DGM then remove this section or set
// _gaRedirectDGM to false

if(document.location.search.indexOf('MID=google')>=0 && _gaRedirectDGM==true)
{
	document.location.replace(document.location.pathname + "?utm_source=google&utm+medium=cpc&utm_campaign=adwords");
}



// Visitor Segmentation
// if the user has logged in then look for the expected cookie and 
// then set the user variable to 'active customer'

if(document.cookie.indexOf(_gaCustomerCookieName) >=0)
{
	// to avoid tracking this transaction as a page view we
	// create a new tracker and set this to point to null
	var tmpTracker = _gat._getTracker("UA-0-0");
	tmpTracker._setDomainName('xylo.com.au');
	
	// then set the user defined variable
	tmpTracker._setVar('active customer');
}


function ga()
{
	// initialise the link tracking
	_gaTrackLinks();
	// Banner Tracking
	var strMb = _gaGetHashParam("frombanner");
	if(strMb !="")
	{
		_gaTrackBanner(strMb);
	}
	
}

if(window.attachEvent)
{
	window.attachEvent('onload',ga)
}
else if(window.addEventListener)
{
	window.addEventListener('load',ga,false);
}

