/**
 * @author:		randy johnson
 * 				highschoolalums.com
 * @date:		2008 02 14
 * @function:	this plugin makes certain that clicking either TAB or SHIFT+TAB moves to the next field
 * 				within the specified FORM - control does not get transferred out to a random browser field
 */
jQuery.fn.HSATabControl = function(params)
	{
	params = jQuery.extend(
		{
		formClass:		'tabControl',
		button:			null
		},params);
		
	return this.each(function()
		{
		a = [];
		var frm = $(this).attr('id');
		
// identify all form fields select,input[type!="hidden"],textarea,button
		$('#' + frm + ' :input:not(:hidden)').each(function()
			{
//			alert($(this).attr('id'));
			var tab = $(this).attr('tabindex');
			var id  = $(this).attr('id');
			
// only process the fields that have an id and a tabindex
			if (typeof(tab) != 'undefined' && typeof(id) != 'undefined')
				{
				a.push([$(this).attr('tabindex'),$(this).attr('id')]);
				
// if input field then we need to make certain everything gets selected
				if (this.tagName == 'INPUT')
					switch ($(this).attr('type'))
						{
						case 'button':
						case 'checkbox:':
						case 'file':
						case 'image':
						case 'radio':
						case 'reset':
						case 'submit':		break;
						
						case 'hidden':
						case 'password':
						case 'text':	   $('#' + id).bind('focus',function(e) {	this.select(); return true; });
												break;
						}
					
					
				$(this).bind('keydown',function(e)
					{
					var keycode = ($.browser.msie) ? window.event.keyCode : e.keyCode;
					if (keycode != 9) return true;       // tab keypress
//					alert('t[' + e.type + '] [' + $(this).attr('id'));
					
					e.preventDefault();
					$(this).triggerHandler('change');
					$(this).triggerHandler('blur');
					$(this).triggerHandler('focus');
				
					for (var i=0;i<a.length;i++) if (a[i][1] == this.id) break;
					
					do
						{
						i += (e.shiftKey) ? -1 : 1;
						if (i == a.length) i = 0;            // zero based
						if (i < 0) i = a.length-1;
						}
					while ($('#' + a[i][1]).attr('disabled'));
					
					$('#' + a[i][1]).focus();
					return false;						
					})
				}
			});
			
// make certain that focus is placed on the first field
		a.sort();
		if (a.count > 0) $('#' + a[0][1]).focus();
		return false;
		});
	}
