jQuery.fn.trilemma = function(options){
  var options  = options || {};
  var cbfs  = this; // establish checkbox container
  var cbs    = this.find('input:checkbox');
  var maxnum  = options.max ? options.max : 2;
    
   
  // If Preference is checked disable all other checkboxes
  // Afterwards remove the class disabled from the preference lable
  
  //Disable the submit button by default
  $('#formbuttons1 input[type="submit"]').attr("disabled", "disabled");
  $('#checkboxerror').show();
  
  
  $(this).bind('click', function() {
    if ($('span.preference input:checkbox').is(':checked')) {     
     
      cbs.not(':last').each( function() {
        $(this).attr('disabled','true');
        $(this).removeAttr('checked');
          if (options.disablelabels) {
            var thisid = $(this).attr('id');
            $('label[for="'+thisid+'"]').addClass('disabled');
          }
            
      });
      
      $('label[for="preference"]').removeClass('disabled');
      
    }   
    
  });

  
  // If a checkbox is checked and it exceeds the max all other checkboxes are disabled
  
  cbs.each( function() {
    $(this).bind('click', function() {
      if ($(this).is(':checked')) {
               
        //If a choise is made activate the submit button
        $('#formbuttons1 input[type="submit"]').removeAttr("disabled");
        $('#checkboxerror').hide();
                       
        if (cbs.filter(':checked').length == maxnum) {
          cbs.not(':checked').each( function() {
            $(this).attr('disabled','true');
            if (options.disablelabels) {
              var thisid = $(this).attr('id');
              $('label[for="'+thisid+'"]').addClass('disabled');
            }
            
          });
        }
      } else {
        
        //If one checkbox is selected activate the submit button
        if (cbs.filter(':checked').length == 1) { 
          $('#formbuttons1 input[type="submit"]').removeAttr("disabled");
          $('#checkboxerror').hide();
        }
        
        //If nothing is selected disble the submit button
        else {
          $('#formbuttons1 input[type="submit"]').attr("disabled", "disabled");
          $('#checkboxerror').show();
        }
        
        cbs.removeAttr('disabled');
        if (options.disablelabels) {
          cbfs.find('label.disabled').removeClass('disabled');
        }
      }
    }
    );
  });
  
  return this;
}
  
  

