function euro_keypress(){restringirNumero(1000000000, 2);}
function euro_blur(obj){esNumeroEntre(obj, 1000000000, 2);}
function tipo_keypress(){restringirNumero(1000000, 3);}
function tipo_blur(obj){esNumeroEntre(obj, 1000000, 3);}
function trim(s){return s.replace(/(^\s*)|(\s*$)/g, '');}
function restringirNumero(max, decimales, bSign, bThousand, bAllow_leading_zeros)
{// evento onKeyPress
	var objTxt = event.srcElement;
	var objValue = objTxt.value;
	var keyCode = event.keyCode;
	
	if (decimales == null) decimales = -2;
	if (bSign == null) bSign = false;
	if (bThousand == null) bThousand = true;
	if (bAllow_leading_zeros == null) bAllow_leading_zeros = false;


	if ((keyCode == 43 || keyCode == 45) && objValue.indexOf('+') == -1 && objValue.indexOf('-') == -1 && bSign)
	{
		// Se ponga donde se ponga un signo siempre aparece al principio y
		// después el cursor se va al final del objeto.
		event.returnValue = false;
		objTxt.value = String.fromCharCode(keyCode) + objTxt.value;
		return false;
	}
	if ((keyCode > 57 || keyCode < 43 || keyCode == 47) || (keyCode == 43 || keyCode == 45) && (objValue.indexOf('+') > -1 || objValue.indexOf('-') > -1 || !bSign) || ((keyCode == 44 || keyCode == 46) && (objValue.indexOf(',') > -1 || decimales == -2)))
	{
		event.returnValue = false;
		esNumeroEntre(objTxt, max, decimales, bSign, bThousand, bAllow_leading_zeros);
		return false;
	}
	if (keyCode == 46) // punto (.)
		event.keyCode = 44; // coma (,)
}
function formatNum(valor, decimales, bSign, bThousand, bAllow_leading_zeros)
{
	var val = stripSpaces(valor);

	if (decimales == null) decimales = -2;
	if (bThousand == null) bThousand = true;
	if (bSign == null) bSign = false;
	if (bAllow_leading_zeros == null) bAllow_leading_zeros = false;

	if (bSign)
		val = comprobarSigno(val); // en esta función ya se llama a stripLeadingZeros(val)
	else
	{
		val = val.replace(/[+-]/g , '');
		if (!(bAllow_leading_zeros && (decimales == -2 || decimales == 0) && !bThousand))
			val = stripLeadingZeros(val);
	}

	if (decimales > -2)
		val = limpiarDecimal(val, decimales);

	if (bThousand)
		val = ponerPuntos(val);
	
	if (isNaN(quitarFormatoNumero(val))) // en caso de que se hubiera pegado un valor erroneo.
		val = valor;
		
	return val;
}
function esNumeroEntre(objTxt, max, decimales, bSign, bThousand, bAllow_leading_zeros)
{
	if (decimales == null) decimales = -2;
	if (bThousand == null) bThousand = true;
	if (bSign == null) bSign = false;
	if (bAllow_leading_zeros == null) bAllow_leading_zeros = false;

	var num = objTxt.value;
	if (decimales == -2)
	{
		num = num.replace(/[,]/g , '');
		objTxt.value = num;
	}
	num = quitarFormatoNumero(num);
	if (trim(num) == '' || trim(num) == '.')
	{
		objTxt.value = '';
		return true;
	}

	var mensaje = 'Debe ser un número menor que ' + ponerPuntos(max) + '\n(el signo decimal debe ser la coma ",")';

	if (!bAllow_leading_zeros)
		num = (num == '')? num: parseFloat(num);

	if (num == '' || (esNumero(num) && num <= max))
	{
		num += '';
		num = num.replace(/[.]/g, ',');
		objTxt.value = formatNum(num, decimales, bSign, bThousand, bAllow_leading_zeros);
		return true;
	}

	alert(mensaje);
	objTxt.focus();
	objTxt.select();
	return false;
}
function esNumero(dato)
{
	dato = '' + dato;
	return (dato != '' && !isNaN(dato) && dato.indexOf('E') == -1 && dato.indexOf('e') == -1);
}
function ponerPuntos(num)
{
	num = '' + num;
	var aux_i = '';
	var aux_d = '';
	var numero = num.replace(/[.]/g, ''); // limpiar formato pero dejar la coma (,)
	var num_sin_dec;
	var decimales = '';
	var num_puntos = '';
	var signo = numero.charAt(0);
	if (signo == '-' || signo == '+')
		numero = numero.substr(1);
	else
		signo = '';
	if (numero.indexOf(',') != -1)
	{
		num_sin_dec = numero.substring(0, numero.indexOf(','));
		decimales = numero.substr(numero.indexOf(','));
	}
	else
		num_sin_dec = numero;

	if (num_sin_dec.length > 3)
	{
		aux_i = num_sin_dec;
		while (aux_i.length > 3)
		{
			aux_d = aux_i.substr(aux_i.length - 3);
			aux_i = aux_i.substr(0, aux_i.length - 3);
			num_puntos = '.' + aux_d + num_puntos;
		}
		num = signo + aux_i + num_puntos + decimales;
	}
	else
		num = signo + numero;
	return num;
}
function quitarFormatoNumero(dato)
{
	dato = '' + dato;
	dato = dato.replace(/[.]/g, '');
	dato = dato.replace(/\,/g, '.');
	return dato;
}
function stripSpaces(s)
{
	return s.replace(/\s/g, '');
}
function comprobarSigno(value)
{
	value = stripLeadingZeros(value);
	if (value == '-' || value == '+') return '';
	if (value.lastIndexOf('+') > 0)
		return value.replace(/[+]/g, '');
	if (value.lastIndexOf('-') > 0)
		return value.replace(/[-]/g, '');
	return value;
}
function stripLeadingZeros(s)
{
	var signo = s.charAt(0);
	if (signo == '-' || signo == '+')
		s = s.substr(1);
	else
		signo = '';

	for (var i = 0; i < s.length; ++i)
		if (s.charAt(i) != '0' || s.charAt(i+1) == ',' || s.charAt(i+1) == '')
			return signo + s.substr(i);
	return signo + s;
}
function redondear(numero, decimales)
{
	if (numero == null || numero == '-' || numero == '+')
		return '';
	if (numero > 99999999999) // los numeros grandes los aproxima como quiere...
		return numero;
	if (decimales == null) decimales = 0;
	var aux = Math.pow(10, decimales);
	return (Math.round(numero * aux)/aux);
}
function limpiarDecimal(numero, decimales)
{
	if (decimales == null) decimales = -2;
	numero = '' + numero;
	var signo = numero.charAt(0);
	if (signo == '-' || signo == '+')
		numero = numero.substr(1);
	else
		signo = '';

	if (decimales > -1)
	{
		numero = '' + redondear(quitarFormatoNumero(numero), decimales);
		numero = numero.replace(/[.]/g, ',');
	}

	var coma_index = numero.indexOf(',');
	if (coma_index == (numero.length - 1))
		numero = numero.substr(0, coma_index);
	else if (coma_index == 0)
		numero = '0' + numero;

	numero = signo + numero;

	return numero;
}
