function iduiiter(arg)
%IDUIITER Handles all iteration control for parameter estimation methods.
%	Arguments
%	open     Opens the dialog box
%	interr   Sets the callback to be interruptible
%	continue Makes the iterations continue where stopped
%	stop     Stops the iterations
%	close    Closes the dialog window

%	L. Ljung 4-4-94
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.7 $  $Date: 1995/05/11 14:07:19 $

global XIDparest XIDsumb XIDiter XIDlayout

if strcmp(arg,'open')
   FigName=idlaytab('figname',22);
   if ~figflag(FigName)
       layout
       butw=mStdButtonWidth;
       PW = iduilay2(5);
       XIDiter(1)=figure('NumberTitle','off',...
       'Name',FigName,'NextPlot','New','tag','sitb22',...
       'Color',get(0,'DefaultUIControlBackgroundColor'),'vis','off');
       set(XIDiter(1),'Menubar','none');
       s1='iduipoin(1);';s3='iduipoin(2);';
% ******************
       % LEVEL 1
       pos = iduilay1(PW,5);
       uicontrol(XIDiter(1),'pos',pos(1,:),'style','frame');
       uicontrol(XIDiter(1),'pos',pos(4,:),'style','push','string',...
             'Options','callback',...
             [s1,'iduiopt(''dlg_ioopt'',''ioopt'',1,1:4);',s3]);
       uicontrol(XIDiter(1),'pos',pos(5,:),'string','Close',...
                'style','push','callback','iduiiter(''close'')');
       uicontrol(XIDiter(1),'pos',pos(6,:),'string','Help','style','push',...
                 'callback',...
                 'iduihelp(''iduiiter.hlp'',''Help: Iteration Control'');');
       XIDiter(7)=uicontrol(XIDiter(1),'pos',pos(2,:),...
                 'style','push','string','Stop',...
                 'callback',[s1,'iduiiter(''stop'');',s3],'enable','off');

       XIDiter(8)=uicontrol(XIDiter(1),'pos',pos(3,:),'string','Continue',...
                'style','push','callback',[s1,'iduiiter(''continue'');',s3]);


% ****************
        % LEVEL 2
     
       lev2 = pos(1,2)+pos(1,4);
       pos = iduilay1(PW,2,1,lev2,[],2.5);
       uicontrol(XIDiter(1),'pos',pos(1,:),'style','frame');
       XIDiter(2)=uicontrol(XIDiter(1),'pos',pos(2,:),'string',...
                 'Info to command line','style','check');
       XIDiter(3)=uicontrol(XIDiter(1),'pos',pos(3,:),'string',...
                 'Enable Interrupt',...
                 'style','check','callback',[s1,'iduiiter(''interr'');',s3]);
      
       % LEVEL 3
      
       lev3 = pos(1,2)+pos(1,4);
       pos = iduilay1(PW,8,2,lev3,[],[0.5 1.5 1.5 1.5]);
       uicontrol(XIDiter(1),'pos',pos(1,:),'style','frame');
       XIDiter(4)=uicontrol(XIDiter(1),'pos',pos(6,:),'style','text');
       XIDiter(5)=uicontrol(XIDiter(1),'pos',pos(7,:),'style','text');
       XIDiter(9)=uicontrol(XIDiter(1),'pos',pos(8,:),'style','text');
       XIDiter(6)=uicontrol(XIDiter(1),'pos',pos(9,:),'style','text');
       uicontrol(XIDiter(1),'pos',pos(2,:),'style','text',...
                'string','Iter #');
       uicontrol(XIDiter(1),'pos',pos(3,:),'style',...
                'text','string','Current fit');
       uicontrol(XIDiter(1),'pos',pos(4,:),'style',...
                'text','string','Previous fit');
       uicontrol(XIDiter(1),'pos',pos(5,:),'style','text',...
                'string','Update norm (%)'); 
       figpos = [50 40 PW pos(1,2)+pos(1,4)+mEdgeToFrame];
       set(XIDiter(1),'pos',figpos);      
       set(get(XIDiter(1),'children'),'unit','norm');
       if length(XIDlayout)>21,if XIDlayout(22,3)
         eval('set(XIDiter(1),''pos'',XIDlayout(22,1:4))','')
       end,end
  end

  set(XIDiter(1),'vis','on')
% ******************************

elseif strcmp(arg,'interr')
   if get(XIDiter(3),'value')
      yesno='yes';onoff='on';
   else 
      yesno='no';onoff='off';
   end
   set(XIDparest(2),'interruptible',yesno)
   set(XIDiter(8),'interruptible',yesno)
   set(XIDiter(7),'enable',onoff)
elseif strcmp(arg,'continue')
    mname=get(XIDparest(7),'string');
    set(XIDparest(3),'string',mname);
    set(XIDparest(7),'string',[mname,'c']);
     sumb=findobj(get(0,'children'),'flat','tag','sitb30');
     modax=findobj([XIDsumb(1);sumb(:)],'type','axes',...
          'tag',get(XIDparest(3),'userdata'));
     if ~isempty(modax)
         modax=modax(1);
         modn=findobj(modax,'tag','name');
         mod_inf=get(modn,'userdata');
         modlin=findobj(modax,'tag','modelline');
         if ~isempty(modlin)
                  nn=iduicalc('unpack',get(modlin,'UserData'),1);
         else nn=[];
         end
     else nn=[];
     end
    if isempty(nn),
       errordlg(str2mat('Failed to capture last model.',...
                        'To continue, drag-and-drop last estimated model to the',...
                        'Orders: edit field in the Parametric Models dialog box',...
                        'and then press the Estimate button.'));
       iduistat('') 
       return
    end
    iduistat(['Continuing using PEM from ',mname],1);
    iduiio('estimate',nn,mod_inf);
elseif strcmp(arg,'stop')
    set(XIDiter(7),'userdata',1);
    iduistat('Iterations will be stopped after the current one.')
elseif strcmp(arg,'close')
    set(XIDiter(1),'Visible','off')
    %set(XIDparest(5),'value',0)
end

