/* Copied from 
    http://www.ncbi.nlm.nih.gov/books/bookres.fcgi/system_default/search.js
    to books-in-pmc js renderer area.
 
    Must be updated to produce (according to Jira issue PMC-100):
        BOOK_BASE  = http://web.ncbi.nlm.nih.gov/sites/PMCBooks@1.5?
           (we add 'db=books')
        PUBMED_BASE=http://www.ncbi.nlm.nih.gov/sites/entrez?
           (we add 'db=PubMed')
    
    SEARCH_STRING - search string from the input box
    BOOK_NAME - book abbrev
    
    * Search all books:
        $BOOK_BASE
            &cmd=Search
            &term=$SEARCH_STRING
            &doptcmdl=Books
        For example:
        http://web.ncbi.nlm.nih.gov/sites/PMCBooks@1.5
            ?db=books&cmd=Search&term=cell&doptcmdl=Books
        ??? http://www.ncbi.nlm.nih.gov/sites/entrez
            ?db=Books&cmd=Search&term=cell&doptcmdl=Books
    
    * Seach this book:
        $BOOK_BASE
            &cmd=Search
            &term=$SEARCH_STRING AND BOOK_NAME[book]
            &doptcmdl=TOCView
        for example
        http://web.ncbi.nlm.nih.gov/sites/PMCBooks@1.5
            ?db=books&cmd=Search&term=cell%20AND%20arev%5Bbook%5D&doptcmdl=TOCView
    
    * Search PubMed:
        $PUBMED_BASE
            &cmd=Search
            &term=$SEARCH_STRING
            &doptcmdl=Books 
*/

// Process the search form on a book page. Triggered by submitting frmSearch form.
// Check type of search, and load values into the 'frmGo' form, then submit it.
//
// The forms are generated by book-part/main.xsl/searchbox.
//
// Form 'frmSearch' (called 'f1' below since it's the first one to happen!) has:
//     A text box named 'f1_term'
//     A set of 3 radio buttons named 'f1_sbook'
//     A button named 'f1_search'
//
// Form 'frmGo' has fields and defaults to match Entrez query parameters
//     @action=(error message, since we always set it from here)
//     db=Books
//     cmd=Search
//     term=.
//     doptcmdl=Books
// 
// Warning: HTTP GET is ASCII-only.
//

function Process(bookID,bookBase,pubmedBase)
{
	//alert("In Process, bookID '"+bookID+"', bookBase '"+bookBase
	//	  +"', pubmedBase '"+pubmedBase+"'");

	// Default the domains for book and pubmed searches, unless passed in
	// via the pmcrender.ini file.
	if (bookBase == "") {
		bookBase = "http://web.ncbi.nlm.nih.gov/sites/PMCBooks@1.6?";
		//alert ("Didn't receive bookBase, defaulting to: " + bookBase);
	}
	if (pubmedBase == "") {
		pubmedBase = "http://www.ncbi.nlm.nih.gov/sites/entrez?";
		//alert ("Didn't receive pubmedBase, defaulting to: " + pubmedBase);
	}
	bookBase = bookBase + "db=books";
	pubmedBase = pubmedBase + "db=PubMed";

	// Fill out the hidden search form
    var f1 = document.forms['frmSearch'];                  // f is user's form
    var fg = document.forms['frmGo'];                     // fg is hidden

	//alert("first printing: f1.f1_term.value: " + f1.f1_term.value);
	var xxx = f1.f1_term.value;
	//alert("xxx " + xxx);
	if (f1.f1_term.value == "") {
		alert("No search term(s) entered.");
		return(false);
	}
	
	// ----- SK -----
	arg_log = "log$";
	arg_book = "bname";
	
	// remove elements to avoid duplication
	if (document.getElementById(arg_log))
		fg.removeChild(document.getElementById(arg_log))
	if (document.getElementById(arg_book))
		fg.removeChild(document.getElementById(arg_book))

	var input_log = document.createElement("input");

	input_log.name = arg_log;
	input_log.id = arg_log;
	input_log.type = "hidden";
	// -----	-----
	
	// WARNING: Assigning shorthand, like 'fg.cmd = "Search"', works in Firefox
	// and *appears* to work in IE, but in IE it makes it impossible to do a
	// following reference to f1.f1_term.value (yes, really!)
	//    
    if( f1.f1_sbook[1].checked == true ) {                  // Search all books
		//alert("search all books");
		fg.setAttribute("action",bookBase);
		fg.db.value = "Books";
		fg.cmd.value = "Search";
		fg.term.value = f1.f1_term.value;
        fg.doptcmdl.value="Books";

		input_log.value = "bookall";	//	SK
    }
    
    
//CHANGE ML (02/9) Added further statement to account for collection search
    
	else if( f1.f1_sbook[0].checked == true) {
		
		fg.setAttribute("action",bookBase);
		fg.db.value = "Books";
		fg.cmd.value = "Search";
		
		if( bookID.match("collect") != null) {		// Search this searies
			fg.term.value = f1.f1_term.value + " AND " + bookID + "[filter]";
			fg.doptcmdl.value="Books";
		}
		else {										// Search this book
			fg.term.value = f1.f1_term.value + " AND " + bookID + "[book]";
			fg.doptcmdl.value="TOCView";
		}

		input_log.value = "booksrch";	//	SK
	}
    else {                                                // Search all PubMed
		//alert("search pubmed");
		fg.setAttribute("action",pubmedBase);
		fg.db.value = "PubMed";
		fg.cmd.value = "Search";
		fg.term.value = xxx;
        fg.doptcmdl.value="Books";

		input_log.value = "bookpubmed";	//	SK
    }

	// ----- SK -----
	fg.appendChild(input_log);
	
	var input_from = document.createElement("input");

	input_from.name = arg_book;
	input_from.id = arg_book;
	input_from.type = "hidden";
	input_from.value = bookID;
	fg.appendChild(input_from);
	// -----	-----

	//alert("About to submit. fg.db '" + fg.db.value
	//	  + "', fg.cmd '"      + fg.cmd.value + "', fg.term '"     + fg.term.value
	//	  + "', fg.doptcmdl '" + fg.doptcmdl.value + "'.");

	// Send the form to do the search
    fg.submit();
    return false;
}


// Trap enter key and call Process() as needed.
function KeyPress(bookID,e,bookBase,pubmedBase)
{
	if (document.forms['frmSearch'].f1_term.value == "") {
		return(false);
	}
     var nav = ( navigator.appName == "Netscape" ) ? true : false;
     var msie = ( navigator.appName.indexOf("Microsoft") != -1 ) ? true : false;
     var k = 0;
     if( nav ) { k = e.which; }
     else if( msie ) { k = e.keyCode; }
     if( k==13 ) Process(bookID,bookBase,pubmedBase);
}


// Following used in Bookshelf version; possibly can be deleted now?
function BVShow(url)
{
    location = '/books/bv.fcgi?rid=' + url;
}
