<%
/******************************************************************************
 * 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" import="com.edeal.frontline.*,java.util.*, com.edeal.frontline.navigation.*"%>
<%@ page import="javax.swing.tree.DefaultMutableTreeNode" %>
<%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl" %>
<jsp:useBean class="com.edeal.frontline.SelectionJournalistBean" id="SelectionJournalist" 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 dico = context.getDataDictionary();

	SelectionJournalistBean obj = (SelectionJournalistBean)request.getAttribute("SelectionJournalist");
	
	String id = null;
	try{
		id = SelectionJournalist.getID();
	}catch(Exception e){}

	String errorID = (String)request.getAttribute("ErrorID");

	String[] strObjSql = {"Support", "Journalist"};
	String[] strRootFld = {"SjoCriSupID", "SjoCriJouID"};
	String[] strObjDisp = new String[strObjSql.length];
	String[] strRootCriID = new String[strObjSql.length];

	boolean hasCrit = false;

	for(int i = 0; i < strObjSql.length; i++) {
		ObjectsBean objBean = dico.getTable("sql", strObjSql[i]);
		try {
			strObjDisp[i] = objBean.getObjDisplay();
		} catch (FieldNotInitializedException ex) {
			strObjDisp[i] = strObjSql[i];
		}
		try {
			strRootCriID[i] = (String)obj.getClass().getMethod("get" + strRootFld[i], null).invoke(obj, null);
			hasCrit = hasCrit || strRootCriID[i] != null;
		} catch (Exception ex) {
			//logger.error("STACK TRACE",ex);
		}
	}
		

boolean isCreate = id == null;
	
String salesModel = context.getContextParameter("SalesModel");
boolean isB2C = "B2C".equalsIgnoreCase(salesModel);
String refPerID = dico.getRefIdByCode("SelMaxTyp", "PER");

Vector criteriaGroups = (Vector)flRequest.getAttribute("criteria_list");
if (criteriaGroups == null) {
	criteriaGroups = new Vector();
}
boolean hasCriteria = false;
for (int i = 0; i < criteriaGroups.size(); i++) {
	Hashtable crit = (Hashtable)criteriaGroups.elementAt(i);
	if (crit.get("criteria_tree") != null) {
		hasCriteria = true;
	}
}


%>
<link rel="stylesheet" href="<fl:webapp/>/css/smalltabs_css.jsp" type="text/css">

<%
	if(errorID != null) {
		String errorMsg = null;
		if(errorID.equals("NOTBROS")) {
			errorMsg = Utils.getMessage(session, "M3035", "Les critères sélectionnés ne se trouvent pas au même niveau");
		} else if(errorID.equals("ALONE")) {
			errorMsg = Utils.getMessage(session, "M3036", "Veuillez sélectionner plusieurs critères");
		} else if(errorID.equals("ROOTSELECTED")) {
			errorMsg = Utils.getMessage(session, "M3037", "Il n'est pas possible de dégrouper la racine");
		} else if(errorID.equals("TOOMANY")) {
			errorMsg = Utils.getMessage(session, "M3038", "Veuillez ne sélectionner qu'un seul groupe de critères");
		} else if(errorID.equals("LEAFSELECTED")) {
			errorMsg = Utils.getMessage(session, "M3039", "Veuillez sélectionner un groupe de critères");
		} else {
			errorMsg = Utils.getMessage(session, "M303A", "Erreur inconnue") + " (" + errorID + ")";
		}

%>
<script language="javascript">
function doInit() {
	window.alert('<%= Utils.formatToJavascript(errorMsg) %>');
}

</script>
<%
	}
%>
<script language="javaScript">
	function doAdd(parentID, objID, field){
	var win = window.open('<fl:link url="/create_criteria.fl?dummy=1" list="true"/>&RootFld=' + field + '&CtrID='+'<%=id%>'+'&CriFobID=' + objID + '&Src=SelectionJou' + (parentID ? '&CriParentID=' + parentID + '&GrpID=' + parentID : ''), 'create_criteria', 'menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes,width=800,height=250');
	}
	
	function doSubmit() {
		var f = document.forms[0];
		return true;
	}
	
	function doAddEvent(parentID, objID, field) {
		window.open('<fl:link url="/edit_eventcriteria.fl?dummy=1" list="true"/>&RootFld=' + field + '&CtrID=<fl:getProperty name="Selection" property="SelID" alt=""/>&CriFobID=' + objID + (parentID ? '&CriParentID=' + parentID + '&GrpID=' + parentID : ''), 'create_criteria', 'menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes,width=800,height=250');
	}
	function doEdit(criteriaID, objID, field) {
		window.open('<fl:link url="/edit_criteria.fl?dummy=1" list="true"/>&RootFld=' + field + '&CriFobID=' + objID + '&id=' + criteriaID + '&CtrID=<%=id%>&Src=SelectionJou', 'create_criteria', 'menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes,width=800,height=250');
	}
	// function doEditEvent(criteriaID, objID, field) {
	//	window.open('<fl:link url="/edit_eventcriteria.fl?dummy=1" list="true"/>&RootFld=' + field + '&CriFobID=' + objID + '&id=' + criteriaID, 'create_criteria', 'menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes,width=800,height=220');
	// }
	function hasSelected() {
		var f = document.forms[0];
		if (f.CriID == null) {
			return false;
		}
		var result = false;
		if (f.CriID.length) {
			for (var i = 0; !result && i < f.CriID.length; i++) {
				result = result || f.CriID[i].checked;
			}
		} else {
			result = f.CriID.checked;
		}
		return result;
	}
	function doDeleteCriteria() {
		performOperation('delete');
	}
	function performOperation(operation) {
		
		if (hasSelected()) {		
			var field = criteriaFields.elementAt(CriteriaTabs_selectedSTab);
			var url = '<fl:webapp/>/criteria_operation.fl?op=' + operation + '&CtrID=<%= id %>&RootFld=' + field;
			var ids = '';
			var f = document.forms[0];
			if (f.CriID.length) {
				for (var i = 0; i < f.CriID.length; i++) {
					if (f.CriID[i].checked && f.CriID[i].getAttribute('field') == field) {
						ids += '&CriID=' + f.CriID[i].value;
					}
				}
			} else {
				if (f.CriID.checked && f.CriID.getAttribute('field') == field) {
					ids += '&CriID=' + f.CriID.value;
				}
			}
			if (ids != '') {
				url += ids;				
				doAjax(url);
			}
		}
	}
	
	function doGroupAnd() {
		performOperation('and');
	}
	
	function doGroupOr() {
		performOperation('or');
	}
	
	function doUngroup() {
		performOperation('ungroup');
	}
	var criteriaFields = new FlVector();

</script>

<body<% if(errorID != null) { %> onLoad="doInit();"<% } %>>

<fl:bigTabs objectTitle="<%=Utils.getMessage(session, \"L64C8\", \"Ciblage journaliste\")%>" ou="<%=\"/read_selectionjournalist.fl\" +(id != null ? \"?id=\"+id : \"\")%>">
<fl:form action="/save_selectionjournalist.fl" bean="SelectionJournalist">
<table class="container">
	<tr>
		<td colspan="3">
			<table class="container" cellpadding="0">
				<tr class="fieldline">
					<td class="labelTitreFiche">
						<fl:label field="SjoName" labelClassName="titreFiche"/>
					</td>
					<td class="fieldTitreFiche">
						<fl:input property="SjoName" mandatoryMsgCode="M2014"/>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr class="ligne_separation"><td></td></tr>
</table>
<% if (!isCreate) {
%>
	<fl:smallTabs name="CriteriaTabs"><% 
	for (int i = 0; i < criteriaGroups.size(); i++) { 
		Hashtable group = (Hashtable)criteriaGroups.elementAt(i);
		String title = (String)group.get("title");
		String objSql = (String)group.get("objsql");
		String field = (String)group.get("field");		
		String objID = dico.getTable("sql", objSql).getObjID();
		DefaultMutableTreeNode tree = (DefaultMutableTreeNode)group.get("criteria_tree");
							%>
		<fl:sTab id="<%= \"sTab\" + i %>" title="<%= title + (tree != null ? \" (x)\" : \"\") %>">
			<div>
			<script language="javascript" type="text/javascript">criteriaFields.addElement('<%= field %>');</script>
<%
		String criID = "";
		if (tree != null) {
			flRequest.setAttribute("criteria_tree", tree);
			try {
				criID = ((CriteriaBean)tree.getUserObject()).getID();
			} catch (Exception e) {
			}
			String url = "/marketing/criteria/read.jsp";
%>
								<jsp:include page="<%= url %>" flush="true" >
									<jsp:param name="Edit" value="1"/>
									<jsp:param name="ObjID" value="<%= objID %>"/>
									<jsp:param name="RootFld" value="<%= field %>"/>
									<jsp:param name="CriID" value="<%= criID %>"/>
								</jsp:include>
<%
			flRequest.removeAttribute("criteria_tree");
		}
%>
								<a href="javascript:doAdd('', '<%= objID %>', '<%= field %>')">(+)</a>
								</div>
							</fl:sTab>
	
<%	}
%>
	</fl:smallTabs>
<%
}else{%>
<input name="$$returnedHtmlHead" type="hidden" value="...">
<input name="$$returnedHtmlBody" type="hidden" value="...">
<%
}
%>
	
</fl:form>
</fl:bigTabs>
</body>