// preset global variables and make them available to other script
var exist_ISBN = "0"; // exist variables will be used to track whether a particular piece of data has been found and harvested
var bib_ISBN="0"; //  bib variable will hold the data

var exist_ISSN = "0"; 
var bib_ISSN="0";

var exist_title="0";
var bib_title="0";

var exist_subject="0";
var bib_subject="0";

var exist_author="0";
var bib_author="0";

var exist_LCCN="0";
var bib_LCCN="0";

var exist_OCLC="0";
var bib_OCLC="0";

//harvest data
 try {					// wrap in a try to hide errors and failures from the user
	
	var tr = document.getElementsByTagName('TR');		// get the rows for every table on the page
	
	for(i = 0; i < tr.length; i++) {
		var x=tr[i].getElementsByTagName('TD');		// for each row, get all of the cells
	
		if (x.length == 2 && x[0].innerHTML == "ISBN") {		// if I have 2 cells one with the ISBN
			bib_ISBN = x[1].innerHTML.replace(/(<([^>]+)>)/ig,"");		// get the ISBN and strip all tags
			bib_ISBN = bib_ISBN.replace(/[\n\t\s\-]/ig,"");		// regex cleanup of the text
			bib_ISBN = bib_ISBN.replace(/\(.*\)/ig,"");/:.*$/ig,""
			bib_ISBN = bib_ISBN.replace(/:.*$/ig,"");
			exist_ISBN='1';										// flag the variable as populated
		}

		if (x.length == 2 && x[0].innerHTML == "ISSN") {		// if I have 2 cells one with the ISSN
			bib_ISSN = x[1].innerHTML.replace(/(<([^>]+)>)/ig,"");		// get the ISSN and strip all tags
			// bib_ISSN_long = pre_ISSN + x[1].innerHTML.replace(/(<([^>]+)>)/ig,"");		//  regex cleanup
			bib_ISSN = bib_ISSN.replace(/[\n\t\s]/ig,"");
			bib_ISSN = bib_ISSN.replace(/\(.*\)/ig,"");
			bib_ISSN = bib_ISSN.substr(0, 9);
			exist_ISSN='1';								// flag the variable as populated
		}

		if (x.length == 2 && x[0].innerHTML == "Title") {		// if I have 2 cells one with the title
			bib_title = x[1].innerHTML.replace(/(<([^>]+)>)/ig,"");		// get the title and strip all tags 
			bib_title = bib_title.replace(/[\n\t]/ig,"");
			bib_title = bib_title.replace(/\/.*/ig,"");
			bib_title = bib_title.replace(/:.*$/ig,"");		// strip anything following a ':' - usually a subtitle
			bib_title = "Title: " + bib_title;				// and preface it with "Title: "
			exist_title='1';
		}

		if (x.length == 2 && x[0].innerHTML == "Subject") {		// if I have 2 cells one with the title
			bib_subject = x[1].innerHTML.replace(/(<([^>]+)>)/ig,"");		// get the title and strip all tags 
			bib_subject = bib_subject.replace(/[\n\t]/ig,"");
			bib_subject = bib_subject.replace(/\/.*/ig,"");
			bib_subject = bib_subject.replace(/-.*$/ig,"");		// strip anything following a '-' - usually a subtitle
			bib_title = "Subject: " + bib_subject;				// and preface it with "Title: "
			exist_subject='1';
		}

		if (x.length == 2 && x[0].innerHTML == "Author") {		// if I have 2 cells one with the author
			bib_author = x[1].innerHTML.replace(/(<([^>]+)>)/ig,"");		// get the author and strip all tags
			bib_author = bib_author.replace(/[\n\t]/ig,"");	
			exist_author='1';
		}

		if (x.length == 2 && x[0].innerHTML == "LCCN") {		// if I have 2 cells one with the LCCN
			bib_LCCN = x[1].innerHTML.replace(/(<([^>]+)>)/ig,"");		// get the LCCN and strip all tags
			bib_LCCN = bib_LCCN.replace(/[\n\t]/ig,"");	
			exist_LCCN='1';
		}		
		
		if (x.length == 2 && x[0].innerHTML == "Record #") {		// if I have 2 cells one with the OCLC #
			bib_OCLC = x[1].innerHTML.replace(/(<([^>]+)>)/ig,"");		// get the OCLC# and strip all tags
			bib_OCLC = bib_OCLC.replace(/[\n\t]/ig,"");
			bib_OCLC = bib_OCLC.replace(/\(.*\)/ig,"");
			var bib_OCLC_long = "OCLC:" + bib_OCLC;			// this is helpful for GoogleBooks API
			var OCLCnumTest = /ssj|heb|bks|SSJ|BKS|HEB/;	// not al my record nubers are OCLC#s, so I search for 
			var OCLCnumExist = bib_OCLC.search(OCLCnumTest); // the prefixes of the ones that are not, and only set the  
			if (OCLCnumExist == -1) {						// exist variable to 1 if the non OCLC prefixes are not present
				exist_OCLC='1';
			}
		}
	}
 }catch (e) {} // close the try and catch any errors