/*********************************************************************************************
'##  OnlyNumber()
'##  Permite q seja digitado apenas números e teclas especiais
'*********************************************************************************************/
function OnlyNumber(teclapres){
	if(typeof(teclapres) == "undefined"){
		var tecla = event.keyCode;
		
		if (tecla == 9 || tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 || tecla == 13 || tecla == 46 || tecla == 37 || tecla == 39 || tecla == 35 || tecla == 36){ //teclas numéricas, back, TAB, ENTER, DEL
			event.returnValue=true;
		}else{
			event.returnValue=false;
		}

	}else{
		var tecla = teclapres.keyCode ? teclapres.keyCode : teclapres.which ? teclapres.which : teclapres.charCode;
		//alert(tecla);
		//if ((tecla >= 48 && tecla <= 57) || tecla == 9 || tecla == 8  || tecla == 13 || tecla == 46 || tecla == 37 || tecla == 39 || tecla == 35 || tecla == 36){ //teclas numéricas, back, TAB, ENTER, DEL
		if(tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && 
			tecla <= 105 || tecla == 13 || tecla == 37 || 
			tecla == 39 || tecla == 35 || 
			tecla == 36 || tecla == 46 || tecla == 9){
			return true;
		}else{
			return false;
		}

	}
}


/*********************************************************************************************
'##  FormataValor_OnBlur(Field, Dec)
'##  Adiciona Casas Decimais de um campo com valor inteiro
'*********************************************************************************************/
function FormataValor_OnBlur(Field, Dec){
	FieldValue = Field.value;

	Field.value = formatNumber(FieldValue, Dec);
}


/*********************************************************************************************
'##  formatNumber(Valor, Dec)
'##  Adiciona Casas Decimais de um campo com valor inteiro
'*********************************************************************************************/
function formatNumber(sValor, Dec){
	if(sValor == ""){
		return "";
	}

	FieldValue = new String(sValor);
	var strDecs = "";
	var vrTmp = "";
	//campo <> "" e sem nenhum setagem
	contPt = 0;
	if(FieldValue.indexOf(".") == -1 & FieldValue.indexOf(",") == -1 & FieldValue != ""){		
		// formatando pontos
		for(j=FieldValue.length;j>=0;j--){
			vrLetra = FieldValue.substr(j,1);
			vrTmp = vrLetra + vrTmp;
			if(contPt == 3 && j>0){
				contPt = 0;
				vrTmp = "." + vrTmp;						
			}
			contPt++;
		}

		//Casas decimais zero
		for(i=1;i<=Dec;i++){
			strDecs = strDecs + "0";
		}
		return vrTmp + "," + strDecs
	}
	
	if(FieldValue.indexOf(",") != -1 & FieldValue != ""){
		strCampo = FieldValue.split(",");
		strAntes = strCampo[0];
		strDepois = strCampo[1];
		while(strAntes.indexOf(".")>0){
			strAntes = strAntes.replace(".", "");
		}
		for(j=strAntes.length;j>=0;j--){
			vrLetra = strAntes.substr(j,1);
			vrTmp = vrLetra + vrTmp;
			if(contPt == 3 && j>0){
				contPt = 0;
				vrTmp = "." + vrTmp;						
			}
			contPt++;
		}

		//Casas decimais zero
		for(i=1;i<=Dec-strDepois.length;i++){
			strDecs = strDecs + "0";
		}
		return vrTmp + "," + strDepois + strDecs;
	}
	if(FieldValue.indexOf(".") != -1 & FieldValue != ""){
		strCampo = FieldValue.split(".");
		strAntes = strCampo[0];
		strDepois = strCampo[1];
		while(strAntes.indexOf(".")>0){
			strAntes = strAntes.replace(".", "");
		}
		for(j=strAntes.length;j>=0;j--){
			vrLetra = strAntes.substr(j,1);
			vrTmp = vrLetra + vrTmp;
			if(contPt == 3 && j>0){
				contPt = 0;
				vrTmp = "." + vrTmp;						
			}
			contPt++;
		}

		//Casas decimais zero
		if(strDepois.length < Dec){
			for(var i=1;i<=Dec-strDepois.length;i++){
				strDecs = strDecs + "0";
			}
		}else{
			strDepois = strDepois.substring(0,2);
		}
		return vrTmp + "," + strDepois + strDecs;
	}
	if(FieldValue.indexOf(".") != -1 & FieldValue != ""){
		strCampo = FieldValue.split(".");
		strAntes = strCampo[0];
		strDepois = strCampo[1];
		while(strAntes.indexOf(".")>0){
			strAntes = strAntes.replace(".", "");
		}
		for(j=strAntes.length;j>=0;j--){
			vrLetra = strAntes.substr(j,1);
			vrTmp = vrLetra + vrTmp;
			if(contPt == 3 && j>0){
				contPt = 0;
				vrTmp = "." + vrTmp;						
			}
			contPt++;
		}

	
		//Casas decimais zero
		if(strDepois.length < Dec){
			for(var i=1;i<=Dec-strDepois.length;i++){
				strDecs = strDecs + "0";
			}
		}else{
			strDepois = strDepois.substring(0,2);
		}
		return vrTmp + "," + strDepois + strDecs;
	}
}

function formatDataExibir(Data){
	if(Data == null){
		return "";			
	}
	if(Data != ""){
		var vtData;

		//se estiver formatado com data e hora
		if(Data.indexOf(" ") != -1){
			var vtTmp = Data.split(" ");
			vtData = vtTmp[0].split("-");
		}else{
			vtData = Data.split("-");
		}		
		
		return vtData[2] + "/" + vtData[1] + "/" + vtData[0];
	}else{
		return "";
	}
}
 
 
/*********************************************************************************************
'##  FormataValor(campo, NroDecimais)
'##  Formata o valor dinamicamente
'*********************************************************************************************/
function FormataValor_OnKeyDown(objCampo, NroDecimais, evtKeyPress){

	if(window.event){
		var tecla = evtKeyPress.keyCode; 
	}
	
	//Eventos Firefox / mozila
	else if(evtKeyPress.which){
		var tecla = evtKeyPress.which;
	}

	vr = objCampo.value;
	var SetaPontos = false;

	if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && 
			tecla <= 105 || tecla == 110 || tecla == 188 || tecla == 190 || 
			tecla == 13 || tecla == 37 || tecla == 39 || tecla == 35 || 
			tecla == 36 || tecla == 46 || tecla == 9 || tecla == 194){ // teclas permitidas
		
		if(tecla == 110 || tecla == 188 || tecla == 190 || tecla == 194){ //caso a tecla seja ponto trocar por virgula
			if(vr.indexOf(",") > -1 || vr.indexOf(".") > -1 ){
				evtKeyPress.returnValue = false;
				return false;
			}

			
			vr = vr + ",";
			SetaPontos = true;
			if(vr.substr(0,1) == ","){ // se o 1º dígito for ","
				vr = "0" + vr;
				objCampo.value = vr;
				SetaPontos = false;
			}
		}

		var nroVirgula = vr.split(",");
		if(nroVirgula.length > 1){
			if(tecla == 8 || tecla == 46){
				if(vr.substr(vr.length-1, 1) == ","){
					vr = vr.substr(0,vr.length);
					while(vr.indexOf(".")>0){ //se nao tiver mais virgula, checar se existem pontos e remover
						vr = vr.replace(".","");
					}
					objCampo.value = vr;
				}					
				return true;
			}
			if((nroVirgula[1].length == NroDecimais)){
				return false;
			}
		}

		if(SetaPontos){ //caso tenha o NroDecimais, irá formatar o número (pontos e virgulas)
			contPt = 0;
			vrTmp = "";
			
			//alert(nroVirgula.length);
			for(i=nroVirgula[0].length;i>=0;i--){
				vrLetra = nroVirgula[0].substr(i,1);
				vrTmp = vrLetra + vrTmp;
				if(contPt == 3 && i>0){
					contPt = 0;
					vrTmp = "." + vrTmp;						
				}
				contPt++;
			}
			vr = vrTmp + "," + nroVirgula[1];
			//alert(vr)
			//alert(vr.substr(vr.length-1,1));
			objCampo.value = vr;
			return false;
		}		
	}else{
		evtKeyPress.returnValue = false;
		return false;
	}

}

/*********************************************************************************************
'##  FormataData_OnKeyDown(campo)
'##  Formata a data para DD/MM/AAAA
'*********************************************************************************************/
function FormataData_OnKeyDown(campo){
	var NroDigitos = campo.value.length;
	var tecla = event.keyCode;
	if(tecla != 8 && tecla != 9){
		OnlyNumber();
		if(NroDigitos == 10){
			event.returnValue = false;
		}
		if(NroDigitos == 2 || NroDigitos == 5){
			campo.value = campo.value + "/";
		}
	}
}

/*********************************************************************************************
'##  FormataDataHora_OnKeyDown(campo)
'##  Formata a data para DD/MM/AAAA
'*********************************************************************************************/
function FormataDataHora_OnKeyDown(campo, teclapres){
	var NroDigitos = campo.value.length;
	
	//var tecla = event.keyCode;

	var tecla = teclapres.keyCode ? teclapres.keyCode : teclapres.which ? teclapres.which : teclapres.charCode;



	if(tecla != 8 && tecla != 9){
		OnlyNumber(teclapres);
		if(NroDigitos == 16){
			event.returnValue = false;
		}
		if(NroDigitos == 2 || NroDigitos == 5){
			campo.value = campo.value + "/";
		}
		if(NroDigitos == 10){
			campo.value = campo.value + " ";
		}

		if(NroDigitos == 13){
			campo.value = campo.value + ":";
		}
	}
}

/*********************************************************************************************
'##   mask_OnKeyPress(this, '##/##/####', event)
'##  Formata campo numérico para o formato da máscara
'*********************************************************************************************/
function mask_OnKeyPress(campo, mask, evtKeyPress){
	var nTecla, sCampo, letra, sCampoLimpo, nros;
	var sNovoValor = "";
	sCampoLimpo = "";
	sCampo = campo.value;
	nros = "0123456789";
	
	//Eventos IE
	if(window.event){
		nTecla = evtKeyPress.keyCode; 
	}
	
	//Eventos Firefox / mozila
	else if(evtKeyPress.which){
		nTecla = evtKeyPress.which;
	}
	//alert(nTecla);
	if(nTecla != 8){

		if(!OnlyNumber(evtKeyPress)){ //checando se foi digitado apenas números
			return false;
		}
		if(typeof(nTecla) == "undefined"){
			return true;	
		}
		if((sCampo.length == mask.length) && (campo.selectionStart == campo.selectionEnd )){
			return false;
		}
		

		for(i=0;i<=sCampo.length-1;i++){ //Limpando o campo digitado e pegando apenas números
			if(nros.indexOf(sCampo.charAt(i)) >= 0){
				sCampoLimpo += sCampo.charAt(i);
			}
		}

		//Montando a máscara
		var iMask = 0;
		for(i=0;i<=sCampoLimpo.length-1;i++){
			letra = sCampoLimpo.charAt(i);
			if(i == 0 && mask.charAt(0) != "#"){
				sNovoValor += mask.charAt(0)  + letra ;
				iMask++;
			}else{
				if(mask.charAt(iMask + 1) != "#"){
					sNovoValor += letra + mask.charAt(iMask + 1);
					iMask++;
				}else{
					sNovoValor += letra;
				}
			}
			iMask++;
		}
		campo.value = sNovoValor;
	}
}
