jQuery.fn.Kvalida = function(settings){
	settings = jQuery.extend({
		erroClass: "kv-campo-erro"
	}, settings);
	var retorno = new Object;
		retorno.validador = new Object; //Acumulador dor erros encontrados
		
	var campos = jQuery("input, select, textarea",this);
	
	/*campos.mouseover(function(){
		jQuery(".kv-detalhe-erro").hide();
	});*/
	
	jQuery.each(campos, function(ci, cval){
		jQuery(this).removeClass(settings.erroClass);
		if(jQuery(cval).attr("kvalida")){
			var validacoes = jQuery(cval).attr("kvalida").split(" ");
			jQuery.each(validacoes, function(ri, rval){
				var txtErro = jQuery.KvalidaRegras[rval](jQuery(cval));
				if( txtErro ){
					retorno.validador[ jQuery(cval).attr('name') ] = txtErro;
				}
			});
		}
	});
	
	jQuery(this).KpintaErro(settings, retorno);
	
	return this;
}

jQuery.KvalidaRegras = {
	//informação obrigatória
	obrigatorio: function(campo){
		if(jQuery(campo).attr("type") == 'checkbox'){
			if(jQuery(campo).attr("checked")){
				return false;
			}else{
				return '0030';
			}
		}
		if(jQuery(":selected",campo).length > 0){
			if(jQuery(campo).val() == -1 || jQuery(campo).val() == ""){
				return '0030';
			}else{
				return false;
			}
		}
		if(campo.val() != ''){
			return false //codigo da msg
		}
		return '0030';
	},
	//somente numeros
	numerico: function(campo){
		if (/^[0-9]*$/.test(jQuery(campo).val())){
			return false;
		}
		return '0032'; //codigo da msg
	},
	//cpf valido - necessario carregar cpf-cnpj.js
	cpf: function(campo){
		if( jQuery(campo).val() == '' || isCpf(jQuery(campo).val())){
			return false;
		}
		return '0033'; //codigo da msg
	},
	//cnpj valido - necessario carregar cpf-cnpj.js
	cnpj: function(campo){
		if( jQuery(campo).val() == '' || isCnpj(jQuery(campo).val())){
			return false;
		}
		return '0033'; //codigo da msg
	},
	email: function(campo){
		if ( jQuery(campo).val() == '' || /^[a-zA-Z0-9]{1}([\._a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+){1,3}$/.test(jQuery(campo).val())){
			return false;
		}
		return '0033';
	},
	//compara ois campos de senha necessita o atributo "compara" com o selector do campo a ser comparado ex: #senha
	comparaSenha: function(campo){
		var compara = jQuery(campo).attr('compara');
		if(jQuery(compara).val() == jQuery(campo).val()){
			return false;
		}
		return '0006';
	},
	//compara ois campos de senha necessita o atributo "compara" com o selector do campo a ser comparado ex: #senha
	comparaEmail: function(campo){
		var compara = jQuery(campo).attr('compara');
		if(jQuery(compara).val() == jQuery(campo).val()){
			return false;
		}
		return '0035';
	},
	segura: function(campo){
		var numero = 0;
		var letra = 0;
		var senha = jQuery(campo).val();
		for(var i=0; i<senha.length ; i++){
			if(senha.charAt(i) <= 9){
				numero++;
			}else{
				letra++;	
			}
		}
		if( numero >= 2 && letra >= 4 && senha.length >= 6 && senha.length <= 10){
			return false;
		}
		return '0003';
	},
	contaNome: function(campo){
		var texto = jQuery(campo).val();
		var termos = texto.split(" ");
		var n_termos = termos.length
		for( i = 0; i < termos.length; i++ ){
			if( termos[i].length == 0 ){
				n_termos--;
			}
		}
		if( n_termos > 1 ){
			return false;
		}
		return '0046';
	}
};

jQuery.fn.KpintaErro = function(settings, dados){
	var form = this;
	var camposErro = new Array;
	jQuery('.'+dados.erroClass,form).removeClass(dados.erroClass);
	jQuery('input, select, textarea',form).each(function(){
		//jQuery(this).css({width:jQuery(this).attr("originalWidth")});
	});
	jQuery.each(dados.validador, function(i, val){
		jQuery.textoIdioma(val, function(data){
			//var txt = jQuery("<div/>").html( data.text() ).addClass("textErro");
			jQuery("*[name='"+i+"']",form).KmarcadorErro(data.text()); //.after(txt);
		});
		jQuery("*[name='"+i+"']",form).addClass(settings.erroClass);
		camposErro.push(jQuery("*[name='"+i+"']",form).attr("campo")||i);
	});
	if( camposErro.length > 0 )
		jQuery.modalCod('0008',camposErro,{unico:true});
};

//mostra os detalhes do erro
jQuery.fn.detalhe = function(texto){
	this.focus(	function(){
					texto.show();//.mouseover(function(){ jQuery(this).hide(); });
					var posicao = jQuery(this).position();
					var detalhe = texto.css({
						top: posicao.top-(texto.outerHeight()+2),
						left: posicao.left-60
				   	});
		})
		.blur( function(){
					texto.hide();
		});
	return this;
};

jQuery.fn.KmarcadorErro = function(erro){
	var detalhe = jQuery("<div/>").html(erro).addClass("kv-detalhe-erro").hide().insertAfter(this);
	var seta = jQuery("<div/>").addClass("kv-seta-erro").appendTo(detalhe);
	
	var campo = jQuery(this).addClass("kv-marca-erro").detalhe(detalhe);
	//var img = jQuery("<div/>").addClass("kv-marca-erro").detalhe();
	//campo.width( campo.outerWidth()-24 );
	//campo.before(img);
}