// Copyright (C) 2007 Cognos Incorporated. All Rights Reserved.
// Cognos and the Cognos logo are trademarks of Cognos Incorporated.

/*

Class CToolbarController

*/

function CToolbarController(rvMainWnd) {

	this.m_mainWnd = rvMainWnd;
	this.m_toolbar = new CBar('toolbarContainer', gToolbarStyle);
	this.m_toolbar.getObservers().attach(this, this.toolbarButtonClicked, this.m_toolbar.onmouseup);
	this.m_toolbar.getObservers().attach(this, this.toolbarButtonClicked, this.m_toolbar.onkeypress);

	this.m_toolbar.setMenuType(cHorizonalBar);
	this.m_toolbar.setAlign('right');

	// download button
	this.m_downloadButton = new CToolbarButton(this.m_toolbar, 'javascript:DownloadReport();', '../rv/images/action_download.gif', gDownloadTooltip, gToolbarButtonStyle);

	// save button
	this.m_saveButton = new CToolbarButton(this.m_toolbar, 'javascript:SaveReport(false);', '../rv/images/action_save_report_output.gif', gSaveReportTooltip, gToolbarButtonStyle);

	// save as report view
	this.m_saveAsReportViewButton = new CToolbarButton(this.m_toolbar, 'javascript:SaveAsReportView(false);', '../rv/images/action_save_report_view.gif', gSaveAsRVTooltip, gToolbarButtonStyle);

	// email report
	this.m_emailReportButton = new CToolbarButton(this.m_toolbar, 'javascript:SendReport(false);', '../rv/images/action_send_report.gif', gEmailReportTooltip, gToolbarButtonStyle);

	// toolbar seperator
	this.m_toolbar.add(gToolbarSeperator);

	// previous report button
	this.m_previousReportButton = new CToolbarButton(this.m_toolbar, 'javascript:rvMainWnd.executePreviousReport(-1);', '../rv/images/action_previous_report.gif', gPreviousReportTooltip, gToolbarButtonStyle);

	// run report
	this.m_runReportButton = new CToolbarButton(this.m_toolbar, 'javascript:RunReport();', '../rv/images/action_run.gif', gRunReportTooltip, gToolbarButtonStyle);

	// toolbar seperator
	this.m_toolbar.add(gToolbarSeperator);

	// drill down button
	this.m_drillDownButton = new CToolbarButton(this.m_toolbar, 'goDrillManager.rvDrillDown();', '../rv/images/action_drill_down.gif', gDrillDownTextItem, gToolbarButtonStyle);

	// drill up button
	this.m_drillUpButton = new CToolbarButton(this.m_toolbar, 'goDrillManager.rvDrillUp();', '../rv/images/action_drill_up.gif', gDrillUpTextItem, gToolbarButtonStyle);

	// goto button
	this.m_goToButton = new CToolbarButton(this.m_toolbar, 'goDrillManager.getDrillThroughParameters("execute");', '../rv/images/action_go_to.gif', gGoToTextItem, gToolbarButtonStyle);

	// toolbar seperator
	this.m_toolbar.add(gToolbarSeperator);

	// view in html
	this.m_viewInHtmlButton = new CToolbarButton(this.m_toolbar, "javascript:viewReport('HTML');", '../rv/images/action_view_html.gif', gViewInHTMLTooltip, gToolbarButtonStyle);

	// view in PDF
	this.m_viewInPDFButton = new CToolbarButton(this.m_toolbar, "javascript:viewReport('PDF');", '../rv/images/action_view_pdf.gif', gViewInPDFTooltip, gToolbarButtonStyle);

	// view in XLS - Set to the first available format
	var sUIBlackList = this.m_mainWnd.getUIHide().toUpperCase();
	var sXLSURL = "";
	var sXLSIcon = "";
	var sXLSTooltip = "";

	if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_SINGLEXLS ') < 0) {
		sXLSURL = "javascript:viewReport('singleXLS');";
		sXLSIcon = "../rv/images/action_view_excel_2000_single_sheet.gif";
		sXLSTooltip = gViewInSingleXLSTooltip;
	}
	else if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_XLS ') < 0) {
		sXLSURL = "javascript:viewReport('XLS');";
		sXLSIcon = "../rv/images/action_view_excel_2000.gif";
		sXLSTooltip = gViewInXLSTooltip;
	}
	else if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_XLWA ') < 0) {
		sXLSURL = "javascript:viewReport('XLWA');";
		sXLSIcon = "../rv/images/action_view_excel_2002.gif";
		sXLSTooltip = gViewinXLSWebArchiveTooltip;
	}
	else {
		sXLSURL = "javascript:viewReport('CVS');";
		sXLSIcon = "../rv/images/action_view_csv.gif";
		sXLSTooltip = gViewInCSVTooltip;
	}
	this.m_viewInXLSButton = new CToolbarButton(this.m_toolbar, sXLSURL, sXLSIcon, sXLSTooltip, gToolbarButtonStyle);

	// view in XML
	this.m_viewInXMLButton = new CToolbarButton(this.m_toolbar, "javascript:viewReport('XML');", "../rv/images/action_view_xml.gif", gViewInXMLTooltip, gToolbarButtonStyle);

}

function CToolbarController_getMainWnd() {
	return this.m_mainWnd;
}


function CToolbarController_draw() {

	if(gEnableToolbar == false)
		return;

	// Enable/disable/remove toolbar buttons as required
	var sUIBlackList = this.m_mainWnd.getUIHide().toUpperCase();

	f = document.formWarpRequest;
	mainWnd = this.getMainWnd();

	// download button
	if(f["ui.spec"] || f["ui.action"].value != 'view' || (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_DOWNLOAD ') >= 0)) {
		this.m_downloadButton.hide();
	}
	else if(f["ui.action"].value == 'view') {
		if ((f['run.outputFormat'].value == 'HTML') || (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_DOWNLOAD_' + f['run.outputFormat'].value + ' ') >= 0))
			this.m_downloadButton.disable();
	}

	// save button
	if(f["ui.spec"] || f["ui.action"].value == 'view' || f.m_save.value != 'true' || (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_SAVE ') >= 0))
		this.m_saveButton.hide();

	// save as report view
	if(f["ui.spec"] || f["ui.action"].value == 'view' || f.objectClass.value == 'reportView' || (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_SAVEAS ') >= 0))
		this.m_saveAsReportViewButton.hide();

	// email report
	if(f["ui.spec"] || (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_SEND ') >= 0))
		this.m_emailReportButton.hide();


	reportHistory = mainWnd.getReportHistory();
	if(reportHistory.length == 0 || (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_HISTORY ') >= 0))
		this.m_previousReportButton.hide();
	else {

		reportHistoryDropDownMenu = this.m_previousReportButton.createDropDownMenu(gMenuStyle);

		reportHistory.sort(sortReportHistoryStackDescending);

		for(var i = 0; i < reportHistory.length; ++i) {
			var action = 'javascript:rvMainWnd.executePreviousReport('+ reportHistory[i].getIdx() + ');';
			reportHistoryDropDownItem = new CMenuItem(reportHistoryDropDownMenu, reportHistory[i].getReportName(), action, "", gMenuItemStyle);
		}
	}

	// run report button
	if(f.m_execute.value != 'true' || (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_RUN ') >= 0) || (canUseCognosViewerRunWithOptions == false))
		this.m_runReportButton.hide();

	// Drill down
	if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_DRILLDOWN ') >= 0 || typeof goDrillManager == "undefined") {
		this.m_drillDownButton.hide();
	}
	else {
		this.m_drillDownButton.disable();
	}

	// Drill up
	if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_DRILLUP ') >= 0 || typeof goDrillManager == "undefined") {
		this.m_drillUpButton.hide();
	}
	else {
		this.m_drillUpButton.disable();
	}

	// Go To
	if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_GOTO ') >= 0 || typeof goDrillManager == "undefined")
		this.m_goToButton.hide()
	else {
		gotoDropDownMenu = this.m_goToButton.createDropDownMenu(gMenuStyle, gGoToOptionsTooltip);
		gotoDropDownMenu.registerCallback(goDrillManager.getDrillThroughParameters);
	}

	// view in html output
	if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_HTML ') >= 0)
		this.m_viewInHtmlButton.hide();
	else if(f["ui.action"].value == 'view' && gHtmlOutputObj == '')
		this.m_viewInHtmlButton.disable();

	// view in pdf
	if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_PDF ') >= 0)
		this.m_viewInPDFButton.hide();
	else if(f["ui.action"].value == 'view' && gPDFOutputObj == '')
		this.m_viewInPDFButton.disable();

	// view in xls
	var nItemsDisabled = 0;
	if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_SINGLEXLS ') >= 0)
		nItemsDisabled++;
	if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_XLS ') >= 0)
		nItemsDisabled++;
	if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_XLWA ') >= 0)
		nItemsDisabled++;
	if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_CSV ') >= 0)
		nItemsDisabled++;

	if ((sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS ') >= 0) || nItemsDisabled == 4) //Disable if the top element is disabled, or if all entries are disabled
		this.m_viewInXLSButton.hide();
	else if(f["ui.action"].value == 'view' && nItemsDisabled < 3) { //Only create the drop down if we have more than one item enabled

		excelFormatDropDownMenu = this.m_viewInXLSButton.createDropDownMenu(gMenuStyle, gViewOptionsTooltip);

		if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_SINGLEXLS ') < 0) {
			viewInSingleXLSMenuItem = new CMenuItem(excelFormatDropDownMenu, gViewInSingleXLSTooltip, "javascript:viewReport('singleXLS');", "../rv/images/action_view_excel_2000_single_sheet.gif", gMenuItemStyle);
			if(gSingleXLSOutputObj == '') {
				viewInSingleXLSMenuItem.disable();
			}
		}

		if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_XLS ') < 0) {
			viewInXLSMenuItem = new CMenuItem(excelFormatDropDownMenu, gViewInXLSTooltip, "javascript:viewReport('XLS');", "../rv/images/action_view_excel_2000.gif", gMenuItemStyle);
			if(gXLSOutputObj == '') {
				viewInXLSMenuItem.disable();
			}
		}

		if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_XLWA ') < 0)
		{
			viewInXLSWebArchiveMenuItem = new CMenuItem(excelFormatDropDownMenu, gViewinXLSWebArchiveTooltip, "javascript:viewReport('XLWA');", "../rv/images/action_view_excel_2002.gif", gMenuItemStyle);
			if(gXLSWebArchiveOutputObj == '') {
				viewInXLSWebArchiveMenuItem.disable();
			}
		}

		if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_CSV ') < 0) {
			viewInCSVMenuItem = new CMenuItem(excelFormatDropDownMenu, gViewInCSVTooltip, "javascript:viewReport('CSV');", "../rv/images/action_view_csv.gif", gMenuItemStyle);
			if(gCSVOutputObj == '') {
				viewInCSVMenuItem.disable();
			}
		}

		this.m_viewInXLSButton.disable();
		var numOfItems = excelFormatDropDownMenu.getNumItems();
		for(var i = 0; i < numOfItems; ++i) {
			currentItem = excelFormatDropDownMenu.get(i);
			if(currentItem.isEnabled()) {
				this.m_viewInXLSButton.setIcon(currentItem.getIcon().getPath());
				this.m_viewInXLSButton.setAction(currentItem.getAction());
				this.m_viewInXLSButton.setToolTip(currentItem.getLabel());
				this.m_viewInXLSButton.enable();
				break;
			}
		}

	}
	else if (nItemsDisabled < 3) {
		excelFormatDropDownMenu = this.m_viewInXLSButton.createDropDownMenu(gMenuStyle, gViewOptionsTooltip);

		// add the excel type formats to the drop down menu
		if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_SINGLEXLS ') < 0)
			viewInSingleXLSMenuItem = new CMenuItem(excelFormatDropDownMenu, gViewInSingleXLSTooltip, "javascript:viewReport('singleXLS');", "../rv/images/action_view_excel_2000_single_sheet.gif", gMenuItemStyle);
		if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_XLS ') < 0)
			viewInXLSMenuItem = new CMenuItem(excelFormatDropDownMenu, gViewInXLSTooltip, "javascript:viewReport('XLS');", "../rv/images/action_view_excel_2000.gif", gMenuItemStyle);
		if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_XLWA ') < 0)
			viewInXLSWebArchiveMenuItem = new CMenuItem(excelFormatDropDownMenu, gViewinXLSWebArchiveTooltip, "javascript:viewReport('XLWA');", "../rv/images/action_view_excel_2002.gif", gMenuItemStyle);
		if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XLS_CSV ') < 0)
			viewInCSVMenuItem = new CMenuItem(excelFormatDropDownMenu, gViewInCSVTooltip, "javascript:viewReport('CSV');", "../rv/images/action_view_csv.gif", gMenuItemStyle);
	}

	// view in XML output
	if (sUIBlackList.indexOf(' RV_TOOLBAR_BUTTONS_XML ') >= 0)
		this.m_viewInXMLButton.hide();
	if(f["ui.action"].value == 'view' && gXMLOutputObj == '')
		this.m_viewInXMLButton.disable();


	switch(f['run.outputFormat'].value) {
		case 'HTML':
		case 'HTMLFragment': {
			this.m_viewInHtmlButton.pressed();
			break;
		}
		case 'PDF': {
			this.m_viewInPDFButton.pressed();
			break;
		}
		case 'XML': {
			this.m_viewInXMLButton.pressed();
			break;
		}
	}

	this.m_toolbar.draw();
}

function CToolbarController_hideOpenMenus() {
	this.m_toolbar.closeMenus();
}

function CToolbarController_toolbarButtonClicked() {
	// if a toolbar button was pressed, hide the context menu within the main window
	rvContextMenu = this.getMainWnd().getContextMenu();
	if (typeof rvContextMenu != 'undefined' && rvContextMenu != null) {
		rvContextMenu.hide();
	}
}


function CToolbarControlller_getGoToButton() {
	return this.m_goToButton;
}

function CToolbarController_getDrillUpButton() {
	return this.m_drillUpButton;
}

function CToolbarController_getDrillDownButton() {
	return this.m_drillDownButton;
}

function CToolbarController_getDownloadReportButton() {
	return this.m_downloadButton;
}

function CToolbarController_getSaveAsReportViewButton() {
	return this.m_saveAsReportViewButton;
}

function CToolbarController_getSaveReportOutputButton() {
	return this.m_saveButton;
}

function CToolbarController_getEmailReportButton() {
	return this.m_emailReportButton;
}

function CToolbarController_getRunReportButton() {
	return this.m_runReportButton;
}

function CToolbarController_getPreviousReportButton() {
	return this.m_previousReportButton;
}

function CToolbarController_getViewInHTMLButton() {
	return this.m_viewInHtmlButton;
}

function CToolbarController_getViewInPdfButton() {
	return this.m_viewInPDFButton;
}

function CToolbarController_getViewInExcelButton() {
	return this.m_viewInXLSButton;
}

function CToolbarController_getViewInXmlButton() {
	return this.m_viewInXMLButton;
}

function CToolbarController_update(subject)
{
	if(subject instanceof CSelectionController)
	{		
		if(typeof goDrillManager != "undefined")
		{
			var oGotoToolbarButton = this.getGoToButton();
			var oMenu = oGotoToolbarButton.getMenu();
			if(oMenu)
			{
				// when the selection changes clear out the cached report drill targets (if any)		
				oMenu.clear();
			}		
			
			var oDrillDownToolbarButton = this.getDrillDownButton();
			if(goDrillManager.canDrillDown())
			{
				oDrillDownToolbarButton.enable();
			}
			else
			{
				oDrillDownToolbarButton.disable();
			}
			
			var oDrillUpToolbarButton = this.getDrillUpButton();
			if(goDrillManager.canDrillUp())
			{
				oDrillUpToolbarButton.enable();
			}
			else
			{
				oDrillUpToolbarButton.disable();
			}
			
		}
	}

}

CToolbarController.prototype.draw = CToolbarController_draw;
CToolbarController.prototype.getMainWnd = CToolbarController_getMainWnd;
CToolbarController.prototype.hideOpenMenus = CToolbarController_hideOpenMenus;
CToolbarController.prototype.toolbarButtonClicked = CToolbarController_toolbarButtonClicked;
CToolbarController.prototype.update = CToolbarController_update;
CToolbarController.prototype.getDownloadReportButton = CToolbarController_getDownloadReportButton;
CToolbarController.prototype.getSaveAsReportViewButton = CToolbarController_getSaveAsReportViewButton;
CToolbarController.prototype.getSaveReportOutputButton = CToolbarController_getSaveReportOutputButton;
CToolbarController.prototype.getEmailReportButton = CToolbarController_getEmailReportButton;
CToolbarController.prototype.getRunReportButton = CToolbarController_getRunReportButton;
CToolbarController.prototype.getPreviousReportButton = CToolbarController_getPreviousReportButton;
CToolbarController.prototype.getGoToButton = CToolbarControlller_getGoToButton;
CToolbarController.prototype.getDrillUpButton = CToolbarController_getDrillUpButton;
CToolbarController.prototype.getDrillDownButton = CToolbarController_getDrillDownButton;
CToolbarController.prototype.getViewInHtmlButton = CToolbarController_getViewInHTMLButton;
CToolbarController.prototype.getViewInPdfButton = CToolbarController_getViewInPdfButton;
CToolbarController.prototype.getViewInExcelButton = CToolbarController_getViewInExcelButton;
CToolbarController.prototype.getViewInXmlButton = CToolbarController_getViewInXmlButton;