<%
/******************************************************************************
 * 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.*,java.text.*,java.lang.reflect.*, com.edeal.frontline.navigation.*"%>
<%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl" %>
<jsp:useBean class="com.edeal.frontline.ScriptResponseBean" id="ScriptResponse" scope="request"/>
<%
final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(getClass());
String contextPath = (String) session.getAttribute("contextPath");

FlRequest flRequest = new FlRequest(request);
FlContext context = flRequest.getContext();
DataDictionary dico = context.getDataDictionary();

String id = flRequest.getParameter("scriptID");
String isVrp = Utils.getRequestParameter(context, request, "vrp");
boolean isReportInclude = Utils.getRequestParameter(context, request, "report") != null;
ScriptResponseBean srp = new ScriptResponseBean(id, session); 
if (isReportInclude) {
	if (ScriptResponse.getSession() == null) {
		ScriptResponse.setSession(session);
	}
	try {
		ScriptResponse.populateFromID(id);
	} catch (FrontlineException fe) {
	} catch (AccessDeniedException ade) {
	}
}



String perID= flRequest.getParameter("perID");
if (perID == null){
try {
	perID = srp.getSrpPersonID();
} catch (FieldNotInitializedException fnie) {
}
}
String scriptID = null;


try {
	scriptID = srp.getSrpScriptID();
} catch (FieldNotInitializedException fnie) {
}

String[][] queryScript = {{"ScrID = " + Utils.formatToSQL(context, scriptID)}};
String[] fieldsScript = {"ScrLabel" , "ScrSeqNum" , "ScrEventID"};
String scriptLabel = "???";
String scriptNumber = "???";
String scriptEventID = null;

Vector listScript = ScriptBean.listSummary(session , fieldsScript , queryScript);
if (listScript != null && listScript.size() > 0) {
	scriptEventID = (String) ((Hashtable) listScript.elementAt(0)).get("ScrEventID");
	scriptLabel = (String) ((Hashtable) listScript.elementAt(0)).get("ScrLabel");
	Integer scriptNumberI = (Integer) ((Hashtable) listScript.elementAt(0)).get("ScrSeqNum");
	if (scriptNumberI != null) {
		DecimalFormat df = FlLocale.getInstance(session).getDecimalFormat("0000");
		scriptNumber = df.format((long) (scriptNumberI.intValue()));
	}
}
EventBean evtObj = null;
try {
	evtObj = new EventBean(scriptEventID, session);
} catch (AccessDeniedException ade) {
} catch (FrontlineException fe) {
}

String evtLabel = "";
try { evtLabel = Utils.formatToWeb(evtObj.getEvtLabel(), false); }
catch (FieldNotInitializedException fnie) {}

%>
<% if (!isReportInclude) {} %>
<html>
<head>

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

<script language="javascript" type="text/javascript" src="<fl:webapp/>/js/smalltabs.jsp"></script>
<script type="text/javascript" language="javascript" src="<fl:webapp/>/js/calendar.jsp"></script>
<script type="text/javascript" language="javascript" src="<fl:webapp/>/js/utils.js"></script>
<script type="text/javascript" language="javascript" src="<fl:webapp/>/js/checkFields.jsp"></script>
<script type="text/javascript" language="javascript" src="<fl:webapp/>/js/presentField.js"></script>
<script type="text/javascript" language="javascript" src="<fl:webapp/>/js/ajax/sarissa.js"></script>
<script type="text/javascript" language="javascript" src="<fl:webapp/>/js/ajax/ajax.js"></script>
<script type="text/javascript" language="javascript" src="<fl:webapp/>/js/master_js.jsp"></script>
<script type="text/javascript" language="javascript" src="<fl:webapp/>/js/ac/fobAC_js.jsp"></script>


</head>
<body>
<div id="object_css" style="top: 0px; left: 0px; bottom: 0px; height: 100%; overflow: auto;">
<fl:bigTabs ignoreList="true" objectTitle="<%= Utils.getMessage(session, \"L70BB\", false, true, \"Réponses du script\") %>">
<%
String[] fieldsPer = {"PerCivID:Val", "PerName", "PerFstName", "PerPhone", "PerFax", "PerMail", "PerTitle", 
											"PerEntID", "PerEntID:EntCorpName", "PerEntID:EntAd1", "PerEntID:EntAd2", "PerEntID:EntAd3", "PerEntID:EntZip", "PerEntID:EntCity", "PerEntID:EntPhone", "PerEntID:EntFax",
											"PerLocID", "PerLocID:LocAd1", "PerLocID:LocAd2", "PerLocID:LocAd3", "PerLocID:LocZip", "PerLocID:LocCity", "PerLocID:LocPhone", "PerLocID:LocFax"
										};
String[][] queryPer = {{"PerID = " + Utils.formatToSQL(context, perID)}};
String perCompleteName = "???";
Vector listPer = PersonBean.listSummary(session , fieldsPer , queryPer);
if (listPer != null && listPer.size() > 0) {
	Hashtable row = (Hashtable) listPer.elementAt(0);
	String perCiv = (String)row.get("PerCivID:Val");
	String perName = (String)row.get("PerName");
	String perFstName = (String)row.get("PerFstName");
	String perTitle = (String)row.get("PerTitle");
	String perMail = (String)row.get("PerMail");
	String perFax = (String)row.get("PerFax");
	String perPhone = (String)row.get("PerPhone");
	perCompleteName = (perCiv != null ? perCiv : "") + " " + (perFstName != null ? perFstName : "") + " " + (perName != null ? perName : "");
	String entID = (String)row.get("PerEntID");
	String entCorpName = (String)row.get("PerEntID:EntCorpName");
	String entAd1 = (String)row.get("PerEntID:EntAd1");
	String entAd2 = (String)row.get("PerEntID:EntAd2");
	String entAd3 = (String)row.get("PerEntID:EntAd3");
	String EntZip = (String)row.get("PerEntID:EntZip");
	String entCity = (String)row.get("PerEntID:EntCity");
	String entFax = (String)row.get("PerEntID:EntFax");
	String entPhone = (String)row.get("PerEntID:EntPhone");
	String locID = (String)row.get("PerLocID");
	String locAd1 = (String)row.get("PerLocID:LocAd1");
	String locAd2 = (String)row.get("PerLocID:LocAd2");
	String locAd3 = (String)row.get("PerLocID:LocAd3");
	String locZip = (String)row.get("PerLocID:LocZip");
	String locCity = (String)row.get("PerLocID:LocCity");
	String locFax = (String)row.get("PerLocID:LocFax");
	String locPhone = (String)row.get("PerLocID:LocPhone");
	StringBuffer address = new StringBuffer();
	if (locID != null) {
		if (locAd1 != null) {
			address.append(locAd1);
			address.append("\n");
		}
		if (locAd2 != null) {
			address.append(locAd2);
			address.append("\n");
		}
		if (locAd3 != null) {
			address.append(locAd3);
			address.append("\n");
		}
		if (locZip != null) {
			address.append(locZip);
			address.append(" ");
		}
		if (locCity != null) {
			address.append(locCity);
		}
	}
	if (address.length() == 0) {
		if (entID != null) {
			if (entAd1 != null) {
				address.append(entAd1);
				address.append("\n");
			}
			if (entAd2 != null) {
				address.append(entAd2);
				address.append("\n");
			}
			if (entAd3 != null) {
				address.append(entAd3);
				address.append("\n");
			}
			if (EntZip != null) {
				address.append(EntZip);
				address.append(" ");
			}
			if (entCity != null) {
				address.append(entCity);
			}
		}
	}
	String[] fieldsPvt = {"PvtParStatus:Val"};
	String[][] queryPvt = {{"PvtEventID = " + Utils.formatToSQL(context, scriptEventID), "PvtPersonID = " + Utils.formatToSQL(context, perID)}};
	Vector pvts = PersonEventBean.listSummary(context, fieldsPvt, queryPvt);
	String pvtParStatus = "";
	if (!pvts.isEmpty()) {
		Hashtable h = (Hashtable)pvts.elementAt(0);
		pvtParStatus = (String)h.get("PvtParStatus:Val");
	}		
	

%>

<table class="container" cellpadding="0">
	<tr >
		<td class="titreFiche">
		<fl:getMessage code="L61F9" alt="Contact"/>
		</td>
	</tr>
</table>



<table class="container" cellpadding="0">
		<tr class="ligne_separation">
			<td></td>
		</tr>
		<tr>
			<td class="column2">
			<table>
				<tr class="fieldline">
					<td class="dataLabel"><fl:getMessage code="F0046" alt="Société"/></td>
					<td class="dataValue">
						<% if (entID != null) { %>
				    	<a href="<fl:webapp/>/read_enterprise.fl?id=<%= entID %>" target="_blank"><span><%= Utils.formatToWeb(entCorpName, false) %></span></a>
						<% } else { %>
						&nbsp;
						<% } %>
					</td>
				</tr>
				<tr class="fieldline">
					<td class="dataLabel"><fl:getMessage code="F2012" alt="Adresse"/></td>
					<td class="dataValue"><%= address.length() == 0 ? "&nbsp;" : Utils.formatToWeb(address.toString(), false) %></td>
				</tr>
				<tr class="fieldline">
					<td class="dataLabel"><fl:getMessage code="F0049" alt="Tél."/></td>
					<td class="dataValue"><%= Utils.formatToWeb(entPhone, false) %></td>
				</tr>
				<tr class="fieldline">
					<td class="dataLabel"><fl:getMessage code="F2013" alt="Fax"/></td>
					<td class="dataValue"><%= Utils.formatToWeb(entFax, false) %></td>
				</tr>
				
			</table>
			</td>
	<td class="separation"><div class="separation"></div></td>
	<td class="column2">
		<table class="data_column" cellpadding="0">
			<tr class = "fieldline">
				<td class = "dataLabel"><fl:getMessage code="L61F9" alt="Contact"/></td>
				<td class="dataValue">
					<a href="<fl:webapp/>/read_person.fl?id=<%= perID %>" target="_blank"><span><%= Utils.formatToWeb(perCompleteName, false) %></span></a>
				</td>
			</tr>
			<tr class = "fieldline">
				<td class = "dataLabel"><fl:getMessage code="F2016" alt="Titre"/></td>
				<td class="dataValue"><%= Utils.formatToWeb(perTitle, false) %></td>
			</tr>
			<tr class = "fieldline">
				<td class = "dataLabel"><fl:getMessage code="F0049" alt="Tél."/></td>
				<td class="dataValue"><%= Utils.formatToWeb(perPhone, false) %></td>
			</tr>
			<tr class = "fieldline">
				<td class = "dataLabel"><fl:getMessage code="F2013" alt="Fax"/></td>
				<td class="dataValue"><%= Utils.formatToWeb(perFax, false) %></td>
			</tr>
			<tr class = "fieldline">
				<td class = "dataLabel"><fl:getMessage code="F201E" alt="e-mail"/></td>
				<td class="dataValue">
					<a href="mailto:<%= perMail %>"><%= Utils.formatToWeb(perMail, false) %></a>
				</td>
			</tr>
			<tr class = "fieldline">
					<td class = "dataLabel"><fl:getMessage code="F206F" alt="Etat"/></td>
					<td class="dataValue"><%= pvtParStatus %></td>
				</tr>
				<tr class = "fieldline">
					<td class = "dataLabel"><fl:getMessage code="L62B3" alt="Date détection"/></td>
					<td class="dataValue"><%= Utils.formatToWebDate(session, srp.getCrDt(), false) %></td>
				</tr>
		</table>
	</tr>
</table>
<%
}
%>
<table>
<%
String[][] query = {{"LseScriptID = " + Utils.formatToSQL(context, scriptID)}};
String[] fields = {"LseID" , "LseScriptElementID" , "LsePosition", "LsePageID", "LsePageID:ScpCode", "LsePageID:ScpTitre", "LsePageID:ScpPosition"};
String[] orderBy = {"LsePageID:ScpPosition", "LsePosition"};
String oldPageID = "";

Vector lseList = LinkScriptElementBean.listSummary(session, fields, query, orderBy);
if (lseList != null && lseList.size() > 0) 
{
%>
	<tr valign="baseline">
		<td colspan="5" class="titreFiche" style="font-size: small; font-weight: bold">
		<fl:getMessage code="L6149" alt="Réponses"/>
		</td>
	</tr>
<%
	for (int i=0 ; i < lseList.size() ; i++) {
		Hashtable line = (Hashtable) lseList.elementAt(i);
		String scriptElementID = (String) line.get("LseScriptElementID");
		Integer lsePosI = (Integer) line.get("LsePosition");
		String lseID = (String) line.get("LseID");
		String pageID = (String)line.get("LsePageID");
		String pageTitle = (String)line.get("LsePageID:ScpTitre");
		String pageCode = (String)line.get("LsePageID:ScpCode");
		int lsePosition = 99;
		if (lsePosI != null) {
			lsePosition = lsePosI.intValue();
		}
		String lsePos = Integer.toString(lsePosition);

		ScriptElementBean curScriptElement = null;
	
		try {
			curScriptElement = new ScriptElementBean(scriptElementID , session);
		} catch (AccessDeniedException ade) {
		} catch (FrontlineException fex) {
		}
		
		if (curScriptElement != null) {
			if (curScriptElement.getSceHasAnswers() != 1) {
				String qLabel = null;
				try {
					qLabel = curScriptElement.getSceLabel();
				} catch (FieldNotInitializedException fnie) {
				}
				boolean isTitle = false;
				if (qLabel != null) {
					int spaceIdx = qLabel.indexOf(" ");
					if (spaceIdx > -1) {
						String numberStr = qLabel.substring(0, spaceIdx);
						boolean probs = false;
						for (int j = 0; j < numberStr.length() && !probs; j++) {
							if (!Character.isDigit(numberStr.charAt(j)) && numberStr.charAt(j) != '.') {
								probs = true;
							}
						}
						isTitle = !probs;
					}
				}
				if (isTitle) {
%>
<%
				}
			} else {
				String qLabel = null;
				try {
					qLabel = curScriptElement.getSceLabel();
				} catch (FieldNotInitializedException fnie) {
				}
				String qTypeID = null;
				try {
					qTypeID = curScriptElement.getSceType();
				} catch (FieldNotInitializedException fnie) {
				}
				String qRefTableID = null;
				String qType = "FRE";
				if ( qTypeID != null && qTypeID.equals(dico.getRefIdByCode("SceType" , "REF")) ) {
					qType = "REF";
					try {
						qRefTableID = curScriptElement.getSceRefTable();
					} catch (FieldNotInitializedException fnie) {
					}
				}
				if ( qTypeID != null && qTypeID.equals(dico.getRefIdByCode("SceType" , "LST")) ) {
					qType = "LST";
				}
				String[][] querySer = {{"SerSrpID = " + Utils.formatToSQL(context, id), "SerLseID = " + Utils.formatToSQL(context, lseID)}};
				String[] orderBySer = {"SerInstance"};
				Vector listSer = ScriptElementResponseBean.list(session , querySer, orderBySer);
				if (listSer == null || listSer.size() == 0) {
					continue;
				}

				String replyDisplay = "";
				if (listSer != null && listSer.size() > 0) {
					for (int j=0 ; j < listSer.size() ; j++) {
						ScriptElementResponseBean curSer = (ScriptElementResponseBean) listSer.elementAt(j);
						if ("FRE".equals(qType)) { //- debut cas FRE
							String serText = null;
							try {
								serText = curSer.getSerText();
							} catch (FieldNotInitializedException fnie) {
							}
							replyDisplay += (serText != null ? Utils.formatToWeb(serText, false) : "") + (j < listSer.size() - 1 ? "<br>" : "");
						} //- fin cas FRE
						
						if ("LST".equals(qType)) { //- debut cas LST
							int serPosition = -1;
							try {
								serPosition = curSer.getSerPosition();
							} catch (FieldNotInitializedException fnie) {
							}
							String lstAnswer = null;
							try {
								Method getter = Class.forName("com.edeal.frontline.ScriptElementBean").getMethod("getSceLstAnswer" + serPosition , null);
								lstAnswer = (String) getter.invoke(curScriptElement , null);
							} catch (Exception e) {
								logger.warn("DBG: " + e.getClass().getName() + " : " + e.getMessage());
							}
							String serMoreText = null;
							try {
								serMoreText = curSer.getSerMoreText();
							} catch (FieldNotInitializedException fnie) {
							}
							if (lstAnswer != null) {
								replyDisplay += lstAnswer + (serMoreText != null ? " (" + serMoreText + ")" : "") + (j < listSer.size() - 1 ? "<br>" : "");
							}
						} //- fin cas LST
						
						if ("REF".equals(qType)) { //- debut cas REF
							String serRefValID = null;
							try {
								serRefValID = curSer.getSerRefValID();
							} catch (FieldNotInitializedException fnie) {
							}
							String serMoreText = null;
							try {
								serMoreText = curSer.getSerMoreText();
							} catch (FieldNotInitializedException fnie) {
							}
							String refAnswer = null;
							try {
								refAnswer = (String) dico.getRefDisp(session, qRefTableID , "Te1" , serRefValID);
							} catch (FrontlineException fe) {
							}
							if (refAnswer != null) {
								replyDisplay += refAnswer + (serMoreText != null ? " (" + serMoreText + ")" : "") + (j < listSer.size() - 1 ? "<br>" : "");
							}
						} //- fin cas REF
					}//fin du for
				}//fin du if
				// detection de la coupure
				boolean coupure = false;
				if (i == 0) {
					coupure = true;
				} else  if ("".equals(oldPageID)) {
					coupure = pageID != null;
				} else if (pageID == null) {
					coupure = !"".equals(oldPageID);
				} else {
					coupure = !oldPageID.equals(pageID);
				}
				if (coupure && pageID != null) {
					%>
				<tr class = "fieldline">
				<td class = "dataValue"><%= Utils.formatToWeb(pageCode, false) %><%= pageCode != null && pageTitle != null ? " - " : "" %><%= Utils.formatToWeb(pageTitle, false) %></td>
				</tr>
					<%
					oldPageID = pageID != null ? pageID : "";
				}
				if (!"".equals(replyDisplay)) { %>
				<tr>
				<td class = "dataLabel">
				<input type="hidden" name="<%= lsePos %>_LseID" value="<%= lseID %>">
				<%= qLabel == null ? "&nbsp;" : qLabel %>
				</td>
				<td class = "dataValue"><%= replyDisplay %></td>
				</tr>
<%
				}//if (!"".equals(replyDisplay))
			}//fin du if (curScriptElement.getSceHasAnswers() != 1) else
		}//fin du if (curScriptElement != null)
	}//fin du for chaque lse
}// fin du if il y a des lse

String srpNotes = null;
try {
	srpNotes = srp.getSrpNotes();
} catch (FieldNotInitializedException e) {}
if (srpNotes != null) { %>
<tr>
<td class="transparent">&nbsp;</td>
</tr>
<tr>
	<td colspan="2" class="normal" style="font-style: italic"><br><br><br></td>
</tr>
<% } %>
</table>
<% if (!isReportInclude) { %>
</fl:bigTabs>
<br>
<center><a href="javascript:window.close()"><img src="<fl:webapp/>/icons/ico/fermer_red_long.gif"></a></center>
<br>
</div>
</body>
</html>
<% } %>
