/* MediaPlus cmo33 Version cmo33B 08/11/06 10:25:44 delta js/autotab.js
** Copyright 2001 Publishing Business Systems, Inc.
** All Rights Reserved Worldwide.
*/
/* #167137 06.02.06 todd	Created based on function from JavaScript Source
**							www.javascriptsource.com
** js/autotab.js
** - Set a specific input field up to autotab when a specified number of 
**   characters are entered.  **  To use, call this as the "onKeyUp" event
**   handler for the fields desiring auto tab
**
** ARGUMENTS:
** input: field to apply autotab to
** len:   number of characters to autotab after
** event: keyup event that caused this function to be called.
** 
** -------------------------------------------------------------------------- */

// Browser type control variable
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

// Disable autocomplete in phone fields to avoid Firefox permission errors
function disablePhoneAutoComplete () {
	if (isNN) {
		var myform = document.forms[0];
		if (myform.phone1) {
			myform.phone1.setAttribute ('autocomplete', 'off');
		}	
		if (myform.phone2) {
			myform.phone2.setAttribute ('autocomplete', 'off');
		}
		if (myform.phone3) { 
			myform.phone3.setAttribute ('autocomplete', 'off');
		}
	}
}

// Cause an input field up to autotab after entry of specified # of characters
function autoTab(input, len, e) {

	// Determines if a given char is contained in an array of characters.
	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
			if(arr[index] == ele) {
				found = true;
			}
			else {
				index++;
			}
		return found;
	}

	// Determine index of a specified field (input)
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input) {
				index = i;
			}
			else { 
				i++;
			}
		return index;
	}

	// body of autoTab
	// get keyCode of key that was pressed
	var keyCode = (isNN) ? e.which : e.keyCode; 

	// Check for special characters that shouldn't cause autotab
	// Tab, Backspace, Page Up, Page Down, Home, End, Arrows, Del, Ins
	var filter = (isNN) ? [0,8,9,16,33,34,35,36,37,38,39,40,45,46] : 
						  [0,8,9,16,17,18,33,34,35,36,37,38,39,40,45,46];
	if (input.value.length >= len && !containsElement(filter,keyCode)) {

		// truncate entered value based on length limit
		input.value = input.value.slice(0, len);

		// Send focus to next field
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}

	return true;
}
