// JavaScript Document

/* Validação da Senha de Cadastro */

$.validator.passwordRating.messages = {
	"similar-to-username": "User Name",
	"too-short": "Senha Curta",
	"very-weak": "Muita Fraca",
	"weak": "Fraca",
	"good": "Boa",
	"strong": "Forte"
}

$(document).ready(function() {
	// validate signup form on keyup and submit
	var validator = jQuery("#signupform").validate({
		rules: {
			username: {
				required: true,
				email: true,
				minlength: 5
			},
			password: {
				password: "#username",
			},
			password_confirm: {
				required: true,
				equalTo: "#password",
				minlength: 5
			},
			dd_nome: {
				required:true
			},
			dd_cpf: {
				cnpj_cpf: true
			}
		},
		messages: {
			username: {
				required: "Informe seu e-mail",
				email: "E-mail inválido",
				minlength: jQuery.format("O e-mail deverá ter no minimo {0} caracteres")
			},
			password_confirm: {
				required: "Repita sua senha",
				minlength: jQuery.format("A senha deverá ter no mínimo {0} caracteres"),
				equalTo: "Senhas não conferem"
			},
			dd_cpf: {
				cnpj: 'CPF/CNPJ inválido'
			}
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			error.prependTo( element.parent().next() );
		},
		// specifying a submitHandler prevents the default submit, good for the demo
		submitHandler: function() {
			alert("submitted!");
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
	});

	// Mascara no form de cadastro
   	jQuery("#dd_data_nasc").mask("99/99/9999");
	jQuery("#dd_rg").mask("?9999999999999999");
	jQuery("#dd_cep").mask("99.999-999");
	jQuery("#dd_cpf").mask("999.999.999-99");
	 
	
	// Ajax Busca CEP
	jQuery("#dd_cep").blur(function() {
		jQuery.ajax({
			url: "webservice/cep/cep.php?cep="+jQuery(this).val(),
  			context: document.body,
			dataTypeString: "jon",
  			success: function(data){
				eval("dados = "+data);
				jQuery("#dd_logradouro").val(dados.logradouro);
				jQuery("#dd_bairro").val(dados.bairro);
				jQuery("#dd_cidade").val(dados.cidade);
				jQuery("#dd_uf").val(dados.uf);
 			}
		});
	})
	
});

$(function() {
	var today = new Date();
	var minAno = today.getFullYear() - 90;
	var maxAno = today.getFullYear() - 18;
	
	$( "#dd_data_nasc" ).datepicker({
		changeMonth: true,
		changeYear: true,
		minDate: new Date(minAno, 1 - 1, 1),
		maxDate: new Date(maxAno, 11, 31),
		dateFormat: 'dd/mm/yy',
	});
});

function chanceLabel(tipo) {

	if(tipo == "j") {
		jQuery("#label_nome").html("Razão Social:<sup>*</sup>");
		jQuery("#label_rg").html("IE:");
		jQuery("#label_cpf").html("CNPJ:");
		jQuery("#field_data_nasc").hide();
		jQuery("#dd_cpf").val("");
		jQuery("#dd_cpf").mask("99.999.999/9999-99");
	}else {
		jQuery("#label_nome").html("Nome Completo:<sup>*</sup>");
		jQuery("#label_rg").html("RG:");
		jQuery("#label_cpf").html("CPF:");
		jQuery("#field_data_nasc").show();
		jQuery("#dd_cpf").val("");
		jQuery("#dd_cpf").mask("999.999.999-99");
	}
	
}

// Validar Formulario de Contato
$(document).ready(function() {
	
	var validator = jQuery("#formContato").validate({
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			error.prependTo( element.parent().next() );
		},
		// specifying a submitHandler prevents the default submit, good for the demo
		submitHandler: function() {
			alert("submitted!");
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}	
	});
});
