///////////////////////////////////////////////////////////
// JavaScript functions for the medicago.org/genome site //
///////////////////////////////////////////////////////////


function preloadImages() {
	var image_names = new Array();
  var i = 0;
	image_names[i] = "project_home_off.gif"; i++;
	image_names[i] = "project_home_over.gif"; i++;
	image_names[i] = "project_home_on.gif"; i++;
	image_names[i] = "about_project_off.gif"; i++;
	image_names[i] = "about_project_over.gif"; i++;
	image_names[i] = "about_project_on.gif"; i++;
	image_names[i] = "genome_stats_off.gif"; i++;
	image_names[i] = "genome_stats_over.gif"; i++;
	image_names[i] = "genome_stats_on.gif"; i++;
	image_names[i] = "BAC_registry_off.gif"; i++;
	image_names[i] = "BAC_registry_over.gif"; i++;
	image_names[i] = "BAC_registry_on.gif"; i++;
//	image_names[i] = "marker_queries_off.gif"; i++;
//	image_names[i] = "marker_queries_overgif"; i++;
//	image_names[i] = "marker_queries_on.gif"; i++;
//	image_names[i] = "BAC_queries_off.gif"; i++;
//	image_names[i] = "BAC_queries_over.gif"; i++;
//	image_names[i] = "BAC_queries_on.gif"; i++;
	image_names[i] = "downloads_off.gif"; i++;
	image_names[i] = "downloads_over.gif"; i++;
	image_names[i] = "downloads_on.gif"; i++;
	image_names[i] = "genome_links_off.gif"; i++;
	image_names[i] = "genome_links_over.gif"; i++;
	image_names[i] = "genome_links_on.gif"; i++;
	image_names[i] = "blast_search_off.gif"; i++;
	image_names[i] = "blast_search_over.gif"; i++;
	image_names[i] = "blast_search_on.gif"; i++;
	image_names[i] = "genome_maps_off.gif"; i++;
	image_names[i] = "genome_maps_over.gif"; i++;
	image_names[i] = "genome_maps_on.gif"; i++;
	image_names[i] = "news_off.gif"; i++;
	image_names[i] = "news_over.gif"; i++;
	image_names[i] = "news_on.gif"; i++;
	image_names[i] = "meetings_off.gif"; i++;
	image_names[i] = "meetings_over.gif"; i++;
	image_names[i] = "meetings_on.gif"; i++;
	image_names[i] = "literature_off.gif"; i++;
	image_names[i] = "literature_over.gif"; i++;
	image_names[i] = "literature_on.gif"; i++;
	image_names[i] = "participants_off.gif"; i++;
	image_names[i] = "participants_over.gif"; i++;
	image_names[i] = "participants_on.gif"; i++;
//	image_names[i] = "site_comments_off.gif"; i++;
//	image_names[i] = "site_comments_over.gif"; i++;
//	image_names[i] = "site_comments_on.gif"; i++;
	image_names[i] = "report_errors_off.gif"; i++;
	image_names[i] = "report_errors_over.gif"; i++;
	image_names[i] = "report_errors_on.gif"; i++;
	image_names[i] = "medicago_home_off.gif"; i++;
	image_names[i] = "medicago_home_over.gif"; i++;
	image_names[i] = "medicago_home_over.gif"; i++;
	image_names[i] = "go_off.gif"; i++;
	image_names[i] = "go_over.gif"; i++;
	image_names[i] = "go_down.gif"; i++;
	image_names[i] = "help_off.gif"; i++;
	image_names[i] = "help_over.gif"; i++;
	image_names[i] = "help_down.gif"; i++;
//	image_names[i] = "readme_off.gif"; i++;
//	image_names[i] = "readme_over.gif"; i++;
//	image_names[i] = "readme_on.gif"; i++;
	image_names[i] = "browse_over.gif"; i++;
	image_names[i] = "browse_off.gif"; i++;
	image_names[i] = "browse_on.gif"; i++;
	image_names[i] = "Get_sequence_over.gif"; i++;
	image_names[i] = "Get_sequence_off.gif"; i++;
	image_names[i] = "Get_sequence_on.gif"; i++;
	image_names[i] = "release_over.gif"; i++;
	image_names[i] = "release_off.gif"; i++;
	image_names[i] = "release_on.gif"; i++;
  
	image_names[i] = "map/chr_small0_over.gif"; i++;
	image_names[i] = "map/chr_small0_on.gif"; i++;
	image_names[i] = "map/chr_small0_off.gif"; i++;
	image_names[i] = "map/chr_small1_over.gif"; i++;
	image_names[i] = "map/chr_small1_on.gif"; i++;
	image_names[i] = "map/chr_small1_off.gif"; i++;
	image_names[i] = "map/chr_small2_over.gif"; i++;
	image_names[i] = "map/chr_small2_on.gif"; i++;
	image_names[i] = "map/chr_small2_off.gif"; i++;
	image_names[i] = "map/chr_small3_over.gif"; i++;
	image_names[i] = "map/chr_small3_on.gif"; i++;
	image_names[i] = "map/chr_small3_off.gif"; i++;
	image_names[i] = "map/chr_small4_over.gif"; i++;
	image_names[i] = "map/chr_small4_on.gif"; i++;
	image_names[i] = "map/chr_small4_off.gif"; i++;
	image_names[i] = "map/chr_small5_over.gif"; i++;
	image_names[i] = "map/chr_small5_on.gif"; i++;
	image_names[i] = "map/chr_small5_off.gif"; i++;
	image_names[i] = "map/chr_small6_over.gif"; i++;
	image_names[i] = "map/chr_small6_on.gif"; i++;
	image_names[i] = "map/chr_small6_off.gif"; i++;
	image_names[i] = "map/chr_small7_over.gif"; i++;
	image_names[i] = "map/chr_small7_on.gif"; i++;
	image_names[i] = "map/chr_small7_off.gif"; i++;
	image_names[i] = "map/chr_small8_over.gif"; i++;
	image_names[i] = "map/chr_small8_on.gif"; i++;
	image_names[i] = "map/chr_small8_off.gif"; i++;
	
	for (var i=0; i<image_names.length; i++) {
		var preload_image = new Image;
		preload_image.src = "images/" + image_names[i];
	}
}


function swapImage(img, src) {
	imgObj = eval('document.' + img);
	if (imgObj != null) {
		imgObj.src = src;
	}
}


// Pop up a window and write the content to it
function writeConsole(content) {
	top.consoleRef=window.open('','myconsole',
		'width=400,height=350'
		+',menubar=0'
		+',toolbar=1'
		+',status=0'
		+',scrollbars=1'
		+',resizable=1')
	top.consoleRef.document.writeln(
		'<html><head><title>Query</title></head>'
		+'<body bgcolor=white onLoad="self.focus()">'
		+content
		+'<br><br><a href="javascript:window.close()">Close</a>'
		+'</body></html>')
	top.consoleRef.document.close()
}


// highlight a table column header
// NOT WORKING ON MOST BROWSERS!
function highlight(whichtablename, whichtablenum, whichcell) {
//	if (document.all) {
//		var tables = document.getElementsByTagName('table');
//		alert("tables[whichtablenum].rows[0].cells: "+tables[whichtablenum].rows[0].cells);
//		alert("tables[whichtablenum].rows[0].cells.length: "+tables[whichtablenum].rows[0].cells.length);
//		alert("tables[whichtablenum].rows[0].cells.item(whichcell): "+tables[whichtablenum].rows[0].cells.item(whichcell));
//		alert("tables[whichtablenum].rows[0].cells[0]: "+tables[whichtablenum].rows[0].cells[0]);
//    tables[whichtablenum].rows[0].cells[whichcell].bgColor = '#66ff66';
//	} else {
//		var table = document.getElementsByName(whichtablename)[0];
//		table.rows[0].cells[whichcell].bgColor = '#66ff66';
//	}
}


// unighlight a table column header
function unhighlight(whichtablename, whichtablenum, whichcell) {
//	if (document.all) {
//		var tables = document.getElementsByTagName('table');
//		tables[whichtablenum].rows[0].cells[whichcell].bgColor = '#99ff99';
//	} else if (document.childNodes) {
//		var table = document.getElementsByName(whichtablename)[0];
//		table.rows[0].cells[whichcell].bgColor = '#99ff99';
//	}
}


// load the appropriate page given what was selected in the drop-down
// and the Accession.
function goToDataLink(selectobj, acc, ver, clone_name) {
	var new_loc;
	var index = selectobj.selectedIndex;
	var linkname = selectobj.options[index].value;
	if (linkname == "Ensembl") {
//		window.location.href = "http://decifr.ccgb.umn.edu/Medicago_truncatula/contigview?clone=" + acc + "." + ver;
		new_loc = "http://decifr.ccgb.umn.edu/Medicago_truncatula/contigview?clone=" + acc + "." + ver;
		window.open(new_loc, "", "");
	} else if (linkname == "Genbank") {
//		window.location.href = "http://www.ncbi.nlm.nih.gov:80/entrez/viewer.fcgi?db=nucleotide&val=" + acc;
		new_loc = "http://www.ncbi.nlm.nih.gov:80/entrez/viewer.fcgi?db=nucleotide&val=" + acc;
		window.open(new_loc, "", "");
	} else if (linkname == "OU") {
//		window.location.href = "http://dna8.genome.ou.edu/cgi-bin/gbrowse?source=medicago_new;name=" + acc;
		new_loc = "http://dna8.genome.ou.edu/cgi-bin/gbrowse?source=medicago_new;name=" + acc;
		window.open(new_loc, "", "");
	} else if (linkname == "TIGR") {
//		window.location.href = "http://www.tigr.org/tigr-scripts/medicago/GBROWSE/gbrowse/GBROWSE/gbrowse2?source="+acc;
//		new_loc = "http://www.tigr.org/tigr-scripts/medicago/GBROWSE/gbrowse/GBROWSE/gbrowse2?source="+acc;
		new_loc = "http://www.tigr.org/docs/tigr-scripts/euk_manatee/BacAnnotationPage.cgi?db=mta1;name=" + clone_name;
		window.open(new_loc, "", "");
	}
}

// sort table on the clicked field
function sortOnField(formobj, whichfield) {
	formobj.sortfield3.value = formobj.sortfield2.value;
	formobj.sortfield2.value = formobj.sortfield1.value;
	formobj.sortfield1.value = whichfield;
	formobj.submit();
}


// pop up a readme file
function popupReadme(readme_file) {
  var url = "readme.php?name="+readme_file;
  var name = "Read Me";
  var parms = "scrollbars=yes,width=350,height=400";
  
	var readme_win = window.open(url, name, parms);
//	var readme_win = window.open("readme.php?name="+readme_file, "Read Me", "scrollbars=yes,width=350,height=400");
	readme_win.focus();
}


// submit form to the download page
function downloadReport(reportform) {
	reportform.action = "download_query.php";
	reportform.submit();
}


function openNewWindow(url) {
	window.open(url,'newPage','scrollbars=yes,resizable=yes,width=600,height=400,toolbar=no,menubar=no');
}

function openLegend() {
  var urlname = "chr_describe.html";
  var legendwin = window.open(urlname, "Legend", "scrollbars,resizeable,height=450,width=500");
  if (!legendwin) {
    alert("You have disabled pop-up windows. The map can't be displayed.");
  } else {
    legendwin.focus();
  }
}

function showPolicyLinks() {
  document.getElementById('policybox').style.top = adjustY(mouse_Y-50, 170);
  document.getElementById('policybox').style.left = adjustX(mouse_X+30, 180);
  show('policybox');
}

function show_old_version_box() {
  document.getElementById('old_version_box').style.top = adjustY(mouse_Y-50, 170);
  document.getElementById('old_version_box').style.left = adjustX(mouse_X+30, 180);
  show('old_version_box');
}
function history_openContigViewerApplet(chr) {
   window.open("history_contig_viewer.php?chromosome="+chr, "", "width=1200,height=630,resizable");
}


function cvit_help() {
  cvit = window.open("", "CViT", "scrollbars,resizeable,height=450,width=500");
  cvit.document.writeln("<html>");
  cvit.document.writeln("<link rel=\"stylesheet\" type=\"text/css\" href=\"genome.css\">");
  cvit.document.writeln("<body leftmargin=5 topmargin=5>");
  cvit.document.writeln("<table height=\"100%\" border=0><tr><td>");
  cvit.document.writeln("<h3>CViT &#151; Chromosome Visualization Tool</h3>");
  cvit.document.writeln("</td></tr><tr><td>");
  cvit.document.writeln("<p>CViT is a whole-genome mapping tool that places features on chromosomes.");
  cvit.document.writeln("This website provides two utilities which use CViT.</p>");
  cvit.document.writeln("<ul><li><b>CViT-blast</b> &#151; Displays blast results on the <i>Medicago truncatula</i> genome. Mulitple sequences can be submitted and hits for each sequence will be assigned a different color.</li>");
  cvit.document.writeln("<li><b>CViT-BACs</b> &#151; Shows BAC locations on the <i>Medicago truncatula</i> genome. BACs may be submitted in groups with each group assigned a different color.");
  cvit.document.writeln("</u>");
  cvit.document.writeln("</td></tr><tr valign=\"bottom\" height=\"100%\"><td>");
  cvit.document.writeln("");
  cvit.document.writeln("<a href=\"javascript:window.close()\">close</a>");
  cvit.document.writeln("</td></tr></table>");
  cvit.document.writeln("</body></html>");
  
//  alert("All about CViT");
}

  function doSearch() {
    var sindex = document.searchform.type.selectedIndex;
    var search_in = document.searchform.type.options[sindex].value;
    if (search_in == "Everywhere") {
      document.searchform.action = "show_reports.php";
    } else if (search_in == "Markers") {
      document.searchform.action = "batch_marker_report.php";
    } else if (search_in == "BACs") {
      document.searchform.action = "batch_bac_report.php";
    } else if (search_in == "Contigs") {
      document.searchform.action = "batch_contig_report.php";
    }
    document.searchform.submit();
  }


