
/* MÉTODO TRIM
	Adiciona o método trim() à classe String, semelhante ao que temos em Java e .NET
	ex:
		var s = "   ABC    "; 	=> "   ABC   "
		s = s.trim();			=> "ABC"
*/

String.prototype.trim = function(){
	return this.replace(/^\s*|\s*$/g,"");	
}





var abaSelecionada = "";
function SelecionaAba(aba) {
	var bForcaSelecao = (arguments.length>1)? arguments[1] : true;
	if (!bForcaSelecao&&abaSelecionada!="") return false;
	if (aba == abaSelecionada) return false;
	var obj = document.getElementById("divAba"+aba);
	if (obj!=null) {
		obj.style.display = 'inline';
		obj = document.getElementById("tdAba"+aba);
		obj.className = 'abaSelecionada'
		if (abaSelecionada!="") {
			obj = document.getElementById("divAba"+abaSelecionada);
			obj.style.display = 'none';
	
			obj = document.getElementById("tdAba"+abaSelecionada);
			obj.className = 'aba'
		}
		abaSelecionada = aba;
		return true;
	}
	return false;
}

function ValidaCampo(campo, tipo) {
	switch (tipo) {
	case "NUMBER" : 
		ret = ValidaNumero(campo);
		break;
	case "VALOR" : 
		ret = ValidaValor(campo);
		break;
	case "TELEFONE" : 
		ret = ValidaTel(campo);
		break;
	case "CPF" : 
		ret = ValidaCPF(campo);
		break;
	case "CNPJ" : 
		ret = ValidaCNPJ(campo);
		break;
	case "DATA" : 
		ret = ValidaData(campo);
		break;
	case "HORA" : 
		ret = ValidaHora(campo);
		break;
	case "MMYYYY" : 
		ret = ValidaMMYYYY(campo);
		break;
	case "DDMM" : 
		ret = ValidaDDMM(campo);
		break;
	case "EMAIL" :
		ret = ValidaEmail(campo);
		break;
	default :
		ret = true;
	}
}
	
function ValidaForm(frm) {
	for (x=0; x<frm.elements.length; x++) {
		if (frm.elements[x].value != null) {
			if (frm.elements[x].value.length==0) {
				if (frm.elements[x].getAttribute("required") == 'true') {
					try {
						pai = frm.elements[x].parentElement;
						while(pai.id.indexOf("divAba")<0) {
							pai = pai.parentElement;
						}
						SelecionaAba(pai.id.substring(6, pai.id.length));
					}
					catch (e){}
					alert(frm.elements[x].getAttribute("errorMessage"));
					frm.elements[x].focus();
					return false;
				}
			}
			else {
				if (frm.elements[x].getAttribute("typeField")) {
					var strTypeField = frm.elements[x].getAttribute("typeField");
					if (strTypeField!=null) strTypeField = strTypeField.toUpperCase();
					switch (strTypeField) {
						case "NUMBER" : 
							ret = ValidaNumero(frm.elements[x].value);
							break;
						case "VALOR" : 
							ret = ValidaValor(frm.elements[x].value);
							break;
						case "TELEFONE" : 
							ret = ValidaTel(frm.elements[x].value);
							break;
						case "CPF" : 
							ret = ValidaCPF(frm.elements[x].value);
							break;
						case "CNPJ" : 
							ret = ValidaCNPJ(frm.elements[x].value);
							break;
						case "DATA" : 
							ret = ValidaData(frm.elements[x].value);
							break;
						case "HORA" : 
							ret = ValidaHora(frm.elements[x].value);
							break;
						case "MMYYYY" : 
							ret = ValidaMMYYYY(frm.elements[x].value);
							break;
						case "DDMM" : 
							ret = ValidaDDMM(frm.elements[x].value);
							break;
						case "EMAIL" :
							ret = ValidaEmail(frm.elements[x].value);
							break;
						default :
							ret = true;
					}
					if (!ret) {
						try {
							pai = frm.elements[x].parentElement;
							while(pai.id.indexOf("divAba")<0) {
								pai = pai.parentElement;
							}
							SelecionaAba(pai.id.substring(6, pai.id.length));
						}
						catch (e){
						}
						
						var strErrorMessageType = frm.elements[x].getAttribute("errorMessageType");
						if (strErrorMessageType==null) strErrorMessageType = "Formato inválido";
						alert(strErrorMessageType);
						frm.elements[x].focus();
						return false;
					}
				}
			}		
		}
	}
	return true;
}

function ValidaNumero(num)
{
	for (var i=0;i<num.length;i++) {
		if ((num.charAt(i) < "0") || (num.charAt(i) > "9")) {
			return false;
		}
	}
	return true;
}

function ValidaValor(num)
{
	for (var i=0;i<num.length;i++) {
		if ((num.charAt(i) < "0") || (num.charAt(i) > "9")) {
			if (num.charAt(i) != "," && num.charAt(i) != ".") {
				return false;
			}
		}
	}
	return true;
}


function ValidaTel(num) {
	if (ValidaNumero(num)) {
		if (num.length < 7 || num.length > 8) {
			return false
		}
		else {
			return true ;
		}
	}
	else {
		return false;
	}
}
function ValidaEmail(email)
{
	return email.search(/^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$/)==0;
}
/*
function ValidaEmail(email) {
        var achou_ponto=false;
        var achou_arroba=false;
        var achou_caracter=false;
        for (var i=0; i<email.length; i++) {
                if (email.charAt(i)=="@") achou_arroba=true;
                else if (email.charAt(i)==".") achou_ponto=true;
                else if (email.charAt(i)!=" ") achou_caracter=true;
        }
        return (achou_ponto & achou_arroba & achou_caracter);
}
*/
function ValidaCPF(strCpf)
{
	strCpf = strCpf.replace(new RegExp("[.-]","g"),"");

// 	***	descomentar para validar tamanho do cpf e se tem apenas numero	
//	if (strCpf.length!=11) return false;
//	if (!ValidaNumero(strCpf) return false;
	
	var varFirstChr = strCpf.charAt(0);	
	var vaCharCPF = false;
	for(var i=0;i<=10;i++){
		var c = strCpf.charAt(i);             
		if(!(c>='0')&&(c<='9')){
			return false;
	    }              
	    if(c!=varFirstChr)
			vaCharCPF = true;
	}
	if(!vaCharCPF){
		return false;
	}
	soma=0;	
	for(i=0;i<9; i++){ 
		soma += (10-i) * ( eval(strCpf.charAt(i)) );
	}
	digito_verificador = 11-(soma % 11);
	if((soma % 11) < 2)
		digito_verificador = 0;	
		if (eval(strCpf.charAt(9)) != digito_verificador){
			return false;
		}
		soma=0;	
		for(i=0;i<9; i++){
			soma += (11-i)*(eval(strCpf.charAt(i)));
		}
		soma += 2*(eval(strCpf.charAt(9)));
		digito_verificador = 11-(soma % 11);
		if((soma % 11)<2) 
			digito_verificador = 0;
		if(eval(strCpf.charAt(10)) != digito_verificador){ 
			return false;
		}
	return true;
}

function ValidaMMYYYY (data) {
	data = '01/' + data;
	return ValidaData (data);
}

function ValidaDDMM(data) {
	data = data + '1900';
	return ValidaData (data);
}

function ValidaData (data) {
	separador = '/';
	aux = data;
	barras = 0;
	erro = 0;
	dia = 0;
	mes = 0;
	ano = 0;
	eh_barra = 1;

	meses = new Array(12);
	meses = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	
	for (var i=0; (i<aux.length) && (erro==0); i++) {
		if (aux.charAt(i)==separador) {
			if (eh_barra==0) {
				barras++;
				eh_barra = 1;
			}
			else erro = 1;
		}
		else {
			a = parseInt(aux.charAt(i), 10);
			if (isNaN(a)) erro = 1;
			eh_barra = 0;
		}
	}
	if (barras != 2) erro = 1;

	if (erro==0) {
		pos = aux.indexOf(separador);
		pos2 = aux.indexOf(separador, pos+1);
		dia = parseInt(aux.substring(0, pos), 10);
		mes = parseInt(aux.substring(pos+1, pos2), 10);
		ano = parseInt(aux.substring(pos2+1, aux.length), 10);
		if (ano<1900 || ano>2050) erro = 1;
		else {
			if (eh_bissexto(ano)) meses[1] = 29;
			else meses[1] = 28;
			if (mes<1 || mes>12) erro = 1;
			else if (dia<1 || dia>meses[mes-1]) erro = 1;
		}
	}
	if (erro==1) return false;
	return true;
}

function ValidaHora(hora) {
	pos = 0;
	hh = "";
	mm = "";
	pos = hora.indexOf(':')
	hh = parseInt(hora.substring(0,pos))
	mm = parseInt(hora.substring(pos+1,hora.length))
	if (hh < 0 || hh > 23) return false;
	if (mm < 0 || mm > 59) return false;
	return true;
}

function eh_bissexto(ano)
{
	if ( ((ano - 1996) % 4) == 0) return true;
	return false;
}

function CriaArray(n) {
	this.length=n
	for (var i=1;i<=n;i++)
		{this[i]=""}
}

function ValidaCNPJ(campo) {
 with (Math) {
	 w = 0;
	 Resp1 = "";
	 Resp2 = "";
	 
 	 campo = campo.replace('/','');
 	 campo = campo.replace('-','');
 	 campo = campo.replace('.','');
 	 campo = campo.replace('.','');
	 CGC = campo;
 
	 if (CGC.length != 14) {
	 	return false;
	 }
 
	 if (!ValidaNumero(CGC)) { 
	 	return false;
	 }
 
	 VtCGC = new CriaArray(CGC.length);

	 for (var i=0;i < CGC.length;i++) {
 		if ((CGC.charAt(i) == "0") || (CGC.charAt(i) == "1") || (CGC.charAt(i) == "2") || (CGC.charAt(i) == "3") || (CGC.charAt(i) == "4") || (CGC.charAt(i) == "5") || (CGC.charAt(i) == "6") || (CGC.charAt(i) == "7") || (CGC.charAt(i) == "8") || (CGC.charAt(i) == "9")) {
	 		VtCGC[w]=parseFloat(CGC.charAt(i));	
	 		w++;
		}
 	}


 	Soma1 = (VtCGC[0]*5)+(VtCGC[1]*4)+(VtCGC[2]*3)+(VtCGC[3]*2)+(VtCGC[4]*9)+(VtCGC[5]*8)+(VtCGC[6]*7)+(VtCGC[7]*6)+(VtCGC[8]*5)+(VtCGC[9]*4)+(VtCGC[10]*3)+(VtCGC[11]*2)+0.0001;
 	Divisao1 = Soma1 / 11; 
 	RestoParc1 = (Divisao1 - floor(Divisao1))*11;
 	Resto1 = floor(RestoParc1);
 
 	Soma2 = (VtCGC[0]*6)+(VtCGC[1]*5)+(VtCGC[2]*4)+(VtCGC[3]*3)+(VtCGC[4]*2)+(VtCGC[5]*9)+(VtCGC[6]*8)+(VtCGC[7]*7)+(VtCGC[8]*6)+(VtCGC[9]*5)+(VtCGC[10]*4)+(VtCGC[11]*3)+(VtCGC[12]*2)+0.0001;
 	Divisao2 = Soma2 / 11; 
 	RestoParc2 = (Divisao2 - floor(Divisao2))*11;
 	Resto2 = floor(RestoParc2);


 	if (((Resto1 == 0) || (Resto1 == 1)) && (VtCGC[12] == 0)) {
  		Resp1 = "V";
 	} else {
  		Digito1 = 11 - Resto1;
  		if ((Digito1 == VtCGC[12]) && (Resto1 > 1)) {
			Resp1 = "V";
  		}
 	}
 
 	if (((Resto2 == 0) || (Resto2 == 1)) && (VtCGC[13] == 0)) {
  		Resp2 = "V";
 	} else {
  		Digito2 = 11 - Resto2;
  		if ((Digito2 == VtCGC[13]) && (Resto2 > 1)) {
   			Resp2 = "V";
  		}
 	}
 
 	if ((Resp1 == "V") && (Resp2 == "V")) 
 	{
   		return true;
 	} else {
  		return false;   
 	} 
 }
}
function ValidaTamanho(obj, tam, dsc) {
	if(obj.value.length>tam) {
		obj.value = obj.value.substring(0,tam);
		alert(dsc);
		obj.focus();
		return false;
	}
	return true;
}


function formatarCPF(campo,teclaPres)
{
	var tecla=teclaPres.keyCode;
	vr = "";
	for(i=0;i<campo.value.length;i++)
		if(campo.value.charAt(i)!="." && campo.value.charAt(i)!="-" && campo.value.charAt(i)!="/")
			vr=vr + campo.value.charAt(i);
	tam = vr.length ;
	if (tam < 15 && tecla != 8){tam = vr.length + 1 ;}
	if (tecla == 8){ tam = tam - 1 ;}
	if (tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
		if (tam <= 2) { campo.value = vr ;}
		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 ) ; }
	}
}

// EXEMPLO DE CHAMADA: onkeypress="javascript:formatarCNPJ(this,event)"
function formatarCNPJ(campo,teclaPres)
{
	var tecla=teclaPres.keyCode;
	vr = "";
	for(i=0;i<campo.value.length;i++)
		if(campo.value.charAt(i)!="." && campo.value.charAt(i)!="-" && campo.value.charAt(i)!="/")
			vr=vr + campo.value.charAt(i);
	tam = vr.length ;

	if (tam < 14 && tecla != 8){tam = vr.length + 1;}
	if (tecla == 8){ tam = tam - 1 ;}
	if (tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
		if (tam <= 2) { campo.value = vr ;}
		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) { campo.value = vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
		if ((tam >= 13) && (tam <= 15) ) { campo.value = vr.substr( 0, tam - 12 ) + '.' +vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam );}
	}
}

function formatarTelefone(campo,teclaPres)
{
	var tecla=teclaPres.keyCode;
	vr = "";
	for(i=0;i<campo.value.length;i++)
		if(campo.value.charAt(i)!="-")
			vr=vr + campo.value.charAt(i);
	tam = vr.length ;
	if (tecla == 8){ tam = tam - 1 ;}
	if (tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
		if (tam < 3) { campo.value = vr ;}
		if ((tam >= 3) && (tam <= 6) ) { 
			campo.value = vr.substr(0,3) + '-' + vr.substr(3,tam-3);
		}
		if (tam > 6) { 
			campo.value = vr.substr(0,4) + '-' + vr.substr(4,tam-4);
		}
	}
}

function formatarCEP(campo,teclaPres)
{
	separador1 = '-'; 
	conjunto1 = 5;
	if (campo.value.length == conjunto1)
	{
		campo.value = campo.value + separador1;
	}
	if (event.keyCode < 48 || event.keyCode > 57)
	{
		event.returnValue = false;
	}

}

function formatarData(campo,teclaPres)
{
	var tecla=teclaPres.keyCode;
	vr = "";

	//if (!IsNumber(tecla)){
	//	return false;
	//}

	for(i=0;i<campo.value.length;i++)
		if(campo.value.charAt(i)!="/")
			vr=vr + campo.value.charAt(i);
	tam = vr.length ;
	if (tecla == 8){ tam = tam - 1 ;}
	if (tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
		if (tam < 2) { campo.value = vr ;}
		if ((tam >= 2) && (tam <= 4) ) { 
			campo.value = vr.substr(0,2) + '/' + vr.substr(2,tam-2);
		}
		if ((tam > 4)) { 
			campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,tam-4);
		}
	}
}

function formataValor(campo,tammax,teclaPres) {
	var tecla = teclaPres.keyCode;
	var vr='';
	if (campo.value.length >= tammax)
		return;
	for(i=0;i<campo.value.length;i++)
		if(campo.value.charAt(i)!="." && campo.value.charAt(i)!=",")
			vr=vr + campo.value.charAt(i);
	
	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 ; }
		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 ) ;
		}
	}
}

function formatarHora(campo,teclaPres)
{
	var tecla=teclaPres.keyCode;
	vr = "";

	//if (!IsNumber(tecla)){
	//	return false;
	//}

	for(i=0;i<campo.value.length;i++)
		if(campo.value.charAt(i)!=":")
			vr=vr + campo.value.charAt(i);
	tam = vr.length ;
	if (tecla == 8){ tam = tam - 1 ;}
	if (tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
		if (tam < 2) { campo.value = vr ;}
		if ((tam >= 2)) { 
			campo.value = vr.substr(0,2) + ':' + vr.substr(2,tam);
		}
	}
}

function AutoSkip(obj) {
	with(document.forms[0]) {
		if (obj.value.length == obj.maxLength) 		{
			if (arguments.length>1) {
				arguments[1].focus();
			} else {
				idObjeto = 0;
				for (x=0; x<elements.length; x++) {
					try {
						if (elements[x].type == 'text') {
							if (elements[x].name == obj.name)
								idObjeto = x;
							if (idObjeto > 0 && x != idObjeto) 	{
								elements[x].focus();
								break;				
							}
						}
					}
					catch (e) {}
				}
			}
		}
	}
}


function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

function RetornaMoeda(sValor) {
	var vr='';
	for(i=0;i<sValor.length;i++)
		if(sValor.charAt(i)!="." && sValor.charAt(i)!=",")
			vr=vr + sValor.charAt(i);
	tam = vr.length;
	if (tam <= 2 ) tam = 3
		if ( tam <= 2 ){ sValor = vr ; }
		if ( (tam > 2) && (tam <= 5) ){
			sValor = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; 
		}
		if ( (tam >= 6) && (tam <= 8) ){
			sValor = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; 
		}
		if ( (tam >= 9) && (tam <= 11) ){
			sValor = 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) ){
			sValor = 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) ){
			sValor = 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 ) ;
		}
		return sValor;
}

function SubmitFromText(e, oForm) {
  tecla = (window.Event) ? e.which : e.keyCode;
  if (tecla == 13) oForm.submit();
  else return e;
}

function MaxLenght(m_oField, m_MaxLenght, m_Tecla) {
/*
 Função que limita o tamanho de campos textarea
 Chamada da função: OnKeyDown="JavaScript: return MaxLenght(this, 5, event);"
 Obs1: O segundo parâmetro é o tamanho máximo que você quer que o campo receba
 Obs2: Se voc^r criar um div com o id da seguinte maneira: "DIV_"+NomeDoCampoTextarea. A função imprime nesse div
       a quantidade de caracteres disponíveis para o usuário.
 Por exemplo: <textarea name="MEUCAMPO" OnKeyDown="JavaScript: return MaxLenght(this, 5, event);"></textarea>
              <div id="DIV_MEUCAMPO"></div>
*/
  var tecla = (window.Event) ? m_Tecla.which : m_Tecla.keyCode;
  var nu_Len_Field = m_oField.value.length;
  var oDiv = document.getElementById("DIV_"+m_oField.name);

  if (oDiv) oDiv.innerHTML = parseInt(m_MaxLenght)-parseInt(nu_Len_Field);
  if ((tecla == 8) || (tecla == 9) || ((tecla >= 35) && (tecla <= 40)) || (tecla == 46) || (tecla == 116)) return true;
  else {

    if (nu_Len_Field >= m_MaxLenght) return false;
    else return true;
  }
}

/*
FUNÇÃO PARA FORMATAR MOEDA

EXEMPLOS DE CHAMADA:
onkeyup="return formatMoeda(this, event);"
onkeyup="return format(this, event, 'S');" -- CASO QUEIRA USAR VALOR NEGATIVO NO CAMPO

*/

function formatMoeda(field, evt, neg)
{
	var negativo = field.value.charAt(0) == "-";
	var v = (field.value.replace(/[^0-9]/g, ""));
	if(!isNaN(v))
	{
		var l = v.length;
		if(l<3)
		{
			var formatado = v;
			if (arguments[2] == null){
				field.value = formatado;
			}else{
				if(negativo)formatado = "-"+formatado;
				field.value = formatado;
			}

		}
		else
		{
			var formatado = virgula(v.substr(0, l-2))+"," + v.substring(l-2,l);
			if (arguments[2] != null){
				if(negativo)formatado = "-"+formatado;
				field.value = formatado;				
			}else{
				field.value = formatado;
			}
		}
	}
}

function virgula(str)
{
	var j  = 0;
	var s = "";
	for(var i = str.length-1; i >=0; --i)
	{
		s = str.charAt(i) + s;
		j++;
		if(j%3==0)
			s = "." + s;
	}
	if(s.charAt(0)==".") s = s.substr(1);
	return s;
}


// EXEMPLO DE CHAMADA: onkeypress="javascript: return ApenasNumeros(event);"
function ApenasNumeros(caracter) {
  tecla = (window.Event) ? caracter.which : caracter.keyCode;

 if((tecla > 47 && tecla < 58) || (tecla == 44) || (tecla == 46)) { // numeros de 0 a 9
  return true;
 }
 else {
  if (tecla != 8) { // backspace
   return false;
  }
  else {
   return true;
  }
 }
}

function Mascaras(objForm, StrCampo, StrMascara, StrKeyPress) {
      var i, Cont, sValue, StrTam, StrTamMask,StrBolMask, sCod, nTecla;

/*
      if(document.all) { // Internet Explorer
        nTecla = StrKeyPress.keyCode; }
      else if(document.layers) { // Nestcape
        nTecla = StrKeyPress.which;
      }
*/
      nTecla = (window.Event) ? StrKeyPress.which : StrKeyPress.keyCode;
//      alert(nTecla);
	  
      sValue = objForm[StrCampo].value;

      // Limpa todos os caracteres de formatação que
      // já estiverem no campo.
      sValue = sValue.toString().replace( "-", "" );
      sValue = sValue.toString().replace( "-", "" );
      sValue = sValue.toString().replace( ".", "" );
      sValue = sValue.toString().replace( ".", "" );
      sValue = sValue.toString().replace( "/", "" );
      sValue = sValue.toString().replace( "/", "" );
      sValue = sValue.toString().replace( ":", "" );
      sValue = sValue.toString().replace( "(", "" );
      sValue = sValue.toString().replace( "(", "" );
      sValue = sValue.toString().replace( ")", "" );
      sValue = sValue.toString().replace( ")", "" );
      sValue = sValue.toString().replace( " ", "" );
      sValue = sValue.toString().replace( " ", "" );
      StrTam = sValue.length;
      StrTamMask = StrMascara.length;
      i = 0;
      Cont = 0;
      sCod = "";
      StrTamMask = StrTam;
      while (i <= StrTamMask) {
        StrBolMask = ((StrMascara.charAt(i) == "-") || (StrMascara.charAt(i) == ".") || (StrMascara.charAt(i) == "/") || (StrMascara.charAt(i) == ":"))
        StrBolMask = StrBolMask || ((StrMascara.charAt(i) == "(") || (StrMascara.charAt(i) == ")") || (StrMascara.charAt(i) == " "))

        if (StrBolMask) {
          sCod += StrMascara.charAt(i);
          StrTamMask++; }
        else {
          sCod += sValue.charAt(Cont);
          Cont++;
        }
        i++;
      }

      objForm[StrCampo].value = sCod;
      objForm[StrCampo].maxLength = StrMascara.length;


      if (nTecla != 8) { // Backspace
        if (StrMascara.charAt(i-1) == "9") { // Apenas números...
          return ((nTecla > 47) && (nTecla < 58)); } // Números de 0 a 9
        else { // Qualquer caracter...
          return true;
        } }
      else {
        return true;
      }




}


