function numbersonly(e) {
	var key;
	var keychar;
	if (window.event)
	key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
		return true;
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;
	else
		return false;
}

function floatsonly(e) {
	var key;
	var keychar;

	if (window.event)
	key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
		return true;
	else if ((("0123456789.").indexOf(keychar) > -1))
		return true;
	else
		return false;
}

//onkeyup="JavaScript:formataData(this,event)"
//onKeydown="JavaScript:FormataData(this,event)"
function FormataData(Campo, teclapres) {
	var tecla = (window.Event) ? teclapres.which : teclapres.keyCode;
	var vr = new String(Campo.value);
 
	//Verifico se foi digitado uma tecla numérica
	if (tecla > 47 && tecla < 58){
		vr = vr.replace("/", "");
		vr = vr.replace("/", "");
		tam = vr.length + 1;
		if (tecla != 9 && tecla != 8 && tecla != 13 && tecla != 27 && tecla != 37 && tecla != 39 && tecla != null  && tecla != 0 ){
			if (tam > 2 && tam < 5)
				Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, tam);
				if (tam >= 5 && tam <=10)
					Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4);
		}
	}else{
		return false;
	}
}


function FormataHora(Campo, teclapres) {
	var tecla = (window.Event) ? teclapres.which : teclapres.keyCode;
	var vr = new String(Campo.value);
 
	//Verifico se foi digitado uma tecla numérica
	if (tecla > 47 && tecla < 58){
		vr = vr.replace(":", "");
		vr = vr.replace(":", "");
		tam = vr.length + 1;
		if (tecla != 9 && tecla != 8){
			if (tam > 2 && tam < 5)
				Campo.value = vr.substr(0, 2) + ':' + vr.substr(2, tam);
			//if (tam >= 5 && tam < 9)
				//Campo.value = vr.substr(0,2) + ':' + vr.substr(2,2) + ':' + vr.substr(4,2);
		}
	}else{
		return false;
	}
}


function FormataCPF(Campo, teclapres) {
	var tecla = (window.Event) ? teclapres.which : teclapres.keyCode;
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");
	tam = vr.length + 1;
	if (tecla != 9 && tecla != 8){
		if (tam > 3 && tam < 7)
			Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
		if (tam >= 7 && tam <10)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
		if (tam >= 10 && tam < 12)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9);
		}
}

function FormataCNPJ(Campo, teclapres) {
	var tecla = (window.Event) ? teclapres.which : teclapres.keyCode;
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");
	tam = vr.length + 1 ;
	if (tecla != 9 && tecla != 8){
		if (tam > 2 && tam < 6)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
		if (tam >= 6 && tam < 9)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
		if (tam >= 9 && tam < 13)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
		if (tam >= 13 && tam < 15)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
		}
}

function CalcularDV(sCampo, iPeso) {
	var iTamCampo;
	var iPosicao, iDigito;
	var iSoma1 = 0;
	var iSoma2=0;
	var iDV1, iDV2;
	iTamCampo = sCampo.length;
	for (iPosicao=1; iPosicao<=iTamCampo; iPosicao++){
		iDigito = sCampo.substr(iPosicao-1, 1);
		iSoma1 = parseInt(iSoma1,10) + parseInt((iDigito * Calcular_Peso(iTamCampo - iPosicao, iPeso)),10);
		iSoma2 = parseInt(iSoma2,10) + parseInt((iDigito * Calcular_Peso(iTamCampo - iPosicao + 1, iPeso)),10);
		}
	iDV1 = 11 - (iSoma1 % 11);
	if (iDV1 > 9)
		iDV1 = 0;
	iSoma2 = iSoma2 + (iDV1 * 2);
	iDV2 = 11 - (iSoma2 % 11);
	if (iDV2 > 9)
		iDV2 = 0;
	Ret = (parseInt(iDV1 * 10,10) + parseInt(iDV2));
	Ret = "0" + Ret;
	Ret = Ret.substr(Ret.length - 2,Ret.length);
	return(Ret);
}

function Calcular_Peso(iPosicao, iPeso) {
	//Pesos
	//CPF 11
	//CNPJ 9
	return (iPosicao % (iPeso - 1)) + 2;
}
	
//  onKeyPress="return(FormataReais(this,'',',',event))
function FormataReais(fld, milSep, decSep, 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 = fld.value.length;
	for(i = 0; i < len; i++)
	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
	    if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
	   aux2 = '';
	   for (j = 0, i = len - 3; i >= 0; i--) {
	       if (j == 3) {
		       aux2 += milSep;
			   j = 0;
		   }
		   aux2 += aux.charAt(i);
		   j++;
       }
	   fld.value = '';
	   len2 = aux2.length;
	   for (i = len2 - 1; i >= 0; i--)
	   fld.value += aux2.charAt(i);
	   fld.value += decSep + aux.substr(len - 2, len);
    }
  return false;
}

function FormataInteiro(Campo, TamMax, teclapres) {
	var tecla = (window.Event) ? teclapres.which : teclapres.keyCode;
	var vr = new String(Campo.value);
	var TamCampo = Campo.value.length;
	
	//VERIFICO SE FOI DIGITADO UMA TECLA NUMÉRICA
	if (tecla > 47 && tecla < 58){
		if (TamCampo < TamMax){
			//Campo.value = vr;
		}else{
			return false;
		}
	}else{
		return false;
	}
}

//CAPTURA CLIQUE DO USUÁRIO
document.onclick = objSeleciona;


//VERIFICA BROWSER. SE FOR IE USA O WINDOW.EVENT
function verificaEvento(ev){
	if(typeof(ev) == 'undefined') var ev = window.event;
	return ev;
}
//FIM VERIFICA BROWSER. SE FOR IE USA O WINDOW.EVENT


//CAPTURA ELEMENTO ACIONADO POR ALGUM EVENTO E POSICIONA NO FINAL DO CAMPO.
function objSeleciona(e){

	var verEv = verificaEvento(e);
	var evento = verEv.target ? verEv.target : verEv.srcElement;
	var evmouse = new String(evento.onkeyup);
	var campo = evento.name;

	if (evmouse.indexOf("formataValor") > 0){
		if ((document.all[campo].selectionStart == 'undefined') || (document.all[campo].selectionStart == undefined)){
			rng = document.all[campo].createTextRange();
			rng.move("textedit");
			rng.select();
		}else{
			document.all[campo].selectionStart=document.all[campo].value.length;
			document.all[campo].selectionEnd=document.all[campo].value.length;
		}
	}

}
//FIM CAPTURA ELEMENTO ACIONADO POR ALGUM EVENTO E POSICIONA NO FINAL DO CAMPO.


//FORMATA VALOR
function formataValor(Campo)
{
	var tecla = 0;
	
	document.onkeypress = objSeleciona;

  Campo.value = String(Campo.value);

  //Formata valor do campos se a casa dos centavos estiver sem o zero depois do ponto
  //if (Campo.value.indexOf('.') != -1)
  //{ //alert(Campo.value);
  //  Campo.value = Campo.value + "0";
  //  Campo.value = Campo.value.substring(0,Campo.value.indexOf('.')+3);
  //}

	CampoFormatado = limpaCampo(Campo,"0123456789");

	if (CampoFormatado.length <= 2)
  { 
		//CENTAVOS
		CampoFormatado = CampoFormatado.replace(",","");
		if (CampoFormatado == "") CampoFormatado = "00";
		CampoFormatado = "0," + CampoFormatado;
		Campo.value = CampoFormatado;

	}
  else
  {

		if (CampoFormatado.length >= 3) 
    {

			//CENTENA
			CampoFormatado = CampoFormatado.substring(0,CampoFormatado.length-2) + "," + CampoFormatado.substring(CampoFormatado.length-2,CampoFormatado.length);

			//RETIRA ZEROS A ESQUERDA
			if (CampoFormatado.length >= 5) 
      {
				if (CampoFormatado.substring(0,1) == "0")
        {
					CampoFormatado = CampoFormatado.substring(1,CampoFormatado.length);
				}
			}
			//FIM RETIRA ZEROS A ESQUERDA

			if (CampoFormatado.length >= 7) 
      {

				//MILHAR
				CampoFormatado = CampoFormatado.substring(0,CampoFormatado.length-6) + "." + CampoFormatado.substring(CampoFormatado.length-6,CampoFormatado.length);

				if (CampoFormatado.length >= 11) 
        {
					//MILHÃO
					CampoFormatado = CampoFormatado.substring(0,CampoFormatado.length-10) + "." + CampoFormatado.substring(CampoFormatado.length-10,CampoFormatado.length);

					if (CampoFormatado.length >= 15) 
          {
						//BILHÃO
						CampoFormatado = CampoFormatado.substring(0,CampoFormatado.length-14) + "." + CampoFormatado.substring(CampoFormatado.length-14,CampoFormatado.length);

					}

				}
			
			}

			//PREENCHE CAMPO FORMATADO
			Campo.value = CampoFormatado;

		}

	}

}
//FIM FORMATA VALOR


//RETIRA CARACTERES INVÁLIDOS
function limpaCampo(Campo,ValorValido){

	var result = "";
	var aux;

	for (var i=0; i < Campo.value.length; i++) {

		aux = ValorValido.indexOf(Campo.value.substring(i, i+1));

		if (aux>=0) {

			result += aux;

		}

	}

	return result;
}
//FIM RETIRA CARACTERES INVÁLIDOS


//FORMATA DATA COM MM/AAAA
function FormataMesAno(Campo){

	if (Campo.value != "/"){
		if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)){
			if (Campo.value.length < 7){
				if (Campo.value.length == 2) Campo.value = Campo.value.substr(0,2) + "/" + Campo.value.substr(2,Campo.value.length);
			}
		}
	}else Campo.value = Campo.value.substring(0, Campo.value.length-1);
}


// TRATA VALOR
function trataValor(val)
{
  valfinal = "";

  if ((val != undefined) && (val != 'undefined'))
  {
    for(i=0; i<val.length; i++){
      if (val.substring(i,i+1) != ".") valfinal += val.substring(i,i+1);
    }
    valorTratado = valfinal;
    valorTratado = valorTratado.replace(",",".");
    
    return valorTratado;

  } else return '0,00';

}