jQuery.fn.allfield = function(action) {
  return this.each( function() {
        if(action) {
            switch(action) {
                case "update" :
                    //update state of checkbox based on the value of the text box
                    //this need to be called manually as the $().val("foo") does not trigger the on changed event
                    var inputText = $("input:text", this);
                    if (inputText.length == 0) {
                        inputText = $("input:hidden", this);
                    }
                    var label = $("label", this);
                    var checkBox = $("input:checkbox", this);
                    if(inputText.val() == label.text()) {
                        inputText.attr('disabled', 'true');
                        checkBox.attr('checked', true)
                    } else {
                        inputText.removeAttr('disabled');
                        checkBox.removeAttr('checked')
                    }
                    break;
            }
        } else {
            $(this).change( function(e) {
                var target = $(e.target);
                if(target.is(':checkbox')) {
                    //update the value of the text box or equivalen hidden field for a change event on the check box
                    var inputText = $("input:text", this);
                    if (inputText.length == 0) {
                        inputText = $("input:hidden", this);
                    }

                    if(target.is(":checked")) {
                        var label = $("label", this);
                        inputText.attr('old-value', inputText.val());
                        inputText.attr('disabled', 'true');
                        inputText.val(label.text());
                    } else {
                        inputText.val(inputText.attr('old-value') ? inputText.attr('old-value') : "");
                        inputText.removeAttr('disabled', 'true');
                    }
                }
            });
        }
    }
  );
};

