
var categories=new Array(11);
categories[0]="main";
categories[1]="other";
categories[2]="go";
categories[3]="domains";
categories[4]="pathways";
categories[5]="general";
categories[6]="functional";
categories[7]="interactions";
categories[8]="lit";
categories[9]="disease";
categories[10]="tissue";

function checkDownloadForm()
{
	var FM=document.getElementById("frmDBDownload");
	
	for (var i=0;i<FM.termFileList.length;i++)
	{
		if(FM.termFileList[i].checked)
		{
			FM.submit();
			return;
		}

	}

	alert("You must select at least one file to download.");
	return;

}
function checkEmail()
{
	var FM=document.getElementById("frmDownloadLogin");
	if(FM.email==null||FM.email.value=="")
	{
		alert("You must enter a valid email address.");
		return;
	}
	FM.submit();
}

function register()
{
	var FM=document.getElementById("frmDBDownload");

	if(FM.first==null||FM.first.value=="")
	{
		alert("Please enter your first name.");
		return;
	}
	if(FM.last==null||FM.last.value=="")
	{
		alert("Please enter your last name.");
		return;
	}
	if(FM.org==null||FM.org.value=="")
	{
		alert("Please enter the name of your organization.");
		return;
	}
	if(FM.email==null||FM.email.value=="")
	{
		alert("You must enter a valid email address.");
		return;
	}
var emailStr=FM.email.value.toUpperCase();
if(emailStr.indexOf("HOTMAIL")>0||emailStr.indexOf("GMAIL.COM")>0||emailStr.indexOf("YAHOO")>0)
	{
		alert("You must enter an email address from your academic, government, or commercial organization.\nEmail addresses from public providers will not be accepted.");
		return;
	}


var checked=false;
for(var i = 0; i < FM.entity.length; i++) 
{
		if(FM.entity[i].checked) 
		{
			checked=true;
		}
}

if(!checked)
	{
		alert("Please choose the type of organization to which you belong.");
		return;
	}



FM.action='downloadRegistration.jsp';
FM.submit();
}

function submitConvertedList()
{
	//alert(document.getElementById("VTO").value);
	//alert(document.getElementById("convertedIds").value);
	
var win=false;
try
   {
   	win=window.opener==null;
	if(!win)
	{
		win=window.opener.closed;
	}

   }
catch(err)
   {
   	win=true;
   }


//alert(win);


	if(win)
	{
		alert("You have closed the main DAVID window.  You will now be redirected to start the conversion process again.");
		var FM=document.getElementById("FRM");
		FM.action='conversion.jsp?VFROM='+document.getElementById("VFROM").value;FM.target='';FM.submit();
	}
	else
	{
	if(document.getElementById("convertedIds").value==null||document.getElementById("convertedIds").value=="")
	{
		alert("You must first convert your ids.");
		return;
	}
	var FM = window.opener.document.forms[0];
	FM.ids.value=document.getElementById("convertedIds").value;
	FM.idType.value=document.getElementById("VTO").value;
       //alert(FM.idType.value);
	FM.uploadType.value="list";

	FM.Mode.value="paste";
	
	FM.action="tools.jsp";
	FM.target="";
	FM.submit();
	window.opener.focus();
	}
	//alert(FM.idType.value);



}
function submitConvertedPopulation()
{
	//alert(document.getElementById("VTO").value);
	//alert(document.getElementById("convertedIds").value);
	
var win=false;
try
   {
   	win=window.opener==null;
	if(!win)
	{
		win=window.opener.closed;
	}

   }
catch(err)
   {
   	win=true;
   }


//alert(win);


	if(win)
	{
		alert("You have closed the main DAVID window.  You will now be redirected to start the conversion process again.");
		var FM=document.getElementById("FRM");
		FM.action='conversion.jsp?VFROM='+document.getElementById("VFROM").value;FM.target='';FM.submit();
	}
	else
	{
	if(document.getElementById("convertedIds").value==null||document.getElementById("convertedIds").value=="")
	{
		alert("You must first convert your ids.");
		return;
	}
	var FM = window.opener.document.forms[0];
	FM.ids.value=document.getElementById("convertedIds").value;
	FM.idType.value=document.getElementById("VTO").value;
       //alert(FM.idType.value);
	FM.uploadType.value="population";

	FM.Mode.value="paste";
	
	FM.action="tools.jsp";
	FM.target="";
	FM.submit();
	window.opener.focus();
	}
	//alert(FM.idType.value);



}

function checkDefaults()
{

	var CB;
	var count;
	var i;
	var j;
	var group=new Array(5); 
	group[0]="go";
	group[1]="domains";
	group[2]="pathways";
	group[3]="functional";
	group[4]="disease";

	if(!document.forms['summaryForm'])
	{
		return;
	}
	var state;
	state=document.forms['summaryForm'].elements['checkDefault'].checked;
	for(j=0;j<group.length;j++)
	{
		CB = document.forms['summaryForm'].elements[group[j]+'_CB'];
		if(CB)
		{
				
		for(i = 0; i < CB.length; i++)
		{
			if(CB[i].value=="GOTERM_BP_ALL"||CB[i].value=="GOTERM_CC_ALL"||CB[i].value=="GOTERM_MF_ALL"
					   ||CB[i].value=="BBID"||CB[i].value=="BIOCARTA"||CB[i].value=="KEGG_PATHWAY"
					   ||CB[i].value=="COG_ONTOLOGY"||CB[i].value=="INTERPRO"||CB[i].value=="PIR_SUPERFAMILY"
					   ||CB[i].value=="SMART"||CB[i].value=="SP_PIR_KEYWORDS"||CB[i].value=="UP_SEQ_FEATURE"
					   ||CB[i].value=="OMIM_DISEASE")

			{
				CB[i].checked=state;
			}
		}
		}
	}
	
	countAll();
	

}
function uncheckAll()
{

	var CB;
	var count;
	var i;
	var j;
	var group=new Array(11); 
	group[0]="go";
	group[1]="domains";
	group[2]="pathways";
	group[3]="functional";
	group[4]="disease";
	group[5]="main";
	group[6]="other";
	group[7]="general";
	group[8]="interactions";
	group[9]="lit";
	group[10]="tissue";


	if(!document.forms['summaryForm'])
	{
		return;
	}
	var state;
	state=false;
	for(j=0;j<group.length;j++)
	{
		CB = document.forms['summaryForm'].elements[group[j]+'_CB'];
		if(CB)
		{
				
		for(i = 0; i < CB.length; i++)
		{
			CB[i].checked=state;
			
		}
		}
	}
	document.forms['summaryForm'].elements['checkDefault'].checked=false;
	countAll();
	

}

function createSublist()
{
	var  sublistStr='';
	var form=0;
	if(document.forms.length>1)
	{
		form=(document.forms.length-1);
	}
	var x=0; 
	var boxes=document.forms[form].elements['CKDL'];

	if(!boxes)
	{
		alert('no checkboxes');
		return;
	}
	var count=0;
	for (var i=0; i<boxes.length; i++) 
	{ 
		if(boxes[i].checked)
		{
			sublistStr=sublistStr+','+boxes[i].value;
			count++;
		}
			
	}
	if(count==0)
	{
		alert('no boxes checked');
		return;
	}
	//alert(sublistStr);
	//alert(form);
if(form==0)
{


	window.opener.document.forms[0].rowids.value=sublistStr;
	window.opener.document.forms[0].sublist.value=prompt("Enter a name for this sublist");
	window.opener.document.forms[0].submit();
	window.opener.focus();

}
else
{
	document.forms[0].rowids.value=sublistStr;
	document.forms[0].sublist.value=prompt("Enter a name for this sublist");
	document.forms[0].submit();
}

 
}

function geneReport(dids)
{
	var form=0;
	if(document.forms.length>1)
	{
		form=(document.forms.length-1);
	}
	//alert(document.forms[form].id);
	setRowids(dids);
	setAction('geneReport.jsp');

	//alert(document.getElementById("rowids").value);

	document.forms[form].submit();
}
function geneReportFull(dids)
{
	var form=0;
	if(document.forms.length>1)
	{
		form=(document.forms.length-1);
	}
	//alert(document.forms[form].id);
	setRowids(dids);
	setAction('geneReportFull.jsp');

	//alert(document.getElementById("rowids").value);

	document.forms[form].submit();



}
function termReport(ids,didCount)
{
	
	var form=0;
	if(document.forms.length>1)
	{
		form=(document.forms.length-1);
	}

	setRowids(ids);setAction('termReport.jsp?didCount='+didCount);document.forms[form].submit();

}
function termReport2(didCount)
{
	
	var form=0;
	if(document.forms.length>1)
	{
		form=(document.forms.length-1);
	}

	setAction('termReport2.jsp?didCount='+didCount);document.forms[form].submit();

}


function submitForm(action,target,form)
{
	form.action=action;
	form.target=target;
	form.submit();

}
function submitRelatedGenes(action,target,form)
{
	if(form.list.value==-1)
	{
		alert("This is an intensive operation which may take several minutes.");
	}
	form.action=action;
	form.target=target;
	form.submit();

}
function gene2gene()
{
	var wnd = window.open("gene2gene.jsp?stringency="+document.forms['gene2gene'].stringency.value+"&kappa="+document.forms['gene2gene'].kappa.value+"&overlap="+document.forms['gene2gene'].overlap.value+"&initialSeed="+document.forms['gene2gene'].initialSeed.value+"&frequency="+document.forms['gene2gene'].frequency.value+"&finalSeed="+document.forms['gene2gene'].finalSeed.value+"&linkage="+document.forms['gene2gene'].linkage.value, "_self", "width=700, height=500, resizable=yes, scrollbars=yes");
	wnd.focus();

}
function setCheckCount(grouptd,count){
					
			
			var obj=document.getElementById(grouptd);
			//alert(obj.innerHTML);
			obj.innerHTML="&nbsp;("+count+" selected)";
		}
		function countGroup(GroupNode) {
		//alert(GroupNode.parentNode.parentNode.parentNode.parentNode.id+"->"+GroupNode.parentNode.parentNode.parentNode.id+"->"+GroupNode.parentNode.parentNode.id+"->"+GroupNode.parentNode.id+"->"+GroupNode.id);
		if(!document.forms['summaryForm'])
		{
		return;
		}
	var CB = document.forms['summaryForm'].elements[GroupNode+'_CB'];
	if(CB)
	{
					    var count=0;
				
				for(var i = 0; i < CB.length; i++)
			{
				if(CB[i].checked)
				{
					count++;
				}
			}
			 setCheckCount(GroupNode+"td",count);
	}
}
function countAll()
{
	countGroup("main");
	countGroup("other");
	countGroup("go");
	countGroup("domains");
	countGroup("pathways");
	countGroup("general");
	countGroup("functional");
	countGroup("interactions");
	countGroup("lit");
	countGroup("disease");
}
function getCurrentList()
{

		
		if(!document.forms['summaryForm'])
		{
			return;
		}
		return document.getElementById("currentList").value;

}
function getTerms()
{

		
		var  GroupNode='go';
		var  termStr='';
		if(!document.forms['summaryForm'])
		{
			return;
		}
				var x=0; 
var selectCount=0;
for (x=0; x<11; x++) 
{ 
	var CB = document.forms['summaryForm'].elements[categories[x]+'_CB'];
	if(CB)
	{
	
			    var count=0;
				
				for(var i = 0; i < CB.length; i++)
				{
					if(CB[i].checked)
					{
						termStr=termStr+','+CB[i].value;
						selectCount++;
					}
				}
	}
}
if(selectCount>0)
{		 
	//alert(termStr);
	return termStr;
}
else
{
	alert('You must select an annotation category.');
	return null;
}
			 
			// alert('here');
}


function Details(group, term, idt){

	var wnd = window.open("geneReport.jsp?TX='9606:Homo sapiens'&RF=Y&LS=demolist1&GR=" + group + "&TM=" + term + "&ID=" + idt, "", "width=700, height=500, resizable=yes, scrollbars=yes");
	wnd.focus();
}
function twoDView(index,type){

	var wnd = window.open("2DView.jsp?index=" + index+"&clusterType=" + type, "", "");
	wnd.focus();
}
function setAction(action)
{
	
	form=0;
	if(document.forms.length>1)
	{
		form=(document.forms.length-1);

	}

	document.forms[form].action=action;
	//alert(action);
	//alert(document.forms[form].id);

	if(action!="term2term.jsp"&&action!="gene2gene.jsp"&&action!="relatedTerms.jsp"&&action!="relatedGenes.jsp")
	{
		document.forms[form].target="_blank";
	}
	else
	{
		document.forms[form].target="_self";
	}

}
function setRowids(rowids)
{
	var form=0;
	if(document.forms.length>1)
	{
		form=(document.forms.length-1);
	}
	
	//document.getElementById("rowids").value=rowids;
document.forms[form].rowids.value=rowids;

	//alert(document.getElementById("rowids").value);
	//alert(document.forms[form].id);

}
function setHeading(headingStr)
{
	var form=0;
	if(document.forms.length>1)
	{
		form=(document.forms.length-1);
	}

	document.forms[form].heading.value=headingStr;
}
function setClusterSort(sort)
{
	var form=0;
	if(document.forms.length>1)
	{
		form=(document.forms.length-1);
	}

	document.forms[0].clusterSort.value=sort;
}

function RT(cat, trm, UT){
if (cat== "GOTERM_BP_ALL") cat="1";
else if (cat== "GOTERM_MF_ALL") cat="2";
else if (cat== "GOTERM_CC_ALL") cat="3";
else if (cat== "KEGG_PATHWAY") cat="4";
else if (cat== "BIOCARTA") cat="5";
else if (cat== "SP_PIR_KEYWORDS") cat="6";
else if (cat== "BBID") cat="7";
else if (cat== "SMART_NAME") cat="8";
else if (cat== "GENETIC_ASSOCIATION_DB") cat="9";
else if (cat== "UP_SEQ_FEATURE") cat="10";
else if (cat== "COG_KOG_ONTOLOGY") cat="11";
else if (cat== "OMIM_PHENOTYPE") cat="12";
else if (cat== "INTERPRO_NAME") cat="13";
else if (cat== "PIR_SUPERFAMILY_NAME") cat="14";
else cat="0";
	if(cat == "0"){
		alert("The category is not supported for related Search tool.");
		return;
	}
	var wnd = window.open("relatedTerms.jsp?LT="+UT+"&CAT=" + cat + "&TRM=" + trm, "RT", "width=880, height=550, resizable=yes, scrollbars=yes");
	wnd.focus();
}
function relatedTerms(termId){
	var wnd = window.open("relatedTerms.jsp?id=" + termId, "Related Terms", "width=880, height=550, resizable=yes, scrollbars=yes");
	wnd.focus();

}
function relatedGenes(rowId){
	var wnd = window.open("relatedGenes.jsp?id=" + rowId, "Related Genes", "width=880, height=550, resizable=yes, scrollbars=yes");
	wnd.focus();

}


function LinkOut(cat, trm){		
	var url = "";
	
	if(cat.toUpperCase() == "GOTERM_BP_ALL"){
		url = "http://www.ebi.ac.uk/ego/GSearch?query=" + trm+ "&mode=name";
	}
	if(cat.toUpperCase() == "UP_SEQ_FEATURE"){
		url = "http://www.pir.uniprot.org/";
	}
	if(cat.toUpperCase() == "GOTERM_MF_ALL"){
		url = "http://www.ebi.ac.uk/ego/GSearch?query=" + trm + "&mode=name";
	}
	
	if(cat.toUpperCase() == "SP_PIR_KEYWORDS"){
		url = "http://us.expasy.org/cgi-bin/get-entries?KW=" + trm;
	}	 
	
	if(cat.toUpperCase() == "BIOCARTA"){
			url = "biocarta.jsp?PATH=" + trm.replace(":", "$") + "&TB=demolist1";
	}	
	
	if(cat.toUpperCase() == "KEGG_PATHWAY"){
			url = "kegg.jsp?PATH=" + trm.replace(":", "$") + "&TB=demolist1";
	}	
	
	
	if(cat.toUpperCase() == "GOTERM_CC_ALL"){
				url = "http://www.ebi.ac.uk/ego/GSearch?query=" + trm + "&mode=name";
	}
	
	if(cat.toUpperCase() == "BBID"){
					url = "http://bbid.grc.nia.nih.gov/geneimages/" + trm + ".jpeg";
	}
	
	if(cat.toUpperCase() == "PIR_SUPERFAMILY_NAME"){
					url = "http://pir.georgetown.edu/cgi-bin/ipcSF?id=" + trm.substring(0, trm.indexOf(":"));
	}
	
	if(cat.toUpperCase() == "SMART_NAME"){
					url = "http://smart.embl-heidelberg.de/smart/do_annotation.pl?BLAST=DUMMY&ACC=" + trm.substring(0, trm.indexOf(":"));
	}
	
	if(cat.toUpperCase() == "INTERPRO_NAME"){
					url = "http://www.ebi.ac.uk/interpro/DisplayIproEntry?ac=" + trm.substring(0, trm.indexOf(":")) ;
	}
	
	if(cat.toUpperCase() == "SP_PIR_KEYWORDS"){
					url = "http://us.expasy.org/cgi-bin/get-entries?KW=" + trm;
	}
	
	if(cat.toUpperCase() == "OMIM_PHENOTYPE"){
					url = "http://www.ncbi.nlm.nih.gov/entrez/dispomim.cgi?id=" + trm.substring(0, trm.indexOf(":"));
	}
	
	if(cat.toUpperCase() == "COG_KOG_ONTOLOGY"){
					url = "http://www.ncbi.nlm.nih.gov/COG/new/ ";
	}
	if(cat.toUpperCase() == "GENETIC_ASSOCIATION_DB"){
					url = "http://geneticassociationdb.nih.gov/";
	}	
	
	
	if(url != ""){
		var wnd = window.open(url, "LKOT", "width=780, height=600, resizable=yes, scrollbars=yes");
		wnd.focus();
	}
		
}
function CreateNRChart(){
	
		if(getTerms()!=null)
		{
			var wnd = window.open("term2term.jsp?annot=" + getTerms()+"&currentList=" + getCurrentList(), "", "width=980, height=620, toolbar=yes, menubar=yes, resizable=yes, scrollbars=yes");
			wnd.focus();
		}

		

		return;

}
function CreateCluster(){
	
		if(getTerms()!=null)
		{
			var wnd = window.open("cluster.jsp?annot=" + getTerms()+"&currentList=" + getCurrentList(), "", "width=980, height=620, toolbar=yes, menubar=yes, resizable=yes, scrollbars=yes");
			wnd.focus();
		}
		return;

}
function CreateTable()
{

	
	if(getTerms()!=null)

	{

		var wnd = window.open("annotationReport.jsp?annot=" + getTerms()+"&currentList=" + getCurrentList(), "", "width=800, height=620, toolbar=yes, menubar=yes, resizable=yes, scrollbars=yes");
		wnd.focus();
	}
		

		return;
}
function knowledgebaseReport()
{

	
	if(getTerms()!=null)

	{

		var wnd = window.open("knowledgebaseReport.jsp?annot=" + getTerms()+"&currentList=" + getCurrentList(), "", "width=800, height=620, toolbar=yes, menubar=yes, resizable=yes, scrollbars=yes");
		wnd.focus();
	}
		

		return;
}
function exportTable()
{
	
	if(getTerms()!=null)
	{

		var wnd = window.open("annotationExport.jsp?annot=" + getTerms(), "", "width=800, height=620, toolbar=yes, menubar=yes, resizable=yes, scrollbars=yes");
		wnd.focus();
	}
		

		return;
}

function CreateChart()
{
	
	if(getTerms()!=null)
	{

		var wnd = window.open("chartReport.jsp?annot=" + getTerms()+"&currentList=" + getCurrentList(), "", "width=800, height=620, toolbar=yes, menubar=yes, resizable=yes, scrollbars=yes");
		wnd.focus();
	}
		

		return;


}
function ShowGeneList()
{
	
	window.location = "list.jsp";
		


}
function exportChart()
{
	
	if(getTerms()!=null)
	{

		var wnd = window.open("chartExport.jsp?annot=" + getTerms(), "", "width=800, height=620, toolbar=yes, menubar=yes, resizable=yes, scrollbars=yes");
		wnd.focus();
	}

		return;


}


var plus = new Image();
  plus.src = "images/plus.gif";
var minus = new Image();
  minus.src = "images/minus.gif";
var item = new Image();
  item.src = "images/item.gif";

function door(obj){

  obj = obj.parentNode;
  obj.className = (obj.className == 'open') ? 'closed' : 'open';
  return false;
}

function getAnnotationReport(annot)
{
	var wnd = window.open("annotationReport.jsp?annot=" + annot, "", "width=720, height=350, toolbar=no, menubar=yes, resizable=yes, scrollbars=yes");
	wnd.focus();
}
function getKnowledgebaseReport(annot)
{
	var wnd = window.open("knowledgebaseReport.jsp?annot=" + annot, "", "width=720, height=350, toolbar=no, menubar=yes, resizable=yes, scrollbars=yes");
	wnd.focus();
}
function getChartReport(annot)
{
	var wnd = window.open("chartReport.jsp?annot=" + annot, "", "width=720, height=350, toolbar=no, menubar=yes, resizable=yes, scrollbars=yes");
	wnd.focus();
}

function OpenHelp(part){
	var wnd = window.open("helps/linear_search.html#" + part, "Details", "width=720, height=350, resizable=yes, scrollbars=yes");
	wnd.focus();
}

function OpenHelp2(part){
	var wnd = window.open("helps/functional_annotation.html#" + part, "Details", "width=720, height=350, resizable=yes, scrollbars=yes");
	wnd.focus();
}
function CombinedReport(usertable,did){
	var wnd = window.open("geneReportFull.jsp?LT=" + usertable + "&ID=" + did, "Report", "width=640, height=500, toolbar=no, menubar=yes, resizable=yes, scrollbars=yes");
	wnd.focus();
}


function RG(val){	
	var wnd = window.open("relatedGenes.jsp?LT=demolist1&ID=" + val, "", "width=880, height=360, toolbar=no, menubar=yes, resizable=yes, scrollbars=yes");
	wnd.focus();
}
