function SomenteNumero(objeto)
{
	//alert(event.keyCode)
	if(event.keyCode!=44 && (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 != 45)) 
		{
			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
	//if(str.indexOf("-")==-1){
		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);
	//}
	//else{
		//str.replace("-","")
		//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).replace("-","") + "." + str.substring(decpoint,str.length).replace("-","");
	//}
}
// 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;
}

function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

function formatar_moeda(campo, separador_milhar, separador_decimal, tecla) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? tecla.which : tecla.keyCode;

	if (whichCode == 13) return true; // Tecla Enter
	if (whichCode == 8) return true; // Tecla Delete
	key = String.fromCharCode(whichCode); // Pegando o valor digitado
	if (strCheck.indexOf(key) == -1) return false; // Valor inválido (não inteiro)
	len = campo.value.length;
	for(i = 0; i < len; i++)
	if ((campo.value.charAt(i) != '0') && (campo.value.charAt(i) != separador_decimal)) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(campo.value.charAt(i))!=-1) aux += campo.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) campo.value = '';
	if (len == 1) campo.value = '0'+ separador_decimal + '0' + aux;
	if (len == 2) campo.value = '0'+ separador_decimal + aux;

	if (len > 2) {
		aux2 = '';

		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += separador_milhar;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}

		campo.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
		campo.value += aux2.charAt(i);
		campo.value += separador_decimal + aux. substr(len - 2, len);
	}

	return false;
}






