// Page level object containing the buttons and their assigned trigger fields
// Use as follows from client-side scripts:
//
//     PageButtonTriggerFields.addTrigger('someButtonClientId', 'someFieldClientId');
//
var PageButtonTriggerFields = {
    "Buttons" : [],
    
    addTrigger : function(buttonId, fieldId) {
        var matchFound = false;
        
        $.each(this.Buttons, function() {
            if(this.ButtonID == buttonId) {
                this.Fields.push(fieldId);
                matchFound = true;
                return;
            }
        });
        
        if(matchFound)
            return;
            
        var item = {
            "ButtonID" : buttonId,
            "Fields" : [fieldId]
        };
        
        this.Buttons.push(item);
    }
};


// After page loaded, assign the keypress events to the proper fields
$(function () {
    $.each(PageButtonTriggerFields.Buttons, function () {
        var buttonId = this.ButtonID;
        $.each(this.Fields, function () {
            $("#" + this).live("keypress", function (e) {
                // JQuery consolidates the keycode into a single property regardless of browser
                if (e.which == 13) {
                    e.preventDefault();
                    $("#" + buttonId).focus().click();
                }
            });
        });
    });
});
