function getElementsByClassName(className, tag, elm) {
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}	
	}
	return returnElements;
}

function getHTMLForFindItImage() {

	var tr = document.getElementsByTagName('TR');		// we have to iterate through every TR b/c we can't get to the Intenet Links otherwise
	
	var HTML = '';
	var URLBegin = '';
	var HTMLURL = '';
	
	var endofOpen = '';
	var endofMiddle = '';
	
	// If it is the first url in Internet Links:
		
	for(i = 0; i < tr.length; i++) {					// for every TR in the document

		var x=tr[i].getElementsByTagName('TH');			// get the TH
		var y=tr[i].getElementsByTagName('TD');			// get the TD

		if (x.length == 1 && x[0].innerHTML == 'Internet Links:') {	// if the row has 2 columns and the first one has the text of Internet Links: and the second one has Check Find It for complete electronic availability

	 		HTML = y[0].innerHTML;							
			
			if (navigator.appVersion.match(/\bMSIE\b/)) {
				
				//alert ("IE");
					
				URLBegin = HTML.substring(9,34);
				//alert(URLBegin);
		
				if (URLBegin == 'http://sfx.wisconsin.edu/') {
			
					//alert ("passed");

					endOfOpen = HTML.indexOf('http');
					endOfMiddle = HTML.indexOf('>');
					//alert ("endofOpen: " + endOfOpen);
					//alert ("endofMiddle: " + endOfMiddle);
			
					HTMLURL = HTML.substring(endofOpen+9,endOfMiddle-1);
					//alert ("HTML URL: " + HTMLURL);
					
					HTML = '<a href="' + HTMLURL + '"><img src="http://www.library.wisc.edu/images/findit.gif" alt="Find It" height="19" width="92" /></a> ';

					x[0].innerHTML = '<a href="/help/internetlinks.htm" title="What Is This?">Internet Links</a>:';
					y[0].innerHTML = HTML;

				}
					
				else {}
			
			}

			else {
			
				//alert ("Not IE");
					
				URLBegin = HTML.substring(10,35);
				//alert(URLBegin);
		
				if (URLBegin == 'http://sfx.wisconsin.edu/') {
			
					//alert ("passed");

					endOfOpen = HTML.indexOf('http');
					endOfMiddle = HTML.indexOf('>');
					//alert ("endofOpen: " + endOfOpen);
					//alert ("endofMiddle: " + endOfMiddle);
			
					HTMLURL = HTML.substring(endofOpen+10,endOfMiddle-1);
					//alert ("HTML URL: " + HTMLURL);
					
					HTML = '<a href="' + HTMLURL + '"><img src="http://www.library.wisc.edu/images/findit.gif" alt="Find It" height="19" width="92" /></a> ';

					x[0].innerHTML = '<a href="/help/internetlinks.htm" title="What Is This?">Internet Links</a>:';
					y[0].innerHTML = HTML;

				}
			
				else {}
			
			}

		}

	}
	
	// What if it's not the first url in Internet Links:
		
	for(i = 0; i < tr.length; i++) {					// for every TR in the document

		var z=tr[i].getElementsByTagName('TD');			// get the TD

		if (z.length == 2 && z[0].innerHTML == '') {	// if the row has 2 columns and the first one is blank and the second one has an sfx url

	 		HTML = z[1].innerHTML;							
			
			//alert("test: " + HTML);
			
			if (navigator.appVersion.match(/\bMSIE\b/)) {
				
				//alert ("IE");
					
				URLBegin = HTML.substring(9,34);
				//alert(URLBegin);
		
				if (URLBegin == 'http://sfx.wisconsin.edu/') {
			
					//alert ("passed");

					endOfOpen = HTML.indexOf('http');
					endOfMiddle = HTML.indexOf('>');
					//alert ("endofOpen: " + endOfOpen);
					//alert ("endofMiddle: " + endOfMiddle);
			
					HTMLURL = HTML.substring(endofOpen+9,endOfMiddle-1);
					//alert ("HTML URL: " + HTMLURL);
					
					HTML = '<a href="' + HTMLURL + '"><img src="http://www.library.wisc.edu/images/findit.gif" alt="Find It" height="19" width="92" /></a> ';

					z[1].innerHTML = HTML;

				}
					
				else {}
			
			}

			else {
			
				//alert ("Not IE");
					
				URLBegin = HTML.substring(9,34);
				//alert(URLBegin);
		
				if (URLBegin == 'http://sfx.wisconsin.edu/') {
			
					//alert ("passed");

					endOfOpen = HTML.indexOf('http');
					endOfMiddle = HTML.indexOf('>');
					//alert ("endofOpen: " + endOfOpen);
					//alert ("endofMiddle: " + endOfMiddle);
			
					HTMLURL = HTML.substring(endofOpen+9,endOfMiddle-1);
					//alert ("HTML URL: " + HTMLURL);
					
					HTML = '<a href="' + HTMLURL + '"><img src="http://www.library.wisc.edu/images/findit.gif" alt="Find It" height="19" width="92" /></a> ';

					z[1].innerHTML = HTML;

				}
			
				else {}
			
			}

		}

	}
	
	return(HTML);

}


function writeFindItImage() {

	aTags = getElementsByClassName("location-code", "a", document);
	
	for(i = 0; i < aTags.length; i++) {	
		//alert("Location code is " + aTags[i].innerHTML);
		
		if (aTags[i].innerHTML =='e,openurl') {
			HTMLstring = getHTMLForFindItImage();
		}
		else {
		}

	}
	
}