﻿function autoTab(input, e) {
	var ind = 0;
	var isNN = (navigator.appName.indexOf("Netscape") != -1);
	var keyCode = (isNN) ? e.which : e.keyCode;
	var nKeyCode = e.keyCode;

	if (keyCode == 13) {
		if (!isNN) { window.event.keyCode = 0; } // evitar o beep   
		ind = getIndex(input);
		if (input.form[ind].type == 'textarea') {
			return;
		}
		ind++;
		input.form[ind].focus();
		if (input.form[ind].type == 'text') {
			input.form[ind].select();
		}
	}

	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input) {
			index = i;
			if (i < (input.form.length - 1)) {
				if (input.form[i + 1].type == 'hidden') {
					index++;
				}
				if (input.form[i + 1].type == 'button' && input.form[i + 1].id == 'tabstopfalse') {
					index++;
				}
			}
		}
		else
			i++;
		return index;
	}
}

function F_ChecaTamanho(campo, minimo) {
	if (campo.value.length > minimo) {
		return (true);

	} else {
	
		return (false);

	}
}

function F_FormataCPF(Campo, teclaPress) {
	var tecla = teclaPress.keyCode;
	vr = event.srcElement.value;
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");
	tam = vr.length + 1;

	if (tecla != 9 && tecla != 8) {
		if (tam >= 4 && tam <= 7) {
			event.srcElement.value = vr.substr(0, 3) + '.' + vr.substr(3, 3);
		}
		if (tam >= 7 && tam < 11) {
			event.srcElement.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(6, 3);
		}
		if (tam >= 10 && tam < 14) {
			vr = vr.replace(".", "");
			vr = vr.replace(".", "");
			vr = vr.replace(".", "");
			event.srcElement.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(6, 3) + '-' + vr.substr(9, 2);
		}
	}
}

function F_FormataCNPJ(Campo, teclapres) {
	var tecla = teclapres.keyCode;
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");
	tam = vr.length + 1;

	if (tecla != 9 && tecla != 8) {
		if (tam > 2 && tam < 6)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
		if (tam >= 6 && tam < 9)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 3) + '.' + vr.substr(5, tam - 5);
		if (tam >= 9 && tam < 13)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 3) + '.' + vr.substr(5, 3) + '/' + vr.substr(8, tam - 8);
		if (tam >= 13 && tam < 15)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 3) + '.' + vr.substr(5, 3) + '/' + vr.substr(8, 4) + '-' + vr.substr(12, tam - 12);
	}
}

function F_FormataCEP(Campo, teclaPress) {
	var tecla = teclaPress.keyCode;
	vr = event.srcElement.value;
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");
	tam = vr.length + 1;

	if (tecla != 9 && tecla != 8) {
		//            if (tam >= 3 && tam <= 6) {
		//                event.srcElement.value = vr.substr(0, 2) + '.' + vr.substr(2, 3);
		//            }
		if (tam >= 6 && tam < 9) {
			event.srcElement.value = vr.substr(0, 5) + '-' + vr.substr(5, 3);
		}
	}
}

// move o cursor para a posição pos
function F_MovimentaCursor(textarea, pos) {
	if (pos <= 0)
		return; //se a posição for 0 não reposiciona

	if (typeof (document.selection) != 'undefined') {
		//IE
		var oRange = textarea.createTextRange();
		var LENGTH = 1;
		var STARTINDEX = pos;

		oRange.moveStart("character", -textarea.value.length);
		oRange.moveEnd("character", -textarea.value.length);
		oRange.moveStart("character", pos);
		//oRange.moveEnd("character", pos);
		oRange.select();
		textarea.focus();
	}
	if (typeof (textarea.selectionStart) != 'undefined') {
		//FireFox
		textarea.selectionStart = pos;
		textarea.selectionEnd = pos;
	}
}

//descobre qual a posição do cursor no campo
function F_PosicaoCursor(textarea) {
	var pos = 0;
	if (typeof (document.selection) != 'undefined') {
		//IE
		var range = document.selection.createRange();
		var i = 0;
		for (i = textarea.value.length; i > 0; i--) {
			if (range.moveStart('character', 1) == 0)
				break;
		}
		pos = i;
	}
	if (typeof (textarea.selectionStart) != 'undefined') {
		//FireFox
		pos = textarea.selectionStart;
	}

	if (pos == textarea.value.length)
		return 0; //retorna 0 quando não precisa posicionar o elemento
	else
		return pos; //posição do cursor
}

// limpa todos os caracteres especiais do campo solicitado
function F_FiltraCampo(campo) {
	var s = "";
	var cp = "";
	vr = campo.value;
	tam = vr.length;
	for (i = 0; i < tam; i++) {
		if (vr.substring(i, i + 1) != "/"
            && vr.substring(i, i + 1) != "-"
            && vr.substring(i, i + 1) != "."
            && vr.substring(i, i + 1) != "("
            && vr.substring(i, i + 1) != ")"
            && vr.substring(i, i + 1) != ":"
            && vr.substring(i, i + 1) != ",") {
			s = s + vr.substring(i, i + 1);
		}
	}
	return s;
	//return campo.value.replace("/", "").replace("-", "").replace(".", "").replace(",", "")
}

// recupera o evento do form
function F_GetEvent(evt) {
	if (!evt) evt = window.event; //IE
	return evt;
}
//Recupera o código da tecla que foi pressionado
function F_GetKeyCode(evt) {
	var code;
	if (typeof (evt.keyCode) == 'number')
		code = evt.keyCode;
	else if (typeof (evt.which) == 'number')
		code = evt.which;
	else if (typeof (evt.charCode) == 'number')
		code = evt.charCode;
	else
		return 0;

	return code;
}

//evita criar mascara quando as teclas são pressionadas
function F_TeclaValida(tecla) {
	if (tecla == 8 //backspace
	//Esta evitando o post, quando são pressionadas estas teclas.
	//Foi comentado pois, se for utilizado o evento texchange, é necessario o post.
        || tecla == 9 //TAB
        || tecla == 27 //ESC
        || tecla == 16 //Shif TAB 
        || tecla == 45 //insert
        || tecla == 46 //delete
        || tecla == 35 //home
        || tecla == 36 //end
        || tecla == 37 //esquerda
        || tecla == 38 //cima
        || tecla == 39 //direita
        || tecla == 40)//baixo
		return false;
	else
		return true;
}

// limpa todos caracteres que não são números
function F_FiltraNumeros(campo) {
	var s = "";
	var cp = "";
	vr = campo;
	tam = vr.length;
	for (i = 0; i < tam; i++) {
		if (vr.substring(i, i + 1) == "0" ||
            vr.substring(i, i + 1) == "1" ||
            vr.substring(i, i + 1) == "2" ||
            vr.substring(i, i + 1) == "3" ||
            vr.substring(i, i + 1) == "4" ||
            vr.substring(i, i + 1) == "5" ||
            vr.substring(i, i + 1) == "6" ||
            vr.substring(i, i + 1) == "7" ||
            vr.substring(i, i + 1) == "8" ||
            vr.substring(i, i + 1) == "9") {
			s = s + vr.substring(i, i + 1);
		}
	}
	return s;
	//return campo.value.replace("/", "").replace("-", "").replace(".", "").replace(",", "")
}

//Formata Telefone
function F_FormataTelefone(campo, evt) {
	//(00) 0000-0000
	var xPos = F_PosicaoCursor(campo);
	evt = F_GetEvent(evt);
	var tecla = F_GetKeyCode(evt);
	if (!F_TeclaValida(tecla))
		return;

	vr = campo.value = F_FiltraNumeros(F_FiltraCampo(campo));
	tam = vr.length;

	if (tam == 1)
		campo.value = '(' + vr;
	else if (tam >= 2 && tam < 6)
		campo.value = '(' + vr.substr(0, 2) + ') ' + vr.substr(2);
	else if (tam >= 6)
		campo.value = '(' + vr.substr(0, 2) + ') ' + vr.substr(2, 4) + '-' + vr.substr(6);

	//(
	//    if(xPos == 1 || xPos == 3 || xPos == 5 || xPos == 9)
	//        xPos = xPos +1
	F_MovimentaCursor(campo, xPos);
}


//    function F_FormataTelefone(element, e) { if (e.keyCode != 8) { length = element.text.length; if (length == 2) { if (element.value.charAt(0) != "(") element.value = "(" + element.value + ")"; } if (length == 3) if (element.value.charAt(0) == "(") element.value += ")"; if (length == 8) element.value += "-"; } }

function F_DialogModal(url, nome, nrTamanho, nrLargura) {
	eval(window.showModalDialog(url, nome, 'Resizable:no; DialogHeight:' + nrTamanho + 'px ; DialogWidth:' + nrLargura + 'px; Edge:raised; Help:no; Scroll:no; Status:no; Center:yes;'));
	//eval(caixa);
}

function F_AbrirNovaJanela(URL) {
	window.open(URL, "", "", "")
}
