<%--
 * Copyright by Intland Software
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Intland Software. ("Confidential Information"). You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Intland.
 *
 * $Revision$ $Date$
--%>

<%@ taglib uri="jstl-c" prefix="c" %>
<%@ taglib uri="jstl-fmt" prefix="fmt" %>

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="uitaglib" prefix="ui" %>

<%@ page import="java.util.Map"%>
<%@ page import="com.intland.codebeamer.controller.form.SearchForm"%>

<%-- JSP fragment contains the advanced filter fields only --%>

<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
<!-- Hide script from old browsers

// initialize the search filters
function initSearchIn() {
	var searchInProjects = document.getElementById('searchInProjects');
	var searchInWorkingSet = document.getElementById('searchInWorkingSet');
	var projIdList = document.getElementById('projIdList');
	var wsIdList = document.getElementById('wsId');

	if (searchInProjects && searchInProjects.checked) {
		if (searchInWorkingSet) {
			// deselect all working sets
			for(i = 0, j=wsIdList.options.length; i < j; i++) {
				wsIdList.options[i].selected = false;
			}
		}
		// select all projects
		if (projIdList.selectedIndex == -1 && projIdList.length > 0) {
			for(i = 0, j=projIdList.options.length; i < j; i++) {
				projIdList.options[i].selected = true;
			}
		}
	} else if (searchInWorkingSet && searchInWorkingSet.checked) {
		if (searchInProjects) {
			// deselect all projects
			for(i = 0, j=projIdList.options.length; i < j; i++) {
				projIdList.options[i].selected = false;
			}
		}
		// select one working set
		if (wsIdList.selectedIndex == -1 && wsIdList.length > 0) {
			wsIdList.selectedIndex = 0;
		}
	}
}

YAHOO.util.Event.onDOMReady(initSearchIn);
// -->
</SCRIPT>

<%
	boolean associationMode = "true".equals(request.getParameter("associationMode"));
	Map searchCheckboxes = (Map)request.getAttribute("searchCheckboxes");
	if (associationMode) {
		// association currently can not be created for these, so removing checkboxes
		//searchCheckboxes.remove("Accounts");
		//searchCheckboxes.remove("Projects");
		searchCheckboxes.remove("Source symbols");
		searchCheckboxes.remove("Source commits");
		searchCheckboxes.remove("Tags");
	}
%>

<form:hidden path="advanced" />

<c:set var="colspan" value="4" />

<TABLE BORDER="0" CELLSPACING="1" CELLPADDING="4">

<TR>
	<TD nowrap="nowrap" WIDTH="200" CLASS="optional">&nbsp;Search for:&nbsp;</TD>

	<TD nowrap="nowrap">
		<form:input path="filter" title="Enter Full Text Search Pattern" size="60" maxlength="80"
			id="searchFilterPattern" onkeypress="return submitOnEnter(this,event)" />
		<input type="submit" class="button" title="Start Search" value="GO" />
		<BR/><form:errors path="filter" cssClass="invalidfield"/>
	</TD>
</TR>

<TR>
	<TD nowrap="nowrap" WIDTH="200" CLASS="optional">&nbsp;Syntax:&nbsp;</TD>

	<TD nowrap="nowrap">
		<form:checkbox path="luceneQuery" title="Allow advanced query syntax" />Allow query syntax
		<ui:helpLink helpURL="/help/search.do" title="Search help" /><BR/>
	</TD>
</TR>

<c:url var="spaceImgUrl" value="/images/space.gif" />

<TR VALIGN="top">
	<TD nowrap="nowrap" CLASS="optional">&nbsp;In Contents:&nbsp;</TD>

	<TD><TABLE WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0">
		<TR>
			<TD COLSPAN="${colspan}"><form:checkbox path="selectAll" title="Select/Clear All"
				onclick="setAllStatesFrom(this, 'searchOnArtifact')" />Select/Clear All</TD>
		</TR>

		<TR>
			<TD nowrap="nowrap" WIDTH="100%" COLSPAN="${colspan}"
				CLASS="toolheadsubline"><img src="${spaceImgUrl}" width="1" height="1" /></TD>
		</TR>

		<TR>
			<c:forEach var="checkbox" varStatus="forStatus" items="${searchCheckboxes}">
				<TD nowrap="nowrap">
				<form:checkbox path="searchOnArtifact" value="${checkbox.value}" />
				<c:choose>
				<c:when test="${! empty searchIcons[checkbox.key]}">
					<img src="<c:url value='${searchIcons[checkbox.key]}'/>" title="${checkbox.key}" class="margin-right:5px;">
					${checkbox.key}</img>
				</c:when>
				<c:otherwise>
					<span class="margin-right:5px;">${checkbox.key}</span>
				</c:otherwise>
				</c:choose>
				</TD>
				<c:if test="${(forStatus.count-1) % colspan == (colspan-1)}">
					</tr><tr>
				</c:if>
			</c:forEach>
		</TR>

		</TABLE>
	</TD>
</TR>

<TR VALIGN="top">
	<TD nowrap="nowrap" CLASS="optional">&nbsp;Search In:&nbsp;</TD>

	<TD>
		<DIV CLASS="toolheadsubline"><img src="${spaceImgUrl}" width="1" height="1" /></DIV>
		<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">

			<TR VALIGN="top">
				<TD>
					<form:radiobutton path="searchIn" value="<%=SearchForm.SEARCH_IN_PROJECTS%>" id="searchInProjects" onclick="initSearchIn();return true;" />
					&nbsp;Projects:&nbsp;<br/>
					<form:select path="projIdList" id="projIdList" multiple="true" size="${projSelectorSize}" cssClass="fixMiddleSelectWidth" onchange="document.getElementById('searchInProjects').checked='true';initSearchIn();return true;">
						<form:options items="${availableProjects}" itemValue="id" itemLabel="name"/>
					</form:select>
				</TD>

			<c:if test="${!isAnonymousUser}">
				<TD>&nbsp;&nbsp;&nbsp;</TD>

				<TD>
					<form:radiobutton path="searchIn" value="<%=SearchForm.SEARCH_IN_WORKING_SETS%>" id="searchInWorkingSet" onclick="initSearchIn();return true;" />
					&nbsp;Working Sets:&nbsp;<br/>
					<form:select path="wsId" id="wsId" multiple="true" size="${projSelectorSize}" cssClass="fixMiddleSelectWidth" onchange="document.getElementById('searchInWorkingSet').checked='true';initSearchIn();return true;">
						<form:options items="${availableWorkingSets}" itemValue="id" itemLabel="name"/>
					</form:select>
				</TD>
			</c:if>

			</TR>
		</TABLE>
	</TD>
</TR>

<TR>
	<TD nowrap="nowrap" CLASS="optional">&nbsp;Owner/Submitter:&nbsp;</TD>

	<TD><form:input path="ownerPattern" title="User pattern such as: name, company, email, address, phone ..."
		size="60" maxlength="80" />
		<br /><form:errors path="ownerPattern" cssClass="invalidfield"/> </TD>
</TR>

<TR>
	<TD CLASS="optional" nowrap="nowrap">
		 &nbsp;Created/Submitted:&nbsp;
	</TD>

	<TD nowrap="nowrap">

		<ui:duration property="createdAtDuration" value="${searchForm.createdAtDuration}" allowFutureDuration="false">
			&nbsp;After:
			<form:input path="createdAtFrom" size="12" maxlength="30" id="createdAtFrom" />&nbsp;
			<ui:calendarPopup textFieldId="createdAtFrom" otherFieldId="createdAtTo"/>

			&nbsp;Before: <form:input path="createdAtTo" size="12" maxlength="30" id="createdAtTo" />&nbsp;
			<ui:calendarPopup textFieldId="createdAtTo" otherFieldId="createdAtFrom"/>
		</ui:duration>
		<form:errors path="createdAtDuration" cssClass="invalidfield"/>
	</TD>
</TR>

<TR>
	<TD CLASS="optional" nowrap="nowrap">
		 &nbsp;Modified:&nbsp;
	</TD>

	<TD nowrap="nowrap">
		<ui:duration property="lastModifiedAtDuration" value="${searchForm.lastModifiedAtDuration}" allowFutureDuration="false">
			&nbsp;After:
			<form:input path="lastModifiedAtFrom" size="12" maxlength="30" id="lastModifiedAtFrom" />&nbsp;
			<ui:calendarPopup textFieldId="lastModifiedAtFrom" otherFieldId="lastModifiedAtTo"/>

			&nbsp;Before:
			<form:input path="lastModifiedAtTo" size="12" maxlength="30" id="lastModifiedAtTo" />&nbsp;
			<ui:calendarPopup textFieldId="lastModifiedAtTo" otherFieldId="lastModifiedAtFrom"/>
		</ui:duration>
		<form:errors path="lastModifiedAtDuration" cssClass="invalidfield"/>
	</TD>
</TR>

</TABLE>
