/****************************************************************************************
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.
08/12/2010  Ben Rondot  	Deleted "AUTOTAG FUNCTIONS". Webtrends 8.6.2 is now tracking all downloads and outside links.

****************************************************************************************/
/* 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();
}