// Licensed Materials - Property of IBM
//
// IBM Cognos Products: pps
//
// (C) Copyright IBM Corp. 2005, 2017
//
// US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
// This forms the javascript functions used for the display options   
// action pane for PowerPlay Studio.
// The functions are Internet Explorer specific.

var XTAB_OBJ_REGULAR_IDX = -1;
var XTAB_OBJ_CALC_IDX = -1;
var XTAB_OBJ_RANK_IDX = -1;
var XTAB_OBJ_8020_IDX = -1;
var XTAB_OBJ_SUM1_IDX = -1;
var XTAB_OBJ_SUM2_IDX = -1;
var XTAB_OBJ_HIGH_EXC_IDX = -1;
var XTAB_OBJ_LOW_EXC_IDX = -1;
var XTAB_OBJ_MEASURE_IDX = -1;

var XTAB_POS_CELL = 0;
var XTAB_POS_ROW = 1;
var XTAB_POS_COL = 2;
var XTAB_POS_ALL = 3;

var overwriteall = false;

var xtabOptionsComboBoxes = new Array();
var xtabOptionComboBoxIds = new Array();

var currentSelectedItem = "";

function areFormatsEqual(format1,format2) {
	return ((format1.getAttribute("useDefaults") == format2.getAttribute("useDefaults")) &&
			(format1.getAttribute("fontSize") == format2.getAttribute("fontSize")) &&
			(format1.getAttribute("color") == format2.getAttribute("color")) &&
			(format1.getAttribute("backColor") == format2.getAttribute("backColor")) &&
			(format1.getAttribute("bold") == format2.getAttribute("bold")) &&
			(format1.getAttribute("italic") == format2.getAttribute("italic")) &&
			(format1.getAttribute("underline") == format2.getAttribute("underline")) &&
			(format1.getAttribute("textAlign") == format2.getAttribute("textAlign")));
}

function overwriteFormat(format1,format2) {
	format1.setAttribute("useDefaults",format2.getAttribute("useDefaults"));
	format1.setAttribute("fontSize",format2.getAttribute("fontSize"));
	format1.setAttribute("color",format2.getAttribute("color"));
	format1.setAttribute("backColor",format2.getAttribute("backColor"));
	format1.setAttribute("bold",format2.getAttribute("bold"));
	format1.setAttribute("italic",format2.getAttribute("italic"));
	format1.setAttribute("underline",format2.getAttribute("underline"));
	format1.setAttribute("textAlign",format2.getAttribute("textAlign"));
}

function init() {

	topparent.openActionPane();
	
	topparent.setActionPaneHeight("300");
	
	removeMessage();
	if (xtabType != 0 && xtabType != 9)
		displayCommonMessage(strErrorMessageNoXtab, MESSAGE_TYPE_INFO, ["OK"], [function() {topparent.closeActionPane();}]);
	
	ContextMenu.initialize(true,false);
	
	notifyColorChange = "paletteColorChanged();";
	
	initializeObjTypeSelect("objectTypeSelect","changeCurrentFormatItem();");
	initializeXtabPositionSelect("xtabPositionSelect","changeCurrentFormatItem();");
	initializeFontSizeSelect("xtabfontsizeselect","formatItemChanged();");
	
	storeOptionsUnderTag(document.body);
	initializeOptionValues();

	changeCurrentFormatItem();
}

function initializeObjTypeSelect(comboBoxId,action) {
	var currentSeriesSelect = document.getElementById(comboBoxId);
	if (currentSeriesSelect) {
		var currentComboBox = initComboBox(currentSeriesSelect);
		var ctr = 0;
		
		currentComboBox.addItem(new cbTextItem(strCrosstab, action, true));
		XTAB_OBJ_REGULAR_IDX = ctr++;
		
		currentComboBox.addItem(new cbTextItem(strCalculation, action, false));
		XTAB_OBJ_CALC_IDX = ctr++;
		
		currentComboBox.addItem(new cbTextItem(strRank, action, false));
		XTAB_OBJ_RANK_IDX = ctr++;
		
		currentComboBox.addItem(new cbTextItem(strSuppressed, action, false));
		XTAB_OBJ_8020_IDX = ctr++;
		
		currentComboBox.addItem(new cbTextItem(strSummary1, action, false));
		XTAB_OBJ_SUM1_IDX = ctr++;
		
		currentComboBox.addItem(new cbTextItem(strSummary2, action, false));
		XTAB_OBJ_SUM2_IDX = ctr++;
		
		if (enableHighException) {
			currentComboBox.addItem(new cbTextItem(strHighException, action, false));
			XTAB_OBJ_HIGH_EXC_IDX = ctr++;
		}
		
		if (enableLowException) {
			currentComboBox.addItem(new cbTextItem(strLowException, action, false));
			XTAB_OBJ_LOW_EXC_IDX = ctr++;
		}
		
		currentComboBox.addItem(new cbTextItem(strMeasure, action, false));
		XTAB_OBJ_MEASURE_IDX = ctr++;

		xtabOptionsComboBoxes[comboBoxId] = currentComboBox;
		xtabOptionComboBoxIds[xtabOptionComboBoxIds.length] = comboBoxId;
	}
}

function initializeXtabPositionSelect(comboBoxId,action) {
	var currentSeriesSelect = document.getElementById(comboBoxId);
	if (currentSeriesSelect) {
		var currentComboBox = initComboBox(currentSeriesSelect);
		
		currentComboBox.addItem(new cbTextItem(strDataCell, action, true));
		currentComboBox.addItem(new cbTextItem(strRowHeader, action, false));
		currentComboBox.addItem(new cbTextItem(strColHeader, action, false));
		currentComboBox.addItem(new cbTextItem(strAll, action, false));

		xtabOptionsComboBoxes[comboBoxId] = currentComboBox;
		xtabOptionComboBoxIds[xtabOptionComboBoxIds.length] = comboBoxId;
	}
}

function initializeFontSizeSelect(comboBoxId,action) {
	var currentFontSizeSelect = document.getElementById(comboBoxId);
	if (currentFontSizeSelect) {
		var currentComboBox = initComboBox(currentFontSizeSelect);
		currentComboBox.addItem(new cbTextItem(strDefault, action, false));
		currentComboBox.addItem(new cbTextItem("4", action, false));
		currentComboBox.addItem(new cbTextItem("6", action, false));
		currentComboBox.addItem(new cbTextItem("7", action, false));
		currentComboBox.addItem(new cbTextItem("8", action, false));
		currentComboBox.addItem(new cbTextItem("9", action, false));
		currentComboBox.addItem(new cbTextItem("10", action, true));
		currentComboBox.addItem(new cbTextItem("11", action, true));
		currentComboBox.addItem(new cbTextItem("12", action, false));
		currentComboBox.addItem(new cbTextItem("13", action, false));
		currentComboBox.addItem(new cbTextItem("14", action, false));
		currentComboBox.addItem(new cbTextItem("15", action, false));
		currentComboBox.addItem(new cbTextItem("16", action, false));
		currentComboBox.addItem(new cbTextItem("18", action, false));
		currentComboBox.addItem(new cbTextItem("20", action, false));

		xtabOptionsComboBoxes[comboBoxId] = currentComboBox;
		xtabOptionComboBoxIds[xtabOptionComboBoxIds.length] = comboBoxId;
	}
}

function fontButtonClicked(event) {

	var eventM = new eventManager(event);
	eventM.cancelBubble();

	var button = eventM.getSrc();

	toggleButton(button);
	
	formatItemChanged();
}

function toggleButton(button) {
	if (button.getAttribute("isdisabled") == "true")
		return;

	if (button.getAttribute("pressed") == "true") {
		setButtonState(button,false);
	} else {
		setButtonState(button,true);
	}
}

function setButtonState(button,pressed) {
	if (pressed) {
		button.className = "chartOptionsFontButtonPressed";
		button.setAttribute("pressed","true");
	} else {
		button.className = "chartOptionsFontButton";
		button.setAttribute("pressed","false");
	}
}

function alignButtonClicked(event) {

	var eventM = new eventManager(event);
	eventM.cancelBubble();

	var button = eventM.getSrc();

	setButtonState(document.getElementById("xtab_left"),false);
	setButtonState(document.getElementById("xtab_center"),false);
	setButtonState(document.getElementById("xtab_right"),false);

	setButtonState(button,true);
	
	formatItemChanged();
}

function changeCurrentFormatItem() {

	var crosstabLocationShow = true;
	currentSelectedItem = "XF_";
 
	if (xtabOptionsComboBoxes["objectTypeSelect"].getSelectedItem() == XTAB_OBJ_REGULAR_IDX) {
		currentSelectedItem += "R";
	} else if (xtabOptionsComboBoxes["objectTypeSelect"].getSelectedItem() == XTAB_OBJ_CALC_IDX) {
		currentSelectedItem += "C";
	} else if (xtabOptionsComboBoxes["objectTypeSelect"].getSelectedItem() == XTAB_OBJ_RANK_IDX) {
		currentSelectedItem += "RK";
	} else if (xtabOptionsComboBoxes["objectTypeSelect"].getSelectedItem() == XTAB_OBJ_8020_IDX) {
		currentSelectedItem += "O";
	} else if (xtabOptionsComboBoxes["objectTypeSelect"].getSelectedItem() == XTAB_OBJ_SUM1_IDX) {
		currentSelectedItem += "S1";
	} else if (xtabOptionsComboBoxes["objectTypeSelect"].getSelectedItem() == XTAB_OBJ_SUM2_IDX) {
		currentSelectedItem += "S2";
	} else if (xtabOptionsComboBoxes["objectTypeSelect"].getSelectedItem() == XTAB_OBJ_HIGH_EXC_IDX) {
		currentSelectedItem += "H";
		crosstabLocationShow = false;
	} else if (xtabOptionsComboBoxes["objectTypeSelect"].getSelectedItem() == XTAB_OBJ_LOW_EXC_IDX) {
		currentSelectedItem += "L";
		crosstabLocationShow = false;
	} else if (xtabOptionsComboBoxes["objectTypeSelect"].getSelectedItem() == XTAB_OBJ_MEASURE_IDX) {
		currentSelectedItem += "M";
		crosstabLocationShow = false;
	}
	
	overwriteall = false;
	if (crosstabLocationShow) {
		document.getElementById("xtabposspan").style.visibility = "visible";
		if (xtabOptionsComboBoxes["xtabPositionSelect"].getSelectedItem() == XTAB_POS_ROW)
			currentSelectedItem += "R";
		else if (xtabOptionsComboBoxes["xtabPositionSelect"].getSelectedItem() == XTAB_POS_COL)
			currentSelectedItem += "C";
		else if (xtabOptionsComboBoxes["xtabPositionSelect"].getSelectedItem() == XTAB_POS_ALL)
			overwriteall = true;
	} else {
		document.getElementById("xtabposspan").style.visibility = "hidden";
	}
	
	changeCurrentlySelectedFormatItem();
}

function paletteColorChanged() {
	formatItemChanged();
}

function writeFormatDataToOption(option) {
	option.setAttribute("useDefaults",(document.getElementById("xtabusedefaults").checked)? "true" : "false");
	
	if (document.getElementById("xtabusedefaults").checked) {
		document.getElementById("settingsTable").style.visibility = "hidden";
	} else {
		document.getElementById("settingsTable").style.visibility = "visible";
	}
	
	option.setAttribute("bold",document.getElementById("xtab_bold").getAttribute("pressed"));
	option.setAttribute("italic",document.getElementById("xtab_italic").getAttribute("pressed"));
	option.setAttribute("underline",document.getElementById("xtab_underlined").getAttribute("pressed"));
	
	var fontSizes = new Array(0,4,6,7,8,9,10,11,12,13,14,15,16,18,20);
	option.setAttribute("fontSize",fontSizes[xtabOptionsComboBoxes["xtabfontsizeselect"].getSelectedItem()]);

	option.setAttribute("color",getColorPickerColor("textColor"));
	option.setAttribute("backColor",getColorPickerColor("cellColor"));	
	
	var textAlign = 0;
	if (document.getElementById("xtab_center").getAttribute("pressed") == "true")
		textAlign = 1;
	else if (document.getElementById("xtab_right").getAttribute("pressed") == "true")
		textAlign = 2;
	option.setAttribute("textAlign",textAlign);
}

function formatItemChanged() {
	
	var option;
	if (!overwriteall) {
		writeFormatDataToOption(document.getElementById("opt_" + currentSelectedItem));
	} else {
		writeFormatDataToOption(document.getElementById("opt_" + currentSelectedItem));
		writeFormatDataToOption(document.getElementById("opt_" + currentSelectedItem + "R"));
		writeFormatDataToOption(document.getElementById("opt_" + currentSelectedItem + "C"));
	}
	
	refreshPreview();	
}

function selectCellPosition() {
	xtabOptionsComboBoxes["xtabPositionSelect"].setSelectedItem(0);
}

function forceAllPositionsToCellFormat() {
	overwriteFormat(document.getElementById("opt_" + currentSelectedItem + "R"), document.getElementById("opt_" + currentSelectedItem));
	overwriteFormat(document.getElementById("opt_" + currentSelectedItem + "C"), document.getElementById("opt_" + currentSelectedItem));
}

function changeCurrentlySelectedFormatItem() {

	if (overwriteall) {
	
		var option1 = document.getElementById("opt_" + currentSelectedItem);
		var option2 = document.getElementById("opt_" + currentSelectedItem + "R");
		var option3 = document.getElementById("opt_" + currentSelectedItem + "C");
	
		if (!(areFormatsEqual(option1,option2) && areFormatsEqual(option2,option3))) {
			displayCommonMessage(errAll, MESSAGE_TYPE_INFO, ["OK","Cancel"], [function() {forceAllPositionsToCellFormat();changeCurrentlySelectedFormatItem();removeMessage();},function() {selectCellPosition();removeMessage();}]);
			return;
		}
	}
	
	var option = document.getElementById("opt_" + currentSelectedItem);

	if (option.getAttribute("useDefaults") == "true") {
		document.getElementById("xtabusedefaults").checked = true;
		document.getElementById("settingsTable").style.visibility = "hidden";
	} else {
		document.getElementById("xtabusecustom").checked = true;
		document.getElementById("settingsTable").style.visibility = "visible";
	}
	
	setButtonState(document.getElementById("xtab_bold"),option.getAttribute("bold") == "true");
	setButtonState(document.getElementById("xtab_italic"),option.getAttribute("italic") == "true");
	setButtonState(document.getElementById("xtab_underlined"),option.getAttribute("underline") == "true");
	
	var fontSizeItem = 0;
	switch (option.getAttribute("fontSize")) {
		case 0: fontSizeItem = 0; break;
		case 4: fontSizeItem = 1; break;
		case 6: fontSizeItem = 2; break;
		case 7: fontSizeItem = 3; break;
		case 8: fontSizeItem = 4; break;
		case 9: fontSizeItem = 5; break;
		case 10: fontSizeItem = 6; break;
		case 11: fontSizeItem = 7; break;
		case 12: fontSizeItem = 8; break;
		case 13: fontSizeItem = 9; break;
		case 14: fontSizeItem = 10; break;
		case 15: fontSizeItem = 11; break;
		case 16: fontSizeItem = 12; break;
		case 18: fontSizeItem = 13; break;
		case 20: fontSizeItem = 14; break;
	}

	xtabOptionsComboBoxes["xtabfontsizeselect"].setSelectedItem(fontSizeItem);
	
	setColorPicker("textColor",option.getAttribute("color"));
	setColorPicker("cellColor",option.getAttribute("backColor"));
	
	var textAlign = parseInt(option.getAttribute("textAlign"));
	setButtonState(document.getElementById("xtab_left"),(textAlign == 0));
	setButtonState(document.getElementById("xtab_center"),(textAlign == 1));
	setButtonState(document.getElementById("xtab_right"),(textAlign == 2));
	
	refreshPreview();
}

function onOk() {
	applyOptions(true);
}

function onCancel() {
	topparent.clearActionPane();
}

function onApply() {
	applyOptions(false);
}

function applyOptions(closePane) {
	var cmd = createCommand();

	if (cmd != "") {
		cmd = "B:" + cmd;
		if (closePane)
			topparent.getXtabFrame().document.fhidden.ACTPANE.value = "0";
		topparent.getXtabFrame().doit(cmd);
	}
	
	if (closePane) {
		topparent.clearActionPane();
	}
}

function clearAllPopups() {
	for (var i = 0; i < xtabOptionComboBoxIds.length; i++) {
		xtabOptionsComboBoxes[xtabOptionComboBoxIds[i]].hideDropDown();
	}
}

function refreshPreview() {
	var preview = document.getElementById("xtabPreview");
	var option = document.getElementById("opt_" + currentSelectedItem);
	
	preview.style.color = "#" + option.getAttribute("color");
	preview.style.backgroundColor = "#" + option.getAttribute("backColor");

	(option.getAttribute("bold") == "true")? preview.style.fontWeight = "bold" : preview.style.fontWeight = "normal" ;
	(option.getAttribute("italic") == "true")? preview.style.fontStyle = "italic" : preview.style.fontStyle = "normal" ;
	(option.getAttribute("underline") == "true")? preview.style.textDecoration = "underline" : preview.style.textDecoration = "none" ;
	
	var textAlign = parseInt(option.getAttribute("textAlign"));
	if (textAlign == 0)
		preview.style.textAlign = "left";
	else if (textAlign == 1)
		preview.style.textAlign = "center";
	else
		preview.style.textAlign = "right";
		
	if (option.getAttribute("fontSize") != 0) {
		preview.style.fontSize = option.getAttribute("fontSize") + "pt";
	} else
		preview.style.fontSize = "9pt";	
}