// JavaScript Document

(function($){
 $.fn.validar = function(opt) {
	 
	var defaults = {
		message: function(msg) {
			alert("Ocorrerm os seguintes erros:"+msg);
		}
	};
	var opt = $.extend(defaults, opt);	 
	 
    return this.each(function() {
		obj = $(this);  
		obj.find('input[type=text],input[type=password]').each(function() {
			nome=($(this).attr("name"));
			//obj.find('label[for='+nome+']').hide();
			$(this).attr("label",obj.find('label[for='+nome+']').html());
			$(this).attr("erro","");
			$(this).focus(function() {
			 	$(this).attr("focus",1);
				if ($(this).val()==$(this).attr("label")) {
					$(this).val("");
					$(this).removeClass("empty");
					$(this).removeClass("mandatory");
					$(this).removeClass("invalid");
					$(this).removeClass("incomplete");
				}
				if ($(this).hasClass("password")) {
					document.getElementById($(this).attr("id")).type="password";
				}				
			});
			$(this).blur(function() {
				$(this).attr("erro","");
				if ($(this).val()=="") {
					$(this).val($(this).attr("label"));
					$(this).addClass("empty");				
				}
				if ($(this).hasClass("empty") && $(this).hasClass("password") ) {
					document.getElementById($(this).attr("id")).type="text";
				} else if ($(this).hasClass("password")) {
					passes=$(this).closest("form").find('input.password');
					if ($(passes[0]).val() != $(passes[1]).val()) {
						$(passes[0]).addClass("invalid");
						$(passes[1]).addClass("invalid");	
						$(this).attr("erro","não coincide com a repetição.");
					}  else {
						$(passes[0]).removeClass("invalid");
						$(passes[1]).removeClass("invalid");							
					}
					
				}
				if ($(this).hasClass("empty") && $(this).hasClass("required")) {
					$(this).addClass("incomplete");
					$(this).attr("erro","é obrigatório.");
				}
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				if ($(this).hasClass("email") && $(this).attr("focus")=="1" && !emailReg.test( $(this).val() )) {
					$(this).addClass("invalid");
					$(this).attr("erro","não é um email válido.");
				} else {
					$(this).removeClass("invalid");
				}

				if ($(this).attr("focus")=="1" && parseInt($(this).attr("minlength"))>0 && $(this).attr("value").length < parseInt($(this).attr("minlength")) ) {
					$(this).addClass("invalid");
					$(this).attr("erro","deve ter " +parseInt($(this).attr("minlength"))+ " caracteres no mínimo.");
				}
				if ($(this).attr("focus")=="1" && $(this).hasClass("incomplete") ) {
					$(this).addClass("mandatory");
				}		
			});
			$(this).blur();			
		});

		obj.submit(function(){
			obj_form=$(this);
			if(obj_form.find('.incomplete,.invalid').length>0) {
				aviso="";
				obj_form.find('.incomplete,.invalid').each(function() {
					$(this).focus(); 
					$(this).blur();	
					if ($(this).attr("erro"))
					aviso+="<br/>"+$(this).attr("label")+" "+$(this).attr("erro");	
				});
				opt.message(aviso);
				return false;
			}
		});


    });
 };
})(jQuery);
