﻿function remove(str, sub) {
i = str.indexOf(sub);
r = "";
if (i == -1) return str;
r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
return r;
}

function validarTxt(txtInput, strMsg) {
	if (txtInput.value.length == 0 ) {
		if(typeof(strMsg) != "undefined"){
			alert(strMsg);
		}
		txtInput.focus();
		return false;
	}
	return true;
}

function validarEmail(txtEmail, strMsg) {

	if (txtEmail.value.length == 0 ) {
		if(typeof(strMsg) != "undefined"){
			alert(strMsg);
		}
		txtEmail.focus();
		return false;
	}else if(txtEmail.value.length)

	//var strEmailPat=/^[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*\\.[A-Za-z0-9]{2,4}$/;
	var strEmailPat=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{1,3})+$/;
	var matchArray=txtEmail.value.match(strEmailPat);

	if (matchArray==null) {
		if(typeof(strMsg) != "undefined"){
			alert(strMsg);
		}
		txtEmail.focus();
		return false;
	}
	return true;
}

function validarCPF(txtInput, strMsg) {
	if (txtInput.value.length == 0 ) {
		if(typeof(strMsg) != "undefined"){
			alert(strMsg);
		}
		txtInput.focus();
		return false;
	}
	
	var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
	if(!filtro.test(txtInput.value)){
		if(typeof(strMsg) != "undefined"){
		alert(strMsg);
		}
		txtInput.focus();
		return false;
	}
   
   var inputValue = remove(txtInput.value, ".");
   inputValue = remove(inputValue, "-");
	
	if(inputValue.length != 11 || inputValue == "00000000000" || inputValue == "11111111111" ||
	inputValue == "22222222222" || inputValue == "33333333333" || inputValue == "44444444444" ||
	inputValue == "55555555555" || inputValue == "66666666666" || inputValue == "77777777777" ||
	inputValue == "88888888888" || inputValue == "99999999999"){
		if(typeof(strMsg) != "undefined"){
			alert(strMsg);
		}
		return false;
	}

	soma = 0;
	for(i = 0; i < 9; i++)
	 soma += parseInt(inputValue.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
	 resto = 0;
	if(resto != parseInt(inputValue.charAt(9))){
		if(typeof(strMsg) != "undefined"){
			alert(strMsg);
		}
		txtInput.focus();
		return false;
	}
	soma = 0;
	for(i = 0; i < 10; i ++)
	 soma += parseInt(inputValue.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
	 resto = 0;
	if(resto != parseInt(inputValue.charAt(10))){
		if(typeof(strMsg) != "undefined"){
			alert(strMsg);
		}
		txtInput.focus();
		return false;
	}
	return true;
 }
 
function validarCNPJ(txtInput, strMsg) {
	//Declaração as variáveis
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais, cnpj;
	cnpj = txtInput.value;
	
	//Verificando se o campo é nulo
	if (txtInput.value.length == 0 ) {
		if(typeof(strMsg) != "undefined"){
			alert(strMsg);
		}
		txtInput.focus();
		return false;
	}
	
	//Filtrar o campo para verificar se está com máscara
	var filtro = /\d{2,3}.\d{3}.\d{3}\/\d{4}-\d{2}/;
	if(!filtro.test(cnpj)) {
	 window.alert(strMsg);
	 return false;
	}
	  //Ultilização expressão regular para retirar o que não for número
	  cnpj = cnpj.replace(/\D+/g, '');
	  digitos_iguais = 1;

	  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)) {
				alert(strMsg);
				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)) {
				alert(strMsg);
				return false;
			}
			return true;
		}else {
			alert(strMsg);
			return false;
		}
	return true;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

// Função que valída os campos
function validarContato(objForm) {
	var bolValida = true;
	if(objForm.nome) {
		bolValida = validarTxt(objForm.nome, "Por favor informe o nome.");
		if (!bolValida)
		return false;
	}
	if(objForm.email) {
		bolValida = validarEmail(objForm.email, "Por favor informe um e-mail válido.");
		if (!bolValida)
		return false;
	}
	if(objForm.telefone) {
		bolValida = validarTxt(objForm.telefone, "Por favor informe o telefone.");
		if (!bolValida)
		return false;
	}
	if(objForm.assunto) {
		bolValida = validarTxt(objForm.assunto, "Por favor digite o assunto.");
		if (!bolValida)
		return false;
	}
	if(objForm.mensagem) {
		bolValida = validarTxt(objForm.mensagem, "Por favor digite a mensagem.");
		if (!bolValida)
		return false;
	}
	return bolValida;
}
