//============================================== // Start doc ready $( document ).ready(function() { $('.timer').hide(); get_session_data(); $('#max_choices_show').html(max_choices); update_selected_counters() var accordion_genres = $( "#genres" ).accordion({ heightStyle: "content", autoHeight: true, collapsible: true, header: "h3", active: false }); $('.tabs-format').tabs({}); //$('.tooltip').tooltip(); $("#frmLogin").submit(function(event) { event.preventDefault; var $form = $( this ), barcode = $form.find( "input[name='barcode']" ).val(), pin = $form.find( "input[name='pin']" ).val(); $.post('lib/validate_login.php',{ barcode: barcode, pin: pin },function( data ) { switch (data.login_result){ case 'OK': case 'User DELINQUENT': case 'User BLOCKED': author_codes = data.author_codes; author_code_id = data.author_code_id; $('.logged_in').show(); $('.not_logged_in').hide(); //uncheck all checkboxes... $('input[type=checkbox]').prop("checked", false); // ...then check the ones the user has selected if (author_code_id){ author_code_id.forEach(function(code) { $("input[type='checkbox'][data-author_code_id='" + author_code_id + "']").prop('checked', true); }); }; get_session_data(); break; case 'Privilege has expired': alert("Your library privileges have expired - please contact the library if you wish to continue") break; case 'Invalid PIN for station user': alert("You have entered an incorrect PIN number"); break; case 'User not found': alert("We're sorry - we could not find the library card number you entered."); break; default: alert("An unknown error has occured. Please contact the library to continue."); } }, 'json'); return false; }); $('#btnLogOut').click(function(e){ e.preventDefault; $.post('lib/logout.php',function( data ) { location.reload(); }); }); // toggle the checkboxes on the choices list table $('#dialog').on( 'click', '#listSelectAll', function(e) { $(':checkbox[class=author_checkbox]').prop('checked', this.checked); }); $('#btnShowList').click(function(e){ if (num_selected > 0){ $.post('lib/get_choices.php',{barcode: barcode},function( data ) { $("#dialog").append(data); choices_table = $('#tblChoices').DataTable({ "responsive": false, "autoWidth": false, "paging": false, "iDisplayLength": 15, "searching": false, "columnDefs": [ { "width": "20", "targets": 0 } ] }); $('#tblChoices_info').hide(); $("#dialog").dialog({ //title: fname + " " + lname + "'s " + num_selected +" Favorite Authors Club Selections", title: fname + " " + lname + "'s Favorite Authors Club Selections", resizable: false, autoOpen: true, height:'auto', width:500, position: { my: "top", at: "center", of: window }, modal: true, buttons: { "Remove selected": function() { remove_selected_from_datatable(); get_session_data() update_selected_counters(); }, "Print this list": function() { print_list(); }, OK: function() { $("#dialog").empty(); $(this).dialog('close'); get_session_data() update_selected_counters(); } } }); }); $("#dialog").empty(); }else{ alert("You have not made any selections!"); } }); /* $("a").click(function(e){ //e.preventDefault; var checkBox = $(this).prev(); checkBox.prop("checked", !checkBox.prop("checked")); checkBox.trigger('click'); return false; }); */ //========================================================================= // Checkbox click function //========================================================================= $('input[type=checkbox]').click(function() { var calling_checkbox = $(this); var author_id = calling_checkbox.data('author_id'); var code_id = calling_checkbox.data('code_id'); var author_code_id = calling_checkbox.data('author_code_id'); //var author_group = calling_checkbox .data('author_group'); // The uncheck_list is a list of author_code_id's to uncheck when calling box is checked (found in author_code_table) // this was created because of James Patterson's many series. We hates James Patterson. uncheck_list_piped = calling_checkbox.data('uncheck_list') +''; var uncheck_list = uncheck_list_piped.split("|"); var action = calling_checkbox.prop("checked"); //first, if this is a check, not an uncheck, uncheck all boxes of this author if (action == true){ $("input[type='checkbox'][data-author_id='" + author_id + "']").attr('checked',false); } // then recheck (or not) the calling checkbox calling_checkbox.prop('checked', action); var author_group_count = get_author_group_count(document) // uncheck any on the uncheck list that are checked (if it's got any to uncheck) uncheck_list.forEach( function(s){ var is_checked = $('[data-author_code_id="' + s + '"]').prop('checked'); if (is_checked == true){ $('[data-author_code_id="' + s + '"]').prop('checked', false).change(); $.post('lib/update_choice.php',{patron_id: patron_id, author_code_id: s, author_id:author_id, code_id:code_id, action: 'false'},function( data ) { }); } }); // need to check for over-limit first, so that only one post has to be made - this is crap $.post('lib/update_choice.php',{patron_id: patron_id, author_id:author_id, author_code_id: author_code_id, code_id:code_id, action: action},function( data ) { // re-check (or not) the calling check box num_selected = $("input:checkbox:checked").length - author_group_count ; var max_choices = 15; console.log(num_selected+':' + max_choices); var total_count = update_selected_counters(); if ( total_count > (max_choices) ){// too many choices - remove author from choices and uncheck calling box $.post('lib/update_choice.php',{patron_id: patron_id, author_code_id: author_code_id, author_id:author_id, code_id:code_id, action: 'false'},function( data ) { calling_checkbox.prop('checked', false); update_selected_counters(); alert("You may choose a maximum of " + max_choices + " authors"); }); }else{ } update_selected_counters(); }); }); //========================================================================= // END Checkbox click function //========================================================================= $('#suggest_a_title').click(function(event){ event.preventDefault; alert("We are taking you to our 'contact us' form. Along with your name, leave the name of the author and the words, 'favorite authors club' in the message") window.location.replace('//willardlibrary.org/contact-us'); return false; }) $('#btnClearAll').click(function(event){ if (confirm('Are you sure you want to clear all of your selections?')) { $.post('lib/clear_all.php',{patron_id: patron_id},function( data ) { $('input[type=checkbox]').prop("checked", false); get_session_data(); update_selected_counters() }); } $("#dialog").empty(); accordion_genres.accordion({ active: false }); }); $('#btnRemoveMe').click(function() { if (confirm("Are you sure you want end your Favorite Authors Club Membership?\n(You can rejoin at any time)")) { $.post('lib/end_membership.php',{patron_id:patron_id},function( data ) { },'json'); $('#btnLogOut').click(); } }); $('#btnToggleMembership').click(function() { var result switch (patron_status){ case 'Y': result = confirm("You are about to set your Favorite Author's Club status to 'Vacation mode'. \n\nWe will remember your selections but won't continue to put anything on hold until you reactivate your membership. \nYou can reactivate your membership at any time. \n\nClick 'OK' to confirm"); patron_status = 'N'; $('#membership_status').html('Your membership is currently in Vacation Mode.'); break; case 'N': //result = confirm("You are about to set your Favorite Author's Club status to 'active'. \n\nClick 'OK' to confirm") patron_status = 'Y'; result = true; // no real need for a confirm box here. $('#membership_status').html('Your membership is currently in active mode.'); break; default: alert("patron status is invalid") result = false; } $('#membership_status').effect("highlight", {color: "yellow"}, 8000); if (result == true){ $.post('lib/suspend_membership.php',{membership_status: patron_status },function( data ) { get_session_data(); }); } }); function get_author_group_count(el){ var arr_author_group = $(el).find('input:checked').filter(function() { return $(this).attr("data-author_group") > ""; }); var author_group_count = arr_author_group.length -1; if (author_group_count < 0){ author_group_count = 0; } return author_group_count; } function print_list(){ $("#dialog").printArea(); } function remove_selected_from_datatable(){ var searchIDs = $("#tblChoices input:checkbox:checked").map(function(){ return $(this); }).get(); var arrAuthorCodeIDs = new Array(); $.each(searchIDs, function( index, obj) { arrAuthorCodeIDs.push(obj.data('author_code_id')); }); $("#tblChoices input:checkbox:checked").each(function(i, obj) { choices_table.row( $(this).parents('tr') ).remove(); }) $.post('lib/remove_choices_from_table.php',{patron_id: patron_id, author_codeids: arrAuthorCodeIDs},function( data ) { get_session_data(); choices_table.draw(); var checked_count = $("#tblChoices input:checkbox").length - 1 //$("#dialog").dialog('option', 'title', fname + " " + lname + "'s " + checked_count +" Favorite Authors Club Selections"); $("#dialog").dialog('option', 'title', fname + " " + lname + "'s Favorite Authors Club Selections"); }); } function update_selected_counters(){ // loop throw each format $(".genre-format").each(function(i, obj) { var author_group_count = get_author_group_count($(this)); var numChecks = $(this).find(":checkbox:checked").length; numChecks = numChecks - author_group_count; var format = $(this).data('format'); var genre = $(this).data('genre') // update format labels within each genre var format_header = $(document).find("[data-format='" + format + "']").find("[data-genre='" + genre + "']"); if (numChecks > 0){ format_header.html("  " + numChecks + " selected  "); }else{ format_header.html(""); } }) var total_count = 0 // loop through each genre $(".genre_list").each(function(i, obj) { var author_group_count = get_author_group_count(obj); var genre = $(this).data('genre'); var numChecks = $(this).find(":checkbox:checked").length - author_group_count; // update genre labels var genre_header = $(document).find("span[data-genre_total='" + genre + "']"); if (numChecks > 0){ genre_header.html("  " + numChecks + " selected  "); }else{ genre_header.html(""); } total_count = total_count + numChecks; }) // get page total selected $('#num_selected').html(total_count); return total_count; } function update_timer(e){ } function get_session_data(){ $.post('lib/get_session_data.php',function( data ) { logged_in = data.logged_in; lname = data.lname; fname = data.fname; barcode = data.barcode; pin = data.pin; num_selected = data.code_count; patron_status = data.membership_status; patron_id = data.fac_id; max_choices = data.max_choices; $('#max_choices_show').html(max_choices); if (logged_in == true){ $('.timer').show(); $.idleLogout({ logoutSeconds: session_life, countdownSeconds: logoff_coundown, updateMilliseconds:1000, logoutUrl: '/' }); setInterval(function(e){ update_timer(e); }, 1000); $('#timeouttext').html(sformat(session_life)) $('.logged_in').show(); $('#user_name').html(fname + ' ' + lname + '!'); $('.not_logged_in').hide(); // uncheck all $('input[type=checkbox]').prop("checked", false); if (num_selected > 0){ data.author_id.forEach(function(id) { $('[data-author_code_id="' + id + '"]').prop('checked', true).change(); }); }; update_selected_counters(); $('#user_name').html(fname + ' ' + lname + '!'); if (data.membership_status == "N"){ $('#membership_status').html('Your membership is currently in Vacation Mode.'); alert("Your membership is currently in 'Vacation Mode'. \nClick the 'Reactivate my Membership' Button to continue to receive holds"); }else{ $('#membership_status').html('Your membership is currently in active mode.'); } $('#membership_status').effect("highlight", {color: "yellow"}, 8000); if (patron_status == 'Y' ){ $('#btnToggleMembership').text("Set my membership to Vacation Mode"); }else{ $('#btnToggleMembership').text("Reactivate my Membership"); } //============== }// end if logged_in = true //============== },'json'); }// end function get_session_data function sformat(s) { var fm = [ Math.floor(s / 60) % 60, // MINUTES s % 60 // SECONDS ]; return $.map(fm, function(v, i) { return ((v < 10) ? '0' : '') + v; }).join(':'); } //============================================== // end doc ready });