var reqSearch; //the xmlhttprequest for the search var results; // JSON object holding search results var keyword; // keyword searched var itemsPerPage = 10; //pagination var divResults = document.getElementById('divSearchWithin'); var selectedResult = -999; //keep track of which row is selected and shaded var showFullResults = false; var prevKeyword = ""; var btnSearch = document.getElementById("btnSearch"); btnSearch.onmouseover = handleButton; btnSearch.onmouseout = handleButton; btnSearch.onmousedown = handleButton; function searchKeyPress(e) { if (!e) { e = window.event; } if (e && e.keyCode == 13) { doSearch(false); e.cancelBubble = true; return false; //here to cancel the event } } function doSearch(showAllResults) { //set global variable showFullResults = showAllResults; //expand if collapsed if(document.getElementById("divSearchWithin").style.display == 'none') { toggleDiv('divSearchWithin'); } keyword = document.getElementById('txtSearchBISearch').value; if (keyword !== "") { var url = '/Services/Search.aspx?isbn13=' + isbn13 + '&keyword=' + keyword + '&showFullResults=' + showFullResults; divResults.innerHTML = 'Searching...'; reqSearch = getXMLHTTPRequest(); //new request reqSearch.onreadystatechange = searchComplete; //set handler reqSearch.open('GET', url, true); //go little request go reqSearch.send(null); //set prevKeyword for full search prevKeyword = keyword; } else if ((prevKeyword !== "") && (showAllResults)) { var url = '/Services/Search.aspx?isbn13=' + isbn13 + '&keyword=' + prevKeyword + '&showFullResults=' + showFullResults; divResults.innerHTML = 'Searching...'; reqSearch = getXMLHTTPRequest(); //new request reqSearch.onreadystatechange = searchComplete; //set handler reqSearch.open('GET', url, true); //go little request go reqSearch.send(null); } } function searchComplete() { if (reqSearch.readyState == 4)//4 = complete { divResults.innerHTML = '...parsing...'; results = eval(reqSearch.responseText); displayResults(1); //show first page of results resetSearchedPages(); } } function displayResults(pageNum) { divResults.innerHTML = '...paging...'; rowCount = results.pages.length ; var searchTerm = document.getElementById('txtSearchBISearch').value; var txtReg = '(' + searchTerm + ')'; var reg = new RegExp(txtReg,'gi' ); //global, case-insensitive var resultsHTML = ''; //build the output in here before writing it out if (rowCount === 0) { resultsHTML += '
' + rowCount + ' results for "' + searchTerm + '"
'; resultsHTML += '
Your search returned no results. Please try a different search term.

'; } else { if (showFullResults) { resultsHTML += '' + rowCount + ' results for "' + prevKeyword + '"'; } else { resultsHTML += 'First ' + rowCount + ' results for "' + searchTerm + '"

'; } //how many pages are there? var maxPage = Math.floor((rowCount-1) / itemsPerPage) + 1; var firstRow = (pageNum-1) * itemsPerPage ; var lastRow = firstRow + itemsPerPage - 1; //don't try to go past last row if(lastRow > rowCount - 1) { lastRow = rowCount - 1; } //header nav if (showFullResults) { resultsHTML += drawPagingControl(pageNum,maxPage); } for (i=firstRow; i <= lastRow; i++) { //separator if(i>firstRow) { resultsHTML += '
'; } if (results.pages[i].pageName != "") { strPage = 'Page ' + results.pages[i].pageName; } else { strPage = 'Page number not available'; } strSnippet = results.pages[i].text + '... '; var resultClass, resultClick, searchTermReplace; if (isViewablePage(results.pages[i].sequence)) { resultClass = "sbResult"; resultClick = 'onClick="selectResult(' + i + ');"'; searchTermReplace = '$1'; } else { resultClass = "sbResultUnavailable"; resultClick = ''; strSnippet += '
Page unavailable'; searchTermReplace = '$1'; } //highlight the search terms strSnippet = strSnippet.replace(reg,searchTermReplace); resultsHTML += '
' + strPage + '
' + strSnippet + '
'; } //footer nav if (showFullResults) { resultsHTML += drawPagingControl(pageNum,maxPage); } else { resultsHTML += '
See all results for "' + searchTerm + '" »
(Loading times may vary)
'; } } divResults.innerHTML = resultsHTML; } function selectResult(newResult) { //clear the old selection if it exists var oldRow = document.getElementById('sbResult' + selectedResult); if(oldRow) { oldRow.style.backgroundColor = "#FFFFFF"; } //select the new row selectedResult = newResult; var newRow = document.getElementById('sbResult' + selectedResult); newRow.style.backgroundColor = "#DFDFDF"; //goToPageSequence(results.pages[selectedResult].sequence); getSearchPageImage(results.pages[selectedResult].pageGuid); } function drawPagingControl(curPage, maxPage) { var strOut = ''; strOut += ''; // show current postion "xx of yy" strOut += ''; strOut += ''; strOut += '
'; if(curPage > 1) { // draw prev control strOut += '« Previous'; } else { // disabled prev control strOut += " "; } strOut += '' + curPage + ' of ' + maxPage + ''; if(curPage < maxPage) { //draw next control strOut += 'Next »'; } else { //disabled next control strOut += " "; } strOut += '
'; return strOut; } //get the selected page with search term highlighted (highlit?) function getSearchPageImage(pageGuid) { //expand if collapsed if(document.getElementById("divSearchWithin").style.display == 'none') { toggleDiv('divSearchWithin'); } var keyword = document.getElementById('txtSearchBISearch').value; var url = '/Services/GetSearchPage.aspx?isbn13=' + isbn13 + '&keyword=' + keyword + '&pageGuid=' + pageGuid; reqSearch = getXMLHTTPRequest(); //new request reqSearch.onreadystatechange = gotSearchPage; //set handler reqSearch.open('GET', url, true); //go little request go reqSearch.send(null); } function gotSearchPage() { if (reqSearch.readyState == 4)//4 = complete { goToPageSequence(results.pages[selectedResult].sequence); } } function searchKeyword(keyword) { var txtSearchBISearch = document.getElementById('txtSearchBISearch'); txtSearchBISearch.value = keyword; doSearch(); }