
$(document).ready(function(){ init(); });

function init() {

    // Initialise table row sorting
    $("table.sortable").tableDnD({
      dragHandle: 'dragger',
      onDrop: function(table, row) {
        // update row colors
        $("table.sortable tr").removeClass('listRowOdd listRowEven');
        $("table.sortable tr:odd:not(.nodrag)").addClass('listRowOdd');
        $("table.sortable tr:even:not(.nodrag)").addClass('listRowEven');

        // get new order
        var rows     = table.tBodies[0].rows;
        var newOrder = "";
        for (var i=0; i<rows.length; i++) {
            var order = $("._recordNum", rows[i]).val();
            if (order) {
              if (newOrder != "") { newOrder += ","; }
              newOrder += order;
            }
        }

        // Save changes via ajax
        $.ajax({
          url: '?',
          type: "POST",
          data: {
            menu:       $('#menu').val(),
            action:     'listDragSort',
            recordNums: newOrder
          },
          error:  function(XMLHttpRequest, textStatus, errorThrown){
            alert("There was an error sending the request! (" +XMLHttpRequest['status']+" "+XMLHttpRequest['statusText'] +")");
          },
          success: function(msg){
            if (msg) { alert("Error: " + msg); }
          }
        });

      }
    });
}

//
function toggleCheckboxes(checkedBool) {

  if (checkedBool) {
    $('.selectRecordCheckbox').each(function() {
        this.checked = "checked";
    });
  }
  else {
    $('.selectRecordCheckbox').each(function() {
        this.checked = "";
    });
  }

}


function toggleAdvancedSearchOptions() {

  //$('.hideShowSecondarySearchFields, .secondarySearchField').toggle();
  // workaround for jquery 1.3.2 IE8 bug with toggle() as described here: http://dev.jquery.com/ticket/4512 and http://stackoverflow.com/questions/975153/jquery-toggle-not-working-with-trs-in-ie
  $('.hideShowSecondarySearchFields, .secondarySearchField').each(function(){
    if (this.style.display == 'none') { $(this).show(); }
    else                              { $(this).hide(); }
  });

  //
  var showingSecondaryFields = $('.secondarySearchField:visible').length > 0;
  if (showingSecondaryFields) { $('#showAdvancedSearch').val('1'); }
  else                        { $('#showAdvancedSearch').val('0'); }

}
