var downStrokeField;

/* AutoTab para campos Numéricos */

function autoTabNumber(fieldName,nextFieldName,fakeMaxLength) {
	
	var myForm = document.forms[document.forms.length - 1];
	var myField = myForm.elements[fieldName];
	
	myField.nextField = myForm.elements[nextFieldName];

	if (myField.maxLength == null) {		
	   myField.maxLength = fakeMaxLength;
	}

	myField.onkeydown = autoTabNumber_keyDown;	
	myField.onkeyup   = autoTabNumber_keyUp;
}

function autoTabNumber_keyDown() {
	
	this.beforeLength = this.value.length;
	downStrokeField = this;
}

function autoTabNumber_keyUp() {
	
	onlyNumbers(this);
	
	if ((this == downStrokeField) && 
		(this.value.length > this.beforeLength) && 
		(this.value.length >= this.maxLength)
		)	{ this.nextField.focus(); }
		
	downStrokeField = null;	
}

/* AutoTab para campos Caracteres */

function autoTabChar(fieldName,nextFieldName,fakeMaxLength) {
	
	var myForm = document.forms[document.forms.length - 1];
	var myField = myForm.elements[fieldName];
	
	myField.nextField = myForm.elements[nextFieldName];

	if (myField.maxLength == null) {		
	   myField.maxLength = fakeMaxLength;
	}

	myField.onkeydown = autoTabChar_keyDown;	
	myField.onkeyup   = autoTabChar_keyUp;
}

function autoTabChar_keyDown() {
	
	this.beforeLength = this.value.length;
	downStrokeField = this;
}

function autoTabChar_keyUp() {
	
	// onlyChars(this); Implementar depois
	
	if ((this == downStrokeField) && 
		(this.value.length > this.beforeLength) && 
		(this.value.length >= this.maxLength)
		)	{ this.nextField.focus(); }
		
	downStrokeField = null;	
}

function onlyNumbers(fieldForm) {
	
	var digits = "0123456789/"  
	var fieldFormTMP   

	for (var i = 0 ; i < fieldForm.value.length;i++) {
		
		fieldFormTMP = fieldForm.value.substring(i,i+1)   
	    
		if (digits.indexOf(fieldFormTMP)== -1) {  
			fieldForm.value = fieldForm.value.substring(0,i);  
        }
    }
}
