;(function($){
	$.extend({
		jqValidacao: function(options) {
			return {
				init: function(options) {
					settings = jQuery.extend({
						lang: options.lang
					}, options);
					$.jqValidacao.loadMask();
					return this;
				},

				// Máscara nos campos
				// -----------------------------------------------------------------
				loadMask: function(p) {
					//$('input:text').setMask();
					//$('.updateArq').live('click',function(){
					$('.isdate').each(function(i){
						//$(".isdate").keypress(function (e) {
						
						//$("#"+ $(this).attr('id')).live('keypress',function(){
						$("#"+ $(this).attr('id')).keypress(function(e) {
							if( $.jqValidacao.keyNumeric(e) ) return false;
							var mask	= '##/##/####';
							var campo =  $(this).val().length;
							var saida = mask.substring(0,1);
							var texto = mask.substring(campo);

							var lenCampo	= new Number(campo);
							var lenMask		= new Number(mask.length);

							if((lenCampo+1) > lenMask) {
								return $.jqValidacao.key_check(e);		
							} else {
								var code = (e.keyCode ? e.keyCode : e.which);

								if( !$.jqValidacao.key_check(e) ){// permite apagar o que foi digitado 
									if(texto.substring(0,1) != saida) {
										$(this).val( $(this).val() + texto.substring(0,1) ); 
									}
								}
							}
						});						

						$("#"+ $(this).attr('id')).blur(function() {
							var valida = $.jqValidacao.isDate({ date:$(this).val() });
							if(!valida) {
								$(this).val("");
								$(this).removeClass("bgbd");
								$(this).addClass("campo_focus");
								//## alert('Informe uma data válida');
								return false;
							}
						});
					});

					$('.iscep').each(function(i)		{ $("#"+ $(this).attr('id')).mask("99999-999"); });
					$('.isphone').each(function(i)  { $("#"+ $(this).attr('id')).mask("(99) 9999-9999"); });
					$('.iscpf').each(function(i)		{ $("#"+ $(this).attr('id')).mask("999.999.999-99"); });
					$('.iscnpj').each(function(i)		{ $("#"+ $(this).attr('id')).mask("99.999.999/9999-99"); });
					$('.ispercent').each(function(i){ $("#"+ $(this).attr('id')).mask("999.99"); });					
					$('.ismaskdate').each(function(i)	{ $("#"+ $(this).attr('id')).mask("99/99/9999"); });
					$('.ismaskhora').each(function(i)	{ $("#"+ $(this).attr('id')).mask("99:99"); });
					$('.ismesano').each(function(i)	{ $("#"+ $(this).attr('id')).mask("99/99"); });
				},// end : loadMask

				// Máscara nos campos
				// -----------------------------------------------------------------
				loadMaskPerson: function(p) {
					//$('input:text').setMask();
					//$('.updateArq').live('click',function(){
					$box = p.boxdiv;
					$($box +' .isdate').each(function(i){
						//$(".isdate").keypress(function (e) {						
						//$("#"+ $(this).attr('id')).live('keypress',function(){
						$("#"+ $(this).attr('id')).keypress(function(e) {
							if( $.jqValidacao.keyNumeric(e) ) return false;
							var mask	= '##/##/####';
							var campo =  $(this).val().length;
							var saida = mask.substring(0,1);
							var texto = mask.substring(campo);

							var lenCampo	= new Number(campo);
							var lenMask		= new Number(mask.length);

							if((lenCampo+1) > lenMask) {
								return $.jqValidacao.key_check(e);		
							} else {
								var code = (e.keyCode ? e.keyCode : e.which);

								if( !$.jqValidacao.key_check(e) ){// permite apagar o que foi digitado 
									if(texto.substring(0,1) != saida) {
										$(this).val( $(this).val() + texto.substring(0,1) ); 
									}
								}
							}
						});

						$("#"+ $(this).attr('id')).blur(function() {
							var valida = $.jqValidacao.isDate({ date:$(this).val() });
							if(!valida) {
								$(this).val("");
								$(this).removeClass("bgbd");
								$(this).addClass("campo_focus");
								//## alert('Informe uma data válida');
								return false;
							}
						});
					});

					$($box +' .iscep').each(function(i)				{ $("#"+ $(this).attr('id')).mask("99999-999"); });
					$($box +' .isphone').each(function(i)			{ $("#"+ $(this).attr('id')).mask("(99) 9999-9999");});
					$($box +' .iscpf').each(function(i)				{ $("#"+ $(this).attr('id')).mask("999.999.999-99"); });
					$($box +' .iscnpj').each(function(i)			{ $("#"+ $(this).attr('id')).mask("99.999.999/9999-99"); });
					$($box +' .ispercent').each(function(i)		{ $("#"+ $(this).attr('id')).mask("999.99"); });					
					$($box +' .ismaskdate').each(function(i)	{ $("#"+ $(this).attr('id')).mask("99/99/9999"); });
					$($box +' .ismaskhora').each(function(i)	{ $("#"+ $(this).attr('id')).mask("99:99"); });
					$($box +' .ismesano').each(function(i)		{ $("#"+ $(this).attr('id')).mask("99/99"); });
				},// end : loadMaskPerson

				isDate: function(p) {
					value = p.date;
					if(value.length >= 1){ // verificando data  
						var data        = value;  
						var dia         = data.substr(0,2);  
						var barra1      = data.substr(2,1);  
						var mes         = data.substr(3,2);  
						var barra2      = data.substr(5,1);  
						var ano         = data.substr(6,4);  
						if(data.length!=10||barra1!="/"||barra2!="/"||isNaN(dia)||isNaN(mes)||isNaN(ano)||dia>31||mes>12)return false;  
						if((mes==4||mes==6||mes==9||mes==11) && dia==31)return false;  
						if(mes==2 && (dia>29||(dia==29 && ano%4!=0)))return false;  
						if(ano < 1900)return false;
					}
					return true;  
				},// end : isDate

				// Validar Formulário de Cadastro
				// -----------------------------------------------------------------
				validarCadastro: function(p) {
					var error = 0;
					$('.required').each(function(i){
						// Verifica se o campo é requerido
						// ------------------------------------------------------
						var item = $(this).val();
						var idCampo = $(this).attr('id');
						if( item.length == 0) {
							error = 1;
						} else {
							$(this).removeClass("campo_focus");
							$(this).addClass("bgbd");
						}

						// Verifica se é uma Data válida
						// ------------------------------------------------------
						if($(this).hasClass("isdate")){
							var valida = $.jqValidacao.isDate({ date:$(this).val() });
							if(!valida) error = 2;				
						}// end : isdate

						// Verifica se o e-mail é válido
						// ------------------------------------------------------
						if($(this).hasClass("isemail")){
							if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(item)) error = 2;
						}// end : ismail
						
						// Destaca o campo com erro
						// ------------------------------------------------------
						if( error != 0){
							$(this).focus();					
							$(this).removeClass("bgbd");
							$(this).addClass("campo_focus");
							//## jAlert('Preencha o campo em destaque corretamente.', 'Alerta de Erro');
							//alert( $(this).attr("rel") );
							$('#'+ idCampo).blur(function(){
								if( $(this).val().length > 0 ){ $(this).removeClass("campo_focus"); $(this).addClass("bgbd"); }
							});
							return false;
						}
					});// end : .required each

					if( error != 0)  return false;					
					return true;
				},// end : validarCadastro


				// teclas especiais (tab, backspace, delete, enter)
				key_check: function(e) {
					var code = (e.keyCode ? e.keyCode : e.which);
					var functional = false;
					if (code ==  8) functional = true;
					if (code ==  9) functional = true;
					if (code == 13) functional = true;
					if (code == 37) functional = true;
					if (code == 39) functional = true;
					if (code == 46) functional = true; // delete
					return functional;				
				},// end : key_check

				keyNumeric: function(e) {
					var code = (e.keyCode ? e.keyCode : e.which);
					var functional = false;
					if(code >= 96 && code <= 105) functional = true;
					return functional;
				},// end : keyNumeric

				/*
				function key_check_OLD (e) {
					var code = (e.keyCode ? e.keyCode : e.which);
					var functional = false;
					if (code ==  8) functional = true;
					if (code ==  9) functional = true;
					if (code == 13) functional = true;
					if (code == 37) functional = true;
					if (code == 39) functional = true;
					if (code == 46) functional = true; // delete
					return functional;
				},

				// filtra somente os números
				function keyNumeric_OLD (e) {
					var code = (e.keyCode ? e.keyCode : e.which);
					var functional = false;
					if(code >= 96 && code <= 105) functional = true;
					return functional;
				}
				*/

				um: function() { 
					//## alert('um ' + settings.name ) 
				}

			};
		}()
	});
})(jQuery);