/*
 *+------------------------------------------------------------------------+
 *| IBM Confidential
 *| OCO Source Materials
 *| BI and PM: viewer
 *| (C) Copyright IBM Corp. 2001, 2009
 *|
 *| US Government Users Restricted Rights - Use, duplication or
 *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *|
 *+------------------------------------------------------------------------+
 */

/**
 * CognosViewerAction constructor (base class for all cognos viewer action
 * @constructor
 */
function CognosViewerAction()
{
	this.m_oCV = null;
};

/**
 * Sets the cognos viewer object (called by the action factory
 * @param CCognosViewer object
 * @private
 */
CognosViewerAction.prototype.setCognosViewer = function(oCV)
{
	this.m_oCV = oCV;
};

/**
 * Returns an instance to the cognos viewer object
 * @return CCognosViewer object
 */
CognosViewerAction.prototype.getCognosViewer = function()
{
	return this.m_oCV;
};

/**
 * LineageAction - implements lineage in cognos viewer
 */
function LineageAction(){};
LineageAction.prototype = new CognosViewerAction();

/**
 * Execute the lineage request
 */
LineageAction.prototype.execute = function()
{
	var cognosViewer = this.getCognosViewer();
	var selectionController = cognosViewer.getSelectionController();
	var selectionList = selectionController.getAllSelectedObjects();

	if(selectionList.length > 0)
	{

		var config = null;
		if(typeof MDSRV_CognosConfiguration != "undefined")
		{
			var config = new MDSRV_CognosConfiguration();

			var lineageURI = "";
			if(cognosViewer.envParams["metadataInformationURI"])
			{
				lineageURI = cognosViewer.envParams["metadataInformationURI"];
			}

			var dispatcherURI = "";
			if(cognosViewer.envParams["dispatcherURI"])
			{
				dispatcherURI = cognosViewer.envParams["dispatcherURI"];
			}

			config.addProperty("lineageURI", lineageURI);
			config.addProperty("dispatcherURI", dispatcherURI);
			config.addProperty("gatewayURI", cognosViewer.getGateway());
		}

		var searchPath = cognosViewer.envParams["ui.object"];
		var sSelectionContext = getViewerSelectionContext(selectionController, new CSelectionContext(searchPath));

		var lineageHelper = new MDSRV_LineageFragmentContext(config, sSelectionContext);
		lineageHelper.setExecutionParameters(cognosViewer.getExecutionParameters());
		lineageHelper.setReportPath( searchPath );

		lineageHelper.open();
	}
};

/**
 * GlossaryeAction - implements IBM business glossary in cognos viewer
 */
function GlossaryAction(){};
GlossaryAction.prototype = new CognosViewerAction();

/**
 * Execute the IBM business glossary request
 */
GlossaryAction.prototype.execute = function()
{
	var cognosViewer = this.getCognosViewer();
	var selectionController = cognosViewer.getSelectionController();
	var selectionList = selectionController.getAllSelectedObjects();

	if(selectionList.length > 0)
	{
		var config = null;
		if(typeof MDSRV_CognosConfiguration != "undefined")
		{
			var config = new MDSRV_CognosConfiguration();

			var glossaryURI = "";
			if(cognosViewer.envParams["glossaryURI"])
			{
				glossaryURI = cognosViewer.envParams["glossaryURI"];
			}

			var dispatcherURI = "";
			if(cognosViewer.envParams["dispatcherURI"])
			{
				dispatcherURI = cognosViewer.envParams["dispatcherURI"];
			}

			config.addProperty("glossaryURI", glossaryURI);
			config.addProperty("dispatcherURI", dispatcherURI);
			config.addProperty("gatewayURI", cognosViewer.getGateway());
		}

		var searchPath = cognosViewer.envParams["ui.object"];
		var sSelectionContext = getViewerSelectionContext(selectionController, new CSelectionContext(searchPath));

		var glossaryHelper = new MDSRV_BusinessGlossary(config, sSelectionContext);
		glossaryHelper.open();
	}
};

/**
 * DrillDownAction - implements drill down in cognos viewer
 */
function DrillDownAction(){};
DrillDownAction.prototype = new CognosViewerAction();

/**
 * Execute the drill down action
 */
DrillDownAction.prototype.execute = function()
{
};

/**
 * DrillUpAction - implements drill up in cognos viewer
 */
function DrillUpAction(){};
DrillUpAction.prototype = new CognosViewerAction();

/**
 * Execute the drill down action
 */
DrillUpAction.prototype.execute = function()
{

};

/**
 * GotoAction - implements model drill in cognos viewer
 */
function GotoAction(){};
GotoAction.prototype = new CognosViewerAction();

GotoAction.prototype.execute = function()
{
};

/**
 * EmailAction - implements email in cognos viewer
 */
function EmailAction(){};
EmailAction.prototype = new CognosViewerAction();

EmailAction.prototype.execute = function()
{
	//TODO add support for launching a dialog
	return null;
};

/**
 * SaveAsAction - implements saveAs in cognos viewer
 */
function SaveAsAction(){};
SaveAsAction.prototype = new CognosViewerAction();

SaveAsAction.prototype.execute = function()
{
	//TODO add support for launching a dialog
	return null;
};

/**
 * SaveAction - implements save in cognos viewer
 */
function SaveAction(){};
SaveAction.prototype = new CognosViewerAction();

SaveAction.prototype.execute = function()
{
	var cognosViewerRequest = null;
	var cognosViewer = this.getCognosViewer();

	if(cognosViewer)
	{
		cognosViewerRequest = new CCognosViewerRequest("save");

		var sStatus = cognosViewer.getStatus();
		if(sStatus != "working")
		{
			cognosViewerRequest.addFormField("run.continueConversation", "true");
		}
		else
		{
			cognosViewerRequest.addFormField("run.continueConversation", "false");
		}

		cognosViewerRequest.addFormField("run.saveOutput", "true");
	}

	return cognosViewerRequest;
};

/**
 * DownloadAction - implements download in cognos viewer
 */
function DownloadAction(){};
DownloadAction.prototype = new CognosViewerAction();

DownloadAction.prototype.execute = function()
{
	var cognosViewerRequest = new CCognosViewerRequest("");
	var cognosViewer = this.getCognosViewer();

	cognosViewerRequest.addFormField("b_action", "xts.run");
	cognosViewerRequest.addFormField("m", "portal/download.xts");
	cognosViewerRequest.addFormField("m_obj", cognosViewer.envParams["ui.object"]);
	cognosViewerRequest.addFormField("m_name", cognosViewer.envParams["ui.name"]);

	if(cognosViewer.envParams["ui.action"] != "view")
	{
		cognosViewerRequest.addFormField("format",cognosViewer.envParams["run.outputFormat"]);
	}

	cognosViewerRequest.setRequestType("get");

	return cognosViewerRequest;
};

/**
 * RunAction - implements re-run in cognos viewer
 */
function RunAction(){};
RunAction.prototype = new CognosViewerAction();

RunAction.prototype.execute = function()
{
};

/**
 * RenderHTMLAction - implements render in html
 */
function RenderHTMLAction(){};
RenderHTMLAction.prototype = new CognosViewerAction();

RenderHTMLAction.prototype.execute = function()
{
};

/**
 * RenderPDFAction - implements render in pdf
 */
function RenderPDFAction(){};
RenderPDFAction.prototype = new CognosViewerAction();

RenderPDFAction.prototype.execute = function()
{
};

/**
 * RenderXMLAction - implements render in xml
 */
function RenderXMLAction(){};
RenderXMLAction.prototype = new CognosViewerAction();

RenderXMLAction.prototype.execute = function()
{
};

/**
 * RenderXLSAction - implements render in excel 2000
 */
function RenderXLSAction(){};
RenderXLSAction.prototype = new CognosViewerAction();

RenderXLSAction.prototype.execute = function()
{
};

/**
 * RenderSingleXLSAction - implements render in excel single sheet format
 */
function RenderSingleXLSAction(){};
RenderSingleXLSAction.prototype = new CognosViewerAction();

RenderSingleXLSAction.prototype.execute = function()
{
};

/**
 * RenderXLWAAction - implements render in excel web archive format
 */
function RenderXLWAAction(){};
RenderXLWAAction.prototype = new CognosViewerAction();

RenderXLWAAction.prototype.execute = function()
{
};

/**
 * RenderCSVAction - implements render in csv format
 */
function RenderCSVAction(){};
RenderCSVAction.prototype = new CognosViewerAction();

RenderCSVAction.prototype.execute = function()
{
};

/**
 * RenderSpreadsheetMLAction - implements render in excel 2007 format
 */
function RenderSpreadsheetMLAction(){};
RenderSpreadsheetMLAction.prototype = new CognosViewerAction();

RenderSpreadsheetMLAction.prototype.execute = function()
{
};


/**
 * Implements authored drill through
 */
function AuthoredDrillAction(){};
AuthoredDrillAction.prototype = new CognosViewerAction();

AuthoredDrillAction.prototype.executeDrillTarget = function(drillTargetSpecification)
{
	var drillTargetNode = XMLHelper_GetFirstChildElement( XMLBuilderLoadXMLFromString (drillTargetSpecification) );

	var sBookmarkRef = drillTargetNode.getAttribute("bookmarkRef");
	var sTargetPath = drillTargetNode.getAttribute("path");
	var sShowInNewWindow = drillTargetNode.getAttribute("showInNewWindow");

	if((sBookmarkRef != null && sBookmarkRef != "") && (sTargetPath == null || sTargetPath == ""))
	{
		document.location = "#" + sBookmarkRef;
	}
	else
	{
		var sTarget = "";
		if(sShowInNewWindow == "true")
		{
			sTarget = "_blank";
		}

		var aArguments = new Array();

		var objPathArguments = new Array();
		objPathArguments.push("obj");
		objPathArguments.push(sTargetPath);

		aArguments[aArguments.length] = objPathArguments;

		var drillParameterNodes = XMLHelper_FindChildrenByTagName(drillTargetNode, "drillParameter", false);
		for(var index = 0; index < drillParameterNodes.length; ++index)
		{
			var drillParameterArguments = new Array();
			var drillParameterNode = drillParameterNodes[index];
			var sValue = drillParameterNode.getAttribute("value");
			var sName = drillParameterNode.getAttribute("name");

			if(sValue != null && sValue != "")
			{
				drillParameterArguments.push("p_" + sName);
				drillParameterArguments.push(this.buildSelectionChoicesSpecification(drillParameterNode));
			}

			var sNil = drillParameterNode.getAttribute("nil");
			if(sNil != null && sNil != "")
			{
				drillParameterArguments.push("p_" + sName);
				drillParameterArguments.push(this.buildSelectionChoicesNilSpecification());
			}

			if(drillParameterArguments.length > 0)
			{
				aArguments[aArguments.length] = drillParameterArguments;
			}
		}

		var sMethod = drillTargetNode.getAttribute("method");

		var sOutputFormat = drillTargetNode.getAttribute("outputFormat");

		var sOutputLocale = drillTargetNode.getAttribute("outputLocale");

		var sPrompt = drillTargetNode.getAttribute("prompt");
		var dynamicDrill = drillTargetNode.getAttribute("dynamicDrill");

		var sSourceContext = this.getXMLNodeAsString(drillTargetNode, "parameters");
		var sObjectPaths = this.getXMLNodeAsString(drillTargetNode, "objectPaths");

		// if the source and target are the same report, then do a forward instead of a drillThrough action
		var formWarpRequest = document.forms["formWarpRequest" + this.getCognosViewer().getId()];
		if ( this.isSameReport(formWarpRequest, sTargetPath) && this.isSameReportFormat(sOutputFormat)  )
		{
			var cognosViewerRequest = new CCognosViewerRequest("forward");
			var oCV = this.getCognosViewer();

			if(oCV != null && typeof oCV.rvMainWnd != "undefined")
			{
				oCV.rvMainWnd.addCurrentReportToReportHistory();
				var reportHistorySpecification = oCV.rvMainWnd.saveReportHistoryAsXML();
				cognosViewerRequest.addParameter("cv.previousReports", reportHistorySpecification);
			}

			// if we're drilling through to ourself we need to send empty parameters for
			// the parameters that are setup to no send any parameter values
			for(var index = 0; index < drillParameterNodes.length; ++index)
			{
				var drillParameterArguments = new Array();
				var drillParameterNode = drillParameterNodes[index];
				var sValue = drillParameterNode.getAttribute("value");
				var sName = drillParameterNode.getAttribute("name");
				var sNil = drillParameterNode.getAttribute("nil");

				if((sNil == null || sNil == "") && (sValue == null || sValue == ""))
				{
					drillParameterArguments.push("p_" + sName);
					drillParameterArguments.push(this.buildSelectionChoicesNilSpecification());
				}

				if(drillParameterArguments.length > 0)
				{
					aArguments[aArguments.length] = drillParameterArguments;
				}
			}

			for (var index=1; index < aArguments.length; index++)
			{
				cognosViewerRequest.addParameter(aArguments[index][0], aArguments[index][1]);
			}

			oCV.setUsePageRequest(true);

			this.getCognosViewer().sendRequest(cognosViewerRequest);

			setTimeout("oCV"+oCV.getId()+".m_waitPage.showSimpleWaitDialog(oCV" + oCV.getId() + ")",10);
		}
		else
		{
			doSingleDrill(sTarget, aArguments, sMethod, sOutputFormat, sOutputLocale, sBookmarkRef, sSourceContext, sObjectPaths, this.getCognosViewer().getId(), sPrompt, dynamicDrill);
		}
	}
};

AuthoredDrillAction.prototype.isSameReport = function( formWarpRequest, sTargetPath )
{
	if( formWarpRequest["ui.object"] && sTargetPath == formWarpRequest["ui.object"].value )
	{
		return true;
	}

	return false;

}

AuthoredDrillAction.prototype.isSameReportFormat = function( drillTargetFormat )
{
	var drillSourceFormat =  this.getCognosViewer().envParams["run.outputFormat"];
	if( drillSourceFormat )
	{
		if (drillTargetFormat == drillSourceFormat )
		{
			return true;
		}
		//the following case occurs when the target drill-thru definition is set to default format
		// and the source format is HTML.
		else if( drillSourceFormat == "HTML" && drillTargetFormat == "HTMLFragment")
		{
			return true;
		}
	}
	return false;
}

AuthoredDrillAction.prototype.getXMLNodeAsString = function(drillTargetNode, sNodeName)
{
	var sXML = "";
	if(drillTargetNode != null)
	{
		var node = XMLHelper_FindChildByTagName(drillTargetNode, sNodeName, false);
		if(node != null)
		{
			sXML = XMLBuilderSerializeNode(node);
		}
	}

	return sXML;
};

AuthoredDrillAction.prototype.execute = function(rvDrillTargetsSpecification)
{
	var drillTargetSpecifications = this.getCognosViewer().rvMainWnd.getDrillTargets();
	var rvDrillTargetsNode = this.getAuthoredDrillThroughContext(rvDrillTargetsSpecification, drillTargetSpecifications);

	var drillTargets = rvDrillTargetsNode.childNodes;
	if(drillTargets.length == 1)
	{
		this.executeDrillTarget(XMLBuilderSerializeNode(drillTargets[0]));
	}
	else
	{
		doMultipleDrills(XMLBuilderSerializeNode(rvDrillTargetsNode), this.getCognosViewer().getId());
		//Need support from goto page
		//this.showDrillTargets(drillTargets);
	}
};

AuthoredDrillAction.prototype.showDrillTargets = function(drillTargets)
{
	var sAuthoredDrillThroughContext = "<context>";

	for(var index = 0; index < drillTargets.length; ++index)
	{
		var drillTarget = drillTargets[index];

		sAuthoredDrillThroughContext += "<member>";

		var sName = drillTarget.getAttribute("label");
		sAuthoredDrillThroughContext += "<name>";
		sAuthoredDrillThroughContext += sXmlEncode(sName);
		sAuthoredDrillThroughContext += "</name>";

		var sDrillThroughSearchPath = drillTarget.getAttribute("path");
		sAuthoredDrillThroughContext += "<drillThroughSearchPath>";
		sAuthoredDrillThroughContext += sXmlEncode(sDrillThroughSearchPath);
		sAuthoredDrillThroughContext += "</drillThroughSearchPath>";

		var sDrillThroughAction = drillTarget.getAttribute("method");
		sAuthoredDrillThroughContext += "<drillThroughAction>";
		sAuthoredDrillThroughContext += sXmlEncode(sDrillThroughAction);
		sAuthoredDrillThroughContext += "</drillThroughAction>";

		var sDrillThroughFormat = drillTarget.getAttribute("outputFormat");
		sAuthoredDrillThroughContext += "<drillThroughFormat>";
		sAuthoredDrillThroughContext += sXmlEncode(sDrillThroughFormat);
		sAuthoredDrillThroughContext += "</drillThroughFormat>";

		var sData = "parent." + this.getTargetReportRequestString(drillTarget);
		sAuthoredDrillThroughContext += "<data>";
		sAuthoredDrillThroughContext += sXmlEncode(sData);
		sAuthoredDrillThroughContext += "</data>";

		sAuthoredDrillThroughContext += "</member>";
	}

	sAuthoredDrillThroughContext += "</context>";

	// need to fix the ui.backURL and errURL since they'll be getting rejected by caf. TODO post BSEINS
	//cvLoadDialog(this.getCognosViewer(), {"m":"portal/goto.xts","ui.backURL":"javascript:parent.destroyCModal();", "errURL":"javascript:parent.destroyCModal();","authoredDrillthru":sAuthoredDrillThroughContext}, 600, 425);
};

AuthoredDrillAction.prototype.populateContextMenu = function(rvDrillTargetsSpecification)
{
	var toolbarCtrl = this.getCognosViewer().rvMainWnd.getToolbarControl();
	var authoredDrillDropDownMenu = null;
	if (typeof toolbarCtrl != 'undefined' && toolbarCtrl != null)
	{
		var toolbarButton = toolbarCtrl.getItem("goto");
		if (toolbarButton)
		{
			authoredDrillDropDownMenu = toolbarButton.getMenu();
		}
	}

	var cognosViewerContextMenu = this.getCognosViewer().rvMainWnd.getContextMenu();
	var authoredDrillContextMenu = null;
	if (typeof cognosViewerContextMenu != 'undefined' && cognosViewerContextMenu != null)
	{
		authoredDrillContextMenu = cognosViewerContextMenu.getGoToMenuItem().getMenu();
	}

	if(authoredDrillDropDownMenu != null || authoredDrillContextMenu != null)
	{
		var drillTargetSpecifications = this.getCognosViewer().rvMainWnd.getDrillTargets();
		var rvDrillTargetsNode = this.getAuthoredDrillThroughContext(rvDrillTargetsSpecification, drillTargetSpecifications);

		var drillTargets = rvDrillTargetsNode.childNodes;
		if(drillTargets.length > 0)
		{
			for(var index = 0; index < drillTargets.length; ++index)
			{
				var drillTarget = drillTargets[index];

				var sRequestString = "";

				var sRequestString = "oCV" + this.getCognosViewer().getId() + ".m_oDrillMgr.executeAuthoredDrill(\"" + encodeURIComponent(XMLBuilderSerializeNode(drillTarget)) + "\");";
				var sIconPath = this.getTargetReportIconPath(drillTarget);

				var sLabel = drillTarget.getAttribute("label");

				if (authoredDrillDropDownMenu != null)
				{
					new CMenuItem(authoredDrillDropDownMenu, sLabel, sRequestString, sIconPath, gMenuItemStyle, this.getCognosViewer().getWebContentRoot(), this.getCognosViewer().getSkin());
				}

				if (authoredDrillContextMenu != null)
				{
					new CMenuItem(authoredDrillContextMenu, sLabel, sRequestString, sIconPath, gMenuItemStyle, this.getCognosViewer().getWebContentRoot(), this.getCognosViewer().getSkin());
				}
			}
		}
	}

};

AuthoredDrillAction.prototype.buildSelectionChoicesNilSpecification = function()
{
	return "<selectChoices/>";
}

AuthoredDrillAction.prototype.buildSelectionChoicesSpecification = function(drillParameterNode)
{
	var sSelectionChoicesSpecification = "";

	var sValue = drillParameterNode.getAttribute("value");

	if(sValue != null)
	{
		if(sValue.indexOf("<selectChoices>") != -1)
		{
			return sValue;
		}
		else if(sValue != "")
		{
			sSelectionChoicesSpecification += "<selectChoices>";
			sSelectionChoicesSpecification += "<selectOption ";

			if (drillParameterNode.getAttribute("propertyToPass") != null && drillParameterNode.getAttribute("propertyToPass") != "")
			{
				sSelectionChoicesSpecification += "propertyToPass=\""
				sSelectionChoicesSpecification += sXmlEncode(drillParameterNode.getAttribute("propertyToPass"));
				sSelectionChoicesSpecification += "\" ";
			}

			var sMun = drillParameterNode.getAttribute("mun");
			if(sMun != null && sMun != "")
			{
				sSelectionChoicesSpecification += "useValue=\""
				sSelectionChoicesSpecification += sXmlEncode(sMun);
				sSelectionChoicesSpecification += "\" ";

				sSelectionChoicesSpecification += "mun=\""
				sSelectionChoicesSpecification += sXmlEncode(sMun);
				sSelectionChoicesSpecification += "\" ";

				sSelectionChoicesSpecification += "displayValue=\""
				sSelectionChoicesSpecification += sXmlEncode(sValue);
				sSelectionChoicesSpecification += "\"";

			}
			else
			{
				sSelectionChoicesSpecification += "useValue=\""
				sSelectionChoicesSpecification += sXmlEncode(sValue);
				sSelectionChoicesSpecification += "\" ";

				var sDisplayValue = drillParameterNode.getAttribute("displayValue");
				if(sDisplayValue == null || sDisplayValue == "")
				{
					sDisplayValue = sValue
				}

				sSelectionChoicesSpecification += "displayValue=\""
				sSelectionChoicesSpecification += sXmlEncode(sDisplayValue);
				sSelectionChoicesSpecification += "\"";
			}

			sSelectionChoicesSpecification += "/>";
			sSelectionChoicesSpecification += "</selectChoices>";
		}
	}

	return sSelectionChoicesSpecification;
}

AuthoredDrillAction.prototype.getPropertyToPass = function(parameterName, parameterProperties)
{
	if (parameterName != null && parameterName != "" && parameterProperties != null)
	{
		var parameterNodes = parameterProperties.childNodes;
		if (parameterNodes != null)
		{
			for(var index = 0; index < parameterNodes.length; ++index)
			{
				var parameterNode = parameterNodes[index];

				var sName = "";
				if (parameterNode.getAttribute("name") != null)
				{
					sName = parameterNode.getAttribute("name");
				}

				if (sName == parameterName)
				{
					return parameterNode.getAttribute("propertyToPass");
				}
			}
		}
	}

	return "";
}

AuthoredDrillAction.prototype.getTargetReportRequestString = function(drillTargetNode)
{
	var sRequestString = "";

	var sBookmarkRef = drillTargetNode.getAttribute("bookmarkRef");
	var sTargetPath = drillTargetNode.getAttribute("path");
	var sShowInNewWindow = drillTargetNode.getAttribute("showInNewWindow");

	if((sBookmarkRef != null && sBookmarkRef != "") && (sTargetPath == null || sTargetPath == ""))
	{
		sRequestString += "document.location=\"#";
		sRequestString += sBookmarkRef;
		sRequestString += "\";";
	}
	else
	{
		sRequestString += "doSingleDrill(";

		if(sShowInNewWindow == "true")
		{
			sRequestString += "\"_blank\",";
		}
		else
		{
			sRequestString += "\"\",";
		}

		sRequestString += "[[\"obj\",\"";
		sRequestString += encodeURIComponent(sTargetPath);
		sRequestString += "\"]";

		var drillParameterNodes = XMLHelper_FindChildrenByTagName(drillTargetNode, "drillParameter", false);
		for(var index = 0; index < drillParameterNodes.length; ++index)
		{
			var drillParameterNode = drillParameterNodes[index];
			var sValue = drillParameterNode.getAttribute("value");
			var sName = drillParameterNode.getAttribute("name");

			if(sValue != null && sValue != "")
			{
				sRequestString += ", new Array(\"p_" + sName + "\",\"" + encodeURIComponent(this.buildSelectionChoicesSpecification(drillParameterNode)) + "\")";
			}

			var sNil = drillParameterNode.getAttribute("nil");
			if(sNil != null && sNil != "")
			{
				sRequestString += "\", new Array(\"p_" + sName + "\",\"" + encodeURIComponent(this.buildSelectionChoicesNilSpecification()) + "\")";
			}
		}

		sRequestString += "],";

		var sMethod = drillTargetNode.getAttribute("method");
		sRequestString += "\"" + encodeURIComponent(sMethod) + "\",";

		var sOutputFormat = drillTargetNode.getAttribute("outputFormat");
		sRequestString += "\"" + encodeURIComponent(sOutputFormat) + "\",";

		var sOutputLocale = drillTargetNode.getAttribute("outputLocale");
		sRequestString += "\"" + encodeURIComponent(sOutputLocale) + "\",";

		sRequestString += "\"" + encodeURIComponent(sBookmarkRef) + "\",";

		var sSourceContext = XMLBuilderSerializeNode(XMLHelper_FindChildByTagName(drillTargetNode, "parameters", false));
		sRequestString += "\"" + encodeURIComponent(sSourceContext) + "\",";

		var sObjectPaths = XMLBuilderSerializeNode(XMLHelper_FindChildByTagName(drillTargetNode, "objectPaths", false));
		sRequestString += "\"" + encodeURIComponent(sObjectPaths) + "\",";

		sRequestString += "\"" + encodeURIComponent(this.getCognosViewer().getId()) + "\",";

		var sPrompt = drillTargetNode.getAttribute("prompt");
		sRequestString += "\"" + encodeURIComponent(sPrompt) + "\",";

		var dynamicDrill = drillTargetNode.getAttribute("dynamicDrill");
		sRequestString += " " + encodeURIComponent(dynamicDrill);

		sRequestString += ");";
	}

	return sRequestString;
};

AuthoredDrillAction.prototype.getTargetReportIconPath = function(drillTarget)
{
	var sIconPath = "";
	var sBookmarkRef = drillTarget.getAttribute("bookmarkRef");
	var drillParameterNode = XMLHelper_FindChildByTagName(drillTarget, "drillParameter", false);
	if((sBookmarkRef != null && sBookmarkRef != "") && drillParameterNode == null)
	{
		sIconPath = "/common/images/spacer.gif";
	}
	else
	{
		var sMethod = drillTarget.getAttribute("method");
		switch(sMethod)
		{
			case "editAnalysis":
			{
				sIconPath = "/ps/portal/images/icon_ps_analysis.gif";
				break;
			}
			case "editQuery":
			{
				sIconPath = "/ps/portal/images/icon_qs_query.gif";
				break;
			}
			case "execute":
			{
				sIconPath = "/ps/portal/images/action_run.gif";
				break;
			}
			case "view":
			{
				var sOutputFormat = drillTarget.getAttribute("outputFormat");

				switch(sOutputFormat)
				{
					case "HTML":
					case "XHTML":
					case "HTMLFragment":
					{
						sIconPath = "/ps/portal/images/icon_result_html.gif";
						break;
					}
					case "PDF":
					{
						sIconPath = "/ps/portal/images/icon_result_pdf.gif";
						break;
					}
					case "XML":
					{
						sIconPath = "/ps/portal/images/icon_result_xml.gif";
						break;
					}
					case "CSV":
					{
						sIconPath = "/ps/portal/images/icon_result_csv.gif";
						break;
					}
					case "XLS":
					{
						sIconPath = "/ps/portal/images/icon_result_excel.gif";
						break;
					}
					case "SingleXLS":
					{
						sIconPath = "/ps/portal/images/icon_result_excel_single.gif";
						break;
					}
					case "XLWA":
					{
						sIconPath = "/ps/portal/images/icon_result_excel_web_arch.gif";
						break;
					}
					default:
					{
						sIconPath = "/common/images/spacer.gif";
					}
				}
				break;
			}
			default:
			{
				sIconPath = "/common/images/spacer.gif";
			}
		}
	}

	return this.getCognosViewer().getWebContentRoot() + sIconPath;
};


AuthoredDrillAction.prototype.getAuthoredDrillThroughContext = function(sAuthoredDrillThroughTargets, drillTargetSpecifications)
{
	var drillThroughTargetStr = "";

	// validate the incoming arguments
	if(typeof sAuthoredDrillThroughTargets != "string" || typeof drillTargetSpecifications != "object")
	{
		return null;
	}

	// parse the xml string and validate it
	var xmlParsedDrillTargets = XMLBuilderLoadXMLFromString(sAuthoredDrillThroughTargets);
	if(xmlParsedDrillTargets == null || xmlParsedDrillTargets.firstChild == null)
	{
		return null;
	}

	// validate the root node
	var rootNode = XMLHelper_GetFirstChildElement( xmlParsedDrillTargets );
	if(XMLHelper_GetLocalName(rootNode) != "AuthoredDrillTargets")
	{
		return null;
	}

	// validate the rvDrillTargets node
	var rvDrillTargetNodes = XMLHelper_GetFirstChildElement( rootNode );
	if(XMLHelper_GetLocalName(rvDrillTargetNodes) != "rvDrillTargets")
	{
		return null;
	}

	// validate the drillTargets node
	var drillTargets = rvDrillTargetNodes.childNodes;
	if(drillTargets === null || drillTargets.length === 0)
	{
		return null;
	}

	var rvDrillTargetsElement = self.XMLBuilderCreateXMLDocument("rvDrillTargets");

	for(var drillTargetIdx = 0; drillTargetIdx < drillTargets.length; ++drillTargetIdx)
	{
		if(typeof drillTargets[drillTargetIdx].getAttribute == "undefined")
		{
			continue;
		}

		var drillTargetElement = rvDrillTargetsElement.createElement("drillTarget");
		rvDrillTargetsElement.documentElement.appendChild(drillTargetElement);

		var bookmarkRef = drillTargets[drillTargetIdx].getAttribute("bookmarkRef");
		if(bookmarkRef === null)
		{
			drillTargetElement.setAttribute("bookmarkRef", "");
		}
		else
		{
			drillTargetElement.setAttribute("bookmarkRef", bookmarkRef);
		}

		var drillTargetRefIdx = drillTargets[drillTargetIdx].getAttribute("drillIdx");
		if(drillTargetRefIdx == null)
		{
			continue;
		}

		if(drillTargetRefIdx >= drillTargetSpecifications.length)
		{
			continue;
		}

		var drillTargetRef = drillTargetSpecifications[drillTargetRefIdx];
		if(typeof drillTargetRef != "object")
		{
			continue;
		}

		drillTargetElement.setAttribute("outputFormat", drillTargetRef.getOutputFormat());
		drillTargetElement.setAttribute("outputLocale", drillTargetRef.getOutputLocale());
		drillTargetElement.setAttribute("prompt", drillTargetRef.getPrompt());
		drillTargetElement.setAttribute("dynamicDrill", drillTargetRef.isDynamicDrillThrough() ? "true" : "false");

		var useLabel = drillTargets[drillTargetIdx].getAttribute("label");
		if(useLabel === null || useLabel === "")
		{
			useLabel = drillTargetRef.getLabel();
		}

		drillTargetElement.setAttribute("label", useLabel);
		drillTargetElement.setAttribute("path", drillTargetRef.getPath());
		drillTargetElement.setAttribute("showInNewWindow", drillTargetRef.getShowInNewWindow());

		drillTargetElement.setAttribute("method", drillTargetRef.getMethod());

		var currentRvDrillTargetNode = rvDrillTargetNodes;

		var oParameterProperties = "";
		if (typeof drillTargetRef.getParameterProperties() != "undefined" && drillTargetRef.getParameterProperties() != null && drillTargetRef.getParameterProperties() != "")
		{
			oParameterProperties = XMLHelper_GetFirstChildElement(XMLBuilderLoadXMLFromString(drillTargetRef.getParameterProperties()));
		}

		while(currentRvDrillTargetNode)
		{
			var drillParameters = currentRvDrillTargetNode.childNodes[drillTargetIdx].childNodes;
			for(var drillParamIdx = 0; drillParamIdx < drillParameters.length; ++drillParamIdx)
			{
				var drillParameterElement = drillParameters[drillParamIdx].cloneNode(true);
				if (oParameterProperties)
				{
					var propertyToPass = this.getPropertyToPass(drillParameterElement.getAttribute("name"), oParameterProperties);
					if (propertyToPass != null && propertyToPass != "")
					{
						drillParameterElement.setAttribute("propertyToPass", propertyToPass);
					}
				}
				drillTargetElement.appendChild(drillParameterElement);
			}

			currentRvDrillTargetNode = currentRvDrillTargetNode.nextSibling
		}

		var rootOpenTag = '<root xmlns:bus="http://developer.cognos.com/schemas/bibus/3/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
		var rootCloseTag = '</root>';

		var drillTargetParametersString = rootOpenTag + drillTargetRef.getParameters() + rootCloseTag;
		var drillTargetParametersXML = XMLBuilderLoadXMLFromString(drillTargetParametersString);

		var oChild = XMLHelper_GetFirstChildElement( XMLHelper_GetFirstChildElement( drillTargetParametersXML ) );
		if (oChild)
		{
			drillTargetElement.appendChild(oChild.cloneNode(true));
		}

		var drillTargetObjectPathsString = rootOpenTag + drillTargetRef.getObjectPaths() + rootCloseTag;
		var drillTargetObjectPathsXML = XMLBuilderLoadXMLFromString(drillTargetObjectPathsString);

		oChild = XMLHelper_GetFirstChildElement( XMLHelper_GetFirstChildElement( drillTargetObjectPathsXML ) );
		if (oChild)
		{
			drillTargetElement.appendChild(oChild.cloneNode(true));
		}

	}

	return XMLHelper_GetFirstChildElement(rvDrillTargetsElement);

};



