function SomenteNumero(objeto)
{
	if(event.keyCode < 48 || event.keyCode > 57) 
	{
		event.keyCode = 0;
	}
}
function FormataNota(objeto, pintdecimal)
{
	var sTexto = objeto.value;

	ConverteVirgula(objeto);

	for(var i=0;i<sTexto.length;i++)
	{		
		if (sTexto.substring(i-pintdecimal, i) == ",")
		{			
			event.keyCode = 0;
		}
	} 
}

function ConverteVirgula(objeto)
{
	if(event.keyCode == 46)
	{
		event.keyCode = 44
		VerificaVirgula(objeto);
	}	
}

function SomenteDDD(objeto)
{
	if(event.keyCode < 49 || event.keyCode > 57) 
	{
		event.keyCode = 0;
	}
}

function FormataValor(objeto)
{
	if(event.keyCode == 44)
	{
		VerificaVirgula(objeto);
	}	
	else
	{
		if(event.keyCode < 48 || event.keyCode > 57) 
		{
			event.keyCode = 0;
		}
	}
}

function VerificaVirgula(objeto)
{
	var sTexto = objeto.value;
	
	for(var i = 0; i < sTexto.length; i++)
	{
		if (sTexto.substring(i, i+1) == ",")
		{
			event.keyCode = 0;
		}
	} 
}

function FormataNum(pCampo)
{
	var iPosVirg = 0
	var vNumero = pCampo.value

	if (vNumero == ""){
		return vNumero
	}
	
	iPosVirg = vNumero.lastIndexOf(',')

	if (iPosVirg<0){
		//return vNumero + ",00"
		pCampo.value = vNumero + ",00"
		return 0
	}

	if (iPosVirg == 0){
		vNumero = "0" + vNumero
		iPosVirg = iPosVirg + 1
	}

	y = vNumero.substr(iPosVirg + 1, iPosVirg + 3)
	
	if (y.length == 0){
		//return vNumero + "00"
		pCampo.value = vNumero + "00"
		return 0
	}
	else if (y.length == 1){
		//return vNumero + "0"
		pCampo.value = vNumero + "0"
		return 0
	}
	else{
		//return vNumero.substr(0, iPosVirg + 3)
		pCampo.value = vNumero.substr(0, iPosVirg + 3)
		return 0
	}
}

function FormataNum2(pCampo)
{
	var iPosVirg = 0
	var vNumero = pCampo.value

	if (vNumero == ""){
		return vNumero
	}
	
	
	iPosVirg = vNumero.lastIndexOf(',')

	if (iPosVirg<0){
		//return vNumero + ",00"
		pCampo.value = vNumero + ",00"
		return vNumero + ",00"
	}

	if (iPosVirg == 0){
		vNumero = "0" + vNumero
		iPosVirg = iPosVirg + 1
	}

	y = vNumero.substr(iPosVirg + 1, iPosVirg + 3)
	
	if (y.length == 0){
		//return vNumero + "00"
		pCampo.value = vNumero + "00"
		return vNumero + "00"
	}
	else if (y.length == 1){
		//return vNumero + "0"
		pCampo.value = vNumero + "0"
		return vNumero + "0"
	}
	else{
		//return vNumero.substr(0, iPosVirg + 3)
		pCampo.value = vNumero.substr(0, iPosVirg + 3)
		return vNumero.substr(0, iPosVirg + 3)
	}
}

function LimitaCampo(objCampo, intTamanho){
	if (objCampo.value.length > intTamanho - 1){
		event.keyCode = 0;
	}
}
function MaxDigitos(obj, Max, obj2)
{
	if (obj.value.length == Max)
	{
		obj2.focus()
		obj2.select()
	}
}

function FormataValorNovo(campo,tammax,teclapres) {

	var tecla = teclapres.keyCode;
	var vr = campo.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 ){	tam = tam - 1 ; }
		
	if ( tecla == 8 || (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105) ){
		if ( tam <= 2 ){ 
	 		campo.value = vr ; }
	 	tam = tam - 1;
	 	if ( (tam > 2) && (tam <= 5) ){
	 		campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 12) && (tam <= 14) ){
	 		campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 15) && (tam <= 17) ){
	 		campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
	}
}

// generic positive number decimal formatting function
function FormataNumero (expr, decplaces) {
	// raise incoming value by power of 10 times the
	// number of decimal places; round to an integer; convert to string
	var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces))
	// pad small value strings with zeros to the left of rounded number
	while (str.length <= decplaces) {
		str = "0" + str
	}
	// establish location of decimal point
	var decpoint = str.length - decplaces
	// assemble final result from: (a) the string up to the position of
	// the decimal point; (b) the decimal point; and (c) the balance
	// of the string. Return finished product.
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}
// turn incoming expression into a dollar value


function FormataNumeroBonito (expr) {
	
	var strAux = ""
	var str = expr
	
	if(str.indexOf(",") < 0)
	{
		str = str + ",00"
	}
	else
	{
		if(str.indexOf(",") == (str.length - 1))
		{
			str = str + "00"
		}
		else
		{
			if(str.indexOf(",") == (str.length - 2))
			{
				str = str + "0"
			}
			else
			{
				if((str.indexOf(",") + 3) < (str.length ))
				{
					str = str.substring(0,(str.indexOf(",") + 3))
				}
			}
		}
	}
	if(str.length > 6)
	{
		strAux = str.substring(str.indexOf(","))
		str = str.substring(0, str.indexOf(","))
		while (str.length >= 4) {
			strAux = "." + str.substring((str.length - 3)) + strAux
			str = str.substring(0, (str.length - 3))
		}
			
		strAux = str + strAux
	}
	else
	{
		return str;
	}
	if(strAux.substring(0,1)=="-" && strAux.substring(2,1)==".")
	{
		strAux = "-" + strAux.substring(2)
	}
	
	return strAux;
}

