/****************************************************************************************
Copyright (c) 2005 Elanco Animal Health.
Elanco Animal Health, Greenfield, Indiana 46140

This software is the confidential and proprietary information of Elanco Animal Health, a division of Eli Lilly and Company. ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with Elanco Animal Health.

VERSION 	1.1
AUTHOR		Doug Scamahorn

DATE       	NAME           	DESCRIPTON
02/23/2007 	Doug Scamahorn  Initial creation.
03/26/2007  Stacy Kagiwada  Modified for Comfortis.com
04/04/2007  Geoff Baker     Cleanup and error handling added.  Made sure current onclick event wasn't lost when
                            adding WebTrends code.  Fixed so that if domain is not present in link, it is treated
                            as a local link.

****************************************************************************************/
/* GLOBAL UTILITY FUNCTIONS ---------------------------------------------------------------------------- */
//Add an event handler (created by Scott Andrew, http://www.dustindiaz.com/top-ten-javascript/)
function fncAddEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}
//Make sure the browser understands the DOM methods
function fncValidateDomSupport(){
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	return true;
}
//Find elements in document of a certain class name (created by Dustin Diaz, http://www.dustindiaz.com/top-ten-javascript/)
function fncGetElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
//Get the domain portion of a URL.  If no protocol is present then returns ""
//eg. http://www.yahoo.com => "www.yahoo.com"
//eg. http://www.yahoo.com/default.htm => "www.yahoo.com"
//eg. /default.htm => ""
//eg. default.htm => ""
function fncParseDomain( pUrl ) {
	//Get the number of characters from the beginging of the URI to the end of the scheme://
	var vSchemeIndex = pUrl.indexOf( "://" );
	if ( vSchemeIndex > -1 ) {
	    //Get the string after the //
	    pUrl = pUrl.substring( vSchemeIndex + 3 );
	    //Get the ending point of the protocol by looking for the next forward slash
	    var vIndexOfNextForwardSlash = pUrl.indexOf( "/" );
	    if ( vIndexOfNextForwardSlash > -1 ) {
	        //Remove file path and query string
	        pUrl = pUrl.substring( 0,vIndexOfNextForwardSlash )
	    }
	    return pUrl;
    }
    return "";
}
//Get the path and file name from the URL.  If nothing was found then returns ""
//e.g. http://www.yahoo.com/default.htm => "/default.htm"
//e.g. http://www.yahoo.com/pages/default.htm => "/pages/default.htm"
//e.g. http://www.yahoo.com/pages/default.aspx?ID=1 =? "/pages/default.aspx"
//e.g. /default.htm => "/default.htm"
//e.g. default.htm => "default.htm"
//e.g. http://www.yahoo.com => ""
function fncParsePathFile(pUrl) {
	var vDomain = fncParseDomain( pUrl );
	if ( vDomain != "" ) {
		pUrl = pUrl.substring( pUrl.indexOf( vDomain ) + vDomain.length );
	}
	var vIndexOfQueryString = pUrl.indexOf( "?" );
	if ( vIndexOfQueryString > -1 ) {
		pUrl = pUrl.substring( 0, vIndexOfQueryString );
	}
	return pUrl;
}
//Get the query string from the URL.  If not present returns ""
//e.g. http://www.yahoo.com/pages/default.aspx?ID=1 =? "ID=1"
//e.g. http://www.yahoo.com/default.htm => ""
//e.g. http://www.yahoo.com => ""
function fncParseQueryString( pUrl ) {
	var vIndexOfQueryString = pUrl.indexOf( "?" );
	if ( vIndexOfQueryString > -1 ) {
		return pUrl.substring( vIndexOfQueryString + 1 );
	}
	return "";
}
//Get the filename of the URL item.  If not found returns ""
//e.g. http://www.yahoo.com/pages/default.htm => "default.htm"
//e.g. http://www.yahoo.com/pages/default.aspx?ID=1 =? "default.aspx"
//e.g. http://www.yahoo.com/default.htm => "default.htm"
//e.g. http://www.yahoo.com => ""
function fncParseFileName( pUrl ) {
	pUrl = fncParsePathFile( pUrl);
	var vIndexOfLastSlash = pUrl.lastIndexOf( "/" );
	if ( vIndexOfLastSlash > -1 ) {
		pUrl = pUrl.substring( vIndexOfLastSlash + 1 );	
	}
	return pUrl;
}
//Get the filename (without extension) of the URL item.  If not found returns ""
//e.g. http://www.yahoo.com/pages/default.htm => "default"
//e.g. http://www.yahoo.com/pages/default.aspx?ID=1 =? "default"
//e.g. http://www.yahoo.com/default.htm => "default"
//e.g. http://www.yahoo.com => ""
function fncParseFileNameWOExt( pUrl ) {
	pUrl = fncParseFileName( pUrl);
	var vIndexOfLastDot = pUrl.lastIndexOf( "." );
	if ( vIndexOfLastDot > -1 ) {
		return pUrl.substring( 0, vIndexOfLastDot );	
	}
	return pUrl;
}
//Get the file extension (lower case) of the URL item.  If not found returns ""
//e.g. http://www.yahoo.com/pages/default.htm => "htm"
//e.g. http://www.yahoo.com/pages/default.aspx?ID=1 =? "aspx"
//e.g. http://www.yahoo.com/default.htm => "htm"
//e.g. http://www.yahoo.com => ""
function fncParseFileExtension( pUrl ) {
	pUrl = fncParseFileName( pUrl);
	var vIndexOfLastDot = pUrl.lastIndexOf( "." );
	if ( vIndexOfLastDot > -1 ) {
		return pUrl.substring( vIndexOfLastDot + 1 ).toLowerCase();	
	}
	return "";
}
//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(){ 
	return fncParseDomain(window.location.href);
}
//Close the active window
function fncCloseWindow() {
    window.close();
}

/* AUTOTAG FUNCTIONS ---------------------------------------------------------------------------- */
//Find links and automatically add the event to trigger the WebTrends function on click
function fncAutoTag() {
	//Make sure the browser understands the DOM methods
	if ( !fncValidateDomSupport() ) return;
	//Get the content area container
	var vContentArea = fncGetElementsByClass("content-section");
	if ((vContentArea != null) && (vContentArea.length > 0)) {
	    //Create an array with all the links in the content area
	    var vAllContentLinks = vContentArea[0].getElementsByTagName("a");
	    if ((vAllContentLinks != null) && (vAllContentLinks.length > 0)) {
	        //Loop through the links
	        for (var countLinks=0;countLinks<vAllContentLinks.length;countLinks++) {
		        //Add an event listener that calls the webtrends tracking function on click
                fncAddEvent(vAllContentLinks[countLinks],'click',function () { fncAutoTagEvent(this,"content-section"); },false);
	        }
	    }
    }
}
//Add the onclick event
function fncAutoTagEvent(pLinkUrl, pLinkLocation) {
	//Convert the link href value to a string
	var vLinkUrl = pLinkUrl.toString();
	//Call the fncParse functions to populate the following variables
    var vFileDomain=fncParseDomain(vLinkUrl);
    var vFilePath=fncParsePathFile(vLinkUrl);
    var vFileName=fncParseFileNameWOExt(vLinkUrl);
    var vFileType=fncParseFileExtension(vLinkUrl);
    var vFileQueryString=fncParseQueryString(vLinkUrl);
	if ((vFilePath != "") && (typeof(dcsMultiTrack) == "function")) {
	    //Test the domain to see if it is an external link
	    if ( (vFileDomain == "") || (vFileDomain.toLowerCase()==fncGetPageDomain().toLowerCase()) ) {//Use the fncGetPageDomain function to return the current domain
		    //If the domain is local server then test to see if the link is in the main content area
		    if (pLinkLocation=="content-section"){
			    //Create an array of the supported file types
			    var vFileTypeArray = new Array("pdf","doc","ppt","xls","zip","jpg");
			    //Loop through the file type array
			    for (var countType=0; countType < vFileTypeArray.length; countType++) {
				    //If the file type matches one of the file types in the array
				    if (vFileType == vFileTypeArray[countType]){
					    //Call the WT tracking code
					    //dcsMultiTrack('DCS.dcsuri', '<path of document & document file name>', 'WT.ti', '<Document's Title or filename without extension>');
					    dcsMultiTrack('DCS.dcsuri', vFilePath, 'WT.ti', vFileName);
				    }
			    }
		    } 
	    } 
	    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.dcssip', vFileDomain, 'DCS.dcsuri', vFilePath+vFileQueryString, 'WT.ti', vFileName);
	    }
    }
}
/* LOAD FUNCTIONS ---------------------------------------------------------------------------- */
//Call the functions on page load
fncAddEvent(window,'load',fncAutoTag,false);

