// JavaScript Document

function enterPorTab(event, form, field, nextfocus, minval, maxval) {
	//   9 - Tab
	//  13 - Enter
	if (event.keyCode != 9 && event.keyCode != 13 || event.keyCode == 9 && event.shiftKey)
		return;
		
	document.forms[form].elements[nextfocus].focus();
	event.cancelBubble = true;
	event.returnValue = false;
	return false;
}

function perdeuFoco(form, field, minval, maxval) {
	if (!validaNumero(form, field, minval, maxval)) {
		var campo = document.forms[form].elements[field];
		campo.focus();
		if (campo.type.toLowerCase() == "text")
			campo.select();
		event.cancelBubble = true;
		event.returnValue = false;
		return false;
	}
}

function validaNumero(form, field, minval, maxval) {
	var campo = document.forms[form].elements[field];
	var value = parseInt(campo.value);
	if (value < minval || value > maxval) {
		 window.alert("O numero minimo de unidades para este produto e " + minval  );
		//window.alert("O número mínimo de unidades para este produto é de: " + minval + " e " + maxval);
		return false;
	}
	return true;
}

function formataNumero(event, form, field, nextfocus, minval, maxval) {
	var tecla = event.keyCode;
	
	//   8 - Backspace
	//   9 - Tab
	//  13 - Enter
	//  46 - Delete
	//  48 - 0
	//  57 - 9
	//  96 - 0 Numeric Keyboard
	// 105 - 9 Numeric Keyboard
	//  37 - Left
	//  38 - Down
	//  39 - Right
	//  40 - Up	
	
	if (!(tecla == 8 || tecla == 9 || tecla == 13 || tecla == 46 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 || tecla >= 37 && tecla <= 40)) {
		event.cancelBubble = true;
		event.returnValue = false;
		return false;
	}
	
	enterPorTab(event, form, field, nextfocus, minval, maxval);
}

