<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="com.edeal.frontline.controller.actions.ListAction"%><%
/******************************************************************************
 * 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 language="java"%>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="com.edeal.frontline.navigation.EdealBigTabsNavigation"%>
<%@page import="com.edeal.frontline.navigation.EdealBigTabsNavigation.BigTabsParams"%>
<%@page import="java.util.Vector"%>
<%@ page import="com.edeal.frontline.navigation.WebUtils" %>
<%@ page import="com.edeal.frontline.Utils" %>
<%@ page import="com.edeal.frontline.navigation.FlRequest" %>
<%@ page import="com.edeal.frontline.ListBean" %>
<%@ page import="java.util.HashMap" %>
<%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl"%><%
final org.apache.commons.logging.Log loggerTabs = org.apache.commons.logging.LogFactory.getLog(getClass());
FlRequest flRequestTabs = new FlRequest(request);
EdealBigTabsNavigation ebtnTabs = new EdealBigTabsNavigation(flRequestTabs);

// The grid variables are duplicated here with different names because
// this file can be @included by list.jsp and list_criteria.jsp

// List code
String lcTabs = ebtnTabs.getListCode(); 

//Search criteria
String scTabs = ebtnTabs.getBigTabsParameterValue(BigTabsParams.SearchCriteria);

//Object uri
String ouTabs = StringUtils.defaultString(ebtnTabs.getBigTabsParameterValue(BigTabsParams.ObjectURI));

//The current selected tab
String selectedTabTabs = ebtnTabs.getBigTabsParameterValue(BigTabsParams.SelectedTab);

//Criteria tab name
String critTitleTabs = ebtnTabs.getBigTabsParameterValue(BigTabsParams.CriteriaListTitle);
if (critTitleTabs == null) {
	critTitleTabs = Utils.getMessage(session, "A2009", false, true, "Nouvelle recherche");
}

// Criteria uri
String cuTabs = ebtnTabs.getBigTabsParameterValue(BigTabsParams.CriteriaListURI);

// Name of the current object
String objTitleTabs = ebtnTabs.getBigTabsParameterValue(BigTabsParams.ObjectTitle);

// id
String idTabs = flRequestTabs.getRequestParameterOrAttribute("id");

// $$idx
String idx = flRequestTabs.getRequestParameterOrAttribute(lcTabs + "_$$idx");
if (idx != null && !"".equals(idx)) {
	idx = lcTabs + "_$$idx=" + idx;
} else {
	idx = null;
}
 idx = flRequestTabs.getSearchCriteria("$$idx", lcTabs, false);
		
// $$OrderBy
String orderby = (String)flRequestTabs.getSearchCriteria("$$OrderBy",lcTabs,false);

if (orderby != null && !"".equals(orderby)) {
	orderby = lcTabs + "_$$OrderBy=" + orderby;
} else {
	orderby = null;
}
// $$ReverseOrderBy
String reverseorderby = flRequestTabs.getSearchCriteria("$$ReverseOrderBy",lcTabs,false);
if (reverseorderby != null && !"".equals(reverseorderby)) {
	reverseorderby = lcTabs + "_$$ReverseOrderBy=" + reverseorderby;
} else {
	reverseorderby = null;
}

// "ignoreCriteria" is used for historique reasons, but it's really the critList tab
boolean ignoreCritList = ebtnTabs.isIgnoreCritList();
boolean isCritListTabSelected = ebtnTabs.isCritListTabSelected();
boolean isObjectTabSelected = ebtnTabs.isObjectTabSelected();
boolean displayCritList = ebtnTabs.isDisplayCritList();
boolean displayObject = ebtnTabs.isDisplayObject();

if (!isCritListTabSelected && !isObjectTabSelected) {
	if (displayCritList && !displayObject) {
		isCritListTabSelected = true;
	} else if (!displayCritList && displayObject) {
		isObjectTabSelected = true;
	} else {
		isCritListTabSelected = true;
	}
}

int nbTabs = ebtnTabs.numberOfDisplayedTabs();

boolean isFirstDisplayed = false;
int displayedTabs = 0;
boolean sharpCorner = "sharp".equalsIgnoreCase(flRequestTabs.getRequestParameterOrAttribute("corner"));

String objListToken = ebtnTabs.getBigTabsParameterValue(BigTabsParams.ObjectListToken);
HashMap<String, Vector<String>> hm = objListToken == null ? null : (HashMap<String, Vector<String>>)session.getAttribute(ListAction.OBJECT_LIST_ATTR);
Vector<String> iterIds = hm == null ? null : hm.get(objListToken);

ebtnTabs.setBigTabsParameterValue(BigTabsParams.SelectedTab, "0");
String tab1URL = StringUtils.isNotEmpty(cuTabs) ? cuTabs : "/list_criteria.fl";
tab1URL = flRequestTabs.computeURL(tab1URL, false, false, true);
int tmpIndex  = tab1URL.indexOf("?");
if (tmpIndex < 0) {
	tab1URL += "?";
}else if (tmpIndex<tab1URL.length()){
	tab1URL += "&";
}
tab1URL += ebtnTabs.getBigTabsParameterString();

ebtnTabs.setBigTabsParameterValue(BigTabsParams.SelectedTab, "1");
String tab2URL = ouTabs;
tab2URL = flRequestTabs.computeURL(tab2URL, false, false, true);
tmpIndex  = tab2URL.indexOf("?");
if (tmpIndex < 0) {
	tab2URL += "?";
}else if (tmpIndex<tab2URL.length()){
	tab2URL += "&";
}
tab2URL += ebtnTabs.getBigTabsParameterString();

ebtnTabs.revertBigTabsParamsValue(EdealBigTabsNavigation.BigTabsParams.SelectedTab);

String prevUrl = null;
String prevID = null;
String nextUrl = null;
String nextID = null;
if (iterIds != null && iterIds.size() > 0 && StringUtils.isNotEmpty(idTabs) && ebtnTabs.isObjectTabSelected()) {
	int idIdx = iterIds.indexOf(idTabs);
	String strippedUrl = ouTabs.indexOf('?') >= 0 ? ouTabs.substring(0, ouTabs.indexOf('?')) : ouTabs;

	if (idIdx > 0) {
		prevID = iterIds.get(idIdx - 1);
		prevUrl = strippedUrl + "?id=" + prevID;
		prevUrl = flRequestTabs.computeURL(prevUrl, false, false, true);
		prevUrl += "&" + ebtnTabs.getBigTabsParameterString();
	}
	
	if (idIdx < (iterIds.size() - 1) && idIdx != -1) {
		nextID = iterIds.get(idIdx + 1);
		nextUrl = strippedUrl + "?id=" + nextID;
		nextUrl = flRequestTabs.computeURL(nextUrl, false, false, true);
		nextUrl += "&" + ebtnTabs.getBigTabsParameterString();
	}
}
%></script>
	<script language="javascript">
	<%if (!isCritListTabSelected) { %>
	function <%= lcTabs %>_gotoTab1() {
		var urlT1 = "<%= tab1URL %>&doList=1";
		
		try {
			urlT1 += "&<%= lcTabs %>_$$idx=" + <%= lcTabs %>_page;
		} catch (error) {
			urlT1 += "<%= idx == null ? "" : ("&" + idx) %>";
		}
		try {
			if (myGrid_<%= lcTabs %>) {
				if (myGrid_<%= lcTabs %>.getSortProperty("index") != -1 && myGrid_<%= lcTabs %>.getSortProperty("direction") != "none") {
					urlT1 += "&<%= lcTabs %>_";
					urlT1 += ( myGrid_<%= lcTabs %>.getSortProperty("direction") == "descending" ? "$$ReverseOrderBy=" : "$$OrderBy=" );
					urlT1 += myGrid_<%= lcTabs %>.getSortProperty("index");
				}
			}
		} catch (error) {
	<%	if (orderby != null) {	%>
			urlT1 += "&<%= orderby %>";
	<%	} else if (reverseorderby != null) {	%>
			urlT1 += "&<%= reverseorderby %>";
	<%	}	%>
		}
		
		$(".ongletgris a").attr("href",urlT1);
	}
	
	if (typeof $ != 'undefined') {
		$(document).ready(function(){
			<%= lcTabs %>_gotoTab1();
		});
	}
	
	<%}
	if (!isObjectTabSelected) {%>
	
	function <%= lcTabs %>_gotoTab2() {
		var urlT3 = "<%= tab2URL %>";
		
		try {
			urlT3 += "&<%= lcTabs %>_$$idx=" + <%= lcTabs %>_page;
		} catch (error) {
			urlT3 += "<%= idx == null ? "" : ("&" + idx) %>";
		}
		try {
			if (myGrid_<%= lcTabs %>) {
				if (myGrid_<%= lcTabs %>.getSortProperty("index") != -1 && myGrid_<%= lcTabs %>.getSortProperty("direction") != "none") {
					urlT3 += "&<%= lcTabs %>_";
					urlT3 += ( myGrid_<%= lcTabs %>.getSortProperty("direction") == "descending" ? "$$ReverseOrderBy=" : "$$OrderBy=" );
					urlT3 += myGrid_<%= lcTabs %>.getSortProperty("index");
				}
			}
		} catch (error) {
	<%	if (orderby != null) {	%>
			urlT3 += "&<%= orderby %>";
	<%	} else if (reverseorderby != null) {	%>
			urlT3 += "&<%= reverseorderby %>";
	<%	}	%>
		}
		
		$(".ongletgris a").attr("href",urlT3);
	}
	
	if (typeof $ != 'undefined') {
		$(document).ready(function(){
			<%= lcTabs %>_gotoTab2();
		});
	}
	
	<%}%>
	
	function previousInObjectList() {
		saveObjectBeforeLeaving('<%= prevUrl %>', '<%= prevID %>');
	}
	
	function nextInObjectList() {
		saveObjectBeforeLeaving('<%= nextUrl %>', '<%= nextID %>');
	}
	
	function saveObjectBeforeLeaving(urlToCall, nextID) {
		// If we're editing, then check to see if the user has changed the page (only in edit/creation)
		var windowHref = window.location.href;
		if (windowHref.indexOf('?') >= 0) {
			windowHref = windowHref.substring(0, windowHref.indexOf('?')); 
		}
		
		if ((windowHref.indexOf('edit') >= 0 || windowHref.indexOf('create') >= 0) && testForExitChanges) {
			doExitConf = false;
			testForExitChanges();
			
			if (doExitConf) {
				var conf = confirm('<fl:getMessage code="M6153" alt="Voulez vous sauver vos changements?" js="true"/>');
				doExitConf = false;
				if (conf) {
					try {
						var formID = $(':input[name="type"]').val() + '_form';
						$('#' + formID).attr('action','<fl:webapp/>/save_object_iteration_ajax.fl?nextID=' + nextID);
						submit();
					} catch (exception) {
						alert('Could not find form input \'type\': ' + exception);
					}
					return;
				}
			}
		}

		window.location = urlToCall;
	}
</script>
<table id="BTabs" class="container" cellpadding="0">
	<tr><% 
		if (displayCritList) { 
			isFirstDisplayed = true;
			displayedTabs++;
			String bgClass = isCritListTabSelected ? "actif-pos-1" : "inactif-pos-1";
		%>
		<td class="<%= bgClass %>" style="text-align: right">&nbsp;</td>
		<td class="<%= isCritListTabSelected ? "ongletblanc" : "ongletgris" %>"><% 
			if (!isCritListTabSelected) { 
			%><a><%= critTitleTabs %></a><% 
			}  else { 
			%><%= critTitleTabs %><% 
			} 
		%></td><% 
			if (displayedTabs == nbTabs) { 
		%>
		<td class="<%= isCritListTabSelected ? "actif-fin" : "inactif-fin"%>">&nbsp;</td><%
			 } else { 
		%>
		<td class="<%= bgClass = isCritListTabSelected ? "interActifInactif" :  "interInactifActif" %>" style="text-align: right">&nbsp;</td>
			<%}
		}

		if (displayObject) { 
			displayedTabs++;
			if (!isFirstDisplayed) { 
				isFirstDisplayed = true;
		%>
		<td style="text-align: right; width: 15px"><%
				if (isObjectTabSelected) {
			%><img src="<fl:webapp/>/icons/nav/principal_onglet/ong_blanc_pos_1.gif" style="width: 15px; height: 43px"><%
				} else {
			%><img src="<fl:webapp/>/icons/nav/principal_onglet/ong_grise_pos_1.gif" style="width: 15px; height: 43px"><%
				}
		%></td><%
			}
		%>
		<td class="<%= isObjectTabSelected ? "ongletblanc" : "ongletgris" %>">
			<% if (nbTabs > 1) { %>
				<% if (StringUtils.isNotEmpty(prevUrl)) { %>
					<a href="javascript:previousInObjectList()" title="<fl:getMessage code="L65FE" alt="Fiche précédent"/>" style="display: inline;"><img src="<fl:webapp/>/icons/ico/flecheG.gif" style=""></a>
				<% } %>
			<% }		

			if (!isObjectTabSelected) {
			%><a><%= objTitleTabs %></a><%
			} else {
			%><%= objTitleTabs %><%
			}

			if (nbTabs > 1) { %>
			<% if (StringUtils.isNotEmpty(nextUrl)) { %>
				<a href="javascript:nextInObjectList()" title="<fl:getMessage code="L65FD" alt="Fiche prochain"/>" style="display: inline;"><img src="<fl:webapp/>/icons/ico/flecheD.gif"></a>
			<% }
			} %>		
		</td>
		<td class="<%= isObjectTabSelected ? "actif-fin" : "inactif-fin" %>">
		<%
		}
		%>
		<td class="tabRightSpace" style="width: 90%; text-align: right; background: transparent url(<fl:webapp/>/icons/nav/principal_onglet/fd_cell.gif)">
			
		</td>
		<td class="<%= sharpCorner ? "finTabShort" : "finTab" %>" >&nbsp;</td>
	</tr>
</table>
