var searchSelect;
var searchTextBox;
var optionList;
var value;
var taxonId2Box;

function containsSpecialCharacters(text){
    var iChars = "*|,\":<>[]{}`\';\\/()@&$#%";
    for (var i = 0; i < text.length; i++) {
        if (iChars.indexOf(text.charAt(i)) != -1){
            return true;
        }           
    }
    return false;
}
function combotext_onkeyup(e){
    searchSelect = document.getElementById("searchSelect");
    searchTextBox = document.getElementById("speciesName");
    optionList = document.getElementById("searchSelect");
   
   taxonId2Box = document.getElementById("TAXON_ID2");
   
    if (e.keyCode == 40 || e.keyCode == 38) {
        searchSelect.focus(); 
        comboselect_onchange(searchSelect, searchTextBox); 
    }
    else if (e.keyCode == 13) {
        //change document.getElementById("gotoPublic").focus();  
    }
    else if(containsSpecialCharacters(searchTextBox.value)){
        alert("The search cannot contain any of the following characters *|,\":<>[]{}`\';/\\()@&$#%");
        searchTextBox.value = searchTextBox.value.substring(0,searchTextBox.value.length-1);
    }
    else{
        if(searchTextBox.value.length >= 3){
            getSpeciesFromEntry(searchTextBox.value);               
        }
        else{
            document.getElementById("searchSelect").style.display = 'none';
        }           
    }
}   
function showMatchingSpecies(matchingSpecies){
    optionList = document.getElementById("searchSelect");
    optionList.options.length = 0;
    if(matchingSpecies.length>0){    	
        for(var i = 0; i<matchingSpecies.length;i++){
        	var nameAndTaxon = matchingSpecies[i].split("::");
            optionList.options[optionList.options.length] = new Option(nameAndTaxon[0], nameAndTaxon[1], false, false);
        }
        optionList.style.zindex = 999;
        optionList.style.display = 'block';
    }
    else{
        optionList.style.display = 'none';
    }
}
 
function comboselect_onchange() { 
    searchSelect= document.getElementById("searchSelect");
    
    taxonId2Box = document.getElementById("TAXON_ID2");
    
    searchTextBox = document.getElementById("speciesName");
  if(searchSelect.selectedIndex != -1){ 
 	 //searchTextBox.text = searchSelect.options[searchSelect.selectedIndex].text;
 	 
 	taxonId2Box.value = searchSelect.options[searchSelect.selectedIndex].value;
 	searchTextBox.value = searchSelect.options[searchSelect.selectedIndex].text
    //searchTextBox.value = searchSelect.options[searchSelect.selectedIndex].value;
    //document.getElementById("TAXON_ID").textContent="Your Choice: "+searchSelect.options[searchSelect.selectedIndex].text;
    //document.getElementById("TAXON_ID").style.fontWeight="bold";
    }
} 
 
function comboselect_onkeyup(keyCode){ 
    searchSelect= document.getElementById("searchSelect");
    
    taxonId2Box = document.getElementById("TAXON_ID2");
    
    searchTextBox = document.getElementById("speciesName");
  if (keyCode == 13) { 
    comboselect_onchange(searchSelect, searchTextBox); 
    searchSelect.style.display='none'; 
    searchTextBox.focus();     
  } 
}
function highlightSearchButton(){
    
}
function getSpeciesFromEntry(entry){
	Demo.getCompletedSpeciesFromMatch(entry,function(data){
		showMatchingSpecies(data);	
	});
}
function onAccessionBoxChange(){
	if(document.getElementById("ACCESSION_NUMBER").value.length>0){
		document.getElementById("PROTEIN_SEQUENCE").disabled=true;
	}
	else{
		document.getElementById("PROTEIN_SEQUENCE").disabled=false;
	}
}
function onCustomProteinBoxChange(){
	if(document.getElementById("PROTEIN_SEQUENCE").value.length>0){
		document.getElementById("ACCESSION_NUMBER").disabled=true;
	}
	else{
		document.getElementById("ACCESSION_NUMBER").disabled=false;
	}
}
function closeSearchDropDown(){
	comboselect_onchange(searchSelect, searchTextBox); 
    searchSelect.style.display='none'; 
    searchTextBox.focus();  	
}


