/**
 * Supports these field types: "Currency" | "Timezone" | "FKey" | "FKeySet"
 * @constructor
 * @extends LoginPanelAgent
 * @param {String} id This Agent's field reference
 */
function DDAgent(id) {

        this.init(id);

        // procedure callbacks implemented in modRec, filter & collection
        this.update = AgentPCB.update;
        this.onLogin = AgentPCB.onLogin;

        this.submitValue = function(obj) {

                //CheckRule Management:
                var Agent = Manager.getAgent(this.id);
                var doCheckRules = Manager.doWeCheckRules(this.id);
		var hasToBeHiddenByRules = Manager.doWeHideItByRules(this.id);
                var doUpdate = true;
                var previousValue = null;
                //---

                if (obj != '<NullValue/>'){

                        var ov = obj.options[obj.selectedIndex].value;

                        if (ov && ov == 'newRecord') { // new record for FKey or FKeySet
                                var oButton = $('popup_'+this.fieldRef);
                                Manager.openPopup(oButton,oButton.longDesc);
                                return;
                        }
                }

                var xmlValue = '<NullValue/>';
                if (ov && ov!='') {
                        switch (this.fieldType) {
                                case 'FKey' :
                                        var notSureObj = $('check'+this.fieldRef);
                                        var isNotSureParam = '';
                                        if(notSureObj) {
                                                var vals = notSureObj.value.split('|');
                                                notSure = notSureObj.checked ? vals[0] : vals[1];

                                                isNotSureParam = ' notSure="' + notSure + '"';
                                        }

                                        xmlValue = '<FKey recId="'+ov+isNotSureParam + '"/>';
                                        break;
                                case 'FKeySet' :
                                        if (this.recId || this.mmrId) {
                                                Manager.getAgent('link_'+this.fieldRef).addFKey(ov);
                                                $(this.id).selectedIndex = 0;
                                                return;
                                        } else {
                                                var notSureObj = $('check'+this.fieldRef);
                                                var isNotSureParam = '';
                                                if(notSureObj) {
                                                        var vals = notSureObj.value.split('|');
                                                        notSure = notSureObj.checked ? vals[0] : vals[1];

                                                        isNotSureParam = ' notSure="' + notSure + '"';
                                                }

                                                xmlValue = '<FKey recId="'+ov+isNotSureParam + '"/>';
                                        }
                                        break;
                                default :
                                        xmlValue = '<'+this.fieldType+' code="'+ov+'"/>';
                        } // Currency or Timezone
                }

                var xeCommand = Agent.xeGetFieldValue();
                var xeWidgetResult = Agent.xeWidgetResult(xeCommand);
                var xePSValue = xeWidgetResult ? xeWidgetResult.getElementsByTagName('PSValue') : null;
                previousValue = xePSValue && xePSValue != "" ? xeWidgetResult.getElementsByTagName('PSValue')[0].childNodes[0] : null;
                var previousValueRecId = previousValue != null ? previousValue.getAttribute('recId') : null;

                //See XML.js and added String Prototype functions (for "equalsXML"):
                if ( (previousValue == null && xmlValue == null) ||
                         (previousValue != null && xmlValue != null && xmlValue.toString().equalsXML(previousValue.toString()) )
                        ) {

                        doUpdate = false;
                }

                //---
                if (doUpdate) {
                        var doIt = true;

                        if (doCheckRules && previousValue != null)
                                doIt = alertBeforeChange();//Is the user really sure ?

                        if (doIt) {
                                var xeValue = new ParsedXMLElement(xmlValue);

                                var xeCommand;
                                if (this.recId) xeCommand = this.xeSetFieldValue(xeValue);
                                else if (this.mmrId) xeCommand = this.xeSetMMRFieldValue(xeValue);
                                else xeCommand = this.xeGetPSValueLabel(xeValue);


                                var xeWidgetResult = this.xeWidgetResult(xeCommand);
                        this.update(xeWidgetResult);

				if (doCheckRules && !hasToBeHiddenByRules) {
                                        //Will check the rules and "nullify" all values which have to be hidden
                                        checkAllRulesAndFieldValues()
                                }
                        } else {
                                //Reset and select the old value:
                                $j("select[id $='" + id + "'] option:selected").removeAttr("selected");
                                $j("select[id $='" + id + "'] option[value='" + previousValueRecId + "']").attr("selected", "selected");
                        }
                }
        }


	this.newOption = function(xeWidgetResult, recId) {
		switch (this.fieldType) {
	        case 'FKey' :
				var resultLabel = this.getResultLabel(xeWidgetResult);
				var recId = this.getResultValue(xeWidgetResult).getAttribute("recId");
				var newOptionHtml = '<option value="'+recId+'">'+resultLabel+'</option>'
				$j("#"+this.id).append(newOptionHtml);
				
	            $j("select[id $='" + id + "'] option:selected").removeAttr("selected");
				$j("select[id $='" + id + "'] option[value='" + recId + "']").attr("selected", "selected");
	            break;
	        case 'FKeySet' :
	        	var fkeysetLabelText = this.getResultLabel(xeWidgetResult).toString();
	        	var start = fkeysetLabelText.lastIndexOf(";") == -1 ? 0 : fkeysetLabelText.lastIndexOf(";") + 1;
 	
	        	var resultLabel = fkeysetLabelText.substr(start).trim();
				var newOptionHtml = '<option value="'+recId+'">'+resultLabel+'</option>'
				$j("#"+this.id).append(newOptionHtml);
				
	       		$(this.id).selectedIndex = 0;
	        	break;
	        default :
	       		$(this.id).selectedIndex = 0;
		}
	}

	
        //manager.js function over-written
        this.clearValue = function() {
                if (this.fieldType == 'FKeySet') {
                        if (this.recId || this.mmrId) {
                                var linkedAgent = Manager.getAgent('link_'+this.fieldRef);
                                linkedAgent.clearValue();
                        }
                }

        }


        this.toString = function() {
                return 'DDAgent: {agentPrefix: ' + this.agentPrefix + '; fieldRef: ' + this.fieldRef + '}';
        }
}

DDAgent.prototype = new LoginPanelAgent();