/*
***************************************************************************

  Biomolecular Interaction Network Database (BIND)

  The Blueprint Initiative
  522 University Avenue, 9th Floor, Suite 900
  Toronto, Ontario, Canada, M5G 1W7

  Hogue Lab - University of Toronto Biochemistry Department
  Samuel Lunenfeld Research Institute, Mount Sinai Hospital

  Publication to cite:
  Bader GD, Betel D, Hogue CW. (2003) BIND: the Biomolecular
  Interaction Network Database. Nucleic Acids Res. 31(1): 248-50
  PMID: 12519993

  Copyright Notice:

  Copyright 2003, 2004 Mount Sinai Hospital (MSH)

  This program is free software; you can redistribute it and/or 
  modify it under the terms of the GNU General Public License as 
  published by the Free Software Foundation; either version 2 of 
  the License, or any later version.

  This program is distributed in the hope that it will be useful, 
  but WITHOUT ANY WARRANTY; without even the implied warranty of 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License 
  along with this program; if not, write to the
         Free Software Foundation, Inc., 
         59 Temple Place, Suite 330, Boston, MA  
         02111-1307  USA 
  or visit http://www.gnu.org/copyleft/gpl.html

***************************************************************************/

var isAlreadySubmitted = false;

function setSubmitState(state)
{
	isAlreadySubmitted = state;
}

function checkSubmitState(href, target)
{
	if (isAlreadySubmitted)
	{
		alert("Please wait; your request is already being processed");
	}
	else
	{		
		if (target)
		{
			window.open(href, target);
		}
		else
		{
			window.location = href;
		}
	}
	return false;
}

function dontCheckSubmitState(href, target)
{
	if (target)
	{
		window.open(href, target);
	}
	else
	{
		window.location = href;
	}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_nbGroup(event, grpName) { //v6.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    nbArr = document[grpName];
    if (nbArr)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
      nbArr[nbArr.length] = img;
  } }
}

/**
 * Sets a button's butval (action) and depth values.
 *
 * @param theform	The source form submitted; one of [isub | mcsub | psub]
 * @param thename	A string of the action associated with this button.
 * @param depth		The depth of this button. Top-level tables and their buttons are level 0 (as well
 *					as the navigation buttons). Child tables of top-level tables are level 1, etc.
 */
function submitBINDButton(theform, thename, depth)
{
	setSubmitState(true);
	if (theform.butval) {theform.butval.value=thename;}
	if(depth>-1) {theform.actionDepth.value=depth;}
	theform.submit();
}

function submitIndexedBINDButton(theform, tablevalue, indexvalue)
{
	theform.tabletype.value = tablevalue;
	theform.actionIndex.value = indexvalue;
}

function confirmFormSubmit(theform, message, tablevalue)
{
	if (confirm(message)) {
		theform.tabletype.value = tablevalue;
		return true;
	} else {
		return false;
	}
}

function confirmIndexedSubmit(theform, message, tablevalue, indexvalue)
{
	if (confirm(message)) {
		theform.tabletype.value = tablevalue;
		theform.actionIndex.value = indexvalue;
		return true;
	} else {
		return false;
	}
}

function callNRSeqHound(id, mol)
{
	if(id=="" || id=="0")
		alert('You must fill in an identifier before retrieving the non-redundant seqhound list.');
	else
		window.open('Action?pg=10&id=' + escape(id) + '&mol=' + escape(mol));
}


function callHelp(id, iprop)
{
	window.open('Action?pg=30&id=' + id + '&iprop=' + iprop, 'bindHelp', 'menubar=no,width=300,height=300').focus();
}


/**
 * Opens a location in the current window.
 * @param location  url to open
 * @param params    parameters (must include '?')
 */
function openLocation(form, location, params)
{
	if (params != null) {
		location = location + params;
	}
	window.location = location;
	return false;
}

function setRecFormat(idquery, idqueryformat)
{
	if(document.forms['idqueryformat']!=null) {
		idquery.recordStyle.value = idqueryformat.recordStyle.value;
	}
}

function toggleSHound(theform) {
	if(theform.shoundOverRide.value=='false') {
		theform.shoundOverRide.value='true';
	} else {
		theform.shoundOverRide.value='false';
	}
	
	submitBINDButton(theform, 'shoundToggle', -1);
}

function openCutPasteDialog(formName, tabletype, actionIndex, actionDepth, numVariable) {
	window.open('Action?pg=1999&tabletype=' + escape(tabletype) + '&actionIndex=' + escape(actionIndex) + '&formName=' + escape(formName) + '&actionDepth=' + escape(actionDepth) + '&numVariable=' + escape(numVariable), 'BIND_CUTPASTE', 'menubar=no,width=300,height=400');
}

var imagePrefix = 'images/bbs/XSLT/';
var expandImage = imagePrefix + 'expand.jpg';
var collapseImage = imagePrefix + 'collapse.jpg';

function getElement(id, component)
{
    return document.getElementById(id + '_' + component);
}

function doExpandCollapse(id)
{
    var content = getElement(id, 'content');
    if (content.style.display == 'none')
    {
        expand(id);
    }
    else
    {
        collapse(id);
    }
}

function expand(id)
{
    var content = getElement(id, 'content');
    var summary = getElement(id, 'summary');
    var expand = getElement(id, 'expand');

    content.style.display = 'block';
    expand.src = collapseImage;

    if (summary != null)
    {
        summary.style.display = 'none';
    }
}

function collapse(id)
{
    var content = getElement(id, 'content');
    var summary = getElement(id, 'summary');
    var expand = getElement(id, 'expand');

    content.style.display = 'none';
    expand.src = expandImage;

    if (summary != null)
    {
        summary.style.display = 'block';
    }
}

function expandNI(id)
{
    var content = getElement(id, 'content');
    var summary = getElement(id, 'summary');

    content.style.display = 'block';

    if (summary != null)
    {
        summary.style.display = 'none';
    }

}

function collapseNI(id)
{
    var content = getElement(id, 'content');
    var summary = getElement(id, 'summary');

    content.style.display = 'none';

    if (summary != null)
    {
        summary.style.display = 'block';
    }
}


function openLinkOut(sel) 
{
	var URL;
	var win = "";
	
	if (typeof(sel) == "string") {
		URL = sel;
	} else if (sel.selectedIndex) {
		// Select list option with url and page title
	    URL = sel.options[sel.selectedIndex].value;
	    win = sel.options[sel.selectedIndex].text.replace(" ", "");
	    sel.selectedIndex = 0;
    
	    //This is a hack and we should figure out another way to make this feature general
	    if(win=='StructureViewer') {
	        openLink(URL);
	        return;
	    }
	} else {
		// Field where value is url
		URL = sel.value;
	}
	
	//Make sure calls to BIND stay in the same window
	if(win=='BIND') {
		win="_self";
	}
    
    if (URL != "") window.open(URL, win);
}

function openLink(URL)
{
    if(URL!="") location = URL;
}

function submitenter(myfield, e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13) {
		myfield.form.submit();
		return false;
	} else {
		return true;
	}
}

function launch_viewer(sel) {
	if(sel.options[sel.selectedIndex].value == -1) return;

	if(sel.options[sel.selectedIndex].text == 'Interaction Network 3.0') {
 		location.href = sel.options[sel.selectedIndex].value;
	}
//	else if(sel.options[sel.selectedIndex].text == 'Interaction Network 2.0') {
//		window.open(sel.options[sel.selectedIndex].value,'Visualize','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=540,height=560');
//	}
	else {
		window.open(sel.options[sel.selectedIndex].value,'Visualize','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=540,height=560');
	}
}

function doDirectoryExpandCollapse(id)
{
	doExpandCollapseHTML(id, imagePrefix + 'folderOpen.gif', imagePrefix + 'folderClosed.gif', null);
}

function doExpandCollapseHTML(id, imgCol, imgExp, title) {

    var content = getElement(id, 'content');
    if (content.style.display == 'none')
    {
        expandHTML(id, imgCol, title);
    }
    else
    {
        collapseHTML(id, imgExp, title);
    }
	
}

function expandHTML(id, img, title)
{
    var content = getElement(id, 'content');
    var summary = getElement(id, 'summary');
    var expand = getElement(id, 'expand');

    content.style.display = 'block';
    if(img!=null) {
    	expand.src = img;
    }
    else {
	    expand.src = collapseImage;
    }

    if(title!=null) {
    	expand.title = "Collapse " + title;
    	expand.alt = "Collapse " + title;
    }

    if (summary != null)
    {
        summary.style.display = 'none';
    }
}

function collapseHTML(id, img, title)
{
    var content = getElement(id, 'content');
    var summary = getElement(id, 'summary');
    var expand = getElement(id, 'expand');

    content.style.display = 'none';
    if(img!=null) {
    	expand.src = img;
    }
    else {
	    expand.src = expandImage;
    }

    if(title!=null) {
    	expand.title = "Expand " + title;
    	expand.alt = "Expand " + title;
    }

    if (summary != null)
    {
        summary.style.display = 'block';
    }
}
