/**
	Closes the advanced search area and sets the hidden form field 'expanded'
	to reflect the state of the advanced search area.
*/
function advSearchClose() {
	changeBGImageSrc('advSearchBar', '../images/advsearchbar_closed.gif');
	getObjectRef('expanded').value = 0;
}

/**
	Opens the advanced search area and sets the hidden form field 'expanded'
	to reflect the state of the advanced search area.
*/
function advSearchOpen() {
	changeBGImageSrc('advSearchBar', '../images/advsearchbar_downarrow.gif');
	getObjectRef('expanded').value = 1;
}

/**
	Toggles the expanded state of the advanced search area.
*/
function advSearchToggle() {
	if (getObjectRef('expanded').value == 1) advSearchClose();
	else advSearchOpen();
}

/**
	Sets the background image of the advanced search header bar to a 'hover' image.
*/
function advSearchOver() {
	if (getObjectRef('expanded').value == 0) changeBGImageSrc('advSearchBar', '../images/advsearchbar_closed_hover.gif');
}

/**
	Resets the background image of the advanced search header bar.
*/
function advSearchOut() {
	if (getObjectRef('expanded').value == 0) changeBGImageSrc('advSearchBar', '../images/advsearchbar_closed.gif');
}

/**
	Initializes the advanced search area to be open/closed based on the current setting of
	the hidden form field 'expanded'
*/
function initializeAdvSearch() {
	if (getObjectRef('expanded').value == 1) advSearchOpen();
	else advSearchClose();
}

/**
	Makes an AJAX call to get all subgenres of the currently selected genre in the
	search form and then populates the subgenre select box.
*/
function displaySubgenres(subGenre) {
	subjectCode = getOptionValue('select_genre');
	//doGetHttpRequest("manageSubgenres.php", "subjectCode="+subjectCode+"&subGenre="+subGenre+"&func=getSubgenres", f_displaySubgenres);
	var ajax = new AjaxRequest();
	ajax.doGetHttpRequest("manageSubgenres.php", "subjectCode="+subjectCode+"&subGenre="+subGenre+"&func=getSubgenres", function() {
		fillElementHTML('select_subgenre', ajax.getResp());
	});
	/*
	var ajax = new GoAjax();
	ajax.setUrl("manageSubgenres.php");
	ajax.setFunc("getSubgenres");
	ajax.setParams("subjectCode="+subjectCode+"&subGenre="+subGenre);
	ajax.setElement('select_subgenre');
	ajax.start();
	*/
}
var f_displaySubgenres = function() {
	fillElementHTML('select_subgenre', getResp());
}

/**
	Builds a string representation of the search query currently being built based on the
	state of the search form and then sets the innerHTML of the 'advSearchQueryString' span
	to the string.
*/
function advSearchToString() {
	buyablePage = (getObjectRef('chk_hideOutOfStock')) ? true : false;

	str = ' ';
	title = getObjectRef('txt_searchTitle').value;
	author = getObjectRef('txt_searchAuthor').value;
	keyword = getObjectRef('txt_searchKeyword').value;
	genreLabel = wrapSpan(getOptionHTML('select_genre'), "black");
	sortedBy = wrapSpan(getOptionHTML('select_sortBy'), "black");

	str += wrapSpan(((title.length > 0) ? title : "Any book"), "black");
	str += " by " + wrapSpan(((author.length > 0) ? author : "any author"), "black");
	if (keyword.length > 0) str+= " containing " + wrapSpan(keyword, "black");
	if (getOptionIndex('select_genre') !== 0) str += " within "+genreLabel;
	str += " sorted by "+sortedBy;	
	
	str2 = ' ';
	abridged = (getOptionValue('select_abridged') != 'both') ? getOptionValue('select_abridged') : '';
	abridgedSpan = wrapSpan(abridged, "black");
	
	if (buyablePage) {
		media = getOptionValue('select_media');
		mediaSpan = wrapSpan(media+"s", "black");
		if ((getCheckbox('chk_hideOutOfStock')) && (getCheckbox('chk_hideLowStock'))) str2 += wrapSpan("hide low and out of stock", "black") + " items";
		else if (getCheckbox('chk_hideOutOfStock')) str2 += wrapSpan("hide out of stock", "black") + " items";
		else if (getCheckbox('chk_hideLowStock')) str2 += wrapSpan("hide low stock", "black") + " items";
		if (media != 'both') str2 += " showing " + abridgedSpan + " " + mediaSpan + " only";
		else if (abridged != '') str2 += " showing "+abridgedSpan+" titles only";
	}
	else if (abridged != '') str2 += " showing "+abridgedSpan+" titles only";
	
	getObjectRef('advSearchQueryString').innerHTML = str.toLowerCase() + "<br />" + str2.toLowerCase();
}

