/****************************************************************************************
VERSION 	1.1
AUTHOR		Doug Scamahorn

DATE       	NAME           		DESCRIPTON
02/23/2007 	Doug Scamahorn  	Initial creation.
12/04/2008	Keith Willoughby	Added script to remove contact link from sitemap.
****************************************************************************************/
/* GLOBAL UTILITY FUNCTIONS ---------------------------------------------------------------------------- */
//Make sure the browser understands the DOM methods
function fncValidateDomSupport(){
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
}

//Get the current page's domain. This is useful for comparisons b/w the parent page's domain and the domain of the child file being linked to from the parent
function fncGetPageDomain(){ 
	var vCurrentPageAddress = window.location.href;
	//alert(vCurrentPageAddress);
	//Get the number of characters from the beginging of the URI to the end of the scheme://
	var vSchemeIndex = vCurrentPageAddress.indexOf("//")+2; //2 is the number of characters in the //
	//Get the string after the //
	var vDomainPlusPathString = vCurrentPageAddress.substring(vSchemeIndex);
	//Get the ending point of the protocol by looking for the next forward slash
	var vIndexOfNextForwardSlash = vDomainPlusPathString.indexOf("/");
	//alert(vIndexOfNextForwardSlash);
	//Get the protocol by getting the string from the 0 index character to the next forward slash
	var vDomainString = vDomainPlusPathString.substring(0,vIndexOfNextForwardSlash)
	//alert(vDomainString);
	return vDomainString;
}
//Get the path, file name, and file type of the link supplied, scheme://host.domain:port/path/filename.type?field1=value1&field2=value2
function fncSliceLink(pLink){ 
	//Get the link's href
	var vLinkAddress = pLink;
	//alert(vLinkAddress);
	//See if the link contains a query string
	if (vLinkAddress.indexOf("?")>1) {
		//alert("query string");
		//If the link contains a query string, store the query sting for later use
		var vQueryString = vLinkAddress.substring(vLinkAddress.indexOf("?"));
		//alert(vQueryString);
		//If the link contains a query string, then remove the query string for further processing
		vLinkAddress=vLinkAddress.substring(0,vLinkAddress.indexOf("?"));
		//alert(vCurrentPageAddress);
	} else {
		vQueryString="";
	}
	//Get the number of characters from the beginging of the URI to the end of the scheme://
	var vSchemeIndex = vLinkAddress.indexOf("//")+2; //2 is the number of characters in the //
	//alert (vProtocolIndex);
	//Get the string after the //
	var vDomianPlusPathString = vLinkAddress.substring(vSchemeIndex);
	//Get the ending point of the protocol by looking for the next forward slash
	var vIndexOfNextForwardSlash = vDomianPlusPathString.indexOf("/");
	//alert(vIndexOfNextForwardSlash);
	//Get the string after the forward slash which is the path
	var vPathString = vDomianPlusPathString.substring(vIndexOfNextForwardSlash);
	//alert(vPathString);
	//Get the protocol by getting the string from the 0 index character to the next forward slash
	var vDomainString = vDomianPlusPathString.substring(0,vIndexOfNextForwardSlash)
	//alert(vDomainString);
	//Find the last occurance of a forward slash
	var vIndexOfLastForwardSlash = vPathString.lastIndexOf("/")+1;//1 is the number of characters in the /
	//alert(vIndexOfLastForwardSlash);
	//Get the filename and type string
	var vFileNameAndTypeString = vPathString.substring(vIndexOfLastForwardSlash);
	//alert(vFileNameAndTypeString);
	//Find the last dot in the filename and type string
	var vIndexOfLastDot = vFileNameAndTypeString.lastIndexOf(".");//1 is the number of characters in the .
	//alert(vIndexOfLastDot);
	//Get the file name
	var vFileNameString = vFileNameAndTypeString.substring(0,vIndexOfLastDot);
	//alert(vFileNameString);
	//Get the file type
	var vFileTypeString = vFileNameAndTypeString.substring(vIndexOfLastDot);
	//alert(vFileTypeString);
	//Return an array of the values from the function for use in other scripts
	var vReturnValuesArray = new Array(vDomainString,vPathString,vFileNameString,vFileTypeString,vQueryString);
	return vReturnValuesArray;
	
}
/* AUTOTAG FUNCTIONS ---------------------------------------------------------------------------- */
//Find links and automatically add the event to trigger the WT function on click
function fncAutoTag()  {
	//Make sure the browser understands the DOM methods
	fncValidateDomSupport();
	//Get the content area container
	if (!document.getElementsByTagName("body")) return false;
	var vContentArea = document.getElementsByTagName("body");
	//Create an array with all the links in the content area
	var vAllContentLinks = vContentArea[0].getElementsByTagName("a");
	//Loop through the links
	for (var countLinks=0;countLinks<vAllContentLinks.length;countLinks++) {
		//Add an event listener that calls the webtrends tracking function on click
		vAllContentLinks[countLinks].onclick = function () {
			fncAutoTagEvent(this,"contentArea");
			var vLinkUrl = this.toString();
			var vFileDomain=fncSliceLink(vLinkUrl)[0];
		}
	}
}
//Add the onclick event
//Remove Contact Us Link from Sitemap
function fncRemoveContactUs()  {
	var contactLink = document.getElementsByTagName("a");
	for (count = 1; count < contactLink.length; count++) {
		var linkHref = contactLink[count].getAttribute("href");
		if (linkHref == "http://canada.realizeband.com/dtcf/components/contact" || linkHref == "/dtcf/components/contact") {
			var container = contactLink[count].parentNode;
			container.removeChild(contactLink[count]);
		}
	}
}		
function fncAutoTagEvent(pLinkUrl, pLinkLocation) {
	//Convert the link href value to a string
	var vLinkUrl = pLinkUrl.toString();
	//alert(vLinkUrl);
	//Call the fncSliceLink function to populate the following variables
	var vFileDomain=fncSliceLink(vLinkUrl)[0];
	//alert(vFileDomain);
	var vFilePath=fncSliceLink(vLinkUrl)[1];
	var vFileName=fncSliceLink(vLinkUrl)[2];
	var vFileType=fncSliceLink(vLinkUrl)[3];
	var vFileQueryString=fncSliceLink(vLinkUrl)[4];
	//Test the domain to see if it is an external link
	if (vFileDomain==fncGetPageDomain()) {//Use the fncGetPageDomain function to return the current domain
		//If the domain is realizeband.com then call the WT tracking code
		//dcsMultiTrack('DCS.dcsuri', '<path of document & document file name>', 'WT.ti', '<Document's Title or filename without extension>', 'DCSext.EX', '<Document's file type>');
		dcsMultiTrack('DCS.dcsuri', vFilePath, 'WT.ti', vFileName, 'DCSext.EX', vFileType);
		//alert("INTERNAL: The file path is: " + vFilePath + ", the file name is: " + vFileName + ", and the file type is: " + vFileType);
	} 
	else { //The link is to an external site
		//Call the WT tracking code.
		//dcsMultiTrack('DCS.dcssip', '<domain of the web site>', 'DCS.dcsuri', '<path and/or name of the web page>', 'WT.ti', '<Page Title or Site Title>');
		dcsMultiTrack('DCS.dcsuri',vFileDomain+vFilePath+vFileQueryString,'WT.ti',vFileName);				
		//alert("EXTERNAL: The domain is: "+vFileDomain+", the file path is: "+vFilePath+vFileQueryString+", and the file name is: "+vFileName);
	}
}

/* LOAD FUNCTIONS ---------------------------------------------------------------------------- */
//Call the functions on page load
addLoadEvent(fncAutoTag);
addLoadEvent(fncRemoveContactUs);