/**
 * @constructor
 * @extends Agent
 * @param {String} id (Required) This Agent's field reference
 */
function FKeySetAgent(id) {

        this.init(id);

        /**
         * Deletes FKey with given index from FKeySet
         */
        this.deleteFKey = function(index) {
                var xeFKeySet = this.getExistingFKeySet();
            if (xeFKeySet) {
                        xeFKeySet.removeChild(xeFKeySet.childNodes[index]);
                        this.submitFKeySet(xeFKeySet)}}

        /**
         * Adds a FKey with the given record id to the FKeySet
         */
        this.addFKey = function(recId) {
                var xeFKeySet = this.getExistingFKeySet();
                if (!xeFKeySet) xeFKeySet = new XMLElement('FKeySet');
                var xeFKey = new XMLElement('FKey',xeFKeySet);
                xeFKey.setAttribute('recId',recId);
                this.submitFKeySet(xeFKeySet)}

        /**
         * @private
         */
        this.getExistingFKeySet = function() {
                var oArgs = {includeModGUIData:false};
                var xeCommand = (this.recId) ? this.xeGetFieldValue(oArgs) : this.xeGetMMRFieldValue(oArgs);
                var xeWidgetResult = this.xeWidgetResult(xeCommand);
        var xePSValue = xeWidgetResult.getChild('PSValue');
        if (xePSValue) return xePSValue.getChild('FKeySet');}

        /**
         * @private
         */
        this.submitFKeySet = function(xeFKeySet) {

                if (xeFKeySet.childNodes.length==0) xeFKeySet = new XMLElement('NullValue');
                var oArgs = {returnHTMLLabel:false};
                var xeCommand;
                if (this.recId) xeCommand = this.xeSetFieldValue(xeFKeySet,oArgs);
                else xeCommand = this.xeSetMMRFieldValue(xeFKeySet,oArgs);
                var xeWidgetResult = this.xeWidgetResult(xeCommand);

                var xePSValueLabel = xeWidgetResult.getChild('PSValueLabel');
                var html = "&nbsp;";
                if (xePSValueLabel) {
                        var lcd = xePSValueLabel.getChildData();
                        if (lcd && lcd!='') html = lcd;}
                $('label_'+this.fieldRef).innerHTML = html;
                this.displayErrorMsg(this.getErrorMsg(xeWidgetResult));}

        this.toString = function() {
                return 'FKeySetAgent: {agentPrefix: ' + this.agentPrefix + '; fieldRef: ' + this.fieldRef + '}'}
}

FKeySetAgent.prototype = new Agent();
