/**
 * Supports: "FKey" | "FKeySet"
 * @constructor
 * @extends Agent
 * @param {String} id This Agent's id
 */
function PopupAgent(id) {

	if (id) { // prevents prototype declarations creating these attribs & methods
		this.init(id);
		this.popupWindow = null; // set when popup window opens, reset to null when closing

		// procedure callbacks implemented in modRec, filter & collection
		this.update = AgentPCB.update;
	}
	this.isStable = function() { // is closed
		// if the popup is open there may be unsaved modifications
		try {
			return (!this.popupWindow || this.popupWindow.closed!=false)}
		catch (e) {
			return true}}

	this.getObj = function() {
		return $(this.id)}

	this.openPopup = function(href) {
		// check that modifier is not already open
		if (this.isStable()) {
			// open popup
			this.popupWindow = openPositionedPopup(href, Manager.getWinName(this.id), this.getObj())}}

	this.closePopup = function() {
		if (!this.isStable()) this.popupWindow.close();}

	/**
	 * Called by popup window onSubmit. Submits new value to server.
	 */
	this.sendRequest = function(recId) {

		if (this.fieldType=='FKeySet' && this.recId) {
			Manager.getAgent('link_'+this.fieldRef).addFKey(recId);
			this.closePopup()}
		else {
			var xmlValue = (recId && recId!='') ? '<FKey recId="'+recId+'"/>' : '<NullValue/>';
			var xeValue = new ParsedXMLElement(xmlValue);

			var xeCommand;
			if (this.recId) xeCommand = this.xeSetFieldValue(xeValue);
			else if (this.mmrId) xeCommand = this.xeMMRSetFieldValue(xeValue);
			else xeCommand = this.xeGetPSValueLabel(xeValue,{dateFormat:'long'});

			var xeWidgetResult = this.xeWidgetResult(xeCommand);
	        this.update(xeWidgetResult)}}

    this.displayErrorMsgInPopup = function(errorMsg) {
    	if (!this.isStable) { // popup win is open
    		var pw = this.popupWindow;
    		var oDiv = pw.$("errorMsg");
    		if (oDiv) { // DIV exists
    			oDiv.innerHTML = errorMsg;
				oDiv.style.display = (errorMsg!=null && errorMsg!='') ? '' : 'none';
				if (pw.resizePopup) pw.resizePopup();}}}

	this.toString = function() {
		return 'PopupAgent: {agentPrefix: ' + this.agentPrefix + '; fieldRef: ' + this.fieldRef + '}'}
}

PopupAgent.prototype = new Agent();
