/*  collectionBasics.html

Javascript functions associated with searching and displaying 
 collection specimens.
*/

// Drop-down list of ages, used in the search page paleobotanyCollection.html
function display_age()
{
	document.write("<select name='age' multiple size='4'>");
	document.write("<option value='all' selected>- All -</option>");
	document.write("<option value='Holocene'>Holocene</option>");
	document.write("<option value='Pleistocene'>Pleistocene</option>");
	document.write("<option value='Pliocene'>Pliocene</option>");
	document.write("<option value='Miocene'>Miocene</option>");
	document.write("<option value='Oligocene'>Oligocene</option>");
	document.write("<option value='Eocene'>Eocene</option>");
	document.write("<option value='Paleocene'>Paleocene</option>");
	document.write("<option value='Cretaceous'>Cretaceous</option>");
	document.write("<option value='Jurassic'>Jurassic</option>");
	document.write("<option value='Triassic'>Triassic</option>");
	document.write("<option value='Permian'>Permian</option>");
	document.write("<option value='Carboniferous'>Carboniferous</option>");
	document.write("<option value='Devonian'>Devonian</option>");
	document.write("<option value='Silurian'>Silurian</option>");
	document.write("<option value='Ordovician'>Ordovician</option>");
	document.write("<option value='Cambrian'>Cambrian</option>");
	document.write("</select>");
}

/* PASS VARIABLES BETWEEN PAGES */
// retrieve variables passed by "GET" method 
// Display search results, used in the results page paleobotanyResults.html
function displayCollectionResults(database, text)
{
	var results = new Array;
	results = parseCollectionResults(database, text);
	var numResults = results.length - 1;
	document.write("<b>Number of Results: " + numResults + "</b><BR>");
	var i;
	var field;
	document.write("<table border=1 cellpadding=4 align='center'>");
	for(i=0; i<results.length; i++)
	{
		document.write("<tr>");
		for(field in results[i])
		{
			if(field != "taxon")
			{
				if((results[i][field] != "") && (results[i][field] != "<i></i>"))
				{
					document.write("<td class='small'>" + results[i][field] + "</td>");
				}
				else
				{
					document.write("<td class='small'>&nbsp;</td>");
				}
			}
		}
		document.write("</tr>");
	}
	document.write("</table>");
}

/* 
Based on the collection database send to the appropriate function
 (only paleobotany so far available).
*/
function parseCollectionResults(database, text)
{
	var searchTerms = new Array;
	var results = new Array;
	switch(database)
	{
		case top.paleobotDB:
			searchTerms = parsePaleobot(text);
			results	= getResults(top.paleobotDB, searchTerms);
			results[0] = top.paleobotDB['titles'];								
			break;
		case top.invertDB:
			break;
		case top.vertDB:
			break;
	}
	return(results);
}

// Parse search terms and place in an array
function parsePaleobot(text)
{
	var tempSearchTerms = {family: "", age: "", genus: "", formation: "", species: "", locality: "", specimen: "", author: "", preservat: "", reference: "", status: "", organ: ""};
	var searchTerms = new Array();

	// parse passed variables into arrays
	var start=0;
	var temp="";
	for(field in tempSearchTerms)
	{
		var first = text.indexOf(field+"=", start);
		if(first != -1)
		{
			first = text.indexOf("=", start);
			start = first+1;
			var last = text.indexOf("&", start);
			temp = text.slice(start, last);
			temp = temp.replace("+", " ");
			if(temp != "")
			{
				searchTerms[field] = temp;
			}
		}
	}
	return(searchTerms);
}

// Create an array containing all the specimens that matched the search parameters.
function getResults(database, searchTerms)
{
	var results = new Array;
	var counting = 1;
	for(entry in database)
	{
		if(entry != 'titles')
		{
			if(testStatement(database, searchTerms, entry) == true)
			{
				results[counting] = database[entry];
				counting++;
			}
		}
	}
	return(results);
}

/*
Test each entry in the database to see if it matches the
 search requirements.
*/
function testStatement(database,searchTerms, entry)
{
	var finalResult = false;
	var emptyCount=0;
	var totalCount=0;
	for(field in searchTerms)
	{
		if((database[entry][field].indexOf(searchTerms[field]) != -1) || (searchTerms[field] == "") || (searchTerms[field] == "all"))
		{ 
			emptyCount++;
		}
		totalCount++;
	}
	if(totalCount <= emptyCount)
	{
		finalResult = true;
	}
	return(finalResult);
}
