function checkIfIsNum(ev) {
	if(ev.keyCode) tecl=ev.keyCode; else tecl=ev.which;
	if(tecl) {
		if(tecl!=9 &&  //Tab
			tecl!=8 &&  //Backspace
		   tecl!=35 && //End
			tecl!=36 && //Home
			tecl!=37 && //Seta esquerda
			tecl!=39 && //Seta direita
			tecl!=46 && //Delete
			(tecl<48 || tecl>57) //qq outra coisa além de Números de 0 a 9
		  ) return false;
	}
	return true;
}

function checaEmail(campo){
	var tam = campo.length;			
	var ixArroba = campo.indexOf("@");
	var rxArroba = campo.lastIndexOf("@");
	
	if(campo.indexOf(" ") == -1)
		if((ixArroba == rxArroba) && (ixArroba > 0) && (ixArroba != (tam-1)))
			if(campo.indexOf(".", ixArroba +1) > 1 && campo.lastIndexOf(".") < (tam-1))
				return true;
    return false;
}

function temNPalavras(txt,numPal) {
	var tp_cnt = 0;
	var tp_txt = sanizaString(txt);
	
	if(tp_txt.length>=1) tp_cnt = 1;
	
	tp_pos = tp_txt.indexOf(" ");
	while (tp_pos != -1) {
		tp_cnt++;
		tp_pos = tp_txt.indexOf(" ",tp_pos+1);
	}
	if(tp_cnt<numPal) return false; else return true;
}

function sanizaString(txt) {
	var tmpStr = txt;
	while (tmpStr.indexOf("  ")>-1) tmpStr=tmpStr.replace(/  /g," ");
	while (tmpStr.charAt(0)==" ") tmpStr=tmpStr.slice(1);
	while (tmpStr.charAt(tmpStr.length-1)==" ") tmpStr=tmpStr.slice(0,-1);
	return tmpStr;
}

function focaCampo(campo) {
	campo.focus();
	if(campo.type=="text" || campo.type=="password") campo.select();
}

//VALIDA CNPJ
function valida_cnpj(cnpj) {
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
	digitos_iguais = 1;
	if (cnpj.length < 13 || cnpj.length > 14) return false;
	for (i = 0; i < cnpj.length - 1; i++)
		if (cnpj.charAt(i) != cnpj.charAt(i + 1)) {
			digitos_iguais = 0;
			break;
		}
	if (!digitos_iguais) {
		tamanho = cnpj.length - 2
		numeros = cnpj.substring(0,tamanho);
		digitos = cnpj.substring(tamanho);
		soma = 0;
		pos = tamanho - 7;
		for (i = tamanho; i >= 1; i--) {
			soma += numeros.charAt(tamanho - i) * pos--;
			if (pos < 2) pos = 9;
		}
		
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(0)) return false;
		tamanho = tamanho + 1;
		numeros = cnpj.substring(0,tamanho);
		soma = 0;
		pos = tamanho - 7;
		for (i = tamanho; i >= 1; i--) {
			soma += numeros.charAt(tamanho - i) * pos--;
			if (pos < 2) pos = 9;
		}

		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(1)) return false;
		
		return true;
	} else return false;
}
