function limpaAcentos(texto) {
	var comacento = "âáàäãéèêëíìîïóòôöõúùûüçñÂÁÀÄÃÉÈÊËÍÌÎÏÓÒÔÖÕÚÙÛÜÇÑ"
	var semacento = "aaaaaeeeeiiiiooooouuuucnAAAAAEEEEIIIIOOOOOUUUUCN"
	var cont = 0;
	for (cont = 0; cont < texto.length; cont += 1) {
		posicao = comacento.indexOf(texto.charAt(cont));
		if ( posicao >= 0) {
			texto = texto.substr(0, cont) + semacento.substr(posicao, 1);
		}
	}
	return texto;
}

function limpaLogin(texto) {
	var letras = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&(){}][+=.,;";
	var cont = 0;
	while (cont < texto.length) {
		if (letras.indexOf(texto.charAt(cont)) < 0) {
			texto = texto.substr(0, cont) + texto.substr(cont + 1);
		} else {
			cont++;
		}
	}
	return texto;
}

function limpaCampo(valor) {
	var numeros = "0123456789";
	var cont = 0;
	while (cont < valor.length) {
		if (numeros.indexOf(valor.charAt(cont)) < 0) {
			valor = valor.substr(0, cont) + valor.substr(cont + 1);
		} else {
			cont++;
		}
	}
	return valor;
}

function FormataLogin(campo,teclapres) {
	var nmCampo = document.getElementById(campo);
	if (nmCampo) {
		var tecla = teclapres.keyCode;
		vr = limpaLogin(nmCampo.value);
		nmCampo.value = vr;
	}
}

function FormataNumero(campo,teclapres) {
	var nmCampo = document.getElementById(campo);
	if (nmCampo) {
		var tecla = teclapres.keyCode;
		vr = limpaCampo(nmCampo.value);
		nmCampo.value = vr;
	}
}

function FormataValor(campo,decimais,teclapres) {
	var nmCampo = document.getElementById(campo);
	if (nmCampo) {
		var tecla = teclapres.keyCode;
		vr = limpaCampo(nmCampo.value);
		tam = vr.length;

		if (tam > decimais) {
			fracionaria = vr.substr(vr.length - decimais, decimais);
			inteira = vr.substr(0, vr.length - decimais);
			// Faz com inteira o que fez com identidade (separa em partes 3 a 3)
			tam = inteira.length;
			vraux = inteira.substr(0, (tam % 3));
			for (cont = (tam % 3); cont < tam; cont += 3)
				 vraux = vraux + '.' + inteira.substr(cont, 3);
			if (tam % 3 == 0) vraux = vraux.substr(1);
			vr = vraux + ',' + fracionaria;
		 }
		nmCampo.value = vr;
	}
}

function FormataValorLimpo(campo,decimais,teclapres) {
	var nmCampo = document.getElementById(campo);
	if (nmCampo) {
		var tecla = teclapres.keyCode;
		vr = limpaCampo(nmCampo.value);
		tam = vr.length;

		if (tam > decimais) {
			fracionaria = vr.substr(vr.length - decimais, decimais);
			inteira = vr.substr(0, vr.length - decimais);
			vr = inteira + ',' + fracionaria;
		 }
		nmCampo.value = vr;
	}
}

function FormataData(campo,teclapres) {
	var nmCampo = document.getElementById(campo);
	if (nmCampo) {
		var tecla = teclapres.keyCode;
		vr = limpaCampo(nmCampo.value);
		tam = vr.length + 1;
		if (tam > 3) {
			vr = vr.substr(0, 2) + '/' + vr.substr(2);
			if (tam > 5)
				vr = vr.substr(0, 5) + '/' + vr.substr(5);
		}
		nmCampo.value = vr;
	}
}

function FormataHora(campo,teclapres) {
	var nmCampo = document.getElementById(campo);
	if (nmCampo) {
		var tecla = teclapres.keyCode;
		vr = limpaCampo(nmCampo.value);
		tam = vr.length + 1;
		if (tam > 3) {
			vr = vr.substr(0, 2) + ':' + vr.substr(2);
			if (tam > 5) {
				vr = vr.substr(0, 5) + ':' + vr.substr(5);
			}
		}
		nmCampo.value = vr;
	}
}

function FormataDataHora(campo,teclapres) {
	var nmCampo = document.getElementById(campo);
	if (nmCampo) {
		var tecla = teclapres.keyCode;
		vr = limpaCampo(nmCampo.value);
		tam = vr.length + 1;
		if (tam > 3) {
			vr = vr.substr(0, 2) + '/' + vr.substr(2);
			if (tam > 5) {
			 	vr = vr.substr(0, 5) + '/' + vr.substr(5);
				if (tam > 9) {
					vr = vr.substr(0, 10) + ' ' + vr.substr(10);
					if (tam > 11) {
						 vr = vr.substr(0, 13) + ':' + vr.substr(13);
					}
				}
			}
		}
		nmCampo.value = vr;
	}
}

function FormataCEP(campo,teclapres) { //00000-000
	var nmCampo = document.getElementById(campo);
	if (nmCampo) {
		var tecla = teclapres.keyCode;
		vr = limpaCampo(nmCampo.value);
		tam = vr.length + 1;
		if (tam > 5) {
	 		vr = vr.substr(0, 5) + '-' + vr.substr(5);
		}
		nmCampo.value = vr;
	}
}

function FormataTelefone(campo,teclapres) { //(00)0000.0000
	var nmCampo = document.getElementById(campo);
	if (nmCampo) {
		var tecla = teclapres.keyCode;
		vr = limpaCampo(nmCampo.value);
		vr = vr.replace(/(\.|\(|\)|\/|\-| )+/g,'');
		tam = vr.length + 1;

		if (tam > 2 && tam <= 6)
			vr = '(' + vr.substr(0,2) + ') ' + vr.substr(2,tam);
		if (tam >= 7 && tam < 12)
			vr = '(' + vr.substr(0,2) + ') ' + vr.substr(2,4) + '.' + vr.substr(6,tam-6);
		nmCampo.value = vr;
	}
}

function FormataCpfCgc(campo,teclapres) {
	var nmCampo = document.getElementById(campo);
	if (nmCampo) {
		if (nmCampo.value.length <= 14) {
			FormataCpf(campo,teclapres)
		} else {
			FormataCgc(campo,teclapres)
		}
	}
}

function FormataCpf(campo,teclapres) {
	var nmCampo = document.getElementById(campo);
	if (nmCampo) {
		var tecla = teclapres.keyCode;
		vr = limpaCampo(nmCampo.value);
		tam = vr.length + 1;

		if (tam > 4) {
			vr = vr.substr(0, 3) + '.' + vr.substr(3);
			if (tam > 7)
				vr = vr.substr(0, 7) + '.' + vr.substr(7);
				if (tam > 10)
					vr = vr.substr(0, 11) + '-' + vr.substr(11);
		}
		nmCampo.value = vr;
	}
}

function FormataCgc(campo,teclapres) {
	var nmCampo = document.getElementById(campo);
	if (nmCampo) {
		var tecla = teclapres.keyCode;
		vr = limpaCampo(nmCampo.value);
		tam = vr.length + 1;

		if (tam > 3) {
			vr = vr.substr(0, 2) + '.' + vr.substr(2);
			if (tam > 6)
				vr = vr.substr(0, 6) + '.' + vr.substr(6);
				if (tam > 9)
					vr = vr.substr(0, 10) + '/' + vr.substr(10);
					if (tam > 13)
						vr = vr.substr(0, 15) + '-' + vr.substr(15);
		}
		nmCampo.value = vr;
	}
}

function FormataTextarea(campo,maximo,barra,tema) {
	var nmCampo = document.getElementById(campo);
	var progresso = 100 - parseInt(nmCampo.value.length/maximo*100)
	if (nmCampo.value.length >= maximo) {
		nmCampo.value = nmCampo.value.substring(0,maximo);
		$("#"+campo).css("background-color","#eeeeee");
	} else {
		$("#"+campo).css("background-color","#ffffff");
	}
	$("#barra_"+campo+"_texto").text("Caracteres restantes: " + (maximo - nmCampo.value.length))
	return progresso;
}

function limpaChat(campo,teclapres) {
	var nmCampo = document.getElementById(campo);
 	if (nmCampo) {
		var caracteres = "+><&";
		var cont = 0;
		valor = nmCampo.value;
		while (cont < valor.length) {
			if (caracteres.indexOf(valor.charAt(cont)) >= 0)
				valor = valor.substr(0, cont) + valor.substr(cont + 1);
			else
				cont++;
		}
		nmCampo.value = valor;
	}
}

