$(document).ready(function() {
						   
	//("form").find("input[@valida*=cep]");
	$('form').submit(function() {
		var erro = '';
		
		$(this).find("input[@valida*=cep]").each(function(){
			erro += cep($(this));
		});
		
		$(this).find("input[@valida*=obrig]").each(function(){
			erro += vazio($(this));
		});
		$(this).find("select[@valida*=select]").each(function(){
			erro += svazio($(this));
		});
		
		$(this).find("input[@valida*=data]").each(function(){
			erro += data($(this));
		});
		
		$(this).find("input[@valida*=senha]").each(function(){
			erro += senha($(this));
		});
		$(this).find("input[@valida*=cpf]").each(function(){
			erro += cpf($(this));
		});
		$(this).find("input[@valida*=email]").each(function(){
			erro += email($(this));
		});
		$(this).find("#TERMO").each(function(){
			erro += ($("#TERMO:checked").val() == 'on') ? '' : 'check';
		});
		
		if (erro != ''){
			return false;
		}
	});//form

//###########################################################################################################

	$("input[@valida*=cep]").keydown( function() { 
		var $this = $(this);
		$this.attr("maxlength","9");
		setTimeout(function() {
		cep($this);
 		}, 1);// FIM TIMEOUT
	});

	$("select[@valida*=select]").change( function() { 
		var $this = $(this);
		setTimeout(function() {
		svazio($this);
		}, 1);// FIM TIMEOUT
	});

	$("input[@valida*=obrig]").keydown( function() { 
		var $this = $(this);
		setTimeout(function() {
		vazio($this);
 		}, 1);// FIM TIMEOUT
	});

	$("input[@valida*=data]").keydown( function() { 
		var $this = $(this);
		$this.attr("maxlength","10");
		setTimeout(function() {
		data($this);
 		}, 1);// FIM TIMEOUT
	});
	
	$("input[@valida*=senha]").keydown( function() { 
		var $this = $(this);
		$this.attr("maxlength","12");
		setTimeout(function() {
		senha($this);
 		}, 1);// FIM TIMEOUT
	});

    //incompleto
	$("input[@valida*=login]").keydown( function() { 
		var $this = $(this);
		$this.attr("maxlength","30");
		setTimeout(function() {
		login($this);
 		}, 1);// FIM TIMEOUT
	});

	$("input[@valida*=cpf]").keydown( function() { 
		var $this = $(this);
		$this.attr("maxlength","14");
		setTimeout(function() {
		cpf($this);
 		}, 1);// FIM TIMEOUT
	});
	
	$("input[@valida*=email]").keydown( function() { 
		var $this = $(this);
		$this.attr("maxlength","150");
		setTimeout(function() {
		email($this);
 		}, 1);// FIM TIMEOUT
	});

//###########################################################################################################

	function ferro($this,$erro) 
	{	
		$this.css("border-color","red");
		var div = "<b>teste</b>" 
		var erro = $this.attr("erro");
		if ((erro == 0) || (erro))
		{
			$this.next().html($erro);
		}
		else 
		{
			$this.attr({ 
			  erro: ('1')
			});
			$this.after('<b>'+$erro+'</b>');
		}
	}
	
	function fok($this) 
	{
		$this.css("border-color","green");
		var erro = $this.attr("erro");
		if (erro)
		{
			$this.next().html('');
		}
		else 
		{
			$this.attr({ 
			  erro: ('0')
			});
			$this.after('<b></b>');
		}
	}
	
	function cep($this) 
	{
		var v = $this.val();
			v=v.replace(/\D/g,"")
    		v=v.replace(/^(\d{5})(\d)/,"$1-$2") 
			$this.val(v);
			if(v.length != 9){
				ferro($this,' - CEP invalido');
				return '/cep';
			} else {
				fok($this);
				return ''
			}
	}

	function email($this) 
	{
		var v = $this.val();
		$this.val(v);
		var erro = Vemail(v);
		if(erro != true){
			ferro($this,' - E-mail invalido');
			return '/email';
		} else {
			fok($this);
			return ''
		}
	}
	
	function vazio($this){
			var v = $this.val();
			$this.val(v);
			if(v.length == ''){
				ferro($this,' - Campo Obrigatorio');
				return '/vazio';
			} else {
				fok($this);
				return ''
			}
	}

	function svazio($this){
			var v = $this.val();
			$this.val(v);
			if(v.length == ''){
				$this.css("border-color","red");
				return '/vazio';
			} else {
				$this.css("border-color","red");
				return ''
			}
	}

	function data($this) {
			var v = $this.val();
 		    v=v.replace(/\D/g,"")                 
  			v=v.replace(/(\d{2})(\d)/,"$1/$2")    
    		v=v.replace(/(\d{2})(\d)/,"$1/$2")     
    		v=v.replace(/(\d{4})(\d{1,2})$/,"$1-$2")
			$this.val(v);
			var x=v.split("/"); 
			
	        var reTipo = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/;
			var x = reTipo.test(v);
			if(!x){
				ferro($this,' - Data Invalida');
				return '/DATA';
			} else {
				fok($this);
				return ''
			}
	}

	function senha($this) {
			var v = $this.val();
			var conf = ''
			var a = $("input[@valida*=senha]")[0].value;
			var b = $("input[@valida*=senha]")[1].value;
			if (a != b && v.length >= 6 && a != '' && b != '') {
				var erro = ' - As senhas digitadas nao conferem';
				conf = 'n';
				} else {
				var erro = ' - Minimo 6 e maximo 12 caracteres';
				}
			if(v.length < 6 || v.length > 12 || conf == 'n'){
				ferro($this,erro);
				return '/SENHA';
			} else {
				fok($this);
				return '';
			}
	}

	function login($this) 
	{
		var v = $this.val();
			v=v.replace(/[\W]/g,"")
			$this.val(v);
			if(v.length > 30 || v.length < 4){
				ferro($this,' - Minimo 4 e maximo 30 caracteres');
				return '/login';
			} else {
				fok($this);
				return ''
			}
	}
	
	function cpf($this)
	{
			var v = $this.val();
    		v=v.replace(/\D/g,"")                 
    		v=v.replace(/(\d{3})(\d)/,"$1.$2")    
    		v=v.replace(/(\d{3})(\d)/,"$1.$2")     
    		v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2")
			$this.val(v);
			var erro = Vcpf(v);
			if(erro != ''){
				ferro($this,' - CPF invalido');
				return '/CPF';
			} else {
				fok($this);
				return ''
			}
	}
//################################  CPF ###########################################################################

	function Vemail(pVal)
	{
		var reTipo = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
		return reTipo.test(pVal);
	}
	
	function Vcpf(v)
	{
		var cpf = v;
		var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
		if(!filtro.test(cpf)){
			return 'cpf/';
		}

		cpf = remove(cpf, ".");
		cpf = remove(cpf, "-");
		
		if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
			cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
			cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
			cpf == "88888888888" || cpf == "99999999999"){
			return 'cpf/';
		}
		
		soma = 0;
		for(i = 0; i < 9; i++)
		soma += parseInt(cpf.charAt(i)) * (10 - i);
		resto = 11 - (soma % 11);
		if(resto == 10 || resto == 11)
		resto = 0;
		if(resto != parseInt(cpf.charAt(9))){
			return 'cpf/';
		}
		soma = 0;
		for(i = 0; i < 10; i ++)
		soma += parseInt(cpf.charAt(i)) * (11 - i);
		resto = 11 - (soma % 11);
		if(resto == 10 || resto == 11)
			resto = 0;
		if(resto != parseInt(cpf.charAt(10))){
			return 'cpf/';
		}
		return '';
	}
		
	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;
	}
 
});
