/*
 *
 */
window.onload = function ()
{
	window.loaded = true;
	if (document.captureEvents) {
		document.captureEvents(Event.KEYUP);
		document.onkeyup = ns4_key;
	}
	window.allFish = new Array();
	for (n=0; n < window.fish.length; n++) {
		window.allFish[n] = new Option(window.fish[n].name, window.fish[n].id);
	}
}

/*
 * Netscape 4 hackery to catch keyup event
 */
function ns4_key(e)
{
	if (window.loaded) {
		if (document.forms["search"]) {
			findFish(document.forms["search"]);
		}
	}
}

/*
 *
 */
function validate(theForm)
{
	foundFish = findFish(theForm);
	//if (foundFish == true) {
	//	return true;
	//} else {
	//	return false;
	//}
}

function selectFish(event)
{
	if(document.forms["commonnameForm"].fish_id.value == '') {
		return true; // If no Fish ID, return... Added SK 8th Sept 2008.
	}
	
	submitNow = false;
	theTime = new (Date);
	theTime = theTime.getTime();
	ddl = document.forms["commonnameForm"].fish_id;
	if (event == 'onmousedown') {
		if (window.mouseDownTime == undefined) {
			window.mouseDownTime = theTime;
		}
	}
	if (event == 'onmouseup') {
		elapsedTime = theTime - window.mouseDownTime;
		if (elapsedTime > 500) {
			submitNow = true;
		}
	}
	if (event == 'onchange') {
		submitNow = true;
	}
	if ((window.nameSubmitted != true) && (submitNow == true)) {
		window.nameSubmitted = true;
		document.forms["commonnameForm"].submit();
	}
}

/*
 *
 */
function findFish(theForm)
{
	freeTextQuery = theForm.q.value.toLowerCase();
	fish = window.fish;
	found = false;
	refinementArray = new Array ();
	if (freeTextQuery != "") {
		for (n=0; n < fish.length & !found; n++) {
			fishname = fish[n].name.toLowerCase();
			if (freeTextQuery == fishname) {
				refinementArray[refinementArray.length] = new Option(fish[n].name, fish[n].id);
				found = true;
			} else {
				if (fishname.indexOf(freeTextQuery) != -1) {
					refinementArray[refinementArray.length] = new Option(fish[n].name, fish[n].id);
				}
			}
		}
	}
	showRefinements(theForm, refinementArray);
	if (found) {
		return true;
	} else {
		return false;
	}
}

/*
 *
 */
function showRefinements(theForm, refinementArray)
{
	commonNameForm = document.forms["commonnameForm"];
	commonNameForm.fish_id.options.length = 0;
	if (refinementArray.length > 0) {
		for (n=0; n < refinementArray.length; n++) {
			commonNameForm.fish_id.options[n] = refinementArray[n];
		}
		theForm.fish_id.value = refinementArray[0].value;
		commonNameForm.fish_id.options[n] = new Option("", 0);
		commonNameForm.fish_id.options[n].disabled = true;
		for (allFishIndex=0; allFishIndex < window.allFish.length; allFishIndex++) {
			commonNameForm.fish_id.options[n + 1 + allFishIndex] = window.allFish[allFishIndex];
		}
		commonNameForm.fish_id.options[0].selected = true;
	}
}

/*
 *
 */
function populateSearch(theForm)
{
	theForm.q.value = theForm.refine.options[theForm.refine.options.selectedIndex].text;
	theForm.fish_id.value = theForm.refine.options[theForm.refine.options.selectedIndex].value;
	findFish(theForm);
}

/*
 *
 */
function sci_sort(a, b)
{
   if (a.scientific_name < b.scientific_name)
	  return -1
   if (a.scientific_name > b.scientific_name)
	  return 1
   // a must be equal to b
   return 0
}

/*
 *
 */
function swapNameType (theForm)
{
	fish = window.fish;
	if (!fish) {
		window.status = 'Debug: Fish data not loaded';
		return true;
	} else {
		selected_id = theForm.fish_id.options[theForm.fish_id.selectedIndex].value;
		theForm.fish_id.options.length = 0;
		if (theForm.name_type.checked == 1) {
			if (window.f_sci_sorted == undefined) { window.f_sci_sorted = fish.sort(sci_sort); }
			f = window.f_sci_sorted;
		} else {
			f = fish;
		}
		for (n=0; n < f.length; n++) {
			fish_id = f[n].id;
			if (theForm.name_type.checked == 1) {
				fish_name = f[n].scientific_name;// + ' (' + f[n].name + ')';
			} else {
				fish_name = f[n].name;
			}
			if (selected_id == fish_id) {
				selectedIndex = n; // save for later hack
				defaultSelected = true;
			} else {
				defaultSelected = false;
			}
			theOption = new Option(fish_name, fish_id, defaultSelected); //defaultSelected not working in IE5, hence hack below
			theForm.fish_id.options[theForm.fish_id.options.length] = theOption;
		}
		// Just for IE5:
		if (theForm.fish_id.options.selectedIndex != selectedIndex); {
			theForm.fish_id.options.selectedIndex = selectedIndex;
		}
	}
}
