/********************************************************************
 *  T.McD.		08/27/2002		WebTrendsLive Enhancements  
 *	this function is called from a line of javascript code inserted at the bottom of the page
 *	using footer.jsp (would normally have been called in an 'onload' event in the body tag,
 *	but this require changing every page in the site). it scans through all the links on the
 *	page and creates 'onclick' events for only those links which which include the WTLPromo
 *	attribute.
 */
var nPageLoadMS = (new Date()).getTime();
var debug = false;
var oLinkHolder = null;
var strSaveTarget = "";
var myArray = new Array;
var myCount = 0;
var rePattern = new RegExp("( |%20)*wtlp[a-z]+( |%20)*=( |%20)*[ a-z0-9%_\-]+", "i");
var reNewWindow = new RegExp("^(_blank|_new){1}$", "i");
var bFollowLink = true;
var bOpenNewWindow = false;

function setPromoEvents()
{
	for (var i=0; i < document.links.length; i++)
	{
		var oLink = document.links[i];
		if (oLink.search)
		{
			var strCheckQueryString = oLink.search;
			
			if (strCheckQueryString.search(rePattern) > 0)
			{
				if (oLink.onclick==null)
				{
					oLink.onclick = markPromoView;
				}
			}
		}
	}
}

function markPromoView(e)
{
	if (!e) var e = window.event;
	processViewFor(this);
	return false;
}

function logPromoClick(strThisPromo)
{
	logWtlViewForPromo(strThisPromo);
}

function logWtlViewForPromo(strThisPromo)
{
	var strUrl = "Link : " + strThisPromo + " - " + wtl_URL;
	var strTitle = strThisPromo;
	var strContentGroup = "Promotion," + strThisPromo;
	var strCartEvent = "view";
	var strCartName = strThisPromo;
	 
	ORDER = "";
	wtl_Tag6(jspvalTagIDPromo, jspvalSIDPromo, strUrl, strTitle, strContentGroup, strCartEvent, strCartName);
}

function processViewFor(oLink)
{
	// look for a 'target="_BLANK"' or 'target="_BLANK"' attribute on the link.
	// if found, set boolean var to open link in new window instead of current one. 
	bOpenNewWindow = oLink.target.search(reNewWindow)==0 ? true : false;
	
	var strQueryString = "";
	if (oLink.search)
	{
		strQueryString = oLink.search;
	}
	else if (oLink.hash)
	{
		var strHashString = oLink.hash;
		nQMIndex = strHashString.indexOf("?");
		if ( nQMIndex >= 0 )
		{
			strQueryString = strHashString.substring(nQMIndex);
			oLink.hash = strHashString.substring(0, nQMIndex);
		}
	}
	
	var reLeadingSpace = new RegExp("^[ ]+");	// searches for any leading whitespace character
	var reTrailingSpace = new RegExp("[ ]+$");	// searches for any trailing whitespace character

	if (strQueryString.length > 0)
	{
		matchArray = strQueryString.match(rePattern);
		promoArray = matchArray[0].split("=");
		if (promoArray.length > 0)
		{
			strPromoName = promoArray[1];
			if (strPromoName.indexOf("%20")>=0) strPromoName = unescape(strPromoName);
			strPromoName = strPromoName.replace(reLeadingSpace, "").replace(reTrailingSpace, "");
		}
		else
		{
			strPromoName = "UNKNOWN";
		}
		oLink.search = strQueryString.replace(rePattern, "WTLPromo="+escape(strPromoName));
	}
	else
	{
		strPromoName = "NONE";
		oLink.search = "?WTLPromo=NONE";
	}
	
	strSessionImageSrc = "/inc/set-session-promo.jsp?saveWTLPromo=" + escape(strPromoName);
	postImageData(strSessionImageSrc);
	//alert(strSessionImageSrc);
	
	nPromoLinkMS = (new Date()).getTime();
	nViewMS = nPromoLinkMS - nPageLoadMS + jspvalServerMS;
	oLink.search = oLink.search + "&vms=" + nViewMS;
	
	//if (debug) alert(oLink.href);
	if (debug)
	{
		alert("promo name is: " + strPromoName + "\n" +
			"view ms is: " + nViewMS + "\n" +
			"");
	}
	
	
	logWtlViewForPromo(strPromoName);
	
	oLinkHolder = oLink;
	wtl_imgarray[wtl_ptr-1].onload = goToHref;
	wtl_imgarray[wtl_ptr-1].onerror = goToHref;
	
	return false;
}

function goToHref()
{
	if ( bOpenNewWindow )
	{
		// the only way to process 'target="_BLANK"' attributes in JavaScript. 
		window.open(oLinkHolder.href, "newwindow");
	}
	else
	{
		location.href = oLinkHolder.href;
	}
}

function postImageData(strSourceData)
{
	myArray[myCount] = new Image;
	myArray[myCount].src = strSourceData;
	myCount++;
}