<!--#include file="template_sup.asp" -->
                <TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
                  <TBODY>
                    <TR>
                      <TD vAlign=center width=5><IMG
                  height=19 alt=""
                  src="images/woodsDayLink.gif" width=17
                  border=0> </TD>
                      <TD noWrap><B>Search results</B></TD>
                      <TD width="100%"> <HR SIZE=1> </TD>
                    </TR>
                  </TBODY>
                </TABLE>
                <TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
                  <TBODY>
                    <TR>
                      <TD width=1>&nbsp;</TD>
                      <TD width="100%">
                        <TABLE cellPadding=1 width="100%">
                          <TBODY>
                            <TR>
                              <TD vAlign=top>
<% ' First take care that the entry is correct or return the errors!
'------------------------------------------------------------------------------------------------------

'In case someone is using the old NameCache parameter

if len(Request("NameCache"))>0 then
	paraname="NameCache"
elseif len(Request("taxon"))>0 then
	paraname="taxon"
else
	paraname="name"
end if

' Allow multiples items to be search

errorcode=0
sql_or=""
for each name in Request(paraname)
	'Add * to the end if not existing
	if right(name,1) <> "*" then
		name=name & "*"
	end if

	'replace characters
	name=Replace(name, "*", "%")
	name=Replace(name, "+", " ")
	name=Replace(name, "'", "''")
	name=Replace(name, "  ", " ")
	name2=Left(name, InStr(name," ")) + Replace(name, " ", " % ", InStr(name," ")+1)
	sql_or=sql_or & "(Name.NameCache LIKE '"& name &"') OR (Name.NameCache LIKE '"& name2 &"') OR "

	'Error handling
	if Instr(name,"%") > 0 and Len(name) < 4 then
		errorcode=1
	end if
	if Instr(name,"%%") > 0 then
		errorcode=2
	end if
	if Mid(name,1,1) = "%" and Mid(name,4,1) = "%" then
		errorcode=3
	end if
	if len(name)<3 then
		errorcode=4
	end if

Next

if len(sql_or)<4 then
	errorcode=1
else
	'Delete the latest OR in the sql_or
	sql_or=left(sql_or,len(sql_or)-3)
end if


'-------- Here start the error handling ---------------
%>
<% if errorcode=1 then %>
	<p><font color="#990000"><strong>Input error 1. Please enter at least three letters with a wildcard</strong></font></p>
                                <p><a href="javascript:window.history.back()">Return</a></p>
<% elseif errorcode=2 then %>
	<p><font color="#990000"><strong>Input error 2. Please do not enter successive wildcards</strong></font></p>
								  <p><a href="javascript:window.history.back()">Return</a></p>
<% elseif errorcode=3 then %>
	<p><font color="#990000"><strong>Input error 3. Please enter at least three letters between wildcards</strong></font></p>
								  <p><a href="javascript:window.history.back()">Return</a></p>
<% elseif errorcode=4 then %>
	<p><strong><font color="#990000">Input error 4. Please enter at least three letters</font></strong></p>
								  <p><a href="javascript:window.history.back()">Return</a></p>
<% else %>

<%

'There was no problems in the input --------

'The SQL statement changes depending a Reference is selected or not.
If Request("RefId")<>"" then
	SQL=SQL&"SELECT DISTINCT  Name.NameCache AS Name FROM Name INNER JOIN PTaxon ON Name.NameId = PTaxon.PTNameFk WHERE ("& sql_or &") AND PTRefFk="& Request("RefId") & " ORDER BY Name.NameCache"
else
	SQL=SQL&"SELECT DISTINCT  Name.NameCache AS Name FROM Name INNER JOIN PTaxon ON Name.NameId = PTaxon.PTNameFk WHERE "& sql_or &" ORDER BY Name.NameCache"
end if
'Response.write(SQL)
Set Rspecie = Server.CreateObject("ADODB.Recordset")
Rspecie.ActiveConnection = MM_jun_STRING
Rspecie.Source = SQL
Rspecie.CursorType = 3
Rspecie.CursorLocation = 2
Rspecie.LockType = 1
Rspecie.Open()

%>
<%' if a reference is selected take his name from the database
if Request("RefId")<>"" then
	SQL="SELECT Refcache FROM reference WHERE RefId="& Request("RefId")
	Set Rname_ref = Server.CreateObject("ADODB.Recordset")
	Rname_ref.ActiveConnection = MM_jun_STRING
	Rname_ref.Source = SQL
	Rname_ref.CursorType = 3
	Rname_ref.CursorLocation = 2
	Rname_ref.LockType = 1
	Rname_ref.Open()
	name_ref=Rname_ref("RefCache")
	Rname_ref.close
	set Rname_ref=Nothing
end if
%>
<p><b><%=Rspecie.Recordcount%></b> names found. Click on name for details.
<%if Request("RefId")<>"" then%>
	Search restricted to: <%=name_ref%><br><br>
<%end if%>
</p>
<table width="100%" cellpadding="6" cellspacing="6">
                                  <tr>

<td valign="top">
                                      <p><b>Matches:</b></p>
                                      <!-- recordset start -->

                                      <% if Rspecie.RecordCount>0 then %> <font size=1>
                                      <% While NOT Rspecie.EOF %>
                                      <!-- record start -->
                                      <a href="PTaxonDetail.asp?NameCache=<%=Replace(Rspecie("Name"),chr(34),"%22")%>&PTRefFk=<%=Request("RefId")%>"><%=Replace(Rspecie("Name"),chr(34),"&quot;")%></a>
                                      <!-- record end -->
                                      <br>
									  <%
	Rspecie.Movenext
	Wend
	%>
                                      </font> <%else %> <br>
                                      There are not results matching your query.
                                      <%end if%> <%
Rspecie.close
set Rspecie=Nothing
%> </td>
                                      <!-- recordset end -->
                                  </tr>
                                </table>

<%
'end of the error in the input handled
end if %>

																</TD>
                              <TD vAlign=top noWrap
              align=right></TD>
                            </TR>
                          </TBODY>
                        </TABLE></TD>
                    </TR>
                  </TBODY>
                </TABLE>
<!--#include file="template_inf.asp" -->