<%
	/******************************************************************************
	 * Copyright (c) 2000-2004 e-Deal
	 *
	 * e-Deal S.A.
	 * 55 rue Brillat Savarin
	 * 75013 Paris
	 * France
	 *
	 * T: +33 (0)1 53 80 80 30
	 * F: +33 (0)1 73 01 69 77
	 * http://www.e-deal.com
	 * 
	 * La diffusion de ce code source sous quelque forme que ce soit sans
	 * l'autorisation de E-DEAL est interdite.
	 *
	 * Vous �tes autoris�s � modifier ce code source uniquement pour votre usage
	 * propre et sous r�serve que les mentions de copyright demeurent intactes.
	 *
	 * Ce code est fourni en l'�tat. Aucune garantie d'aucune sorte, explicite ou
	 * implicite n'est donn�e. En aucun cas E-DEAL ne pourra �tre tenu pour
	 * responsable des dommages pouvant r�sulter de l'utilisation de ce code
	 * source.
	 *
	 *
	 ******************************************************************************/
%><%@page session="true"%>

<%@page import="com.edeal.frontline.kpi.DisplayType"%>
<%@page import="com.edeal.frontline.kpi.ICalcAxis"%>
<%@page import="java.util.List"%>
<%@ page import="com.edeal.frontline.navigation.FlRequest"%>
<%@ page import="com.edeal.frontline.FlContext"%>
<%@ page import="com.edeal.frontline.Utils"%>
<%@ page import="com.edeal.frontline.FieldNotInitializedException"%>
<%@ page import="com.edeal.frontline.DataDictionary"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.util.Hashtable"%>
<%@ page import="com.edeal.frontline.FrontlineException"%>
<%@ page import="com.edeal.frontline.ActorBean"%>
<%@ page import="java.util.Vector"%>
<%@ page import="java.util.Set"%>
<%@ page import="com.edeal.frontline.SnapshotBean"%>
<%@page import="com.edeal.frontline.FlLocale"%>
<%@page import="com.edeal.frontline.kpi.AxisType"%>
<%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl"%>
<jsp:useBean class="com.edeal.frontline.SnapshotDisplayBean" id="SnapshotDisplay" scope="request" />
<%
	final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
			.getLog(getClass());
	FlRequest flRequest = new FlRequest(request);
	FlContext context = flRequest.getContext();
	DataDictionary dict = context.getDataDictionary();
	
	String id = null;
	try {
		id = SnapshotDisplay.getID();
	} catch (FieldNotInitializedException e) {
	}
	String sdtId = null;
	try {
		sdtId = SnapshotDisplay.getSdiSdtID();
	} catch (FieldNotInitializedException e) {
	}
	
	SnapshotBean sna = (SnapshotBean) request.getAttribute("activeSnapshot");
	String snaId = null;
	try {
		snaId = sna.getID();
	} catch (Exception e) {
	}
	
	String[] sdiFilterType = new String[4];
	try {
		sdiFilterType[0] = SnapshotDisplay.getSdiFilter1Type();
	} catch (FieldNotInitializedException e) {
	}
	try {
		sdiFilterType[1] = SnapshotDisplay.getSdiFilter2Type();
	} catch (FieldNotInitializedException e) {
	}
	try {
		sdiFilterType[2] = SnapshotDisplay.getSdiFilter3Type();
	} catch (FieldNotInitializedException e) {
	}
	try {
		sdiFilterType[3] = SnapshotDisplay.getSdiFilter4Type();
	} catch (FieldNotInitializedException e) {
	}

	boolean[] sdiFilterExclude = new boolean[4];
	try {
		sdiFilterExclude[0] = SnapshotDisplay.isSdiFilter1Exclude();
	} catch (FieldNotInitializedException e) {
	}
	try {
		sdiFilterExclude[1] = SnapshotDisplay.isSdiFilter2Exclude();
	} catch (FieldNotInitializedException e) {
	}
	try {
		sdiFilterExclude[2] = SnapshotDisplay.isSdiFilter3Exclude();
	} catch (FieldNotInitializedException e) {
	}
	try {
		sdiFilterExclude[3] = SnapshotDisplay.isSdiFilter4Exclude();
	} catch (FieldNotInitializedException e) {
	}
	
	List<Object>[] possibleValues = (List<Object>[]) request.getAttribute("possibleValues");
	
	Set<String> sfString[] = new Set[4];
	sfString[0] = (Set<String>) request.getAttribute("sfString1");
	sfString[1] = (Set<String>) request.getAttribute("sfString2");
	sfString[2] = (Set<String>) request.getAttribute("sfString3");
	sfString[3] = (Set<String>) request.getAttribute("sfString4");
	
	List<ICalcAxis> activeAxes = (List<ICalcAxis>) request.getAttribute("activeAxes");
	
%>
<script type="text/javascript" language="javascript">
	function doSubmit() {
		presentFieldsValue();
		document.forms[0].submit();
	}
</script>

<!--  snapshot combo change handling -->
<script type="text/javascript" language="javascript">
	function snapshotChanged() {
		var rs = document.getElementById("reloadSnapshot");
		rs.value = "true";
		document.forms[0].submit();
	}

	function onSdiInfosFetched(index) {
		hideWaiting();
		if (eDealXmlhttpArr[index].readyState != 4) {
			return;
		}
		var json = eDealXmlhttpArr[index].responseText;
		var sdiInfos = eval('(' + json + ')');

		var da = document.getElementById("displayAxes");

		var axesInfos = sdiInfos["sdiAxes"];
		var daHtml = "";
		var idx = 0;
		for ( var x in axesInfos) {
			var i = axesInfos[x];
			// needed for chrome
			if (typeof (i) == 'function') {
				continue;
			}
			idx++;
			daHtml += "<tr class=\"fieldline\">";
			daHtml += "<td class=\"dataLabel\">"+i["name"]+"</td>";

			daHtml += "<td class=\"dataValue\"><select class=\"input select\" name=\"SdiDisplayAxe"+idx+"\">";
			
			if (!i["mandatory"]) {
				daHtml += "<option value=\"0\">--</option>";
			}
			var dispInfos = sdiInfos["displayAxes"];
			for (var z in dispInfos) {
				var di = dispInfos[z];
				if (typeof (di) == 'function') {
					continue;
				}
				daHtml += "<option value=\""+di["number"]+"\" "+ 
				(i["selected"] == di["number"] ? "selected=\"selected\">" : ">")  + di["name"] + "</option>";
			}
			
			daHtml += "</select><br/>";
			//daHtml += "</tr>";
			daHtml += "<span class=\"comment\">"+i["desc"]+"</span></td></tr>";
		}
		
		$(da).html(daHtml);
		
		
		var ia = document.getElementById("interactiveAxes");
		var calcInfos = sdiInfos["calcAxes"];
		var axesInteractifsLbl = "<%= Utils.getMessage(session, "T0030", false, true, "Quickview") %>";
		var iaHtml = "<tr><td colspan=\"2\" class=\"dataLabel\">"+axesInteractifsLbl+"</td>";
		iaHtml += "<td><table>";
		for (var x in calcInfos) {
			var i = calcInfos[x];
			// needed for chrome
			if (typeof (i) == 'function') {
				continue;
			}
			
			if (i["type"] != 'FOB' && i["type"] != 'REF' && i["number"] != 4) {
				continue;
			}  
			
			iaHtml += "<tr>";
			iaHtml += "<td width=\"20px\">&nbsp;</td>";
			var qvChecked = i["quickview"];
			iaHtml += "<td width=\"20px\"><input type=\"checkbox\" name=\"SdiHasAxis"+i["number"]+"QView\" id=\"qvCheck"+i["number"]+"\" "+(qvChecked ? "checked=\"checked\"" : "")+"/></td>";
			iaHtml += "<td><label for=\"qvCheck"+i["number"]+"\">"+i["name"]+"</label></td>";
			iaHtml += "</tr>";
		}
		iaHtml += "</table></td></tr>"
		$(ia).html(iaHtml);		
	}
	
	function doDisplayTypeChange(sdtKey) {
		var snaId = "<%= snaId %>";
		var url = "ajax_snapshotdisplay_infos.fl?SdtID=" + sdtKey + "&SnaID="
			+ snaId + "<%= (id != null ? "&SdiID="+id : "") %>";
		doAjax(url, onSdiInfosFetched);
	}
</script>

<style>
.fieldset {
        -moz-border-radius: 8px;
        -webkit-border-radius: 8px;
        border-radius: 8px;
}
</style>


<fl:form action="/save_snapshotdisplay.fl" bean="SnapshotDisplay">
	<fl:bigTabs
		objectTitle="<%= Utils.getMessage(session, \"T0015\", false, true, \"Repr�sentation unitaire\") %>"
		ou="<%= \"/edit_snapshotdisplay.fl\" + (id != null ? \"?id=\" + id : \"\") %>">
		<table class="container" cellpadding="0">
			<tr class="fieldline">
				<td colspan="3">
				<table class="container" cellpadding="0">
					<tr class="fieldline">
						<td class="labelTitreFiche"><fl:label field="SdiName"
							labelClassName="titreFiche" /></td>
						<td class="fieldTitreFiche"><fl:input property="SdiName"
							mandatoryMsgCode="T0013" alt="" /></td>
					</tr>
				</table>
				</td>
			</tr>
		</table>

		<table class="container" cellpadding="0">
			<tr>
				<td class="column2">
				<table class="data_column" cellpadding="0">

					<tr>
						<td>

						<table cellpadding="10">
							<tr>
								<% 
						List<DisplayType> dts = (List<DisplayType>) request.getAttribute("displayTypes");
						for (DisplayType dt : dts) { %>
								<td><input id="radio<%= dt.getCode() %>" type="radio"
									name="SdiSdtID" value="<%= dt.getKey() %>"
									onchange="doDisplayTypeChange(this.value);"
									<%= dt.getKey().equals(sdtId) ? "checked": "" %>> <label
									for="radio<%= dt.getCode() %>"><%= dt.getName() %></label> </input></td>
								<% } %>
							</tr>

							<tr>
								<% for (DisplayType dt : dts) { %>
								<td align="center"><img src="<%= dt.getIcon() %>"
									alt="<%= dt.getCode() %>"
									onclick="javascript:document.getElementById('radio<%= dt.getCode() %>').checked = true; doDisplayTypeChange('<%= dt.getKey() %>')" />
								</td>
								<% } %>
							</tr>
						</table>

						</td>
					</tr>
				</table>
				</td>

				<td class="separation">
				<div class="separation"></div>
				</td>

				<td class="column2">
				<input type="hidden" id="reloadSnapshot"
					name="reloadSnapshot" value="false">
				<input type="hidden" id="mergedTables"
					name="mergedTables" value="false">
					
				<table class="data_column" cellpadding="0">
					<fl:fieldEdit property="SdiCode" mandatoryMsgCode="M2001" unicityMsgCode="M60CE" />
					<fl:fieldEdit property="SdiSnaID" comboFobThreshold="0"
						onchange="snapshotChanged();" />
					<tr>
						<td colspan="2" id="infosIndic"></td>
					</tr>

				</table>
				</td>
			</tr>

			<tr class="ligne_separation">
				<td></td>
			</tr>

			<tr>
				<td class="column2">
				<table class="data_column" id="displayAxes" cellpadding="0">

				</table>
				</td>

				<td class="separation">
				<div class="separation"></div>
				</td>

				<td class="column2">
				<table class="data_column" id="interactiveAxes" cellpadding="0">

				</table>
				</td>
			</tr>

			<tr class="ligne_separation">
				<td></td>
			</tr>

		</table>



		<% if (sna != null) { %>



		<link href="<fl:webapp/>/css/smalltabs_css.jsp" rel="stylesheet"
			type="text/css">
		<fl:smallTabs name="mesSmallTabs">
			<% 
			int i = 0;
			for (ICalcAxis ica : activeAxes) { 
			i++;
			request.setAttribute("ica", ica);
			if (ica.getType() != AxisType.IGNORED) {
			%>
			<fl:sTab id="<%= \"axis\" + ica.getUserCode() + \"Filter\" %>"
				title="<%= Utils.getMessage(session, \"T0020\", false, true, \"Filtre sur axe\") +\" \"+ ica.getDisplayName(FlLocale.getInstance(session).getLanguage()) %>"
				visible="true">

				<input type="hidden" name="SdiFilter<%= i %>Values" id="SdiFilter<%= i %>Values"/>

				<div style="margin-bottom: 15px;"><input type="radio"
					name="SdiFilter<%= i %>Type"
					value="<%= dict.getRefIdByCode("SdiFilter1Type", "NONE") %>"
					<%= sdiFilterType[i-1] == null || sdiFilterType[i-1].equals(dict.getRefIdByCode("SdiFilter1Type", "NONE")) ? " checked":" " %>><%= Utils.getMessage(session, "T002B", false, true, "Aucun filtre") %></input>
				</div>
				
				<%
				if (ica.getType() != AxisType.STRING || (i-1 < possibleValues.length && possibleValues[i-1] != null && possibleValues[i-1].size() != 0)) { %>
				<div style="margin-bottom: 15px;">
				<fieldset class="fieldset">
				<legend class="legend">
				<input type="radio"
					name="SdiFilter<%= i %>Type"
					value="<%= dict.getRefIdByCode("SdiFilter1Type", "STATIC") %>"
					<%= sdiFilterType[i-1] != null && sdiFilterType[i-1].equals(dict.getRefIdByCode("SdiFilter1Type", "STATIC")) ? " checked":" " %>><%= Utils.getMessage(session, "T002C", false, true, "Filtre statique") %></input><br />
				</legend>
				<% if (ica.getType() == AxisType.FOB || ica.getType() == AxisType.REF) {	%>
				<table style="margin-left: 30px; width: 350px;" >
					<fl:fieldEdit property="<%= ica.getField() %>" forceMultival="true"/>
				</table>
				
				<% } else if (ica.getType() == AxisType.TIME) { %>
				<label for="sfStart<%= i %>">[&nbsp;</label>
				<input type="text" id="sfStart<%= i %>" name="sfStart<%= i %>" 
				value="<%= request.getAttribute("sfStart"+i) != null ? request.getAttribute("sfStart"+i) : "" %>"/>
				<a href="javascript:openCalendar(document.forms[0].sfStart<%= i %>)">
				<img src="icons/ico/clock_neutre.gif" class="icon" alt="cal"/></a>
				
				--&gt;
				
				<input type="text" id="sfEnd<%= i %>" name="sfEnd<%= i %>" 
				value="<%= request.getAttribute("sfEnd"+i) != null ? request.getAttribute("sfEnd"+i) : "" %>"/>
				<a href="javascript:openCalendar(document.forms[0].sfEnd<%= i %>)">
				<img src="icons/ico/clock_neutre.gif" class="icon" alt="cal"/></a>
				<label for="sfEnd<%= i %>">&nbsp;]</label><br/>
				
				
				
				<% } else if (ica.getType() == AxisType.INT || ica.getType() == AxisType.DELAY) { %>
				 
					<label style="margin-left: 30px;"><b>[</b>&nbsp;</label>
					<input type="text" name="sfMin<%= i %>" 
				value="<%= request.getAttribute("sfMin"+i) != null ? request.getAttribute("sfMin"+i) : "" %>"
					/>
					&nbsp;-&nbsp;
					<input type="text" name="sfMax<%= i %>"
					value="<%= request.getAttribute("sfMax"+i) != null ? request.getAttribute("sfMax"+i) : "" %>"
					/>
					<label>&nbsp;<b>]</b></label><br/>
					
				<% } else if (ica.getType() == AxisType.STRING) { 
				for (Object o : possibleValues[i-1]) { %> 
						<input style="margin-left: 30px;" name="sfString<%= i %>" 
						type="checkbox" value="<%= o.toString() %>"
						<%= sfString[i-1] != null && sfString[i-1].contains(o.toString()) ? " checked" : "" %>
						><%= o.toString() %></input>
						<br/>
				<% } %>
				
				
				<% } else { %>
				<p>Unsupported axis type</p>
				
				
				<% } %> 
				<input style="margin-left: 20px;" type="radio"
					name="SdiFilter<%= i %>Exclude" value="false" <%= !sdiFilterExclude[i - 1] ? " checked": ""%>> Inclure
				uniquement ces valeurs </input><br />
				<input style="margin-left: 20px;" type="radio"
					name="SdiFilter<%= i %>Exclude" value="true" <%= sdiFilterExclude[i - 1] ? " checked": ""%>> Exclure ces
				valeurs </input>
				
				</fieldset>
				</div>
				<% } %>
				
				<fieldset class="fieldset">
				<legend class="legend">
				<input type="radio"
					name="SdiFilter<%= i %>Type"
					value="<%= dict.getRefIdByCode("SdiFilter1Type", "DYNAMIC") %>"
					<%= sdiFilterType[i-1] != null && sdiFilterType[i-1].equals(dict.getRefIdByCode("SdiFilter1Type", "DYNAMIC")) ? " checked":" " %>><%= Utils.getMessage(session, "T002D", false, true, "Filtre dynamique") %></input><br />
				</legend>

				<fl:kpiFilters name="<%= \"SdiFilter\" + i + \"DfiID\" %>" axis="ica" axisNum="<%= \"\" + i %>" sdi="SnapshotDisplay" styleClass="input select"/>
				</fieldset>
				
				<% if (ica.getType() == AxisType.FOB) {	%>
				<div><input type="radio" name="SdiFilter<%= i %>Type"
					value="<%= dict.getRefIdByCode("SdiFilter1Type", "FORM") %>"
					<%= sdiFilterType[i-1] != null && sdiFilterType[i-1].equals(dict.getRefIdByCode("SdiFilter1Type", "FORM")) ? " checked":" " %>><%= Utils.getMessage(session, "T002E", false, true, "Filtre fiche") %></input>
				</div>
				<% } %>
				<div id="axis<%= ica.getUserCode() %>Content"></div>
			</fl:sTab>
			<% 
				} 
			 }
			 %>
			
			<fl:sTab id="format" title="<%= Utils.getMessage(session, \"F2054\", false, true, \"Format\") %>">

			<table class="container" cellpadding="0">
			<tr>
				<td class="column2">
				<table class="data_column" cellpadding="0">
					<fl:fieldEdit property="SdiFmtKeepOneOutOf" />
					<fl:fieldEdit property="SdiFmtPointLabels" />
					<fl:fieldEdit property="SdiFmtGreenHcoID" />
					<fl:fieldEdit property="SdiFmtGreenValue" />
					<fl:fieldEdit property="SdiFmtRedHcoID" />
					<fl:fieldEdit property="SdiFmtRedValue" />
				</table>
				</td>
				<td class="separation">
				<div class="separation"></div>
				</td>
				<td class="column2">
				<table class="data_column" cellpadding="0">
					<fl:fieldEdit property="SdiFmtNbDecimal" />
				</table>
				</td>
			</tr>
			</table>

			
			</fl:sTab>
			
		</fl:smallTabs>

		<% } %>

	</fl:bigTabs>



</fl:form>

<script type="text/javascript" language="javascript">
	doDisplayTypeChange("<%= sdtId %>");
</script>

