
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 validaForm(frm) {
	var erro  = true;
	var vazio = /^\s*$/;
	var num   = /^([0-9]{1,})$/;
	var email = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	var data  = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;   
   
    if(document.getElementsByClassName) {
		var campoString 	= frm.getElementsByClassName("string");
		var camposNumero 	= frm.getElementsByClassName("numero");
		var camposEmail 	= frm.getElementsByClassName("email");
		var camposData  	= frm.getElementsByClassName("data");
   
		if(campoString.length > 0)
			for(x=0;x < campoString.length; x++) {
				if(vazio.test(campoString[x].value)) {
					bordaCampo(campoString[x],'#006699');
					erro = false;
				}
			}
     
		if(camposNumero.length > 0)
			for(x=0;x < camposNumero.length; x++) {
				if(num.test(camposNumero[x].value) == false) {
					bordaCampo(camposNumero[x],'#006699');
					erro = false;
				}
			}
   
		if(camposEmail.length > 0)
			for(x=0;x < camposEmail.length; x++) {
				if(email.test(camposEmail[x].value) == false) {
					bordaCampo(camposEmail[x],'#006699');
					erro = false;
				}
			}
   	 
		if(camposData.length > 0) 
			for(x=0;x < camposData.length; x++) {
				if(data.test(camposData[x].value) == false) { 
					bordaCampo(camposData[x],'#006699')
					erro = false;
				}     
			}
	} else {
		var elements = frm.getElementsByTagName('input');
		for(x = 0; x < elements.length; x++) {
			if (elements[x].className == "string") {
				if(vazio.test(elements[x].value)) {
					bordaCampo(elements[x],'#006699'); 
					erro = false; }
			}	
			else if(elements[x].className == "numero") {
				if(num.test(elements[x].value) == false) {
					bordaCampo(elements[x],'#006699'); 
					erro = false; }
			}
			else if(elements[x].className == "email") {
				if(email.test(elements[x].value) == false) {
					bordaCampo(elements[x],'#006699'); 
					erro = false; }
			}
			else if(elements[x].className == "data") {
				if(data.test(elements[x].value) == false) {
					bordaCampo(elements[x],'#006699'); 
					erro = false; }
			}
		  }
	}
return erro; 
}
    
function bordaCampo(campo,cor) {
	campo.style.backgroundColor = cor;
	return true; }
	
function deletarRegistro(url) {
	window.alert(url);
	if(window.confirm('Deseja excluir o registro?')) {
		window.location=url;
	}
}	
