function err=iduiconf(arg,strr)
%IDUICONF Manages the confidence interval lines.
%	ARG:
%	onoff Checks the right menu item and toggles visibility of confidence lines
%	set   Sets the confidence according to the submenu
%	conf  Opens the dialog box
%	l1cb  Handles the info in the box when slider has changed
%	2     Ditto when # of standard deviations has changed
%	3     Ditto when desired confidence level has changed
%	apply Effectuates the new choice. The # of standard
%	      deviations are put as UserData of XIDmen(figno,8)

%	L. Ljung 4-4-94
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.7 $  $Date: 1995/05/02 16:32:08 $

global XIDplotw XIDcf XIDlayout
 
if strcmp(arg,'onoff')|strcmp(arg,'conf')|strcmp(arg,'set')
  hconf=findobj(gcf,'tag','confonoff');
  number=iduigetp(gcf);
  xusd=get(gcf,'UserData');[rxusd,cxusd]=size(xusd);
  xax=xusd(3:rxusd,1)';
  onoff=0;
  hm=get(gcf,'currentmenu');
end

if strcmp(arg,'onoff')
  offon=get(hm,'checked');
  if strcmp(offon,'off'),
     iduistat('Marking confidence intervals ...',0,number)
     onoff1='on';onoff2='off';
  else
     onoff2='on';onoff1='off';
  end
  [actmod,cacmod,confq]=fiactham;
  if strcmp(onoff1,'on'),if ~isempty(confq),idconfcp(confq),end,end
  for kk=xax
    if strcmp(get(kk,'visible'),'on')
       xusd=get(kk,'userData');[rxusd,cxusd]=size(xusd);
       indxv=2*actmod+2;
       iduivis(xusd(indxv,:),onoff1);
       if strcmp(onoff1,'on')
       if any(any(xusd(indxv,:)==-1))
          iduistat('No confidence region for N4SID models.',0,number)
       elseif any(any(xusd(indxv,:)==-2))
          iduistat('No confidence region for ETFE models.',0,number)
       else
        iduistat('Confidence intervals marked by dash-dotted lines.',0,number)
       end % if anyany
       else % of onoff1
        iduistat('Confidence intervals removed.',0,number)
       end
    end
  end
  set(hm,'checked',onoff1)
elseif strcmp(arg,'set')
   lab=get(hm,'label');labb=lab(find(lab~='&'));
   ch=get(hconf,'checked');
   set(hconf,'label',menulabel(['Show ',labb,' confidence &intervals']));
   set(hconf,'checked',ch);
   set(hconf,'userdata',get(hm,'userdata'));
   iduiclpw(number);
   iduistat(['Confidence interval changed to ',lab '.'])
elseif strcmp(arg,'conf')
   figname=idlaytab('figname',29);
   iduistat('Opening confidence dialog box ...')
   if ~figflag(figname,0)
       iduistat('Opening confidence dialog box ...')
       layout
       butwh=[mStdButtonWidth mStdButtonHeight];%butw=55;buth=25;
       butw=butwh(1);buth=butwh(2);
       ftb=mFrameToText;  % Frame to button
       bb = 2; % between buttons
       etf = mEdgeToFrame;
       FigW = iduilay2(3);

       XIDcf(1)=figure('Nextplot','New',...
             'NumberTitle','off','Name',figname,'vis','off','tag','sitb29',...
             'Color',get(0,'DefaultUIControlBackgroundColor'));
       set(XIDcf(1),'Menubar','none');
       s1='iduipoin(1);';s2=iduipoin(1);'iduistat(''Compiling ...'');';
       s3='iduipoin(3);';

       % LEVEL 1
       pos = iduilay1(FigW,3);
       uicontrol(XIDcf(1),'pos',pos(1,:),'style','frame');
       uicontrol(XIDcf(1),'Pos',pos(2,:),'style','push',...
                  'string','Apply','callback',[s1,'iduiconf(''apply'');',s3]);
       uicontrol(XIDcf(1),'Pos',pos(3,:),'style','push',...
          'callback','set(gcf,''visible'',''off'')','string','Close');
       uicontrol(XIDcf(1),'Pos',pos(4,:),'style','push',...
          'string','Help','callback','iduihelp(''idsetcon.hlp'',''Setting the Confidence Interval'');');

       % LEVEL2
       lev2 = pos(1,2)+pos(1,4)+mStdButtonHeight/2;

      XIDcf(2)=uicontrol(XIDcf(1),'pos',...
               [etf+ftb lev2 FigW-2*(etf+ftb) buth],'style','slider',...
               'max',10,'min',0,'callback',[s1,'iduiconf(''l1cb'');',s3]);

       % LEVEL 3
  
       lev3 = lev2+buth+mStdButtonHeight/2;
       pos = iduilay1(FigW,4,2,lev3,4,1.5);
       uicontrol(XIDcf(1),'pos',pos(1,:),'style','frame');
       XIDcf(3)=uicontrol(XIDcf(1),'pos',pos(4,:),'style','edit',...
           'callback',[s1,'iduiconf(''2'');',s3],'backgroundcolor','white',...
             'HorizontalAlignment','left');
      uicontrol(XIDcf(1),'pos',pos(2,:),'style','text','string',...
              'Number of std dev''s','HorizontalAlignment','left');
      XIDcf(4)=uicontrol(XIDcf(1),'pos',pos(5,:),'style','edit',...
           'callback',[s1,'iduiconf(''3'');',s3],'backgroundcolor','white',...
             'HorizontalAlignment','left');
      uicontrol(XIDcf(1),'pos',pos(3,:),'style','text','string',...
             'Confidence level','HorizontalAlignment','left');
       FigWH=[FigW pos(1,2)+pos(1,4)+etf];
       ScreenPos = get(0,'ScreenSize');
       FigPos=[(ScreenPos(3:4)-FigWH)/2 FigWH];
       set(XIDcf(1),'pos',FigPos);
      set(get(XIDcf(1),'children'),'unit','norm');              
      if length(XIDlayout)>28
       if XIDlayout(29,3)
          eval('set(XIDcf(1),''pos'',XIDlayout(29,1:4))','')
       end
     end
      set(XIDcf(1),'vis','on')
  end
  iduistat('Ready to set confidence level.')
  set(XIDcf(1),'userdata',number);
  sd=get(hconf,'userdata');
  set(XIDcf(3),'string',num2str(sd));
  iduiconf('2');
elseif strcmp(arg,'l1cb')
    val=get(XIDcf(2),'value');
    set(XIDcf(3),'string',num2str(val))
    numv=erf(val/sqrt(2));
    if numv<0.999 
        str=num2str(numv);
    else 
        str=['1-',num2str(erfc(val/sqrt(2)))];
    end
    set(XIDcf(4),'string',str);

elseif strcmp(arg,'2')
    strr=get(XIDcf(3),'string');
    if iduiconf('test',strr),return,end
    val=eval(strr);
    numv=erf(val/sqrt(2));
    if numv<0.999 
            str=num2str(numv);
    else 
            str=['1-',num2str(erfc(val/sqrt(2)))];
    end
    set(XIDcf(4),'string',str);

    set(XIDcf(2),'value',val)

elseif strcmp(arg,'3')
    strr=get(XIDcf(4),'string');
    if iduiconf('test',strr),return,end
    val1=eval(strr);
    val=erfinv(val1)*sqrt(2);
    set(XIDcf(2),'value',val)
    set(XIDcf(3),'string',num2str(val))
elseif strcmp(arg,'apply')
   number=get(XIDcf(1),'userdata');
   val=get(XIDcf(4),'string');
   ssd=get(XIDcf(3),'string');
   if iduiconf('test',val)|iduiconf('test',ssd),return,end
   nval=eval(val);strval=[num2str(nval*100),'%'];sd=eval(ssd);
   hconf=findobj(XIDplotw(number,1),'tag','confonoff');
   ch=get(hconf,'checked');
   set(hconf,'Label',menulabel(['Show ',strval,' confidence &intervals']),'userdata',sd);
   iduiclpw(number);
   iduistat(['Confidence interval changed to ',strval '.'])
elseif strcmp(arg,'test')
   err=0;
   eval('val=eval(strr);','err=1;')
   msg=str2mat('The entry must be a positive real number.');
   if err, errordlg(msg);return,end  
   if length(val)>1|length(val)<1,errordlg(msg);return,end
   if val<0|isstr(val),errordlg(msg);return,end
end
