<%
/******************************************************************************
 * Copyright (c) 2000-2005 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.*,java.text.*"%>
<%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl" %>
<%@ page import="com.edeal.frontline.navigation.FlRequest, java.lang.reflect.*" %>
<%@ page import="com.edeal.frontline.DataDictionary" %>
<%@ page import="com.edeal.frontline.helper.custom.FormHelper" %>
<%@ page import="com.edeal.frontline.controller.actions.netbusiness.*" %>
<jsp:useBean class="com.edeal.frontline.FormBean" id="Form" scope="request"/>
<jsp:useBean class="com.edeal.frontline.LinkFormQuestionBean" id="LinkFormQuestion" 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();
	FlLocale locale =  FlLocale.getInstance(session);
	boolean isThereAnyFormElement = false ;
	String id = null;
	try{
		id = Form.getID();
	}catch(FieldNotInitializedException fnie){}
	
		int seqNum = 0;
	try{
		seqNum = Form.getFrmSeqNum();
	}catch(FieldNotInitializedException fnie){
		seqNum = CounterFactory.getCounter(context , "FormNum");		
	}
	
	DecimalFormat df = locale.getDecimalFormat("0000");
	
	
	String frmEventID = null;
	String frmStatusID = null;
	String frmStatusCode = null;
	String evtTitle = null;
	String evtLabel = null;
	String evtCode = null;
	int evtStatutI = -1;
	String evtCurrentStatusLabel = null;
	EventBean evt = null;
	
	try{
		frmEventID = Form.getFrmEventID();
	}catch(FieldNotInitializedException fnie){}

	try{
		frmStatusID = Form.getFrmStatus();
	}catch(FieldNotInitializedException fnie){}
	
	if(frmStatusID != null && !"".equals(frmStatusID)){
		frmStatusCode = dico.getRefCode("FrmStatus", frmStatusID);
	}
	
	try{
		evtStatutI = (int) Form.getFrmEveAutoSt();
	}catch(FieldNotInitializedException fnie){
	evtStatutI = -1;}

	
	if (frmEventID!= null){
		try{
			evt = new EventBean(frmEventID, session);
		}catch(Exception e){}
		
		try{
			evtLabel = evt.getEvtLabel();
		}
		catch(Exception e){}
		
		try{
			evtCode = evt.getEvtCode();
		}
		catch(Exception e){}
		
		evtTitle = evtLabel + " - " + evtCode;
		
		
		if (evtStatutI !=-1){
		
			
			
			try{
				Method getter = Class.forName("com.edeal.frontline.EventBean").getMethod("getEvtStatus" + evtStatutI , null);
				evtCurrentStatusLabel = (String) getter.invoke(evt , null);
			}catch(Exception e){
				evtCurrentStatusLabel ="";
			}
		}
		
	}
	
	Vector<Vector<String>> questionTypesValues = null;
	
	try{
		questionTypesValues = dico.getRefList(session, "Tq_", "Te1");
	}catch (FrontlineException fle) {
		logger.warn(fle.getMessage());
	}
	
	String actID = null;
	Boolean isAdmin = false;
	
	if (session != null) {
	  actID = (String)session.getAttribute("effectiveActorID");
	  if (actID == null) {
	    actID = (String)session.getAttribute("actorID");
	    if (actID == null) {
	      throw new FrontlineException("there is not an actor ID in session");
	    }
	  }
	}
	
	try {
		ActorBean act = new ActorBean(actID, context);
		Vector<String> actRolesIDs = new Vector<String>();
		// On récupère les roles
		actRolesIDs.addAll(act.getRoles());
		String roleADMID = dico.getRefIdByCode("ActRoles", "ADMIN"); /* Role Administrateur*/
		if(!(actRolesIDs.isEmpty())) {
			Iterator iter = actRolesIDs.iterator();
			String rowl = "";
			while (iter.hasNext()) {
		      rowl = (String)iter.next();
		      if( rowl.equalsIgnoreCase(roleADMID)) {
		    	  isAdmin = true;
		      }
		    }
		}
	} catch (FrontlineException e){}

%>

<style type="text/css">
.dataValueX {
	width: 30%;
	overflow: hidden;
	margin: 0px;
	padding: 0px;
	padding-left: 10px;
	font-size: small;
	font-size: expression("x-small");
	color: rgb(0, 0, 0);
	font-family: Arial, Verdana, sans-serif;
	vertical-align: top;
	padding: 4px;
}
.borderLineOne {
	border-right: 1px dashed #9EC6D0;
	border-bottom: 1px dashed #9EC6D0;
}
.borderLineTwo {
	border-right: 1px dashed #9EC6D0;
}
.form_test { display: none; }
</style>
<script type="text/javascript" language="javascript" src="<fl:webapp/>/js/checkFields.jsp"></script>
<script type="text/javascript">
function doOnLoad(){
	var msg = '<%=request.getAttribute("nbDeleted")%>';
	
	if (msg != 'null' && msg.length >0){
		alert(msg+' <fl:getMessage code="L615C" alt="réponses supprimées" js="true"/>');
	}
}

function testFormWithPerson() {
	var perid=$("#OppPerID").val();
	var formUrl='<fl:getProperty name="Form" property="FrmPublicURL" alt=""/>';
	var testUrl="<fl:webapp/>/test_customform.fl?<%= CustomFormTestAction.REQ_PARAM_PERSON_ID %>="+perid+"&<%= CustomFormTestAction.REQ_PARAM_FORM_URL %>="+encodeURIComponent(formUrl);
	window.open(testUrl);
}

function editquestionstypes(){
	var f = window.document.questionstypes;
	args = '&LfqQuestionTypes=' ;
	
	values = document.getElementsByName("LfqQuestionType_");
	
	for(var x=0; x < values.length; x++)
	{
		if (values[x].value != '')
		args += values[x].value+",";
	}
	
	var ajaxURL = '<fl:link url="/edit_questionstypes_ajax.fl?"/>' + args;
	doAjax(ajaxURL, addEqpCallback);
	alert('<%=Utils.getMessage(session, "LF3A3", true, true, "Enregistré.")%>');
}

function addEqpCallback(arrIndex){
	hideWaiting();
}


</script>
<fl:bigTabs objectTitle="<%=Utils.getMessage(session, \"L63EE\", false, true, \"Questionnaire\")%>" ou="<%=\"/read_customform.fl\"+(id != null ? \"?id=\"+id : \"id\")%>">
	<div>
	<table class="container" cellpadding="0">
		<tr>
			<td colspan="3" class="titreFiche"><fl:getProperty name="Form" property="FrmLabel" alt="" /></td>
		</tr>
		<tr class="ligne_separation"><td colspan="3"></td></tr>	
			<tr>
				<td class="column2">
					<table class="container" cellpadding="0">			
						<tr class="fieldline">
							<td class="dataLabel"><fl:getMessage code="F202D" alt="Numéro"/></td>
							<td class="dataValue"><input type="hidden" name="FrmSeqNum" value="<%= "" + seqNum %>"><%= df.format((long)seqNum) %></td>
						</tr>
						<%-- <fl:fieldRead property="FrmCategory"/>
						<fl:fieldRead property="FrmTabMode" valueCodeIfTrue="L2005" valueCodeIfFalse="L2006"/> --%>
						<fl:fieldRead property="FrmTypID_"/>
						<fl:fieldRead property="FrmLngID"/>
						<fl:fieldRead property="FrmIsPublic" valueCodeIfTrue="L2005" valueCodeIfFalse="L2006"/>
						<tr class="fieldline">
							<td class="dataLabel"><fl:label field="FrmPublicURL"/></td>
							<td class="dataValue">
								<a href="<fl:getProperty name="Form" property="FrmPublicURL" alt=""/>"><fl:getProperty name="Form" property="FrmPublicURL" alt=""/>
							</td>
						</tr>
								
					</table>
				</td>
				<td class="separation"><div class="separation"></div></td>
				<td class="column2">
					<table class="container" cellpadding="0">
						<% if("PUBLISHED".equals(frmStatusCode)){ %>
						<tr>
							<td class="dataLabel"><fl:getMessage code="L81CB" alt="Tester avec une personne"/></td>
							<td class="dataValue">
								<form onsubmit="return false;">
									<fl:input property="OppPerID" nodiv="true"/>
									<div style="width: 70%; text-align: right;">
									<a href="javascript:testFormWithPerson()"><fl:getMessage code="A0009" alt="Tester"/></a></div>
									</div>
								</form>
							</td>
						</tr>
						<%} %>
						<fl:fieldRead property="FrmStatus"/>				
						<fl:fieldRead property="FrmAvailableFromDt"/>
						<fl:fieldRead property="FrmAvailableToDt"/>
						
						<%-- http://u-project2.umanis.com/view.php?id=39749: Export guide / localités et Millésime --%>
						<fl:fieldRead property="FrmMillesime_"/>
					</table>
				</td>
		</tr>		
		<tr><td colspan="3" class="ligne_separation"/></tr>
		</table>
<link href="<fl:webapp/>/css/smalltabs_css.jsp" rel="stylesheet" type="text/css">
<fl:smallTabs name="FormQuestDetails" >
	<fl:sTab id="formEvent" title="<%= Utils.getMessage(session, \"L305F\", false, true, \"Evénement\") %>">
		<table class="container" cellpadding="0">
				<tr>
					<td class="column2">
						<table class="container" cellpadding="0">			
							<fl:fieldRead property="FrmEventID" alt="" bean="true" href="true" disp="<%= flRequest.computeURL(\"/read_event.fl\") %>"/>
						</table>
					</td>
				</tr>
				<tr>	
					<td class="column2">
						<table class="data_column" cellpadding="0">						
							<fl:fieldRead property="FrmParStatusRule"/>	
						</table>			
					</td>
					<td class="separation"><div class="separation"></div></td>
					<td class="column2">
						<table class="container" cellpadding="0">																				
							<fl:fieldRead property="FrmParStatus"/>						
						</table>
					</td>
				</tr>
			</table>
	</fl:sTab>

	<fl:sTab id="formQuest" title="<%=Utils.getMessage(session, \"L606D\", false, true, \"Questions\")+\"/\"+Utils.getMessage(session, \"L6094\", false, true, \"Paragraphes\")%>">
		<form name="questionstypes" >
		<table class="container">
				<tr>
					<td class="headerDataGrid" style="width: 20%;">
						<fl:getMessage code="L6094" alt="Paragraphes"/>
					</td>
					<td class="headerDataGrid" style="width: 60%;">
						<fl:getMessage code="L606D" alt="Questions"/>
					</td>
					<td class="headerDataGrid" style="width: 10%;"><fl:getMessage code="FF22B" alt="Position"/></td>
					<td class="headerDataGrid" style="width: 15%;"><fl:getMessage code="F1023" alt="Type"/></td>
					<!-- <td class="headerDataGrid" style="width: 20%;">champs associés</td> -->
				</tr>
					<%
					if (id != null) {
						String[][] query = {{"LfqFormID = " + Utils.formatToSQL(context, id)}};
						String[] fields = {"LfqID" , "LfqFormQuestionID" , "LfqFormQuestionID:FqtLabel", "LfqParagraph", "LfqPosition", "LfqQuestionType_", "LfqCode_"};
						String[] orderBy = {"LfqPosition"};
						
						Vector linksList = LinkFormQuestionBean.listSummary(session , fields, query, orderBy);
						if (linksList != null && linksList.size() > 0) {
							for (int i=0 ; i < linksList.size() ; i++) {
								Hashtable curLink = (Hashtable) linksList.elementAt(i);
								String curLfqFormQuestionID = (String) curLink.get("LfqFormQuestionID");
								String curLfqParagraph = (String) curLink.get("LfqParagraph");
								String fqtLabel = (String) curLink.get("LfqFormQuestionID:FqtLabel");
								if(fqtLabel != null && "Hidden".equals(fqtLabel)){
									continue;
								}
								int lfqPosition = (Integer) curLink.get("LfqPosition");
								String lfqQuestionType_ = (String) curLink.get("LfqQuestionType_");
								String lfqCode_ = (String) curLink.get("LfqCode_");
								String lfqID = (String) curLink.get("LfqID");
				%>
				<% if  (curLfqFormQuestionID == null) {
				%>
					<tr class="<%= (i%2 != 0) ? "tabSimpleFdBlanc":"tabSimpleFdBleu" %>">
						<td colspan="4" style="vertical-align:top;text-align:left ; width: 20%">
								<%= Utils.getMessage(session, "L616E", "(P)") + " " + Utils.getMessage(session, curLfqParagraph, curLfqParagraph) %>
						</td>
					</tr>					
				<% } else {
				%>
					<tr class="<%= (i%2 != 0) ? "tabSimpleFdBlanc":"tabSimpleFdBleu" %>">
						<td>&nbsp;</td>
						<td style="vertical-align:top;text-align:left ; width: 60%">
								<%= Utils.getMessage(session, "L616F", "(Q)") + " " + fqtLabel %>
						</td>
						<td style="vertical-align:top;text-align:left ; width: 10%">
								<%=lfqPosition %>
						</td>
						
						<td style="vertical-align:top;text-align:left ; width: 15%">
						<% if(lfqCode_ == null || lfqCode_.length()==0){
							boolean ok = false;
							try{
								LinkFormQuestionBean lfq = new LinkFormQuestionBean(lfqID, context);
								String fqtID = lfq.getLfqFormQuestionID();
								FormQuestionBean fqt = new FormQuestionBean(fqtID, context);
								String qType = fqt.getFqtType();
								if(qType != null)
								ok = qType.equals(dico.getRefIdByCode("FqtType" , "FRE")) 
										||qType.equals(dico.getRefIdByCode("FqtType" , "FRL")) 
										||qType.equals(dico.getRefIdByCode("FqtType" , "INT")); 
							}catch(Exception e){
								logger.warn(e.getMessage());
							}
							if(ok){
								isThereAnyFormElement = true;
							%>
			<select name="LfqQuestionType_" style="background-color: #FDCBCC">
			
<%				if (questionTypesValues != null) {
	
			if(lfqQuestionType_!=null){ %><option value="<%=lfqID %>|null"></option><% }else{ %><option value=""></option><%}
			for (Vector<String> row : questionTypesValues) {
				String refID = row.firstElement();
				String refDisp = row.elementAt(1);
%>					<option value="<%=lfqID %>|<%= Utils.formatToWeb(refID, true) %>" <%if(lfqQuestionType_!=null && lfqQuestionType_.equals(refID)){ %>selected="selected"<%} %> ><%= Utils.formatToWeb(refDisp, false) %></option>
<%					}
		}
%>			</select>
<% }} %>
						</td>
						
						<%-- <td style="vertical-align:top;text-align:left ; width: 20%">
								<fl:input property="LfqStoreInFldID_" alt="" comboFobThreshold="0"/>
						</td> --%>
					</tr>				
				<%	}
				%>

				<%			}
				
						}
						
					}
				%>
		<tr>
		<%if(isThereAnyFormElement){ %>
		<tr>
		<td colspan="3" style="width: 85%">
		</td>
		<td  style="text-align:center;">
		<a href="javascript:editquestionstypes()">
		<img src="<fl:webapp/>/icons/ico/valid_green_long.gif" id="bt_addEq" title="<fl:getMessage code="A0003" alt="Enregistrer"/>"></a>
		</td>
		</tr>
		<% } %>
		</table>			
		</form>
	</fl:sTab>
	
	<% if(isAdmin){ %>
	<fl:sTab id="formQuestAdmin" title="<%=Utils.getMessage(session, \"MF1C8\", false, true, \"Questions/Paragraphes (Admin)\")%>" onselect="refreshLFQConfig()">
		<fl:grid name="LFQCONF" ajaxUrl="<%= \"/embedded_list_ajax.fl?code=LFQCONF&LfqFormID=\" + id %>"/>
		<script language="javascript">
			function deleteMarkedLfqID(LfqID) {
				if (confirm('Etes-vous sûr de vouloir supprimer cette ligne du questionnaire ?')) {
					doAjax('<fl:webapp/>/delete_object_ajax.fl?type=LinkFormQuestion&id=' + LfqID , waitNrefreshSLLfq);
				}
			}
		
			function waitNrefreshSLLfq(arrIndex) {
				if(eDealXmlhttpArr[arrIndex].readyState == 4) {
					var rtrn = trimStr(eDealXmlhttpArr[arrIndex].responseText);
					if (rtrn == '') {
						refreshLFQConfig();
					} else {
						hideWaiting();
						alert('Impossible de supprimer la ligne du questionnaire.');
					}
				}
			}
			
			function refreshLFQConfig(){
				_grid__LFQCONF_js_.render();
			}
		</script>
	</fl:sTab>
	<% } %>
	
		<% 
			String intro="";
			String conclusion="";
			String termsOfUse="";
			String header="";
			String footer="";
			String message="";
			try {
				 intro = Form.getFrmWebIntro();
			}catch(FieldNotInitializedException fnie){}
			try {
				conclusion = Form.getFrmWebConclusion();
			}catch(FieldNotInitializedException fnie){}
			try {
				termsOfUse = Form.getFrmWebTermsOfUse();
			}catch(FieldNotInitializedException fnie){}
			try {
				message = (String)Form.getCustomProperty("FrmMsgMerci_");
			}catch(FieldNotInitializedException fnie){}
			try {
				header = Form.getFrmWebHeader();
			}catch(FieldNotInitializedException fnie){}
			try {
				footer = Form.getFrmWebFooter();
			}catch(FieldNotInitializedException fnie){}
		%>	
	<fl:sTab id="FormIntroConc" title="<%= Utils.getMessage(session, \"F619E\", \"Introduction\") + \"/\" + Utils.getMessage(session, \"F619D\", \"Conclusion\") %>">
		<fl:notes name="FrmWebIntro" title="<%= Utils.getMessage(session, \"F619E\", \"Introduction\") %>" nbPix="35">
			<%= intro %>
		</fl:notes>
		<fl:notes name="FrmWebConclusion" title="<%= Utils.getMessage(session, \"F619D\", \"Conclusion\") %>" nbPix="35">
			<%= conclusion  %>
		</fl:notes>
		<fl:notes name="FrmWebTermsOfUse" title="<%= Utils.getMessage(session, \"F619F\", \"Mentions légales\") %>" nbPix="35">
			<%= termsOfUse  %>
		</fl:notes>
		<fl:notes name="FrmMsgMerci_" title="" nbPix="35">
			<fl:fieldRead property="FrmMsgMerci_" separator="<br>" bean="true" href="true" disp="<%= flRequest.computeURL(\"/read_messages.fl\") %>"/>
		</fl:notes>
	</fl:sTab>
	<fl:sTab id="FormHeadFoot" title="<%=Utils.getMessage(session, \"F61A0\", \"En-tête\")+\"/\"+Utils.getMessage(session, \"F61A1\", \"Pied de page\") %>">
		<fl:notes name="FrmWebHeader" title="<%= Utils.getMessage(session, \"F61A0\", \"En-tête\") %>" nbPix="35">
			<%= header %>
		</fl:notes>
		<fl:notes name="FrmWebFooter" title="<%= Utils.getMessage(session, \"F61A1\", \"Pied de page\") %>" nbPix="35">
			<%= footer %>
		</fl:notes>
	</fl:sTab>
	
	<fl:sTab id="FormDetails" title="<%= Utils.getMessage(session, \"F2024\", true, \"Notes\") %>">
		<div>
			<table class="container">
				<tr class="fieldline">
					<td class="dataValue">
						<fl:getProperty name="Form" property="FrmDetails" alt=" "/>
					</td>
				</tr>
			</table>				
		</div>
	</fl:sTab>	
		
</fl:smallTabs>				
	
	</div>
</fl:bigTabs>