// Code to make the menus work properly
// functions: init, hideIt, showIt, hideActive, popMenu
// Author: Liz Pardes
var ActiveMenu = null;

function init(){
   //document.getElementById('ToolsMenu').style.visibility="hidden";
   document.getElementById('CollMenu').style.visibility="hidden";
   document.getElementById('AboutMenu').style.visibility="hidden";
   document.getElementById( 'ResourcesMenu' ).style.visibility="hidden";
}

function hideIt(object) {
   object.style.visibility="hidden";
}

function showIt(object) {
   object.style.visibility="visible";
}

function hideActive(){
   if (ActiveMenu != null){
      hideIt(ActiveMenu);
      ActiveMenu = null;
   }
}

function popMenu(m)
{
   hideActive();
   ActiveMenu=document.getElementById(m);
   showIt(ActiveMenu);
}

// New Improved Version of openWin!
// doc: document
// args: elemname~urlarg,elemname~urlarg
//    elemname: Element name from the calling document, whose value will be
//              pulled and included in the url opened.
//    urlarg: the argument name to use in the url
// url: base url to be opened
// window_name: Window Name
function openWindow( doc, args, url, window_name ){

   if( url == null ){
      window.open( '', window_name, "WIDTH=800,HEIGHT=500,scrollbars" );
      return;
   }

   if( args == null ){
      if( window_name == 'Help'){
         window.open( url, window_name, "WIDTH=500,HEIGHT=500,scrollbars" );
      }else{
         window.open( url, window_name, "WIDTH=800,HEIGHT=500,scrollbars" );
      }
      return;
   }


   if( null == url.match( /\&$/ ) && null == url.match(/\?$/ ) ){
      url = url + "?";
   }
   // Multiple arguments
   var elements = new Array();
   elements = args.split( /,/ );

   for( var i = 0; i < elements.length; i++ ){
      if( null == elements[i].match( /~/ ) ){
         // INVALID FORMAT, ERROR
         alert( "ERROR: Invalid argument format: use element_name~url_argument" );
         return;
      }
      var e = new Array();
      e = elements[i].split( /~/ );
      var val = doc.getElementsByName( String( e[0] ) )[0].value;
      if( null == val ){
         // ERROR, COULD NOT GET VALUE
         alert( "ERROR: Unable to obtain value for element named " + e[0] );
         return;
      }

      if( i > 0 ){ url += "&"; }

      url += e[1] + "=" + val;

   }

   // Dislike this, as it removes from the generic-ness. Need to add a variable
   // to indicate "small window"
   if( window_name == 'Comments' || window_name == 'Help' ){
      window.open( url, window_name, "WIDTH=500,HEIGHT=500,scrollbars" );
   }else{
      window.open( url, window_name, "WIDTH=800,HEIGHT=500,scrollbars" );
   }

}

// A generic-use function intended to be used by a popup. The popup ( child )
// calls this function to update a value in the parent page.
// UPDATED: You must call your own self.close() if you want the popup to close
// after calling this function.
function update_parent( d, name ){
   var child = d.getElementsByName( String( name ) )[0];

   var parent = opener.document.getElementsByName( String( name ) )[0];

   parent.value = child.value;

}

// Update the value of an input element
// Supply the document (d), input name (n), new value (v), element index (b)
function upd_val( d, n, v, b ){
   var input = d.getElementsByName( String( n ) )[b];
   input.value = v;
}

function openTool( t ){
   if( t == 'IQ' ){
      window.open("http://image.hudsonalpha.org/IQ/html/category.shtml", "IQ");
   }else if( t == 'MGC' ){
      window.open( "http://mgc.ucsc.edu/cgi-bin/hgGateway", "MGCwin" );
   }else if( t == 'DFCI' ){
      window.open( "http://horfdb.dfci.harvard.edu/", "DFCIwin" );
   }else if( t == 'HIP' ){
      window.open( "http://flex.med.harvard.edu/FLEX/", "HIPwin" );
   }else if( t == 'OQ' ){
      window.open( "/OQ", "OQ" );
   }

}

