function gui_Iexstim(varargin)
global zeng zeng2
action = varargin{1};
switch action
case 'initial'
temp=findobj('type','figure','tag','gui_Iexstim');
if isempty(zeng.Iexstim)
    zexst('err','Your work has no extracellular stimulator')
elseif ~isempty(temp)
    PointerLocation=get(0,'PointerLocation');
    FG_position=get(temp,'position');
    FG_Size=[PointerLocation(1) 0 FG_position(3) FG_position(4)];
    FG_Size(2)=PointerLocation(2)-FG_Size(4)-10;
    set(temp,'position',FG_Size)
    figure(temp)
else
    if zeng2.options.vestim==1 %calculated
        xyzenable='on';
    elseif zeng2.options.vestim==2 %imported
        xyzenable='off';
    else
        zexst('err',['Error in GUI_Iexstim']);
        return
    end
    stim_number=length(zeng.Iexstim);
    PointerLocation=get(0,'PointerLocation');
    FG_Size=[PointerLocation(1) 0 368 110+(stim_number-1)*30];
    FG_Size(2)=PointerLocation(2)-FG_Size(4)-10;
    if nargin==2
        FG_Size(1:2)=varargin{2}(1:2);
    end
    Iexstim.parent=gcbf;
    Iexstim.figure = figure('Color',[0.8 0.8 0.8], ...
        'MenuBar','none',...
        'Name','Setting:Extracellular Stimulation',...
        'NumberTitle','off',...
    	'Position',FG_Size, ...
        'Resize','off',...
        'tag','gui_Iexstim',...
    	'ToolBar','none');
    set(Iexstim.figure,'position',FG_Size);
    FG_Backgournd=get(Iexstim.figure,'Color');
    uicontrol('Parent',Iexstim.figure, ...
	'BackgroundColor',[0.85 0.8 0.75], ...
    'callback','gui_Iexstim(''okay'')',...
	'ListboxTop',0, ...
	'Position',[59 10 50 22], ...
	'String','Ok', ...
	'Tag','Pushbutton');
    uicontrol('Parent',Iexstim.figure, ...
	'BackgroundColor',[0.85 0.8 0.75], ...
    'callback','delete(gcbf)',...
	'ListboxTop',0, ...
	'Position',[159 10 50 22], ...
	'String','Close', ...
	'Tag','Pushbutton');
    uicontrol('Parent',Iexstim.figure, ...
	'BackgroundColor',[0.85 0.8 0.75], ...
    'callback','gui_Iexstim(''okay'',''apply'')',...
	'ListboxTop',0, ...
	'Position',[259 10 50 22], ...
	'String','Apply', ...
	'Tag','Pushbutton');
    uicontrol('Parent',Iexstim.figure, ...
	'BackgroundColor',[0.85 0.8 0.75], ...
    'callback','gui_Iexstim(''add'')',...
	'ListboxTop',0, ...
	'Position',[330 10 15 15], ...
	'String','+', ...
	'Tag','Pushbutton');
    uicontrol('Parent',Iexstim.figure, ...
	'BackgroundColor',[0.85 0.8 0.75], ...
    'callback','gui_Iexstim(''substract'')',...
	'ListboxTop',0, ...
	'Position',[345 10 15 15], ...
	'String','-', ...
	'Tag','Pushbutton');


    uicontrol('Parent',Iexstim.figure, ...
	'BackgroundColor',FG_Backgournd, ...
	'HorizontalAlignment','left', ...
	'ListboxTop',0, ...
	'Position',[11 FG_Size(4)-30 400 18], ...
	'String','   X(um)            Y(um)           Z(um)       Amp(uA)      Dur(us)     Delay(us)', ...
	'Style','text', ...
	'Tag','StaticText1');        

    for i=1:stim_number
        Iexstim.x(i) = uicontrol('Parent',Iexstim.figure, ...
    	'BackgroundColor',[1 1 1], ...
    	'ListboxTop',0, ...
    	'Position',[5 50+(i-1)*30 55 26], ...
        'string',num2str(zeng.Iexstim(i).xyz(1)),...
    	'Style','edit', ...
        'enable',xyzenable,...
    	'Tag','xyz');
        Iexstim.y(i) = uicontrol('Parent',Iexstim.figure, ...
    	'BackgroundColor',[1 1 1], ...
    	'ListboxTop',0, ...
    	'Position',[65 50+(i-1)*30 55 26], ...
        'string',num2str(zeng.Iexstim(i).xyz(2)),...
    	'Style','edit', ...
        'enable',xyzenable,...
    	'Tag','xyz');
        Iexstim.z(i) = uicontrol('Parent',Iexstim.figure, ...
    	'BackgroundColor',[1 1 1], ...
    	'ListboxTop',0, ...
    	'Position',[125 50+(i-1)*30 55 26], ...
        'string',num2str(zeng.Iexstim(i).xyz(3)),...
    	'Style','edit', ...
        'enable',xyzenable,...
    	'Tag','xyz');
        Iexstim.amp(i) = uicontrol('Parent',Iexstim.figure, ...
    	'BackgroundColor',[1 1 1], ...
    	'ListboxTop',0, ...
        'string',num2str(zeng.Iexstim(i).amp),...
    	'Position',[185 50+(i-1)*30 58 26], ...
    	'Style','edit', ...
    	'Tag','EditText1');
        Iexstim.dur(i) = uicontrol('Parent',Iexstim.figure, ...
    	'BackgroundColor',[1 1 1], ...
    	'ListboxTop',0, ...
    	'Position',[250 50+(i-1)*30 58 26], ...
        'string',num2str(zeng.Iexstim(i).dur*1000),...
    	'Style','edit', ...
    	'Tag','EditText1');
        Iexstim.delay(i) = uicontrol('Parent',Iexstim.figure, ...
    	'BackgroundColor',[1 1 1], ...
    	'ListboxTop',0, ...
    	'Position',[315 50+(i-1)*30 48 26], ...
        'string',num2str(zeng.Iexstim(i).delay*1000),...
    	'Style','edit', ...
    	'Tag','EditText1');
        if ~isnumeric(zeng.Iexstim(i).amp)
            set([Iexstim.amp(i) Iexstim.dur(i) Iexstim.delay(i)],'enable','off')
        end
    end
    set(Iexstim.figure,'userdata',Iexstim)
end %if isempty(zeng.Iexstim)

case 'add'
    feval('Iexstim',[0 0 0],1,1,1)                         % [xyz(um),amp(uA),dur(us),delay(us)]
    temp=get(gcf,'position');
    delete(findobj('type','figure','tag','gui_Iexstim'));
    gui_Iexstim('initial',temp(1:2))
case 'substract'
    global zeng
    Iexstim_length=length(zeng.Iexstim);
    if Iexstim_length>1
        zeng.Iexstim(Iexstim_length)=[];
        temp=get(gcf,'position');
        delete(findobj('type','figure','tag','gui_Iexstim'));
        gui_Iexstim('initial',temp(1:2))
    end
case 'okay'
    Iexstim=get(gcbf,'userdata');
    Temp_Iexstim=zeng.Iexstim;
    for i=1:length(Iexstim.amp)
        if zeng2.options.vestim==1 %'calculated')
            temp=str2num(get(Iexstim.x(i),'string'));
            if isempty(temp)| ~isreal(temp)
                zexst('err',['Position of Iexstim.X(' num2str(i) ') has to be a number!!!, You know?']);
                return
            end
            Temp_Iexstim(i).xyz(1)=temp;

            temp=str2num(get(Iexstim.y(i),'string'));
            if isempty(temp)| ~isreal(temp)
                zexst('err',['Position of Iexstim.Y(' num2str(i) ') has to be a number!!!, You know?']);
                return
            end
            Temp_Iexstim(i).xyz(2)=temp;
            
            temp=str2num(get(Iexstim.z(i),'string'));
            if isempty(temp)| ~isreal(temp)
                zexst('err',['Position of Iexstim.Z(' num2str(i) ') has to be a number!!!, You know?']);
                return
            end
            Temp_Iexstim(i).xyz(3)=temp;
        end
        

        if strcmp('off',get(Iexstim.amp(i),'enable'))
%            Temp_Iexstim(i).amp    =get(Iexstim.amp(i),'string');
%            Temp_Iexstim(i).dur    =0;         
%            Temp_Iexstim(i).delay  =0;
        else
            temp=str2num(get(Iexstim.amp(i),'string'));
            if isempty(temp)| ~isreal(temp)
                zexst('err',['Amplitude of Iexstim(' num2str(i) ') has to be a number !!!, You know?']);
                return
            end
            Temp_Iexstim(i).amp    =temp;
       
            temp=str2num(get(Iexstim.dur(i),'string'))/1000;
            if isempty(temp)| ~isreal(temp) | temp<=0
                zexst('err',['Duration of Iexstim(' num2str(i) ') has to be a positive number !!!, You know?']);
                return
            end
            Temp_Iexstim(i).dur    =temp;
            
            temp  =str2num(get(Iexstim.delay(i),'string'))/1000;
            if isempty(temp)| ~isreal(temp) | temp<0
                zexst('err',['Delay of Iexstim(' num2str(i) ') has to be a positive number or zero !!!, You know?']);
                return
            end
            Temp_Iexstim(i).delay=temp;
        end
    end %for i=1:length(Iexstim.amp)
    zeng.Iexstim=Temp_Iexstim;
    if nargin==1
        %from okay
    else
        %from apply
        zexst('calculate')
    end
end
