// JavaScript Document
// scripts to sort and display the contents of an Ebook XML file (generated by makeXML.js)

// First call find_ebooks which loads the correct file, removes preceiding articles from the title and saves the titles;
// find_ebooks then calls getPubInfo and getAuthInfo to gather associated publisher and author/editor info and save to ttl_arr;
//   getPubInfo also determines if title is new by calling check_date
// find_ebooks then sorts ttl_arr and displays the results

//*************************************************************************
// ttl_arr:
//		ttl_arr[x] - title 							[set in find_ebooks()]
//			ttl_arr[x][1][i] - array of pubs		[set in getPubInfo() ]
//			ttl_arr[x][2] - length of pubs array	[set in getPubInfo() ]
//			ttl_arr[x][3] - article 				[set in find_ebooks()]
//			ttl_arr[x][4] - bool (Free?)			[set in getPubInfo() ]
//			ttl_arr[x][5] - bool (New?)				[set in getPubInfo() ]
//			ttl_arr[x][6][j] - array of authors		[set in getAuthInfo()]
//			ttl_arr[x][7] - series info string		[set in find_ebooks()]
//			ttl_arr[x][8] - notes info string		[set in find_ebooks()]
//*************************************************************************

//Global vars
//-----------
var result_text = '';
var startsWithLettRegExp = /^\D/;
var newCount = 0;
var searchStr;

//*****************************************************************************************************
// check_date()
// ------------
// returns: bool
//
// is passed a month value and checks whether it precedes the current month by 3; if so, true is
// returned, and a new icon is added next to the title.
//*****************************************************************************************************
function check_date (dateNode) {
			
		var today = new Date();
		adYr  = adDate[0].getElementsByTagName("year");
		adMon = adDate[0].getElementsByTagName("month");

	switch (today.getMonth()+1) {
		case 1:
			if (((today.getFullYear() - adYr[0].firstChild.nodeValue == 1) &&
				((adMon[0].firstChild.nodeValue == 12) || 
				 (adMon[0].firstChild.nodeValue == 11) || 
				 (adMon[0].firstChild.nodeValue == 10)))
				||
				((today.getFullYear() == adYr[0].firstChild.nodeValue) && 
				 (adMon[0].firstChild.nodeValue == 1)))
				return true;
			break;
		case 2:
			if (((today.getFullYear() - adYr[0].firstChild.nodeValue == 1) &&
				((adMon[0].firstChild.nodeValue == 12) || 
				 (adMon[0].firstChild.nodeValue == 11)))
				||
				((today.getFullYear() == adYr[0].firstChild.nodeValue) && 
				 ((adMon[0].firstChild.nodeValue == 2) || 
				  (adMon[0].firstChild.nodeValue == 1))))
				return true;
			break;
		case 3:
			if (((today.getFullYear() - adYr[0].firstChild.nodeValue == 1) &&
				(adMon[0].firstChild.nodeValue == 12))
				||
				((today.getFullYear() == adYr[0].firstChild.nodeValue) && 
				 ((adMon[0].firstChild.nodeValue == 3) || 
				  (adMon[0].firstChild.nodeValue == 2) || 
				  (adMon[0].firstChild.nodeValue == 1))))
				return true;
			break;
		case 4:
			if ((today.getFullYear() == adYr[0].firstChild.nodeValue) && 
				 ((adMon[0].firstChild.nodeValue == 4) ||
				  (adMon[0].firstChild.nodeValue == 3) ||
				  (adMon[0].firstChild.nodeValue == 2) ||
				  (adMon[0].firstChild.nodeValue == 1)))
				return true;
			break;
		case 5:
			if ((today.getFullYear() == adYr[0].firstChild.nodeValue) && 
				 ((adMon[0].firstChild.nodeValue == 5) ||
				  (adMon[0].firstChild.nodeValue == 4) ||
				  (adMon[0].firstChild.nodeValue == 3) ||
				  (adMon[0].firstChild.nodeValue == 2)))
				return true;
			break;
		case 6:
			if ((today.getFullYear() == adYr[0].firstChild.nodeValue) && 
				 ((adMon[0].firstChild.nodeValue == 6) ||
				  (adMon[0].firstChild.nodeValue == 5) ||
				  (adMon[0].firstChild.nodeValue == 4) ||
				  (adMon[0].firstChild.nodeValue == 3)))
				return true;
			break;
		case 7:
			if ((today.getFullYear() == adYr[0].firstChild.nodeValue) && 
				 ((adMon[0].firstChild.nodeValue == 7) ||
				  (adMon[0].firstChild.nodeValue == 6) ||
				  (adMon[0].firstChild.nodeValue == 5) ||
				  (adMon[0].firstChild.nodeValue == 4)))
				return true;
			break;
		case 8:
			if ((today.getFullYear() == adYr[0].firstChild.nodeValue) && 
				 ((adMon[0].firstChild.nodeValue == 8) ||
				  (adMon[0].firstChild.nodeValue == 7) ||
				  (adMon[0].firstChild.nodeValue == 6) ||
				  (adMon[0].firstChild.nodeValue == 5)))
				return true;
			break;
		case 9:
			if ((today.getFullYear() == adYr[0].firstChild.nodeValue) && 
				 ((adMon[0].firstChild.nodeValue == 9) ||
				  (adMon[0].firstChild.nodeValue == 8) ||
				  (adMon[0].firstChild.nodeValue == 7) ||
				  (adMon[0].firstChild.nodeValue == 6)))
				return true;
			break;
		case 10:
			if ((today.getFullYear() == adYr[0].firstChild.nodeValue) && 
				 ((adMon[0].firstChild.nodeValue == 10) ||
				  (adMon[0].firstChild.nodeValue == 9) ||
				  (adMon[0].firstChild.nodeValue == 8) ||
				  (adMon[0].firstChild.nodeValue == 7)))
				return true;
			break;
		case 11:
			if ((today.getFullYear() == adYr[0].firstChild.nodeValue) && 
				 ((adMon[0].firstChild.nodeValue == 11) ||
				  (adMon[0].firstChild.nodeValue == 10) ||
				  (adMon[0].firstChild.nodeValue == 9) ||
				  (adMon[0].firstChild.nodeValue == 8)))
				return true;
			break;
		case 12:
			if ((today.getFullYear() == adYr[0].firstChild.nodeValue) && 
				 ((adMon[0].firstChild.nodeValue == 12) ||
				  (adMon[0].firstChild.nodeValue == 11) ||
				  (adMon[0].firstChild.nodeValue == 10) ||
				  (adMon[0].firstChild.nodeValue == 9)))
				return true;
			break;
	}
	return false;
}	//**check_date**//


//*****************************************************************************************************
// getPubInfo()
// ------------
// parms: x--array of <item> nodes from Ebooks
//        i--current iteration of for loop from 0-x.length
//        ttl_arr--array of titles and associated info pulled from Ebooks
//
// for each publisher get URL, name, edition, pubdate and whether it's free and new;
// write values to ttl_arr[x][1,2,4,5]
//*****************************************************************************************************
function getPubInfo (x, i, ttl_arr) {
	//Publisher Names and URLs
	pubs = x[i].getElementsByTagName("publisher");

	for (j=0; j<pubs.length; j++) {
		url = pubs[j].getElementsByTagName("URL");
		pubName = pubs[j].getElementsByTagName("name");
		free = pubs[j].getElementsByTagName("Free");
		pubEd = pubs[j].getElementsByTagName("edition");
		pubDate = pubs[j].getElementsByTagName("pubdate");

		//URL & NAME -- array
		ttl_arr[i][1][j] = new Array (url[0].firstChild.nodeValue, pubName[0].firstChild.nodeValue);
		ttl_arr[i][2] = pubs.length;
		
		//EDITION
		if (pubEd[0])
			ttl_arr[i][1][j][2] = pubEd[0].firstChild.nodeValue;
		
		//PUBDATE
		if (pubDate[0])
			ttl_arr[i][1][j][3] = pubDate[0].firstChild.nodeValue;
		
		//FREE?
		if (free.length >= 1) 
			ttl_arr[i][4] = true;
	}
	
	//NEW?
	adDate = x[i].getElementsByTagName("date_added");
	if (adDate.length >= 1) 
		ttl_arr[i][5] = check_date(adDate);

}	//**getPubInfo**//


//*****************************************************************************************************
// getAuthInfo()
// ------------
// parms: x--array of <item> nodes from Ebooks
//        i--current iteration of for loop from 0-x.length
//        ttl_arr--array of titles and associated info pulled from Ebooks
//
// for each publisher get array of author/editor last names;
// write values to ttl_arr[x][6]
//*****************************************************************************************************
function getAuthInfo (x, i, ttl_arr) {
	//Publisher Names and URLs
	auths = x[i].getElementsByTagName("editauth");

	ttl_arr[i][6] = new Array;
	for (j=0; j<auths.length; j++) {
		//get LAST NAME only
		ttl_arr[i][6][j] = auths[j].firstChild.nodeValue.split(",",1);
	}
}	//**getAuthInfo**//


//*****************************************************************************************************
// getEBSubjHead()
// ------------
// parms: valu1--the subject short-hand name requested by user
//		  *returns: subject long name for display in table
//
// converts the subject heading from the short name used to call the functions to the longer name used
// in the select list that users see.
//*****************************************************************************************************
function getEBSubjHead(valu1) {
		
	switch (valu1) {
		case 'AIDS': return("AIDS/HIV");
		case 'Allergy' : return("Allergy &amp; Respiratory Medicine");
		case 'Alternative': return("Alternative Medicine");
		case 'Biochem': return("Biochemistry");
		case 'Biology': return("Biology &amp; Histology");
		case 'Bioterror': return("Bioterrorism");
		case 'Cardiology': return("Cardiovascular Medicine");
		case 'Dentistry': return("Dentistry &amp; Oral Surgery");
		case 'Emergency': return("Emergency Medicine");
		case 'Family': return("Family Practice");
		case 'Forensic': return("Forensic Science");
		case 'Gastro': return("Gastroenterology");
		case 'Geriatrics': return("Geriatrics &amp; Gerontology");
		case 'Infectious': return("Infectious Diseases");
		case 'Internal': return("Internal Medicine");
		case 'Neurology': return("Neurology &amp; Neuroscience");
		case 'Obstetrics': return("Obstetrics &amp; Gynecology");
		case 'Pharm': return("Pharmacology, Toxicology &amp; Pharmaceutics");
		case 'Psych': return("Psychiatry & Psychology");
		case 'Radio': return("Radiology");
		case 'Respiratory': return("Respiratory &amp; Critical Care Medicine");
		case 'Spanish': return("Spanish Language Resources");
		case 'Consumer' : return ("Consumer Health");
		case 'http://webcomm.bcd.tamhsc.edu/library/EbooksXML/Consumer' : return ("Ebooks");

		//for those subject headings that are the same
		default: return(valu1);
	}
}	//**getEBSubjHead**//


//*****************************************************************************************************
// displayEBs()
// ------------
// parms: valu1--the beginnig letter or subject requested by user
//        ttl_arr[]--array of information on each <item> in the specific XML file
//        pass--how many times have we called this (or another displayXX() funct.) already?
//      * result_text--global string
//
// writes one long string to result_text which is then written to the page using innerHTML 
// property of displayResults;
// results structure is a table with a green header that includes search value and # of results;
//
// first write header,
// then if title has only one publisher write the row for the title
//		more info icon, free icon?, url, title, new icon?, more info box 
//			(with title, authors, publisher name, edition & pubdate)
//			if title had a preceding article before sorting put it back when writing title
//	else if title has multiple publishers write the row with publisher URLs after title
//			include other information and articles too
//*****************************************************************************************************
function displayEBs (field1, valu1, ttl_arr, pass) {

	//init result_text
	if (pass==1)
		result_text = '';

	//begin of result table
	if (pass==1) {
		result_text += '<table cellpadding="0" cellspacing="0" width="100%">';
			result_text += '<tr class="TableHeader">';
			if (field1 == "beginLett") {
				result_text += '<td class="EbooksLink" width="400">View: <u>' + valu1 + '</u></td>';
			}
			else if (field1 == "subject") {
				result_text += '<td class="EbooksLink" width="400">View: <u>' + getEBSubjHead(valu1) + '</u></td>';
			}
			else if (field1 == "BCD") {
				result_text += '<td class="EbooksLink" width="400">' + field1 + ': <u>' + valu1 + '</u></td>';
			}
			else if (field1 == "new") {  //New ebooks
				result_text += '<td class="EbooksLink" width="400">' + valu1 + '</td>';
			}
			result_text += '<td class="EbooksLink" width="165" align="right">Number of Titles: <u>' + (ttl_arr.length) + '</u></td>';
			result_text += '</tr>';
	}
	else {
		result_text += '<table cellpadding="0" cellspacing="0" width="100%">';
		//add some formatting
			result_text += '<tr><td width="30px">&nbsp;</td><td width="335">&nbsp;</u></td><td width="200" align="right">&nbsp;</td></tr>';
		//add a subheader to show what the resource type is, only if not the nonJava page
			result_text += '<tr><td>&nbsp;</td></tr><tr><td align="center" style="border-bottom:solid 2px #006600"><a href="javascript:void(0);" onClick="showHideList(' + "'EBList'" + ');"><img src="http://bcd.tamhsc.edu/library/img/web/misc/green-hide.gif" id="EBListImg" alt="Hide List" title="Hide List"/></a></td><td colspan="2" style="border-bottom:solid 2px #006600"><font color="#006600" face="Arial, Helvetica, sans-serif" size="-1"><strong>Ebooks</strong></font></td></tr>';
		//put a border along the left side and start the table
			result_text += '<tr><td style="border-left:solid 2px #006600">&nbsp;</td><td colspan="2" id="EBList"><table cellpadding="0" cellspacing="0" width="100%">';
//			result_text += '<tr><td valign="top" style="border-top:solid 2px #99CC99"><img src="http://bcd.tamhsc.edu/library/img/web/misc/3rd_EB_Title_grn.gif" class="VrtMenuTitle" style="vertical-align:top" /></td> <td colspan="2" valign="top" width="100%" style="border-top:solid 2px #99CC99"><table cellpadding="0" cellspacing="0" width="100%">';
	}

	//prepare cookie: when a user clicks on a link save the current field1 and valu1 values so that this
	//page will be open if they return to the ebooks page using the back button
	//cName = "EBsrch";
	//cVal  = field1.charAt(0) + ',' + valu1;
	//I commented this out because it made the page too clunky. To put it back in add:  onClick="setCookie(cName,cVal)"
	//to the URL lineS below
	
	for (i=0; i<ttl_arr.length; i++) {
			//set vars to ttl_arr[] values
			Title 	= ttl_arr[i][0];
			numPubs = ttl_arr[i][2];
			Article = ttl_arr[i][3];
			Free 	= ttl_arr[i][4];
			New 	= ttl_arr[i][5];
			Series  = ttl_arr[i][7];
			Notes	= ttl_arr[i][8];
			
		//for new ebooks we need to keep count of the total # of new books, since
		//this function gets called 3 times for newest, newer and new books
		x = i + newCount;
				
		//create the info icon and title link
		//for titles with ONLY ONE PROVIDER
		if (numPubs == 1) {
				//set vars to ttl_arr[] values
				Editauths = ttl_arr[i][6];
				//Publisher sub-fields in ttl_arr[]
				URL     = ttl_arr[i][1][0][0];
				Name    = ttl_arr[i][1][0][1];
				Edition = ttl_arr[i][1][0][2];
				Pubdate = ttl_arr[i][1][0][3];
				
				statusMsg = "Resource Info";
				statusBlnk = "";
				
				resultId = "infoNote" + x;

			if (pass==1)
				result_text += '<tr><td class="TableRow" colspan="2">';
			else
				result_text += '<tr><td class="TableRow" colspan="2" style="padding-left:1px">';

				//INFO-more icon and event action
				result_text += '<a href="#" onClick="showHide_Ebooks(' + x + ');return false;" style="cursor:help"><img align="texttop" src="http://bcd.tamhsc.edu/library/img/web/misc/info-more.gif" id="' + resultId + '" title="More info"  onMouseOver="window.status=statusMsg; return true" onMouseOut="window.status=statusBlnk;"/></a> ';
				//FREE?
				if (Free)
					result_text += '<img align="texttop" src="http://bcd.tamhsc.edu/library/img/web/buttons_bars/FreeStar.gif" alt="Free"> ';
				//URL
				result_text += '<a class="Results" href="' + URL.toString() + '">';
				
				//put any preceding articles back at front of title
				if (Article == 1)
					result_text += 'A ';
				else if (Article == 2)
					result_text += 'An ';
				else if (Article == 3)
					result_text += 'The ';
				//TITLE
				result_text += Title.toString() + '</a>';
				
				//NEW?
				if (New)
					result_text += ' <img src="http://bcd.tamhsc.edu/library/img/web/misc/new.gif" align="absmiddle" alt="new" title="new"/>';
				
				//new divs for additional info slidedown box
				result_text += '<div id="infoBox_slidedown" class="EB_info_slidedown">';
					result_text += '<div id="infoBox_contentBox' + x + '" class="EB_info_contentBox">';
						result_text += '<div id="infoBox_content' + x + '" class="EB_info_content">';
							result_text += '<table width="200px" style="font-size:1.2em">';
								result_text += '<tr>';
									result_text += '<td colspan="2"><b>' + Title.toString() + '</b>';
									
									//include AUTHOR last name with TITLE
									if (Editauths.length > 0) {
										//a title may have 0-4 editors/authors
										result_text += ' (';
										for (k=0; k<Editauths.length; k++) {
											result_text += Editauths[k];
											if (k == Editauths.length-1) {break}
											result_text += ', ';
										}
										result_text += ')';
									}
									result_text += '</td>';
								result_text += '</tr>';
								//SERIES info
								if (Series) {
								result_text += '<tr>';
									result_text += '<td valign="top">Series: </td>';
									result_text += '<td style="border-bottom:solid 1px #000000">[' + Series.toString() + ']</td>';
								result_text += '</tr>';
								}
								//PUBLISHER's NAME
								result_text += '<tr>';
									result_text += '<td width="30%" valign="top">Provider: </td>';
									result_text += '<td width="70%" style="border-bottom:solid 1px #000000">' + Name.toString() + '</td>';
								result_text += '</tr>';
								//EDITION
								if (Edition) {
								result_text += '<tr>';
									result_text += '<td valign="top">Edition: </td>';
									 result_text += '<td style="border-bottom:solid 1px #000000">' + Edition.toString() + '</td>';
								result_text += '</tr>';
								}
								//PUBDATE
								if (Pubdate) {
								result_text += '<tr>';
									result_text += '<td valign="top">Year: </td>';
									 result_text += '<td style="border-bottom:solid 1px #000000">' + Pubdate.toString() + '</td>';
								result_text += '</tr>';
								}
								//NOTES
								if (Notes) {
								result_text += '<tr>';
									result_text += '<td valign="top"><font color="#FF3300"><b>Note: </b></font></td>';
									result_text += '<td style="border-bottom:solid 1px #000000">' + Notes.toString() + '</td>';
								result_text += '</tr>';
								}
							result_text += '</table>';
						result_text += '</div>';
					result_text += '</div>';
				result_text += '</div>';

			result_text += '</td></tr>';
		}
		
		//create the info icon and title link
		//for titles with MULTIPLE PROVIDERS
		else if (numPubs > 1) {
				//set vars to ttl_arr[] values
				Editauths = ttl_arr[i][6];
				Series    = ttl_arr[i][7];
				Notes     = ttl_arr[i][8];
				
				resultId = "infoNote" + x;

			if (pass==1)
				result_text += '<tr><td class="TableRow" colspan="2">';
			else
				result_text += '<tr><td class="TableRow" colspan="2" style="padding-left:1px">';

				//INFO-more icon and event action
				result_text += '<a href="#" onClick="showHide_Ebooks(' + x + ');return false;" style="cursor:help"><img align="texttop" src="http://bcd.tamhsc.edu/library/img/web/misc/info-more.gif" id="' + resultId + '" title="More info"/></a> ';
				//FREE?
				if (Free)
					result_text += '<img align="texttop" src="http://bcd.tamhsc.edu/library/img/web/buttons_bars/FreeStar.gif" alt="Free"> '
				//put any preceding articles back at front of title
				if (Article == 1)
					result_text += 'A ';
				else if (Article == 2)
					result_text += 'An ';
				else if (Article == 3)
					result_text += 'The ';
				//TITLE
				result_text += Title.toString();
				
				//for titles with multiple pubs, include pub name in ()
				for (j=0; j<(numPubs); j++) {
						//set vars to ttl_arr[] values
						URL     = ttl_arr[i][1][j][0];
						Name    = ttl_arr[i][1][j][1];
					//URL & NAME
					result_text += ' (<a class="Results" href="' + URL.toString() + '">' + Name.toString() + '</a>) ';
				}
					
					
				//NEW?
				if (New)
					result_text += ' <img src="http://bcd.tamhsc.edu/library/img/web/misc/new.gif" align="absmiddle" alt="new" title="new"/>';
				
				//new divs for additional info slidedown box
				result_text += '<div id="infoBox_slidedown" class="EB_info_slidedown">';
					result_text += '<div id="infoBox_contentBox' + x + '" class="EB_info_contentBox">';
						result_text += '<div id="infoBox_content' + x + '" class="EB_info_content">';
							result_text += '<table width="200px" style="font-size:1.2em">';
								result_text += '<tr>';
									result_text += '<td colspan="2"><b>' + Title.toString() + '</b>';
									
									//include AUTHOR last name with TITLE
									if (Editauths.length > 0) {
										//a title may have 0-4 editors/authors
										result_text += ' (';
										for (k=0; k<Editauths.length; k++) {
											result_text += Editauths[k];
											if (k == Editauths.length-1) {break}
											result_text += ', ';
										}
										result_text += ')';
									}
									result_text += '</td>';
								result_text += '</tr>';

								//this title is available from more than one provider
								for (k=0; k<(numPubs); k++) {
									//Publisher sub-fields
									URL = ttl_arr[i][1][k][0];
									Name = ttl_arr[i][1][k][1];
									Edition = ttl_arr[i][1][k][2];
									Pubdate = ttl_arr[i][1][k][3];
									//SERIES info
									if (Series) {
									result_text += '<tr>';
										result_text += '<td valign="top">Series: </td>';
										result_text += '<td style="border-bottom:solid 1px #000000">[' + Series.toString() + ']</td>';
									result_text += '</tr>';
									}
									//PUBLISHER NAME
									result_text += '<tr>';
										result_text += '<td width="30%" valign="top">Provider: </td>';
											result_text += '<td width="70%" style="border-bottom:solid 1px #000000">' + Name.toString() + '</td>';
									result_text += '</tr>';
									//EDITION
									if (Edition) {
									result_text += '<tr>';
										result_text += '<td valign="top">Edition: </td>';
										 result_text += '<td style="border-bottom:solid 1px #000000">' + Edition.toString() + '</td>';
									result_text += '</tr>';
									}
									//PUBDATE
									if (Pubdate) {
									result_text += '<tr>';
										result_text += '<td valign="top">Year: </td>';
										 result_text += '<td style="border-bottom:solid 1px #000000">' + Pubdate.toString() + '</td>';
									result_text += '</tr>';
									}
									//NOTES
									if (Notes) {
									result_text += '<tr>';
										result_text += '<td valign="top"><font color="#FF3300"><b>Note: </b></font></td>';
										result_text += '<td style="border-bottom:solid 1px #000000">' + Notes.toString() + '</td>';
									result_text += '</tr>';
									}
									result_text += '<tr><td colspan="2" style="border-top:solid 2px #000000">&nbsp;</td></tr>';
								}
							result_text += '</table>';
						result_text += '</div>';
					result_text += '</div>';
				result_text += '</div>';

			result_text += '</td></tr>';
		}
	}
	result_text += '<tr><td>&nbsp;</td></tr>';
	result_text += "</table>";
	
	if (pass>1)
		result_text += " </td></tr></table>";
	
	//before leaving add the length of ttl_arr to newCount; if this is being called from find_newEbooks()
	//this this function will be called again, and newCount will serve as an offset to keep a count of
	//each new book
	newCount += ttl_arr.length;
}	//**displayEBs**//


//*****************************************************************************************************
// displayAllEBs()
// ------------
// parms: valu1--the beginnig letter or subject requested by user
//        ttl_arr[]--array of information on each <item> in the specific XML file
//      * result_text--global string
//
// same as displayEBs() but only called when user requests View All option--it does not include
// the more info box (it takes way to long to build the display for all that info); instead
// the info icon displays the publisher name as a title/alt tag of the info icon image.
//*****************************************************************************************************
function displayAllEBs (valu1, ttl_arr) {
//alert('displayAllEBs()');

	//init result_text
	result_text = '';
	
	result_text += '<table cellpadding="0" cellspacing="0" width="100%">';
		result_text += '<tr class="TableHeader">';
			result_text += '<td class="EbooksLink" width="200">View: <u>' + valu1 + '</u></td>';
			result_text += '<td class="EbooksLink" width="365" align="right">Number of Titles: <u>' + (ttl_arr.length) + '</u></td>';
		result_text += '</tr>';
		result_text += '<tr><td>&nbsp;</td></tr>';

	//prepare cookie: when a user clicks on a link save the current field1 and valu1 values so that this
	//page will be open if they return to the ebooks page using the back button
	//cName = "EBsrch";
	//cVal  = "b,All";
	
	for (i=0; i<ttl_arr.length; i++) {
			//set vars to ttl_arr[] values
			Title = ttl_arr[i][0];
			numPubs = ttl_arr[i][2];
			Article = ttl_arr[i][3];
			Free = ttl_arr[i][4];
			New = ttl_arr[i][5];
		
		//put section header rows based on first letter of title
		if (i==0) {
			result_text += '<tr class="TableHeader"><td colspan="2"><a class="EbooksLink">0to9</a></td></tr>';
		}
		else if ((startsWithLettRegExp.test(Title.toString())) && (Title.toString().charAt(0) != ttl_arr[i-1][0].toString().charAt(0))) {  //the first letter of the title has changed
			result_text += '<tr><td>&nbsp;</td></tr>';
			result_text += '<tr class="TableHeader"><td><a class="EbooksLink" name="' + Title.toString().charAt(0) + '">' + Title.toString().charAt(0) + '</a></td>';
			result_text += '<td align="right"><a href="#top">top<img src="http://bcd.tamhsc.edu/library/img/web/misc/return_arrow_bw.gif" height="20px" width="20px" border="0" alt="Return to top of page." align="texttop" title="Return to top of page."></a></td></tr>';
		}
		
		//create the info icon and title link 
		for (j=0; j<(numPubs); j++) {
				//Publisher sub-fields in ttl_var[]
				URL     = ttl_arr[i][1][j][0];
				Name    = ttl_arr[i][1][j][1];
			
			result_text += '<tr><td class="TableRow" colspan="2">';
				result_text += '<img align="texttop" src="http://bcd.tamhsc.edu/library/img/web/buttons_bars/info.gif" alt="Provided through ' + Name.toString() + '" title="Provided through ' + Name.toString() + '"> ';
				
				//include a Free icon if free from this publisher
				if (Free)
					result_text += '<img align="texttop" src="http://bcd.tamhsc.edu/library/img/web/buttons_bars/FreeStar.gif" alt="Free"> '
				
				result_text += '<a class="Results" href="' + URL.toString() + '">';
				
				//put any preceding articles back at front of title
				if (Article == 1)
					result_text += 'A ';
				else if (Article == 2)
					result_text += 'An ';
				else if (Article == 3)
					result_text += 'The ';
				result_text += Title.toString() + '</a>';
				
				//for titles with multiple pubs, include pub name in ()
				if (numPubs>1)
					result_text += ' (' + Name.toString() + ')';
				
				//if this is new then include the new! icon
				if (New)
					result_text += ' <img src="http://bcd.tamhsc.edu/library/img/web/misc/new.gif" align="absmiddle" alt="new" title="new"/>';
					
			result_text += '</td></tr>';
		}
	}
	result_text += "</table>";
}	//**displayAllEBs**//



//*****************************************************************************************************
// showWorking()
// -------------
// Show the user that something is happening by displaying a "Working..." mesaage and "wait" cursor
//*****************************************************************************************************
function showWorking() {
var cursor = 
     document.layers ? document.cursor :
     document.all ? document.all.cursor :
     document.getElementById ? document.getElementById('cursor') : null;
	 
	cursor="wait";
	document.getElementById('displayResults').innerHTML = "<p>Working...</p>";
	return;
}	//**showWorking**//



//*****************************************************************************************************
// find_ebooks()
// -------------
// parms: field1--indicates what type of value valu1 is (beginLett, subject)
//        valu1--the letter or subject used to locate the appropriate XML file
//
// for each entry in the specified XML file, remove any preceding articles from the titles,
// write the title and article indicator to ttl_arr[], then gather the publisher and author info;
// sort the array alphabetically by title; 
// call displayEB to create the result_text string and set to displayResults.innerHTML
//*****************************************************************************************************
function find_ebooks (field1, valu1) {
//alert('find_ebooks()');

		var precedArt = /^A\s|^An\s|^The\s/i;
		var ttl_arr = new Array();

	//reset the subject category choice
	document.getElementById('EB_Subject').selectedIndex = 0;
	
	//reset the slidedownIndex used in showHide_Ebooks() of dropDown.js
	//everytime that a new page is requested, in case they left a info box
	//open when they went to another page
	slidedownIndex = -1;
	
	if (valu1 == 'All'){
		if (field1 == 'nonJava') {
			fName = "http://bcd.tamhsc.edu/library/ebooks/abc.xml";
			Ebooks = loadXMLDoc(fName);
		}
		else {
			//change some appearance parameters based on search choice
			document.getElementById('hiddenJumpTo').style.visibility='visible'; 
			document.getElementById('alphaloopAll').style.borderBottom='none';
			
			//determine the name of the file to load
			fName = "abc.xml";
			Ebooks = loadXMLDoc(fName);
		}
	}
	else {
		//change some appearance parameters based on search choice
		document.getElementById('hiddenJumpTo').style.visibility='hidden';
		document.getElementById('alphaloopAll').style.borderBottom='1px solid';
		document.getElementById('alphaloopAll').className='alphaloopAll';
		
		//determine the name of the file to load
		fName = valu1 + ".xml";
		Ebooks = loadXMLDoc(fName);
	}
	
	//move the XML file into variable x
	x=Ebooks.getElementsByTagName("item");
//alert(x.length);


		
	for (i=0; i<x.length; i++) {
		
		ttl = x[i].getElementsByTagName("title");

		//Check the title for preceding A, An, or The
		if (ttl[0].firstChild.nodeValue.substr(0,2) == "A ") {
			title = ttl[0].firstChild.nodeValue.slice(2);
			art = 1;
		}
		else if (ttl[0].firstChild.nodeValue.substr(0,3) == "An ") {
			title = ttl[0].firstChild.nodeValue.slice(3);
			art = 2;
		}
		else if (ttl[0].firstChild.nodeValue.substr(0,4) == "The ") {
			title = ttl[0].firstChild.nodeValue.slice(4);
			art = 3;
		}
		else {
			title = ttl[0].firstChild.nodeValue;
			art = 0;
		}
		ttl_arr[i] = new Array (title, new Array ());
		ttl_arr[i][3] = art;

		//get alternate title info, if any
		//if this is an alphabetic list then use the appropriate title,
		//if it's subject list then use the alternate title.
		attl = x[i].getElementsByTagName("alt_title");
		if (attl[0]) {
			if (attl[0].firstChild.nodeValue.substr(0,2) == "A ") {
				atitle = attl[0].firstChild.nodeValue.slice(2);
				art = 1;
			}
			else if (attl[0].firstChild.nodeValue.substr(0,3) == "An ") {
				atitle = attl[0].firstChild.nodeValue.slice(3);
				art = 2;
			}
			else if (attl[0].firstChild.nodeValue.substr(0,4) == "The ") {
				atitle = attl[0].firstChild.nodeValue.slice(4);
				art = 3;
			}
			else {
				atitle = attl[0].firstChild.nodeValue;
				art = 0;
			}
			
			if ((valu1 == 'All') || (field1 == 'subject')) {
				ttl_arr[i][0] = atitle;
				ttl_arr[i][3] = art;
			}
			else if (field1 == 'beginLett') {
				if (valu1 == atitle.charAt(0)) {
					ttl_arr[i][0] = atitle;
					ttl_arr[i][3]= art;
				}
			}
		}
		
		//get series info, if any
		series = x[i].getElementsByTagName("series");
		if (series[0]) {
			ttl_arr[i][7] = series[0].firstChild.nodeValue;
		}

		//get any notes
		notes = x[i].getElementsByTagName("notes");
		if (notes[0]) {
			ttl_arr[i][8] = notes[0].firstChild.nodeValue;
		}

		//get other info: Publisher names, URLs, Edition, Pubdate, Authors, free and new info
		getPubInfo(x, i, ttl_arr);
		getAuthInfo(x, i, ttl_arr);
	}
	
	
	//sort the array of results
	ttl_arr.sort();

	//set newCount to 0 in case it was previously called by find_newEbooks()
	newCount = 0;
	
	//if this is for the nonJava page then send the result back to insert in the HTML page
	if (field1 == "nonJava") {
		displayAllEBs ('All', ttl_arr);
//alert (result_text);
		return result_text;
	}
	//otherwise display the results
	else 
		if (valu1 == "All")
			displayAllEBs ('All', ttl_arr);
		else
			displayEBs (field1, valu1, ttl_arr, 1);
	
	//change pointer back to default
	document.body.style.cursor="default";
	
	//display result table
	document.getElementById('displayResults').innerHTML = result_text;
}	//**find_ebooks**//


//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// Procedures for NEW EBOOKS
// -------------------------
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------

//*****************************************************************************************************
// check_newness()
// ---------------
// returns: bool, or new/newer/newest
//
// is passed a month value and checks whether it precedes the current month by 3; if so, it 
// it returns an indication of how new it is (Newest=same month, Newer=last month, New=prev 2 months
//*****************************************************************************************************
function check_newness (dateNode) {
			
		var today = new Date();
		adYr  = adDate[0].getElementsByTagName("year");
		adMon = adDate[0].getElementsByTagName("month");

	switch (today.getMonth()+1) {
		case 1:
			if (today.getFullYear() - adYr[0].firstChild.nodeValue == 1) {
				if (adMon[0].firstChild.nodeValue == 12)
					return ('Newer');
				if (adMon[0].firstChild.nodeValue == 11)
					return ('New');
				if (adMon[0].firstChild.nodeValue == 10)
					return ('New');
			}
			else if (today.getFullYear() == adYr[0].firstChild.nodeValue) {
				if (adMon[0].firstChild.nodeValue == 1)
					return ('Newest')
			}
			break;
		case 2:
			if (today.getFullYear() - adYr[0].firstChild.nodeValue == 1) {
				if (adMon[0].firstChild.nodeValue == 12)
					return ('New');
				if (adMon[0].firstChild.nodeValue == 11)
					return ('New');
			}
			else if (today.getFullYear() == adYr[0].firstChild.nodeValue) {
				if (adMon[0].firstChild.nodeValue == 2)
					return ('Newest')
				if (adMon[0].firstChild.nodeValue == 1)
					return ('Newer');
			}
			break;
		case 3:
			if (today.getFullYear() - adYr[0].firstChild.nodeValue == 1) {
				if (adMon[0].firstChild.nodeValue == 12)
					return ('New');
			}
			else if (today.getFullYear() == adYr[0].firstChild.nodeValue) {
				if (adMon[0].firstChild.nodeValue == 3)
					return ('Newest')
				if (adMon[0].firstChild.nodeValue == 2)
					return ('Newer');
				if (adMon[0].firstChild.nodeValue == 1)
					return ('New');
			}
			break;
		case 4:
			if (today.getFullYear() == adYr[0].firstChild.nodeValue) {
				if (adMon[0].firstChild.nodeValue == 4)
					return ('Newest')
				if (adMon[0].firstChild.nodeValue == 3)
					return ('Newer');
				if (adMon[0].firstChild.nodeValue == 2)
					return ('New');
				if (adMon[0].firstChild.nodeValue == 1)
					return ('New');
			}
			break;
		case 5:
			if (today.getFullYear() == adYr[0].firstChild.nodeValue) {
				if (adMon[0].firstChild.nodeValue == 5)
					return ('Newest')
				if (adMon[0].firstChild.nodeValue == 4)
					return ('Newer');
				if (adMon[0].firstChild.nodeValue == 3)
					return ('New');
				if (adMon[0].firstChild.nodeValue == 2)
					return ('New');
			}
			break;
		case 6:
			if (today.getFullYear() == adYr[0].firstChild.nodeValue) {
				if (adMon[0].firstChild.nodeValue == 6)
					return ('Newest')
				if (adMon[0].firstChild.nodeValue == 5)
					return ('Newer');
				if (adMon[0].firstChild.nodeValue == 4)
					return ('New');
				if (adMon[0].firstChild.nodeValue == 3)
					return ('New');
			}
			break;
		case 7:
			if (today.getFullYear() == adYr[0].firstChild.nodeValue) {
				if (adMon[0].firstChild.nodeValue == 7)
					return ('Newest')
				if (adMon[0].firstChild.nodeValue == 6)
					return ('Newer');
				if (adMon[0].firstChild.nodeValue == 5)
					return ('New');
				if (adMon[0].firstChild.nodeValue == 4)
					return ('New');
			}
			break;
		case 8:
			if (today.getFullYear() == adYr[0].firstChild.nodeValue) {
				if (adMon[0].firstChild.nodeValue == 8)
					return ('Newest')
				if (adMon[0].firstChild.nodeValue == 7)
					return ('Newer');
				if (adMon[0].firstChild.nodeValue == 6)
					return ('New');
				if (adMon[0].firstChild.nodeValue == 5)
					return ('New');
			}
			break;
		case 9:
			if (today.getFullYear() == adYr[0].firstChild.nodeValue) {
				if (adMon[0].firstChild.nodeValue == 9)
					return ('Newest')
				if (adMon[0].firstChild.nodeValue == 8)
					return ('Newer');
				if (adMon[0].firstChild.nodeValue == 7)
					return ('New');
				if (adMon[0].firstChild.nodeValue == 6)
					return ('New');
			}
			break;
		case 10:
			if (today.getFullYear() == adYr[0].firstChild.nodeValue) {
				if (adMon[0].firstChild.nodeValue == 10)
					return ('Newest')
				if (adMon[0].firstChild.nodeValue == 9) {
					return ('Newer');
				}
				if (adMon[0].firstChild.nodeValue == 8)
					return ('New');
				if (adMon[0].firstChild.nodeValue == 7)
					return ('New');
			}
			break;
		case 11:
			if (today.getFullYear() == adYr[0].firstChild.nodeValue) {
				if (adMon[0].firstChild.nodeValue == 11)
					return ('Newest')
				if (adMon[0].firstChild.nodeValue == 10)
					return ('Newer');
				if (adMon[0].firstChild.nodeValue == 9)
					return ('New');
				if (adMon[0].firstChild.nodeValue == 8)
					return ('New');
			}
			break;
		case 12:
			if (today.getFullYear() == adYr[0].firstChild.nodeValue) {
				if (adMon[0].firstChild.nodeValue == 12)
					return ('Newest')
				if (adMon[0].firstChild.nodeValue == 11)
					return ('Newer');
				if (adMon[0].firstChild.nodeValue == 10)
					return ('New');
				if (adMon[0].firstChild.nodeValue == 9)
					return ('New');
			}
			break;
	}
	return false;
}	//**check_newness**//


//*****************************************************************************************************
// getNewPubInfo()
// ------------
// parms: x--array of <item> nodes from Ebooks
//        i--current iteration of for loop from 0-x.length
//        ttl_arr--array of titles and associated info pulled from Ebooks
//        len--current lenght of the passed array (new_arr[]/newer_arr[]/newest_arr[]
//
// for each publisher get URL, name, edition, pubdate and whether it's free and new;
// write values to ttl_arr[x][1,2,4,5]
//*****************************************************************************************************
function getNewPubInfo (x, i, ttl_arr, len) {
	//Publisher Names and URLs
	pubs = x[i].getElementsByTagName("publisher");

	for (j=0; j<pubs.length; j++) {
		url = pubs[j].getElementsByTagName("URL");
		pubName = pubs[j].getElementsByTagName("name");
		free = pubs[j].getElementsByTagName("Free");
		pubEd = pubs[j].getElementsByTagName("edition");
		pubDate = pubs[j].getElementsByTagName("pubdate");
		
		//URL & NAME -- array
		ttl_arr[len][1][j] = new Array (url[0].firstChild.nodeValue, pubName[0].firstChild.nodeValue);
		ttl_arr[len][2] = pubs.length;
		//EDITION
		if (pubEd[0])
			ttl_arr[len][1][j][2] = pubEd[0].firstChild.nodeValue;
		//PUBDATE
		if (pubDate[0])
			ttl_arr[len][1][j][3] = pubDate[0].firstChild.nodeValue;
		
		//FREE?
		if (free[0]) 
			ttl_arr[len][4] = true;
	}
	
	//NEW?
	adDate = x[i].getElementsByTagName("date_added");
	if (adDate[0]) 
		ttl_arr[len][5] = check_date(adDate);

}	//**getNewPubInfo**//


//*****************************************************************************************************
// getNewAuthInfo()
// ------------
// parms: x--array of <item> nodes from Ebooks
//        i--current iteration of for loop from 0-x.length
//        ttl_arr--array of titles and associated info pulled from Ebooks
//        len--current lenght of the passed array (new_arr[]/newer_arr[]/newest_arr[]
//
// for each publisher get array of author/editor last names;
// write values to ttl_arr[x][6]
//*****************************************************************************************************
function getNewAuthInfo (x, i, ttl_arr, len) {
	//Publisher Names and URLs
	auths = x[i].getElementsByTagName("editauth");

	ttl_arr[len][6] = new Array;
	for (j=0; j<auths.length; j++) {
		//get LAST NAME only
		ttl_arr[len][6][j] = auths[j].firstChild.nodeValue.split(",",1);
	}
}	//**getNewAuthInfo**//


//*****************************************************************************************************
// find_newEbooks()
// -------------
//
// for each entry in the specified XML file, remove any preceding articles from the titles,
// write the title and article indicator to ttl_arr[], then gather the publisher and author info;
// sort the array alphabetically by title; 
// call displayEB to create the result_text string and set to displayResults.innerHTML
//*****************************************************************************************************
function find_newEbooks() {

		var precedArt = /^A\s|^An\s|^The\s/i;
		var new_arr = new Array();
		var new_len = 0;					//the length of the new array
		var newer_arr = new Array();
		var newer_len = 0;					//the length of the newer array
		var newest_arr = new Array();
		var newest_len = 0;					//the length of the newest array
		var newness_text = '';
		
	//name of external XML file with new <item>s
	fName = "New.xml";

	//load the XML file and initialize
	Ebooks = loadXMLDoc(fName);
	x=Ebooks.getElementsByTagName("item");
	
	for (i=0; i<x.length; i++) {
		adDate = x[i].getElementsByTagName("date_added");
		ttl = x[i].getElementsByTagName("title");
		
		//Check the title for preceding A, An, or The
		if (ttl[0].firstChild.nodeValue.substr(0,2) == "A ") {
			title = ttl[0].firstChild.nodeValue.slice(2);
			art = 1;
		}
		else if (ttl[0].firstChild.nodeValue.substr(0,3) == "An ") {
			title = ttl[0].firstChild.nodeValue.slice(3);
			art = 2;
		}
		else if (ttl[0].firstChild.nodeValue.substr(0,4) == "The ") {
			title = ttl[0].firstChild.nodeValue.slice(4);
			art = 3;
		}
		else {
			title = ttl[0].firstChild.nodeValue;
			art = 0;
		}
		
		//get alternate title info, if any
		//if this is an alphabetic list then use the appropriate title,
		//if it's subject list then use the alternate title.
		attl = x[i].getElementsByTagName("alt_title");
		//Check the title for preceding A, An, or The
		if (attl[0]) {
			if (attl[0].firstChild.nodeValue.substr(0,2) == "A ") {
				title = attl[0].firstChild.nodeValue.slice(2);
				art = 1;
			}
			else if (attl[0].firstChild.nodeValue.substr(0,3) == "An ") {
				title = attl[0].firstChild.nodeValue.slice(3);
				art = 2;
			}
			else if (attl[0].firstChild.nodeValue.substr(0,4) == "The ") {
				title = attl[0].firstChild.nodeValue.slice(4);
				art = 3;
			}
			else {
				title = attl[0].firstChild.nodeValue;
				art = 0;
			}
		}
		
		switch (check_newness(adDate)) {
			case 'New':
				//move title and article values to new_arr[]
				new_arr[new_len] = new Array (title, new Array ());
				new_arr[new_len][3] = art;
				//get series info, if any
				series = x[i].getElementsByTagName("series");
				if (series[0]) {
					new_arr[new_len][7] = series[0].firstChild.nodeValue;
				}
				//get any notes
				notes = x[i].getElementsByTagName("notes");
				if (notes[0]) {
					new_arr[new_len][8] = notes[0].firstChild.nodeValue;
				}
				//get other info: Publisher names, URLs, Edition, Pubdate, Authors, free and new info
				getNewPubInfo(x, i, new_arr, new_len);
				getNewAuthInfo(x, i, new_arr, new_len);
				new_len++;
				break;
			case 'Newer':
				//move title and article values to new_arr[]
				newer_arr[newer_len] = new Array (title, new Array ());
				newer_arr[newer_len][3] = art;
				//get series info, if any
				series = x[i].getElementsByTagName("series");
				if (series[0]) {
					newer_arr[newer_len][7] = series[0].firstChild.nodeValue;
				}
				//get any notes
				notes = x[i].getElementsByTagName("notes");
				if (notes[0]) {
					newer_arr[newer_len][8] = notes[0].firstChild.nodeValue;
				}
				//get other info: Publisher names, URLs, Edition, Pubdate, Authors, free and new info
				getNewPubInfo(x, i, newer_arr, newer_len);
				getNewAuthInfo(x, i, newer_arr, newer_len);
				newer_len++;
				break;
			case 'Newest':
				//move title and article values to new_arr[]
				newest_arr[newest_len] = new Array (title, new Array ());
				newest_arr[newest_len][3] = art;
				//get series info, if any
				series = x[i].getElementsByTagName("series");
				if (series[0]) {
					newest_arr[newest_len][7] = series[0].firstChild.nodeValue;
				}
				//get any notes
				notes = x[i].getElementsByTagName("notes");
				if (notes[0]) {
					newest_arr[newest_len][8] = notes[0].firstChild.nodeValue;
				}
				//get other info: Publisher names, URLs, Edition, Pubdate, Authors, free and new info
				getNewPubInfo(x, i, newest_arr, newest_len);
				getNewAuthInfo(x, i, newest_arr, newest_len);
				newest_len++;
				break;
		}
	}
	
	//sort the array of results
	new_arr.sort();
	newer_arr.sort();
	newest_arr.sort();

	//set newCount to 0; this keeps track of how many new books are being displayed
	newCount = 0;
	
	//display the results
	displayEBs ('new','New this month', newest_arr, 1);
		newness_text += result_text;
	displayEBs ('new','New last month', newer_arr, 1);
		newness_text += result_text;
	displayEBs ('new','New', new_arr, 1);
		newness_text += result_text;
	
	//display result table
	document.getElementById('displayResults').innerHTML = newness_text;

}	//**find_newEbooks**//