/**
 * Supports these field types: "Bool" | "Gender" | "FKey" | "FKeySet"
 * @constructor
 * @base Agent
 * @param {String} id This Agent's field reference
 */
function CheckBoxAgent(id) {

	this.init(id);

	// procedure callbacks implemented in modRec, filter & collection
	this.onLogin = AgentPCB.onLogin;
	this.update = AgentPCB.update;

	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;
		//---
		
		var xmlValue = '<NullValue/>';
		var ov = obj.value;
		if (ov!='null') {
			switch (this.fieldType) {
				case 'FKey' : 
					xmlValue = '<FKey recId="'+ov+'"/>'; 
					break;
				case 'FKeySet' :
					if (this.recId || this.mmrId) {
						Manager.getAgent('link_'+this.fieldRef).addFKey(ov);
						this.reset();
						return;
					} else 
						xmlValue = '<FKey recId="'+ov+'"/>';
					break;
			default : // Bool | Gender
				if (obj.type=='checkbox') {
					var vals = ov.split('|');
					ov = (obj.checked) ? vals[0] : vals[1];}
					xmlValue = '<'+this.fieldType+' value="'+ov+'"/>';
			}
		}

		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("input[type='radio'][id $='" + id + "'][:checked]").removeAttr("checked");
				$j("input[type='radio'][id $='" + id + "'][value='" + previousValueRecId + "']").attr("checked", "checked");
			}
		}			
    }

    this.reset = function() {
    	var rbs = d.forms[0][this.id];
    	for (var i=0; i<rbs.length; i++) {
			if (rbs[i].checked) rbs[i].checked = false;}}

	this.toString = function() {
		return 'CheckBoxAgent: {agentPrefix: ' + this.agentPrefix + '; fieldRef: ' + this.fieldRef + '}'}
}

CheckBoxAgent.prototype = new LoginPanelAgent();