$(document).ready(function() {
	validaFormPadrao();
	

	$(".ddd").mask("99");
	$(".telefone").mask("99 9999-9999");
	$(".dia, .mes").mask("99");
	$(".ano").mask("9999");
	$(".cep").mask("99999-999");
	//$("input.hint").hint();

});

jQuery.validator.messages.required = " ";
jQuery.validator.messages.email = " ";


jQuery.validator.addClassRules({
	"numeros" : {
		regex: "^[0-9]{1,}$"
	},
	"rg" : {
		minlength : 7,
		rg : true
	},
	"cpf" : {
		cpf : true
	},
	"login" : {
		regex: "^[a-z0-9]{1,}$"
	},
	"loginConf": {
    	equalTo: "input[name=login]"
    },
	"loginSenha": {
    	equalTo: "input[name=senha]"
    }

});

jQuery.validator.addMethod(
    "regex",
    function(value, element, regexp) {
        var check = false;
        var re = new RegExp(regexp);
        return this.optional(element) || re.test(value);
    }
);

jQuery.validator.addMethod(
        "rg",
        function(value, element) {
			return this.optional(element) || ValRG(value);
        }
);

jQuery.validator.addMethod(
        "cpf",
        function(value, element) {
			return this.optional(element) || validaCPF(value);
        }
);



function validaFormPadrao(){
	var validator = $(".formulario").validate({
		errorContainer: $(".msgErrorAssine"), 
		errorLabelContainer: $(".msgError"),
		errorClass: "erro",
		
		highlight: function(element, errorClass) {
		    $(element).addClass(errorClass);
		    $(element.form).find("label[for=" + element.name + "]").addClass('lblErro');
		    $(element.form).find("label[name=" + element.name + "]").addClass('lblErro');
			// $(".erroFeedback").css('display', 'block');
			 
		},
		
		unhighlight: function(element, errorClass) {
			$(element).removeClass(errorClass);
			$(element.form).find("label[for=" + element.name + "]").removeClass('lblErro');
			$(element.form).find("label[for=" + element.name + "]").show();
			//$("#erro-"+element.name+", #boxFormAmigo .alertErro").css('display', 'none');
		}
	});
}

function nu(campo){
	var digits="0123456789"
	var campo_temp 
	for (var i=0;i<campo.value.length;i++){
		campo_temp=campo.value.substring(i,i+1) 
		if (digits.indexOf(campo_temp)==-1){
			campo.value = campo.value.substring(0,i);
			break;
		}
	}
}

function ValRG(numero){
	/*
	##  Igor Carvalho de Escobar
	##    www.webtutoriais.com
	##   Java Script Developer
	*/
	var numero = numero.split("");
	tamanho = numero.length;
	vetor = new Array(tamanho);
	
	if(tamanho>=1){
		vetor[0] = parseInt(numero[0]) * 2; 
	}
	if(tamanho>=2){
		vetor[1] = parseInt(numero[1]) * 3; 
	}
	if(tamanho>=3){
		vetor[2] = parseInt(numero[2]) * 4; 
	}
	if(tamanho>=4){
		vetor[3] = parseInt(numero[3]) * 5; 
	}
	if(tamanho>=5){
		vetor[4] = parseInt(numero[4]) * 6; 
	}
	if(tamanho>=6){
		vetor[5] = parseInt(numero[5]) * 7; 
	}
	if(tamanho>=7){
		vetor[6] = parseInt(numero[6]) * 8; 
	}
	if(tamanho>=8){
		vetor[7] = parseInt(numero[7]) * 9; 
	}
	if(tamanho>=9){
		vetor[8] = parseInt(numero[8]) * 100; 
	}
	
	total = 0;
	
	if(tamanho>=1){
		total += vetor[0];
	}
	if(tamanho>=2){
		total += vetor[1]; 
	}
	if(tamanho>=3){
		total += vetor[2]; 
	}
	if(tamanho>=4){
		total += vetor[3]; 
	}
	if(tamanho>=5){
		total += vetor[4]; 
	}
	if(tamanho>=6){
		total += vetor[5]; 
	}
	if(tamanho>=7){
		total += vetor[6];
	}
	if(tamanho>=8){
		total += vetor[7]; 
	}
	if(tamanho>=9){
		total += vetor[8]; 
	}
	
	resto = total % 11;
	if(resto!=0 || tamanho < 5){
		return false;
	}
	else{
		return true;
	}
}

function validaCPF(valor) {
	cpf = valor;
	erro = new String;
	if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n";
	var nonNumbers = /\D/;
	if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";
	if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
	         erro += "Numero de CPF invalido!"
	}
	var a = [];
	var b = new Number;
	var c = 11;
	for (i=0; i<11; i++){
	       a[i] = cpf.charAt(i);
	      if (i < 9) b += (a[i] * --c);
	}
	if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
	b = 0;
	c = 11;
	for (y=0; y<10; y++) b += (a[y] * c--);
	if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
	       erro +="Digito verificador com problema!";
	}
	if (erro.length > 0){
	       //alert(erro);
	       return false;
	}
	return true;
}

/* HINT */
/**
* @author Remy Sharp
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/
(function ($) {

$.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }
    
  return this.each(function () {
    // get jQuery version of 'this'
    var $input = $(this),
   
    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = $(this.form),
      $win = $(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title
      
      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};

})(jQuery);
/* HINT */


