<!--
// expandType( inventorytypecategoryid )
// Input: an inventory type category id number
// Return: none
// This function fills the category information DIV with subcategories
//	for a particular inventory type category.
function expandType( inventorytypecategoryid )
{
/*	var typeCategoryAjax = new AU3Ajax();
	var request = 'inventorytypecategoryid=' 
			+ encodeURIComponent( inventorytypecategoryid );
	typeCategoryAjax.evaluate( 'search/ajax_getTypeCategory.php', request );*/
	expandCategory( inventorytypecategoryid );
}

// expandCategory( inventorycategoryid[, addCrumb] )
// Input: an inventory category id number;
//			optionally, a boolean to determine if a breadcrumb should be
//				added for this category, default TRUE
// Return: none
// This function either hides a visible category div, or shows the DIV,
//	using AJAX to fill it with appropriate drill-down contents.
function expandCategory( inventorycategoryid, addCrumb )
{
	if (addCrumb == undefined)
	{
		addCrumb = 'true';
	}
		var categoryAjax = new AU3Ajax();
		var request = 'inventorycategoryid=' 
			+ encodeURIComponent( inventorycategoryid )
			+ '&addCrumb=' + encodeURIComponent(addCrumb);
		categoryAjax.evaluate( 'search/ajax_getCategory.php', request );
}

// getModels( inventorytypeid, manufacturerid )
// Input: an integer inventorytypeid;
//			an integer manufacturerid
// Return: none
// This function stocks the model list of a particular inventory type
//	with the appropriate models.
function getModels( inventorytypeid, manufacturerid )
{
	var modelAjax = new AU3Ajax();
	var request = 'inventorytypeid='
		+ encodeURIComponent( inventorytypeid )
		+ '&manufacturerid='
		+ encodeURIComponent( manufacturerid );
	modelAjax.evaluate( 'search/ajax_getModels.php', request );
}

// doSearch
function doSearch( offset, limit )
{
	// Don't do anything if we'd be processing a blank search
	if (document.getElementById('keywordsearch').value == '')
	{
		return;
	}
	var searchAjax = new AU3Ajax();
	var request = 'keywords='
		+ encodeURIComponent( document.getElementById('keywordsearch').value );
	if (offset != undefined)
	{
		if (request != '')
		{
			request +='&';
		}
		request += 'offset=' + encodeURIComponent( offset );
	}
	if (limit != undefined)
	{
		if (request != '')
		{
			request +='&';
		}
		request += 'limit=' + encodeURIComponent( limit );
	}
	if (document.getElementById('inventoryType') != null)
	{
		showElement( 'inventoryType' );
		document.getElementById('searchCategory').innerHTML='';
		searchAjax.evaluate( 'search/ajax_search.php', request );
	}
	else
	{
		location.href = 'search.php?doSearch='
			+ encodeURIComponent( 
					document.getElementById('keywordsearch').value);
	}
}

function doCrumbSearch( offset, limit )
{
	var searchAjax = new AU3Ajax();
	var request = '';
	if (document.getElementById('searchWithinKeywords') != undefined
			&& document.getElementById('searchWithinKeywords') != '')
	{
		request += 'keywords=' 
			+ encodeURIComponent( 
					document.getElementById('searchWithinKeywords').value );
	/*	if (document.getElementById('keywordsearch').value != '')
		{
			request += encodeURIComponent( ' ' 
						+ document.getElementById('keywordsearch').value );
		}*/
	}
/*	else if (document.getElementById('keywordsearch').value != '')
	{
		request += 'keywords='
			+ encodeURIComponent( ' ' 
					+ document.getElementById('keywordsearch').value );
	}*/
	if (offset != undefined)
	{
		if (request != '')
		{
			request +='&';
		}
		request += 'offset=' + encodeURIComponent( offset );
	}
	if (limit != undefined)
	{
		if (request != '')
		{
			request +='&';
		}
		request += 'limit=' + encodeURIComponent( limit );
	}
	searchAjax.evaluate( 'search/ajax_searchbybreadcrumbs.php', request );
}

// this function sets the manufacturer and model select lists and stocks 
//	the breadcrumbs appropriately, as well as performing a breadcrumb search.
function fullSearch( inventorytypeid, manufacturerid, manufacturername, 
		modelid, modelname, searchrank )
{
	// set the dropdown lists correctly
	var manSelect = document.getElementById('manufacturer');
	var modSelect = document.getElementById('modelid' );
	var i;
	for (i = 0; i < manSelect.options.length; ++i)
	{
		if (manSelect.options[i].value == manufacturerid)
		{
			manSelect.selectedIndex = i;
			break;
		}
	}
	// add the breadcrumbs
	addBreadcrumb( searchrank++, manufacturername, "manufacturerid",
			manufacturerid, true );
	setTimeout( 'addBreadcrumb( ' + searchrank + ', "' + modelname 
				+ '", "modelid", ' + modelid + ', '
				+ 'true );', 80 );
	// refresh important data
	setTimeout( 'getModels( ' + inventorytypeid + ', ' + manufacturerid + ');',
		   	160 );
	setTimeout( 'getItemCategories();getTypeData();loadInterchangeData();',
			180 );
	// do the search
	setTimeout( 'updateBreadcrumbs();doCrumbSearch();', 210 );
}

// update breadcrumbs
function updateBreadcrumbs()
{
	var updateBreadcrumbsAjax = new AU3Ajax();
	updateBreadcrumbsAjax.loadInto( 'search/ajax_displaybreadcrumbs.php', null,
			'breadcrumbs');
}

// add a breadcrumb to the breadcrumbs list
function addBreadcrumb( rank, title, searchtype, searchoption, overwrite,
	   autosearch )
{
	var addBreadcrumbsAjax = new AU3Ajax();
	var request = 'rank=' + encodeURIComponent( rank )
		+ '&title=' + encodeURIComponent( title )
		+ '&searchtype=' + encodeURIComponent( searchtype )
		+ '&searchoption=' + encodeURIComponent( searchoption )
		+ '&overwrite=' + encodeURIComponent( overwrite );
	addBreadcrumbsAjax.execute( 'search/ajax_addcrumb.php', request );
	setTimeout( "updateBreadcrumbs();", 60 );
	if (autosearch == undefined || autosearch != false)
	{
		setTimeout( "doCrumbSearch();", 90 );
	}
}

// remove a specific breadcrumb from the breadcrumbs list
function removeBreadcrumb( crumbid )
{
	var removeBreadcrumbsAjax = new AU3Ajax();
	var request = 'crumbid=' + encodeURIComponent( crumbid );
	removeBreadcrumbsAjax.evaluate( 'search/ajax_removecrumb.php', request );
	setTimeout( "updateBreadcrumbs();", 60 );
//	doCrumbSearch();
}

// add or remove a breadcrumb.  This function will search for the 
//	breadcrumb by its content to decide the behavior
function toggleBreadcrumb( rank, title, searchtype, searchoption, overwrite )
{
	var toggleBreadcrumbsAjax = new AU3Ajax();
	var request = 'rank=' + encodeURIComponent( rank )
		+ '&title=' + encodeURIComponent( title )
		+ '&searchtype=' + encodeURIComponent( searchtype )
		+ '&searchoption=' + encodeURIComponent( searchoption )
		+ '&overwrite=' + encodeURIComponent( overwrite );
	toggleBreadcrumbsAjax.execute( 'search/ajax_togglecrumb.php', request );
	setTimeout( "updateBreadcrumbs();doCrumbSearch();", 60 );
}

// get the item category (USED, CORE, etc..) options
function getItemCategories()
{
	var itemCategoryAjax = new AU3Ajax();
	itemCategoryAjax.loadInto( 'search/ajax_getItemCategories.php', null, 
			'itemcategory' );
}


function getTypeData()
{
	var typeDataAjax = new AU3Ajax();
	typeDataAjax.loadInto( 'search/ajax_getTypedata.php', null, 'typedata' );
}

function getMoreTypedata( typedataid )
{
	// fetch the remaining typedata entries for the specified typedata id
	//	for this inventory type
	if (document.getElementById( 'moreTypedata-' + typedataid).style.display
			== "none" )
	{
		document.getElementById( 'moreTypedataControl-' + typedataid ).innerHTML
			= "(-) Less";
		document.getElementById( 'moreTypedata-' + typedataid ).style.display
			= "inline";
		var typeDataAjax = new AU3Ajax();
		var request = 'typedataid=' + encodeURIComponent( typedataid );
		typeDataAjax.evaluate( 'search/ajax_getMoreTypedata.php', request );
	}
	else
	{
		document.getElementById( 'moreTypedata-' + typedataid ).style.display
			= "none";
		document.getElementById( 'moreTypedataControl-' + typedataid ).innerHTML
			= "(+) More";
	}
}

// functions to hide and unhide search elements based on id
function hideElement( elementid )
{
	document.getElementById( elementid ).style.display = 'none';
}

function showElement( elementid )
{
	document.getElementById( elementid ).style.display = 'block';
}

// this function toggles a checkbox to be checked or unchecked, the 
//	opposite of whatever it is now.
function toggleCheckbox( checkboxid )
{
	var checkbox = document.getElementById( checkboxid );
	if (checkbox.checked)
	{
		checkbox.checked = false;
	}
	else
	{
		checkbox.checked = true;
	}
}

// loadInterchangeData( [year] )
// Input: Optionally, a year to get interchange data for
function loadInterchangeData( year )
{
	var request = '';
	if (year != undefined)
	{
		request += 'year=' + encodeURIComponent( year );
	}
	var interchangeAjax = new AU3Ajax();
	interchangeAjax.evaluate( 'search/ajax_loadInterchange.php', request ); 
}

// getFeaturedItems()
// Input: none
// This function fills the search results container element with
//	the featured items appropriate to the typecategoryid or inventorytypeid
//	specified.
function getFeaturedItems()
{
	var featuredAjax = new AU3Ajax();
	featuredAjax.loadInto( 'search/ajax_getFeaturedItems.php', '', 
			'searchresults' );
}

// clearLastSearch()
// Input: None
// This function clears the last search data to get a "fresh search."
function clearLastSearch()
{
	var lastSearchAjax = new AU3Ajax();
	lastSearchAjax.execute( 'search/ajax_clearLastSearch.php', '' );
}

// newSearchNavigate( url )
// Input: a URL to navigate to
// This function clears the last search, then navigates to a URL after a 
//	short delay
function newSearchNavigate( url )
{
	clearLastSearch();
	setTimeout( "location.href='" + url + "';", 80 );
}

// handleKeypress( evt )
// Input: a KeyPress event
// This function handles what happens when a keypress is received.  It really
//	only cares about the enter key, but it has to decide intelligently
//	if it needs to do a crumb search or a keyword only search
function handleKeypress( evt )
{
	var e = evt || window.event;
	var code = e.charCode || e.keyCode;
	// we really only care about the enter key
	if (code == 13)
	{
		// our behavior depends on two things -- first, what page we're
		//	on.
		if (location.href.indexOf( 'search.php' ) != -1)
		{
			// we're on a search page, see if our focus is in the top
			//	search box
			if (document.activeElement 
					== document.getElementById('keywordsearch') )
			{
				setTimeout( "doSearch();", 30 );
			}
			else
			{
				setTimeout( "doCrumbSearch();" , 30 );
			}
		}
		else if (location.href.indexOf( 'request.php' ) != -1)
		{
			// we're on the a request form.  Because data validation
			//	appears to be not easy to automatically invoke, simulate
			//	pressing the submit button and let that handle data
			//	validation.
			if (document.activeElement 
					== document.getElementById('keywordsearch') )
			{
				// If our focus is in the top search box, still search
				setTimeout( "doSearch();", 30 );
			}
			else
			{
				// simulate the click
				document.getElementById("Submit").click();
			}
		}
		else
		{
			// if we have any keywords to search by
			if (document.getElementById('keywordsearch').value != '')
			{
				// we're on a page that isn't a search page, just do a full 
				//	keyword-only search
				location.href = 'search.php?doSearch='
					+ encodeURIComponent( 
							document.getElementById('keywordsearch').value);
			}
		}
	}
}
document.onkeypress = handleKeypress;

// do any automatic searching
function autoSearch( searchOptions )
{
	var lastSearchAjax = new AU3Ajax();
	var request = 'searchoptions=' + encodeURIComponent( JSON.stringify(searchOptions) );
	lastSearchAjax.evaluate( 'search/ajax_autosearch.php', request );
}
// -->

