/*
 *+------------------------------------------------------------------------+
 *| 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.
 *|
 *+------------------------------------------------------------------------+
 */


cvLoadDialog = function(oCognosViewerObject, aFormFields, width, height)
{
	var formWarpRequest = document.getElementById("formWarpRequest" + oCognosViewerObject.getId());

	if(formWarpRequest && oCognosViewerObject)
	{
		oCognosViewerObject.showWaitPage(false);

		var modalDialog = new CModal("", "", document.body, null, null, height, width, true, true, false, true, oCognosViewerObject.getWebContentRoot());

		var dialogForm = document.createElement("FORM");
		dialogForm.method = "POST";
		dialogForm.action = document.forms["formWarpRequest" + oCognosViewerObject.getId()].action;
		dialogForm.target = CMODAL_CONTENT_ID;
		dialogForm.style.margin = "0px";

		document.body.appendChild(dialogForm);

		for(formFieldName in aFormFields)
		{
			dialogForm.appendChild(createHiddenFormField(formFieldName, aFormFields[formFieldName]));
		}

		dialogForm.appendChild(createHiddenFormField("cv.id", oCognosViewerObject.getId()));
		dialogForm.appendChild(createHiddenFormField("b_action", "xts.run"));

		dialogForm.appendChild(createHiddenFormField("ui.action", formWarpRequest["ui.action"].value));
		dialogForm.appendChild(createHiddenFormField("ui.object", formWarpRequest["ui.object"].value));

		if(typeof oCognosViewerObject.rvMainWnd != "undefined")
		{
			dialogForm.appendChild(createHiddenFormField("run.outputFormat", oCognosViewerObject.rvMainWnd.getCurrentFormat()));
		}

		if(typeof formWarpRequest["run.outputLocale"] != "undefined")
		{
			dialogForm.appendChild(createHiddenFormField("run.outputLocale", formWarpRequest["run.outputLocale"].value));
		}

		if(typeof dialogForm["backURL"] == "undefined" && typeof dialogForm["ui.backURL"] == "undefined" && typeof formWarpRequest["ui.backURL"] != "undefined")
		{
			dialogForm.appendChild(createHiddenFormField("ui.backURL", formWarpRequest["ui.backURL"].value));
		}

		if(typeof oCognosViewerObject != "undefined" && typeof oCognosViewerObject.getConversation != "undefined" && typeof oCognosViewerObject.getTracking != "undefined")
		{
			dialogForm.appendChild(createHiddenFormField("ui.conversation", oCognosViewerObject.getConversation()));
			dialogForm.appendChild(createHiddenFormField("m_tracking", oCognosViewerObject.getTracking()));

			if(oCognosViewerObject.envParams["ui.name"] != "undefined")
			{
				dialogForm.appendChild(createHiddenFormField("ui.name", oCognosViewerObject.envParams["ui.name"]));
			}
		}

		// Remove onBeforeUnLoad for this submission but set it back after.
		var oldUnload = window.onbeforeunload;
		window.onbeforeunload = null;

		// Submit the form
		dialogForm.submit();

		window.onbeforeunload = oldUnload;

		document.body.removeChild(dialogForm);

		modalDialog.show();
	}
}

function createHiddenFormField(name, value)
{
	var formField = document.createElement("input");
	formField.setAttribute("type", "hidden");
	formField.setAttribute("name", name);
	formField.setAttribute("id", name);
	formField.setAttribute("value", value);
	return(formField);
}

/*
 * Checks the response for an authentication fault. If it finds one it will launch the log on dialog
 * Returns true if an authentication fault was detected
 */
function processAuthenticationFault(response)
{
	var authenticationFault = response.getElementsByTagName("authenticationFault");
	if (authenticationFault != null && authenticationFault.length > 0)
	{
		if (typeof g_firefoxPluginHelper != "undefined" && g_firefoxPluginHelper != null)
		{
			g_firefoxPluginHelper.addDebugLogsFromResponseXML(response);
		}

		var cvID = authenticationFault[0].getElementsByTagName("cvID");
		var cognosViewerID = "";
		if (cvID != null && cvID.length > 0)
		{
			if (typeof cvID[0].text != "undefined")
			{
				cognosViewerID = cvID[0].text;
			}
			else
			{
				cognosViewerID = cvID[0].textContent;
			}
		}

		launchLogOnDialog(cognosViewerID, response);
		return true;
	}

	return false;
}

/*
 * Calls close.xts which end up showing the log on dialog
 */
function launchLogOnDialog(cvID, response)
{
	try
	{
		var oCV = eval("oCV" + cvID);
		var params = {"b_action":"xts.run", "m":"portal/close.xts", "h_CAM_action" : "logonAs"};

		if (response != null)
		{
			var namespaceNodes = XMLHelper_FindChildrenByTagName(response, "namespace", true);
			if (namespaceNodes != null)
			{
				for(var index = 0; index < namespaceNodes.length; ++index)
				{
					var namespaceNode = namespaceNodes[index];
					if (namespaceNode != null)
					{
						var namespaceNameNode = XMLHelper_FindChildByTagName(namespaceNode, "name", false);
						var namespaceValueNode = XMLHelper_FindChildByTagName(namespaceNode, "value", false);
						if (namespaceNameNode != null && namespaceValueNode != null)
						{
							var namespaceName = XMLHelper_GetText(namespaceNameNode);
							var namespaceValue = XMLHelper_GetText(namespaceValueNode);
							if (namespaceName != null && namespaceName.length > 0)
							{
								params[namespaceName] = namespaceValue;
							}
						}
					}
				}
			}
		}

		cvLoadDialog(oCV, params, 540, 360);
	}
	catch(exception)
	{
	}
}

/*
 * Goes through the array of cognos viewer objects to find the one with a LAST_ACTION set
 */
function getCVWaitingOnFault()
{
	// need to find our oCV object
	var oCV = null;
	for (var iIndex=0; iIndex < window.gaRV_INSTANCES.length; iIndex++)
	{
		// if the oCV object has a non-empty LAST_ACTION then is must
		// be the one that triggered the log on
		if (typeof window.gaRV_INSTANCES[iIndex]["LAST_ACTION"] != "undefined" && window.gaRV_INSTANCES[iIndex]["LAST_ACTION"] != '')
		{
			oCV = window.gaRV_INSTANCES[iIndex];
			break;
		}
	}

	return oCV;
}

/*
 * Set all the cognos viewer LAST_ACTION to empty
 */
function clearLastAction()
{
	for (var iIndex=0; iIndex < window.gaRV_INSTANCES.length; iIndex++)
	{
		if (typeof window.gaRV_INSTANCES[iIndex]["LAST_ACTION"] != "undefined")
		{
			window.gaRV_INSTANCES[iIndex]["LAST_ACTION"] = "";
		}

		if (typeof window.gaRV_INSTANCES[iIndex]["LAST_ACTION_PARAMS"] != "undefined")
		{
			window.gaRV_INSTANCES[iIndex]["LAST_ACTION_PARAMS"] = "";
		}

	}
}

/*
 * Callback used from the logon dialog
 */
function ccModalCallBack(command, data)
{
	var oCV = getCVWaitingOnFault();

	destroyCModal();

	if (oCV != null)
	{
		if (typeof command != "undefined" && command == "ok")
		{
			if (oCV.getRV() != null)
			{
				oCV.getRV().updateUserName();
			}

			if (oCV["LAST_ACTION"] == "getAvailableOutput")
			{
				oCV.getRV().getAvailableOutput();
			}
			else if (oCV["LAST_ACTION"] == "SubscriptionManager")
			{
				var actionParams = oCV["LAST_ACTION_PARAMS"];
				oCV.getSubscriptionManager().AJAXHelper(actionParams.param1, actionParams.param2, actionParams.param3, actionParams.param4, actionParams.param5);
			}
			else if (oCV["LAST_ACTION"] == "getContextData")
			{
				oCV.getSelectionController().getCCDManager().FetchContextData(oCV["LAST_ACTION_PARAMS"].param1);
			}
			else if (oCV["LAST_ACTION"] == "sendRequest")
			{
				oCV.getWaitPageImpl().hide();
				var oRequest = oCV["LAST_ACTION_PARAMS"].param1;
				if (oRequest != null && oRequest.getAction() == 'wait' && oCV.getRV() != null)
				{
					oCV.getRV().RunReport();
				}
				else
				{
					oCV.sendRequest(oRequest);
				}
			}
		}
		else
		{
			oCV.rvMainWnd.hideOpenMenus();
		}
	}
}

/*
 * Callback when the user hits the cancel button in a fault dialog
 */
function closeErrorPage()
{
	var oCV = getCVWaitingOnFault();
	destroyCModal();
	clearLastAction();
	if (oCV != null)
	{
		oCV.rvMainWnd.hideOpenMenus();
	}
}

function getNodeFromEvent(evt)
{
	var node = null;
	if (document.all) {
		node = evt.srcElement;
	}
	else
	{
		node = evt.explicitOriginalTarget ? evt.explicitOriginalTarget : evt.originalTarget;
	}

	return node;
};

function getDocumentFromEvent(evt)
{
	var node = null;
	var nodeDocument = null;
	if (document.all)
	{
		node = evt.srcElement;
		nodeDocument = node.document;
	}
	else
	{
		node = evt.explicitOriginalTarget ? evt.explicitOriginalTarget : evt.originalTarget;
		nodeDocument = node.ownerDocument;
	}

	return nodeDocument;
};

function html_encode(str)
{
	return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
}

