/* $Id: tbsearch.js,v 1.1 2009/08/27 02:07:37 kharris Exp $ */
function processTBSearchForm (e)
{
	var obj = null;
	if ( e.srcElement != null )
	{
		obj = e.srcElement;
	}
	else
	{
		obj = e.target;
	}
	if ( obj != null )
	{
		stopEvent (e);
		// back up from the href to the input field to get the form.
		var myForm = '';
		if ( e.type == "submit" )
		{
			myForm = document.getElementById("horsesearch");
		}
		else
		{
			myForm = obj.previousSibling.form;
		}
		var process = true;
		if ( myForm != null )
		{
			var myAction = myForm.getAttribute ( "action" );
			if ( myForm["tb_rider"].value != "" )
			{
				myAction = myAction.replace ( /horses\./, 'riders.' );
				myForm.setAttribute ( "action", myAction );
			}
			else if ( myForm["tb_handler"].value != "" )
			{
				myAction = myAction.replace ( /horses\./, 'handlers.' );
				myForm.setAttribute ( "action", myAction );
			}
			// Remove the event to prevent double submission
			removeEvent( myForm, "submit", processTBSearchForm, false );
			myForm.submit ();
		}
	}
}

function setSearchHandlers (e)
{
	var myForm = document.getElementById("horsesearch");
	if ( myForm != null )
	{
		removeEvent( myForm, "submit", processTBSearchForm, false );
		myForm.onsubmit = function () { return false };
		addEvent ( myForm, "submit", processTBSearchForm, false );
		
		var tbSrchBtn = document.getElementById("srchTBSubLnk");
		if ( tbSrchBtn != null )
		{
			tbSrchBtn.onclick = function () { return false };
			addEvent ( tbSrchBtn, "click", processTBSearchForm, false );
		}
	}
}

addEvent ( window, "load", setSearchHandlers, false );