BioMartLocation = 'http://www.phytozome.net';
document.fingerprints = new Array();

function zebraTable(tableID){
	table = dojo.byId(tableID);
	rows = table.getElementsByTagName("tr");
	for(i=1;i<rows.length;i++){
	    if ( i % 2 == 1 ){
	    	rows[i].style.backgroundColor = '#FFFFFF' ;
	    }
	    else{
	    	rows[i].style.backgroundColor = '#ECECEC' ;
		}
	}

}

function make_visible(id){
	if (document.getElementById(id)){
		var vis_obj = document.getElementById(id);
		vis_obj.style.display = '';
		}
	}
	
function make_invisible(id){
	if (document.getElementById(id)){
		var invis_obj = document.getElementById(id);
		invis_obj.style.display = 'none';
		}
	}

function makeGroupVisible(pre){
	var ElementIds = collectElements(pre);
	for (i=0;i<ElementIds.length;i++){
			ElementIds[i].style.display = '';
		}
	}

function makeGroupInvisible(pre){
	var ElementIds = collectElements(pre);
	for (i=0;i<ElementIds.length;i++){
			ElementIds[i].style.display = 'none';
		}
	}

function toggleGroupVisibility(preface){
	var ElementIds = collectElements(preface);
	for (i=0;i<ElementIds.length;i++){
		if (ElementIds[i].style.display == 'none')
			ElementIds[i].style.display = '';
		else ElementIds[i].style.display = 'none';
	}
}

function toggleVisibility(id){
	obj = document.getElementById(id);
	if (obj.style.display == 'none')
		obj.style.display = '';
	else obj.style.display = 'none';
	}


function goTo (page) {
/* This function is called from the navigation menu
   to jump to the designated URL. Empty values
   are ignored and "--" indicates a menu seperator    */
   
	if (page != "" ) {
		if (page == "--" ) {
			resetMenu();
		} else {
			document.location.href = page;
		}
	}
	return false;
}




//tree.php	
function collapseTD(id){
	obj = document.getElementById(id);
	obj.style.visibility = 'collapse';	
}

//tree.php
function expandTD(id){
	obj = document.getElementById(id);
	obj.style.visibility = 'visible';
}

//results,blast results
function countIds(theElements,theName){
	var N = 0;
        for(i=0; i<theElements.length; i++){
                if(theElements[i].type == "checkbox" && theElements[i].checked == true){
                	var Name = new String(theElements[i].name);
                	if(Name == theName){
                    		 N++
	        	}
        	}
	}
	return N;
}

//results,blast results
function countIdsAndAddInputs(theElements,theName,theForm){
	//first clear out the old guys
	var R=0;
	var j=0;
//	for(var j=0; j < theForm.elements.length; j++) {
    while(theForm.elements[j]!=null){
		input = theForm.elements[j];
    		if (input.name==theName){
    			theForm.removeChild(input);
    			console.log('removed element');
    			R++;
    		}
    		else j++;
    	}
	
	
	console.log('removed '+R+' elements');
	//now get started
	var N = 0;
        for(i=0; i<theElements.length; i++){
                if((theElements[i].type == "checkbox")&&(theElements[i].checked == true)){
	                	var Name = new String(theElements[i].name);
	                	if(Name == theName){
	//                		console.log('adding element b');
	                		var newInput=document.createElement('input');
	                		newInput.type='hidden';
	                		newInput.name=theName;
	                		newInput.value=theElements[i].value;                		
	                    	theForm.appendChild(newInput);
	                    	console.log('added');
	                    	N++;
                
                }
        	}
	}
     console.log('appended '+N+' inputs');
	return N;
}

//replaces changeTabs() below
function selectTab(chosenTab){
	tabs = chosenTab.parentNode.childNodes;
	for(var i=0; i < tabs.length; i++) {
		if (tabs[i].tagName=='LI'){
			tabs[i].className = "";
			boxId = tabs[i].id.replace('tab','box');
			make_invisible(boxId);
		}
	}
	chosenTab.className="current";
	chosenBoxId = chosenTab.id.replace('tab','box');
	make_visible(chosenBoxId);
}

function selectTabRemote(listName,chosenTabName){
	list = document.getElementById(listName);
	tabs = list.childNodes;
	for(var i=0; i < tabs.length; i++) {
		if (tabs[i].tagName=='LI'){
			tabs[i].className = "";
			boxId = tabs[i].id.replace('tab','box');
			make_invisible(boxId);
		}
	}
	document.getElementById('tab_'+chosenTabName).className='current';
	make_visible('box_'+chosenTabName);
}



//many
function changeTab(set,tabName){
	//get a list of tabs
	switch(set){
		case "cluster":
			tabs = new Array ("summary","analyze","find","options","getSeq");
			break;
		case "display":
			tabs = new Array ("clusterGenes","functionalAnalysis","MSA");
			break;
		case "graphics":
			break;
		case "search":
			tabs = new Array ("text","blast");
			break;
		case 'results':
			tabs = new Array ("summary","analyze","display","ontologies");
			break;
		case 'blast':
			tabs = new Array ("graphic","analyze");
			break;
		default: 
			break;
	}	
	// unset all tabs and hide all boxes
	for(i=0; i<tabs.length;i++){
		tab = document.getElementById("tab_"+tabs[i]);
		tab.className = "";
		box = document.getElementById("box_"+tabs[i]);
		box.style.display = "none";
		}
	//then set current tab and make box visible
	setTab = document.getElementById("tab_"+tabName);
	setTab.className = "current";
	setBox = document.getElementById("box_"+tabName);
	setBox.style.display = "";	
}


//many
function collectElements(preface){	
	var i=0;
	var customcollect=new Array();
	while (document.getElementById(preface + "_" + i)!=null){
		customcollect[i]= document.getElementById(preface + "_" + i);
		i++;
		}
	return customcollect;	
	}


//search
function checkFormState(){
	if(document.getElementById('radioDNA').checked){
//		window.alert('dna checked');
		makeGroupInvisible('proteinBlast');
		makeGroupVisible('dnaBlast');		
	}
	if(document.getElementById('radioOnt').checked){
		make_visible('ontology_list');
	}
	if(document.getElementById('radioEmail').checked){
		make_visible('emailinput');
	}
	if(document.getElementById('activeForm').value == 'blast'){
		changeTab('search','blast');
	}
	else if(document.getElementById('activeForm').value == 'text'){
		changeTab('search','text');
	}
}

function checkProgram(formName){
	form=document.getElementById(formName);
	dna = document.getElementById('radioDNA');
	if (dna.checked){
		for (var j = 0; j<form.elements.length;j++){
			if(form.elements[j].name=='dnaProgram') programSelect = form.elements[j];
		}
	}
	else {
		for (var j = 0; j<form.elements.length;j++){
			if(form.elements[j].name=='proteinProgram')programSelect = form.elements[j];
		}
	}
	program = programSelect.options[programSelect.selectedIndex];
	nucPattern = /nucl.*query.*to/i;
	if (program.innerHTML.match(nucPattern)){
		for (var i = 0; i<form.elements.length;i++){
			if(form.elements[i].name=='sequence'){
				sequence = form.elements[i];
				sequence.value = sequence.value.replace(/^\s+|\s+$/g, '');
				seqArray = sequence.value.split("\n");
				isntDNA = /[^acgtnu\s]/i;
				// check to see if the first line is a FASTA header; if not, check if it's not DNA
				if (!seqArray[0].match(/^>/)){
					if (seqArray[0].match(isntDNA)) return false;
				}
				for (var j = 1; j<seqArray.length; j++){
					if (seqArray[j].match(isntDNA)) return false;
				}
				return true;
			}
		}
	}
	else return true;
}

function checkBlastForm(formName){
	form=document.getElementById(formName);
	// first determine target type	
	var targetClass = 'genome'; //default
	for (var j = 0; j<form.elements.length;j++){
		if(form.elements[j].name == 'targetClass'){
			if(form.elements[j].checked) targetClass = form.elements[j].value;
		}
	}
	console.log('target Class = '+targetClass);
	label = document.getElementById('targetLabel');
	label.innerHTML = targetClass;
	make_invisible('nodeTarget');
	make_invisible('proteomeTarget');
	make_invisible('genomeTarget');
	make_visible(targetClass+'Target');
	switch (targetClass){
		case 'node':
			make_visible('nodeParam');
			make_visible('proteinProgram');
			make_invisible('dnaProgram');
			break;
		case 'proteome':
			make_invisible('nodeParam');
			make_visible('proteinProgram');
			make_invisible('dnaProgram');
			break;
		case 'genome':
			make_invisible('nodeParam');
			make_invisible('proteinProgram');
			make_visible('dnaProgram');
			break;
	}
	restrainBlastN(formName);
}

function restrainBlastN(formName){
	form=document.getElementById(formName);
	dna = document.getElementById('radioDNA');
	for (var j = 0; j<form.elements.length;j++){
		if (dna.checked){
			if(form.elements[j].name=='dnaProgram') programSelect = form.elements[j];
		}
		else {
			if(form.elements[j].name=='proteinProgram')programSelect = form.elements[j];
		}
		if (form.elements[j].name=='showal') show = form.elements[j];
	}
	program = programSelect.options[programSelect.selectedIndex];
	if (program.value == 'blastn'){
//		window.alert('blastn selected');
		for(var k = 0; k<show.options.length;k++){
			if(show.options[k].value == 25) show.selectedIndex = k;
		}		
	}
	else {
		for(var k = 0; k<show.options.length;k++){
			if(show.options[k].value == 100) show.selectedIndex = k;
		}		

	}
}

//search
function validateBlast(form){
	if(!checkProgram(form.id)){
		seqOK = confirm('Warning: your sequence is not nucleotide sequence, and you have selected a program which requires nucleotide input. Click OK to proceed or Cancel to return to the form.');
		if(!seqOK) return false;
		}
	var missing = new Array();
	for (var i = 0; i<form.elements.length;i++){
		form.elements[i].value = form.elements[i].value.replace(/^\s+|\s+$/g, '');
		switch (form.elements[i].name){
			case "email":
				if (form.elements[i].value==1&&form.elements[i].checked){
					for (var j = 0; j<form.elements.length;j++){
						if(form.elements[j].name=='emailAddress'){
							form.elements[j].value = form.elements[j].value.replace(/^\s+|\s+$/g, '');
							if (form.elements[j].value == ''){
								missing.push(form.elements[j].name);
							}
						}
					}
				}
				break;
			case "sequence":
			case "ethr":
			case "program":
			case "output":
			case "matrix":
			case "wordlength":
			case "showal":
				if (form.elements[i].value == ''){
					missing.push(form.elements[i].name);
					}
				break;
			case "targetClass":
				if(form.elements[i].checked){
					for (var j = 0; j<form.elements.length;j++){
						if(form.elements[j].name.match(form.elements[i].value)){
							if (form.elements[j].value == ''){
								missing.push(form.elements[j].name);								
							}
						}
					}
				}
				break;
			default:
				break;
			}
		}
		// to check form values
		/*
		for (var i = 0; i<form.elements.length;i++){
			console.log(form.elements[i].name + ' = ' + form.elements[i].value);
		}
		*/
		if(missing.length>0){
			var warning = '';
			for (var a = 0; a<missing.length;a++){
				if(missing[a] == 'emailAddress') warning = '- To receive email notification, you must provide an email address.\n\n'+warning;
				else if(missing[a].match('Target')) warning = '- Please choose a target database.\n\n'+warning;
				else warning = warning + 'missing parameter: '+missing[a]+'\n';
			}
			window.alert(warning);
			return false;
		}
		else return true;
	}

//search
function validateKeyword(form){
	for (var i = 0; i<form.elements.length;i++){
		form.elements[i].value = form.elements[i].value.replace(/^\s+|\s+$/g, '');
		switch (form.elements[i].name){
			case "method":
				if (form.elements[i].value == ''){
					window.alert('You must select a node');
					return false;
					}
				break;

			case "searchText":
				if (form.elements[i].value == ''){
					window.alert('Missing parameter: '+form.elements[i].name);
					return false;
					}
				break;
			case "orgFilter":
				if (form.elements[i].checked){
					orgProfile = collectOrgs(form);
					orgInput = document.getElementById('orgProfileInput');
					if(orgProfile.match(/[01]/)){
						//mask functionality
						orgProfile = maskProfile(orgProfile);
						orgInput = document.getElementById('orgProfileInput');
						orgInput.value = orgProfile;
					}
					else orgInput.value = '';
				}
				else {
					document.getElementById('orgProfileInput').value='';
				}
				break;
			default:
				break;
			}
		}
	return true;
//	return false;
	}

function maskProfile(profile){
	nodeSelect = document.getElementById('nodeSelect');
	node = nodeSelect.options[nodeSelect.selectedIndex].value;
	if(node=='0') return profile;//if this is an all nodes search, there is no mask
	theMask = String(mask[node]);
	nodeMaskArray = theMask.split('');
	profileArray = profile.split('');
	maskedProfile = '';
	if (nodeMaskArray.length!=profileArray.length){
		window.alert ('lengths don\'t match\nmask:' +theMask+' ('+ nodeMaskArray.length +')\nprofile: '+profile+' ('+profileArray.length+')');
	}
//	else window.alert('lengths match');
	for (i=0; i<nodeMaskArray.length;i++){
		if(nodeMaskArray[i]==0){
			maskedProfile=maskedProfile+'?';
		}
		else{
			maskedProfile = maskedProfile+profileArray[i];
		}
	}
//	window.alert('masked profile = '+maskedProfile);
	return maskedProfile;
}

function selectFilter(){
	filter = document.getElementById('useFilter');
	if (filter.checked) make_visible('orgProfileRow');
	else make_invisible('orgProfileRow');

	select = document.getElementById('nodeSelect');
	node = select.options[select.selectedIndex].value;
	proteomes = '';
	if (window.includedProteomes){
		for(i in includedProteomes[0]){
			make_invisible('row'+includedProteomes[0][i]+'_on');
			make_visible('row'+includedProteomes[0][i]+'_off');
		}
		for(k in includedProteomes[node]){
			proteomes = proteomes+includedProteomes[node][k]+',';
			make_invisible('row'+includedProteomes[node][k]+'_off');
			make_visible('row'+includedProteomes[node][k]+'_on');
		}
	}
}

function collectOrgs(form){
	orgs= new Array();
	for (var j = 0; j<form.elements.length;j++){
		if(a = form.elements[j].name.match(/filter(\d+)$/)){
			if (form.elements[j].checked){	
				orgs[a[1]] = [form.elements[j].value];
			}
		}
	}
	//sort these values by proteome id to ensure the profile is in the correct order
	var keys = new Array();
	for(k in orgs){
		 keys.push(k);
	}
	keys.sort( function (a, b){return (a - b);} );	
	orgProfile = '';
	for (var l = 0; l < keys.length; l++){
		orgProfile = orgProfile+orgs[keys[l]];
	}
	return orgProfile;
}


//search
function DisableButton(id) {
	b = document.getElementById(id);
	b.disabled = true;
	b.value = 'Submitting';
//	b.form.submit();
	}

//cluster (use for results too?)	
function uncheckAll(c){
	inputs = document.getElementsByTagName('input');
	for(var i=0; i < inputs.length; i++) {
		if (inputs[i].name == c && inputs[i].checked){
			checkChange(i);
		}
	}
}

function checkAll(c){
	inputs = document.getElementsByTagName('input');
	for(var i=0; i < inputs.length; i++) {
		if (inputs[i].name == c && !inputs[i].checked){
			checkChange(i);
		}
	}
}


/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
*/
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects
	 for(var item in arr) {
	  var value = arr[item];
	 
	  if(typeof(value) == 'object') { //If it is an array,
	   dumped_text += level_padding + "'" + item + "' ...\n";
	   dumped_text += dump(value,level+1);
	  } else {
	   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
	  }
	 }
	} else { //Stings/Chars/Numbers etc.
	 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
} 


//results
function loadSpecies(){
	for (i in document.show){
		cookie = readCookie(document.db+'_species'+i);
		if (cookie=="hide") sendSpeciesToOther(i);
		}
	}

function clusterJoin(form){
	var sT = document.getElementById('searchText');
	sT.value = '';
	for (var i=0; i<form.elements.length;i++){
		if (form.elements[i].name == 'cluster'){
			if(form.elements[i].checked){ 
				sT.value += ' clusterid:'+form.elements[i].value;
			}
		}
	}
	document.multiCluster.submit();
}

function expandOntologies(){
	ontBox = dojo.byId('search_ontologies');
	console.log('class = '+ontBox.className);
	if (ontBox.className == 'expanded'){
		ontBox.className = 'condensed';
	}
	else ontBox.className = 'expanded';
	console.log('done'); 
}

function launchBiomart(form){
	var clusters = '';
	for (var i=0; i<form.elements.length;i++){
		if (form.elements[i].name == 'cluster'){
			if(form.elements[i].checked){ 
				clusters += form.elements[i].value + ',';
			}
		}
	}
	clusters = clusters.replace(/,$/, '');
	BioMartLink = BioMartLocation + '/biomart/martview?'
				+'VIRTUALSCHEMANAME=zome_mart'
				+'&ATTRIBUTES='
					+'phytozome_clusters.default.sequences.peptide_name'
					+'|phytozome_clusters.default.sequences.organism_name'
					+'|phytozome_clusters.default.sequences.gene_name'
					+'|phytozome_clusters.default.sequences.transcript_name'
					+'|phytozome_clusters.default.sequences.peptide_sequence'
				+'&FILTERS='
					+'phytozome_clusters.default.cluster_filters.cluster_id_list.%22'+clusters+'%22'
				+'&DISPLAY='
					+'attributes'
				;
	window.open(BioMartLink);
}



function borderOn(object){
	object.style.borderTopStyle = 'solid';
}

function borderOff(object){
	object.style.borderTop = "";
}


//blast
function clusterJoinB(form){
	var sT='';
	for (var i=0; i<form.elements.length;i++){
		if (form.elements[i].name == 'cluster'){
			if(form.elements[i].checked){ 
				sT += ' clusterid:'+form.elements[i].value;
			}
		}
	}
	goTo('show_cluster.php?method=0&search=1&searchText='+sT+'&detail=1');
}

//results

function checkJV(form,program){
	if( countIds(document.cluster_boxes.elements,'cluster') == 0){
		alert('You must select at least one cluster');
		return false;
		}
	includeQuery = document.getElementById('includeQuery');
	if (includeQuery.checked){
		switch(program){
			case('blastp'):
				radioCod = document.getElementById('JVcod');
				if (radioCod.checked){
					window.alert('Query and cluster sequences sequences must be both peptide or both nucleotide. ĘPlease choose a different sequence type');
					return false;
					}
				break;

			case('blastx'):
				radioPro = document.getElementById('JVpro');
				radioCon = document.getElementById('JVcon');
				if (radioPro.checked||radioCon.checked ){
					window.alert('Query and cluster sequences sequences must be both peptide or both nucleotide. ĘPlease choose a different sequence type');
					return false;
					}
				break;
			}
		}
	return true;
}


function checkJV2(form,program){
	JVform = dojo.byId('Jalview_form');
	//console.log('elements'+ JVformElements);
	console.log('checking JV form. '+ countIds(document.cluster_boxes.elements,'cluster')+' clusters checked');
	if(countIdsAndAddInputs(document.cluster_boxes.elements,'cluster',JVform) == 0){
		alert('You must select at least one cluster');
		return false;
		}
	includeQuery = document.getElementById('includeQuery');
	if (includeQuery.checked){
		switch(program){
			case('blastp'):
				radioCod = document.getElementById('JVcod');
				if (radioCod.checked){
					window.alert('Query and cluster sequences sequences must be both peptide or both nucleotide. ĘPlease choose a different sequence type');
					return false;
					}
				break;

			case('blastx'):
				radioPro = document.getElementById('JVpro');
				radioCon = document.getElementById('JVcon');
				if (radioPro.checked||radioCon.checked ){
					window.alert('Query and cluster sequences sequences must be both peptide or both nucleotide. ĘPlease choose a different sequence type');
					return false;
					}
				break;
			}
		}
	JVform.submit();
	//now clear out the added inputs
//	removeInputs(JVform,'cluster');
}




//results, cluster
function sendSpeciesToOther(species){
	var i=0;
	var SPcells = new Array();
	var OTHcells = new Array();
	title = document.getElementById("title_"+species);
	title.style.display = "none";
	while (document.getElementById("row_"+i)!=null){
		SPcells[i] = document.getElementById("row_" + i +"_proteome_" +species);
		SPcells[i].style.display = "none";
		document.others[i] = document.others[i] + document.members[species][i];		
		OTHcells[i] = document.getElementById("row_" + i +"_other");
		OTHcells[i].innerHTML = document.others[i];
		i++;
		}
	make_invisible('hide'+species);
	make_visible('show'+species);
	document.show[species] = 0;
}


//results, cluster
function getSpeciesFromOther(species){
	var i=0;
	var SPcells = new Array();
	var OTHcells = new Array();
	
	title = document.getElementById("title_"+species);
	title.style.display = "";
	
	while (document.getElementById("row_"+i)!=null){
		SPcells[i] = document.getElementById("row_" + i +"_proteome_" +species);
		SPcells[i].style.display = "";

		document.others[i] = document.others[i] - document.members[species][i];
		
		
		OTHcells[i] = document.getElementById("row_" + i +"_other");
		if (document.others[i] == 0){
			OTHcells[i].innerHTML = "-";
			}			
		else OTHcells[i].innerHTML = document.others[i];

		i++;
		}
	make_visible('hide'+species);
	make_invisible('show'+species);
	document.show[species] = 1;
}

// cluster, results
function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "; expires=Thu, 31-Dec-2020 00:00:00 GMT";
	document.cookie = name+"="+value+expires+"; path=/";
}
// cluster,results
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


//cluster,results
function setSpeciesCookies(){
	for (i in document.show){
		if (document.show[i]) 
			setCookie(document.db+'_species'+i,"show",365);
		else setCookie(document.db+'_species'+i,"hide",365);
		}	
	window.alert('Species settings have been saved');
	}
//cluster,results
function readSpeciesCookies(){
	for (i in document.show){
		cookie = readCookie(document.db+'_species'+i);
		if (cookie=="hide") hideSpecies(i);
		}
	}





//cluster
function getSeqVal(form){
	var orgs = "";
	for (var i = 0; i<form.elements.length;i++){
		if (form.elements[i].name == 'exclude')
		excluded = form.elements[i];
	}
	for (i in document.show){
		if (!document.show[i])
		orgs = orgs+ "," + i ;	
	}
	orgs = orgs.substring(1);
	if (orgs.length>0){
		excluded.value = orgs;
		window.alert('Please note that not all organisms will be included');
	}
	else excluded.value = -1;
	

	return true;
	}
//cluster
function checkJVradios(action,program){
	JVpro = document.getElementById('JVpro');
	JVcod = document.getElementById('JVcod');
	JVcon = document.getElementById('JVcon');
	if (action=='includeQuery'){
		if (program=='blastp'){
		
		}
		else if (program=='blastx'){
		
		}
	}
	else if (action=='excludeQuery'){
		JVpro.disabled= false;
		make_visible(document.JVproButId);
		make_invisible('JVproInactive');
		JVcod.disabled= false;
		make_visible(document.JVcodButId);
		make_invisible('JVcodInactive');
		JVcon.disabled= false;
		make_visible(document.JVconButId);
		make_invisible('JVconInactive');
	}
}


	
//cluster
function typeNuc(action){
	nbut = document.getElementById('radioNuc');
	if (action=='disable'){
		radioChange(document.pepInpId);
		nbut.disabled = true;
		make_invisible(document.nucButId);
		make_visible('radioNucInactive');
	}
	else if (action=='enable'){
		nbut.disabled= false;
		make_visible(document.nucButId);
		make_invisible('radioNucInactive');
	}
}
//cluster
function getOnts(){
	var ont = new String("");
	for(i=0; i<document.ont_search.elements.length; i++){
		if(document.ont_search.elements[i].type == "checkbox" && document.ont_search.elements[i].checked == true){
		var N = new String(document.ont_search.elements[i].name);
		if(N=="ontId"){
			ont = ont + " " +
			document.ont_search.elements[i].value;
			}
		}
	}
	document.search2.searchText.value = ont;
	if (ont.length<1){
		window.alert('No ontologies selected');
		return false;
		}
	else return true;
	}
	
//cluster
function hideSpecies(proteomeID){
	pacids = document.genes[proteomeID];
	for(i=0;i<pacids.length;i++){
		// cluster gene table
		a = document.getElementById("row"+pacids[i]);
		a.style.display = "none";
		// MSA
		if(b = document.getElementById("msa_label"+pacids[i]))
		b.style.display = "none";
		if(c = document.getElementById("msa_line"+pacids[i]))
		c.style.display = "none";
		}
	sendSpeciesToOther(proteomeID);
	make_invisible('hide'+proteomeID);
	make_visible('show'+proteomeID);
	document.show[proteomeID] = 0;
	}

//cluster
function showSpecies(proteomeID){
	pacids = document.genes[proteomeID];
	for(i=0;i<pacids.length;i++){
		a = document.getElementById("row"+pacids[i]);
		a.style.display = "table-row";
		if(b = document.getElementById("msa_label"+pacids[i]))
		b.style.display = "table-row";
		if(c = document.getElementById("msa_line"+pacids[i]))
		c.style.display = "table-row";
		}
	getSpeciesFromOther(proteomeID);	
	make_invisible('show'+proteomeID);
	make_visible('hide'+proteomeID);	
	document.show[proteomeID] = 1;
	}
//cluster
function setLists(){
	inputs = document.getElementsByTagName('input');
	for(var i=0; i < inputs.length; i++) {
		if (inputs[i].checked ){
			boxID = inputs[i].id;
			boxName = inputs[i].name;
			if (boxName == 'geneId'){
				var re = new RegExp("geneBox(.*)");
				boxID.match(re);
				var listId = "geneSpan"+ RegExp.$1;
				make_visible(listId);
			}
			else if (boxName == 'ontId'){
				var re = new RegExp("ontBox(.*)");
				boxID.match(re);
				var listId = "ontSpan"+RegExp.$1;
				make_visible(listId);				
			}

		}		
	}	
}
//cluster
// images for use in synteny view
var geneL = "images/genes/gene_l.gif";
var geneR = "images/genes/gene_r.gif";
var highlightedGeneL = "images/genes/gene_l_red.gif";
var highlightedGeneR = "images/genes/gene_r_red.gif";
var whiteGeneL = "images/genes/gene_l_white.gif";
var whiteGeneR = "images/genes/gene_r_white.gif";
var highlightedWhiteGeneL = "images/genes/gene_l_red_white.gif";
var highlightedWhiteGeneR = "images/genes/gene_r_red_white.gif";

var anchorGeneL = "images/genes/anchorL.gif";
var anchorGeneR = "images/genes/anchorR.gif";
var highlightedAnchorL = "images/genes/anchorL_red.gif";
var highlightedAnchorR = "images/genes/anchorR_red.gif";


//cluster
function expand(geneId){
	make_visible('xtra'+geneId+'_xid');
	make_visible('xtra'+geneId+'_symbol');
	make_visible('xtra'+geneId+'_defline');
	make_visible('minus'+geneId);
	make_invisible('plus'+geneId);
	make_invisible('3dot'+geneId);
//	window.alert('expanded '+geneId);
	row = document.getElementById('row'+geneId);
	row.style.lineHeight = "16px"; // doesn't have any effect, but forces browser to rerender table row
	}
//cluster
function contract(geneId){
	make_invisible('xtra'+geneId+'_xid');
	make_invisible('xtra'+geneId+'_symbol');
	make_invisible('xtra'+geneId+'_defline');
	make_invisible('minus'+geneId);
	make_visible('plus'+geneId);
	make_visible('3dot'+geneId);

	row = document.getElementById('row'+geneId);
	row.style.lineHeight = "";

	}
//cluster
function getIds(){
	var id = new String("");
	for(i=0; i<document.gene_search.elements.length; i++){
		if(document.gene_search.elements[i].type == "checkbox" && document.gene_search.elements[i].checked == true){
			var N = new String(document.gene_search.elements[i].name);
			if(N=="geneId"){
				id = id + 
					document.gene_search.elements[i].value;
			}
		}
	}
	document.search1.searchText.value = id;
	if (id.length<1){
		window.alert('No genes selected');
		return false;
		}
	else return true;
}
//cluster
function setGraphicsTab(tabName){
	searchTabs = new Array ("domain","synteny","exons");
	//first clear
	for(i=0; i<searchTabs.length;i++){
		tab = document.getElementById("tab_"+searchTabs[i]);
		tab.style.borderColor = '#FFFFFF';
		hideColumn(searchTabs[i]);
		}
	//then set current tab and make box visible
	setTab = document.getElementById("tab_"+tabName);
	setTab.style.borderColor = "#31557F";
	showColumn(tabName);
	}
//cluster
function highlightCluster(cluster){
	clearActiveEntity();
	var clusterElementIds = collectElements("cluster" + cluster);
	for (i=0;i<clusterElementIds.length;i++){
		if (clusterElementIds[i].src.match(geneL))
			clusterElementIds[i].src = highlightedGeneL;
		else if (clusterElementIds[i].src.match(whiteGeneL))
			clusterElementIds[i].src = highlightedWhiteGeneL;
		else if (clusterElementIds[i].src.match(geneR))
			clusterElementIds[i].src = highlightedGeneR;
		else if (clusterElementIds[i].src.match(whiteGeneR))
			clusterElementIds[i].src = highlightedWhiteGeneR;
		else if (clusterElementIds[i].src.match(anchorGeneL))
			clusterElementIds[i].src = highlightedAnchorL;
		else if (clusterElementIds[i].src.match(anchorGeneR))
			clusterElementIds[i].src = highlightedAnchorR;
		}	

	var boxId = 'cluster_' + cluster + '_infobox';
	document.active_entity = "cluster_" + cluster;
	make_visible(boxId);
	}

// this (called by highlight cluster and domain)
function clearActiveEntity(){
	if (document.active_entity){
		var re1 = new RegExp("cluster_(.*)");
		var re2 = new RegExp("domain_(.*)");
		if (document.active_entity.match(re1)){
//			clearClusters();
			var cluster = RegExp.$1;
			var clusterElementIds = collectElements("cluster" + cluster);
			for (i=0;i<clusterElementIds.length;i++){
				if (clusterElementIds[i].src.match(highlightedGeneL))
					clusterElementIds[i].src = geneL;
				else if (clusterElementIds[i].src.match(highlightedGeneR))
					clusterElementIds[i].src = geneR;
				else if (clusterElementIds[i].src.match(highlightedWhiteGeneL))
					clusterElementIds[i].src = whiteGeneL;
				else if (clusterElementIds[i].src.match(highlightedWhiteGeneR))
					clusterElementIds[i].src = whiteGeneR;
				else if (clusterElementIds[i].src.match(highlightedAnchorL))
					clusterElementIds[i].src = anchorGeneL;
				else if (clusterElementIds[i].src.match(highlightedAnchorR))
					clusterElementIds[i].src = anchorGeneR;
				}				
			}
		else if (document.active_entity.match(re2)){ 
			var domain = RegExp.$1;
			var domainElementIds = collectElements(domain);
			for (i=0;i<domainElementIds.length;i++){
				domainElementIds[i].style.borderStyle = "none";
				domainElementIds[i].style.zIndex = "0";
				}				
			}
		infoboxId = document.active_entity + "_infobox";
		make_invisible(infoboxId);
		document.active_entity = undefined;			
		}
	else {
		make_invisible('infobox_intro');
		make_invisible('infobox_introSyn');
		make_invisible('infobox_introDom');		
		}
	}
//cluster
function highlightDomain(domain){
	if (document.getElementById(domain+'_300')){
		window.alert('I\m sorry. I can\'t do it. There\'s just too many of them.');
		return;
	}
//	clearActiveEntity();
	var domainElementIds = collectElements(domain);
	for (i=0;i<domainElementIds.length;i++){
		domainElementIds[i].style.borderStyle = "solid";
		domainElementIds[i].style.zIndex = "1";
		}	
//	var boxId = 'domain_' + domain + '_infobox';
//	make_visible(boxId);
//	changeTab('cluster','info');
//	document.active_entity = "domain_" + domain;
	}
//cluster
function syntenyUp(){
	if (document.synteny < document.max_match){
		document.synteny++;
		re = /match(\d+)/;
		var all = document.getElementsByTagName('img');
		for (var e = 0; e < all.length; e++){
			if (all[e].className.match(re)){
				re.exec(all[e].className);
				if (RegExp.$1<document.synteny){
					if (all[e].src.match(highlightedGeneL))
						all[e].src = highlightedWhiteGeneL;
					else if (all[e].src.match(geneL))
						all[e].src = whiteGeneL;
					else if (all[e].src.match(highlightedGeneR))
						all[e].src = highlightedWhiteGeneR;
					else if (all[e].src.match(geneR))
						all[e].src = whiteGeneR;
					}
				
				}
			}
		for (f=2;f<=document.max_match; f++){
			make_invisible('SN'+f);
		}
		make_visible('SN'+document.synteny);
		}
	else{
		window.alert("#matches can't go higher");	
		}
	}
//cluster
function syntenyDown(){
	if (document.synteny >= 3){
		document.synteny--;
		re = /match(\d+)/;
		var all = document.getElementsByTagName('img');
		for (var e = 0; e < all.length; e++){
			if (all[e].className.match(re)){
				re.exec(all[e].className);
				if (RegExp.$1>=document.synteny){
					if (all[e].src.match(highlightedWhiteGeneL))
						all[e].src = highlightedGeneL;
					if (all[e].src.match(whiteGeneL))
						all[e].src = geneL;
					if (all[e].src.match(highlightedWhiteGeneR))
						all[e].src = highlightedGeneR;
					if (all[e].src.match(whiteGeneR))
						all[e].src = geneR;
					}
				
				}
			}
		for (f=2;f<=document.max_match; f++){
			make_invisible('SN'+f);
		}
		make_visible('SN'+document.synteny);
		}
	else{
		window.alert("#matches can't go lower");	
		}
	}

//cluster
function showGeneID(gene,cluster,text){
	if (document.active_entity == "cluster_" + cluster){
		return overlib(text);
		}
	}






//cluster
function showInfo(active,boxID){
	if (document.active_entity == undefined){
		fadeIn(boxID,0);
		}
	else if (document.active_entity.match(active)){
		fadeIn(boxID,0);
		}
	}


function fadeIn(objectID){
	if (document.oID != undefined){
		clearTimeout(document.to);
		if (document.oID != objectID){
			make_invisible(document.oID);
			}
	}
	
	if (document.oID != objectID){
		document.oID = objectID;
		document.opac = 0;
		
		obj= document.getElementById(objectID);
		obj.style.opacity = document.opac;
		obj.style.display = '';
		
		fade();
		}
	}

function fade(){
	objectID = document.oID;
	document.opac = 1;
//	document.opac += 0.1;
	obj= document.getElementById(objectID);
	obj.style.opacity = document.opac;
	if(document.opac<1){
		setTimeout('fade()',50);
	}
}


//cluster
function fadeInfo(boxID){
	if (document.oID == boxID){
//		document.to = setTimeout('fadeOut()',2000);	
//		fadeOut();	
	}
}

function fadeOut(){
	make_invisible(document.oID);
	document.oID = undefined;
	clearActiveEntity();
//	if (document.mo != undefined){
//		fadeIn(document.mo);
//		}
}






//cluster
function hideGeneID(gene){
	var boxId = 'gene_' + gene + '_info';
	return nd();
	}
//cluster
function showSpanInfo (domain,text){
	if (document.active_entity == "domain_" + domain){
		return overlib(text);
		}
	}
//cluster
function hideSpanInfo (span){
	return nd();
	}
//cluster
function loadColumns(){
	var columns = new Array("location","xid","symbol","defline","graphics");
	var defaults = new Array("show","show","show","show","show");
	for(i=0;i<columns.length;i++){
		cookie = readCookie(columns[i]);
		if (cookie){
			if (cookie == "show"){
				showColumn(columns[i]);
				}
			}
		else{
			if (defaults[i] == "show"){
				showColumn(columns[i]);
				}
			}
		}
	}
//cluster
function setColumnCookies(){
	for (i in document.columnShow){
		if (document.columnShow[i]) 
			setCookie(i,"show");
		else setCookie(i,"hide");;
		}	
	window.alert('Gene display settings have been saved');
}
//cluster
function showColumn(columnName){
	showImg = "show_" + columnName;
	hideImg = "hide_" + columnName;
	columnClass = "g_" + columnName;
	var columnCells = collectElements(columnClass);
	for (j=0;j<columnCells.length;j++){
		columnCells[j].style.display = "";
		}
	make_invisible(showImg);
	make_visible(hideImg);
	document.columnShow[columnName] = true;	
	}

//cluster
function hideColumn(columnName){
	showImg = "show_" + columnName;
	hideImg = "hide_" + columnName;
	columnClass = "g_" + columnName;
	var columnCells = collectElements(columnClass);
	for (j=0;j<columnCells.length;j++){
		columnCells[j].style.display = "none";
		}	
	make_invisible(hideImg);
	make_visible(showImg);
	document.columnShow[columnName] = false;	
	}

//style 
// comes from http://www.alistapart.com/articles/zebratables

  // this function is needed to work around 
  // a bug in IE related to element attributes
  function hasClass(obj) {
     var result = false;
     if (obj.getAttributeNode("class") != null) {
         result = obj.getAttributeNode("class").value;
     }
     return result;
  }   

 function stripe(id) {

    // the flag we'll use to keep track of 
    // whether the current row is odd or even
    var even = false;
  
    // if arguments are provided to specify the colours
    // of the even & odd rows, then use the them;
    // otherwise use the following defaults:
    var evenColor = arguments[1] ? arguments[1] : "#fff";
    var oddColor = arguments[2] ? arguments[2] : "#eee";
  
    // obtain a reference to the desired table
    // if no such table exists, abort
    var table = document.getElementById(id);
    if (! table) { window.alert('no such table');return; }
    
    // by definition, tables can have more than one tbody
    // element, so we'll have to get the list of child
    // &lt;tbody&gt;s 
    var tbodies = table.getElementsByTagName("tbody");

    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {
    
     // find all the &lt;tr&gt; elements... 
      var trs = tbodies[h].getElementsByTagName("tr");
      
      // ... and iterate through them
      for (var i = 0; i < trs.length; i++) {

        // avoid rows that have a class attribute
        // or backgroundColor style
        if (! hasClass(trs[i]) &&
            ! trs[i].style.backgroundColor) {
 		  
          // get all the cells in this row...
          var tds = trs[i].getElementsByTagName("td");
        
          // and iterate through them...
          for (var j = 0; j < tds.length; j++) {
        
            var mytd = tds[j];

            // avoid cells that have a class attribute
            // or backgroundColor style
            if (! hasClass(mytd) &&
                ! mytd.style.backgroundColor) {
        
              mytd.style.backgroundColor =
                even ? evenColor : oddColor;
            
            }
          }
        }
        // flip from odd to even, or vice-versa
        even =  ! even;
      }
    }
    window.alert('table done');
  }
