$.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("preenchido", function( value, element ) {
	return value != element.defaultValue;
}, "");
	
	
jQuery.validator.addMethod("defaultInvalid", function(value, element) {
	return value != element.defaultValue;
}, "");
	
jQuery.validator.messages.required = "";
	
function validaFormulario(form,destino,fnc) {
	
	$('#'+form+' input.preenchido').focus(function() {
		if (this.value == this.defaultValue){ 
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('#'+form+' input.preenchido').blur(function() {
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});	
	//$('#'+destino).hide();
	$('#'+form+" input.telefone").mask("(99) 9999-9999");
	$('#'+form+" input.cep").mask("99.999-999");
	$('#'+form+" input.cpf").mask("999.999.999-99");
	$('#'+form+" input.cnpj").mask("99.999.999/9999-99");
	$('#'+form+" input.data").mask("99/99/9999");
	//$("input.data").mask("99/99/9999");
	

	$('#'+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(obj) {
			$('#'+form+" input.submit").attr('disabled', true);
			//$('#'+form+" input.submit").attr('value', 'Aguarde, enviando dados...');
			if(destino != '') {		
				//$('#'+destino).show();
				submitForm(form,destino); //------------------------Envia via conv2ajax----------------------
				$('#'+destino).html('Aguarde, enviando dados...');
				$('#'+form).resetForm(); 
			}
			else {
				obj.submit();	
			}
		},
		debug:false
	});

}

