// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. 
// If you wish to share this code with others, please just point them
// to the URL instead.
// Please DO NOT link directly to my .js files from your site. Copy
// the files to your server and use them there. Thank you.
// ===================================================================

//-------------------------------------------------------------------
// getElementIndex(input_object)
//   Pass an input object, returns index in form.elements[] for the object
//   Returns -1 if error
//-------------------------------------------------------------------
function getElementIndex( obj )
{
	var theform = obj.form;
	for ( var i=0; i < theform.elements.length; ++i )
	{
		if ( obj.name == theform.elements[i].name )
			return i;
	}
	return -1;
}

// -------------------------------------------------------------------
// tabNext(input_object)
//   Pass an form input object. Will focus() the next field in the form
//   after the passed element.
//   a) Will not focus to hidden or disabled fields
//   b) If end of form is reached, it will loop to beginning
//   c) If it loops through and reaches the original field again without
//      finding a valid field to focus, it stops
// -------------------------------------------------------------------
function tabNext(obj)
{
	if (navigator.platform.toUpperCase().indexOf("SUNOS") != -1)
	{
		obj.blur(); return; // Sun's onFocus() is messed up
	}
	
	var theform = obj.form;
	var i = getElementIndex( obj );
	var j = i + 1;
	if ( j >= theform.elements.length ) { j=0; }
	if (i == -1) { return; }
	while (j != i)
	{
		if ((theform.elements[j].type!="hidden") && (theform.elements[j].name != theform.elements[i].name) && (!theform.elements[j].disabled))
		{
			theform.elements[j].focus();
			break;
		}
		j++;
		if (j >= theform.elements.length) { j=0; }
	}
}



//-------------------------------------------------------------------
// getElementIndex(input_object, event)
//   Pass an input object, if the text limit of the object has been reached, move to the next one
//-------------------------------------------------------------------
function autoAdvance(btn, event)
{
	if ( document.all )
	{
		var obj = eval( 'document.all.' + btn );
		
		event.returnValue=false;
		event.cancel = true;
		
		if ( obj.value.length >= obj.maxLength )
			tabNext( obj );
	}
	else if ( document.getElementById )
	{
		var obj = document.getElementById( btn );
		
		event.returnValue=false;
		event.cancel = true;
		
		if ( obj.value.length >= obj.maxLength )
			tabNext( obj );
	}
	else if ( document.layers )
	{
		event.returnValue=false;
		event.cancel = true;

		if ( btn.value.length >= btn.maxLength )
			tabNext( btn );
	}
}
