/****************************************************************************************
Copyright (c) 2005 Elanco.
Elanco, Greenfield, Indiana 46140

This software is the confidential and proprietary information of Elanco, 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.

VERSION 	1.1
AUTHOR		Doug Scamahorn

DATE       	NAME           	DESCRIPTON
02/23/2007 	Doug Scamahorn  Initial creation.
05/10/2007	Doug Scamahorn	Added tracking of internal and external links as well as ability to allow sitepass function to fire for external links other than msds.xhq.lilly.com.
09/25/2008	Ben Rondot  	Commented out the tracking of internal and external links as well as ability to allow sitepass function to fire for external links other than msds.xhq.lilly.com. Now all links which need the sitepass interstial page will need to be noted in the link itself. Reason: Locations is now directed to the Elanco.com site and no interstital is needed.
04/08/2009  Dan Laughlin	Removed comments surrounding the tracking of internal and external links as well as ability to allow sitepass function to fire for external links other than msds.xhq.lilly.com.

****************************************************************************************/
/* GLOBAL UTILITY FUNCTIONS ---------------------------------------------------------------------------- */
//Make sure the browser understands the DOM methods
function fncValidateDomSupport(){
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
}
//getElementsByClass - http://www.dustindiaz.com/getelementsbyclass/
function getElementsByClass(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;
}
//Load Event Function
function fncAddLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
//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 (!getElementsByClass("contentArea")) return false;
	var vContentArea = getElementsByClass("contentArea");
	//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];
			if (vFileDomain!=fncGetPageDomain() && vFileDomain!="msds.xh1.lilly.com") {
				sitepass(this);
				return false;
			}
			else if (vFileDomain!=fncGetPageDomain() && vFileDomain!="elms.xh1.lilly.com") {
				sitepass(this);
				return false;
			}
		}
	}
	//Get the tout area container
	if (!getElementsByClass("toutArea")) return false;
	var vToutArea = getElementsByClass("toutArea");
	//alert(vToutArea.length);
	//Create an array with all the links in the content area
	if (!vToutArea[0]) return false;
	var vAllToutLinks = vToutArea[0].getElementsByTagName("a");
	//alert(vAllToutLinks.length);
	//Loop through the links
	for (var countLinks=0;countLinks<vAllToutLinks.length;countLinks++) {
		//Add an event listener that calls the webtrends tracking function on click
		vAllToutLinks[countLinks].onclick = function () {
			fncAutoTagEvent(this,"toutArea");
			var vLinkUrl = this.toString();
			var vFileDomain=fncSliceLink(vLinkUrl)[0];
			if (vFileDomain!=fncGetPageDomain() && vFileDomain!="msds.xh1.lilly.com") {
				sitepass(this);
				return false;
			}
			else if (vFileDomain!=fncGetPageDomain() && vFileDomain!="elms.xh1.lilly.com") {
				sitepass(this);
				return false;
			}
		}
	}
}
//Add the onclick event
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];
	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 elanco.us then test to see if the link is in the contentArea
		if (pLinkLocation=="contentArea"){
			//Create an array of the supported file types
			var vFileTypeArray = new Array(".pdf",".doc",".ppt",".xls");
			//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);
					//alert("INTERNAL: The file path is: "+vFilePath+", the file name is: "+vFileName+", and the file type is: "+vFileType);
				}
			}
		} else if (pLinkLocation=="toutArea") { //If the link is not in the contentArea, is it in the toutArea?
			//Call the WT tracking code
			dcsMultiTrack('DCS.dcsuri', vFilePath, 'WT.ti', vFileName);
			//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.dcssip', 'vFileDomain', 'DCS.dcsuri', 'vFilePath+vFileQueryString', 'WT.ti', 'vFileName');
		//alert("EXTERNAL: The domain is: "+vFileDomain+", the file path is: "+vFilePath+vFileQueryString+", and the file name is: "+vFileName);
	}
}
/* TRAINING MODULE AND CALCULATOR WINDOW FUNCTIONS ------------------------------------------- */
function openModule(theURL){
	window.open(theURL,'newTopic','width=770,height=512,left=0,top=0,menubar=No,toolbar=No,location=No,scrollbars=Yes,status=No,resizable=Yes,fullscreen=No');
}
function openCalc(theURL){
	window.open(theURL,'newCalc','width=565,height=450,left=0,top=0,menubar=Yes,toolbar=No,location=No,scrollbars=Yes,status=No,resizable=Yes,fullscreen=No');
}
function close_window() {
    window.close();
}
/* LOAD FUNCTIONS ---------------------------------------------------------------------------- */
//Call the functions on page load
fncAddLoadEvent(fncAutoTag);