function advancedSearchToggle() {
	var link = document.getElementById('advancedSearch');
	
	if(link.innerHTML =='Advanced Search') {
		var addRemoveButtons = document.getElementById('advancedSearchButtons');
		var radioButtons = document.getElementById('advancedSearchRadios');
		addRemoveButtons.style.display = 'block';
		radioButtons.style.display = 'block';
		link.innerHTML  = 'Normal Search';
		
		populateSearchIn('true');
		populateSearchType('cloneSearchList');
		
		
	}
	else {
		var addRemoveButtons = document.getElementById('advancedSearchButtons');
		var radioButtons = document.getElementById('advancedSearchRadios');
		var searchAllArr = document.getElementsByName('searchAll');
		var searchAll = searchAllArr[0];
		var searchAny = searchAllArr[1];
		
		addRemoveButtons.style.display = 'none';
		radioButtons.style.display = 'none';
		link.innerHTML  = 'Advanced Search';
		var searchIns= document.getElementsByName('searchIn');
		var totalSelects = searchIns.length;
		for(i=0; i< totalSelects;i++)
		{
			removeCloneSearch();
		}
		populateSearchIn('false');
		populateSearchType('cloneSearchList');
	}
}

function populateSearchIn(advancedSearch) {
	var searchIns= document.getElementsByName('searchIn');
	var searchIn = searchIns[1];
	searchIn.options.length = 0;
	var searchOption = 0;
	
	if(advancedSearch == 'false') {
		searchIn.options[0] = new Option('Search all','0');
		searchOption = 1;
	}
	
	searchIn.options[searchOption] = new Option('Gene symbol','1');
	searchIn.options[searchOption + 1] = new Option('NCBI accession','2');
	searchIn.options[searchOption + 2] = new Option('cDNA library name','3');
	searchIn.options[searchOption + 3] = new Option('Clone name','4');
	searchIn.options[searchOption + 4] = new Option('Anatomy item','5');
	searchIn.options[searchOption + 5] = new Option('Clone page ID', '6');
	
}

function populateSearchType(searchInId,searchTypeValue) {
  	var searchIn= document.getElementById(searchInId);
  	var searchInNode = getSearchInNodePosition(searchInId);
  	
	var selectedOption = searchIn.options[searchIn.selectedIndex];
	
	var searchTypeDiv = document.getElementById("cloneSearchType");
	if(selectedOption.text != 'Search all') {
		searchTypeDiv.setAttribute("style", "");
	}
	else {
		searchTypeDiv.setAttribute("style", "display:none;");
	}
		
	var searchTypes = document.getElementsByName('searchType');
	var searchType = searchTypes[searchInNode - 1];
	searchType.options.length = 0; //clear the searchType dropdown
	  
  	if(selectedOption.text=='NCBI accession' || selectedOption.text=='cDNA library name' || selectedOption.text=='Clone page ID' || selectedOption.text=='Anatomy item') {	
		searchType.options[0] = new Option('is','is'); //Just add one searchType in there, 'contains' for exact match.
  	}
	else if(selectedOption.text=='Gene symbol' || selectedOption.text=='Clone name') {
		searchType.options[0] = new Option('contains','contains');
		searchType.options[1] = new Option('is','is');
	}

	if(searchTypeValue != null) {
		searchType.value = searchTypeValue.value;
	}
}

function getSearchInNodePosition(searchInId) {
	var searchIns= document.getElementsByName('searchIn');
	var searchInsLength = searchIns.length;
	
	for(var i=0; i < searchInsLength; i++) {
		var id = searchIns[i].id;
		if(id == searchInId) {
			return i;
		}
	}
}

function addCloneSearch(sIn,sValue) {

	var searchIns= document.getElementsByName('searchIn');
	var searchType = document.getElementsByName('searchType');
	var searchValues = document.getElementsByName('searchValue');
	
	//Each element added again will have its id attribute = name+"_"+htmlElementCount;
	htmlElementIdCount = (searchValues.length);

	var newSearchValue = searchValues[1].cloneNode(true);
	var newSelect = searchIns[1].cloneNode(true);
	var searchType = searchType[0].cloneNode(true);

	newSearchInId = "cloneSearchList" + "_"+ htmlElementIdCount;
	newSelect.setAttribute("id",newSearchInId);
	newSelect.setAttribute("name","searchIn");
	if(sIn == null) {
		newSelect.value=searchIns[1].value;
	}
	else {
		newSelect.value=sIn;
	}

	id = "cloneSearchBox"+ "_"+ htmlElementIdCount;
	newSearchValue.setAttribute("id",id);
	newSearchValue.setAttribute("name","searchValue");
	if(sValue == null) {
		newSearchValue.value='';
	}
	else {
		newSearchValue.value=sValue;
	}
	var cloneSuggest = "clone_suggest_"+htmlElementIdCount;
	newSearchValue.setAttribute("onblur","setTimeout(\"closeSearchSuggest('" + cloneSuggest + "');\", 200)");
	newSearchValue.setAttribute("onkeyup","cloneSuggestionDisplay(event,this.value,'" + id + "','" + cloneSuggest + "','" + newSearchInId + "')");
	
	id = "searchType" + "_"+ htmlElementIdCount;
	searchType.setAttribute("id",id);
	searchType.setAttribute("name","searchType");
	searchType.options.length = 0;
	
	var selectedOption = newSelect.options[newSelect.selectedIndex];
	
	if(selectedOption.text=='NCBI accession' || selectedOption.text=='cDNA library name' || selectedOption.text=='Clone page ID' || selectedOption.text=='Anatomy Item') {	
		searchType.options[0] = new Option('is','is'); //Just add one searchType in there, 'contains' for exact match.
  	}
	else if(selectedOption.text=='Gene symbol' || selectedOption.text=='Clone name') {
		searchType.options[0] = new Option('contains','contains');
		searchType.options[1] = new Option('is','is');
	}

	var lineBreak = document.createElement("br");
	var anotherLineBreak = document.createElement("br");
	
	var span = document.createElement("span");
	span.innerHTML = "&nbsp;";
	var posSpan = document.createElement("span");
	posSpan.style.paddingLeft = "3px";
	var span2 = document.createElement("span");
	span2.innerHTML = "&nbsp;";
	var span3 = document.createElement("span");
	span3.innerHTML = "&nbsp;";
	var suggestDiv = document.createElement("div");
	suggestDiv.setAttribute("id", cloneSuggest);
	suggestDiv.setAttribute("style", "position:absolute;background-color: #FFFFFF;text-align: left;border: 0px solid #000000;left:400px");

	var searchBlock = document.getElementById('searchBlock');
	if(searchIns.length < 4) {
		searchBlock.appendChild(lineBreak);
		
		if(searchIns.length > 2) {
			searchBlock.appendChild(anotherLineBreak);
		}
		searchBlock.appendChild(posSpan);
		searchBlock.appendChild(newSelect);
		searchBlock.appendChild(span);
		searchBlock.appendChild(searchType);
		searchBlock.appendChild(span2);
		searchBlock.appendChild(newSearchValue);
		searchBlock.appendChild(suggestDiv);
		
	}
}

function removeCloneSearch() {

 	var selects = document.getElementsByName('searchIn');
	var searchBlock = document.getElementById('searchBlock');
	if(selects.length > 2) {
		searchBlock.removeChild(searchBlock.lastChild); //remove lineBreak
		if(selects.length > 3) {
			searchBlock.removeChild(searchBlock.lastChild); // remove another lineBreak
		}
		searchBlock.removeChild(searchBlock.lastChild); //remove posSpan
		searchBlock.removeChild(searchBlock.lastChild); //remove newSelect
		searchBlock.removeChild(searchBlock.lastChild); //remove span
		searchBlock.removeChild(searchBlock.lastChild); // search type
		searchBlock.removeChild(searchBlock.lastChild); // span2
		searchBlock.removeChild(searchBlock.lastChild); //remove newSearchValue
		searchBlock.removeChild(searchBlock.lastChild); // suggestDiv
	}
}

function getSelectedOption(options, selectedOption) {
	var numOptions = options.length;
	
	for(var i = 0; i < numOptions; i++) {
		var optionValue = options[i].value;
		
		if(optionValue == selectedOption) {
			return i;
		}
	}
	return -1;
}
