<!--#include file="template_sup.asp" -->

<%

'-----------------------------------------------------------------------------------------------------------------------
' PTaxonDetail.asp - August 2003 - Javier de la Torre
'
' This file show information about one or more potential taxons. It can recieve different parameters and with them
' it constructs the SQL statements that are need to select all the PTaxon that match this criteria.
' When the PTaxon are selected then each one is represented inside a gray table. For each PTaxon there are many other
' connections to the database that had to be made to get all the needed information.
'
' Parameters:
' NameCache: String --> Cache field of the name table
' NameId: Integer --> Id of the Name of the potential taxons
' PTRefFk: integer --> Id of the reference of the potential taxons
'
'-----------------------------------------------------------------------------------------------------------------------

%>

<%


'Specify if the project is IOPI, HACK!!!!---------------------------------------------
'project="iopi"
'----------------------------------------------------------------------------------------
Dim herarchy()

Dim facts()

' Construct the SQL statement depending on the paramenter passed.

' That selects all the potential taxon.
'If the project is IOPI we have to select also the edition level of the potential taxon, and that makes the SQL statement different

if project="iopi" then

	SQL="SELECT Name.NameCache, Name.FullNameCache, Name.Genus, Name.SpeciesEpi, Name.InfraSpeciesEpi, PTaxon.PTRefFk, Name.NameId, Reference.RefCache, EditionLevel FROM Name INNER JOIN PTaxon ON Name.NameId = PTaxon.PTNameFk INNER JOIN Reference ON PTaxon.PTRefFk = Reference.RefId LEFT OUTER JOIN ReferenceEditionLevel ON Reference.RefId = ReferenceEditionLevel.RefId"

else

	SQL="SELECT Name.NameCache, Name.FullNameCache, Name.Genus, Name.SpeciesEpi, Name.InfraSpeciesEpi, PTaxon.PTRefFk, Name.NameId, Reference.RefCache FROM Name INNER JOIN PTaxon ON Name.NameId = PTaxon.PTNameFk INNER JOIN Reference ON PTaxon.PTRefFk = Reference.RefId"



end if



	'All potential taxon using a specific NameId

	if Request("NameId")<>"" then

		SQL=SQL&" WHERE Name.NameId = "& Request("NameId")

		if Request("PTRefFk")<>"" then

			SQL=SQL&" AND PTRefFk="& Request("PTRefFk")
		else
			Response.write("The actual WebPublisher doesn't support to display names not use in a potential taxon, sorry.")
			Response.end
		end if

	else

		SQL=SQL&" WHERE Name.NameId > 0"

		if Request("NameCache")<>"" then

			SQL=SQL&" AND Name.NameCache = '"& Replace(Request("NameCache"),"'","''") &"'"

	'		SQL=SQL&" AND Name.NameCache = '"& Request("NameCache") "%'"



		else

			SQL=SQL&" AND Name.Genus IS NULL"

		end if

		if Request("PTRefFk")<>"" then

			SQL=SQL&" AND PTRefFk="& Request("PTRefFk")

		end if

	end if

	'We order them finally by the Edition Level, the references allready edited should be show first.

if project="iopi" then

	SQL=SQL&" ORDER BY dbo.ReferenceEditionLevel.EditionLevel DESC, Reference.OrderApparence DESC"
else
	SQL=SQL&" ORDER BY Reference.OrderApparence DESC"

end if

'Response.write(SQL)

'This recordset returns all the potential taxon that match the name criteria and order them by source.

Set Recordset1 = Server.CreateObject("ADODB.Recordset")

Recordset1.ActiveConnection = MM_jun_STRING

Recordset1.Source = SQL

Recordset1.CursorType = 3

Recordset1.CursorLocation = 2

Recordset1.LockType = 1

Recordset1.Open()





if Request("NameCache")<>"" then

	NameTotal=Request("NameCache")

else

	If Recordset1.Recordcount>0 then

		NameTotal=Recordset1("NameCache")

	end if

end if

%>

                <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>Details for: <i><%=NameTotal%></i></B></TD>

                      <TD width="100%"> <HR SIZE=1> </TD>

                    </TR>

                  </TBODY>

                </TABLE>

<%

' Show the google/tropicos/ipni links only if is not a printer-frendly request

If Recordset1.Recordcount>0 then
	IPNI_link = "| <a href=""http://www.ipni.org/ipni/plantsearch?find_isAPNIRecord=on&find_isGCIRecord=on&find_isIKRecord=on&output_format=normal&query_type=by_query&back_page=plantsearch"
	if Recordset1("Genus")<>"" then
		IPNI_link=IPNI_link+"&find_genus="+Recordset1("Genus")
	end if
	if Recordset1("SpeciesEpi")<>"" then
		IPNI_link=IPNI_link+"&find_species="+Recordset1("SpeciesEpi")
	end if
	if Recordset1("InfraSpeciesEpi")<>"" then
		IPNI_link=IPNI_link+"&find_infraspecies="+Recordset1("InfraSpeciesEpi")
	end if
	IPNI_link = IPNI_link + " ""><strong>IPNI</strong></a>"
else
	IPNI_link = ""
end if

if Request("Print")<>1 then %>

<div align="right">
	<a href="http://mobot.mobot.org/cgi-bin/search_vast?name=<%=NameTotal%>" target="_blank"><strong>Tropicos</strong></a>
	<%=IPNI_link%>
	| <a href="http://images.google.com/images?as_q=<%=NameTotal%>&num=20&btnG=Google%2BSearch&as_epq=&hl=&as_oq=&as_eq=&imgsz=&as_filetype=&imgc=&as_sitesearch=&imgsafe=active" target="_blank"><strong>Google Images</strong></a>
</div>
<br>

<%end if%>


<%

'take the names of the different sources to create the navigation menu in the top

Dim Sources

ReDim Sources(0)

s=0

While not Recordset1.eof

	sources(s)=Recordset1("RefCache")

	Redim Preserve sources(s+1)

	s=s+1

	Recordset1.Movenext

wend





Recordset1.MoveFirst



Dim NameA(7)



If Recordset1.Recordcount > 0 then

	cont_sources=0

	While NOT Recordset1.EOF

		'counter for each movement

		cont_sources=cont_sources+1

		'I first obtain the information that belongs to this potential taxon:

		'IOPI!SQL="SELECT PTaxon.PTNameFk, PTaxon.PTRefFk, Status.Status, PTaxon.StatusFk, Reference.NomTitleAbbrev, Reference.RefCache, PTaxon.Notes, Type.TypeCache, Type.TypeId, Rank.Rank FROM Rank RIGHT JOIN ((Reference RIGHT JOIN (Name RIGHT JOIN (PTaxon LEFT JOIN Status ON PTaxon.StatusFk = Status.StatusId) ON Name.NameId = PTaxon.PTNameFk) ON Reference.RefId = PTaxon.PTRefFk) LEFT JOIN Type ON Name.TypeId = Type.TypeId) ON Rank.RankId = Name.RankFk WHERE (((PTaxon.PTNameFk)="&Recordset1("NameId")&") AND ((PTaxon.PTRefFk)="&Recordset1("PTRefFk")&"))"

		SQL="SELECT PTaxon.PTNameFk, PTaxon.DoubtfulFlag, PTaxon.PTRefFk, Status.Status, PTaxon.StatusFk, Reference.NomTitleAbbrev, Reference.RefCache, PTaxon.Notes, Rank.Rank, Rank.RankId FROM Rank RIGHT JOIN ((Reference RIGHT JOIN (Name RIGHT JOIN (PTaxon LEFT JOIN Status ON PTaxon.StatusFk = Status.StatusId) ON Name.NameId = PTaxon.PTNameFk) ON Reference.RefId = PTaxon.PTRefFk) ) ON Rank.RankId = Name.RankFk WHERE (((PTaxon.PTNameFk)="&Recordset1("NameId")&") AND ((PTaxon.PTRefFk)="&Recordset1("PTRefFk")&"))"

		Set Rptaxon = Server.CreateObject("ADODB.Recordset")

		Rptaxon.ActiveConnection = MM_jun_STRING

		Rptaxon.Source = SQL

		Rptaxon.CursorType = 3

		Rptaxon.CursorLocation = 2

		Rptaxon.LockType = 1

		Rptaxon.Open()



		'First select all the information posible from this name

		SQL="SELECT Name.NameCache, Name.FullNameCache, Name.NameId, Name.Genus, Name.SpeciesEpi, Name.InfraSpeciesEpi, Name.GenusSubdivisionEpi, Reference.NomTitleAbbrev, Reference.Volume, RefDetail.Details, Reference.InRefFk, Reference.RefYear, RefDetail.FullNomRefCache, Name.AuthorTeamFk, Name.ExAuthorTeamFk, Name.BasAuthorTeamFk, Name.ExBasAuthorTeamFk, Rank.RankAbbrev, NomStatus.NomStatus, Name.Notes, Name.UnnamedNamePhrase, Name.RankFk FROM NomStatus RIGHT JOIN ((((Name LEFT JOIN Reference ON Name.NomRefFk = Reference.RefId) LEFT JOIN RefDetail ON Name.NomRefDetailFk = RefDetail.RefDetailId) INNER JOIN Rank ON Name.RankFk = Rank.RankId) LEFT JOIN NomStatusRel ON Name.NameId = NomStatusRel.NameFk) ON NomStatus.NomStatusId = NomStatusRel.NomStatusFk WHERE (((Name.NameId)="&Rptaxon("PTNameFk")&"))"

		Set Rname = Server.CreateObject("ADODB.Recordset")

		Rname.ActiveConnection = MM_jun_STRING

		Rname.Source = SQL

		Rname.CursorType = 3

		Rname.CursorLocation = 2

		Rname.LockType = 1

		Rname.Open()





		'0--> Name Part (Juncus Roemeriani)

		'1--> Authors string (L.)

		'2--> Complete Reference starting with ", " (, Byull. Moskovsk. Obshch. Isp. Prir., Otd. Biol., 95: 117 (1990))

		'3--> status part (,nom. illeg.)

		'4--> non part (,non Buchenau(1545))

		'5--> total, all in the same 0+1+2+3+4

		'6--> 0+1

		'7--> Name notes



		if Rname("notes")="ll" then

			'if there's something in the notes field i will use this as the complete name. This solution is valid for the IOPI project

			NameA(0)=Rname("FullNameCache")

			NameA(1)="Not parsed, see the notes"

			NameA(2)="Not parsed, see the notes"

			NameA(3)="Not parsed, see the notes"

			NameA(4)="Not parsed, see the name"

			NameA(5)=Rname("FullNameCache")

			NameA(6)=Rname("FullNameCache")

			NameA(7)=Rname("Notes")

		else

			'I have to add the status of the name (nomenclature status). It's possible to have more than one status, but maximum here is 2

			If Rname("NomStatus")<>"" then

				status_part= Rname("NomStatus")

				if Rname.Recordcount > 1 then

					'There's more than one status

					Rname.movenext

					status_part=status_part & ", " & Rname("NomStatus")

				End if

			End if



			NameA(0)=Rname("NameCache")

			NameA(1)=Replace(Rname("FullNameCache"),Rname("NameCache"),"")

			NameA(2)=Rname("FullNomRefCache")

			NameA(3)=status_part

			NameA(4)=non_part

			NameA(5)=Rname("FullNameCache") &", "&Rname("FullNomRefCache")

			NameA(6)=Rname("FullNameCache")

			NameA(7)=Rname("Notes")



		end if

	Rname.close

	set Rname=Nothing



%>

<%

' Show the reference where is restricted the search.

if Request("PTRefFk")<>"" then%>

Search restricted to <%=Rptaxon("RefCache")%>&nbsp;&nbsp;&nbsp;<a href="<%=Request.ServerVariables("URL")%>?NameCache=<%=NameA(0)%>">Check for other Sources using this name</a><br><br>

<%else%>

<% 'only show the navigation menu of different sources one time:

	if cont_sources = 1 then %>

		<strong><%=ubound(sources)%> different source(s) use this name:</strong>

		<br>

		<br>

		<% for i=0 to (ubound(sources)-1) %>

			- <a href="#<%=i+1%>"><%=sources(i)%></a><br>

		<%next%>

	<%end if%>

	<br>

<%end if%>

<% 'If we are in the IOPI project we change the colour of the table if the edition level for this Reference is set to 1

if Project="iopi" then

	if Recordset1("EditionLevel")=1 then

		table_color="#FFFFF2"

	else

		table_color="#F8F8F8"

	end if

else

	table_color="#F8F8F8"

end if


'In the IOPI project if the edition level of the Reference is set to 1 then we add this string to
'the reference

ReferenceSuffixForIOPI=""
if Project="iopi" then
	if Recordset1("EditionLevel")=1 then
		ReferenceSuffixForIOPI=" [IOPI preferred view]"
	end if
end if

%>

                <table width="100%" border="1" cellspacing="4" cellpadding="0" bordercolor="#666666" style="border-collapse: collapse">

                  <tr>

                    <td><a name="<%=cont_sources%>"></a>

					<TABLE width="100%" cellspacing="6" bgcolor="<%=table_color%>" >

                        <TBODY>

                          <TR>

                            <TD width="16%" valign="top"><strong>Source:</strong></TD>

                            <TD width="84%"><strong><font color="#990000"><%=Rptaxon("Refcache")%><%=ReferenceSuffixForIOPI%><br><br></font></strong></td>

                          </TR>

                          <TR>

                            <TD width="16%" valign="top"><STRONG>Name:</STRONG></TD>

                            <TD width="84%"><B><%=NameA(6)%></B></TD>

                          </TR>

                          <% if len(NameA(2)) > 0 then %>

						  <TR>

                            <TD width="16%" valign="top"><STRONG>Nomencl. ref.</STRONG></TD>

                            <TD><I><B><%=NameA(2)%></B></I></TD>

                          </TR>

						  <%end if %>

						  <% if len(NameA(3)) > 0 then %>

                          <TR>

                            <TD width="16%" valign="top"><STRONG>Nomencl. status(es)</STRONG></TD>

                            <TD><%=NameA(3)%></I></TD>

                          </TR>

						  <%end if %>

<%



	' Search if the name has HOMONYMS and print them

	'------------------------------------------------------------------------------------------

	SQL="SELECT NameFk1 FROM RelName WHERE NameFk2=" & Rptaxon("PTNameFk") &" AND RelNameQualifierFk=2"

	set Rhom = Server.CreateObject("ADODB.Recordset")

	Rhom.ActiveConnection = MM_jun_STRING

	Rhom.Source = SQL

	Rhom.CursorType = 3

	Rhom.CursorLocation = 2

	Rhom.LockType = 1

	Rhom.Open()

	if Rhom.Recordcount > 0 then



		'If it has homonyms show them, take the details.

		SQL="SELECT Name.NameId, Name.NameCache, Name.FullNameCache, Reference.RefYear FROM Name LEFT JOIN Reference ON Name.NomRefFk = Reference.RefId WHERE (Name.NameId = "& Rhom("NameFk1") &")"

		set Rhom_details = Server.CreateObject("ADODB.Recordset")

		Rhom_details.ActiveConnection = MM_jun_STRING

		Rhom_details.Source = SQL

		Rhom_details.CursorType = 3

		Rhom_details.CursorLocation = 2

		Rhom_details.LockType = 1

		Rhom_details.Open()

		While Not Rhom_details.EOF

			non_part= Rhom_details("FullNameCache")

			if Rhom_details("RefYear")<>"" then

				non_part=non_part & " (" & Rhom_details("RefYear") & ")"

			end if

%>

						  <TR>

                            <TD width="16%" valign="top"><STRONG>Homonyms</STRONG></TD>

                            <TD><%=non_part%></TD>

                          </TR>

<%

	Rhom_details.MoveNext

	wend

	Rhom_details.close()

	set Rhom_details = Nothing

	End if

	Rhom.close

	set Rhom = Nothing

	'---------------------------------------------------------------------------------------------

%>

                          <TR>

                            <TD width="16%" valign="top"><STRONG>Rank:</STRONG></TD>

                            <TD><%=Rptaxon("Rank")%></TD>

                          </TR>

<!-- IOPI!

                          <%'if Rptaxon("TypeCache")<>"" then %>

						  <TR>

                            <TD width="16%" vAlign=top><strong>Type:</strong></TD>

                            <TD><%'=Rptaxon("TypeCache")%> (<%'=Rptaxon("TypeId")%>)</TD>

                          </TR>

						  <%'end if%>

-->

<% ' status of the taxon ---------------------------------------------------------------------------%>

                          <TR>

                            <TD width="16%" valign="top"><STRONG>Status:</STRONG></TD>

                            <TD>

							  <%if Rptaxon("StatusFk")=1 then%>

									<font color=green>

                              <%else%>

							  		<font color=red>

                              <%end if%>
                              <%
                              ' If the DoubtfulFlag is set to d then show a PRELIMARY text in fron of the status
                              if Rptaxon("DoubtfulFlag")="d" then%>PRELIMINARY<% end if %>

                              <b><%=ucase(Rptaxon("Status"))%></b></font></TD>

                          </TR>

<%

'Look to see if it's a basyonym of something and print it. BASYONYM OF

'------------------------------------------------------------------------------------------------------

SQL="SELECT dbo.Name.NameId, Name_1.NameId AS NameIdBas, Name_1.FullNameCache FROM dbo.Name Name_1 INNER JOIN Name INNER JOIN RelName ON Name.NameId = RelName.NameFk1 ON Name_1.NameId = RelName.NameFk2 WHERE (((Name.NameId)="&Rptaxon("PTNameFk")&") AND ((RelName.RelNameQualifierFk)=1))"

Set Rbas = Server.CreateObject("ADODB.Recordset")

Rbas.ActiveConnection = MM_jun_STRING

Rbas.Source = SQL

Rbas.CursorType = 3

Rbas.CursorLocation = 2

Rbas.LockType = 1

Rbas.Open()

if Rbas.RecordCount > 0 then

%>

                          <TR>

                            <TD width="16%" valign="top"><STRONG>Is basionym for:</STRONG></TD>

                            <TD><A            href="PTaxonDetail.asp?NameId=<%=Rbas("NameIdBas")%>"><%=Rbas("FullNameCache")%></A></TD>

                          </TR>

                          <%

end if

Rbas.close

set Rbas=Nothing

'-------------------------------------------------------------------------------------------------------

%>

<%' See where this taxon it's included. HERARCHY

'-------------------------------------------------------------------------------------------------------

'only check herarchy if the PTaxon is accepted, if not is not supossed to be linked to anything (integrity rule)



if Rptaxon("StatusFk")=1 then



	'First we will store in an array all the herarchy in inverse order, then they will be shown. This array will

	' be three dimensional, the first dimension will store the nameid, the second the refid and the tirth

	' the namecache field. The very first is an index.

	nameid=Rptaxon("PTNameFk")

	refid=Rptaxon("PTRefFk")



	ReDim herarchy(3,0)

	control=1

	cont=0

	While control=1

		SQL="SELECT PTaxon_1.PTNameFk, PTaxon_1.PTRefFk, Name.NameCache, Name.FullNameCache, Reference.RefCache, Name.Notes AS NameNotes, Rank.Rank"&_

		" FROM (PTaxon AS PTaxon_1 INNER JOIN Name ON PTaxon_1.PTNameFk = Name.NameId) INNER JOIN (PTaxon INNER JOIN RelPTaxon ON (PTaxon.PTNameFk = RelPTaxon.PTNameFk1) AND (PTaxon.PTRefFk = RelPTaxon.PTRefFk1)) ON (PTaxon_1.PTNameFk = RelPTaxon.PTNameFk2) AND (PTaxon_1.PTRefFk = RelPTaxon.PTRefFk2) INNER JOIN Reference ON PTaxon_1.PTRefFk = Reference.RefId INNER JOIN Rank ON Name.RankFk = Rank.RankId"&_

		" WHERE (((PTaxon.PTNameFk)="&nameid&") AND ((PTaxon.PTRefFk)="&refid&") AND ((RelPTaxon.RelQualifierFk)=1)) ORDER BY Name.NameCache"

		Set Rherar = Server.CreateObject("ADODB.Recordset")

		Rherar.ActiveConnection = MM_jun_STRING

		Rherar.Source = SQL

		Rherar.CursorType = 3

		Rherar.CursorLocation = 2

		Rherar.LockType = 1

		Rherar.Open()

		if Rherar.Recordcount > 0 then

			nameid=Rherar("PTNameFk")

			refid=Rherar("PTRefFk")

			Redim Preserve herarchy(3,cont)

			herarchy(0,cont)=nameid

			herarchy(1,cont)=refid

			'Show the reference as "sec." when the reference is different from the one using the potential taxon

			if Rherar("PTRefFk")<> refid then

				herarchy(2,cont)="<strong>"&Rherar("FullNameCache") & "</strong> sec. " & Rherar("RefCache")

			else

				herarchy(2,cont)="<strong>"&Rherar("FullNameCache") & "</strong>"

			end if

			herarchy(3,cont)=Rherar("Rank")



			cont=cont+1

		else

			nameid=""

			refid=""

			control=0

		end if

		Rherar.close

		set Rherar=Nothing

	Wend

	If herarchy(0,0)<>"" then

	%>



							  <TR>

								<TD width="16%" valign="top"><STRONG>Taxonomy (this taxon is included in):</STRONG></TD>

								<TD>

								<%

								col=0

								for i=ubound(herarchy,2) to 0 step -1

								%>

								<%=herarchy(3,i)%> - <A href="PTaxonDetail.asp?NameId=<%=herarchy(0,i)%>&PTRefFk=<%=herarchy(1,i)%>"><%=herarchy(2,i)%></A><br><%for c=0 to col%>&nbsp;&nbsp;&nbsp;&nbsp;<%next%>

								<%

								col=col+1

								next %>

								</TD>

							  </TR>



	<%

	end if

	Redim herarchy(0)

	herarchy(0)=""

end if

'----------------------------------------------------------------------------------------------------------

%>

<%

'Look for incluided taxa only if the potential taxon is accepted. INCLUDED TAXA, HERARCHY DOWN

'-------------------------------------------------------------------------------------------------------------------

if Rptaxon("StatusFk")=1 then

	SQL="SELECT PTaxon_1.PTNameFk, PTaxon_1.PTRefFk, Name.FullNameCache, Reference.RefCache, Reference.RefId AS RefId "&_

	"FROM Reference INNER JOIN ((PTaxon AS PTaxon_1 INNER JOIN (PTaxon INNER JOIN RelPTaxon ON (PTaxon.PTNameFk = RelPTaxon.PTNameFk2) AND (PTaxon.PTRefFk = RelPTaxon.PTRefFk2)) ON (PTaxon_1.PTNameFk = RelPTaxon.PTNameFk1) AND (PTaxon_1.PTRefFk = RelPTaxon.PTRefFk1)) INNER JOIN Name ON PTaxon_1.PTNameFk = Name.NameId) ON Reference.RefId = PTaxon_1.PTRefFk "&_

	"WHERE (((PTaxon.PTNameFk)="&Rptaxon("PTNameFk")&") AND ((PTaxon.PTRefFk)="&Rptaxon("PTRefFk")&") AND ((RelPTaxon.RelQualifierFk)=1)) AND (RelPTaxon.PTRefFk1=RelPTaxon.PTRefFk2) "&_

	"ORDER BY Name.FullNameCache"

	Set Rinclu = Server.CreateObject("ADODB.Recordset")

	Rinclu.ActiveConnection = MM_jun_STRING

	Rinclu.Source = SQL

	Rinclu.CursorType = 3

	Rinclu.CursorLocation = 2

	Rinclu.LockType = 1

	Rinclu.Open()

	if Rinclu.Recordcount>0 then

		contador=0

		While NOT Rinclu.EOF

		contador=contador+1

%>

                          <TR>

                            <TD width="16%" valign="top"><%if contador=1 then%> <STRONG>Included taxa:</STRONG>

                              <%end if%></TD>

                            <TD><A href="PTaxonDetail.asp?NameId=<%=Rinclu("PTNameFk")%>&PTRefFk=<%=Rinclu("PTRefFk")%>"><strong><%=Rinclu("FullNameCache")%></strong><%if Rinclu("PTRefFk")<>Rptaxon("PTRefFk") then%> sec. <%=Rinclu("RefCache")%><%end if%></A></TD>

                          </TR>

<%

		Rinclu.Movenext

		Wend

		Rinclu.Close

		set Rinclu=Nothing

	end if

end if

'--------------------------------------------------------------------------------------------------------------------------

%>

<%

' If the taxon is a synonymum find the accepted name. ACCEPTED NAME

'---------------------------------------------------------------------------------------------------

if Rptaxon("StatusFk")=2 then

	SQL="SELECT PTaxon_1.PTNameFk, PTaxon_1.PTRefFk, Name.NameCache, Name.FullNameCache, Name.Notes AS NameNotes"&_

	" FROM (PTaxon AS PTaxon_1 INNER JOIN Name ON PTaxon_1.PTNameFk = Name.NameId) INNER JOIN (PTaxon INNER JOIN RelPTaxon ON (PTaxon.PTNameFk = RelPTaxon.PTNameFk1) AND (PTaxon.PTRefFk = RelPTaxon.PTRefFk1)) ON (PTaxon_1.PTNameFk = RelPTaxon.PTNameFk2) AND (PTaxon_1.PTRefFk = RelPTaxon.PTRefFk2)"&_

	" WHERE (((PTaxon.PTNameFk)="&Rptaxon("PTNameFk")&") AND ((PTaxon.PTRefFk)="&Rptaxon("PTRefFk")&") AND (RelPTaxon.RelQualifierFk in (2,4,5,6,7))) ORDER BY Name.NameCache"

	Set Racc = Server.CreateObject("ADODB.Recordset")

	Racc.ActiveConnection = MM_jun_STRING

	Racc.Source = SQL

	Racc.CursorType = 3

	Racc.CursorLocation = 2

	Racc.LockType = 1

	Racc.Open()

	if Racc.Recordcount>0 then

		contador=0

		While NOT Racc.EOF

		contador=contador+1

%>

                          <TR>

                            <TD width="16%" valign="top"><%if contador=1 then%>

                              <STRONG>Synonym of:</STRONG>

                              <%end if%></TD>

                            <TD><A href="PTaxonDetail.asp?NameId=<%=Racc("PTNameFk")%>&PTRefFk=<%=Racc("PTRefFk")%>"><%=Racc("FullNameCache")%></A></TD>

                          </TR>

<%

		Racc.Movenext

		Wend

		Racc.Close

		set Racc=Nothing

	end if

end if

'---------------------------------------------------------------------------------------------------------------

%>

<%

'Look to see if it has a basyonym of something. BASYONYM

'-----------------------------------------------------------------------------------------------------------------

SQL="SELECT dbo.Name.NameId, Name_1.NameId AS NameIdBas, Name_1.FullNameCache FROM Name Name_1 INNER JOIN Name INNER JOIN RelName ON Name.NameId = RelName.NameFk2 ON Name_1.NameId = RelName.NameFk1 WHERE (((Name.NameId)="&Rptaxon("PTNameFk")&") AND ((RelName.RelNameQualifierFk)=1))"

Set Rbas = Server.CreateObject("ADODB.Recordset")

Rbas.ActiveConnection = MM_jun_STRING

Rbas.Source = SQL

Rbas.CursorType = 3

Rbas.CursorLocation = 2

Rbas.LockType = 1

Rbas.Open()

if Rbas.RecordCount > 0 then

%>

	<TR>
		<TD width="16%" valign="top"><STRONG>Basionym:</STRONG></TD>
		<TD><STRONG><%=Rbas("FullNameCache")%></STRONG></TD>
	</TR>

<%

end if

Rbas.close

set Rbas=Nothing

'----------------------------------------------------------------------------------------------------------------

%>

<%

'Look for synonyms only if the potential taxon is accepted. SYNONYMS

'----------------------------------------------------------------------------------------------------------------

if Rptaxon("StatusFk")=1 then

	SQL="SELECT PTaxon_1.PTNameFk, PTaxon_1.PTRefFk AS PTRefFk_s, Name.NameCache, Name.FullNameCache, Name.Notes AS NameNotes, PTaxon.PTRefFk AS PTRefFk, RelPTQualifier.RelPTQualifier"&_

	" FROM (PTaxon AS PTaxon_1 INNER JOIN (PTaxon INNER JOIN RelPTaxon ON (PTaxon.PTNameFk = RelPTaxon.PTNameFk2) AND (PTaxon.PTRefFk = RelPTaxon.PTRefFk2)) ON (PTaxon_1.PTNameFk = RelPTaxon.PTNameFk1) AND (PTaxon_1.PTRefFk = RelPTaxon.PTRefFk1)) INNER JOIN Name ON PTaxon_1.PTNameFk = Name.NameId INNER JOIN RelPTQualifier ON RelPTaxon.RelQualifierFk = RelPTQualifier.RelPTQualifierId "&_

	" WHERE (((PTaxon.PTNameFk)="&Rptaxon("PTNameFk")&") AND ((PTaxon.PTRefFk)="&Rptaxon("PTRefFk")&") AND ((RelPTaxon.RelQualifierFk) in (2,4,5,6,7))) ORDER BY RelPTQualifier.RelPTQualifier DESC,Name.RankFk, Name.NameCache"
	Set Rsyn = Server.CreateObject("ADODB.Recordset")

	Rsyn.ActiveConnection = MM_jun_STRING

	Rsyn.Source = SQL

	Rsyn.CursorType = 3

	Rsyn.CursorLocation = 2

	Rsyn.LockType = 1

	Rsyn.Open()

	if Rsyn.Recordcount>0 then

		contador=0

		syn_rel=""

		While NOT Rsyn.EOF

		contador=contador+1

%>

                          <TR>

                            <TD width="16%" valign="top">

							  <%if contador=1 or syn_rel<>Rsyn("RelPTQualifier") then%>

                              		<STRONG><%=ucase(left(Trim(Replace(Replace(Rsyn("RelPTQualifier"),"is",""),"of","")),1))&right(Trim(Replace(Replace(Rsyn("RelPTQualifier"),"is",""),"of","")),len(Trim(Replace(Replace(Rsyn("RelPTQualifier"),"is",""),"of","")))-1)&"(s):"%></STRONG>

                              <%end if%>



                            </TD>

                            <TD><A href="PTaxonDetail.asp?NameId=<%=Rsyn("PTNameFk")%>&PTRefFk=<%=Rsyn("PTRefFk_s")%>"><strong><%=Rsyn("FullNameCache")%></strong></A></TD>

                          </TR>

                          <%
			syn_rel=Rsyn("RelPTQualifier")
		Rsyn.Movenext

		Wend

		Rsyn.Close

		set Rsyn=Nothing

	end if

end if

'-------------------------------------------------------------------------------------------------------------------

%>

<%

'Look for concept synonyms only if the potential taxon is accepted CONCEPT SYNONYMS

if Rptaxon("StatusFk")=1 then



SQL="SELECT     RelQualifierFk, NameId, Reference.RefId, NameCache, FullNameCache, Name.Notes AS NameNotes, RelPTQualifier AS RelQualifier, RelPTaxon.Notes AS RelNotes, Reference.RefCache, RelRefFk, is_congruent, is_included_in, includes, overlaps, excludes, doubtful, Reference_1.RefCache AS RelRefCache, RankFk "&_

"FROM         RelPTQualifier INNER JOIN "&_

"                      RelPTaxon ON RelPTQualifier.RelPTQualifierId = RelPTaxon.RelQualifierFk INNER JOIN "&_

"                      Name ON RelPTaxon.PTNameFk2 = Name.NameId INNER JOIN "&_

"                      Reference ON RelPTaxon.PTRefFk2 = Reference.RefId INNER JOIN "&_

"					   Reference Reference_1 ON RelPTaxon.RelRefFk = Reference_1.RefId "&_

"WHERE     (RelPTaxon.RelQualifierFk > 9) AND (RelPTaxon.PTRefFk1 = "&Rptaxon("PTRefFk")&") AND (RelPTaxon.PTNameFk1 = "&Rptaxon("PTNameFk")&") "&_

"union "&_

"SELECT     RelQualifierFk, NameId, Reference.RefId, NameCache, FullNameCache, Name.Notes AS NameNotes, RelPTQualifier AS RelQualifier, RelPTaxon.Notes AS RelNotes, Reference.RefCache, RelRefFk, is_congruent, is_included_in, includes, overlaps, excludes, doubtful, Reference_1.RefCache AS RelRefCache, RankFk "&_

"FROM         RelPTQualifier INNER JOIN "&_

"                      RelPTaxon ON RelPTQualifier.RelPTQualifierId = dbo.f_core_Reverse(RelPTaxon.RelQualifierFk) INNER JOIN "&_

"                      Name ON RelPTaxon.PTNameFk1 = Name.NameId INNER JOIN "&_

"                      Reference ON RelPTaxon.PTRefFk1 = Reference.RefId INNER JOIN "&_

"					   Reference Reference_1 ON RelPTaxon.RelRefFk = Reference_1.RefId "&_

"WHERE     (RelPTaxon.RelQualifierFk > 9) AND (RelPTaxon.PTRefFk2 = "&Rptaxon("PTRefFk")&") AND (RelPTaxon.PTNameFk2 = "&Rptaxon("PTNameFk")&") "&_

"order by RelQualifierFk, RelRefFk, RankFk, namecache"





	Set Rcsyn = Server.CreateObject("ADODB.Recordset")

	Rcsyn.ActiveConnection = MM_jun_STRING

	Rcsyn.Source = SQL

	Rcsyn.CursorType = 3

	Rcsyn.CursorLocation = 2

	Rcsyn.LockType = 1

	Rcsyn.Open()

	if Rcsyn.Recordcount>0 then



%>

                          <TR>

                            <TD width="16%" valign="top">

                              	<STRONG>Concept Synonym(s):</STRONG>

                            </TD>

                            <TD>&nbsp;</TD>

                          </TR>

<%

		contador=0

		ult_ref=""

		While NOT Rcsyn.EOF

			contador=contador+1



%>

                          <%if Rcsyn("RelRefCache")<>ult_ref then%>

                           <TR>

                             <TD width="16%" valign="top">&nbsp;</TD>



                            <TD> <STRONG>According to: <font color="#990000"><%=Rcsyn("RelRefCache")%></font></STRONG></TD>

                           </TR>

						   <%end if%>

						  <TR>

                            <TD width="16%" valign="top">&nbsp;</TD>

                            <TD>

							<%

								'Construct a string concatenating the images that are needed to represent the concept relation

								concept_images=""

								if Rcsyn("is_congruent")<>0 then concept_images="<img src=""images/congruent_symbol.gif"">"

								if Rcsyn("is_included_in")<>0 then

									if concept_images<>"" then

										concept_images=concept_images&",<img src=""images/is_include_in_symbol.gif"">"

									else

										concept_images="<img src=""images/is_include_in_symbol.gif"">"

									end if

								end if

								if Rcsyn("includes")<>0 then

									if concept_images<>"" then

										concept_images=concept_images&",<img src=""images/includes_symbol.gif"">"

									else

										concept_images="<img src=""images/includes_symbol.gif"">"

									end if

								end if

								if Rcsyn("overlaps")<>0 then

									if concept_images<>"" then

										concept_images=concept_images&",<img src=""images/overlaps_symbol.gif"">"

									else

										concept_images="<img src=""images/overlaps_symbol.gif"">"

									end if

								end if

								if Rcsyn("excludes")<>0 then

									if concept_images<>"" then

										concept_images=concept_images&",<img src=""images/excludes_symbol.gif"">"

									else

										concept_images="<img src=""images/excludes_symbol.gif"">"

									end if

								end if

								concept_images="{"&concept_images&"}"

								if Rcsyn("doubtful")<>0 then concept_images="?"&concept_images

							%>

							<%=concept_images%> <A href="PTaxonDetail.asp?NameId=<%=Rcsyn("NameId")%>&PTRefFk=<%=Rcsyn("RefId")%>"><strong><%=Rcsyn("FullNameCache")%></strong> sec. <%=Rcsyn("RefCache")%></A> <%=Rcsyn("RelNotes")%></TD>

                          </TR>

<%

			Rcsyn.Movenext

			if NOT Rcsyn.EOF then

				ult_ref=Rcsyn("RelRefCache")

			end if

		Wend

		Rcsyn.Close

		set Rcsyn=Nothing

	end if

end if

'------------------------------------------------------------------------------------------------------------------

%>

<%

'Look for missaplied names only if the potential taxon is accepted MISSAPLIED NAMES

if Rptaxon("StatusFk")=1 then

	SQL="SELECT PTaxon_1.PTNameFk, PTaxon_1.PTRefFk AS PTRefFk_s, Name.NameCache, Name.FullNameCache, Name.Notes AS NameNotes, PTaxon.PTRefFk AS PTRefFk, RelPTQualifier.RelPTQualifier as RelQualifier, RelPTaxon.Notes as RelNotes, Reference.RefCache"&_

	" FROM (PTaxon AS PTaxon_1 INNER JOIN (PTaxon INNER JOIN RelPTaxon ON (PTaxon.PTNameFk = RelPTaxon.PTNameFk2) AND (PTaxon.PTRefFk = RelPTaxon.PTRefFk2)) ON (PTaxon_1.PTNameFk = RelPTaxon.PTNameFk1) AND (PTaxon_1.PTRefFk = RelPTaxon.PTRefFk1)) INNER JOIN Name ON PTaxon_1.PTNameFk = Name.NameId INNER JOIN RelPTQualifier ON RelPTaxon.RelQualifierFk = RelPTQualifier.RelPTQualifierId INNER JOIN Reference ON PTaxon_1.PTRefFk = Reference.RefId"&_

	" WHERE (((PTaxon.PTNameFk)="&Rptaxon("PTNameFk")&") AND ((PTaxon.PTRefFk)="&Rptaxon("PTRefFk")&") AND ((RelPTaxon.RelQualifierFk)=3)) ORDER BY Name.NameCache"

	Set Rmiss = Server.CreateObject("ADODB.Recordset")

	Rmiss.ActiveConnection = MM_jun_STRING

	Rmiss.Source = SQL

	Rmiss.CursorType = 3

	Rmiss.CursorLocation = 2

	Rmiss.LockType = 1

	Rmiss.Open()



	if Rmiss.Recordcount>0 then

		contador=0

		While NOT Rmiss.EOF

		contador=contador+1

%>

                          <TR>

                            <TD width="16%" valign="top">

							<%if contador=1 then%>

                              	<STRONG>Missaplied Name(s):</STRONG>

							<%end if%>

                            </TD>

                            <TD><A href="PTaxonDetail.asp?NameId=<%=Rmiss("PTNameFk")%>&PTRefFk=<%=Rmiss("PTRefFk_s")%>"><strong><%=Rmiss("FullNameCache")%></strong> sec. <%=Rmiss("RefCache")%></A> [<%=Rmiss("RelQualifier")%>] <%=Rmiss("RelNotes")%></TD>

                          </TR>

<%

		Rmiss.Movenext

		Wend

		Rmiss.Close

		set Rmiss=Nothing

	end if

end if

'------------------------------------------------------------------------------------------------------------------

%>

<%

'There are two type of notes here, the one from the potential taxon and the one from the name. Show them if there“s something inside

'---------------------------------------------------------------------

 if NameA(7)<>"" then %>

                          <TR>

                            <TD width="16%" valign="top"><STRONG>Name Notes:</STRONG></TD>

                            <TD><%=NameA(7)%></TD>

                          </TR>



<%end if%>

<% if Rptaxon("Notes")<>"" then %>

                          <TR>

                            <TD width="16%" valign="top"><STRONG>Taxon Notes:</STRONG></TD>

                            <TD><%=Rptaxon("Notes")%></TD>

                          </TR>



<%end if%>

						  <TR>

                            <TD width="16%" valign="top"></TD>

                            <TD>&nbsp;</TD>

                          </TR>



<% 'Select all the facts and present them FACTS

'------------------------------------------------------------------------------------------------------------------------

'We need to see if the column "Visible" is in the FactCategory Table to use it or not.
'if it is no present then all factcategories will be shown.
SQL="SELECT sysobjects.name AS Exp1 FROM sysobjects INNER JOIN syscolumns ON sysobjects.id = syscolumns.id WHERE (sysobjects.name LIKE 'FactCategory') AND (syscolumns.name LIKE N'Visible')"
Set RVisibleCol = Server.CreateObject("ADODB.Recordset")
RVisibleCol.ActiveConnection = MM_jun_STRING
RVisibleCol.Source = SQL
RVisibleCol.CursorType = 3
RVisibleCol.CursorLocation = 2
RVisibleCol.LockType = 1
RVisibleCol.Open()
if RVisibleCol.RecordCount > 0 then
	VisibleColExist= True
else
	VisibleColExist= False
end if
RVisibleCol.close
set RVisibleCol=Nothing

'I create a different SQL depending if the Visible column exist or not in the database
if VisibleColExist= True then
	SQL="SELECT PTaxon.PTNameFk, PTaxon.PTRefFk, FactCategory.FactCategory, FactCategory.FactCategoryId , FactCategory.Visible, Fact.Fact, FactCategory.MaxFactNumber FROM (PTaxon INNER JOIN Fact ON (PTaxon.PTRefFk = Fact.PTRefFk) AND (PTaxon.PTNameFk = Fact.PTNameFk)) INNER JOIN FactCategory ON Fact.FactCategoryFk = FactCategory.FactCategoryId WHERE (((PTaxon.PTNameFk)="&Rptaxon("PTNameFk")&") AND ((PTaxon.PTRefFk)="&Rptaxon("PTRefFk")&")) ORDER BY FactCategory.FactCategory"
Else
	SQL="SELECT PTaxon.PTNameFk, PTaxon.PTRefFk, FactCategory.FactCategory, FactCategory.FactCategoryId , Fact.Fact, FactCategory.MaxFactNumber FROM (PTaxon INNER JOIN Fact ON (PTaxon.PTRefFk = Fact.PTRefFk) AND (PTaxon.PTNameFk = Fact.PTNameFk)) INNER JOIN FactCategory ON Fact.FactCategoryFk = FactCategory.FactCategoryId WHERE (((PTaxon.PTNameFk)="&Rptaxon("PTNameFk")&") AND ((PTaxon.PTRefFk)="&Rptaxon("PTRefFk")&")) ORDER BY FactCategory.FactCategory"
End if
Set Rfacts = Server.CreateObject("ADODB.Recordset")
Rfacts.ActiveConnection = MM_jun_STRING
Rfacts.Source = SQL
Rfacts.CursorType = 3
Rfacts.CursorLocation = 2
Rfacts.LockType = 1
Rfacts.Open()

if Rfacts.RecordCount > 0 then



'--------------------------------------------------------

' Just for debugging

'While not Rfacts.eof

'	Response.Write("<b>" & Rfacts("FactCategory")&": </b>"&Rfacts("Fact")&", " & Rfacts("MaxFactNumber") &"<br>")

'	Rfacts.Movenext

'wend

Rfacts.Requery

'--------------------------------------------------------

'To concatenate the facts by category first all of them are included in an array

	Redim facts(1,0)

	control=0

	'I will use a different code depending if the Visible column exist or not, using the VisibleColExist= True

	While not Rfacts.eof
		if VisibleColExist= True then
			if control> 0 then
				'Only add the fact to the array if it can be output (MaxFactNumber<>0)
				if Rfacts("Visible")=True then
					if facts(0,control-1)=Rfacts("FactCategory") then
						facts(1,control-1)=facts(1,control-1) & ", " & Rfacts("Fact")
					else
						Redim preserve facts(1,control)
						facts(0,control)=Rfacts("FactCategory")
						facts(1,control)=Rfacts("Fact")
						control=control+1
					end if
				end if
			else
				if Rfacts("Visible")=True then
					facts(0,0)=Rfacts("FactCategory")
					facts(1,0)=Rfacts("Fact")
					control=1
				else
					control=0
				end if
			end if
		Else
			if control> 0 then
				if facts(0,control-1)=Rfacts("FactCategory") then
					facts(1,control-1)=facts(1,control-1) & ", " & Rfacts("Fact")
				else
					Redim preserve facts(1,control)
					facts(0,control)=Rfacts("FactCategory")
					facts(1,control)=Rfacts("Fact")
					control=control+1
				end if
			else
				facts(0,0)=Rfacts("FactCategory")
				facts(1,0)=Rfacts("Fact")
				control=1
			end if
		End if
		Rfacts.MoveNext
	Wend


	Rfacts.close

	set Rfacts=Nothing

%>


<% for i=0 to ubound(facts,2) %>

						   <TR>

							<TD width="16%" valign="top"><i><%=facts(0,i)%>:</i></TD>

                            <TD><%=facts(1,i)%></TD>

                          </TR>



<%

	next

end if

'-----------------------------------------------------------------------------------------------------------------------

%>

<!--#include file="ProjectSpecific.asp" -->



                        </TBODY>

                      </table></td>

                  </tr>

                </table>



				<HR SIZE=1>

<%

Rptaxon.close

set Rptaxon = Nothing



Recordset1.MoveNext

Wend

%>



<%

' Message indicating that there was no potential taxon using this name ----------------

'----------------------------------------------------------------------------------------------------------------------

else

%>

                  <h5 align="center">There are no potential taxons using this name! </h5>

<%

'-----------------------------------------------------------------------------------------------------------------------

end if

Recordset1.close

set Recordset1 = Nothing

%>

<%
'Functions for retriving info from the GBIF portal

Function GetGBIFData(TaxonName)
	Dim url, xmlhttp, XMLDOM, XMLNode

	' Call web service using HTTP-GET
	url = "http://www.gbif.net/portal/ecat_xmlquery.jsp?termsAccepted=true&search=" & TaxonName
	Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
	xmlhttp.Open "GET", url, False
	xmlhttp.Send
	' Parse result
	Set XMLDOM = Server.CreateObject("Microsoft.XMLDOM")
	XMLDOM.Load(xmlhttp.responseBody)
	Set xmlhttp = Nothing
	Set XMLNodeTaxonKey = XMLDOM.SelectSingleNode("response/taxa/taxon/taxonkey")
	Set XMLNodeTotalRecords = XMLDOM.SelectSingleNode("response/taxa/taxon/records/totalrecords")
	Set XMLNodeTotalGisRecords = XMLDOM.SelectSingleNode("response/taxa/taxon/records/totalgisrecords")
	Set XMLNodeurl = XMLDOM.SelectSingleNode("response/taxa/taxon/records/url")

	If Not XMLNodeTaxonKey Is Nothing Then

		GetGBIFData = "| <strong><a href="""& XMLNodeurl.text &""" target=""_blank"">"& XMLNodeTotalRecords.text &" record(s) in GBIF</a></strong>"
	 	if XMLNodeTotalGisRecords.text > 0 then
			GetGBIFData = GetGBIFData & " | <strong><a href=""http://www.gbif.net/portal/img/image.jsp?type=taxonmap&key="& XMLNodeTaxonKey.text &"&secondKey=0"" target=""_blank"">Show map!</a></strong> |"
		end if
	Else
		GetGBIFData = "| No data in GBIF |"
	End If
End Function

%>

<!--#include file="template_inf.asp" -->