
/*
  Use an <a> tag like the following. 
  It is important that the javascript in the onclick event handler return false. If the javascript returns false, then the normal href will not run. This same HTML will work in non-javascript browsers.

<p>
<a href="http://www.spreadfirefox.com/"
target="new_win_name"
onclick="open_info(this); return false;" 
title="This link will create a new window or will re-use an already opened one">Promote Firefox adoption</a>
</p>

*/


/*
  The brute force version. Simple. Redraws everything all the time.
  The clever version clever_open_info() is below.
  
  Usage is the same for both.
*/

function open_info(elem_ref)
{
    var win_ref = window.open(elem_ref.href, elem_ref.target);
    win_ref.focus();
}


/*
  Usage: open_info(this); return false;

  Parameter elem_ref is a reference to an element, aka "this". The element
  is expected to have attributes "href" and "target". If you have
  several info windows sharing a target, you must make sure all the
  <a> tags have the same target.

  If a window does not exist or is close, then draw it.

  If we have an existing window, but trying to read its location
  throws an exception, then we need to redraw that window.

  If an existing window has a different href than we want, then redraw
  that window.

  Due to the same domain policy, the browser with throw an error if we
  try to read the location.href of a window in a different domain,
  even if we created that window. Catch the exception and redraw the
  window.
  
  If our target is the same as the target requested, then the new
  content loads in our window.

  The rocket scientists at mozilla.org give an example of how to do
  this, however, their example is woefully incomplete. 
 */

var win_hash = new Array(); // global array used as a hash

function clever_open_info(elem_ref)
{
    var win_ref = win_hash[elem_ref.target];
    var need_redraw = 0;
    try
	{
	    need_redraw = ( win_ref.location.href != elem_ref.href);
	}
    catch(e)
	{
	    need_redraw = 1;
	}
    
    /*
      If the window doesn't exist or if the window exists but
      was closed, or if we can't check the href or the href is
      wrong then create a new window.
    */
    
    if (win_ref == null || win_ref.closed || need_redraw)
	{
	    win_ref = window.open(elem_ref.href, elem_ref.target);
	    win_hash[elem_ref.target] = win_ref;
	}

    /*
      We always have to focus the window. 

      If the window existed but we have to re-create it due to the
      same domain policy with a new location.href, then we must call
      window.focus().
      
      If the window exists and doesn't have to be redrawn,
      then simply focus it.
    */
    win_ref.focus();
}
    


var lastname;

// twl8n jan 07 2008
// function sampledna moved into blast_setup.html so that the template
// code can fill in a value from .app_config

function new_seq()
{
    date = new Date();

    // Note the extra () required around expressions within the string
    // concatenation where we have to add 1 to the month.
    
    document.blastrun.query_name.value="blast_"+date.getFullYear()+"_"+(date.getMonth()+1)+"_"+date.getDate();
    document.blastrun.query_name.value += "_"+date.getHours()+"_"+date.getMinutes()+"_"+date.getSeconds();
}


function showseq(mname,trline)
{
    if (lastname)
	{
	    if (lastname != mname)
		{
		    document.getElementById(lastname).style.display = "none";
		    if (lastline)
			{
			    document.getElementById(lastline).style.background = '#ffffff';
			}
		}
	}

    if (document.getElementById(mname).style.display == "inline")
	{
	    document.getElementById(mname).style.display = "none";
	    document.getElementById(trline).style.background = '#ffffff';
	}
    else
	{
	    document.getElementById(mname).style.display = "inline";
	    lastname=mname;
	    document.getElementById(trline).style.background = '#ffff00';
	    lastline=trline;
	}
}


function runblast()
{
    setdb();
    var ptypes  = new Array();
    ptypes['blastp']='Protein';
    ptypes['blastp']='Protein';
    ptypes['blastn']='Nucleotide';
    ptypes['blastx']='Protein';
    ptypes['tblastn']='Nucleotide';
    ptypes['tblastx']='Nucleotide';

    if (document.blastrun.sl_pk.value==0)
	{
	    window.alert("You must select a Search Library");
	    document.blastrun.dbselect.focus();
	    return;
	}
    //
    // removed JHR multiple search libraries check needs done elsewhere
    //
    //    if (ptypes[document.blastrun.p_opt.value] != document.blastrun.sltname.value)
    //{
    //    window.alert("Please check the Blast Program description.\nYour program selection requires a "+ptypes[document.blastrun.p_opt.value]+" search library");
    //    document.blastrun.p_opt.focus();
    //    return;
    //}

    if (document.blastrun.query.value == "")
	{
	    window.alert("Please enter a sequence or click the COPY EXAMPLE");
	    document.blastrun.p_opt.focus();
	    return;
	}
    /* window.alert(""); */

    document.blastrun.submit();
}


// Var dbselect is a string of 4 comma separated values.  This func is
// called on page load so that we'll have a default value. This makes
// the back button work. Otherwise lib_button is invisible when the
// user clicks the back button (at least in Firefox).

function setdb()
{
    var nbrselected=0;

    for (var i=0; i<document.blastrun.dbselect.length;   i++)
    	{
	    if (document.blastrun.dbselect.options[i].selected==true) { nbrselected++; }
	}
    
    // This could be done with inline values, but it is some what
    // clearer to use a few vars. Nothing on the web UI needs the last
    // two values of the split, so we'll ignore them here.

    var dbvars = document.blastrun.dbselect.options[1].value.split(",");

    document.blastrun.sl_pk.value=dbvars[0];
    document.blastrun.slf_pk.value=dbvars[1];
    document.blastrun.slname.value=dbvars[2];
    document.blastrun.sltname.value=dbvars[3];

    if (dbvars[0]==0)
	{
	    turnonall(dbvars[3]);
	}
    else
	{
	    
	    var slf_pk = document.blastrun.slf_pk.value;
	    var sl_pk  = document.blastrun.sl_pk.value;
	    //
	    // removed jhr multiple search library selection ( need anotehr method to show library information )
	    //
	    //	    document.getElementById('lib_button').href = "sl_list.pl?sl_pk="+sl_pk+"&slf_pk="+slf_pk;
	}
       
}


//
// turn on all nucletide or protein libraries not implemented yet (
// not turning everything on ) hmm something missing for now let them
// select the libraries they are in TYPE, NAME order so they can
// select all of them in one mouse swip
//

function turnonall(libtype)
{
    for (var i=0; i<document.blastrun.dbselect.length;   i++)
    	{
	    document.blastrun.dbselect.options[i].slected = false;
    	    var dbvars = document.blastrun.dbselect.options[i].value.split(",");
	    if (dbvars[0]>0)
		{
		    if (dbvars[3]==libtype)
			{
			    document.blastrun.dbselect.options[i].slected = true;
			}
		}
    	}
}


//
// Turn on all checkboxes
//

function contig_checkall(doit)
{
    if (doit==1)
	{
	    for (idx=0;idx<document.hitdata.contigseq.length;idx++)
		{
		    document.hitdata.contigseq[idx].checked=true;
		}
	}
    else
	{
	    for (idx=0;idx<document.hitdata.contigseq.length;idx++)
		{
		    document.hitdata.contigseq[idx].checked=false;
		}
	}    
}


//
// get all trace names into one variable
//

function run_contig()
{
    document.contig.hitseqs.value="";
    var selcount=0;

    for (idx=0;idx<document.hitdata.contigseq.length;idx++)
	{
	    if (document.hitdata.contigseq[idx].checked==true)
		{
		    document.contig.hitseqs.value = document.contig.hitseqs.value + document.hitdata.contigseq[idx].value + "~";
		    selcount++;
		}
	}

    document.contig.minmatch.value = document.hitdata.minmatch.value;

    if (selcount>=2)
	{
	    document.contig.submit();
	}
    else
	{
	    window.alert("I'm sorry but you must select\nat least two Hit_id's");
	}
}

