


function validaFormulario(form) {
	
	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 4 caracteres.");
	
	jQuery.validator.addMethod("cpf", function( value, element ) {
            if( (element.value!="") && (element.value!="___.___.___-__") ){
		var result = isCpf(element.value)
		return result;
            }
            else{
                return true;
            }
	}, "CPF Incorreto. Digite Novamente.");	

	jQuery.validator.addMethod("data", function( value, element ) {
		var result = validaData(element)
		return result;
	}, "Data Inv&aacute;lida. Digite Novamente.");	
	
	// a custom method making the default value for companyurl ("http://") invalid, without displaying the "invalid url" message
	jQuery.validator.addMethod("defaultInvalid", function(value, element) {
		return value != element.defaultValue;
	}, "");
	
	jQuery.validator.messages.required = "";
	
	
		$('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. Verifique a marca&ccedil;&atilde;o em Vermelho'
					: 'Foram encontrados ' + errors + ' campos n&atilde;o  preenchidos. Verifique a marca&ccedil;&atilde;o em Vermelho';
				alert('Foram Encontrados Campos Obrigatórios não preenchidos. \nVerifique antes de continuar.')
				$("form div.error span").html(message);
				$("form div.error").show();
			} else {
				$("form div.error").hide();
			}
		}).validate({
			messages: {
				senha2: {
					required: " ",
					equalTo: "Digite a senha conforme o campo SENHA"	
				},
				email: {
					required: " ",
					email: "Digite um e-mail v&aacute;lido: Por exemplo: nome@seudominio.com",
					remote: jQuery.format("{0} este e-mail j&aacute; existe em nosso banco de dados, digite um diferente.")	
				},
				cpf: {
					required: " ",
					cpf: "Digite seu CPF",
					remote: jQuery.format("Você já está cadastrado. <a href='esqueceu_senha.php'>Clique aqui para solicitar a senha.</a>")	
				}
			},
			debug:false
		});

	
  $(".resize").vjustify();
  $("div.buttonSubmit").hoverClass("buttonSubmitHover");

  if ($.browser.safari) {
    $("body").addClass("safari");
  }
  
  $("input.telefone").mask("(99) 9999-9999");
  $("input.cep").mask("99999-999");
  $("input.cpf").mask("999.999.999-99");
  $("input.data").mask("99/99/9999");
  $("input.data_hora").mask("99/99/9999 99:99: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;
			//remove os zeros a esquerda
			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
		}
	);


}; 
 

$.fn.vjustify = function() {
    var maxHeight=0;
    $(".resize").css("height","auto");
    this.each(function(){
        if (this.offsetHeight > maxHeight) {
          maxHeight = this.offsetHeight;
        }
    });
    this.each(function(){
        $(this).height(maxHeight);
        if (this.offsetHeight > maxHeight) {
            $(this).height((maxHeight-(this.offsetHeight-maxHeight)));
        }
    });
};

$.fn.hoverClass = function(classname) {
	return this.hover(function() {
		$(this).addClass(classname);
	}, function() {
		$(this).removeClass(classname);
	});

};
