$(document).ready(function() {
       
	   jQuery.fn.valida = function() {

		var erro = '';
		
		$(this).find(".valida_cep").each(function(){
			erro += cep($(this));
		});
		
		$(this).find(".valida_vazio").each(function(){
			erro += vazio($(this));
		});
		
		$(this).find(".valida_select").each(function(){
			erro += svazio($(this));
		});
		
		$(this).find(".valida_data").each(function(){
			erro += data($(this));
		});
		
		$(this).find(".valida_radio").each(function(){
			erro += radio($(this));
		});
		
		$(this).find(".valida_senha").each(function(){
			erro += senha($(this));
		});
		
		$(this).find(".valida_cpf").each(function(){
			erro += cpf($(this));
		});
		$(this).find(".valida_email").each(function(){
			erro += email($(this));
		});
		$(this).find(".valida_real").each(function(){
			erro += real($(this));
		});

			return erro;
};


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

	
	$(".valida_cep").live("keydown", function() { 
		var $this = $(this);
		$this.attr("maxlength","9");
		setTimeout(function() {
		cep($this);
 		}, 1);// FIM TIMEOUT
	});
	$(".valida_real").live("keydown", function() { 
		var $this = $(this);
		setTimeout(function() {
		real($this);
 		}, 1);// FIM TIMEOUT
	});
	$(".valida_radio").live("click", function() { 
		var $this = $(this);
		setTimeout(function() {
		radio($this);
		}, 1);// FIM TIMEOUT
	});
	$(".valida_select").livequery('change', function() { 
		var $this = $(this);
		setTimeout(function() {
		svazio($this);
		}, 1);// FIM TIMEOUT
	});

	$(".valida_vazio").live("keydown", function() { 
		var $this = $(this);
		setTimeout(function() {
		vazio($this);
 		}, 1);// FIM TIMEOUT
	});

	$(".valida_data").live("keydown", function() { 
		var $this = $(this);
		$this.attr("maxlength","10");
		setTimeout(function() {
		data($this);
 		}, 1);// FIM TIMEOUT
	});
	
	$(".valida_data").live("blur", function() { 
		var $this = $(this);
		$this.attr("maxlength","10");
		setTimeout(function() {
		data($this);
 		}, 1);// FIM TIMEOUT
	});
	
	$(".valida_senha").live("keydown", function() { 
		var $this = $(this);
		$this.attr("maxlength","12");
		setTimeout(function() {
		senha($this);
 		}, 1);// FIM TIMEOUT
	});

    //incompleto
	$(".valida_login").live("keydown", function() { 
		var $this = $(this);
		$this.attr("maxlength","30");
		setTimeout(function() {
		login($this);
 		}, 1);// FIM TIMEOUT
	});

	$(".valida_cpf").live("keydown", function() { 
		var $this = $(this);
		$this.attr("maxlength","14");
		setTimeout(function() {
		cpf($this);
 		}, 1);// FIM TIMEOUT
	});
	
	$(".valida_email").live("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");
		if($this.attr('title')){
			$erro = $this.attr('title');
		}
		var erro = $this.attr("erro");
		if ((erro == 0) || (erro))
		{
			$this.prev().html($erro);
		}
		else 
		{
			$this.attr({ 
			  erro: ('1')
			});
			
			$this.after('<b class="erro">'+$erro+'<br /></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 inválido');
				return '/cep';
			} else {
				fok($this);
				return '';
			}
	}
	function real($this) 
	{
		var v = $this.val();
			v=v.replace(/[A-Za-z\.]/g,"");
    		//v=v.replace(/^(\d)(\d{2})/g,"$1,$2") 
			$this.val(v);
			if(v.length == 0){
				ferro($this,'Digite um valor');
				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 inválido');
			return '/email';
		} else {
			fok($this);
			return '';
		}
	}
	
	function vazio($this){
			var v = $this.val();
			$this.val(v);
			if(v.length == ''){
				ferro($this,' - Campo obrigatório');
				return '/vazio';
			} else {
				fok($this);
				return '';
			}
	}
	
	function radio($this){
			var v = $this.val();
			var name = $this.attr("name");
			var value = $("input[name*="+name+"]:checked").attr("name");

			$this.val(v);
			if(value == null){
				$this.parent().css("color","red");
				return '/vazio';
			} else {
				$this.parent().css("color","green");
				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","green");
				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 inválida');
				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 {
				$("input[valida*=senha]").each(function(){
					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 inválido');
				return '/CPF';
			} else {
				fok($this);
				return '';
			}
	}

	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);
	}
	
	//################################  CPF ###########################################################################

	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;
	}
 
});