///////////////////////////////

//// Version 9


// Strip leading and trailing whitespace

function strip_whitespace( theval ) {

    // Ugly hack, but it's cross-platform

    theval = theval + "QAQQXXDEAZLDSL";	
    theval = theval.replace( /\s{0,}QAQQXXDEAZLDSL/g, "" );
    theval = theval.replace( /^\s*/g, "" );

    theval = theval.replace( /\s*$/g, "" );

    return theval;
}


// Check user entry for various problems

function is_valid_text( theval ) {

  // empty or just spaces
  var pattern = /^\s*$/;
  var fail = theval.match(pattern);

  if ( fail ) {
    return false;
  }

  return true;
}



function status_bar(str) {

    window.status = str;

    return true;
}




function verify_file( file ) {

    var regex = /\/$|\\$/;

    if ( file == "" ) {	
	return false;
    }

    if ( regex.test(file) ) {
	return false;
    }    
   
    return true;

}

function close_init_win( x ) {
	
    if ( (document.layers) && navigator.js_loaded != true ) {
      navigator.js_loaded = true;   
      win.window.close();
    }
    else if ( (document.all) && !window.name ) {
      win.window.close();
      window.name = true;
    }
}


// class LinkObj
function LinkObj(win_handle, url, id)
{
  this.mHandle = win_handle;
  this.mUrl = url;
  this.mID = id;
}


// class Util 
// class is used here to provide namespace protection
function Util() {}

// class property used to store window refs  
Util.winList = [];

//
// Util class method
//
// open_window
// a generic window.open function that will
// activate a window if it is already opened
// instead of reloading the content in the window
//
// url - required
// id - required
// settings - optional
// is_child - optional
//
// descriptions:
// url, id, settings, replace - same as the first four arguments of window.open
// is_child (boolean) - whether the opened window is the child of
// the caller window.  
//
// if a different url is provided with the same id, the url will be opened in the
// designated window
//
Util.open_window = function(url, id, settings)
{ 
  var winInst = Util.winList[id];
  var undef;

  if (winInst != undef)
  {
    if (winInst.mHandle.closed)
    {
      Util.winList[id] = null;
    }
    else
    {
      if (url == winInst.mUrl)
      {
        winInst.mHandle.focus();
        return;
      }
    }
  }

  if (settings == undef || settings == '')
  {
    settings = "resizable=yes";
  }

  var winhandle = window.open(url, id, settings);
  var urlObj = new LinkObj(winhandle, url, id);
  
  Util.winList[id] = urlObj;
  urlObj.mHandle.focus();
}
  

// Util class method
//
// close_childwin
// close all child windows spawned by the parent window
// will only work with window that is opened using Util.open_window
// routine 
// NOTE: this function is not in used currently
Util.close_childwin = function()
{
  if (self.closed)
  {
    for (id in winList)
    {
      if (winList[id].is_child)
      {
        winList[id].close();
        winList[id] = null;
      }
    }
  }
}

// end of Util class



function close_progress_win (  ) {
	
        var undef;
	
	if ( win2 != undef ) win2.close();
	
	 	
}


function scatter( url )
{
  Util.open_window( url, "scatterwin", 
                   "width=800,height=600,scrollbars=yes,location=yes,resizable=yes" );
}

function new_win( url, win_name)
{
   if (win_name == 'GO') { 
	Util.open_window( url, win_name, 
        "width=800,height=500,x=0,y=0,scrollbars=yes,location=yes,resizable=yes");
   }
  
   else if (win_name == 'seq') {
       Util.open_window( url, win_name, 
       "width=600,height=400,scrollbars=yes,location=yes,resizable=yes" );
   }

   else {
       Util.open_window( url, win_name, 
       "width=800,height=600,scrollbars=yes,location=yes,resizable=yes,toolbar=yes,menubar=yes" );
   }		
}


function seq( url )
{
   Util.open_window( url, "seqwin",
                    "width=600,height=400,scrollbars=yes,location=yes,resizable=yes" );
}


function blast( url )
{

  Util.open_window( url, "blastwin", 
                    "width=800,height=600,scrollbars=yes,location=yes,resizable=yes" );
}


// The use of unique id is necessary if you want different project to open in 
// different window
function project( url, id )
{
  Util.open_window( url, "projectwin"+id, 
                   "width=800,height=600,scrollbars=yes,location=yes,resizable=yes,toolbar=yes,menubar=yes" );
}

// The use of unique id is necessary if you want different project to open in 
// different window
function report( url, id )
{

  Util.open_window( url, "reportwin"+id, 
                   "width=970,height=700,scrollbars=yes,location=yes,resizable=yes,toolbar=yes,menubar=yes" );

}









function open_console( width, height, cgi, id, center )
{
    var settings = '';
    var undef;
 
    if (id == undef)
    {
      id = 'console';
    }
 
    if ( center != undef ) {
        var winl = (screen.width-width)/2;
        var wint = (screen.height-height)/2;
        settings = ',top=' + wint + ',left=' + winl + ',';
    }
   
    Util.open_window ("console.pl?" + cgi, id, settings + 
                      "resizable=yes,scrollbars=yes,status=1,width=" +
                      width + ",height=" + height);

}


// the function will submit a form to the parent window
// url - base cgi script name
// form - form handle
function parent_win(url, f)
{
  var undef;
  var cgi;

  if (f == undef)
    return false;

  for (var i = 0; i < f.elements.length; ++i)
  {
    var e = f.elements[i];
    if (e == undef) continue;
    if (e == undefined) continue;

    if (e.type == "select-one" || e.type == 'select-multiple')
    {
      for (var j = 0; j < e.options.length; ++j)
      {
        if (e.options[j].selected)
        {
          if (e.name != undef && e.name != undefined)
            cgi += ("&" + e.name + "=" + e.options[j].value);
        }
      }
     }
     else
     { 
        if (e.name != undef && e.value != undef)       
          cgi += ("&" + e.name + "=" + e.value);
     }
  }

  var complete_url = url + "?" + cgi;
  opener.location=complete_url;
}

function OpenHelp (id)
{
  Util.open_window ("console.pl?action=get_help&help_id=" + id, "help",
                    "resizable=yes,scrollbars=yes,status=0,width=400,height=300");
}

function OpenDataWindow (id)
{
  Util.open_window ("test.html", "Data",
                    "location=yes,resizable=yes,scrollbars=yes,status=0,width=400,height=300");
	
}


function CreateWindow (cgi)
{
  Util.open_window("create.pl?" + cgi, "Data",
                   "location=yes,resizable=yes,scrollbars=yes,status=0,width=400,height=500");
	
}


function UploadWindow (cgi)
{
  Utl.open_window ("upload.pl?" + cgi, "Data",
                   "location=yes,resizable=yes,scrollbars=yes,status=0,width=400,height=500");
}






