var LoadPostFamily = -1; //if refining search, then FAMNO, otherwise -1
var LoadPostGenus = -1; //ditto
var LoadPostSpecies = -1; //ditto

String.prototype.trim = function () {
   return this.replace(/^\s*/, "").replace(/\s*$/, "");
};

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
};
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
};

function FamPicked() {
// disable and reset subsidary options
	document.getElementById('PickedSpeciesID').value = -1; 
	document.getElementById('PickedSpeciesID').setAttribute('disabled', true);
	if (document.getElementById('PickedGenusID')) {
		document.getElementById('PickedGenusID').value = -1; 
		document.getElementById('PickedGenusID').setAttribute('disabled', true);
	}
	if (document.getElementById('EnteredGenusStr')) {
		document.getElementById('EnteredGenusStr').value = '[now click Show Genera]'; 
		document.getElementById('EnteredGenusStr').setAttribute('disabled', true);
	}
	if (document.getElementById('PickedFamilyID').value > -1) {
		RunSearchGenus('famno=' + document.getElementById('PickedFamilyID').value.toString()); //cascade to species if possible
	} else {
		document.getElementById('GenusZone').innerHTML = "<input id='EnteredGenusStr' style='width: 480px' onKeyPress='SeeIfGenusSearch(event);'>";
	}
	FloraInfoGenusLink();
	FloraInfoSpeciesLink();
}

function GenPicked() {
//disable and reset subsidary options
	if (document.getElementById('PickedSpeciesID')) {
		document.getElementById('PickedSpeciesID').value = -1; 
		document.getElementById('PickedSpeciesID').setAttribute('disabled', true);
	}
	if (document.getElementById('PickedGenusID').value > -1) {
		RunSearchSpecies('genno=' + document.getElementById('PickedGenusID').value.toString());		
	}
	FloraInfoSpeciesLink();
}

function FindGenus() {
	var srch = document.getElementById('EnteredGenusStr').value.toString().trim();
	if (srch.toString() > '') {
		if (srch.toString().match(/^[a-z*A-Z]+$/)) {
			RunSearchGenus('namelike=' + srch.toString()); //cascade to spp if possible
		} else {
			window.alert("Please only use alphabetic characters and the '*' as a wildcard.");
		}
	} else {
		window.alert("Please enter the genus name. You can use a '*' as a wildcard, so for example entering 'a*' will find all genera beginning with 'a'.");
	}
	return;
}

function ReadySubmit() {
// returns 0 if user is currently entering a genus search string, otherwise -1
	var j = 0;
	var radio_buttons = new Array();
	var the_form = window.document.forms[0];
	var retval = -1;
	
	//check if single genus entered
	if (document.getElementById('EnteredGenusStr')) {
		if (document.getElementById('EnteredGenusStr').value > '') {			
			RunSearchGenus('namelike=' + document.getElementById('EnteredGenusStr').value.toString(), -1); //don't cascade to spp
			return 0;			
			// when genus results are loaded if there is only one, push through to search results
		}
	}
	for (var i = 0; i < the_form.length; i++) {
		var temp = the_form.elements[i].type;
	  if ((temp == "radio") && (the_form.elements[i].checked)) { document.getElementById('PostoptListType').value = the_form.elements[i].value; }
	  if (temp == "select") { window.alert(temp.name); }
	}
	document.getElementById('catDistribs').value = ' |';	// leading space so strpos will return > 0
	var colldistrib = document.getElementById('optDistrib');	
	for (i = 0; i < colldistrib.options.length; i++) {
		if (colldistrib.options[i].selected) {
			document.getElementById('catDistribs').value += colldistrib.options[i].value + '|'; 
		}
	}
	
	document.getElementById('PostFamilyID').value = document.getElementById('PickedFamilyID').value;
	if (document.getElementById('PickedGenusID')) { document.getElementById('PostGenusID').value = document.getElementById('PickedGenusID').value; }
	document.getElementById('PostSpeciesID').value = document.getElementById('PickedSpeciesID').value;
if (document.getElementById('optSynonyms').checked) { document.getElementById('PostoptSynonyms').value = true; }
	
	return retval;
}

function ShowQDShelp() {
	window.alert('Grid references comprise latitude (2 digits, south), longitude (2 digits, east) and 2 letters (each A-D) indicating the degree quadrant (NW=A, NE=B, SW=C, SE=D) and the quarter-degree quadrant respectively.  e.g. 3318AD means the quarter degree square at 33 degrees south, 18 degrees east, NW quadrant, SE quarter-degree quadrant.');
	return;
}

function isEmptySearch() {
	boolIsEmpty = true;
	
	if (document.getElementById('PickedFamilyID').value != -1) { boolIsEmpty = false; }
	if (document.getElementById('PickedGenusID')) {
		if (document.getElementById('PickedGenusID').value != '-1') { boolIsEmpty = false; }
	}
	if (document.getElementById('PickedSpeciesID').value != -1) { boolIsEmpty = false; }
	if (document.getElementById('optLifecycle').value != 0) { boolIsEmpty = false; }
	if (document.getElementById('optLifeform').value != 0) { boolIsEmpty = false; }
  if (document.getElementById('optThreat').value != 0) { boolIsEmpty = false; }
  if (document.getElementById('optEndemic').value != 0) { boolIsEmpty = false; }
	if (document.getElementById('catDistribs').value != ' |') { boolIsEmpty = false; }
	if (document.getElementById('optHeightMax').value != '') { boolIsEmpty = false; }
	if (document.getElementById('optHeightMin').value != '') { boolIsEmpty = false; }
	if (document.getElementById('optAltitudeMax').value != '') { boolIsEmpty = false; }
	if (document.getElementById('optAltitudeMin').value != '') { boolIsEmpty = false; }
	if (document.getElementById('optQDS').value != '') { boolIsEmpty = false; }
	return boolIsEmpty;
}

function isNumeric(strString) {
   if (strString.match(/^-{0,1}\d*\.{0,1}\d+$/)) { return true; }
   return false;
}

function isAlphabetic(strString) {
   if (strString.match(/^[a-zA-Z]+$/)) { return true; }
   return false;  
}

function ValidateNum(objId) {
// returns false if not a number, and produces an error message and blanks the offending field if it contained text
  if (document.getElementById(objId).value == '') { return false; }
  if (!isNumeric(document.getElementById(objId).value)) { 
  	window.alert('Please enter a number'); 
  	document.getElementById(objId).value = '';
  	document.getElementById(objId).focus();
  	document.getElementById(objId).select();
  	return false;
  }
  return true;
}

function ValidateQDS(objId) {  
// returns false if not a valid QDS, and produces an error message and blanks the offending field
	var qds = document.getElementById(objId).value;
	var numpart = 0;
	var strpart = '';
	var allok = 1;
  if (qds == '') return false;
  numpart = qds.substring(0,4);
  strpart = qds.substring(4,6).toUpperCase();
  if ((qds.length < 4) || (qds.length > 6) || !isNumeric(numpart)) allok = 0;
  
  if ((qds.length > 4) && !isAlphabetic(strpart)) allok = 0;
  	
  if (qds.length > 4 && (allok == 1)) {
  	var lettersok = 1;
  	//window.alert(strpart.substring(0,1) + "=" + 'ABCD'.indexOf(strpart.substring(0,1)).toString());
  	if ('ABCD'.indexOf(strpart.substring(0,1)) == -1) lettersok = 0;  	
  	if (qds.length > 5) {  		
  		//window.alert(strpart.substring(1,2) + "=" + 'ABCD'.indexOf(strpart.substring(1,2)).toString());
  		if ('ABCD'.indexOf(strpart.substring(1,2)) == -1) lettersok = 0;  
  	}
  	if (lettersok == 0) allok = 0;  		
  }
  if (allok == 0) {
  	window.alert('Please enter a QDS e.g. 3318AD or degree square, e.g. 3318');
  	document.getElementById(objId).value = '';
  	document.getElementById(objId).focus();
  	document.getElementById(objId).select();
  	return false;
  }
  document.getElementById(objId).value = qds.toUpperCase();
  return true;
}

function resetfrms() {
	window.location = 'searchspp.php';
	//location.reload();
}

function PrepAndTrySubmit() {
	var rs = 0;
	
	rs = ReadySubmit();	
	if (rs == -1) { 
		if (isEmptySearch()) window.alert('Please specify the criteria for your search'); 
		else {
			document.submitfrm.submit(); 
		}
	}
}

function LoadPostedTaxa() {
		
	if (document.getElementById('PostFamilyID').value > 0) LoadPostFamily = document.getElementById('PostFamilyID').value;
	
	if (document.getElementById('PostGenusID').value > 0) LoadPostGenus = document.getElementById('PostGenusID').value;		
	
	if (document.getElementById('PostSpeciesID').value > 0) LoadPostSpecies = document.getElementById('PostSpeciesID').value;
	
	RunSearchFamily('');
	return;
} // LoadPostedTaxa

function FloraInfoParam(param) {
	var winFlora = window.open('flora/results.php?' + param.toString() + '&showdetail=F');
	//window.location = 'flora/results.php?' + param.toString() + '&showdetail=F';
	//winflora.focus();
	return;
} // FloraInfoParam

function FloraInfoGenusLink() {
	if (document.getElementById('PickedGenusID')) {
		var strG = document.getElementById('PickedGenusID').value.toString();
		if (strG != '-1') {
			document.getElementById('FloraInfoGenusLinkSpace').innerHTML = "<a href='flora/results.php?taxon=genno=" + strG.valueOf() + "&showdetail=T'><img src='images/regfloradisa_big.gif' height='22' width='22' border='0' align='absbottom' alt='Flora' title='Floristic information for this genus'></a>";
		} else {
			document.getElementById('FloraInfoGenusLinkSpace').innerHTML = "";
		}
	} else {
		document.getElementById('FloraInfoGenusLinkSpace').innerHTML = "<input type='button' name='FindGen' value='Find genus' style='width: 100px' onclick='FindGenus();'>";
	}
	return;
} //FloraInfoGenusLink

function FloraInfoSpeciesLink() {	
	if (document.getElementById('PickedSpeciesID')) {
		var strS = document.getElementById('PickedSpeciesID').value.toString();
		if (strS != '-1') {
			var strG = document.getElementById('PickedGenusID').value.toString();
			document.getElementById('FloraInfoSpeciesLinkSpace').innerHTML = "<a href='flora/results.php?taxon=genno=" + strG.valueOf() + ",spno=" + strS.valueOf() + "&showdetail=T'><img src='images/regfloradisa_big.gif' height='22' width='22' border='0' align='absbottom' alt='Flora' title='Floristic information for this species'></a>";
		} else {
			document.getElementById('FloraInfoSpeciesLinkSpace').innerHTML = "";
		}
	}
	return;
} //FloraInfoSpeciesLink

function FloraInfoFamilyLink() {
	var strF = document.getElementById('PickedFamilyID').value.toString();
	if (strF != '-1') {
		document.getElementById('FloraInfoFamilyLinkSpace').innerHTML = "<a href='flora/results.php?taxon=famno=" + strF.valueOf() + "&showdetail=F'><img src='images/regfloradisa_big.gif' height='22' width='22' border='0' align='absbottom' alt='Flora' title='Floristic information for this family'></a>";
	} else {
		document.getElementById('FloraInfoFamilyLinkSpace').innerHTML = "";
	}
	return;
} //FloraInfoFamilyLink

function SeeIfGenusSearch(evt) {
	if (evt.keyCode == 13) {
		FindGenus();
	}
	return;
} //SeeIfSearch

function MM_preloadImgs() { 
	var d=document;
	if(d.images) { 
		if (!d.MM_p) d.MM_p=new Array(); 
		var i, j=d.MM_p.length, a=MM_preloadImgs.arguments; 
		for(i=0; i<a.length; i++) 
			if (a[i].indexOf('#')!=0) { 
				d.MM_p[j]=new Image; 
				d.MM_p[j++].src='images/' + a[i];
			}
	}
}