var URLTrackingPrefix = "/c/c.jsp?s=" + siteID + "&a=1&t=1";

//this function is to attach a call back javascript function
//onclick for all the links in a given page when there is not an
//already defined onclick event.
function attach_link_launcher(element) {

	if (element.tagName == "A") {
		//dont attach link_launcher if href value has javascript alert
		//  Added by offshore   Start
		var ele_attributes= element.attributes;
		var attach_launcher=true;

		if(ele_attributes.getNamedItem('href').value.indexOf('javascript:')>=0){
			attach_launcher=false;
		}

		//  Added by offshore   End
		if ((element.onclick == null) && attach_launcher){
			 element.onclick = link_launcher;
		}
	} else {
		var children = element.children;
		if (children != null) {
			var i = 0;
			for (i=0; i<children.length; i++) {
				attach_link_launcher(element.children[i]);
			}
		}
	}
}

//this function is called when a link is clicked.
//for now, we just need to track the click.
function link_launcher(path) {
	var destination = null;

	if (path != null) {
			destination = path;
	} else {
		var element = event.srcElement;
		while (element.tagName != "A") {
			element = element.parentElement;
		}
		destination = element.href;
	}
	trackClick(destination);
}

//this tracks the click of a link in webtrends
//with the same format as a source page with an
//extra parameter 'f' as the click URI and p=1.
//tracks the featureboxes with hsbc_ad query parameter
//or the ones with http:
function trackClick(path)
{
	var clickURL = path;
/*	if(isOffsite(path)) {
		clickURL = path;
	}
	else */
	if(isAnAd(path)) {
		clickURL = getAdValue(path);
	}

	if(clickURL)
		trackwithImage(	URLTrackingPrefix + "&p=1&d=" + getSourceTrackPageURI() + "&f=" + clickURL);
}

//Tries to load an image with the URL given, by which
// there will be an entry in the webserver logs
//which would be processed by webtrends
function trackwithImage( trackSourceURL)
{
	var trackImg = new Image();
	trackImg.src = trackSourceURL;
}

//Tracks the page which is loaded in to the browser
//either with the help of an image tag
//or content file name.
function trackPage() {
	trackwithImage( URLTrackingPrefix + "&p=0&d=" + getSourceTrackPageURI());
}

//returns the tracking URI name based off
// the tracking image or the content file name.
function getSourceTrackPageURI() {
	var trackPageName = captureTrackingName();
	var page;
	if(trackPageName) {
    		page = escape(trackPageName);
	}
	else if (pagePath) {
			page = escape(pagePath);
	}

	if(page)
		return page;
	else
		return "unknown";
}

//capture the name of the page by finding the image 
//with a 'src' value containing 'trackingimage'.
function captureTrackingName() {

	var images = document.images;
	var trackName = "";
	
	for(i=0; i<images.length; i++) {
		src = images[i].src;
		imgName = images[i].name;
		if(src.indexOf("trackingimage") > 0) {			
			trackName = imgName;
		}
	}

	return trackName;
}

//determines whether a particular link is offsite.
//if it contains 'http:', it is assumed to be an
//offsite URL
function isOffsite(path)
{
	return IsHttp(path);
}

//Most of the feature boxes coming from teamsite templates
//would have a parameter called 'HSBC_AD' embedded in
//href value of the link. By this parameter, one can
//track which spiff/feature box was clicked.
function isAnAd(path)
{
	var pos=path.toUpperCase().indexOf("HSBC_AD=");
	return (pos!=-1)?true:false;
}

function getAdValue(path) {
	var pos=path.toUpperCase().indexOf("HSBC_AD=");
	var adValue;
	if (pos!=-1){
			var start=pos+8;
			var end=path.indexOf("&",start);
			if (end==-1){
				end=path.length;
			}
		adValue=path.substring(start,end);
	}
	return adValue;
}

function IsHttp(path){
	var retValue = (path&&(path.indexOf("http")!=-1))?true:false;
	return retValue;
}
