/*
* VALIDAÇÃO GERAL DE FORMULÁRIOS POR RCDMK - Agosto de 2010
* 
* Versão 2.1
* 	- Corrigido o problema com CPF's e CNPJ's válidos dados como inválidos;
* 	- Agora é permitido utilizar as validações de formato, etc., sem o campo ser obrigatório
*
* USO
* Insira um atributo "rel" no campo para validar:
* 	- Preenchimento obigatório: o primeiro caractere do atributo tem que ser um # e o restante, separado por um espaço, será utilizado na mensagem de erro. Ex.: rel="# o nome do campo"
* 	- Validação de e-mail: o segundo caractere do atributo tem que ser @. Ex.: rel="#@ o e-mail de contato" (obrigatório) ou rel=" @ o e-mail de contato" (não obrigatório)
* 	- Validação de CPF: o segundo caractere tem que ser F (de pessoa Física);
* 	- Validação de CNPJ: o segundo caractere tem que ser J (de pessoa Jurídica);
* 	- Validação de tamanho ou comprimento do valor: o segundo caractere tem que ser T e o terceiro o tamanho a ser validado. Ex.: rel="#T9 o telefone"
* 	- Validação por comparação igual (senha e confirmação de senha, etc): o segundo caractere tem que ser = e o terceiro o ID do campo a ser comparado. Ex.: rel="#=confirmaSenha a senha"
*/
function validaform(formulario){
	var pass = true;
	var passCompara = true;
	var msg = 'Atenção:\n\n';
	var msgCompara = msg;
	var campofoco = false;

	if (formulario.elements) {
		var campos = formulario.elements;

		for (var i = 0; i < campos.length; i++) {
			var elemento = campos[i];
			var atual = "";

			if (elemento.attributes && elemento.attributes.rel && elemento.type.toLowerCase() != 'image' && elemento.type.toLowerCase() != 'submit' && elemento.type.toLowerCase() != 'button' && elemento.type.toLowerCase() != 'hidden' && elemento.type.toLowerCase() != 'reset') {
				marcaDesmarcaCampo(elemento, false);
				
				if (atual != elemento.name) {
					atual = elemento.name;
					
					if (elemento.attributes.rel.value.indexOf("#") == 0) {
						//validação de vazio (obrigatório)
						if (((elemento.type.toLowerCase() == "text" || elemento.type.toLowerCase() == "textarea" || elemento.type.toLowerCase() == "file" || elemento.type.toLowerCase() == "password" || elemento.type.toLowerCase().indexOf("select") == 0) && elemento.value == "") || (elemento.type.toLowerCase().indexOf("select") == 0 && (elemento.selectedIndex) ? elemento.selectedIndex == -1 : false)) {
							msg += '- Informe ' + ((elemento.attributes && elemento.attributes.rel && elemento.attributes.rel.value != "#") ? elemento.attributes.rel.value.substring(elemento.attributes.rel.value.indexOf(' ', 1)) : elemento.name) + '.\n';
							pass = false;
							marcaDesmarcaCampo(elemento, true);
							if (!campofoco) campofoco = elemento;
							
						} else if (elemento.type.toLowerCase() == "radio" || elemento.type.toLowerCase() == "checkbox") {
							var opcoes = document.getElementsByName(atual);
							pass = false;
							
							for (var j = 0; j < opcoes.length; j++) {
								if (opcoes[j].checked) {
									pass = true;
									break;
								}
							}
							
							if (!pass) {
								msg += '- Informe ' + ((elemento.attributes && elemento.attributes.rel && elemento.attributes.rel.value != "#") ? elemento.attributes.rel.value.substring(elemento.attributes.rel.value.indexOf(' ', 1)) : elemento.name) + '.\n';
								pass = false;
								marcaDesmarcaCampo(elemento, true);
								if (!campofoco) campofoco = elemento;
							}
						}
					}
					
					//Para otimizar o desempenho, só valida os formatos se passar por todos os obrigatórios
					if (pass && elemento.value != "" && (elemento.type.toLowerCase() == "text" || elemento.type.toLowerCase() == "textarea" || elemento.type.toLowerCase() == "password")) {
						//Tamanho =
						if(elemento.attributes.rel.value.indexOf("T") == 1) {
							var segundo = elemento.attributes.rel.value.substring(2, elemento.attributes.rel.value.indexOf(' ', 1));
							
							console.log(segundo);
							
							if (elemento.value.length != Number(segundo)) {
								msgCompara += '- ' + ((elemento.attributes && elemento.attributes.rel && elemento.attributes.rel.value != "#") ? elemento.attributes.rel.value.substring(elemento.attributes.rel.value.indexOf(' ', 1)) : elemento.name) + ' precisa ter ' + segundo + ' caracteres.\n'
								if (!campofoco) campofoco = elemento;
								marcaDesmarcaCampo(elemento, true);
								passCompara = false;
							}
						}
						
						//Comparação =
						if(elemento.attributes.rel.value.indexOf("=") == 1) {
							var segundo = document.getElementById(elemento.attributes.rel.value.substring(2, elemento.attributes.rel.value.indexOf(' ', 1)));
							
							if (elemento.value != segundo.value) {
								msgCompara += '- ' + ((elemento.attributes && elemento.attributes.rel && elemento.attributes.rel.value != "#") ? elemento.attributes.rel.value.substring(elemento.attributes.rel.value.indexOf(' ', 1)) : elemento.name) + ' e ' + ((elemento.attributes && elemento.attributes.rel && elemento.attributes.rel.value != "#") ? segundo.attributes.rel.value.substring(segundo.attributes.rel.value.indexOf(' ', 1)) : segundo.name) + ' não batem.\n'
								if (!campofoco) campofoco = elemento;
								marcaDesmarcaCampo(elemento, true);
								passCompara = false;
							}
						}
						
						//Comparação >
						else if(elemento.attributes.rel.value.indexOf(">") == 1) {
							var segundo = document.getElementById(elemento.attributes.rel.value.substring(2, elemento.attributes.rel.value.indexOf(' ', 1)));
	
							if (isNaN(cnvvl(elemento.value)) || isNaN(cnvvl(segundo.value)) || cnvvl(elemento.value) < cnvvl(segundo.value)) {
								msgCompara += '- ' + ((elemento.attributes && elemento.attributes.rel && elemento.attributes.rel.value != "#") ? elemento.attributes.rel.value.substring(elemento.attributes.rel.value.indexOf(' ', 1)) : elemento.name) + ' não pode ser menor que ' + ((elemento.attributes && elemento.attributes.rel && elemento.attributes.rel.value != "#") ? segundo.attributes.rel.value.substring(segundo.attributes.rel.value.indexOf(' ', 1)) : segundo.name) + '.\n'
								if (!campofoco) campofoco = elemento;
								marcaDesmarcaCampo(elemento, true);
								passCompara = false;
							}
						}
						
						//Comparação <
						else if(elemento.attributes.rel.value.indexOf("<") == 1) {
							var segundo = document.getElementById(elemento.attributes.rel.value.substring(2, elemento.attributes.rel.value.indexOf(' ', 1)));
	
							if (isNaN(cnvvl(elemento.value)) || isNaN(cnvvl(segundo.value)) || cnvvl(elemento.value) > cnvvl(segundo.value)) {
								msgCompara += '- ' + ((elemento.attributes && elemento.attributes.rel && elemento.attributes.rel.value != "#") ? elemento.attributes.rel.value.substring(elemento.attributes.rel.value.indexOf(' ', 1)) : elemento.name) + ' não pode ser maior que ' + ((elemento.attributes && elemento.attributes.rel && elemento.attributes.rel.value != "#") ? segundo.attributes.rel.value.substring(segundo.attributes.rel.value.indexOf(' ', 1)) : segundo.name) + '.\n'
								if (!campofoco) campofoco = elemento;
								marcaDesmarcaCampo(elemento, true);
								passCompara = false;
							}
						}
						
						//Data
						else if (elemento.attributes.rel.value.indexOf("D") == 1) {
							var ano = new Date(cnvdtUS(elemento.value)).getYear();
							if(isNaN(ano) || ano > 2080 || ano < 1900) {
								msgCompara += '- ' + ((elemento.attributes && elemento.attributes.rel && elemento.attributes.rel.value != "#") ? elemento.attributes.rel.value.substring(elemento.attributes.rel.value.indexOf(' ', 1)) : elemento.name) + ' informada é inválida.\n';
								if (!campofoco) campofoco = elemento;
								marcaDesmarcaCampo(elemento, true);
								passCompara = false;
							}
						}
						
						//E-mail
						else if(elemento.attributes.rel.value.indexOf("@") == 1) {
							if (!elemento.value.match("^([a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9]))$")){
								msgCompara += '- ' + ((elemento.attributes && elemento.attributes.rel && elemento.attributes.rel.value != "#") ? elemento.attributes.rel.value.substring(elemento.attributes.rel.value.indexOf(' ', 1)) : elemento.name) + ' informado é inválido.\n';
								if (!campofoco) campofoco = elemento;
								marcaDesmarcaCampo(elemento, true);
								passCompara = false;
							}
						}
						
						//CPF
						else if(elemento.attributes.rel.value.indexOf("F") == 1 || elemento.attributes.rel.value.indexOf("J") == 1) {
							if (!validaCPFouCNPJ(elemento.value)) {
								msgCompara += '- ' + ((elemento.attributes && elemento.attributes.rel && elemento.attributes.rel.value != "#") ? elemento.attributes.rel.value.substring(elemento.attributes.rel.value.indexOf(' ', 1)) : elemento.name) + ' informado é inválido.\n';
								if (!campofoco) campofoco = elemento;
								marcaDesmarcaCampo(elemento, true);
								passCompara = false;
							}
						}
						
						//Range(Ex.: "0-120" ou "01/01/2010-31/12/2010")
						else if(elemento.attributes.rel.value.indexOf("R") == 1) {
							var rel = elemento.attributes.rel.value;
							var inicio = rel.substring(3, rel.indexOf("-"));
							var final = rel.substring(rel.indexOf("-") + 1, rel.indexOf(" "));
							var valor = elemento.value;
							
							//Números
							if (elemento.attributes.rel.value.indexOf("N") == 2) {
								inicio = cnvvl(inicio);
								final = cnvvl(final);
								valor = cnvvl(valor);
								
								if (valor < inicio || valor > final) {
									msgCompara += '- ' + ((elemento.attributes && elemento.attributes.rel && elemento.attributes.rel.value != "#") ? elemento.attributes.rel.value.substring(elemento.attributes.rel.value.indexOf(' ', 1)) : elemento.name) + ' está fora do intervalo válido.\n';
									if (!campofoco) campofoco = elemento;
									marcaDesmarcaCampo(elemento, true);
									passCompara = false;
								}
							}
							
							//Datas
							if (elemento.attributes.rel.value.indexOf("N") == 2) {
								inicio = new Date(cnvdtUS(inicio));
								final = new Date(cnvdtUS(final));
								valor = new Date(cnvdtUS(valor));
								
								if (isNaN(inicio) || isNaN(final) || isNaN(valor) || valor < inicio || valor > final) {
									msgCompara += '- ' + ((elemento.attributes && elemento.attributes.rel && elemento.attributes.rel.value != "#") ? elemento.attributes.rel.value.substring(elemento.attributes.rel.value.indexOf(' ', 1)) : elemento.name) + ' está fora do intervalo válido.\n';
									if (!campofoco) campofoco = elemento;
									marcaDesmarcaCampo(elemento, true);
									passCompara = false;
								}
							}
						}
					}
				}
			}
		}
	}
	
	if (!pass){
		alert(msg);
		if (campofoco.type.toLowerCase().indexOf("select") == 0 || campofoco.type.toLowerCase() == "checkbox" || campofoco.type.toLowerCase() == "radio") {
			campofoco.focus();
			
		} else  {
			campofoco.select();
		}
	} else {
		pass = passCompara;
		if (!passCompara) {
			alert(msgCompara);
			if (campofoco.type.toLowerCase().indexOf("select") == 0 || campofoco.type.toLowerCase() == "checkbox" || campofoco.type.toLowerCase() == "radio") {
				campofoco.focus();
			
			} else  {
				campofoco.select();
			}
		}
	}
	
	if (window.event) window.event.returnValue = pass;
	
	return pass;
}

function SomenteNumeros(campo, event) {
	return limitarEntrada(campo, event, /[0-9]/);
}

function SomenteNumerosDecimais(campo, event) {
	return limitarEntrada(campo, event, /[0-9,]/);
}

function SomenteLetras(campo, event) {
	return limitarEntrada(campo, event, /[a-zA-Z]/);
}

function formatar(campo, mascara, chave) {
	var tamanho = campo.value.length;
	var atual = mascara.substr(tamanho, 1);
	chave = (!chave) ? "#" : chave;
	
	if (atual != chave) campo.value += atual;
}

function maiusculas(campo) {
	campo.value = campo.value.toUpperCase();
}

function minusculas(campo) {
	campo.value = campo.value.toLowerCase();
}

function limitarEntrada(campo, event, expressao) {
	var e = (!window.event) ? event : window.event;
	var valido = true;
	
	if (e.type == "keypress" || e.type == "keyup" || e.type == "keydown") {
		var code = (!e.which) ? ((!e.charCode) ? e.keyCode : e.charCode) : e.which;
		
		if (code == 8 || code == 9 || code == 46) return true; //backspace, tab e delete
		
		valido = String.fromCharCode(code).match(expressao);
	}
	
	if (e.cancelBubble) e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	if (e.returnValue) e.returnValue = (valido != null) ? valido : false;
	return valido != null;
}

function formataValor(campo) {
	var valor = campo.value;
	
	if (valor != "") {
		campo.value = String(Number(valor.replace(/\./g, "").reverse().replace(/,/, ".").replace(/,/g, "").reverse()).toFixed(2)).replace(/\./g, ",");
	}
}


function marcaDesmarcaCampo(qual, marcar) {
	qual.style.borderColor = (marcar) ? "#CC0000" : "";
	qual.style.backgroundColor = (marcar) ? "#FFCCCC" : "";
}


function cnvdtUS(data) {
	data = String(data);
	var dia = data.substr(0, data.indexOf("/"));
	var mes = data.substr(data.indexOf("/") + 1, data.lastIndexOf("/") - data.indexOf("/") - 1);
	var ano = data.substr(data.lastIndexOf("/") + 1, data.length - data.lastIndexOf("/"));
	
	return (mes + "/" + dia + "/" + ano);
}

function cnvvl(valor) {
	return Number(String(valor).replace(/\./g, "").replace(/,/, "."));
}

function numStr(numero) {
	return String(numero).reverse().replace(/,/g, "").replace(/\./, ",").replace(/\./g, "").reverse();
}

function validaCPFouCNPJ(valor) {
	var numero = valor.replace(/\D/ig, "");

	//if (numero.length != 11 && numero.length != 14) { retirei esta linha para validar apenas o CNPJ conforme linha abaixo
	if (numero.length != 14) {
		return false;
	
	} else if (numero.length == 11) {
		return CalculaCPF(numero);
	
	} else {
		return CalculaCNPJ(numero);
	}
}


function CalculaCNPJ(varCNPJ) {
	var RecebeCNPJ, Numero = new Array(15), soma, resultado1, resultado2;
	RecebeCNPJ = varCNPJ;

	if (RecebeCNPJ.length != 14) {
		return false;
	} else if (RecebeCNPJ == "00000000000000" || RecebeCNPJ === "11111111111111" || RecebeCNPJ === "22222222222222" || RecebeCNPJ == "33333333333333" || RecebeCNPJ == "44444444444444" || RecebeCNPJ == "55555555555555" || RecebeCNPJ == "66666666666666" || RecebeCNPJ == "77777777777777" || RecebeCNPJ == "88888888888888" || RecebeCNPJ == "99999999999999") {
		return false;
	} else {
		Numero = (" "+RecebeCNPJ).split("");
	
		soma = (Numero[1] * 5) + (Numero[2] * 4) + (Numero[3] * 3) + (Numero[4] * 2) + (Numero[5] * 9) + (Numero[6] * 8) + (Numero[7] * 7) + (Numero[8] * 6) + (Numero[9] * 5) + (Numero[10] * 4) + (Numero[11] * 3) + (Numero[12] * 2);
		
		soma = soma % 11;
			
		if (soma < 2) {
			resultado1 = 0;
		} else {
			resultado1 = 11 - soma;
		}
	
		if (resultado1 == Numero[13]) {
			soma = (Numero[1] * 6) + (Numero[2] * 5) + (Numero[3] * 4) + (Numero[4] * 3) + (Numero[5] * 2) + (Numero[6] * 9) + (Numero[7] * 8) + (Numero[8] * 7) + (Numero[9] * 6) + (Numero[10] * 5) + (Numero[11] * 4) + (Numero[12] * 3) + (Numero[13] * 2);
		
			soma = soma % 11;
			
			if (soma < 2) {
				resultado2 = 0;
			} else {
				resultado2 = 11 - soma;
			}
		
			if (resultado2 == Numero[14]) {
				//CNPJ Válido;
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
}

function CalculaCPF(varCPF) {
	var RecebeCPF, Numero = new Array(12), soma, resultado1, resultado2;
	RecebeCPF = varCPF;
	
	if (RecebeCPF.length != 11) {
		return false;
	} else if (RecebeCPF == "00000000000" || RecebeCPF == "11111111111" || RecebeCPF == "22222222222" || RecebeCPF == "33333333333" || RecebeCPF == "44444444444" || RecebeCPF == "55555555555" || RecebeCPF == "66666666666" || RecebeCPF == "77777777777" || RecebeCPF == "88888888888" || RecebeCPF == "99999999999") {
		return false;
	} else {
		Numero = (" "+RecebeCPF).split("");

		soma = (10 * Numero[1]) + (9 * Numero[2]) + (8 * Numero[3]) + (7 * Numero[4]) + (6 * Numero[5]) + (5 * Numero[6]) + (4 * Numero[7]) + (3 * Numero[8]) + (2 * Numero[9]);
		
		soma = soma % 11;
			
		if (soma < 2) {
			resultado1 = 0;
		} else {
			resultado1 = 11 - soma;
		}

		if (resultado1 == Numero[10]) {
			soma = (Numero[1] * 11) + (Numero[2] * 10) + (Numero[3] * 9) + (Numero[4] * 8) + (Numero[5] * 7) + (Numero[6] * 6) + (Numero[7] * 5) + (Numero[8] * 4) + (Numero[9] * 3) + (Numero[10] * 2);
			
			soma = soma % 11;
			
			if (soma < 2) {
				resultado2 = 0;
			} else {
				resultado2 = 11 - soma;
			}
		
			if (resultado2 == Numero[11]) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
}


String.prototype.reverse = function() {
	var tmp = "";
	var texto = this.split("");
	
	for (var i = texto.length - 1; i >= 0; i--)  tmp += texto[i];
	
	return tmp;
}

String.prototype.trim = function() {
	return this.replace(/(^\s+)|(\s+$)/g, "");
}
