/**
 * @title Statistics customization for APC
 * @author DENIS ROUSSEAU
 * @version 1.4.1.2
 */
/*****************************************************************************
 * Internal functions
 *****************************************************************************/
function APC_getCookie(cookieName) 
{
	var r = new RegExp('[; ]'+cookieName+'=([^\\s;]*)');
	var m = (' '+document.cookie).match(r);
	if (cookieName && m) return unescape(m[1]);
	return '';
}
function APC_getCountry()
{
	var c = APC_getCookie("APCSITECODE");
	return (c && c.length > 0)? c : "ww" ;
}
function APC_getSegment()
{
	var c = APC_getCookie("SEGMENTID");
	return (c && c.length > 0)? c : "0" ;
}
function APC_getKeycode()
{
	var c = getPageParam('keycode') ;
	if (c == null || c.length == 0)
	{
		c = APC_getCookie("TSK");
	}
	return (c && c.length > 0)? c : "" ;
}

function APC_getPathSuffix(pu)
{
	var s = ""
	/* About us */
	if (pu.indexOf("/corporate/customer_refs") >= 0)
	{
		var p = getPageParam("ref_type") ;
		if (p && p.length > 0)
		{
			s += "/" + p ;
		}
		else
		{
			p = getPageParam("action") ;
			if (p && p.length > 0)
			{
				s += "/" + p ;
			}			
		}
	}
	else if (pu.indexOf("/company/recognition-and-awards") >= 0)
	{
		var p = getPageParam("awards_display") ;
		if (p && p.length > 0)
		{
			s += "_" + p ;
		}
	}
	
	/* Personal page */
	else if (pu == "/personalpage")
	{
		s += "/" + document.title ;
	}
	/* Personal page event search*/
	else if (pu.indexOf("/personalpage/registration/events/search") >= 0)
	{
		if(document.forms['eventsearch'] != null) {
			var f = document.forms['eventsearch'].elements['EttCode'];
		}
		if (f && f.selectedIndex >= 0)
		{
			s += "-" + f.options[f.selectedIndex].text ;
		}
	}
	
	/* Press center */
	else if (pu.indexOf("/press_center") >= 0)
	{
		var p = getPageParam("articleType") ;
		if (p && p.length > 0)
		{
			s += "/" + p ;
		}
	}
	/* Product documentation */
	else if (pu.indexOf("/prod_docs") >= 0)
	{
		var p = getPageParam("DocType") ;
		if (p && p.length > 0)
		{
			s += "/" + p ;
			p = getPageParam("Query_Type") ;
			if (p && p.length > 0)
			{
				s += "_" + p ;
			}
			p = getPageParam("Value") ;
			if (p && p.length > 0 && p != 'none')
			{
				s += "_" + p ;
			}
			p = getPageParam("sku") ;
			if (p && p.length > 0)
			{
				s += "_" + p ;
			}
		}
	}
	/* Promo */
	else if (pu.indexOf("/isxglobal_registration") >= 0)
	{
		var p = getPageParam("tsk") ;
		if (p && p.length > 0)
		{
			s += "/" + p ;
		}
	}
	
	/* Product category & subcategory */
	else if (pu.indexOf("/products/category") >= 0)
	{
		var p = getPageParam("id") ;
		if (p && p.length > 0)
		{
			s += "/C" + p ;
		}
		p = getPageParam("subid") ;
		if (p && p.length > 0)
		{
			s += "/SC" + p ;
		}
	}
	/* Product family */
	else if (pu.indexOf("/products/family") >= 0)
	{
		var p = getPageParam("id") ;
		if (p && p.length > 0)
		{
			s += "/" + p + "/";
		}
	}
	/* Product offer */
	else if (pu.indexOf("/products/infrastruxure") >= 0)
	{
		var p = getPageParam("section") ;
		if (p && p.length > 0)
		{
			s += "/" + p ;
			p = getPageParam("id") ;
			if (p && p.length > 0)
			{
				s += "/" + p ;
			}
		}
	}
	/* Product reference */
	else if (pu.indexOf("/resource/include/techspec_index") >= 0)
	{
		var p = getPageParam("base_sku") ;
		if (p && p.length > 0)
		{
			s += "/" + p ;
		}
	}
	
	/* Services */
	else if (pu.indexOf('/services') >= 0 || pu.indexOf("/support/service/serviceprogramsfor") >= 0)
	{
		var p = getPageParam("servicetype") ;
		if (p && p.length > 0)
		{
			s += "/" + p ;
		}
	}
	
	/* Selector */
	else if (pu.indexOf("/tools/estimator") >= 0)
	{
		var p = getPageParam("solutionType") ;
		if (p && p.length > 0)
		{
			s += "/" + p ;
		}
	}
	else if (pu == "/tools/locator")
	{
		var p = getPageLastTag('H1') ;
		if (p && p.length > 0)
		{
			s += "/" + p.replace('/',' ') ;
		}
	}
	/* Handbook */
	else if (pu.indexOf("/tools/salestools/handbook") >= 0)
	{
		var p = getPageParam("cat") ;
		if (p && p.length > 0)
		{
			s += "_cat:" + p ;
		}
		p = getPageParam("wp") ;
		if (p && p.length > 0)
		{
			s += "_wp:" + p ;
		}
		p = getPageParam("subCat") ;
		if (p && p.length > 0)
		{
			s += "_subCat:" + p ;
		}
		p = getPageParam("navCatNum") ;
		if (p && p.length > 0)
		{
			s += "_navCatNum:" + p ;
		}
		p = getPageParam("navSubCatNum") ;
		if (p && p.length > 0)
		{
			s += "_navSubCatNum:" + p ;
		}
	}
	/* Solutions */
	else if (pu == "/solutions/display")
	{
		var p = getPageParam("id") ;
		if (p && p.length > 0)
		{
			s += "/" + p ;
		}
	}
	else if (pu.indexOf("/software/index.cfm") >= 0) 
	{
		var p = getPageParam("ID");
		if (p && p.length > 0) 
		{
			s += "_ID:" + p;
		}
		p = getPageParam("cid");
		if (p && p.length > 0) 
		{
			s += "_CID:" + p;
		}
	}
	else if (pu.indexOf("/tools/registration/events") >= 0) 
	{
		var p = getPageParam("EvtCode");
		if (p && p.length > 0) 
		{
			s += "_" + p;
		}
	}
	/* Keycode */
	else if (pu.indexOf("/tools/registration/promo/registercustomer") >= 0) 
	{
		var kc = APC_getKeycode();
		if (kc.length > 0) 
		{
			s += "/" + kc + "_Internal";
		}
	}
	
	/* UPS Selector type */
	else if (pu.indexOf("/tools/ups_selector") >= 0) 
	{
		var af = document.getElementsByTagName('form');
		var fdevice = null;
		for (var i = 0; i < af.length; i++) 
		{
			if (af[i].name == "buildForm") 
			{
				s += "/" + "basic";
				fdevice = af[i];
				break;
			}
			else if (af[i].name == "userPrefs") 
			{
				s += "/" + "express";
				break;
			}
			else if (af[i].name == "addWorkstation") 
			{
				s += "/" + "advanced";
				break;
			}
			else if (af[i].name == "deviceMFG") 
			{
				s += "/" + "deviceType";
				fdevice = af[i];
				break;
			}
		}
		if (fdevice != null) 
		{
			var fi = fdevice.elements['device_type'];
			if (fi && fi != undefined) 
			{
				s += "/" + fi.value;
			}
			else 
			{
				var p = getPageParam("device_type");
				if (p && p.length > 0) 
				{
					s += "/" + p;
				}
			}
		}
	}
	
	return s ;
}

/*****************************************************************************
 * Statistic Customization functions
 *****************************************************************************/
/******************************************************************************
 * Compare Path (case insensitive)
 */
function isCustomEqualPath(p1, p2)
{
	return (p1.toLowerCase() == p2.toLowerCase()) ;	
}
/******************************************************************************
 * Path completion
 */
function getCustomPath(sp, isGlobal)
{
	var msp = sp ;
	if (isGlobal)
	{
		/* if ended by /index, remove it  */
		var p = sp.lastIndexOf("/index") ;
		if (p == sp.lastIndexOf("/"))
		{
			sp = sp.substr(0, p) ;
		}
		/* Add prefix and suffix */
		msp = ((APC_GlobalCountryPrefix)? APC_getCountry().toUpperCase() : '') + sp + APC_getPathSuffix(sp.toLowerCase());
	}
	else if (APC_GlobalCountryPrefix)
	{
		msp = sp.substr(sp.indexOf('/'));
	}
	return msp ;
}

function setCustomXt(pu)
{
	xt_multc = "&x1=" + APC_getSegment();
	if (window.xtparam != null && window.xtparam.indexOf("&x1=") < 0) 
	{
		window.xtparam += xt_multc;
	}
	else 
	{
		window.xtparam = xt_multc;
	};
	if (! APC_GlobalCountryPrefix)
	{
		xtpage = APC_getCountry().toUpperCase() + '::' + xtpage ;
	}
	// search engine tracking
	if (xtn2 == 12 && xtpage=='results')
	{
		xt_mtcl = getPageParam('qt') ;	
		if (xt_mtcl != undefined)
		{
			var np = getPageParam('page') ;
			xt_npg = (document.getElementById('noresults'))? 0 : (np? np : 1) ;
			window.xtparam+="&mc="+xt_mtcl+"&np="+xt_npg;		
		}
	}
}

function setCustomGA(gaObj)
{
	 if (STAT_Method.Async)
	 {
		gaObj.push(['_setCustomVar', 1, "Region", "", 2]);
		gaObj.push(['_setCustomVar', 2, "Country", APC_getCountry(), 2]);
		gaObj.push(['_setCustomVar', 3, "Customer Segment", APC_getSegment(), 2]);
		gaObj.push(['_setCustomVar', 4, "Program Level", "0", 2]);
	 }
	 else
	 {
		gaObj._setCustomVar(1,"Region","",2);
		gaObj._setCustomVar(2,"Country",APC_getCountry(),2);
		gaObj._setCustomVar(3,"Customer Segment",APC_getSegment(),2);  
		gaObj._setCustomVar(4,"Program Level","0",2);  
	 }
}

function doCustomAlert(alertType)
{
	/* If we're really in preview mode */
	var pm = null ;
	if (window.getStatPreviewMap) 
	{
		pm = getStatPreviewMap();
	}
	if (! pm || pm.length > 0)	
	{
		alert("Wrong or missing statistics configuration file");
	}
}

/*****************************************************************************
 * Download Customization functions
 *****************************************************************************/
function setCustomDownloadHook()
{
	/* Hook HitBox call */
	if (typeof _hbRedirect == stat_condition._func) 
	{
		APC_hbRedirect = window._hbRedirect;
		window._hbRedirect = function(type, cat, target, title, e, f, g)
		{
			stat_download.raiseDownloadClickTag(stat_link.getLinkName(target));
			/* mother call */
			APC_hbRedirect(type, cat, target, title, e, f, g);
		};
	}
}

function raiseGoogleSearchClickTag(node)
{
    /* raise click marker */
    xt_med('C', xtn2, 'googleSearch::' + xt_mtcl, 'S');
} 

function doCustomMarker()
{
	// Donwload clicks tracking
	stat_download.activate() ;
	// Google search click tracking
	if (xtn2 == 12 && xtpage == 'results') 
	{
		var pm = [] ;
	    pm.push(['*', 'googleSearch']); 
	    stat_link.addMapEvent(pm, raiseGoogleSearchClickTag) ;
	}
}
