<%
/******************************************************************************
 * Copyright (c) 2000-2006 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.FlContext" %><%@ page import="com.edeal.frontline.Utils" %><%@ page import="com.edeal.frontline.navigation.FlRequest" %><%@ page import="com.edeal.frontline.PersonBean" %><%@ page import="com.edeal.frontline.FieldNotInitializedException" %><%@ page import="java.util.Vector" %><%@ page import="com.edeal.frontline.LinkBean" %><%@ page import="java.util.Hashtable" %><%@ page import="com.edeal.frontline.navigation.WebUtils" %><%@ page import="com.edeal.frontline.DataDictionary" %><%@ page import="com.edeal.frontline.controller.actions.SelectAction" %><%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl" %><%
	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();
	String id = flRequest.getParameter("id");
	String perID = flRequest.getParameter("perID");
	LinkBean Link;
	
	if (id != null) {
		Link = new LinkBean(id, session);
	} else {
		Link = new LinkBean(session);
	}
		
	String firstPer = null;
	try {
		firstPer = Link.getLinFirst();
	} catch (FieldNotInitializedException e) {
		if (perID != null && !"".equals(perID)) {
			firstPer = perID;
		 	Link.setLinFirst(perID);
		 }
	}
	
	String secondPer = null;
	try {
		secondPer = Link.getLinSecond();
	} catch (FieldNotInitializedException e) {
	}
	
	// Récupération personne
	String[] fields = { "PerID", "PerCivID", "PerName", "PerFstName" };
	String[][] query1 = {{ "PerID = " + Utils.formatToSQL(context, firstPer) }};
	Vector list1 = PersonBean.listSummary(session, fields, query1);
	Vector list2 = new Vector();
	if (secondPer != null) {
		String[][] query2 = {{ "PerID = " + Utils.formatToSQL(context, secondPer) }};
		list2 = PersonBean.listSummary(session, fields, query2);
	}
	StringBuffer firstCompleteName = new StringBuffer();
	if (list1 != null && list1.size() > 0) {
		Hashtable row = (Hashtable)list1.elementAt(0);
		if (row.get("PerCivID") != null) {
			firstCompleteName.append(dico.getRefDisp(session, "PerCivID", (String)row.get("PerCivID")));
			firstCompleteName.append(" ");
		}
		if (row.get("PerFstName") != null) {
			firstCompleteName.append(row.get("PerFstName"));
			firstCompleteName.append(" ");
		}
		if (row.get("PerName") != null) {
			firstCompleteName.append(row.get("PerName"));
		}
	}
	StringBuffer secondCompleteName = new StringBuffer();
	if (list2 != null && list2.size() > 0) {
		Hashtable row = (Hashtable)list2.elementAt(0);
		if (row.get("PerCivID") != null) {
			secondCompleteName.append(dico.getRefDisp(session, "PerCivID", (String)row.get("PerCivID")));
			secondCompleteName.append(" ");
		}
		if (row.get("PerFstName") != null) {
			secondCompleteName.append(row.get("PerFstName"));
			secondCompleteName.append(" ");
		}
		if (row.get("PerName") != null) {
			secondCompleteName.append(row.get("PerName"));
		}
	}
%>
<html>
<head>
	<title><fl:getMessage code="L30A4" alt="Edition liens"/></title>
	<link rel="stylesheet" type="text/css" href="<fl:webapp/>/css/common_css.jsp">
	<link rel="stylesheet" type="text/css" href="<fl:webapp/>/css/master_css.jsp">
	<script type="text/javascript" language="javascript" src="<fl:webapp/>/js/master_js.jsp"></script>
	<script language="javascript">
	
		
		function doChangePerson() {
			window.open('<fl:webapp/>/select.fl?type=Person&field=OppPerID&query=<%= WebUtils.urlEncode(context, "PerID <> " + Utils.formatToSQL(context, perID)) %>', 'ChoixPerson' , 'menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes,width=<%= SelectAction.SELECT_WINDOW_WIDTH %>,height=380');
		}
		
		function doLink() {
			var f = document.forms[0];
			if ( f.LinTypeB.value == '') {
				window.alert("<fl:getMessage code="M301A" alt="Veuillez spécifier un role" js="true"/>");
				f.LinTypeB.focus();
				return;
			}
			f.LinSecond.value = f.OppPerID.value;
            if (f.LinSecond.value == ''){
                 window.alert("<fl:getMessage code="L653C" alt="Veuillez spécifier une personne" js="true"/>");
             	 return;
            }
           
			if (f.LinFirst.value == f.LinSecond.value) {
				window.alert("<fl:getMessage code="M6054" alt="Vous ne devez pas choisir deux fois la même personne" js="true"/>");
				return;
			}
			f.$$returnedHtmlBody.value = 'Nothing here';
			f.$$returnedHtmlHead.value = '<sc' + 'ript language=\"javascript\">window.top.opener.lnkDoReturn(); window.top.close();</scr' + 'ipt>';

			f.submit();
		}
	</script>
</head>
<body>
<div id="object_css" style="top: 0px; left: 0px; bottom: 0px; height: 100%;">
<fl:bigTabs ignoreList="true" objectTitle="<%= Utils.getMessage(session, \"L6180\", false, true, \"Lien\")  %>">
<form method="post" action="<fl:webapp/>/save_link.fl">
	<input type="hidden" name="id" value="<%= id != null ? id : "" %>" />
	<input type="hidden" name="LinFirst" id="LinFirst" value="<%= firstPer %>"/>
	<input type="hidden" name="typ" value="Link" />
	<input type="hidden" name="$$returnedHtmlHead" value="" />
	<input type="hidden" name="$$returnedHtmlBody" value="" />
	<input type="hidden" name="$$presentFields" value="" />
	<table class="container" style="width: 100%;" border="0" cellpadding="0" cellspacing="0">
		<tr>
			<td class="column2">
				<table class="data_column" cellpadding="0">
					<tr class="fieldline">
						<td class="dataLabel" style="text-align: left; width: 33%;"><fl:label forInput="lblLinFirst" code="F002E" label="Personne"/></td>
						<td class="dataValue" style="width: 66%;">
							<input name="lblLinFirst" class="transRead" readonly value="<%= Utils.formatToWeb(firstCompleteName.toString(), true) %>">
						</td>
					</tr>
					<tr>
						<td id="LinSecond_label" class="dataLabel"><fl:label forInput="LinSecond" code="F002E" label="Personne"/></td>
						<td id="LinSecond_value" class="dataValue">
							<div class="fieldTable"  style="width: 100%;">
								<table class="fieldTable" cellpadding="0">
									<tr>
										<td class="principalText">
											<div><div style="width:75%;" class="fobLeftDiv">
												<input name="LinSecond" type="hidden" value="<%= Utils.formatToWeb(secondPer, true) %>">
												<input id="OppPerID" name="OppPerID" type="hidden" value="<%= Utils.formatToWeb(secondPer, true) %>">
												<input id="lblOppPerID" name="lblOppPerID" type="text" value="<%= Utils.formatToWeb(secondCompleteName.toString(), true) %>" autocomplete="off" onfocus="displayOnFocus(this);acResizeDisplayDiv(this);" class="input fob">
											</div>
											
												<a href="javascript:doChangePerson();">
												<img src="<fl:webapp/>/icons/ico/rechercher.gif" class="icon" alt="<fl:getMessage code="A2003" alt="Sélectionner"/>" title="<fl:getMessage code="A2003" alt="Sélectionner"/>">
												</a>
												<a href="javascript:var f = window.document.forms[0]; f.OppPerID.value = f.lblOppPerID.value = ''; acLastCompleteValues['lblOppPerID'] = '';acLastCompleteValues['OppPerID'] = ''; void('')">
												<img src="<fl:webapp/>/icons/ico/supprimer.gif" class="icon" alt="<fl:getMessage code="A3005" alt="Vider le champ"/>" title="<fl:getMessage code="A3005" alt="Vider le champ"/>">
												</a></div>
												<% String qr = "PerID <> " + Utils.formatToSQL(context,perID); %>
												<script language="javascript">var _OppPerID_query = '<%= Utils.formatToJavascript(qr) %>';runOnTime("installFobAC(document.forms[0].lblOppPerID);");</script>
										
										</td>
									</tr>
								</table>
							</div>
						</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 id="LinTypeB_label" class="dataLabel"><div style="width: 100%;"><label for="LinTypeB" title="<fl:getMessage code="F611F" alt="Relation"/>"><fl:getMessage code="F611F" alt="Relation"/></label></div></td>
						<td id="LinTypeB_label" class="dataValue">
							<div style="width: 100%;">
								<select id="LinTypeB" name="LinTypeB" class="input select" style="vertical-align: middle; width: 190px;">
								<% 
									Vector refList = dico.getRefList(session, "Rpe", "Te1");
									String LinTypeB;
									try {
										LinTypeB = Link.getLinTypeB();
									} catch (Exception e) {
										LinTypeB = "";
									}
									
									int rSize = refList.size();
									for (int cnt = 0; cnt < rSize; ++cnt) {
										Vector ref = (Vector)refList.elementAt(cnt);
										String refID = (String)ref.elementAt(0);
										String refValue = (String)ref.elementAt(1);
								%>
										<option value="<%= refID %>"<%= LinTypeB.equals(refID) ? " selected" : "" %>><%= refValue %></option>
								<% } %>
								</select>
							</div>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		
	</table>
</form>
</fl:bigTabs>
<br>
<table class="container" width="100%">
	<tr class="fieldline">
		<td colspan="2" style="text-align: center;">
			<a href="javascript:doLink()"><img src="<fl:webapp/>/icons/ico/valid_green_long.gif"></a>&nbsp;&nbsp;&nbsp;
			<a href="javascript:window.close()"><img src="<fl:webapp/>/icons/ico/fermer_red_long.gif"></a>
		</td>
	</tr>
</table>
</div>
</body>
</html>
