$.metadata.setType("attr", "validate");

$.fn.resetForm = function() {
    return this.each(function() {
        if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType))
            this.reset();
    });
};

jQuery.validator.addMethod("password", function( value, element ) {
	var result = this.optional(element) || value.length >= 4;
	if (!result) {
		element.value = "";
		var validator = this;
		setTimeout(function() {
			validator.blockFocusCleanup = true;
			element.focus();
			validator.blockFocusCleanup = false;
		}, 1);
	}
	return result;
}, "Sua senha deve conter pelo menos 6 caracteres, sendo obrigatório pelo menos 1 letra e 1 número.");
	
jQuery.validator.addMethod("cpf", function( value, element ) {
	var result = isCpf(element.value)
	if(!result) {
		element.value = "";
		var validator = this;
		setTimeout(function() {
			validator.blockFocusCleanup = true;
			element.focus();
			validator.blockFocusCleanup = false;
		}, 1);
	}
	return result;
}, "CPF Incorreto. Digite Novamente");	

jQuery.validator.addMethod("data", function( value, element ) {
	if(element.value.length > 5 && element.value != '__/__/____') {
		var result = validaData(element)
		if(!result) {
			element.value = "";
			var validator = this;
			setTimeout(function() {
				validator.blockFocusCleanup = true;
				element.focus();
				validator.blockFocusCleanup = false;
			}, 1);
		}
	}
	else
		return true;
	return result;
}, "Data Inv&aacute;lida. Digite Novamente.");	
	
jQuery.validator.addMethod("defaultInvalid", function(value, element) {
	return value != element.defaultValue;
}, "");
	
jQuery.validator.messages.required = "";
	
function validaFormulario(form,destino,fnc) {
	//$('#'+destino).hide();
	$("input.telefone").mask("(99) 9999-9999");
	$("input.cep").mask("99.999-999");
	$("input.cpf").mask("999.999.999-99");
	$("input.cnpj").mask("99.999.999/9999-99");
	
	$("input.moeda").keyup(function() {
		var strSemMascara;var numContador;var numero;
	
		strSemMascara = this.value;	TamDig = strSemMascara.length;numContador = 0;numero = "";
		for (i = TamDig; (i >= 0); i--){
			if ((parseInt(strSemMascara.substr(i,1))>=0) && (parseInt(strSemMascara.substr(i, 1))<=9)){
				numContador++;
				if ((numContador == 3) && ((TamDig -i) < 5)){numero = ","+numero;numContador = 0;}
				else if (numContador == 3){ numero = "."+numero;numContador = 0;}
				numero = strSemMascara.substr(i, 1)+numero;
			}
		}
			
		TamDig = numero.length;
		for (i=TamDig-1;(i>=0);i--){
			if (((numero.substr(0,1) == "0")||(numero.substr(0,1) == "."))&&(numero.length!=4)){ numero= numero.substr(1,numero.length-1);}
			else{break}
		}			
		this.value = numero
	});



	$('#'+form).bind("invalid-form.validate", function(e, validator) {
		var errors = validator.numberOfInvalids();
		if (errors) {
			var message = errors == 1
				? 'Foi encontrado 1 campo n&atilde;o preenchido. <br>Verifique a marca&ccedil;&atilde;o em Vermelho'
				: 'Foram encontrados ' + errors + ' campos n&atilde;o  preenchidos. <br>Verifique a marca&ccedil;&atilde;o em Vermelho';
			alert('Foram Encontrados Campos Obrigatórios não preenchidos. Verifique antes de continuar.');
			$('#'+form+" .error span").html(message);
			$('#'+form+" .error").show();
			return false;
		} else {
			$('#'+form+" .error").hide();
		}
	}).validate({
		submitHandler: function() {
			
					
			//$('#'+destino).show();
			submitForm(form,destino); //------------------------Envia via conv2ajax----------------------
			$('#'+destino).html('Aguarde, enviando dados...');
			$('#'+form).resetForm(); 
			if(fnc) setTimeout(fnc,1000);
		},
		debug:false
	});

}
