/*
 *+------------------------------------------------------------------------+
 *| 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.
 *|
 *+------------------------------------------------------------------------+
 */

/**
CDrillManager -- shared class between Query Studio and Report Viewer, which handles drill up/down and drill through
*/

function CtxArrayPlaceHolder(){}

var self = window;
// CDrillManager constructor
function CDrillManager(oCV)
{
	this.m_drawDrillTargets = false;

	this.setCV(oCV);
}

CDrillManager.prototype = new CViewerHelper();

CDrillManager.prototype.getSelectionController = function()
{
	var selectionController;
	try
	{
		selectionController = eval("oCVSC" + this.getCV().getId());
	}
	catch(e)
	{
		selectionController = null;
	}

	return selectionController;
}

CDrillManager.prototype.getSelectedObject = function()
{
	var selectionController = this.getSelectionController();
	if(selectionController == null)
	{
		return null;
	}

	var SelObj = null;
	var selectionList = selectionController.getSelections();

	var chartArea = selectionController.getSelectedChartArea();
	if(chartArea != null)
	{
		SelObj = selectionController.getSelectionObjectFactory().getSelectionChartObject(chartArea);
	}
	else if(selectionList.length == 1)
	{
		SelObj = selectionList[0];
	}

	return SelObj;
}


/***************************************************************************************************

COMMON METHODS BETWEEN REPORT VIEWER AND QUERY STUDIO

****************************************************************************************************/

CDrillManager.prototype.canDrillUp = function()
{
	if(this.getDrillOption('drillUp') == true && this.hasMuns()) {
		return true;
	}

	return false;
}

CDrillManager.prototype.canDrillDown = function()
{
	if(this.getDrillOption('drillDown') == true)
	{
		return true;
	}

	return false;
}

CDrillManager.prototype.hasMuns = function(selectionObj)
{

	// if no selection object is passed in, get the current selected object from the selection controller
	if(typeof selectionObj == "undefined") {
		selectionObj = this.getSelectedObject();
	}

	if(selectionObj == null) {
		return false;
	}

	var munArray = selectionObj.getMuns();

	var muns='';
	for(var munIdx = 0; munIdx < munArray.length && muns == ''; ++munIdx)
	{
		if(typeof munArray[munIdx][0] != 'undefined') {
			muns += munArray[munIdx][0];
		}
	}

	return (muns != '');
}

CDrillManager.prototype.getRefQuery = function()
{

	var refQuery = "";

	var selectionObj = this.getSelectedObject();
	if(selectionObj == null) {
		return "";
	}

	refQueries = selectionObj.getRefQueries();
	if(refQueries.length != 0) {
		refQuery = refQueries[0][0];
	}

	return refQuery;
}

CDrillManager.prototype.isIsolated = function()
{
	var selectionController = this.getSelectionController();
	if(selectionController == null || selectionController.getDrillUpDownEnabled() == false)
	{
		return false;
	}

	var selectionObj = this.getSelectedObject();
	if(selectionObj == null)
	{
		return false;
	}

	if(selectionObj instanceof CSelectionChartObject && selectionController != null)
	{
		var chartArea = selectionController.getSelectedChartArea();
		if(chartArea != null)
		{
			var isolated = chartArea.getAttribute("isolated");
			if(typeof isolated != "undefined" && isolated != null && isolated == "true")
			{
				return true;
			}
		}
	}
	else
	{
		var cellRef = selectionObj.getCellRef();
		if(typeof cellRef == "object" && cellRef != null)
		{
			var spanElement = cellRef.getElementsByTagName("span");
			if(spanElement != null && typeof spanElement != "undefined" && spanElement.length > 0)
			{
				var sIsolated = spanElement[0].getAttribute("isolated");
				if(sIsolated != null && sIsolated != "undefined" && sIsolated == "true")
				{
					return true;
				}
			}
		}
	}

	return false;
}

CDrillManager.prototype.getDrillOption = function(drillOption)
{
	var selectionController = this.getSelectionController();
	if(selectionController == null || selectionController.getDrillUpDownEnabled() == false || typeof drillOption == "undefined")
	{
		return false;
	}

	var selectionObj = this.getSelectedObject();
	if(selectionObj == null)
	{
		return false;
	}

	if (this.isIsolated())
	{
		if (drillOption == "drillDown")
		{
			return false;
		}
		else if (drillOption == "drillUp")
		{
			return true;
		}
	}

	if(drillOption == "drillDown")
	{
		if(selectionObj instanceof CSelectionChartObject && selectionController != null)
		{
			var chartArea = selectionController.getSelectedChartArea();
			if(chartArea != null)
			{
				var bIsChartTitle = chartArea.getAttribute("isChartTitle");
				if (typeof bIsChartTitle != "undefined" && bIsChartTitle != null && bIsChartTitle == "true")
				{
					return false;
				}
			}
		}
	}

	var drillOptions = selectionObj.getDrillOptions();

	for(var idx = 0; idx < drillOptions.length; ++idx)
	{
		var currentDrillOption = drillOptions[idx][0];
		if(currentDrillOption == "3" /*drill up and down*/)
		{
			return true;
		}
		else if(drillOption == "drillUp" && currentDrillOption == "1")
		{
			return true;
		}
		else if(drillOption == "drillDown" && currentDrillOption == "2")
		{
			return true;
		}
	}

	// if the drill option flag is not present, the user cannot drill on this cell
	return false;
}

CDrillManager.prototype.canDrillThrough = function()
{
	var selectionController = this.getSelectionController();
	if(selectionController == null || selectionController.getModelDrillThroughEnabled() == false)
	{
		return false;
	}

	var selectionList = selectionController.getAllSelectedObjects();
	if(selectionList.length <= 0) {
		return false;
	}

	return true;
}

CDrillManager.prototype.singleClickDrillEvent = function(evt, app)
{
	var selectionController = this.getSelectionController();

	if (selectionController != null)
	{
		if(this.getCV().bCanUseCognosViewerSelection == true)
		{
			selectionController.pageClicked(evt);
		}
	}

	var node = null;
	if (document.all)
	{
		node = evt.srcElement;
	}
	else
	{
		node = evt.originalTarget;
	}

	try
	{
		if(node.className && node.className.indexOf("dl") == 0)
		{
			if(this.canDrillDown())
			{
				return this.singleClickDrillDown(evt, app);
			}
			else if(this.canDrillUp())
			{
				return this.singleClickDrillUp(evt, app);
			}
		}
	}
	catch (e)
	{
	}

	if(app == 'RV')
	{
		return this.getDrillThroughParameters('execute', evt);
	}

}

CDrillManager.prototype.singleClickDrillDown = function(evt, app /*either 'qs' or 'rv'*/)
{
	if(app == 'QS') {
		this.qsDrillDown();
	}
	else {
		this.rvDrillDown();
	}
}

CDrillManager.prototype.singleClickDrillUp = function(evt, app /*either 'QS' or 'RV'*/)
{
	if(app == 'QS') {
		this.qsDrillUp();
	}
	else {
		this.rvDrillUp();
	}
}

CDrillManager.prototype.getDrillParameters = function(drillType, includeMetadata)
{
	var drillParamsArray = [];

	var selectionObj = this.getSelectedObject();
	if(selectionObj == null) {
		return drillParamsArray; // return an empty array
	}

	if(typeof includeMetadata == "undefined")
	{
		includeMetadata = true;
	}

	var dataItemsArray = selectionObj.getDataItems();
	var munArray = selectionObj.getMuns();
	var lunArray = selectionObj.getDimensionalItems("lun");
	var hunArray = selectionObj.getDimensionalItems("hun");
	var drillOptions = selectionObj.getDrillOptions();

	if(typeof dataItemsArray == "undefined" || typeof munArray == "undefined" || typeof drillOptions == "undefined" || munArray == null || dataItemsArray == null || drillOptions == null) {
		return drillParamsArray; // return an empty array
	}

	if(munArray.length != dataItemsArray.length) {
		return drillParamsArray; // return an empty array
	}

	var num_of_items = munArray.length;
	for(var item_idx = 0; item_idx < num_of_items; ++item_idx) {
		if(dataItemsArray[item_idx].length != 0) {
			if(this.getDrillOption(drillType))
			{
				if(munArray[item_idx][0] == "" || drillParamsArray.toString().indexOf(munArray[item_idx][0],0) == -1) {
					drillParamsArray[drillParamsArray.length] = dataItemsArray[item_idx][0];
					drillParamsArray[drillParamsArray.length] = munArray[item_idx][0];

					if(includeMetadata === true)
					{
						drillParamsArray[drillParamsArray.length] = lunArray[item_idx][0];
						drillParamsArray[drillParamsArray.length] = hunArray[item_idx][0];
					}
				}
			}
		}
	}

	return drillParamsArray;
}

CDrillManager.prototype.getModelDrillThroughContext = function(XMLBuilderLocation)
{
	var modelDrillContext='';

	if(this.canDrillThrough() === true)
	{
		if(typeof gUseNewSelectionContext == "undefined")
		{
			var modelPath = "";
			if(typeof getConfigFrame != "undefined")
			{
				modelPath = decodeURIComponent(getConfigFrame().cfgGet("PackageBase"));
			}
			else if(typeof document.forms["formWarpRequest" + this.getCVId()]["modelPath"] != "undefined")
			{
				modelPath = document.forms["formWarpRequest" + this.getCVId()]["modelPath"].value;
			}

			modelDrillContext = getViewerSelectionContext(this.getSelectionController(), new CSelectionContext(modelPath));
		}
		else
		{
			var parameterValues = new CParameterValues();

			var selectionController = this.getSelectionController();

			if(selectionController) {

				var selectionList = selectionController.getAllSelectedObjects();

				for(var sel_idx = 0; sel_idx < selectionList.length; ++ sel_idx) {
					var selectionObj = selectionList[sel_idx];

					var munArray = selectionObj.getMuns();
					var metaDataItems = selectionObj.getMetadataItems();
					var useValues = selectionObj.getUseValues();
					for(var context_idx = 0; context_idx < metaDataItems.length; ++context_idx) {
						for(var idx = 0; idx < metaDataItems[context_idx].length; ++idx) {

							if(metaDataItems[context_idx][idx] == null || metaDataItems[context_idx][idx] == "") {
								continue;
							}

							var name = metaDataItems[context_idx][idx];
							var useValue;

							// if we have a mun, set it as the use value, otherwise use the useValue we have stored
							if(munArray[context_idx][idx] != null && munArray[context_idx][idx] != "") {
								useValue = munArray[context_idx][idx];
							}
							else {
								useValue = useValues[context_idx][idx];
							}

							// set the display value to what we have stored as the use value
							var displayValue = useValues[context_idx][idx];
							parameterValues.addSimpleParmValueItem(name, useValue, displayValue, "true");
						}
					}
				}
			}

			var contextElement = XMLBuilderLocation.XMLBuilderCreateXMLDocument("context");
			modelDrillContext = parameterValues.generateXML(XMLBuilderLocation, contextElement);
		}

	}

	return modelDrillContext;
}

/***************************************************************************************************

REPORT VIEWER SPECIFIC METHODS

****************************************************************************************************/

CDrillManager.prototype.rvDrillUp = function()
{
	if(this.canDrillUp() == false) {
		// add error handling
		return;
	}

	var drillParams = this.rvBuildXMLDrillParameters("drillUp");
	var refQuery = this.getRefQuery();

	if (this.getCV().envParams["cv.containerApp"] == "AA")
	{
		this.getCV().m_viewerFragment.raiseAADrillUpEvent();
	}

	this.rvSubmitDrillRequest(drillParams, "up", refQuery);
}

CDrillManager.prototype.rvDrillDown = function()
{
	if(this.canDrillDown() == false)
	{
		// add error handling
		return;
	}
	var drillParams = this.rvBuildXMLDrillParameters("drillDown");
	var refQuery = this.getRefQuery();

	if (this.getCV().envParams["cv.id"] == "AA")
	{
		this.getCV().m_viewerFragment.raiseAADrillDownEvent();
	}

	this.rvSubmitDrillRequest(drillParams, "down", refQuery);
}

CDrillManager.prototype.rvBuildXMLDrillParameters = function(drillType)
{
	var drillParamsArray = this.getDrillParameters(drillType, true);
	if(drillParamsArray.length == 0)
	{
		// handle the error
		return drillParams;
	}

	return this.buildDrillParametersSpecification(drillParamsArray);
}

CDrillManager.prototype.buildDrillParametersSpecification = function(drillParamsArray)
{
	var drillParams = '<DrillParameters>';

	var idx = 0;
	while(idx < drillParamsArray.length) {
		drillParams += '<DrillGroup>';
		drillParams += '<DataItem>';
		drillParams += sXmlEncode(drillParamsArray[idx++]);
		drillParams += '</DataItem>';
		drillParams += '<MUN>';
		drillParams += sXmlEncode(drillParamsArray[idx++]);
		drillParams += '</MUN>';
		drillParams += '<LUN>';
		drillParams += sXmlEncode(drillParamsArray[idx++]);
		drillParams += '</LUN>';
		drillParams += '<HUN>';
		drillParams += sXmlEncode(drillParamsArray[idx++]);
		drillParams += '</HUN>';
		drillParams += '</DrillGroup>';
	}

	drillParams += '</DrillParameters>';

	return drillParams;

}

CDrillManager.prototype.rvSubmitDrillRequest = function(drillParams, drillOption, drillRefQuery)
{
	var oReq = new CCognosViewerRequest("drill");
	oReq.addOption("rv_drillOption", drillOption);
	oReq.addOption("rv_drillparams", drillParams);
	oReq.addOption("rv_drillRefQuery", drillRefQuery);
	this.getCV().sendRequest(oReq);
}

CDrillManager.prototype.getAuthoredDrillsForGotoPage = function()
{
	var sResult = "";
	// get the report authored drills
	var aReportAuthoredDrills = this.getAuthoredDrillThroughTargets();
	if(aReportAuthoredDrills.length > 0)
	{

		var sAuthoredDrillThroughTargets = "<AuthoredDrillTargets>";

		for(var iIndex = 0; iIndex < aReportAuthoredDrills.length; ++iIndex)
		{
			sAuthoredDrillThroughTargets +=  eval('"' + aReportAuthoredDrills[iIndex] + '"');
		}

		sAuthoredDrillThroughTargets += "</AuthoredDrillTargets>";

		var authoredDrillAction = new AuthoredDrillAction();
		authoredDrillAction.setCognosViewer(this.getCV());

		var drillTargetSpecifications = this.getCV().rvMainWnd.getDrillTargets();
		if(drillTargetSpecifications.length > 0)
		{
			var rvDrillTargetsNode = authoredDrillAction.getAuthoredDrillThroughContext(sAuthoredDrillThroughTargets, drillTargetSpecifications);
			sResult = XMLBuilderSerializeNode(rvDrillTargetsNode);
		}
	}

	return sResult;
};

CDrillManager.prototype.launchGoToPage = function(drillTargets, bDirectLaunch)
{
	var selectionController = this.getSelectionController();
	if((selectionController != null && selectionController.getModelDrillThroughEnabled() == true) || (typeof drillTargets != "undefined" && drillTargets != null && drillTargets != ""))
	{
		// get the authored drills
		var sAuthoredDrills = this.getAuthoredDrillsForGotoPage();

		// build up the model drill context
		var modelDrillContext = this.getModelDrillThroughContext(self);

		var form = document.getElementById("drillForm");
		if(form != null) {
			document.body.removeChild(form);
		}

		form = document.createElement("form");

		form.setAttribute("id", "drillForm");
		form.setAttribute("name", "drillForm");
		form.setAttribute("target", document.forms["formWarpRequest" + this.getCVId()].getAttribute("target"));
		form.setAttribute("method", "post");
		form.setAttribute("action", document.forms["formWarpRequest" + this.getCVId()].getAttribute("action"));
		form.style.display = "none";

		document.body.appendChild(form);

		if(typeof document.forms["formWarpRequest" + this.getCVId()]["modelPath"] != "undefined")
		{
			form.appendChild(createHiddenFormField("modelPath", document.forms["formWarpRequest" + this.getCVId()]["modelPath"].value));
		}

		if(typeof document.forms["formWarpRequest" + this.getCVId()]["ui.object"] != "undefined" && document.forms["formWarpRequest" + this.getCVId()]["ui.object"].value != "")
		{
			form.appendChild(createFormField("drillSource", document.forms["formWarpRequest" + this.getCVId()]["ui.object"].value));
		}
		else if(typeof this.getCV().envParams["ui.spec"] != "undefined")
		{
			form.appendChild(createFormField("sourceSpecification", this.getCV().envParams["ui.spec"]));
		}

		if(sAuthoredDrills != "")
		{
			form.appendChild(createHiddenFormField("m", "portal/drillthrough.xts"));
			form.appendChild(createFormField("invokeGotoPage", "true"));
			form.appendChild(createFormField("m", "portal/drillthrough.xts"));
			form.appendChild(createFormField("modelDrillEnabled", selectionController.getModelDrillThroughEnabled()));

			if(typeof gUseNewSelectionContext == "undefined")
			{
				form.appendChild(createFormField("newSelectionContext", "true"));
			}
		}
		else
		{
			if(typeof gUseNewSelectionContext == "undefined")
			{
				form.appendChild(createHiddenFormField("m", "portal/goto2.xts"));
			}
			else
			{
				form.appendChild(createHiddenFormField("m", "portal/goto.xts"));
			}
		}

		form.appendChild(createHiddenFormField("b_action", "xts.run"));
		form.appendChild(createHiddenFormField("drillTargets", sAuthoredDrills));

		if(typeof gUseNewSelectionContext == "undefined")
		{
			form.appendChild(createHiddenFormField("drillContext", modelDrillContext));
		}
		else
		{
			form.appendChild(createHiddenFormField("modeledDrillthru", modelDrillContext));
		}

		form.appendChild(createHiddenFormField("errURL", "javascript:window.close();"));


		if(typeof bDirectLaunch != "undefined" && bDirectLaunch == true)
		{
			form.appendChild(this.createFormField("directLaunch", "true"));
		}

		var routingServerGroup = "";
		if(this.getCV().envParams["ui.routingServerGroup"])
		{
			routingServerGroup = this.getCV().envParams["ui.routingServerGroup"];
		}
		form.appendChild(createHiddenFormField("ui.routingServerGroup", routingServerGroup));


		if(this.getCV().getExecutionParameters() != '') {
			form.appendChild(createHiddenFormField("encExecutionParameters", this.getCV().getExecutionParameters()));
		}

		if(document.forms["formWarpRequest" + this.getCVId()].lang && document.forms["formWarpRequest" + this.getCVId()].lang.value != "")
		{
			form.appendChild(createHiddenFormField("lang", document.forms["formWarpRequest" + this.getCVId()].lang.value));
		}

		var target = "winNAT_" + ( new Date() ).getTime();
		var sPath = this.getCV().getWebContentRoot() + "/rv/blankDrillWin.html?cv.id=" + this.getCVId();
		var __goto_win = window.open(sPath, target, "toolbar,location,status,menubar,resizable,scrollbars=1");
		form.target = target;
	}
}

CDrillManager.prototype.buildSearchPageXML = function(XMLBuilderLocation, pkg, model, ctxArr, defMeasArr, dataSpecification, filtArr)
{
	var cognosSearchElement = null;
	if (typeof XMLBuilderLocation.XMLElement == "function")
	{
		cognosSearchElement = XMLBuilderLocation.XMLBuilderCreateXMLDocument("cognosSearch");
		XMLBuilderLocation.XMLBuilderSetAttributeNodeNS(cognosSearchElement.documentElement, "xmlns:cs", "http://developer.cognos.com/schemas/cs/1/");
		var packageElement = cognosSearchElement.createElement("package");
		if (typeof pkg == "string" && pkg !== "")
		{
			packageElement.appendChild(cognosSearchElement.createTextNode(pkg));
		}
		cognosSearchElement.documentElement.appendChild(packageElement);
		var modelElement = cognosSearchElement.createElement("model");
		if (typeof model == "string" && model !== "")
		{
			modelElement.appendChild(cognosSearchElement.createTextNode(model));
		}
		cognosSearchElement.documentElement.appendChild(modelElement);
		var selectedContextElement = cognosSearchElement.createElement("selectedContext");
		XMLBuilderLocation.XMLBuilderSetAttributeNodeNS(selectedContextElement, "xmlns:xs", "http://www.w3.org/2001/XMLSchema");
		XMLBuilderLocation.XMLBuilderSetAttributeNodeNS(selectedContextElement, "xmlns:bus", "http://developer.cognos.com/schemas/bibus/3/");
		XMLBuilderLocation.XMLBuilderSetAttributeNodeNS(selectedContextElement, "SOAP-ENC:arrayType", "bus:parameterValue[]", "http://schemas.xmlsoap.org/soap/encoding/");
		XMLBuilderLocation.XMLBuilderSetAttributeNodeNS(selectedContextElement, "xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
		XMLBuilderLocation.XMLBuilderSetAttributeNodeNS(selectedContextElement, "xsi:type", "SOAP-ENC:Array", "http://www.w3.org/2001/XMLSchema-instance");
		cognosSearchElement.documentElement.appendChild(selectedContextElement);
		for (var idxCtx in ctxArr)
		{
			var itemElement = cognosSearchElement.createElement("item");
			XMLBuilderLocation.XMLBuilderSetAttributeNodeNS(itemElement, "xsi:type", "bus:parameterValue", "http://www.w3.org/2001/XMLSchema-instance");
			var busNameElement = XMLBuilderLocation.XMLBuilderCreateElementNS("http://developer.cognos.com/schemas/bibus/3/", "bus:name", cognosSearchElement);
			XMLBuilderLocation.XMLBuilderSetAttributeNodeNS(busNameElement, "xsi:type", "xs:string", "http://www.w3.org/2001/XMLSchema-instance");
			busNameElement.appendChild(cognosSearchElement.createTextNode(ctxArr[idxCtx].name));
			var busValueElement = XMLBuilderLocation.XMLBuilderCreateElementNS("http://developer.cognos.com/schemas/bibus/3/", "bus:value", cognosSearchElement);
			XMLBuilderLocation.XMLBuilderSetAttributeNodeNS(busValueElement, "xsi:type", "SOAP-ENC:Array", "http://www.w3.org/2001/XMLSchema-instance");
			XMLBuilderLocation.XMLBuilderSetAttributeNodeNS(busValueElement, "SOAP-ENC:arrayType", "bus:parmValueItem[]", "http://schemas.xmlsoap.org/soap/encoding/");
			for (var j = 0; j < ctxArr[idxCtx].values.length; j++)
			{
				var itemChildElement = cognosSearchElement.createElement("item");
				XMLBuilderLocation.XMLBuilderSetAttributeNodeNS(itemChildElement, "xsi:type", "bus:simpleParmValueItem", "http://www.w3.org/2001/XMLSchema-instance");
				var busUseElement = XMLBuilderLocation.XMLBuilderCreateElementNS("http://developer.cognos.com/schemas/bibus/3/", "bus:use", cognosSearchElement);
				XMLBuilderLocation.XMLBuilderSetAttributeNodeNS(busUseElement, "xsi:type", "xs:string", "http://www.w3.org/2001/XMLSchema-instance");
				busUseElement.appendChild(cognosSearchElement.createTextNode(ctxArr[idxCtx].values[j][0]));
				var busDisplayElement = XMLBuilderLocation.XMLBuilderCreateElementNS("http://developer.cognos.com/schemas/bibus/3/", "bus:display", cognosSearchElement);
				XMLBuilderLocation.XMLBuilderSetAttributeNodeNS(busDisplayElement, "xsi:type", "xs:string", "http://www.w3.org/2001/XMLSchema-instance");
				busDisplayElement.appendChild(cognosSearchElement.createTextNode(ctxArr[idxCtx].values[j][1]));
				itemChildElement.appendChild(busUseElement);
				itemChildElement.appendChild(busDisplayElement);
				busValueElement.appendChild(itemChildElement);
			}
			itemElement.appendChild(busNameElement);
			itemElement.appendChild(busValueElement);
			selectedContextElement.appendChild(itemElement);
		}

		var defaultMeasureElement = cognosSearchElement.createElement("defaultMeasure");
		cognosSearchElement.documentElement.appendChild(defaultMeasureElement);

		dataSpecification.buildXML(XMLBuilderLocation, cognosSearchElement, "data");

		var filterElement = cognosSearchElement.createElement("filter");
		cognosSearchElement.documentElement.appendChild(filterElement);
	}

	return cognosSearchElement;
}

CDrillManager.prototype.openSearchPage = function(objPath, sourceContext)
{
	// build up the model drill context
	var modelDrillContext = this.getModelDrillThroughContext(self);

	var searchForm = document.getElementById("searchPage");
	if(searchForm != null) {
		document.body.removeChild(searchForm);
	}

	searchForm = document.createElement("form");

	searchForm.setAttribute("id", "searchPage");
	searchForm.setAttribute("name", "searchPage");
	searchForm.setAttribute("method", "post");
	searchForm.setAttribute("target", searchForm.name);
	searchForm.setAttribute("action", this.getCV().getGateway() + "/gosearch");
	searchForm.style.display = "none";

	document.body.appendChild(searchForm);

	searchForm.appendChild(createHiddenFormField("csn.action", "search"));
	searchForm.appendChild(createHiddenFormField("csn.drill", sourceContext));

	var __search_win = window.open("", searchForm.name, "directories=no,location=no,status=no,toolbar=no,resizable=yes,scrollbars=yes,top=100,left=100,height=480,width=640" );
	__search_win.focus();
	searchForm.submit();
}

CDrillManager.prototype.launchSearchPage = function()
{
	var ctxArr = this.determineSelectionsForSearchPage(this.getSelectionController());
	var dataSpecification = this.getSearchContextDataSpecfication(this.getSelectionController());
	var spXML = this.buildSearchPageXML(self, document.forms["formWarpRequest" + this.getCVId()].packageBase.value, document.forms["formWarpRequest" + this.getCVId()].modelPath.value, ctxArr, [], dataSpecification, []);
	this.openSearchPage(document.forms["formWarpRequest" + this.getCVId()].packageBase.value, XMLBuilderSerializeNode(spXML));
}

/***************************************************************************************************

QUERY STUDIO SPECIFIC METHODS

****************************************************************************************************/

CDrillManager.prototype.qsDrillDown = function()
{
	if(!this.canDrillDown()) {
		// throw up a a generic error page (for now)
		getConfigFrame().dlgGenericSelectionMessage(false);
		return;
	}

	// build the drill down command
	var drillCommand = 'DD:';
	this.qsSendDrillCommand(drillCommand);
}

CDrillManager.prototype.qsDrillUp = function()
{
	if(!this.canDrillUp()) {
		// throw up a a generic error page (for now)
		getConfigFrame().dlgGenericSelectionMessage(false);
		return;
	}

	// build the drill up command
	var drillCommand = 'DU:';
	this.qsSendDrillCommand(drillCommand);
}

CDrillManager.prototype.qsSendDrillCommand = function(drillCommand)
{
	var drillType;
	if(drillCommand == "DU:") {
		drillType = "drillUp";
	}
	else {
		drillType = "drillDown";
	}

	var drillParamsArray = this.getDrillParameters(drillType, false);
	if(drillParamsArray.length == 0){
		// throw up a a generic error page (for now)
		getConfigFrame().dlgGenericSelectionMessage(false);
		return;
	}
	for(var idx = 0; idx < drillParamsArray.length; ++idx) {
		drillCommand += getConfigFrame().escapeParam(drillParamsArray[idx]);
		if(idx+1 < drillParamsArray.length) {
			drillCommand += ',';
		}
	}

	getConfigFrame().sendCmd(drillCommand, "", true);
}

CDrillManager.prototype.qsLaunchGoToPage = function(bDirectLaunch)
{
	var selectionController = this.getSelectionController();
	if(selectionController != null && selectionController.getModelDrillThroughEnabled() == true)
	{
		// build up the model drill context
		var modelDrillContext = this.getModelDrillThroughContext(cf);

		if(modelDrillContext=="") {
			// throw up a a generic error page (for now)
			getConfigFrame().dlgGenericSelectionMessage(false);
			return;
		}

		var gotoForm = document.getElementById("gotoPage");
		if(gotoForm != null) {
			document.body.removeChild(gotoForm);
		}

		gotoForm = document.createElement("form");

		gotoForm.setAttribute("id", "gotoPage");
		gotoForm.setAttribute("name", "gotoPage");
		gotoForm.setAttribute("method", "post");
		gotoForm.style.display = "none";

		document.body.appendChild(gotoForm);

		gotoForm.appendChild(this.createFormField("objpath", decodeURIComponent(getConfigFrame().cfgGet("PackageBase"))));

		if(typeof gUseNewSelectionContext == "undefined")
		{
			gotoForm.appendChild(this.createFormField("m", "portal/goto2.xts"));
		}
		else
		{
			gotoForm.appendChild(this.createFormField("m", "portal/goto.xts"));
		}

		gotoForm.appendChild(this.createFormField("b_action", "xts.run"));

		if(typeof gUseNewSelectionContext == "undefined")
		{
			gotoForm.appendChild(this.createFormField("drillContext", modelDrillContext));
		}
		else
		{
			gotoForm.appendChild(this.createFormField("modeledDrillthru", modelDrillContext));
		}

		if (typeof getConfigFrame().routingServerGroup != "undefined")
		{
			gotoForm.appendChild(this.createFormField("ui.routingServerGroup", getConfigFrame().routingServerGroup));
		}

		if(typeof bDirectLaunch != "undefined" && bDirectLaunch == true)
		{
			gotoForm.appendChild(this.createFormField("directLaunch", "true"));
		}

		var executionParameters = getConfigFrame().goApplicationManager.getReportManager().getParameterManager().getExecutionParameters();
		if (executionParameters)
		{
			gotoForm.appendChild(this.createFormField("encExecutionParameters", executionParameters));
		}

		var target = "winNAT_" + ( new Date() ).getTime();
		var sPath = this.getCV().getWebContentRoot() + "/rv/blankDrillWin.html?cv.id=" + this.getCVId();;
		var __goto_win = window.open(sPath, target, "toolbar,location,status,menubar,resizable,scrollbars=1");
		gotoForm.target = target;
	}
}

CDrillManager.prototype.qsLaunchSearchPage = function()
{
	var cf = getConfigFrame();
	var ctxArr = this.determineSelectionsForSearchPage(goWindowManager.getSelectionController());
	var dataSpecification = this.getSearchContextDataSpecfication(goWindowManager.getSelectionController());
	var pkgBase = decodeURIComponent(cf.cfgGet("PackageBase"));
	var spXML = this.buildSearchPageXML(cf, pkgBase, decodeURIComponent(cf.cfgGet("cmLastModel")), ctxArr, [], dataSpecification, []);
	this.openSearchPage(pkgBase, cf.XMLBuilderSerializeNode(spXML));
}

CDrillManager.prototype.determineSelectionsForSearchPage = function(selectionController)
{
	var ctxArr = new CtxArrayPlaceHolder();
	var allSelections = selectionController.getAllSelectedObjects();
	for (var i = 0; i < allSelections.length; i++)
	{
		var colName = allSelections[i].getColumnName();
		if (!this.containsByIndiceInArray(ctxArr, colName))
		{
			ctxArr[colName] = {};
			ctxArr[colName].name = colName;
			ctxArr[colName].values = [];
		}
		var idx0 = "";

		if (allSelections[i].getMuns() != null && allSelections[i].getMuns().length > 0)
		{
			idx0 = allSelections[i].getMuns()[0][0];
		}
		var idx1 = allSelections[i].getDisplayValues()[0];
		if (!(this.containsInArray(ctxArr[colName].values, 0, idx0) && this.containsInArray(ctxArr[colName].values, 1, idx1))) {
			ctxArr[colName].values[ctxArr[colName].values.length] = [idx0, idx1];
		}
	}
	return ctxArr;
}

CDrillManager.prototype.getSearchContextDataSpecfication = function(selectionController)
{
	var parameterValues = new CParameterValues();
	var dataManager = selectionController.getCCDManager();
	var contextData = dataManager.m_cd;

	for(ctxId in contextData)
	{
		var sUsage = dataManager.GetUsage(ctxId);
		if(sUsage != "2" /*2==MEASURE*/)
		{
			var sRefDataItem = dataManager.GetRDIValue(ctxId);
			var sUseValue = dataManager.GetDisplayValue(ctxId);
			parameterValues.addSimpleParmValueItem(sRefDataItem, sRefDataItem, sUseValue, "true");
		}
	}

	return parameterValues;
}

CDrillManager.prototype.containsByIndiceInArray = function(a, v)
{
	for (var i in a)
	{
		if (i == v) {
			return true;
		}
	}
	return false;
}

CDrillManager.prototype.containsInArray = function(a, idx, v)
{
	for (var i in a)
	{
		if (a[i][idx] == v) {
			return true;
		}
	}
	return false;
}

// temp function for now
CDrillManager.prototype.createFormField = function(name, value)
{
	var formField = document.createElement("input");
	formField.setAttribute("type", "hidden");
	formField.setAttribute("name", name);
	formField.setAttribute("value", value);
	return(formField);
}

/***************************************************************************************************

DRILL THROUGH METHODS

****************************************************************************************************/


CDrillManager.prototype.getAuthoredDrillThroughTargets = function()
{
	var aAuthoredDrillItems = new Array();
	var selectionController = this.getSelectionController();
	var oHtmlItem = null;

	if(selectionController != null)
	{
		if(selectionController.getSelectedColumnIds().length == 1)
		{
			var selections = selectionController.getSelections();
			var previousSelectionColumnRef = "";
			for(var selectionIndex = 0; selectionIndex < selections.length; ++selectionIndex)
			{
				var selectionObject = selections[selectionIndex];

				oHtmlItem = XMLHelper_GetFirstChildElement( selectionObject.getCellRef() );
				while( oHtmlItem )
				{
					if(oHtmlItem.getAttribute("dtTargets") != null)
					{
						aAuthoredDrillItems.push("<rvDrillTargets>" + oHtmlItem.getAttribute("dtTargets") + "</rvDrillTargets>");
						break;
					}

					oHtmlItem = XMLHelper_GetFirstChildElement( oHtmlItem );
				}
			}
		}
		else if(selectionController.getSelectedChartArea() != null)
		{
			oHtmlItem = selectionController.getSelectedChartArea();
			if(oHtmlItem.getAttribute("dtTargets") != null)
			{
				aAuthoredDrillItems.push("<rvDrillTargets>" + oHtmlItem.getAttribute("dtTargets") + "</rvDrillTargets>");
			}
		}
	}




	return aAuthoredDrillItems;
}

CDrillManager.prototype.getDrillThroughParameters = function(method, evt)
{
	if(typeof method == 'undefined')
	{
		method = 'query';
	}

	var aAuthoredDrillThroughTargets = new Array();

	if(typeof evt != "undefined")
	{
		var cellRef;
		if (document.all)
		{
			cellRef = evt.srcElement;
		} else
		{
			cellRef = evt.explicitOriginalTarget ? evt.explicitOriginalTarget : evt.originalTarget;
		}

		try
		{
			while(cellRef)
			{
				if(typeof cellRef.getAttribute != "undefined" && cellRef.getAttribute("dtTargets"))
				{
					aAuthoredDrillThroughTargets.push("<rvDrillTargets>" + cellRef.getAttribute("dtTargets") + "</rvDrillTargets>");
					break;
				}

				cellRef = cellRef.parentNode;
			}
		}
		catch(e)
		{
			// if an exception occurs, just eat it
		}
	}
	else
	{

		var oCV = this.getCV();
		var oDrillMgr = oCV.getDrillMgr();
		var selectionController = oDrillMgr.getSelectionController();

		if(selectionController != null)
		{
			var chartArea = selectionController.getSelectedChartArea();
			if(chartArea != null)
			{
				aAuthoredDrillThroughTargets.push("<rvDrillTargets>" + chartArea.getAttribute("dtTargets") + "</rvDrillTargets>");
			}
			else
			{
				aAuthoredDrillThroughTargets = this.getAuthoredDrillThroughTargets();
			}
		}
	}

	if(aAuthoredDrillThroughTargets.length > 0)
	{
		var sAuthoredDrillThroughTargets = "<AuthoredDrillTargets>";

		for(var iIndex = 0; iIndex < aAuthoredDrillThroughTargets.length; ++iIndex)
		{
			sAuthoredDrillThroughTargets +=  eval('"' + aAuthoredDrillThroughTargets[iIndex] + '"');
		}

		sAuthoredDrillThroughTargets += "</AuthoredDrillTargets>";

		var authoredDrillAction = new AuthoredDrillAction();
		authoredDrillAction.setCognosViewer(this.getCV());

		if(method == "query")
		{
			authoredDrillAction.populateContextMenu(sAuthoredDrillThroughTargets);
			this.showOtherMenuItems();
		}
		else
		{
			if (this.getCV().envParams["cv.id"] == "AA")
			{
				this.getCV().m_viewerFragment.raiseAuthoredDrillClickEvent();
			}
			else
			{
				authoredDrillAction.execute(sAuthoredDrillThroughTargets);
			}
		}
	}
	else if (method == 'query')
	{
		this.showOtherMenuItems();
	}
}

CDrillManager.prototype.executeAuthoredDrill = function(drillTargetsSpecification)
{
	var unEncodedDrillTargetsSpecification = decodeURIComponent(drillTargetsSpecification);
	var authoredDrillAction = new AuthoredDrillAction();
	authoredDrillAction.setCognosViewer(this.getCV());
	authoredDrillAction.executeDrillTarget(unEncodedDrillTargetsSpecification);
};

CDrillManager.prototype.doesMoreExist = function(menuObj)
{
	for(var i = 0; i < menuObj.getNumItems(); i++)
	{
		var menuItem = menuObj.get(i);
		if(menuItem != null)
		{
			if((menuItem instanceof CMenuItem) && (menuItem.getLabel() == this.getCV().oStrings.sMoreTextItem) && (menuItem.getAction() == this.getCVObjectRef() + '.getDrillMgr().launchGoToPage();')) {
				return true;
			}
		}
	}
	return false;
}

CDrillManager.prototype.showOtherMenuItems = function()
{
	// just add the more menu item
	var toolbarCtrl = this.getCV().rvMainWnd.getToolbarControl();

	var gtButton = null;
	var gtDropDownMenu = null;
	if (typeof toolbarCtrl != 'undefined' && toolbarCtrl != null) {
		gtButton = toolbarCtrl.getItem("goto");
		if (gtButton)
		{
			gtDropDownMenu = gtButton.getMenu();
		}
	}

	var contextMenu = this.getCV().rvMainWnd.getContextMenu();
	var sBlackList = this.getCV().rvMainWnd.getUIHide();
	var gtContextMenu = null;
	if (typeof contextMenu != 'undefined' && contextMenu != null) {
		gtContextMenu = contextMenu.getGoToMenuItem().getMenu();
	}

	var searchMenuItem = null;
	var selectionController = this.getSelectionController();

	// there's no report authored drills, just add the more menu item
	if (gtDropDownMenu != null) {
		//Do not add another more menu item if the dropdown menu already has one
		if(this.doesMoreExist(gtDropDownMenu) == false)
		{
			if(typeof gMenuSeperator != "undefined" && gtDropDownMenu.getNumItems() > 0 && (this.getCV().bCanUseCognosViewerIndexSearch || sBlackList.indexOf(' RV_TOOLBAR_BUTTONS_GOTO_RELATED_LINKS ') == -1))
			{
				gtDropDownMenu.add(gMenuSeperator);
			}

			var moreDropDownItem = new CMenuItem(gtDropDownMenu, this.getCV().oStrings.sMoreTextItem, this.getCVObjectRef() + '.getDrillMgr().launchGoToPage();', '', gMenuItemStyle, this.getCV().getWebContentRoot(), this.getCV().getSkin());
			if (this.getCV().bCanUseCognosViewerIndexSearch) {
				searchMenuItem = new CMenuItem(gtDropDownMenu, this.getCV().oStrings.sSearchTextItem, this.getCVObjectRef() + '.getDrillMgr().launchSearchPage();', '', gMenuItemStyle, this.getCV().getWebContentRoot(), this.getCV().getSkin());
			}

			if(sBlackList.indexOf(' RV_TOOLBAR_BUTTONS_GOTO_RELATED_LINKS ') != -1)
			{
				moreDropDownItem.hide();
			}
			else if(selectionController == null || selectionController.getModelDrillThroughEnabled() == false)
			{
				moreDropDownItem.disable();
			}
		}
	}

	if (gtContextMenu != null) {

		if(typeof gMenuSeperator != "undefined" && gtContextMenu.getNumItems() > 0 && (this.getCV().bCanUseCognosViewerIndexSearch || sBlackList.indexOf(' RV_CONTEXT_MENU_GOTO_RELATED_LINKS ') == -1))
		{
			gtContextMenu.add(gMenuSeperator);
		}

		var moreContextMenuItem = new CMenuItem(gtContextMenu, this.getCV().oStrings.sMoreTextItem, this.getCVObjectRef() + '.getDrillMgr().launchGoToPage();', '', gMenuItemStyle, this.getCV().getWebContentRoot(), this.getCV().getSkin());
		if (this.getCV().bCanUseCognosViewerIndexSearch) {
			searchMenuItem = new CMenuItem(gtContextMenu, this.getCV().oStrings.sSearchTextItem, this.getCVObjectRef() + '.getDrillMgr().launchSearchPage();', '', gMenuItemStyle, this.getCV().getWebContentRoot(), this.getCV().getSkin());
		}

		if(sBlackList.indexOf(' RV_CONTEXT_MENU_GOTO_RELATED_LINKS ') != -1)
		{
			moreContextMenuItem.hide();
		}
		else if(selectionController == null || selectionController.getModelDrillThroughEnabled() == false)
		{
			moreContextMenuItem.disable();
		}
	}

	if (searchMenuItem != null && selectionController != null)
	{
		var allSelections = selectionController.getAllSelectedObjects();
		if (allSelections == null || allSelections.length === 0)
		{
			searchMenuItem.disable();
		}
	}

	if (gtDropDownMenu != null) {
		gtDropDownMenu.draw();
		if (gtDropDownMenu.isVisible()) {
			gtDropDownMenu.show();
		}
	}
	if (gtContextMenu != null) {
		gtContextMenu.draw();
		if (gtContextMenu.isVisible()) {
			gtContextMenu.show();
		}
	}
}

CDrillManager.prototype.ddc = function(evt)
{
	// IE check
	if(typeof document.all != "undefined")
	{
		var node = getNodeFromEvent(evt);

		if(node != null)
		{
			var selectionController = this.getSelectionController();
			if(selectionController != null)
			{
				var selectedChartArea = selectionController.getSelectionObjectFactory().getSelectionChartObject(node);
				if(selectedChartArea != null)
				{
					var drillOptions = selectedChartArea.getDrillOptions();
					for(var idx = 0; idx < drillOptions.length; ++idx)
					{
						var currentDrillOption = drillOptions[idx][0];
						var bIsChartTitle = node.getAttribute("isChartTitle");

						if (typeof bIsChartTitle != "undefined" && bIsChartTitle != null && bIsChartTitle == "true" && (currentDrillOption == "3" || currentDrillOption == "1"))
						{
							window.event.toElement.className='drillUpCursor';
							return;
						}
						else if(currentDrillOption == "3" || currentDrillOption == "2")
						{
							window.event.toElement.className='drillCursor';
							return;
						}
					}
				}
			}

			if(node.getAttribute("dtTargets"))
			{
				window.event.toElement.className='drillThroughCursor';
			}
		}

	}


}

CDrillManager.prototype.hdc = function(evt)
{
	// IE check
	if(typeof document.all == "undefined") {
		return;
	}

	window.event.fromElement.className='noDrillCursor';
}

