function gtws=idparest(arg,onoff,enforce)
%IDPAREST  The basic callback function for parametric estimation.
%	Arguments:
%	open   Creates the dialog box for parametric estimation
%	mstype Creates the correct dialog box for model structure editor
%	mspop  Sets the correct pop-up menu for the various supported
%              model structures. (This depends on the inputs and outputs
%	       in the estimation data.)

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

global XIDparest  XIDio XIDmse XIDiter  XIDlayout XIDhw XIDarx XIDss XIDsumb
if strcmp(arg,'close')
    set(XIDparest(1),'Visible','off')
    eval('set(iduiwok(21),''vis'',''off'');','')
    eval('set(iduiwok(22),''vis'',''off'');','')
    return
end
usd=get(XIDhw(3,1),'userdata');
nu=usd(1);ny=usd(2);

if strcmp(arg,'open')
   if nargin<2,onoff='on';end
   FigName=idlaytab('figname',20);
   if ~figflag(FigName,0)
       iduistat('Opening the Parametric Models dialog ...')
       if ~exist('XIDmse'), XIDmse=[0 0 0 0];end
       
       layout
       butw = mStdButtonWidth;
       FigW = iduilay2(3);
       f = figure('NumberTitle','off','Name',FigName,...
          'userdata',[nu ny],'tag','sitb20',...
          'Color',get(0,'DefaultUIControlBackgroundColor'),'vis','off');
       XIDparest(1) = f;
       set(f,'Menubar','none','Nextplot','new');
       s1='iduipoin(1);';s2='iduipoin(1);iduistat(''Compiling ...'');';
       s3='iduipoin(2);';
% ******************
       % LEVEL 1
       pos = iduilay1(FigW,3);%butwh,ftb,bb,bb,etf,lev1,3);
       uicontrol(f,'pos',pos(1,:),'style','frame');
       cb=[s2,'goto_ws=iduiarx(''estimate'');',...
           'if goto_ws,XIDarg=''arx'';idgtws;end,',s3];
       XIDparest(2)=uicontrol(f,'pos',pos(2,:),'string',...
                'Estimate','style','push','callback',cb);
       set(XIDparest(2),'UserData',str2mat('[]','[]','[]','[]'))
       uicontrol(f,'pos',pos(3,:),'string','Close',...
                'style','push','callback',[s1,'idparest(''close''),',s3]);
       uicontrol(f,'pos',pos(4,:),'string','Help','style',...
                'push','callback',...
                'iduihelp(''idparest.hlp'',''Help: Estimating Models'');');


%  LEVEL 2
%       lev2 = pos(1,2)+pos(1,4);
%       pos = iduilay1(FigW,2,1,lev2,[],1.5);
%       uicontrol(f,'pos',pos(1,:),'style','frame');
       % LEVEL 3
       lev3 = pos(1,2)+pos(1,4);
       %pos = iduilay1(FigW,3,1,lev3);
       pos = iduilay1(FigW,12,6,lev3,[],[1 2.2]);
       uicontrol(f,'pos',pos(1,:),'style','frame');
       XIDparest(5)=uicontrol(f,'pos',pos(12,:)+[0 0 0.5*mStdButtonWidth 0],...
           'style','push','string','Iteration control...','enable','off',...
           'callback',[s1,'iduiiter(''open'');',s3]);
       XIDparest(6)=uicontrol(f,'pos',pos(13,:)+[0.7 0 -0.7 0]*mStdButtonWidth,...
           'style','push','string','Order editor...',...
           'callback',[s1,'idparest(''mse'');',s3]);
       uicontrol(f,'pos',pos(8,:),...
                 'string','Method:','style','text',...
                 'HorizontalAlignment','left');
       posmeth1=pos(9,:)+[0 0 -1.1*mStdButtonWidth 0];
       posmeth1 = posmeth1 + [0 0 -mEdgeToFrame 0];
       posmeth2=pos(9,:)+[1.1 0 -1.1 0]*mStdButtonWidth;
       posmeth2 = posmeth2 + [mEdgeToFrame 0 -mEdgeToFrame 0];
       XIDarx(1,2)=uicontrol(f,'pos',posmeth1,'string','ARX',...
               'callback',[s1,'iduiarx(''arx'');idparest(''mstype'');',s3],...
               'style','radio');
       XIDarx(1,5)=uicontrol(f,'pos',posmeth2,'string','IV',...
               'callback',[s1,'iduiarx(''iv'');idparest(''mstype'');',s3],...
               'style','radio');
       XIDss(1,2)=uicontrol(f,'pos',posmeth1,'string','PEM',...
            'callback',[s1,'iduiss(''pem'');idparest(''mstype'');',s3],...
            'style','radio','vis','off');
       XIDss(1,5)=uicontrol(f,'pos',posmeth2,'string','N4SID',...
           'callback',[s1,'iduiss(''ssss'');idparest(''mstype'');',s3],...
           'style','radio','vis','off');
       XIDparest(11)=uicontrol(f,'pos',pos(9,:),...
                'style','text','string','Prediction error method',...
                'vis','off','HorizontalAlignment','left');
       set(XIDarx(1,2),'value',1);set(XIDss(1,5),'value',1);

% ******************************
       % LEVEL 4
       
       uicontrol(f,'pos',pos(6,:),'style','text',...
           'HorizontalAlignment','left','string','Equation:');
       XIDparest(10)=uicontrol(f,'pos',pos(7,:),'style','text',...
            'HorizontalAlignment','left');

       XIDparest(4)=uicontrol(f,'pos',pos(3,:),'style','popup',...
              'callback',[s1,'idparest(''mstype'');',s3],'value',1);
       XIDparest(3)=uicontrol(f,'pos',pos(5,:),...
              'style','edit','callback',[s1,'idparest(''ordedit'');',s3],...
              'HorizontalAlignment','left',...
              'backgroundcolor','white','tag','modst');
       XIDparest(9)=uicontrol(f,'pos',pos(2,:),...
              'HorizontalAlignment','left',...
                'string','Structure:','style','text');
       uicontrol(f,'pos',pos(10,:),...
              'HorizontalAlignment','left',...
                'string','Name:','style','text');
       XIDparest(16)=uicontrol(f,'pos',pos(4,:),...
              'HorizontalAlignment','left',...
                'string','Orders:','style','text');
       XIDparest(7)=uicontrol(f,'pos',pos(11,:),...
             'style','edit','string','arx441',...
              'HorizontalAlignment','left',...
              'backgroundcolor','white');
       FigH=pos(1,2)+pos(1,4)+mEdgeToFrame;
       FigWH=[FigW,FigH];
       sumbpos=get(XIDsumb(1),'pos');
       FigXY=max([sumbpos(1) sumbpos(2)-FigH-25],[0 0]);
       set(f,'pos',[FigXY FigWH]);
       set(get(f,'children'),'units','normal')
       if length(XIDlayout)>19,if XIDlayout(20,3)
        	eval('set(f,''pos'',XIDlayout(20,1:4));','')
       end,end
       set(f,'vis',onoff)
        idparest('setpop')
        %idparest('mse');    
        idparest('mstype');
    end % if figflag
    nn=get(XIDparest(1),'userdata');
    if ~all(nn==[nu ny])
        set(XIDparest(1),'userdata',[nu ny])
        [flag,hnr]=figflag(idlaytab('figname',21),1);
        if flag
           viss=get(hnr,'vis');set(hnr,'vis','off');
        else
           viss='off';
        end
        idparest('setpop');
        idparest('mstype');
        if strcmp(viss,'on'),idparest('mse');end
    end
    iduistat('Choose model Structure and Orders and then press the Estimate button.')
% ************************

elseif strcmp(arg,'mstype')
   Mtype=get(XIDparest(4),'value');
   if (Mtype==1&ny==1&get(XIDarx(1,2),'value'))|... %%%
      (Mtype==5&get(XIDss(1,5),'value'))               %%%
           % These are the cases for order select      %%%
     set(XIDparest(5),'string','Order selection','enable','on',... %%%
         'callback','idparest(''ord_sel'');')          %%%
   else                                                %%%
     set(XIDparest(5),'string','Iteration control...',...  %%%
         'callback',['iduipoin(1);iduiiter(''open'');iduipoin(2);']) %%%
     onoffiter='on';itval=1;
     if Mtype==1,onoffiter='off';itval=0;end
     if Mtype==5&get(XIDss(1,5),'value'),onoffiter='off';itval=0;end
     set(XIDparest(5),'enable',onoffiter)
   end                                                  %%%
  eval('if strcmp(onoffiter,''off''),set(iduiwok(22),''vis'',''off'');end','')
   iduims('both',Mtype,nu,ny);
   idparest('ordedit');
   iduistat('Select orders and press the Estimate button.')
elseif strcmp(arg,'orders')
    err=0;
    eval('get(iduiwok(21),''name'');','err=1;') 
    if err,return,end
    Mtype=get(XIDparest(4),'value');
    if Mtype==1,iduiarx('orders')
    elseif any(Mtype==[2 3 4]), iduiio('orders')
    elseif Mtype==5, iduiss('orders');
    end
    iduistat('Press Estimate to estimate model.')
elseif strcmp(arg,'ordedit')
    set(XIDparest(3),'userdata',[]);
    Mtype=get(XIDparest(4),'value');
    if Mtype==1,flag=iduiarx('mname');
    elseif any(Mtype==[2 3 4]),flag=iduiio('mname');
    elseif Mtype==5,flag=iduiss('mname');
    end    
    if flag,return,end
    mse=0;
    [fl,fi]=figflag(idlaytab('figname',21),1);
    if fl,if strcmp(get(fi,'vis'),'on'),mse=1;end,end
    if mse
       if Mtype==1,iduiarx('ordedit')
       elseif any(Mtype==[2 3 4]), iduiio('ordedit')
       elseif Mtype==5, iduiss('ordedit');
       end
    end
    iduistat('Press Estimate to estimate model.')

elseif strcmp(arg,'setpop')
    str=idmspop(nu,ny);
    set(XIDparest(4),'string',str) 
elseif strcmp(arg,'mse')
    iduiio('open');idparest('ordedit');
elseif strcmp(arg,'estimate')
    Mtype=get(XIDparest(4),'value');
    if Mtype==1
        gtws=iduiarx('estimate');
    elseif any(Mtype==[2 3 4 6])
        gtws=iduiio('estimate');
    elseif Mtype==5
        iduiss('estimate');
    end
elseif strcmp(arg,'ord_sel')
   Mtype=get(XIDparest(4),'value');
   if Mtype==1
     str='[1:10 1:10 1:10]';
   else
     str='1:10';
   end
   set(XIDparest(3),'string',str)
   set(XIDparest(7),'string','')
   if iduiwok(21)
      set(XIDio(1,4),'value',13);
      if Mtype==1
         if ny>1|nu>4,nmu=min(1,nu);else nmu=nu;end
         set(XIDio(1:nmu,[1 3]),'value',13);
      end
   end
   iduistat('Press Estimate and select order in plot that will open.')
end

