//    Licensed Materials - Property of IBM
//
//    IBM Cognos Products: ps
//
//    (C) Copyright IBM Corp. 2005, 2015
//
//    US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
// Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
// Cognos and the Cognos logo are trademarks of Cognos ULC, (formerly Cognos Incorporated).

function openURL(sURL, sTarget) {
	switch (sTarget) {
		case "_parent":
			parent.location.href = sURL;
			break;
		case "_blank":
			window.open(sURL);
			break;
		case "_top":
			top.location.href = sURL;
			break;
		default:
			location.href = sURL;
	}
}

function refresh() {
	openURL(g_PS_backURL + "&refresh=");
}

function debug(e) {
	e = (e) ? e : ((window.event) ? window.event : "");
	if (e.altKey) {
		if (getSessionValue("debug") != "1") {
			setStateValue("debug", "1");
		} else {
			setStateValue("debug", "0");
		}
		location.reload();
	}
}

function doReturn() {
	if (typeof clearMVCState == "function") {
		clearMVCState("search");
	}
	openURL(g_PS_returnURL);
}

function authenticate(action, url) {
	if (window.delCookie) {
		delCookie('cc_state');
	}
	if (action == 'logon' || action == 'relogon') {
		openURL(url + "&h_CAM_action=logon&m_reload=");
	}
	if (action == 'logoff') {
		openURL(url);
	}
}

var imgAnimatedSearch = new Image();
imgAnimatedSearch.src =  g_PS_imageRoot + "action_search_ani.gif";

function searchKeyHandler(event) {
	var key = -1;
	if (event && event.which) {
		key = event.which;
	} else if (window.event) {
		key = window.event.keyCode;
	}

	if (key == 13) {
		var e = event ? event : window.event;
		e.cancelBubble = true;
		search();
		return false;
	}
}

function search() {
	var searchMenuItem = CCPopupMenu.getMenu("searchMenu").getCurrentSelected();
	if (searchMenuItem.id == 'findTextInMenu_k') {
		var searchTermsInput = document.getElementById("ps_banner_isearchfrm");
		if (searchTermsInput !== null) {
			searchTermsInput.value = document.mainSearchForm.stext.value;
		}
		document.mainISearchForm.submit();
	} else {
		// MVC - Content Reference
		// Performing remote search
		if (typeof clearMVCState == "function") {
			clearMVCState("search");
		}
		if (typeof g_PS_mvcRemoteFolderContext != "undefined" && g_PS_mvcRemoteFolderContext != null && g_PS_mvcRemoteFolderContext != '') {
			var e = document.createElement("input");
			e.setAttribute("name", "mvc-sfolder");
			e.setAttribute("type", "hidden");
			e.setAttribute("value", g_PS_mvcRemoteFolderContext);
			document.mainSearchForm.appendChild(e);
		}
		setStateValue('b_search', '0');
		document.mainSearchForm.submit();
	}
	document.images.bannerSearch.src = imgAnimatedSearch.src;
}

function goHome() {
	openURL(g_PS_gateway + "?" + g_PS_COGNOSHome);
}

function homeOptions(cmd) {
	switch (cmd) {
		case 'goHome':
			goHome();
			break;

		case 'setHome':
			var cmdForm = document.forms[g_PS_cmdFormName];
			cmdForm.m.value = g_PS_app + "/submit.xts";
			cmdForm.ifrmcmd.value = "save";
			cmdForm.m_obj.value = "~";
			cmdForm.m_class.value = "account";
			cmdForm.backURL.value = g_PS_backURL;

			var homePageElement = document.createElement("input");
			homePageElement.setAttribute("type", "hidden");
			homePageElement.setAttribute("name", "m_po_homePage");
			homePageElement.setAttribute("value", g_CC_homeQueryStringContext);
			cmdForm.appendChild(homePageElement);

			cmdForm.submit();
			break;
	}
}

function getEncodedAppBackURL() {
	return (g_PS_URLEncodedReturnURL != "") ? g_PS_URLEncodedReturnURL : g_PS_URLEncodedBackURL;
}
function getAppBackURL() {
	return (g_PS_returnURL != "") ? g_PS_returnURL : g_PS_backURL;
}

function launchApp(app, launchMode) {
	var m_objectContext = "";
	
	if (window.g_PS_getObjectContext) {
		m_objectContext = g_PS_getObjectContext();
	}
	
	var m_folderContext = "";
	
	if (window.g_PS_getFolderContext) {
		m_folderContext = g_PS_getFolderContext();
	}
		
	var m_encodedFolderContext = "";
	
	if (window.g_PS_getEncodedFolderContext) {
		m_encodedFolderContext = g_PS_getEncodedFolderContext();
	}		
		
	switch (app) {
		case "executiveViewer":
			cognosLaunchInWindow('_blank', 'menubar=no,toolbar=no,status=no,location=no,resizable=yes,width=700,height=500', 'ui.gateway', g_PS_gateway, 'ui.tool', 'EVStudio', 'ui.backURL', g_PS_backURL, 'ui.object', m_objectContext, 'ui.action', 'new');
			break;
			
		case "IBMCognosDashboard":
			if (launchMode !== undefined &&  launchMode.IBMCognosDashboard == "chromeless")
			{
				var winProp="menubar=no,toolbar=no,status=no,location=no,resizable=yes,scrollbars=yes" ;
				cognosLaunchInWindow('_blank', winProp, 'ui.gateway', g_PS_gateway, 'ui.tool', 'DashboardConsole', 'ui.action', 'view');
			}
			else
			{ 
				directLaunch('ui.gateway', g_PS_gateway, 'ui.tool', 'DashboardConsole', 'ui.action', 'view','ui.backURL', getAppBackURL(), 'ui.errURL', getAppBackURL());
			}
			
			break;
			
		case "IBMBusinessInsight":
			cognosLaunchInWindow('_blank', 'menubar=no,toolbar=no,status=no,location=no,resizable=yes,width=700,height=500', 'ui.gateway', g_PS_gateway, 'ui.tool', 'ReportStudio', 'ui.object', m_objectContext, 'ui.folder', m_folderContext, 'ui.action', 'new', 'ui.profile', 'BUA_standalone');
			break;
			
		case "IBMCognosInsight":
			cognosLaunchInWindow('_blank', 'menubar=no,toolbar=no,status=no,location=no,resizable=yes,width=700,height=500', 'ui.gateway', g_PS_gateway, 'ui.tool', 'cd', 'ui.action', 'new');
			break;
		case "AdaptiveAnalytics":
			cognosLaunch('ui.gateway', g_PS_gateway, 'ui.tool', 'AdaptiveAnalytics', 'ui.backURL', getAppBackURL(), 'ui.object', m_objectContext, 'ui.folder', m_folderContext, 'ui.action', 'run', 'ui.appname', '');
			break;
			
		case "PlanningContributor":
			cognosLaunch('ui.gateway', g_PS_gateway, 'ui.tool', 'Contributor', 'ui.backURL', getAppBackURL(), 'ui.object', m_objectContext, 'ui.folder', m_folderContext, 'ui.action', 'new', 'ui.appname', '');
			break;
			
		case "Controller":
			cognosLaunch('ui.gateway', g_PS_gateway, 'ui.tool', 'Controller');
			break;
			
		case "MetricsManager":
			cognosLaunch('ui.gateway', g_PS_gateway, 'ui.tool', 'MetricStudio', 'ui.backURL', getAppBackURL(), 'ui.object', m_objectContext, 'ui.folder', m_folderContext, 'ui.action', 'new');
			break;
			
		case "EventStudio":
			cognosLaunchInWindow('_blank', 'menubar=no,toolbar=no,status=yes,location=no,resizable=yes,width=650,height=480', 'ui.gateway', g_PS_gateway, 'ui.tool', 'EventStudio', 'ui.backURL', g_PS_backURL, 'ui.object', m_objectContext, 'ui.folder', m_folderContext, 'ui.action', 'new');
			break;
			
		case "QueryStudio":
			cognosLaunch('ui.gateway', g_PS_gateway, 'ui.tool', 'QueryStudio', 'ui.backURL', getAppBackURL(), 'ui.object', m_objectContext, 'ui.folder', m_folderContext, 'ui.action', 'new');
			break;
			
		case "AnalysisStudio":
			cognosLaunchInWindow('_blank', 'menubar=no,toolbar=no,status=yes,location=no,resizable=yes,width=650,height=480', 'ui.gateway', g_PS_gateway, 'ui.tool', 'AnalysisStudio', 'ui.backURL', g_PS_backURL, 'ui.object', m_objectContext, 'ui.folder', m_folderContext, 'ui.action', 'new');
			break;
			
		case "PowerPlayStudio":
			cognosLaunch('ui.gateway', g_PS_gateway, 'ui.tool', 'PowerplayStudio', 'ui.backURL', getAppBackURL(), 'ui.object', m_objectContext, 'ui.folder', m_folderContext, 'ui.action', 'new');
			break;
			
		case "ReportStudioPro":
			cognosLaunchInWindow('_blank', 'menubar=no,toolbar=no,status=no,location=no,resizable=yes,width=700,height=500', 'ui.gateway', g_PS_gateway, 'ui.tool', 'ReportStudio', 'ui.object', m_objectContext, 'ui.folder', m_folderContext, 'ui.action', 'new', 'ui.profile', 'Professional');
			break;
			
		case "Administration":
			openURL(g_PS_gateway + '?b_action=cogadmin&backURL=' + getEncodedAppBackURL());
			break;
			
		case "DrillThrough":
			openURL(g_PS_gateway + "?b_action=xts.run&m=" + g_PS_app + "/cc_drillthrough.xts&m_path=" + m_encodedFolderContext + "&backURL=" + getEncodedAppBackURL());
			break;
			
		case "Portal":
			openURL(g_PS_gateway + '?' + g_PS_CCHome);
			break;
			
		case "MyInbox":
			cognosLaunch('ui.gateway', g_PS_gateway, 'ui.tool', 'MyInbox', 'ui.backURL', g_PS_backURL, 'ui.action', 'view');
			break;
			
		case "MyConnectionsActivities":
			cognosLaunchInWindow('_blank', '', 'ui.gateway', '', 'ui.tool', 'LotusConnections', 'ui.appname', 'activities');									
			break;
			
		case "ConnectionsHomepage":
			cognosLaunchInWindow('_blank', '', 'ui.gateway', '', 'ui.tool', 'LotusConnections', 'ui.appname', 'homepage');									
			break;
	}
}

function bannerHelp(cmd) {
	switch (cmd) {
		case 'getStarted':
			gotoHelp(g_PS_USER_productLocale, "wig_cr_a", "");
			break;
		
		case 'moreDocs':
			gotoHelp(g_PS_USER_productLocale, "doc_lst", "");
			break;
		
		case 'help':
			help();
			break;
			
		case 'welcome':
			openURL(g_PS_gateway + "?b_action=xts.run&m=" + g_PS_app + "/welcome/welcome.xts");
			break;
			
		case 'companyWebsite':
			openURL("http://www-01.ibm.com/software/data/cognos/");
			break;
			
		case 'about':
			// Image params
			var imgBase = "$IMGROOT$about/cc_about_$PRODUCT_LOCALE$.jpg";
			var img = imgBase.replace(/\$IMGROOT\$/g, g_PS_imageRoot).replace(/\$PRODUCT_LOCALE\$/g, g_PS_USER_productLocale);
			var imgDefaultLocale = imgBase.replace(/\$IMGROOT\$/g, g_PS_imageRoot).replace(/\$PRODUCT_LOCALE\$/g, "en");
			// Message params
			var JSMessageFileBase = "$WEB$/ps/portal/js/about/ps/nls/psuiaboutmsg_$PRODUCT_LOCALE$.js";
			var JSMessageFile = JSMessageFileBase.replace(/\$WEB\$/g, g_PS_getWebRoot()).replace(/\$PRODUCT_LOCALE\$/g, g_PS_USER_productLocale);
			var JSMessageFileDefault = JSMessageFileBase.replace(/\$WEB\$/g, g_PS_getWebRoot()).replace(/\$PRODUCT_LOCALE\$/g, "en");
			var aboutBox = new about_dialog(img, imgDefaultLocale, JSMessageFile, JSMessageFileDefault);
			aboutBox.show();
			break;	
	}
}

function personalOptions(cmd) {
	if (window.resetStateParam) {
		resetStateParam('edit');
	}
	switch (cmd) {
		case 'subscriptions':
			openURL(g_PS_gateway + "?b_action=xts.run&m=" + g_PS_app + "/subscriptions/subscriptions.xts&backURL=" + getEncodedAppBackURL());
			break;

		case 'MyDatasets':
			openURL(g_PS_gateway + '?b_action=xts.run&m=' + g_PS_app + '/myDatasets/myDatasets.xts&backURL='  + g_PS_URLEncodedBackURL);
			break;
		
		case 'preferences':
			openURL(g_PS_gateway + "?b_action=xts.run&m=" + g_PS_app + "/preferences/general.xts&m_class=account&m_obj=~&backURL="  + g_PS_URLEncodedBackURL);
			break;
			
		case 'admin':
			openURL(g_PS_gateway + "?b_action=cogadmin&pathinfo=myactivities&backURL="  + g_PS_URLEncodedBackURL);
			break;			
	}
}