 			$( "#sortable" ).sortable({
 				handle: '.dragger'
 			});
-		    /*
-
-			var ac_isclass_ttoptions = {
-	       		source: '/component/functions.cfc?method=ac_isclass_tt',
-				width: 320,
-				max: 50,
-				autofill: false,
-				multiple: false,
-				scroll: true,
-				scrollHeight: 300,
-				matchContains: true,
-				minChars: 1,
-				selectFirst:false
-		    };
-			var ac_noclass_ttoptions = {
-	       		source: '/component/functions.cfc?method=ac_noclass_tt',
-				width: 320,
-				max: 50,
-				autofill: false,
-				multiple: false,
-				scroll: true,
-				scrollHeight: 300,
-				matchContains: true,
-				minChars: 1,
-				selectFirst:false
-		    };
-		    */
-
-
-		    /*
-			$("input.ac_noclass_tt").live("keydown.autocomplete", function() {
-		        $(this).autocomplete(ac_noclass_ttoptions);
-		    });
-
-		      $("input.ac_isclass_tt").live("keydown.autocomplete", function() {
-		        $(this).autocomplete(ac_isclass_ttoptions);
-		    });
-
-
-		    */
+
 			guessAtDisplayName();
 		});
 		function submitForm() {

	</cfoutput>
</cfif>

<!----
<!------------------------------------------------------------------->
<cfif action is "editClassification">
	<style>
		.dragger {
			cursor:move;
		}
		.isterm {
			font-weight:bold;
			font-style:italics;
		}
	</style>
	<script>
		// copy this with create classification
		$(function() {
			$( "#sortable" ).sortable({
				handle: '.dragger'
			});
			var ac_isclass_ttoptions = {
	       		source: '/component/functions.cfc?method=ac_isclass_tt',
				width: 320,
				max: 50,
				autofill: false,
				multiple: false,
				scroll: true,
				scrollHeight: 300,
				matchContains: true,
				minChars: 1,
				selectFirst:false
		    };
		    /*
			var ac_noclass_ttoptions = {
	       		source: '/component/functions.cfc?method=ac_noclass_tt',
				width: 320,
				max: 50,
				autofill: false,
				multiple: false,
				scroll: true,
				scrollHeight: 300,
				matchContains: true,
				minChars: 1,
				selectFirst:false
		    };
		    */

		    $("input.ac_isclass_tt").live("keydown.autocomplete", function() {
		        $(this).autocomplete(ac_isclass_ttoptions);
		    });
		    /*
			$("input.ac_noclass_tt").live("keydown.autocomplete", function() {
		        $(this).autocomplete(ac_noclass_ttoptions);
		    });
		    */
			guessAtDisplayName();
		});
		function submitForm() {
			var linkOrderData=$("#sortable").sortable('toArray').join(',');
			$( "#classificationRowOrder" ).val(linkOrderData);
			var nccellary = new Array();
			$.each($("tr[id^='nccell_']"), function() {
				nccellary.push(this.id);
		    });
			var ncls=nccellary.join(',');
			$( "#noclassrows" ).val(ncls);
			$( "#f1" ).submit();
		}
		function deleteThis(r) {
			$( "#cell_" + r ).remove();
		}
		function nc_deleteThis(r) {
			$( "#nccell_" + r ).remove();
		}
		function addARow() {
			var n=parseInt($("#maxposn").val());
			++n;
			var x='<tr id="cell_' + n + '">';
			x+='<td class="dragger">(drag row here)</td>';
			x+='<td><input size="60" class="ac_isclass_tt" type="text" id="term_type_' + n + '" name="term_type_' + n + '" onchange="guessAtDisplayName(this.id)"></td>';
			x+='<td><input size="60" type="text" id="term_' + n + '" name="term_' + n + '" onchange="guessAtDisplayName(this.id)"></td>';
			x+='<td><span class="likeLink" onclick="deleteThis(\'' + n + '\');">[ Delete this row ]</span></td>';
			x+='</tr>';
			$("#sortable").append(x);
			$("#maxposn").val(n);
		}
		function nc_addARow() {
			var n=parseInt($("#numnoclassrs").val());
			++n;
			var x='<tr id="nccell_' + n + '">';
			x+='<td><input class="ac_noclass_tt" size="60" type="text" id="ncterm_type_' + n + '" name="ncterm_type_' + n + '" onchange="guessAtDisplayName(this.id)"></td>';
			x+='<td><input size="60" type="text" id="ncterm_' + n + '" name="ncterm_' + n + '" onchange="guessAtDisplayName(this.id)"></td>';
			x+='<td><span class="likeLink" onclick="nc_deleteThis(\'' + n + '\');">[ Delete this row ]</span></td>';
			x+='</tr>';
			$("#notsortable").append(x);
			$("#numnoclassrs").val(n);
		}
		function deleteClassification(cid,tnid) {
			var msg='Are you sure you want to delete this classification?\nDo NOT delete classifications because you do not agree with them or because they';
			msg+=' do not fit your collection or taxonomy preferences.\nDeleted classifications from GlobalNames will come back; fix them at the source.';
			msg+='\nIf you did not create the classification you are trying to delete, you should probably click "cancel" now.';
			var r=confirm(msg);
			if (r==true) {
				document.location='/editTaxonomy.cfm?action=deleteClassification&classification_id=' + cid + '&taxon_name_id=' + tnid;
			}
		}

		function guessAtDisplayName(caller) {
			// if this is being called by an element, check if that element is the value
			// of display_name. If so, just exit. Otherwise, rock on.
			if(caller && caller.substring(0, 2) == "nc") {
				var cary=caller.split('_');
				var theIdInt=cary[cary.length-1];
				var theType=$("#ncterm_type_" + theIdInt).val();
				if (theType == 'display_name'){
					return false;
				}
			}
			var genus; // just so that we can italicize @fallback
			var species;
			var infraspecific_term;
			var infraspecific_rank;
			var speciesauthor;
			var subspeciesauthor;
			var dv_element=""; // element of the term type
			var dv_value=""; // contents of the term
			var dv_value_element=""; // element of the term
			var formatstyle = 'iczn'; // default to simple....
			var formattedname; // with HTML
			var lowestclassificationterm;
			$("input[name^='ncterm_type_']").each(function() {
				var thisval = $(this).val();
				var relatedElementID=this.id.replace("type_","");
				var relatedElement=$("#" + relatedElementID).val();

				/*
				if(thisval == "display_name") {
					dv_value_element=relatedElementID;
					dv_value=relatedElement;
					dv_element=this.id;
			    }

			    */
				if(thisval == "author_text") {
					speciesauthor=relatedElement;
			    }
				if(thisval == "infraspecific_author") {
					subspeciesauthor=relatedElement;
			    }
				if(thisval == "nomenclatural_code" && relatedElement=='ICBN') {
					formatstyle='icbn';
				}
			});

			/*

			if (! dv_element){
				// add a row for display_name
				nc_addARow();
				var n=parseInt($("#numnoclassrs").val());
				$('#ncterm_type_' + n).val('display_name');
				dv_element='ncterm_type_' + n;
			}

			*/
			// and this point, there should be a display_name and we should know it's ID.
			$("input[name^='term_type_']").each(function() {
		    	var thisval = $(this).val();
				var relatedElementID=this.id.replace("type_","");
				var relatedElement=$("#" + relatedElementID).val();
				if(thisval == "kingdom" && relatedElement=='Plantae') {
					formatstyle='icbn';
				}
				if(thisval == "species" || thisval == "sp" || thisval == "sp.") {
					species=relatedElement;
				}
				if(thisval == "genus" || thisval == "gen.") {
					genus=relatedElement;
				}
				if(thisval == "subsp." || thisval == "variety" || thisval == "var." || thisval == "varietas" || thisval == "subvar." || thisval == "subspecies") {
					infraspecific_term=relatedElement;
					infraspecific_rank=thisval;
				}
				lowestclassificationterm=relatedElement;
			});
			if (species) {
				formattedname = ' <i>' + species + '</i>';
				if (formatstyle=='icbn'){
					if (speciesauthor) {
						formattedname += ' ' + speciesauthor;
					}
					if (infraspecific_rank) {
						formattedname += ' ' + infraspecific_rank;
					}
					if (infraspecific_term) {
						infraspecific_term=infraspecific_term.replace(species,"").trim();
						infraspecific_term=infraspecific_term.replace(infraspecific_rank,"").trim();
						formattedname += ' <i>' + infraspecific_term + '</i>';
					}
					if (subspeciesauthor) {
						formattedname += ' ' + subspeciesauthor;
					}
				}
				if (formatstyle=='iczn'){
					if (infraspecific_term) {
						infraspecific_term=infraspecific_term.replace(species,"").trim();
						formattedname += ' <i>' + infraspecific_term + '</i>';
					}
					if (speciesauthor) {
						formattedname += ' ' + speciesauthor;
					}
				}
			}
			if (! formattedname) {
				if (genus) {
					formattedname='<i>' + genus + '</i>';
					if (speciesauthor) {
						formattedname += ' ' + speciesauthor;
					}
				} else {
					formattedname=lowestclassificationterm;
				}
			}
			if (formattedname) {
				formattedname=formattedname.replace(/<\/i> <i>/g, ' ').trim();
				$("#dng").val(formattedname);
			}
		}
		function useDNG(){
			var dn=$("#dng").val();
			var idOfDisplayRow;
			$("input:text").each(function(){
				 if( $(this).val()=='display_name' ){
   				 	idOfDisplayRow=this.id;
   				 }
   			});
			if(typeof idOfDisplayRow === 'undefined'){
				nc_addARow();
				var n=parseInt($("#numnoclassrs").val());
				$('#ncterm_type_' + n).val('display_name');
				idOfDisplayRow='ncterm_type_' + n;
			}
			var vc=idOfDisplayRow.replace('type_','');
			$("#" + vc).val(dn);
		}
		function scrollDNW () {
		    $('html, body').animate({
		        scrollTop: $("#dnWarning").offset().top
		    }, 1000);
		}
	</script>
	<cfoutput>
		<cfquery name="d" datasource="user_login" username="#session.dbuser#" password="#decrypt(session.epw,session.sessionKey)#">
			select
				*
			from
				taxon_name,
				taxon_term
			where
				taxon_name.taxon_name_id=taxon_term.taxon_name_id and
				classification_id='#classification_id#'
		</cfquery>
		<cfquery name="thisname" dbtype="query">
			select
				source,
				scientific_name,
				taxon_name_id
			from
				d
			group by
				source,
				scientific_name,
				taxon_name_id
		</cfquery>

		<cfquery name="noclass" dbtype="query">
			select * from d where POSITION_IN_CLASSIFICATION is null order by term_type
		</cfquery>
		<cfquery name="hasclass" dbtype="query">
			select * from d where POSITION_IN_CLASSIFICATION is not null order by  POSITION_IN_CLASSIFICATION
		</cfquery>
		<cfquery name="maxclass" dbtype="query">
			select max(POSITION_IN_CLASSIFICATION) m from hasclass
		</cfquery>
		<cfquery name="maxnoclass" dbtype="query">
			select count(*) m from noclass
		</cfquery>
		<p>
			Editing <strong>#thisName.source#</strong> classification for <strong>#thisName.scientific_name#</strong> (classification_id=#classification_id#)
			<cfset title="Edit Classification: #thisName.scientific_name#">
			<br>
			<a href="/name/#thisname.scientific_name#">[ View Taxon Page ]</a>
			<a href="/editTaxonomy.cfm?action=editnoclass&taxon_name_id=#thisname.taxon_name_id#">[ Edit Non-Classification Data ]</a>
			<span class="likeLink" onclick="deleteClassification('#classification_id#','#thisname.taxon_name_id#');">[ Delete Classification ]</span>
		</p>
		<p>
			Important randomness:
			<ul>
				<li>
					<span class="isterm">Species</span> are binomials, not specific epithets: "Poa abbreviata" rather than "abbreviata."
				</li>
				<li>
					ICZN-like subspecific terms (which should with rare exception be <span class="isterm">subspecies</span>) are trinomials:
					"Alces alces shirasi," not "shirasi."
				</li>
				<li>
					ICBN-like subspecific terms (<span class="isterm">subspecies</span>, <span class="isterm">subsp.</span>, <span class="isterm">var.</span>, etc.)
					are ranked trinomials: "Poa abbreviata subsp. jordalii," not "jordalii."
				</li>
			</ul>
		</p>
		<form name="f1" id="f1" method="post" action="editTaxonomy.cfm">
			<input type="button" onclick="submitForm();" value="Save Edits">
			<input type="button" onclick="scrollDNW();" value="See display_name suggestions">
			<input type="hidden" name="action" value="saveClassEdits">
			<input type="hidden" name="classification_id" id="classification_id" value="#classification_id#">
			<input type="hidden" name="taxon_name_id" id="taxon_name_id" value="#thisname.taxon_name_id#">
			<input type="hidden" name="source" id="source" value="#thisname.source#">
			<cfif len(maxclass.m) is 0>
				<cfset mc=0>
			<cfelse>
				<cfset mc=maxclass.m>
			</cfif>
			<input type="hidden" name="maxposn" id="maxposn" value="#mc#">
			<cfif len(maxnoclass.m) is 0>
				<cfset mc=0>
			<cfelse>
				<cfset mc=maxnoclass.m>
			</cfif>
			<input type="hidden" name="numnoclassrs" id="numnoclassrs" value="#mc#">
			<input type="hidden" name="classificationRowOrder" id="classificationRowOrder">
			<input type="hidden" name="noclassrows" id="noclassrows">
			<h3>
				Non-Classification Terms
			</h3>
			<p style="font-size:small;">
				These are paired terms; unpaired terms will be ignored (=deleted).
			</p>
			<table id="clastbl" border="1">
				<thead>
					<tr><th>Term Type</th><th>Term</th><th>Delete</th></tr>
				</thead>
				<tbody id="notsortable">
					<cfset thisrow=1>
					<cfloop query="noclass">
						<tr id="nccell_#thisrow#">
							<td>
								<input class="ac_noclass_tt" size="60" type="text" id="ncterm_type_#thisrow#" name="ncterm_type_#thisrow#" value="#term_type#" onchange="guessAtDisplayName(this.id)">
							</td>
							<td>
								<input size="60" type="text" id="ncterm_#thisrow#" name="ncterm_#thisrow#" value="#stripQuotes(term)#" onchange="guessAtDisplayName(this.id)">
							</td>
							<td>
								<span class="likeLink" onclick="nc_deleteThis('#thisrow#');">[ Delete this row ]</span>
							</td>
						</tr>
						<cfset thisrow=thisrow+1>
					</cfloop>
				</tbody>
			</table>
			<span class="likeLink" onclick="nc_addARow();">[ add a row ]</span>
			<h3>
				Classification Terms
			</h3>
			<p style="font-size:small;">
				 Order is important here - "large" (eg, kingdom) at top to "small" (eg, subspecies) at bottom. Drag rows to sort.
				 TermType will be ignored if Term is empty. Term will be saved regardless of TermType; unranked terms are OK.
			</p>
			<table id="clastbl" border="1">
				<thead>
					<tr><th>Drag Handle</th><th>Term Type</th><th>Term</th><th>Delete</th></tr>
				</thead>
				<tbody id="sortable">
					<cfloop query="hasclass">
						<tr id="cell_#POSITION_IN_CLASSIFICATION#">
							<td class="dragger">
								(drag row here)
							</td>
							<td>
								<input size="60" class="ac_isclass_tt" type="text" id="term_type_#POSITION_IN_CLASSIFICATION#" name="term_type_#POSITION_IN_CLASSIFICATION#" value="#term_type#" onchange="guessAtDisplayName(this.id)">
							</td>
							<td>
								<input size="60" type="text" id="term_#POSITION_IN_CLASSIFICATION#" name="term_#POSITION_IN_CLASSIFICATION#" value="#term#" onchange="guessAtDisplayName(this.id)">
							</td>
							<td>
								<span class="likeLink" onclick="deleteThis('#POSITION_IN_CLASSIFICATION#');">[ Delete this row ]</span>
							</td>
						</tr>
					</cfloop>
				</tbody>
			</table>
			<span class="likeLink" onclick="addARow();">[ add a row ]</span>
			<p>
				<div id="dnWarning" style="border:2px solid red;padding:2em;margin:2em;">
					<p>
						IMPORTANT!! Classifications should generally have a (one!) non-classification term
						"display_name" with corresponding HTML-formatted, discipline-specific value,
						including authors, infraspecific rank, etc. This is stored in FLAT.FORMATTED_SCIENTIFIC_NAME and used on many forms and
						labels. This suggestion is based on other data, including kingdom, nomenclatural_code, genus, species,
						"subspecies" (including var., forma, etc.), infraspecific rank (such as var., forma, etc.), and infraspecific_author.
					<strong>Bad suggestions here are an indication of missing or malformed data.</strong>
					</p>
					<label for="dng">Our guess at display_name</label>
					<input id="dng" size="80">
					<input type="button" onclick="useDNG();" value="Use this suggestion">
					<input type="button" onclick="guessAtDisplayName();" value="Refresh suggestion">
				</div>
				<input type="button" onclick="submitForm();" value="Save Edits">
			</p>
			<p>
				If you haven't yet saved, you can <a href="/editTaxonomy.cfm?action=editClassification&classification_id=#classification_id#">refresh this page</a>
			</p>
		</form>
	</cfoutput>
</cfif>

---->


<!------------------------------------------------------------------->
<cfif action is "editClassification">
<!----= edit: force, don't suggest, using cttaxon_term ---->

			font-weight:bold;
			font-style:italics;
		}
		.warningDiv {color:red;}
	</style>
	<script>
		// copy this with create classification
