/*****************************************************************************/
// Licio: 09/abr/2003
/*****************************************************************************/
function onlyNumber (ctrl, e, max) {
	var key = e.keyCode;

	//rotulo.innerHTML=key;

	//if (e.shiftKey) return false;

	// cima 38, baixo 40
	// enter, tab, backspace, delete, ?, 0..9, 0..9 (numpad), <, >, home, end
	if (!(key==13 || key==9 || key==8 || key==46 || key==191 || (key>47 && key<58)  || (key>95 && key<106)||
		key==37 || key==39 || key==35 || key==36)) return false;
	
	// vírgula
	//if (!(key==188 && ctrl.value.indexOf(',') == -1)) return false;

	//FormatarNumero(ctrl, e, n);
	
	if ((key>47 && key<58) && (ctrl.value.length >= max)) return false;
	
	return true;
}

function teto(n) {
	if (n == Math.floor(n))
		return n
	else
		return (Math.floor(n) + 1)
}

function charDelete(s,c) {
	var saida = '';
	for (var i = 0; i < s.length; i++)
		if (s.substr(i,1) != c)
			saida = saida + s.substr(i,1);
	return saida;
}

function charReplace(s, c_old, c_new) {
	var saida = '';
	for (var i = 0; i < s.length; i++)
		if (s.substr(i,1) == c_old)
			saida = saida + c_new
		else
			saida = saida + s.substr(i,1);
	return saida;
}


function NumeroPuro_first(s) {
	s = charDelete(s,'.');
	s = charDelete(s,',');
	s = charDelete(s,'+');
	s = charDelete(s,'<');
	//s = charDelete(s,'R$ ');
	s = Math.ceil(s).toString();
	if (s == '0') s = '';
	
	return s
}

function CleanString (str, str_re) {
	var saida = '';
	var re = new RegExp(str_re);
	
	for (var i = 0; i < str.length; i++)
		if (re.test(str.substr(i,1)))
			saida = saida + str.charAt(i);
	
	return saida
}

function NumeroPuro(s) {
	var str_numero = '';
	for (var i = 0; i < s.length; i++)
		if (s.charCodeAt(i)>47 && s.charCodeAt(i)<58)
			str_numero = str_numero + s.charAt(i);
	
	// tirar zeros a esquerda
	//numero = Math.ceil(s).toString();
	//if (s == '0') s = '';
	/*
	var numero = parseInt(texto); alert('numero: ' + numero);
	if (parseInt(texto) == 0)
		return '';
	else
		return texto;
	*/
	return str_numero
}

function FillChars(c,n) {
	var saida = '';
	for (var i = 0; i < n; i++)
		saida = saida + c;
	return saida
}

function FormatarNumero(ctrl, e, n) {
	var saida = '';
	var texto = NumeroPuro(ctrl.value); //alert(texto);
	var tamanho = texto.length;
	var qapgm; // Quantidade de Algarismos no Primeiro Grupo de Milhar

	var key = e.keyCode;
	if ((key>=35 && key<=40) || e.shiftKey) return false

	if (tamanho <= n)
		saida = '0,' + FillChars('0', n - tamanho) + texto;

	else {
		// CALCULAR PARTE INTEIRA
		//-----------------------
		
		esquerda = texto.substr(0,tamanho-n); //alert(esquerda);

		// Quantidade de Algarismos no Primeiro Grupo de Milhar
		qapgm = esquerda.length % 3; // resto da divisão por 3
		if (qapgm == 0) qapgm = 3;
		
		// Calcular qtos grupos de milhares existem
		var milhares = teto(esquerda.length / 3) - 1;
		//var milhares = Math.floor(esquerda.length - qapgm / 3);
		//alert('esquerda: ' + esquerda + ', milhares: ' + milhares);
		
		// pegar o primeiro grupo de milhar
		saida = esquerda.substr(0,qapgm); //alert('saida: ' + saida);
		
		var c_ini;
		for (var m = 1; m <= milhares; m++) {
			c_ini = qapgm+(m-1)*3;
			saida = saida + '.' + esquerda.substr(c_ini,3);
			//alert('esquerda: ' + esquerda + ', qapgm: ' + qapgm + ', milhares: ' + milhares + ', char_ini: ' + c_ini);
		}
	
		
		// CALCULAR PARTE FRACIONÁRIA
		//---------------------------
		direita = texto.substr(tamanho-n,n);
		

		// Resultado Final		
		saida = saida + ',' + direita;
	/*
		for (var i = 1; i <= n; i++)
			saida = texto.substr(tamanho - i,1) + saida;
			
		while (i < texto.length) {
			saida = texto.substr(
			i++;
		}
	*/
	}
	
	//alert(saida);
	ctrl.value = saida;
	return false
}

function FormatarMascara(ctrl, e, mascara, valor, direcao) {
	//var valor = NumeroPuro(ctrl.value); alert(valor);
	var ini; // posição do primeiro caractere a ser analisado
	var saida = '';
	var m; // posição na string de Máscara
	var n; // posição na string de Número (Valor)
	
	var key = e.keyCode;
	if ((key>=35 && key<=40) || e.shiftKey) return false

	if (direcao == 1) { // esquerda para direita
		m = 0;
		n = 0;
		while (m < mascara.length && n < valor.length) {
			if (mascara.charAt(m) == '#') {
				saida = saida + valor.charAt(n);
				n++;
			} else {
				saida = saida + mascara.charAt(m);
			}
			m++;
		}
		
	} else { // direita para esquerda
		m = mascara.length - 1;
		n = valor.length - 1;
		while (m >= 0 && n >= 0 ) {
			if (mascara.charAt(m) == '#') {
				saida = valor.charAt(n) + saida;
				n--;
			} else {
				saida = mascara.charAt(m) + saida;
			}
			m--;
		}
	}
	ctrl.value = saida;
}

function FormatarHorario(ctrl,e) {
	var key = e.keyCode;
	var valor = charDelete(ctrl.value,':');

	if ((key>=35 && key<=40) || e.shiftKey) return false

	//switch (valor.length) {
	//case 0,1,2:
	if (valor.length == 4) {
		tab(ctrl,5);
		return true
	}

	if (valor.length == 1)
		if (valor == '0' || valor == '1' || valor == '2')
			ctrl.value = valor;
		else
			ctrl.value = '';
	else
		if (valor.length == 2)
			ctrl.value = valor;
		else
			if (valor.length > 2)
				ctrl.value = valor.substr(0,2) + ':' + valor.substr(2,2);
	
	return true;
}
