function dodsmsg(mode, messagestring)
global BoxHandle MsgHandle OkHandle IconAxes FrameHandle buttonhasbeenpressed

% Part of the DODS data browser.   MODE = 1 means use a popup window.
% MODE = 0 means display to workspace.  MESSAGESTRING is the string to
% be displayed.

% Cribbed very ungracefully to support both version 4 and 5
% by Deirdre Byrne, University of Maine, 1999/04/14 
% from Matlab's uitools/msgbox.m which is by:
%
%  Loren Dean
%  Copyright (c) 1984-98 by The MathWorks, Inc.
%  $Revision: 1.6 $

%%%%%%%%%%%%%%%%%%%%%
%%% Set Positions %%%
%%%%%%%%%%%%%%%%%%%%%
DefFigPos=get(0, 'DefaultFigurePosition');

MsgOff=7;
IconWidth=32;

FigWidth=190;
MsgTxtWidth=FigWidth-2*MsgOff-IconWidth;

FigHeight=50;
DefFigPos(3:4)=[FigWidth FigHeight];

OKWidth=40;
OKHeight=20;
OKXOffset=(FigWidth-OKWidth)/2;
OKYOffset=MsgOff;


MsgTxtXOffset=MsgOff;
MsgTxtYOffset=MsgOff+OKYOffset+OKHeight;
MsgTxtHeight=FigHeight-MsgOff-MsgTxtYOffset;
IconHeight=32;
IconXOffset=MsgTxtXOffset;
IconYOffset=FigHeight-MsgOff-IconHeight;

%%%%%%%%%%%%%%%%%%%%%
%%% Create MsgBox %%%
%%%%%%%%%%%%%%%%%%%%%
VERSION = version; VERSION = str2num(VERSION(1));
if nargin == 1
  messagestring = mode;
  % SPECIAL -- catch the 'buttonhasbeenpressed' event
  if strcmp(messagestring,'buttonhasbeenpressed')
    buttonhasbeenpressed = 1;
    return
  end
  % get the mode from the browser
  mode = browse('popupvalue');
end
flag = figflag('DODS Browser Message',1);
buttonhasbeenpressed = 0;
if (VERSION > 4 | ~flag) & mode
  load smlogo
  MsgTxtForeClr = [0 0 0];
  backcolor = [0.702 0.702 0.702];
  if VERSION < 5
    ScreenUnits=get(0, 'Units');
    set(0, 'Units', 'points');
    ScreenSize=get(0, 'ScreenSize');
    set(0, 'Units', ScreenUnits);
    DefFigPos(1)=(ScreenSize(3)-MsgTxtWidth)/2;
    DefFigPos(2)=(ScreenSize(4)-(MsgTxtHeight+35))/2;
    BoxHandle=figure('Units', 'points', ...
	'Name', 'DODS Browser Message', ...
	'color', backcolor, ...
	'position', [DefFigPos(1:2) 69*8.2 (MsgTxtHeight+35)], ...
	'NumberTitle','off', ...
	'pointer','arrow','visible','off', ...
	'resize','off','nextplot','new');
    FrameHandle = uicontrol(BoxHandle,'style','frame', ...
	'units','points', ...
	'position', [50.0000  0  497.3280   31.6683], ...
	'backgroundcolor',backcolor);
    OkHandle = uicontrol(BoxHandle,'style','radiobutton', ...
	'units','points', ...
	'foregroundcolor',MsgTxtForeClr,...
	'backgroundcolor',backcolor,...
	'position', [250  6.2377 OKWidth OKHeight], ...
	'callback', '', ...
	'string','OK');
    MsgHandle = uicontrol(BoxHandle,'style','edit', ...
	'Max',2, ...
	'foregroundcolor',MsgTxtForeClr, ...
	'backgroundcolor',backcolor, ...
	'string', '', ...
	'horiz','left', ...
	'units','points', ...
	'position', [46.8392   32.6280  497.32800   45.0000]);
    
    IconAxes=axes('Parent',  BoxHandle,  ...
	'Units', 'points',  ...
	'Position', [IconXOffset IconYOffset+20 ...
	  IconWidth IconHeight]);
    
    Img=image('CData',small_logo,'Parent',IconAxes); 
    set(IconAxes,  ...
	'XLim', get(Img,'XData')+[-0.5 0.5], ...
	'YLim', get(Img,'YData')+[-0.5 0.5], ...
	'Visible', 'on',  ...
	'xtick',[], 'ytick',[], ...
	'xcolor', backcolor, ...
	'ycolor', backcolor, ...
	'YDir', 'reverse');
  else % using VERSION 5
    BoxHandle=dialog('Name',  'DODS Browser Message', ...
	'color', backcolor, ...
	'Pointer', 'arrow', ...
	'Units', 'points', ...
	'Visible', 'off');
    OkHandle=uicontrol(BoxHandle,  ...
	'Style' , 'pushbutton',  ...
	'backgroundcolor', backcolor, ... 
	'Units', 'points',  ...
	'Position', [OKXOffset OKYOffset OKWidth OKHeight], ...
	'String', 'OK',  ...
	'callback', 'dodsmsg(''buttonhasbeenpressed'')', ...
	'HorizontalAlignment', 'center',  ...
	'Tag', 'OKButton' ...
	);
    FigColor=get(BoxHandle, 'Color');
    MsgTxtBackClr=FigColor;
    MsgHandle=uicontrol(BoxHandle, ...
	'Style', 'text',  ...
	'Units', 'points',  ...
	'Position', [MsgTxtXOffset ...
	  MsgTxtYOffset ...
	  MsgTxtWidth ...
	  MsgTxtHeight ...
	  ],  ...
	'String', ' ',  ...
	'Tag', 'MessageBox',  ...
	'HorizontalAlignment', 'left',  ... 
	'BackgroundColor', MsgTxtBackClr,  ...
	'FontWeight', 'bold',  ...
	'ForegroundColor', MsgTxtForeClr, ...
	'visible','on');
    IconAxes=axes('Parent',  BoxHandle,  ...
	'Units', 'points',  ...
	'Position', [IconXOffset IconYOffset ...
	  IconWidth IconHeight]);
    
    Img=image('CData',small_logo,'Parent',IconAxes); 
    set(IconAxes,  ...
	'XLim', get(Img,'XData')+[-0.5 0.5], ...
	'YLim', get(Img,'YData')+[-0.5 0.5], ...
	'Visible', 'on',  ...
	'xtick',[], 'ytick',[], ...
	'xcolor', backcolor, ...
	'ycolor', backcolor, ...
	'YDir', 'reverse');
  end
  % these things not version-dependent
  palette = [0.0000 0.0000 0.0000
    0.2000 0.0000 0.3000
    0.2000 0.0000 0.3000
    0.2000 0.0000 0.3000
    0.2000 0.0000 0.3000
    0.2000 0.0000 0.3000
    0.2000 0.0000 0.3000
    0.2000 0.0000 0.3000
    0.2000 0.0000 0.3000
    0.3700 0.0000 0.5000
    0.3700 0.0000 0.5000
    0.3700 0.0000 0.5000
    0.3700 0.0000 0.5000
    0.3700 0.0000 0.5000
    0.3700 0.0000 0.5000
    0.3700 0.0000 0.5000
    0.3700 0.0000 0.5000
    0.3700 0.0000 0.5000
    0.4516 0.0000 0.7000
    0.4516 0.0000 0.7000
    0.4516 0.0000 0.7000
    0.4516 0.0000 0.7000
    0.4516 0.0000 0.7000
    0.4516 0.0000 0.7000
    0.4516 0.0000 0.7000
    0.4516 0.0000 0.7000
    0.4516 0.0000 0.7000
    0.4548 0.0323 0.8710
    0.4548 0.0323 0.8710
    0.4548 0.0323 0.8710
    0.4548 0.0323 0.8710
    0.4548 0.0323 0.8710
    0.4548 0.0323 0.8710
    0.4548 0.0323 0.8710
    0.4548 0.0323 0.8710
    0.4548 0.0323 0.8710
    0.4548 0.2323 1.0000
    0.4548 0.2323 1.0000
    0.4548 0.2323 1.0000
    0.4548 0.2323 1.0000
    0.4548 0.2323 1.0000
    0.4548 0.2323 1.0000
    0.4548 0.2323 1.0000
    0.4548 0.2323 1.0000
    0.4548 0.2323 1.0000
    0.3323 0.2323 1.0000
    0.3323 0.2323 1.0000
    0.3323 0.2323 1.0000
    0.3323 0.2323 1.0000
    0.3323 0.2323 1.0000
    0.3323 0.2323 1.0000
    0.3323 0.2323 1.0000
    0.3323 0.2323 1.0000
    0.3323 0.2323 1.0000
    0.2300 0.3200 1.0000
    0.2300 0.3200 1.0000
    0.2300 0.3200 1.0000
    0.2300 0.3200 1.0000
    0.2300 0.3200 1.0000
    0.2300 0.3200 1.0000
    0.2300 0.3200 1.0000
    0.2300 0.3200 1.0000
    0.2300 0.3200 1.0000
    0.0323 0.4193 1.0000
    0.0323 0.4193 1.0000
    0.0323 0.4193 1.0000
    0.0323 0.4193 1.0000
    0.0323 0.4193 1.0000
    0.0323 0.4193 1.0000
    0.0323 0.4193 1.0000
    0.0323 0.4193 1.0000
    0.0323 0.4193 1.0000
    0.0323 0.5193 1.0000
    0.0323 0.5193 1.0000
    0.0323 0.5193 1.0000
    0.0323 0.5193 1.0000
    0.0323 0.5193 1.0000
    0.0323 0.5193 1.0000
    0.0323 0.5193 1.0000
    0.0323 0.5193 1.0000
    0.0323 0.5193 1.0000
    0.1323 0.6161 0.9065
    0.1323 0.6161 0.9065
    0.1323 0.6161 0.9065
    0.1323 0.6161 0.9065
    0.1323 0.6161 0.9065
    0.1323 0.6161 0.9065
    0.1323 0.6161 0.9065
    0.1323 0.6161 0.9065
    0.1323 0.6161 0.9065
    0.1323 0.8097 0.9065
    0.1323 0.8097 0.9065
    0.1323 0.8097 0.9065
    0.1323 0.8097 0.9065
    0.1323 0.8097 0.9065
    0.1323 0.8097 0.9065
    0.1323 0.8097 0.9065
    0.1323 0.8097 0.9065
    0.1323 0.8097 0.9065
    0.1323 1.0000 1.0000
    0.1323 1.0000 1.0000
    0.1323 1.0000 1.0000
    0.1323 1.0000 1.0000
    0.1323 1.0000 1.0000
    0.1323 1.0000 1.0000
    0.1323 1.0000 1.0000
    0.1323 1.0000 1.0000
    0.1323 1.0000 1.0000
    0.0000 1.0000 0.6600
    0.0000 1.0000 0.6600
    0.0000 1.0000 0.6600
    0.0000 1.0000 0.6600
    0.0000 1.0000 0.6600
    0.0000 1.0000 0.6600
    0.0000 1.0000 0.6600
    0.0000 1.0000 0.6600
    0.0000 1.0000 0.6600
    0.0323 1.0000 0.4511
    0.0323 1.0000 0.4511
    0.0323 1.0000 0.4511
    0.0323 1.0000 0.4511
    0.0323 1.0000 0.4511
    0.0323 1.0000 0.4511
    0.0323 1.0000 0.4511
    0.0323 1.0000 0.4511
    0.0323 1.0000 0.4511
    0.1313 1.0000 0.3200
    0.1313 1.0000 0.3200
    0.1313 1.0000 0.3200
    0.1313 1.0000 0.3200
    0.1313 1.0000 0.3200
    0.1313 1.0000 0.3200
    0.1313 1.0000 0.3200
    0.1313 1.0000 0.3200
    0.1313 1.0000 0.3200
    0.3226 1.0000 0.2000
    0.3226 1.0000 0.2000
    0.3226 1.0000 0.2000
    0.3226 1.0000 0.2000
    0.3226 1.0000 0.2000
    0.3226 1.0000 0.2000
    0.3226 1.0000 0.2000
    0.3226 1.0000 0.2000
    0.3226 1.0000 0.2000
    0.4193 0.9032 0.0323
    0.4193 0.9032 0.0323
    0.4193 0.9032 0.0323
    0.4193 0.9032 0.0323
    0.4193 0.9032 0.0323
    0.4193 0.9032 0.0323
    0.4193 0.9032 0.0323
    0.4193 0.9032 0.0323
    0.4193 0.9032 0.0323
    0.5161 0.8065 0.0323
    0.5161 0.8065 0.0323
    0.5161 0.8065 0.0323
    0.5161 0.8065 0.0323
    0.5161 0.8065 0.0323
    0.5161 0.8065 0.0323
    0.5161 0.8065 0.0323
    0.5161 0.8065 0.0323
    0.5161 0.8065 0.0323
    0.6129 0.7097 0.0323
    0.6129 0.7097 0.0323
    0.6129 0.7097 0.0323
    0.6129 0.7097 0.0323
    0.6129 0.7097 0.0323
    0.6129 0.7097 0.0323
    0.6129 0.7097 0.0323
    0.6129 0.7097 0.0323
    0.6129 0.7097 0.0323
    0.6774 0.6774 0.0323
    0.6774 0.6774 0.0323
    0.6774 0.6774 0.0323
    0.6774 0.6774 0.0323
    0.6774 0.6774 0.0323
    0.6774 0.6774 0.0323
    0.6774 0.6774 0.0323
    0.6774 0.6774 0.0323
    0.6774 0.6774 0.0323
    0.7742 0.7742 0.0323
    0.7742 0.7742 0.0323
    0.7742 0.7742 0.0323
    0.7742 0.7742 0.0323
    0.7742 0.7742 0.0323
    0.7742 0.7742 0.0323
    0.7742 0.7742 0.0323
    0.7742 0.7742 0.0323
    0.7742 0.7742 0.0323
    0.8710 0.8710 0.0323
    0.8710 0.8710 0.0323
    0.8710 0.8710 0.0323
    0.8710 0.8710 0.0323
    0.8710 0.8710 0.0323
    0.8710 0.8710 0.0323
    0.8710 0.8710 0.0323
    0.8710 0.8710 0.0323
    0.8710 0.8710 0.0323
    0.9677 0.9677 0.0323
    0.9677 0.9677 0.0323
    0.9677 0.9677 0.0323
    0.9677 0.9677 0.0323
    0.9677 0.9677 0.0323
    0.9677 0.9677 0.0323
    0.9677 0.9677 0.0323
    0.9677 0.9677 0.0323
    0.9677 0.9677 0.0323
    1.0000 1.0000 0.3323
    1.0000 1.0000 0.3323
    1.0000 1.0000 0.3323
    1.0000 1.0000 0.3323
    1.0000 1.0000 0.3323
    1.0000 1.0000 0.3323
    1.0000 1.0000 0.3323
    1.0000 1.0000 0.3323
    1.0000 1.0000 0.3323
    1.0000 0.6400 0.1323
    1.0000 0.6400 0.1323
    1.0000 0.6400 0.1323
    1.0000 0.6400 0.1323
    1.0000 0.6400 0.1323
    1.0000 0.6400 0.1323
    1.0000 0.6400 0.1323
    1.0000 0.6400 0.1323
    1.0000 0.6400 0.1323
    1.0000 0.4444 0.0000
    1.0000 0.4444 0.0000
    1.0000 0.4444 0.0000
    1.0000 0.4444 0.0000
    1.0000 0.4444 0.0000
    1.0000 0.4444 0.0000
    1.0000 0.4444 0.0000
    1.0000 0.4444 0.0000
    1.0000 0.4444 0.0000
    1.0000 0.2258 0.0323
    1.0000 0.2258 0.0323
    1.0000 0.2258 0.0323
    1.0000 0.2258 0.0323
    1.0000 0.2258 0.0323
    1.0000 0.2258 0.0323
    1.0000 0.2258 0.0323
    1.0000 0.2258 0.0323
    1.0000 0.2258 0.0323
    0.9000 0.0000 0.0000
    0.9000 0.0000 0.0000
    0.9000 0.0000 0.0000
    0.9000 0.0000 0.0000
    0.9000 0.0000 0.0000
    0.9000 0.0000 0.0000
    0.9000 0.0000 0.0000
    0.9000 0.0000 0.0000
    0.9000 0.0000 0.0000
    0.8000 0.0000 0.0000
    0.8000 0.0000 0.0000
    0.8000 0.0000 0.0000
    0.8000 0.0000 0.0000];
  palette(1,:) = backcolor;
  set(BoxHandle, 'Colormap', palette);
end

% save original for later use:
textstring = messagestring;
if mode % use pop-up window
  if VERSION < 5
    TextStringLength = 69;
    if length(messagestring) > TextStringLength
      NumLines= ceil(length(messagestring)/TextStringLength);
      % find line breaks
      i = findstr(messagestring,setstr(10));
      % find lines > 69 chars
      i = [0 i length(messagestring)];
      d = diff(i);
      if any(d > 70)
	for j = 1:length(d)
	  if d(j) > 70
	    str = messagestring(i(j)+1:i(j+1));
	    l=1; k = [];
	    ii = findstr(str,' ');
	    iii = ii;
	    while length(iii) > 1
	      k(l) = max(find(iii < 70));
	      iii = iii(k(l)+1:length(iii))-iii(k(l));
	      l=l+1;
	    end
	    l = ii(cumsum(k))+i(j);
	  end
	  str = '';
	  for k = 1:length(l)
	    str = [str sprintf('\n')];
	  end
	  messagestring(l) = str;
	end
	messagestring = strrep(messagestring,setstr(10),[setstr(10) ' ']);
	messagestring = [' ' messagestring];
	NumLines = length(findstr(messagestring,setstr(10)));
      end
    else
      NumLines = 1;
      messagestring = [blanks(size(messagestring,1))' messagestring];
    end
    MsgTxtWidth = TextStringLength*8;
    MsgTxtHeight = (NumLines+2)*17;
    ScreenUnits=get(0, 'Units');
    set(0, 'Units', 'points');
    ScreenSize=get(0, 'ScreenSize');
    set(0, 'Units', ScreenUnits);
    % keep window from going off of screen!
    if ((MsgTxtHeight+65) > ScreenSize(4))
      oldmessagestring = messagestring;
    end
    while ((MsgTxtHeight+65) > ScreenSize(4))
      l = findstr(oldmessagestring,setstr(10));
      ll = length(l)-1;
      oldmessagestring = [oldmessagestring(1:l(ll)-1)];
      messagestring = [oldmessagestring(1:l(ll)-1) ' ...'];
      % chop at last newline
       warning =  sprintf('\n\n%s\n%s\n%s\n%s\n', ...
	   [ '... Due to the limitations of your screen size', ...
	     ' the Acknowledgements are'], ...
	   [ 'cut off here. The complete Acknowledgements have ',...
	     'been downloaded '], ...
	   [ 'into your Matlab workspace and are also provided ', ...
	     'with each data '],...
	   ' request from the server.');
      messagestring = sprintf('%s\n%s',messagestring, warning);
      NumLines = length(findstr(messagestring,setstr(10)));
      MsgTxtHeight = (NumLines+2)*17;
    end
    DefFigPos(1)=(ScreenSize(3)-MsgTxtWidth)/2;
    DefFigPos(2)=(ScreenSize(4)-(MsgTxtHeight+35))/2;
    set(MsgHandle,'string',messagestring);
    p = [DefFigPos(1:2) MsgTxtWidth MsgTxtHeight+35];
    set(BoxHandle,'position',p) 
    p = get(MsgHandle,'position');
    set(MsgHandle,'position', [p(1:2) MsgTxtWidth-50 MsgTxtHeight])
    p2 = get(IconAxes,'pos');
    set(IconAxes,'pos',[p2(1) p(2)+MsgTxtHeight-32 p2(3:4)])
    p = get(FrameHandle,'pos');
    set(FrameHandle,'pos', [p(1:2) MsgTxtWidth-50 p(4)])
  else % using version 5
    ScreenUnits=get(0, 'Units');
    set(0, 'Units', 'points');
    ScreenSize=get(0, 'ScreenSize');
    set(0, 'Units', ScreenUnits);
    
    eval('[WrapString,NewMsgTxtPos]=textwrap(MsgHandle,{messagestring},75);')
    NumLines=size(WrapString,1);
    MsgTxtWidth=max(MsgTxtWidth,NewMsgTxtPos(3));
    MsgTxtHeight=max(MsgTxtHeight,NewMsgTxtPos(4));
    if MsgTxtHeight > ScreenSize(4)
      oldmessagestring = messagestring;
    end
    while MsgTxtHeight > ScreenSize(4)
      l = findstr(oldmessagestring,setstr(10));
      ll = length(l)-1;
      oldmessagestring = [oldmessagestring(1:l(ll)-1)];
      messagestring = [oldmessagestring(1:l(ll)-1) ' ...'];
      % chop at last newline
       warning =  sprintf('\n\n%s\n%s\n%s\n%s\n', ...
	   [ '... Due to the limitations of your screen size', ...
	     ' the Acknowledgements are'], ...
	   [ 'cut off here. The complete Acknowledgements have ',...
	     'been downloaded '], ...
	   [ 'into your Matlab workspace and are also provided ', ...
	     'with each data '],...
	   ' request from the server.');
      messagestring = sprintf('%s\n%s',messagestring, warning);
      eval('[WrapString,NewMsgTxtPos]=textwrap(MsgHandle,{messagestring},75);')
      NumLines=size(WrapString,1);
      MsgTxtWidth=max(MsgTxtWidth,NewMsgTxtPos(3));
      MsgTxtHeight=max(FigHeight-MsgOff-MsgTxtYOffset,NewMsgTxtPos(4));
    end
    MsgTxtXOffset=IconXOffset+IconWidth+MsgOff;
    FigWidth=MsgTxtXOffset+MsgTxtWidth+MsgOff; 
    % Center Vertically around icon 
    if IconHeight>MsgTxtHeight, 
      IconYOffset=OKYOffset+OKHeight+MsgOff;
      MsgTxtYOffset=IconYOffset+(IconHeight-MsgTxtHeight)/2;
      FigHeight=IconYOffset+IconHeight+MsgOff; 
      % center around text 
    else, 
      MsgTxtYOffset=OKYOffset+OKHeight+MsgOff;
      IconYOffset=MsgTxtYOffset+(MsgTxtHeight-IconHeight)/2;
      FigHeight=MsgTxtYOffset+MsgTxtHeight+MsgOff; 
    end 
    
    FigWidth=MsgTxtWidth+2*MsgOff+80;
    MsgTxtYOffset=OKYOffset+OKHeight+MsgOff;
    FigHeight=MsgTxtYOffset+MsgTxtHeight+MsgOff; 
    
    OKXOffset=(FigWidth-OKWidth)/2; 

    DefFigPos(1)=(ScreenSize(3)-FigWidth)/2;
    DefFigPos(2)=(ScreenSize(4)-FigHeight)/2;
    DefFigPos(3:4)=[FigWidth FigHeight];

    set(BoxHandle, 'Position', DefFigPos);
    set(OkHandle, 'Position', [OKXOffset OKYOffset OKWidth OKHeight]); 
    
    set(MsgHandle,  ...
	'position', [MsgTxtXOffset MsgTxtYOffset  ...
	  MsgTxtWidth+30 MsgTxtHeight],  ...
	'string', messagestring, ...
	'max', NumLines);
  end
  set(BoxHandle, 'Visible', 'on');
  if VERSION < 5
    i = 0;
    drawnow
    while ~isempty(i)
      drawnow
      i = get(OkHandle,'value');
      if i == 1
	% user pressed the button!!!!
	set(BoxHandle,'visible','off')
	set(OkHandle,'value',0)
	break
      end
    end
  else % using version 5 ....
    drawnow
    while ~buttonhasbeenpressed
      % flush event queue to see if button has been pressed!
      drawnow
      flag = figflag('DODS Browser Message',1);
      if flag
	if buttonhasbeenpressed
	  break 
	end
      else
	% something has gone wrong -- figure is not present
	break
      end
    end
    delete(get(0,'currentfigure'))
  end
  return
else % user does not want a pop-up message
  disp('     ')
  disp(messagestring)
%  s = input('OK >> ');
%  waitforbuttonpress
  return
end % end of 'if mode'
