//
//
//
$(function() {

	$("ul.skin2").tabs("div.skin2 > div");

	$("#divCadastro").hide();
	$('.detalheArtigo').hide();
	$(".label_error").hide();
 $('#AlterarSenha').click(function() {
	$.blockUI({ message: $('#BoxAlterarSenha'), css: { width:'480px', height:'100px' } }); 
});
 $('#btnCancelarAlteracao').click(function(){
	$.unblockUI(); 
});


	//$("#profileTelefoneCelular").mask("(99) 9999-9999", { placeholder:"" });
	//$("#profileTelefoneFixo").mask("(99) 9999-9999", { placeholder:"" });
	//$("#profileDataNascimento").mask("99/99/9999", { placeholder:"" });	

	$("#btnQueroCadastrar").click(function() {
		$("#divCadastro").show();
	}); 

        $("#btnBuscarArtigo").click(function() {
            var hasError = false;
            var fieldValue = $("#q").val();
            if (fieldValue == "") {
                $("#q").show();
                $("#q").focus();
                alert('Atenção.\nFavor informar o que deseja procurar.');
                hasError = true;
            }
            if (hasError)  
                return false;
            else {
                var form = document.getElementById("frmBusca");
                form.method = "GET";
                form.action = "/busca?q="+$("#q").val();
                form.submit();
            }
        });


});

function formAlterarSenhaSubmit() {

	var hasError = false;

	var fieldValue = $("#profileAlterarSenha").val();	
	if (fieldValue == "") {
		$("#profileAlterarSenha_Error").html("Preenchimento obrigatório.");    
		$("#profileAlterarSenha_Error").show();  
		$("#profileAlterarSenha").focus();
		hasError = true;
	}

	var fieldValue = $("#profileNovaSenha").val();	
	if (fieldValue == "") {
		$("#profileNovaSenha_Error").html("Preenchimento obrigatório.");  
		$("#profileNovaSenha_Error").show();  
		$("#profileNovaSenha").focus();
		hasError = true;
	}
	if (fieldValue != "") {
		if (fieldValue.length < 5) {
			$("#profileNovaSenha_Error").html("Senha deve conter, no mínimo, 5 caracteres.");
			$("#profileNovaSenha_Error").show();  
			$("#profileNovaSenha").focus();
			hasError = true;
		}
	}

	var fieldValue = $("#profileNovaSenhaRepetida").val();	
	if (fieldValue == "") {
		$("#profileNovaSenhaRepetida_Error").html("Preenchimento obrigatório.");  
		$("#profileNovaSenhaRepetida_Error").show();  
		$("#profileNovaSenhaRepetida").focus();
		hasError = true;
	}
	if (fieldValue != "") {
		if (fieldValue.length < 5) {
			$("#profileNovaSenhaRepetida_Error").html("Senha deve conter, no mínimo, 5 caracteres.");
			$("#profileNovaSenhaRepetida_Error").show();  
			$("#profileNovaSenhaRepetida").focus();
			hasError = true;
		}
	}

	if ($("#profileNovaSenha").val() != $("#profileNovaSenhaRepetida").val()) {
		$("#profileNovaSenha_Error").html("Senhas devem ser iguais.");    
		$("#profileNovaSenha_Error").show();  
		$("#profileNovaSenha").focus();
		hasError = true;
	}

	if (!hasError) {
		$.ajax ({
			type: "POST",
			url: "/alterar-senha",
			contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		        data: $("#formAlterarSenha").serialize(),
			success: function(response) { 
				alert("Senha alterada com sucesso!");
				$.unblockUI();
			},
			error: function(xhr, ajaxOptions, thrownError) {
				$("#message_Error").html("Senha atual inválida! Tente novamente.");
				$("#message_Error").show();  
				$.unblockUI();
			},  
			async: false     
		});
	}

}

//
//
//
$('#artigoAsssunto').change(function(){

	alert(' !!! ');
	$('.detalheArtigo').hide();
	$('#detalheArtigo'+$("option:selected", this).val()).show()	
});

//
//
//
function isDate(data) {
        if (data != "") {
                erro =0;
                hoje = new Date();
                anoAtual = hoje.getFullYear();
                barras = data.split("/");
                if (barras.length == 3) {
                        dia = barras[0];
                        mes = barras[1];
                        ano = barras[2];
                        resultado = (!isNaN(dia) && (dia > 0) && (dia < 32)) && (!isNaN(mes) && (mes > 0) && (mes < 13)) && (!isNaN(ano) && (ano.length == 4) && (ano <= anoAtual && ano >= 1900));
                        if (!resultado) {
                                return false;
                        }
                 } 
                 else {
	                 return false;
                 }
        	return true;
        }
}

//
//
//
function removeChar(string, pattern) {
	index = string.indexOf(pattern);
	result = "";
	if (index == -1) 
		return string;
	result += string.substring(0, index) + removeChar(string.substring(index + pattern.length), pattern);
	return result;
}
	

