function removeSpecialCharacter(text) {
	var padrao = /[=#$%*\'\"\)<\(>]/;
	invalid = padrao.exec(text);
	if (invalid){
		return removeSpecialCharacter(replaceAll(text, invalid[0], ""));
	} else {
		return text;
	} 
}

function replaceAll(string, token, newtoken) {
	while (string.indexOf(token) != -1) {
		string = string.replace(token, newtoken);
	}
	return string;
}

function verificarMensagem(textoAntes, textoDepois) {
	if(textoAntes != textoDepois) {
		alert('N\u00E3o \u00E9 permitida a inser\u00E7\u00E3o dos caracteres: =#$\'%*\"\)<\(>');
	}
}

//Retorna TRUE caso o campo esteja vazio
function isEmpty(campo) {
	if(trim(campo.value) == "") {
		return true;
	} else {
		return false;
	}
	
}

//TRIM
//Sem informar o segundo parâmetro, as functions abaixo farão trim com os seguintes caracteres:
// " " , "\t" (tab) , "\n" (nova linha) , "\r" (retorno de carro) , "\0" (NUL-byte) e "\x0B" (barra vertical)

//Retira os espaços à esquerda e direita de uma String
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
//Retira os espaços à esquerda de uma String 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
//Retira os espaços à direita de uma String
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
//FIM DO TRIM

function valida(event, obj) {
	if(event.keyCode == 13) {
		return true;
	}
	var textoAntes = $(obj).val();
	$(obj).val(removeSpecialCharacter($(obj).val()));
	verificarMensagem(textoAntes, $(obj).val());
}

//Retorna TRUE caso o email seja inválido
function isInvalidEmailAddress(campo) {
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(campo.value))) {
		return true;
	} else {
		return false;
	}
}


//Valida se é um CPF ou CNPJ
//Retorna: 0 - Se for um CPF ou CNPJ válido
//		   1 - Se for um CPF ou CNPJ incorreto (menos ou mais dígitos)
//         2 - Se for um CPF ou CNPJ inválido	
function validarCPFCNPJ(campo) {
	
	if(campo.value.length == 11) {
		if(!isValidCPF(campo)) {
			return 2;
		}	
		
	} else if(campo.value.length == 14) {
		if(!isValidCNPJ(campo)) {
			return 2;
		}
	
	} else {
		return 1;
	}
	
	return 0;
	
}

//Valida se é um CPF: Retorna TRUE se for válido
function isValidCPF(campo) {

	var numeros, digitos, soma, i, resultado, digitos_iguais, cpf;
    digitos_iguais = 1;
	cpf = campo.value;
	
	if (cpf.length < 11) {
		return false;
	}
	
	for (i = 0; i < cpf.length - 1; i++) {
		if (cpf.charAt(i) != cpf.charAt(i + 1)) {
		  digitos_iguais = 0;
		  break;
		}
	}	
	
	if (!digitos_iguais) {
		numeros = cpf.substring(0,9);
		digitos = cpf.substring(9);
		soma = 0;
		for (i = 10; i > 1; i--) {
			soma += numeros.charAt(10 - i) * i;
		}	
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(0)) {
			return false;
		}	
		numeros = cpf.substring(0,10);
		soma = 0;
		for (i = 11; i > 1; i--) {
			soma += numeros.charAt(11 - i) * i;
		}	  
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(1)) {
			return false;
		}	  
		return true;
		
	} else {
		return false;
	}	
}


//Valida se é um CNPJ: Retorna TRUE se for válido
function isValidCNPJ(campo) {
	if (campo.value.length != 14) {
		return false;
	}
	
	var pos;
	var peso;
	var soma;
	var digito_verificador;
	
	// Calculo do DV1
	pos=12;
	peso=2;
	soma=0;
	while (pos)
	{
		soma+= (parseInt(campo.value.charAt(pos-1))) * peso;
		peso++;
		if (peso > 9)
			peso = 2;
		pos--;
	}

	if (soma == 0) {
		return false;
	}

	digito_verificador=11-(soma % 11);

	if ((soma % 11)<2) {
		digito_verificador=0;
	}
	
	if (parseInt(campo.value.charAt(12))!=digito_verificador) {
	    return false;
	}

	// Calculo do DV2
	pos=13;
	peso=2;
	soma=0;
	while (pos)
	{
		soma+= (parseInt(campo.value.charAt(pos-1))) * peso;
		peso++;
		if (peso > 9)
			peso = 2;
		pos--;
	}
	digito_verificador=11-(soma % 11);
	if ((soma % 11)<2) {
		digito_verificador=0;
	}
	
	if (parseInt(campo.value.charAt(13))!=digito_verificador) {
	   return false;
	}

	return true;
}
