function putFocusInFirstForm()
{
	for ( var iLoopForms = 0; iLoopForms < document.forms.length; iLoopForms++ )
	{
		_form = document.forms[iLoopForms];

		for ( var iLoopElements = 0; iLoopElements < _form.elements.length; iLoopElements++ )
		{
			_element = _form.elements[iLoopElements];

			if ( _element.type != "text" &&
				 _element.type != "textarea" &&
				 _element.type != "file" &&
				 _element.type != "password" &&
				 _element.type != "radio" &&
				 _element.type.indexOf( "select" ) != 0 )
			{
				continue;
			}

			if ( _element.tabindex == -1 )
				continue;

			try
			{
				_element.focus();
				return;
			}
			catch( e )
			{
				// Fail gracefully
			}
		}
	}
}

var documentBody;

function waitCursor( element, timeout, throbber )
{
	// Disable for unit tests
	
	if ( navigator.userAgent.indexOf( "kennardconsulting.core.test" ) != -1 )
		return;
	
	try
	{
		if ( element && element.style )
			element.style.cursor = 'wait';
		
		// Capture document.body as an IE workaround
		
		documentBody = document.body;
		documentBody.style.cursor = 'wait';
	}
	catch( e )
	{
		// Fail gracefully
	}
	finally
	{
		self.setTimeout( function()
		{
			// After the click (setTimeout=0), disable it against another click
		
			if ( element )
				element.disabled = true;
		
			// Optionally show throbber and message
			
			if ( throbber )
			{
				var throbberDiv = document.getElementById( "throbber" );
				
				if ( throbberDiv )
				{
					// Deferred load so that IE animates it
					
					throbberDiv.innerHTML = '<img src="' + throbberDiv.title + '/media/throbber.gif"/><span>' + throbber + '</span>';
					throbberDiv.style.display = 'block';
				}
			}
				
			// Always set it back eventually, in case link
			// opened a new window
		
			if ( !timeout )
				self.setTimeout( function() { autoCursor( element ) }, 10000 );
			else if ( timeout > 0 )
				self.setTimeout( function() { autoCursor( element ) }, timeout );
		}, 0 );
	}
}

function autoCursor( element )
{
	try
	{	
		documentBody.style.cursor = 'auto';
		
		if ( element )
		{
			element.disabled = false;
			
			if ( element.style )
				element.style.cursor = 'auto';
		}

		var throbberDiv = document.getElementById( "throbber" );
		
		if ( throbberDiv )
			throbberDiv.style.display = 'none';
	}
	catch( e )
	{
		// Fail gracefully
	}
}
