// Script to add "autotab" functionality. When certain field are entered, the cursor
// automatically jumps to the next field.

<!-- Begin

//  To use:
//	onkeyup="return autoTab(this, <length of this field>, event);"
//  as in:
//  <input name="EmployeeSSN1" type="text" value="abc" onkeyup="return autoTab(this, 3, event);">


var isNN = (navigator.appName.indexOf("Netscape")!=-1);

// Actual autotab function
function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);

//		alert(input.form[(getIndex(input)+1) % input.form.length].type);
//		alert(input.form[(getIndex(input)+1) % input.form.length].name);
//		alert(input.form[(getIndex(input)) % input.form.length].name);
//		if next input field is type "hidden", the next line won't work.
//			Easy solution: change the order of the input fields in your HTML form.

		input.form[(getIndex(input)+1) % input.form.length].focus();


//	If next input field is text, then highlight any text already in it.
//	Can't do this on a drop-down, radio button, etc.
//	Check type of next field. Type is 'text' for text input boxes. 'select-one' for select boxes.

		if((input.form[(getIndex(input)+1) % input.form.length].type) == 'text')
			input.form[(getIndex(input)+1) % input.form.length].select();
}

function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
		if(arr[index] == ele)
			found = true;
		else
			index++;
			return found;
}

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;
	}
	return true;
}
//  End -->
