<%
/******************************************************************************
 * Copyright (c) 2000-2011 E-DEAL
 *
 * E-DEAL S.A.
 * 41, rue Périer
 * 92120 Montrouge
 * France
 *
 * T: +33 (0)1 73 03 29 80
 * 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 language="java" session="true" contentType="text/html; charset=UTF-8"%>
<%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl" %>
<%@ 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" %>
<jsp:useBean class="com.edeal.frontline.PopulationBean" id="Population" scope="request"/>
<jsp:useBean class="java.lang.String" id="lstCode" 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();
	String id = Population.getPopID();
	int popType = 0 ;
	try { popType = Population.getPopType(); } catch (FieldNotInitializedException ex) {}

	boolean hasFob1, hasFob2, hasFob3, hasFob4, hasFob5;
	try { hasFob1 = Population.getPopObj1() != null; } catch (FieldNotInitializedException ex) { hasFob1 = false; }
	try { hasFob2 = Population.getPopObj2() != null; } catch (FieldNotInitializedException ex) { hasFob2 = false; }
	try { hasFob3 = Population.getPopObj3() != null; } catch (FieldNotInitializedException ex) { hasFob3 = false; }
	try { hasFob4 = Population.getPopObj4() != null; } catch (FieldNotInitializedException ex) { hasFob4 = false; }
	try { hasFob5 = Population.getPopObj5() != null; } catch (FieldNotInitializedException ex) { hasFob5 = false; }
	
	String selID = null;
	String reqID = null;
	String sel2ID = null;
	
	try {
		selID = Population.getPopSelID();
	} catch (FieldNotInitializedException ex) {
	}
	
	try {
		sel2ID = Population.getPopSe2ID();
	} catch (FieldNotInitializedException fnie) {
	}
	
	try {
		reqID = Population.getPopReqID();
	} catch (FieldNotInitializedException ex) {
	}
	
	boolean fIsPopulationInProgress=false;
	try {
		fIsPopulationInProgress=Population.isPopInProgress();
	} catch (FieldNotInitializedException fnie) {}
	
	Boolean isPopParent = (Boolean)request.getAttribute("isPopParent");
	
	String evtID = null;
	try {
		evtID = Population.getPopEvtID();
	} catch (FieldNotInitializedException ex) {
	}

%>
<!-- Javascript include -->
<script language="javascript" type="text/javascript">

	function refreshPopFilles() {
		_grid__PPF_EMB_js_.render("PopParentPopID", '<%=id%>');
	}

	function refreshSynthesis() {
		doAjax('<fl:link url="<%= \"/refresh_populationv2_synthesis.fl?id=\"+Population.getID() %>"/>');
	}
	
	function changeStatus() {
			document.body.submit();
	}
		
	function refreshAddedPopulationEntry() {
		doAjax('<fl:link url="<%=\"/population2entry_list_ajax.fl?showOnlyAdded=true&PpeRemoved=0&PpeAdded=1&PpePopID=\"+ Population.getID() +\"&PopType=\"+ popType %>"/>&targetDiv=AddedPopulationEntryList');
	}
	
	function refreshPopulationEntryShowAll() {
		doAjax('<fl:link url="<%=\"/population2entry_list_ajax.fl?PpeRemoved=0&PpeAdded=0&PpePopID=\"+ Population.getID() +\"&PopType=\"+ popType %>"/>&targetDiv=PopulationEntryList');
	}
	
	function refreshRemovedPopulationEntry() {
		doAjax('<fl:link url="<%=\"/population2entry_list_removed_ajax.fl?PpeRemoved=1&PpeAdded=0&PpePopID=\"+ Population.getID() +\"&PopType=\"+ popType %>"/>&targetDiv=RemovedPopulationEntryList');
	}

	function refreshActiveTab() {
		if (PopulationTabs_selectedSTab == 0) {
			refreshSynthesis();
		} else if (PopulationTabs_selectedSTab == 1) {
			refreshPopulationEntry();
		} else if (PopulationTabs_selectedSTab == 2) {
			refreshAddedPopulationEntry();
		} else if (PopulationTabs_selectedSTab == 3) {
			refreshRemovedPopulationEntry();
		}
	}
	
	function doRemove(ppeID, del, embedded) {
		<%-- Case 1 : remove an entry not previously manually added. --%>
		if (!embedded) { 
			var elementId='popentry_'+ppeID;} else { var elementId = ppeID;}
		var elementToRemove=document.getElementById(elementId);
		<%-- Case 2 : remove an entry previously manually added, it's ID is something like popentryadded_... : after the getAjaxCallBackDeleteForElement
			callback, it's IDs will be changed to popentryremoved_, so the element that
			must be updated is the one those ID will become popentryremoved_... after this method. --%>
		var elementId2='popentryadded_'+ppeID;
		var elementToRemove2=document.getElementById(elementId2);
		
		if (del) {
			doAjax('<fl:link url="/delete_object_ajax.fl?type=PopulationEntry" />&id=' + ppeID, removeExcReturn);
		} else {
			doAjax('<fl:link url="/save_object_ajax_with_report.fl?type=PopulationEntry&PpeRemoved=true&PpeAdded=false&$$presentFields=PpeRemoved" />&id=' + ppeID, getAjaxCallBackDeleteForElement(elementToRemove, elementToRemove2));
		}
	
	}

	function getAjaxCallBackDeleteForElement(jsElement, jsElement2) {
		var fn=function(arrIndex) {
		
			if(eDealXmlhttpArr[arrIndex].readyState == 4) {
			
				hideWaiting();
				rtrn = eDealXmlhttpArr[arrIndex].responseText;
				chrtrn=trimStr(rtrn);
			    
				if (chrtrn == '') {
					//pas d'erreur
					//Changer la classe de l'élément TR 
					//utilise jsElement et jsElement2;
					if (jsElement) {
						jsElement.style.backgroundColor="#C0C0C0"
					} 
					// do not do else if here : we want to update both elements.
					if (jsElement2) {
						jsElement2.style.backgroundColor="#C0C0C0"
					}
				} else {
				 	//voir global_js.jsp
			 		result = eval('(' + rtrn + ')');
					alert('<fl:getMessage code="L604E" alt="Une erreur est survenue " js="true"/>'+' '+displayError(result));
				}
			}
		}
		return fn;
	}
	
	function removeExcReturn(arrIndex) {
		if(eDealXmlhttpArr[arrIndex].readyState == 4) {
			var rtrn = trimStr(eDealXmlhttpArr[arrIndex].responseText);
			if (rtrn == '') {
				refreshActiveTab();
			} else {
				eval(rtrn);
			}
		}
	}
	
	
	
	var submitFlag = false;
	
	function doAdd(ppeID) {
		var elementId='popentryremoved_'+ppeID;
		var elementToAdd=document.getElementById(elementId);
		doAjax('<fl:link url="/save_object_ajax.fl?type=PopulationEntry&PpeRemoved=false&PpeAdded=true&$$presentFields=PpeRemoved" />&id=' + ppeID, getAjaxCallBackAddForElement(elementToAdd));
	}

	function getAjaxCallBackAddForElement(jsElement) {
		var fn=function(arrIndex) {
		
			if(eDealXmlhttpArr[arrIndex].readyState == 4) {
			
				hideWaiting();
				rtrn = eDealXmlhttpArr[arrIndex].responseText;
				chrtrn=trimStr(rtrn);
			    
				if (chrtrn == '') {
					//pas d'erreur
					//Changer la classe de l'élément TR 
					//utilise jsElement;
					jsElement.style.backgroundColor="#C0C0C0"
				} else {
				 	//voir global_js.jsp
			 		result = eval('(' + rtrn + ')');
					alert('<fl:getMessage code="L604E" alt="Une erreur est survenue " js="true"/>'+' '+displayError(result));
				}
			}
		}
		return fn;
	}
	
	function addExcReturn(arrIndex) {
		if(eDealXmlhttpArr[arrIndex].readyState == 4) {
			var rtrn = trimStr(eDealXmlhttpArr[arrIndex].responseText);
	
			if (rtrn == '') {
				refreshActiveTab();
			} else {
				eval(rtrn);
			}
		}
	}
</script>

<%if(Utils.isEmpty(lstCode)){ %>
	<script type="text/javascript">
		function refreshPopulationEntry() {
			doAjax('<fl:link url="<%=\"/population2entry_list_ajax.fl?$$nb=30&PpeRemoved=0&PpeAdded=0&PpePopID=\"+ Population.getID() +\"&PopType=\"+ popType %>"/>&targetDiv=PopulationEntryList');
		}
	</script>
<%} else{%>
	<script type="text/javascript">
			function refreshPopulationEntry() {	
				_grid__<%=lstCode%>_js_.renderWithCriteria({"PpePopID":"<%=id%>"}, null);
			}
	</script>
<%}%>

<link rel="stylesheet" href="<fl:webapp/>/css/smalltabs_css.jsp" type="text/css">
<fl:bigTabs objectTitle="<%= Utils.getMessage(session, \"F000C\", false, true, \"Population\") %>" ou="<%= \"/read_populationv2.fl\" + (id != null ? \"?id=\" + id : \"\") %>">
	<table class="container" cellpadding="0">
		<tr>
			<td colspan="3" class="titreFiche">
				<fl:getProperty name="Population" property="PopName" alt="<%= Utils.getMessage(session, \"L0033\", \"Nouvelle population\") %>"/>
			</td>
		</tr>
		<% if (fIsPopulationInProgress) { %>
		<tr>
			<td colspan="3">
				<img src="<fl:link url="/icons/ico/refresh.gif"/>"/>&nbsp;<fl:getMessage code="L704D" alt="Population en cours de génération"/>
			</td>
		</tr> 
		<% } %>
		<tr class="ligne_separation"><td colspan="3"></td></tr>
		<tr>
			<td class="column2">
				<table class="data_column" cellpadding="0">
					<%	if (selID != null) { %>
						<fl:fieldRead property="PopSelID" bean="true" href="true" disp="<%= flRequest.computeURL(\"/read_selection.fl\") %>"/>
					<%	} %>
					<%	if (sel2ID != null) { %>
						<fl:fieldRead property="PopSe2ID" labelCode="L60F1" bean="true" href="true" disp="<%= flRequest.computeURL(\"/read_selection2.fl\") %>"/>
					<%	} %>
					<fl:fieldRead property="PopAutoDel" valueCodeIfTrue="L2005" valueCodeIfFalse="L2006"/>
					<fl:fieldRead property="PopParentPopID" bean="true" href="true" disp="<%= flRequest.computeURL(\"/read_populationv2.fl\") %>" />
				</table>
			</td>
			<td class="separation"><div class="separation"></div></td>
			<td class="column2">
				<table class="data_column" cellpadding="0">
					<fl:fieldRead property="PopGenType"/>
					<% if (reqID != null) { %>
						<fl:fieldRead property="PopReqID" bean="true" href="true" disp="/read_requester.fl"/>
					<% } %>
					<% if (evtID != null) { %>
						<fl:fieldRead property="PopEvtID" bean="true" href="true" disp="/read_event.fl"/>
					<% } %>
				</table>
			</td>
		</tr>
		<tr class="ligne_separation"><td colspan="3">&nbsp;</td></tr>
	</table>
	
	<fl:smallTabs name="PopulationTabs">
		<fl:sTab id="QUANTITATIF" title="<%= Utils.getMessage(session, \"L6382\", \"Synthèse\") %>" onselect="refreshSynthesis();">
			<div style="text-align: center;width: 100%">
				<table class="container" cellpadding="1" style="width:50%" align="center">
					<tr>
						<td colspan="1" class="headerDataGrid" style="width: 100%; text-align: center;" nowrap><span id="PopTotNbr"><fl:getProperty name="Population" property="PopTotNbr" alt="0" format="int"/></span>&nbsp;<fl:getMessage code="F617E" alt="Entrée(s)"/></td>
					</tr>
					<tr style="vertical-align: top">
						<td class="tabSimpleFdBleu dataValue" style="white-space: nowrap;">
							<table class="container" cellpadding="0">
<% try {
		if (Population.isPopIsEnt()) { %>
								<tr>
									<td class="dataValue" style="text-align: right; width: 1%; white-space: nowrap;" id="PopEntNbr">
										<fl:getProperty name="Population" property="PopEntNbr" alt="&nbsp;" format="int"/>
									</td>
									<td class="dataValue" style="text-align: left; width: 99%; white-space: nowrap;"><fl:getMessage code="F0033" alt="Entreprise(s)"/></td>
								</tr>
<%
		}
	} catch (FieldNotInitializedException fnie) {
	}
							
 	try {
		if (Population.isPopIsPer()) { %>
					      		<tr>
									<td class="dataValue" style="text-align: right; width: 1%; white-space: nowrap;" id="PopPerNbr">
										<fl:getProperty name="Population" property="PopPerNbr" alt="0" format="int"/>
									</td>
									<td class="dataValue" style="text-align: left; width: 99%; white-space: nowrap;"><fl:getMessage code="F0034" alt="Personne(s)"/></td>
								</tr>
								
<%
		}
	} catch (FieldNotInitializedException fnie) {
	}

	try {
		if (Population.isPopIsSil()) { %>
								<tr>
									<td class="dataValue" style="text-align: right; width: 1%; white-space: nowrap;" id="PopSilNbr">
										<fl:getProperty name="Population" property="PopSilNbr" alt="0" format="int"/>
									</td>
									<td class="dataValue" style="text-align: left; width: 99%; white-space: nowrap;"><fl:getMessage code="F0035" alt="Silhouette(s)"/></td>
								</tr>
<%
		}
	} catch (FieldNotInitializedException fnie) {
	}
	
	try {
		if (Population.isPopIsSup()) { %>
								<tr>
									<td class="dataValue" style="text-align: right; width: 1%; white-space: nowrap;" id="PopSupNbr">
										<fl:getProperty name="Population" property="PopSupNbr" alt="0" format="int"/>
									</td>
									<td class="dataValue" style="text-align: left; width: auto; white-space: nowrap;"><fl:getMessage code="F0037" alt="Support(s)"/></td>
								</tr>
<%
		}
	} catch (FieldNotInitializedException fnie) {
	}

	try {
		if (Population.isPopIsJou()) { %>
								<tr>
									<td class="dataValue" style="text-align: right; width: 1%; white-space: nowrap;" id="PopJouNbr">
										<fl:getProperty name="Population" property="PopJouNbr" alt="0" format="int"/>
									</td>
									<td class="dataValue" style="text-align: left; width: auto; white-space: nowrap;"><fl:getMessage code="F0038" alt="Journaliste(s)"/></td>
								</tr>
<%
		}
	} catch (FieldNotInitializedException fnie) {
	}
%>							
<% if (hasFob1) { %>
							<tr>
								<td class="dataValue" style="text-align: right; width: 1%; white-space: nowrap;" id="PopObj1">
									<fl:getProperty name="Population" property="PopFob1Nbr" alt="&nbsp;"/>
								</td>
								<td class="dataValue" style="text-align: left; width: auto; white-space: nowrap;">
									<fl:getProperty name="Population" property="PopObj1" alt="&nbsp;" bean="true"/>(s)&nbsp;
								</td>
							</tr>
<% } %>
<% if (hasFob2) { %>
							<tr>
								<td class="dataValue" style="text-align: right; width: 1%; white-space: nowrap;" id="PopObj2">
									<fl:getProperty name="Population" property="PopFob2Nbr" alt="&nbsp;"/>
								</td>
								<td class="dataValue" style="text-align: left; width: auto; white-space: nowrap;"><fl:getProperty name="Population" property="PopObj2" alt="&nbsp;" bean="true"/>(s)&nbsp;</td>
							</tr>
<% } %>
<% if (hasFob3) { %>
							<tr>
								<td class="dataValue" style="text-align: right; width: 1%; white-space: nowrap;" id="PopObj3">
									<fl:getProperty name="Population" property="PopFob3Nbr" alt="&nbsp;"/>
								</td>
								<td class="dataValue" style="text-align: left; width: auto; white-space: nowrap;"><fl:getProperty name="Population" property="PopObj3" alt="&nbsp;" bean="true"/>(s)&nbsp;</td>
							</tr>
<% } %>
<% if (hasFob4) { %>
							<tr>
								<td class="dataValue" style="text-align: right; width: 1%; white-space: nowrap;" id="PopObj4">
									<fl:getProperty name="Population" property="PopFob4Nbr" alt="&nbsp;"/>
								</td>
								<td class="dataValue" style="text-align: left; width: auto; white-space: nowrap;"><fl:getProperty name="Population" property="PopObj4" alt="&nbsp;" bean="true"/>(s)&nbsp;</td>
							</tr>
<% } %>
<% if (hasFob5) { %>
							<tr>
								<td class="dataValue" style="text-align: right; width: 1%; white-space: nowrap;" id="PopObj5">
									<fl:getProperty name="Population" property="PopFob5Nbr" alt="&nbsp;"/>
								</td>
								<td class="dataValue" style="text-align: left; width: auto; white-space: nowrap;"><fl:getProperty name="Population" property="PopObj5" alt="&nbsp;" bean="true"/>(s)&nbsp;</td>
							</tr>
<% } %>
								<tr>
									<td class="dataValue" style="text-align: right; width: 1%; white-space: nowrap;" id="PopAddNbr">
										<fl:getProperty name="Population" property="PopAddNbr" alt="0" format="int"/>
									</td>
									<td class="dataValue" style="text-align: left; width: 99%;" nowrap><fl:getMessage code="F0039" alt="Entrées rajoutées manuellement"/></td>
								</tr>
								<tr>
									<td class="dataValue" style="text-align: right; width: 1%; white-space: nowrap;" id="PopRmvNbr">
										<fl:getProperty name="Population" property="PopRmvNbr" alt="0" format="int"/>
									</td>
									<td class="dataValue" style="text-align: left; width: 99%;" nowrap><fl:getMessage code="F003A" alt="Entrées retirées manuellement"/></td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</div>
		</fl:sTab>
		<fl:sTab id="BELONG" title="<%= Utils.getMessage(session, \"L002B\", \"Appartiennent à la cible\") %>" onselect="refreshPopulationEntry();">
			<%if(Utils.isEmpty(lstCode)){ %>
			<div id="PopulationEntryList">
				<table cellpadding="0" style="width: 100%;">
					<tr>
						<td>
							&nbsp;
						</td>
					</tr>
				</table>
			</div>
			<%} else  {%> 
				<fl:grid name="<%=lstCode %>" ajaxUrl="<%=\"/embedded_list_ajax_criteria.fl?code=\"+lstCode+\"&lc=\"+lstCode  %>" />
			<% } %>
		</fl:sTab>
		<fl:sTab id="ADDED" title="<%= Utils.getMessage(session, \"A3072\", \"Ont été ajoutés manuellement\") %>" onselect="refreshAddedPopulationEntry();">	
			<div id="AddedPopulationEntryList">
				<table cellpadding="0" style="width: 100%;">
					<tr>
						<td>
							&nbsp;
						</td>
					</tr>
				</table>
			</div>	
		</fl:sTab>
		<fl:sTab id="REMOVED" title="<%= Utils.getMessage(session, \"L002C\", \"Ont été retirés de la cible\") %>" onselect="refreshRemovedPopulationEntry();">	
			<div id="RemovedPopulationEntryList">
				<table cellpadding="0" style="width: 100%;">
					<tr>
						<td>
							&nbsp;
						</td>
					</tr>
				</table>
			</div>	
		</fl:sTab>
		<%if (isPopParent){ %>
		<fl:sTab id="POPULATIONS_FILLES" title="<%= Utils.getMessage(session, \"L637D\", \"Populations filles\") %>" onselect="refreshPopFilles();">
			<fl:grid  name="PPF_EMB" ajaxUrl="/embedded_list_ajax.fl?PopParentPopID=<%=id %>" />
		</fl:sTab>
		<%} %>	
	</fl:smallTabs>		
</fl:bigTabs>
