var screenheight=screen.height;
var bigscreen=1000;
if(screenheight>bigscreen){
document.write("<link rel=stylesheet href='bigscreen.css' type='text/css'>");
var smallwidth=40;
var largewidth=320;
}
else{document.write("<link rel=stylesheet href='smallscreen.css' type='text/css'>");
var smallwidth=20;
var largewidth=200;
}
var bluecolor="#99FFFF";
var greencolor="#66CC66";
function HighlightButton(){
	GetDisplayVariables();
	if (family=="FAMILY"){family="";}
	if (genus=="GENUS"){genus="";}
	if (species=="SPECIES"){species="";}
	if (variety=="VARIETY"){variety="";}
	if (area=="AREA"){area="";}
	var searchstring=family+genus+species+variety+comname+area;
	if(searchstring!=""){
		top.SearchPage.SearchForm.search.style.background="orange";}

	else{top.SearchPage.SearchForm.search.style.background="#99FFFF";}
}

function ListClick(WhichList,AdjBox){
	if(WhichList.size==1){
		WhichList.style.width=largewidth;
		WhichList.size=10;
		AdjBox.style.width=smallwidth;
		if(LastChoice){
			WhichList.selectedindex=LastChoice;}}
	else{
		var LastChoice=WhichList.selectedindex;
		CloseList(WhichList,AdjBox);
		if(WhichList.name=="FamilyList"){
			UpdateFamily();}
		if(WhichList.name=="GenusList"){
			UpdateGenus();}
		if(WhichList.name=="SpeciesList"){
			UpdateSpecies();}
		if(WhichList.name=="VarietyList"){
			UpdateVariety();}
	}
}

function CloseList(WhichList,AdjBox){
	WhichList.style.width=smallwidth;
	WhichList.size=1;
	AdjBox.style.width=largewidth;}

function autoComplete (field, select, property, forcematch) {
	var found = false;
	for (var i = 0; i < select.options.length; i++) {
	if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
		found=true; break;
		}
	}
	if (found) { select.selectedIndex = i; }
	else { select.selectedIndex = -1; }
	if (field.createTextRange) {
		if (forcematch && !found) {
			field.value=field.value.substring(0,field.value.length-1); 
			return;
			}
		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		if (cursorKeys.indexOf(event.keyCode+";") == -1) {
			var r1 = field.createTextRange();
			var oldValue = r1.text;
			var newValue = found ? select.options[i][property] : oldValue;
			if (newValue != field.value) {
				field.value = newValue;
				var rNew = field.createTextRange();
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
				}
			}
		}
	}



function GetLink(){
var baseURL="http://plants.usda.gov/cgi_bin/topics.cgi?earl=plant_search.cgi&mode=";
var searchWords= top.storage.value;
URL=baseURL+searchWords;
top.LinkPage.location=URL;
}

function OpenPage(SearchType){
var SearchString="";
if(SearchType=="CommonName"){
	var SearchString=top.SearchPage.SearchForm.CommonSearchUSDA.value;
	var BaseString="Common+Name&keywordquery=";}
else if(SearchType=="ScientificName"){
	var SearchString=top.SearchPage.SearchForm.ScientificSearchUSDA.value;
	var BaseString="Scientific+Name&keywordquery=";}
if(SearchString==""){
	alert("Please Put Some Search Criterion Into The Box");}
else {
	QueryString=BaseString+SearchString;
	URL="LinkMaster.htm?SearchUSDA=" + QueryString+"&";
	window.open(URL,"LinkWindow");}
}

function HideMe(){
top.Header.WaitPlease.style.display="none";
}

function ShowMe(){
top.Header.WaitPlease.style.display="";}

function TreeNumSearch(){
ShowMe();
top.SearchPage.SearchForm.ComName.value="";
top.SearchPage.SearchForm.PickArea.value="AREA";
top.SearchPage.SearchForm.PickFamily.value="FAMILY";
top.SearchPage.SearchForm.PickGenus.value="GENUS";
top.SearchPage.SearchForm.PickSpecies.value="SPECIES";
top.SearchPage.SearchForm.PickVariety.value="VARIETY";
PrepareSearch();
//alert(treenum);
}
function InactivityTimeout(min)
{ 
	var millsec=60000;
	var delaytime=millsec*min;
	URL="http://www.virginia.edu/blandy/BlandyMap/BlandyMap.htm";
	setTimeout("top.window.location.reload (false)",delaytime);
}
function BlandyWEB()
{
top.location="http://www.virginia.edu/blandy"}

function GetMatches()
{
	InactivityTimeout(15);
	var NumMatches=top.ResultsPage.RunSearch.Matches.value;
		if(NumMatches>0){
			if(NumMatches>1){
				var temp1="There Were "+NumMatches+" Matches In Our Data Base. ";
				var temp2="Do you want to see them on the map now?";
			}
			else {
				var temp1="There Was One Match In Our Data Base. ";
				var temp2="Do you want to see it on the map now?";
			}
			var Report=temp1+temp2;
			var answer = confirm (Report)
			if (answer)
				{plotPoint();}
			
			
		}
				
		if(NumMatches==0){
			FirstRound=top.ResultsPage.RunSearch.FirstRnd.value;
			if(FirstRound !=="1"){
			SearchString=top.SearchPage.SearchForm.SearchString.value
			Report="There were no specimens matching these criteria: " + SearchString;
			alert(Report);}
		}
HideMe();
}


function refresh()
{
 window.parent.frames('Map').location.reload( false );
}

function ClearLayer(WhichOne){
var map = top.Map.document.mapApplet.getMap();
switch(WhichOne)
{
  case 0: var lyr = map.getLayer("Red"); break
  case 1: var lyr = map.getLayer("Yellow"); break
  case 2: var lyr = map.getLayer("Blue"); break
  case 3: var lyr = map.getLayer("Violet");
}
lyr.clear(false);
map.updateMap();
top.Map.MarkerText[WhichOne].value="";
top.Map.MarkerText[WhichOne].style.display="none";

}

function PrintMap(){
alert("MAP IS ABOUT TO PRINT --PLEASE BE PATIENT");
window.parent.frames('Map').focus();
window.parent.frames('Map').print();
}

function Popular(choice){
top.SearchPage.SearchForm.PickArea.value="AREA";
top.SearchPage.SearchForm.PickFamily.value="FAMILY";
top.SearchPage.SearchForm.PickGenus.value="GENUS";
top.SearchPage.SearchForm.PickSpecies.value="SPECIES";
top.SearchPage.SearchForm.PickVariety.value="VARIETY";
top.SearchPage.SearchForm.TreeNum.value="0";
if (choice !=="Pick A Plant")
{top.SearchPage.SearchForm.ComName.value=choice;
Search();
}
}
function setVisible(){
var map=top.Map.document.applets['mapApplet'].getMap();
var lyr=map.getLayer("RedLabels");
lyr.setVisible(!lyr.isVisible());
var lyr=map.getLayer("BlueLabels");
lyr.setVisible(!lyr.isVisible());
var lyr=map.getLayer("YellowLabels");
lyr.setVisible(!lyr.isVisible());
var lyr=map.getLayer("VioletLabels");
lyr.setVisible(!lyr.isVisible());
map.extentChanged();
map.repaintMap();

var map=top.Map.document.applets['mapApplet'].getMap();
}

function UpdateFamily(){
top.ResultsPage.RunSearch.Matches.value="";
top.SearchPage.SearchForm.PickGenus.value="GENUS";
top.SearchPage.SearchForm.PickSpecies.value="SPECIES";
top.SearchPage.SearchForm.PickVariety.value="VARIETY";
  for (var i=top.ResultsPage.RunSearch.AllResults.options.length-1; i>=1; i--){
    top.ResultsPage.RunSearch.AllResults.options[i] = null;}
top.ResultsPage.RunSearch.AllResults.value="Matching Records";

top.SearchPage.SearchForm.submit();
DisableSearchBoxes();
}

function UpdateGenus(){
top.ResultsPage.RunSearch.Matches.value="";
top.SearchPage.SearchForm.PickSpecies.value="SPECIES";
top.SearchPage.SearchForm.PickVariety.value="VARIETY";
top.ResultsPage.RunSearch.AllResults.value="Matching Records";
  for (var i=top.ResultsPage.RunSearch.AllResults.options.length-1; i>=0; i--){
    top.ResultsPage.RunSearch.AllResults.options[i] = null;}
top.SearchPage.SearchForm.submit();
 DisableSearchBoxes();
}

function UpdateSpecies(){
top.ResultsPage.RunSearch.Matches.value="";
top.SearchPage.SearchForm.PickVariety.value="VARIETY";
top.ResultsPage.RunSearch.AllResults.value="Matching Records";
  for (var i=top.ResultsPage.RunSearch.AllResults.options.length-1; i>=0; i--){
    top.ResultsPage.RunSearch.AllResults.options[i] = null;}
top.SearchPage.SearchForm.submit();
DisableSearchBoxes();
}

function UpdateVariety(){
top.ResultsPage.RunSearch.Matches.value="";
top.ResultsPage.RunSearch.AllResults.value="Matching Records";
  for (var i=top.ResultsPage.RunSearch.AllResults.options.length-1; i>=0; i--){
    top.ResultsPage.RunSearch.AllResults.options[i] = null;}
}

function DisableSearchBoxes(){
  top.SearchPage.SearchForm.PickFamily.disabled=true;
  top.SearchPage.SearchForm.PickGenus.disabled=true;
  top.SearchPage.SearchForm.PickSpecies.disabled=true;
  top.SearchPage.SearchForm.PickVariety.disabled=true;
  top.SearchPage.SearchForm.PickFamily.style.background="red";
  top.SearchPage.SearchForm.PickGenus.style.background="red";
  top.SearchPage.SearchForm.PickSpecies.style.background="red";
  top.SearchPage.SearchForm.PickVariety.style.background="red";
}

function Help(URL){
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=700,height=400,left = 50,top = 50');");
}

function Blandy(URL){
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1');");
}

function StoreData(){
matches=top.ResultsPage.RunSearch.Matches.value;
if (matches>500){
   alert("There Are Too Many Records to Show --500 max");
}
else if (matches==0){
alert("No Data To Show");
}
else if (matches<501)
{
var newrows=parseInt(top.Map.QueryMatches.rows)+ parseInt(matches);
top.Map.QueryMatches.rows=newrows;
for (j=0; j<matches;j++)
{
	var newText=top.ResultsPage.RunSearch.AllResults.options[j].innerHTML;
	top.Map.QueryMatches.value=top.Map.QueryMatches.value + "\n" + newText;
}
alert("Scroll Below Map To See The Corresponding Records");
}
}
function Search(){
ShowMe();
top.SearchPage.SearchForm.TreeNum.value="0";
PrepareSearch();
}

function PrepareSearch(){
GetDisplayVariables();
top.ResultsPage.RunSearch.TreeNum.value=treenum;
top.ResultsPage.RunSearch.ComName.value=comname;
top.ResultsPage.RunSearch.Area.value=area;
top.ResultsPage.RunSearch.Family.value=family;
top.ResultsPage.RunSearch.Genus.value=genus;
top.ResultsPage.RunSearch.Species.value=species;
top.ResultsPage.RunSearch.Variety.value=variety;
// Also Place genus species and comname into USDA boxes
var SearchString="";
var SciName="";
var Cname="";
if(treenum !=="0"){SearchString=SearchString+treenum+" ";}
if(family !=="FAMILY"){SearchString=SearchString+family+" ";}
if(genus !=="GENUS"){SearchString=SearchString+genus+" ";
var SciName=genus;}
if(species !=="SPECIES"){SearchString=SearchString+species+" ";
var SciName=SciName+species;}
if(variety !=="VARIETY"){SearchString=SearchString+variety+" ";}
if(comname !==""){SearchString=SearchString+comname+" ";
var Cname=comname;}
if(area !=="AREA"){SearchString=SearchString+area;}
top.SearchPage.SearchForm.CommonSearchUSDA.value=Cname;
top.SearchPage.SearchForm.ScientificSearchUSDA.value=SciName;

if(SearchString){
	top.ResultsPage.RunSearch.submit();
	top.SearchPage.SearchForm.SearchString.value=SearchString;
}
else{alert("No Search Criteria Have Been Selected");}
}

function Reset(){
top.SearchPage.SearchForm.ComName.value="";
top.SearchPage.SearchForm.PickArea.value="AREA";
top.SearchPage.SearchForm.PickFamily.value="FAMILY";
top.SearchPage.SearchForm.PickGenus.value="GENUS";
top.SearchPage.SearchForm.PickSpecies.value="SPECIES";
top.SearchPage.SearchForm.PickVariety.value="VARIETY";
top.SearchPage.SearchForm.TreeNum.value="0";
top.SearchPage.SearchForm.submit();
HighlightButton();
}

function plotPoint(){
top.Map.scrollTo(0, 0);
GetDisplayVariables();
if (matches>0){
var map = top.Map.document.mapApplet.getMap();
getMarkerColor();
LegendRoutine();

var temp2=top.Map.MarkerText[markerOption].value;
if (temp2 !== "")
{var temp3=temp2+", "+PersLegend;
PersLegend=temp3;}
top.Map.MarkerText[markerOption].value=PersLegend;
top.Map.MarkerText[markerOption].style.display="";
if(PersLegend.length < 20){
top.Map.MarkerText[markerOption].size=PersLegend.length;}
else
{top.Map.MarkerText[markerOption].size=20;}


switch(markerOption)
{
  case 0: var lyr = map.getLayer("Red"); break
  case 1: var lyr = map.getLayer("Yellow"); break
  case 2: var lyr = map.getLayer("Blue"); break
  case 3: var lyr = map.getLayer("Violet");
}

var ext = map.getExtent();
var smX=ext.x;
var bigX=ext.x2;
var smY=ext.y;
var bigY=ext.y2;  

for (j=0; j<matches;j++)
{
	var x=parseFloat(top.ResultsPage.RunSearch.XResults.options[j].innerHTML);
	var y=parseFloat(top.ResultsPage.RunSearch.YResults.options[j].innerHTML);
		//change zoom if points off map
		if(x<smX){smX=x;}
		if(x>bigX){bigX=x;}
		if(y<smY){smY=y;}
		if(y>bigY){bigY=y;}
	var treenum=" "+top.ResultsPage.RunSearch.treenumResults.options[j].innerHTML;
	var area=top.ResultsPage.RunSearch.areaResults.options[j].innerHTML;

	var family=top.ResultsPage.RunSearch.familyResults.options[j].innerHTML;
	var genus=top.ResultsPage.RunSearch.genusResults.options[j].innerHTML;
	var species=top.ResultsPage.RunSearch.speciesResults.options[j].innerHTML;
	var variety=top.ResultsPage.RunSearch.varietyResults.options[j].innerHTML;
	var comname=top.ResultsPage.RunSearch.comnameResults.options[j].innerHTML;
	var rec = lyr.newRecord();
	var shape = rec.newShape(1);
	shape.setVertex(0,x,y);
	var recId = ""+(lyr.records.size()+1);
	rec.setId(recId);
	rec.setField(0,recId);
	rec.setField(1,comname);
	rec.setField(2,family);
	rec.setField(3,genus);
	rec.setField(4,species);
	rec.setField(5,variety);
	rec.setField(6,area);
	rec.setField(9,treenum);
	//add record
	lyr.addRecord(rec);
}

//repaint map
ext.x=smX;
ext.x2=bigX;
ext.y=smY;
ext.y2=bigY;
map.zoomToExtent(ext);
map.extentChanged();
map.updateMap();
if (markerOption==3){
markerOption=-1;}
top.Map.color[markerOption+1].checked=true;

//IF DOING A TREE NUM SEARCH SHOW MATCHING RECORD
treenum=top.SearchPage.SearchForm.TreeNum.value;
if(treenum>0){var newText=top.ResultsPage.RunSearch.AllResults.options[0].innerHTML;
var temp="Here is the corresponding database information: ";
var temp2=temp+newText;
		alert(temp2);}
}
Reset();
}

function getMarkerColor(){
markerOption = -1;
for (i=0; i<top.Map.color.length; i++) {
if (top.Map.color[i].checked) {
markerOption = i;
}
}
}

function LegendRoutine(){
if (family=="FAMILY"){family=" ";}
if (genus=="GENUS"){genus=" ";}
if (species=="SPECIES"){species=" ";}
if (variety=="VARIETY"){variety=" ";}
var subtaxa=genus+" "+species+" "+variety;

AddLegend=0;
if (top.SearchPage.SearchForm.TreeNum.value != "0"){
PersLegend=top.SearchPage.SearchForm.TreeNum.value;}

else if (top.SearchPage.SearchForm.ComName.value != ""){
PersLegend=top.SearchPage.SearchForm.ComName.value;}

else if (subtaxa=="     ")
{PersLegend=family;}
else
{PersLegend=subtaxa;}
if(area != "AREA"){
PersLegend=PersLegend+" "+area;
}
//alert(PersLegend);
}
function GetDisplayVariables(){
treenum=top.SearchPage.SearchForm.TreeNum.value;
comname=top.SearchPage.SearchForm.ComName.value;
var val=top.SearchPage.SearchForm.PickArea.selectedIndex;
area=top.SearchPage.SearchForm.PickArea.options[val].innerHTML;
family=top.SearchPage.SearchForm.PickFamily.value;
genus=top.SearchPage.SearchForm.PickGenus.value;
species=top.SearchPage.SearchForm.PickSpecies.value;
variety=top.SearchPage.SearchForm.PickVariety.value;
matches=top.ResultsPage.RunSearch.Matches.value;
}