// GENERAL-PURPOSE FUNCTION TO LOAD MULTIPLE FUNCTIONS
function addLoadEvent(func) { 
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// CREATE TITLE TAGS ON THE FLY - WRITTEN BY RJB 8/3/06
function createTitleTags() { 
	if (!document.getElementsByTagName) return false;
	var images = document.getElementsByTagName("IMG");
	for (var i=0; i < images.length; i++) {
		var image = images[i]
		if (!image.getAttribute("TITLE")) { // Check to see whether title tag already exists.
			if (image.getAttribute("ALT")) { // Check to see whether alt tag exists and it's not empty.
				var alttext = image.getAttribute("ALT");
				if (alttext !== " ") image.setAttribute("title",alttext);
			}
		}
	}
}

// ZEBRA-STRIPE TABLE (Modified by RJB 8/3/06 to allow multiple zebra tables on a page)
function zebraStripeTable() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementsByTagName("table")) return false;
	var tables = document.getElementsByTagName("table");
	if (tables.length < 1) return false;
	for (var i=0; i < tables.length; i++) {
		if (tables[i].getAttribute("class") == "zebra" || tables[i].getAttribute("className") == "zebra") {  // Apparently getAttribute('class') doesn't work in IE (must use getAttribute('className')) because "class" is an ECMAScript reserved name. 
			var rows = tables[i].getElementsByTagName("tr");
			for (j=0; j < rows.length; j++) {
				//manipulate rows
				if (j % 2 == 0) {
					rows[j].className = "even";
				} else {
					rows[j].className = "odd";
				}
			}
		}
	}
}

// POP UP A SMALL WINDOW -- From "DOM Scripting," Pages 88-89
function preparePopUp() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementsByTagName("a")) return false;
	var lnks = document.getElementsByTagName("a");
	if (lnks.length < 1) return false;
	for (var i=0; i<lnks.length; i++) {
		if (lnks[i].getAttribute("class") == "popup" || lnks[i].getAttribute("className") == "popup") {  // Apparently getAttribute('class') doesn't work in IE (must use getAttribute('className')) because "class" is an ECMAScript reserved name. 
			lnks[i].onclick = function() {
				popUp(this.getAttribute("href"));
				return false;
			}
		}
	}
}
function popUp(winURL) {
	window.open(winURL,"popup","width=500,height=300,scrollbars=yes,resizable=yes,menubar=no");
}

// CREATE "CLOSE THIS WINDOW" LINK ON THE FLY TO DISPLAY AT THE BOTTOM OF POP-UP WINDOWS - WRITTEN BY RJB 8/3/06
function autoCloseWindowLink () {
	if (window.name !== "popup") return false;
	if (!document.createElement) return false;
	if (!document.createTextNode) return false;
	var para = document.createElement("p");
	var link = document.createElement("a");
	var linktext = document.createTextNode("Close This Window");
	link.setAttribute("href","javascript:window.close();");
	link.appendChild(linktext);
	para.appendChild(link);
	document.body.appendChild(para);
}

// ONLY PROVIDE THE PRINT FUNCTION FOR JAVASCRIPT ENABLED BROWSERS. - JN 09/14/2007
function ptpEnable(){
	ptp = document.getElementById('printpage');
	ptp.innerHTML = '<a href="javascript:void(printSpecial());">Print this page</a>';
}

//================================================================
// provide WebTrend DCS tagging for popup links
function WebTrendDCS(theURL){
	if(theURL.indexOf("http://") >= 0){
		a = theURL.replace("http://", "");
		i = a.indexOf("/");
		if(i >= 0){
			domain = a.slice(0,i);
			page = a.slice(i, a.length);
		}
		else{
			domain = a;
			page = "";
		}		
		dcsMultiTrack('DCS.dcsref',window.location.href,'DCS.dcssip',domain,'DCS.dcsuri',page);
	}
}

// provides WebTrends DCS tagging for external links and pdf links
// for this type of link DCS take previous page as the referer
function setupWebTrendsDCS(){
	links = document.getElementsByTagName("a");
	hostN = window.location.hostname;
	
	for(i=0; i < links.length; i++){
		aLink = links[i];		
		// external links
		if(aLink.href.indexOf(hostN)<0){
			if(aLink.href.indexOf('popWindow')<0 && aLink.href.indexOf('ETFWindow')<0 && aLink.href.indexOf('PTPWindow')<0){
				aLink.onclick=extLinkAct;
			}
		}
		// internal pdf links
		else{
			if(aLink.href.indexOf(".pdf")>0){
				aLink.onclick=pdfLinkAct;
			}
		}
	}
}

function extLinkAct(evt){
	//alert('ext: ' + ',' + this.href +', DCS.dcsref,' + window.location);
	dcsMultiTrack('DCS.dcsref',window.location.href,'DCS.dcsuri',this.href);
	
	// capture the event and pass it along
	// do not deploy - wait for approval - 11/15/2006
	//evt = (evt) ? evt : ((window.event) ? event : null);
	//extLink(this.href, evt);
}

// provides WebTrends DCS tagging for pdf links
function pdfLinkAct(){
	//alert('DCS.dcsuri' + ', pdf=' + this.href);
	hostN = window.location.hostname;
	uri = this.href;
	p = uri.split(hostN);
	//alert(p[1]);
	dcsMultiTrack('DCS.dcsref',window.location.href,'DCS.dcsuri',p[1]);
}

function extLink(alink, evt){
	popWindow(alink);
	
	//John fixed IE error - 11/06/2006 - testing
	if(evt.srcElement){
	// code for IE browsers
		evt.cancelBubble = true;
		evt.returnValue = false;
	}
	else{
	// code for W3C browsers
		evt.preventDefault();
		evt.stopPropagation();
	}
}
//================================================================


// LOAD THE FUNCTIONS
addLoadEvent(ptpEnable);
addLoadEvent(createTitleTags);
addLoadEvent(zebraStripeTable);
addLoadEvent(preparePopUp);
addLoadEvent(autoCloseWindowLink);
addLoadEvent(setupWebTrendsDCS);
