/**
 * @constructor
 * @extends Agent
 * @param {String} id (Required) This Agent's field reference
 */
function FKeySetAgent(id) {

	this.init(id);
	this.hasFocus = false;

	/**
	 * 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)}

	/**
	 * Move FKey with given index in FKeySet the given direction
	 * @param dir 'up' | 'down'
	 */
	this.moveFKey = function(index,dir) {
		var xeFKeySet = this.getExistingFKeySet();
		var fMove = function(i) { // move down on i is same as move up on i-1
			var xeChild = xeFKeySet.childNodes[i];
			xeFKeySet.removeChild(xeChild);
			xeFKeySet.insertChild(xeChild,i+1)}
    	if (xeFKeySet) {
    		if (dir=='up' && index>0) {
    			fMove(index-1);
    			this.submitFKeySet(xeFKeySet,'moveUp_'+this.fieldRef+(index-1));
    		}
    		else if (dir=='down' && index<xeFKeySet.childNodes.length-1) {
    			fMove(index);
    			this.submitFKeySet(xeFKeySet,'moveDown_'+this.fieldRef+(index+1))}}}
    
	this.refreshFKeySet = function() {
		var xeFKeySet = this.getExistingFKeySet();
		this.submitFKeySet(xeFKeySet);}
	
    /**
     * Displays the modify order UI
     * @param {boolean} isFocus Is true if this a focus or blur event
     */
    this.dispOrderMod = function(isFocus) {
    	this._orderMod(isFocus)}
    
    /**
     * Hides the modify order UI if none of the elements have focus
     * @param {boolean} isFocus Is true if this a focus or blur event
     */
    this.hideOrderMod = function(isFocus) {
    	this._orderMod(isFocus,true)}
    
    this._orderMod = function(isFocus,isHide) {
    	if (isFocus) this.hasFocus = !isHide;
    	var container = $('move_'+this.fieldRef);
    	var tds = container.getElementsByTagName('TD');
    	for (var i=0; i<tds.length; i++) {
    		if (isHide && !this.hasFocus && tds[i].className=='disp') tds[i].className = 'hide';
    		else if (!isHide && tds[i].className=='hide') tds[i].className = 'disp';}}

	/**
	 * @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,focusObjId) {
		var isNull = false;
		if (!xeFKeySet || xeFKeySet.childNodes.length==0) {
			xeFKeySet = new XMLElement('NullValue');
			isNull = true;
		}
		var xeCommand;
		if (this.recId) {
			var oArgs = {returnHTMLLabel:true};
			xeCommand = this.xeSetFieldValue(xeFKeySet,oArgs);
		} else {
			if(!document.forms['form_' + this.fieldRef].elements[0].checked && !isNull) {
				var oArgs = {returnHTMLLabel:true, modificationKind:'add'};
			} else {
				var oArgs = {returnHTMLLabel:true};
			}
			xeCommand = this.xeSetMMRFieldValue(xeFKeySet,oArgs);
		}
		var xeWidgetResult = this.xeWidgetResult(xeCommand);

		var xePSValueLabel = xeWidgetResult.getChild('PSValueLabel');
		//var html = "&nbsp;";
		
		var html = "";
		if (xePSValueLabel) {
			var lcd = xePSValueLabel.getChildData();
			if (lcd && lcd!='') html = lcd.replace(/ class='hide'/gi," class='disp'");}
		$('label_'+this.fieldRef).innerHTML = html;
		//Can be null:
		if (focusObjId && $(focusObjId)) {
			setTimeout(function(focusObjId){
				$(focusObjId).focus()
			}, 100, focusObjId);
		}
		
		this.displayErrorMsg(this.getErrorMsg(xeWidgetResult));
	}

	//manager.js function over-written
	this.clearValue = function() {
		this.submitFKeySet();	//will send "NullValue" and so will clear the BlockSet.
	}
	
	
	this.toString = function() {
		return 'FKeySetAgent: {agentPrefix: ' + this.agentPrefix + '; fieldRef: ' + this.fieldRef + '}'}
}

FKeySetAgent.prototype = new Agent();