function buttonsaveoptions(hObject, eventdata, Empty_Button, Buttons, lcs, basichi, basicfsz, sty, calclib, version, RegSlider, RegText, usmn, usmf)

data = get(Empty_Button,'UserData');

data.islive = 0;
set(usmn,'Value',0);
set(usmf,'Value',1);
SliderOnOffPGA(RegSlider, RegText, data, 0);
set(Empty_Button,'UserData',data);

isaccepted = 1;
iscurrent = 1;
if (isfield(data, 'isSOL')*isfield(data,'noc') == 0) || (data.isSOL == 0) || (data.noc < 1)
    isaccepted = 0;
end
if (isfield(data,'issol') == 0) || (data.issol == 0)
    iscurrent = 0;
end
if isaccepted+iscurrent == 0
    return
end

fig = figure;
pause(.1);

scrsz = get(0,'ScreenSize');

set(fig,'Position',[1/10*scrsz(3:4) 2/3*scrsz(3:4)]);
pause(.1);

set(fig,'Name','Choose spectra to be saved','NumberTitle','off','MenuBar','none','Toolbar','none','color','white');

AxesACC = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.05, 0.375, 0.6, 0.575]);
plot(AxesACC,1,0,'w');


Textfield(1) = uicontrol('Style','text','String','Name:','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.67, 0.15, 0.05, 0.05]);

Textfield(2) = uicontrol('Style','edit','String','','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.72, 0.15, 0.27, 0.05]);

Textfield(3) = uicontrol('Style','text','String','','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.67, 0.2, 0.32, 0.05]);

lgntxt = {};
oldtxt = {};
Ages = [];
for i=1:data.noc
    Ages = [Ages; data.SOL(i).a];
end
for i=1:data.noc
    if data.SOL(i).xbd(1) == data.SOL(i).xbd(2)
        oldtxt{i} = num2str(round(data.SOL(i).xbd(1)));
    else
%         oldtxt{i} = [num2str(round(data.SOL(i).xbd(1))),' - ',num2str(round(data.SOL(i).xbd(2)))];
        [tey,idmax] = max((i-1)*data.step+data.SOL(i).a(data.SOL(i).ifir:data.SOL(i).ilas));
        tex = data.x(data.SOL(i).ifir+idmax)+(max(data.x)-min(data.x))/100;
        oldtxt{i} = num2str(round(tex));
    end
    lgntxt{i} = oldtxt{i};
end


box = [];
i = 0;
for i=1:data.noc
%     TextOld(i) = uicontrol('Style','text','String',['(',num2str(i),') ',oldtxt{i}],'HorizontalAlignment','left','Fontsize',1.5*basicfsz,'BackgroundColor','white',...  [0.695, 0.818-(data.noc-1-i)*0.03, 0.08, 1.75*basichi]
    TextOld(i) = uicontrol('Style','text','String',oldtxt{i},'HorizontalAlignment','left','Fontsize',1.75*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.695, 0.847-(data.noc-1-i)*0.03, 0, 0]);  
    set(TextOld(i),'Position',get(TextOld(i),'Position')+get(TextOld(i),'Extent'))
    
    box(i) = uicontrol('Style','checkbox','String','','Units','normalized','BackgroundColor','white',...
        'Position',[0.795, 0.835-(data.noc-1-i)*0.03, 0.03, 1.75*basichi],'Fontsize',1.8*basicfsz,'FontWeight','bold','FontName',sty,...
        'HandleVisibility','off','Value',1);
end


Button(1) = uicontrol('Style','pushbutton','UserData',0,'String','Cancel','HorizontalAlignment','center','Fontsize',1.5*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.65, 0.02, 0.11, 0.1],...
    'Callback',{@innerbuttoncancel, fig});

Button(2) = uicontrol('Style','pushbutton','UserData',0,'String','Add spectrum to library','HorizontalAlignment','center','Fontsize',1.5*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.765, 0.02, 0.11, 0.1],...
    'Callback',{@innerbuttonadd, Empty_Button, box, Textfield, fig, calclib, version});
%     'Callback',{@buttonexport2libPGA, Empty_Button, Buttons, lcs, basichi, basicfsz, sty, calclib, version, RegSlider, RegText, usmn, usmf});

Button(3) = uicontrol('Style','pushbutton','UserData',0,'String','Save as .mat-file','HorizontalAlignment','center','Fontsize',1.5*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.88, 0.02, 0.11, 0.1],...
    'Callback',{@buttonsavePGA, Empty_Button, Button, 3, RegSlider, RegText, usmn, usmf, version}); 



subploPGA(hObject, eventdata, AxesACC, data, lgntxt, lcs, box, Button);

set(box,'Callback',{@subploPGA, AxesACC, data, lgntxt, lcs, box, Button});

function subploPGA(hObject, eventdata, AxesACC, data, lgntxt, lcs, box, Button)
lgnSOL = {};
lgnREJ = {};
i1 = 0;
i2 = 0;
for i=1:data.noc
    if get(box(i),'Value')
        plot(AxesACC, data.x, i1*data.step+data.SOL(i).a, 'color', lcs(mod(i-1,size(lcs,1))+1,:), 'Linewidth',1);
        hold(AxesACC,'on');
        plot(AxesACC, data.x([1 end]), (i1)*data.step*[1 1], 'k:','Linewidth',0.5);
        if data.SOL(i).xbd(1) < data.SOL(i).xbd(2)
            plot(AxesACC, data.x(data.SOL(i).ifir:data.SOL(i).ilas), i1*data.step+data.SOL(i).a(data.SOL(i).ifir:data.SOL(i).ilas), 'color', lcs(mod(i-1,size(lcs,1))+1,:),'Linewidth',5,'markersize',12);
            [tey,idmax] = max(i1*data.step+data.SOL(i).a(data.SOL(i).ifir:data.SOL(i).ilas));
            tex = data.x(data.SOL(i).ifir+idmax)+(max(data.x)-min(data.x))/100;
            lgntxt{i} = num2str(round(tex));
        else
            plot(AxesACC, data.x(data.SOL(i).ifir), i1*data.step+data.SOL(i).a(data.SOL(i).ifir),'*', 'color', lcs(mod(i-1,size(lcs,1))+1,:),'Linewidth',4,'markersize',12);
            lgntxt{i} = num2str(round(data.SOL(i).xbd(1)));

            if abs(data.x(data.SOL(i).ifir)-min(data.x))*0.1 < abs(data.x(data.SOL(i).ifir)-max(data.x))*0.9
                tex = data.x(data.SOL(i).ifir)+(max(data.x)-min(data.x))/100;
            else
                tex = data.x(data.SOL(i).ifir)-(max(data.x)-min(data.x))/25;
            end
            tey = i1*data.step+data.SOL(i).a(data.SOL(i).ifir);
        end
        text(AxesACC,tex,tey,lgntxt{i});
        i1 = i1+1;
    end
end
hold(AxesACC,'off');
if i1 == 0
    set(Button(2),'Enable','off');
    set(Button(3),'Enable','off');
    plot(AxesACC,data.x([1 end]),0*[1,1],'w');
elseif i1 == 1
    set(Button(2),'Enable','on');
    set(Button(3),'Enable','on');
else
    set(Button(2),'Enable','off');
    set(Button(3),'Enable','on');
end

axis(AxesACC,'tight');
yl = get(AxesACC,'ylim');
title(AxesACC,'Spectra to be saved');
xlabel(AxesACC,'channel');
axis(AxesACC,'tight');
set(AxesACC,'Fontsize',12,'ylim',yl + [0 0.1],'ytick',[]);

function innerbuttonadd(hobject, eventdata, Empty_Button, box, Textfield, parentfig, calclib, version)
data = get(Empty_Button,'UserData');
name = get(Textfield(2), 'String');
if strcmp(name,'') == 1
%     set(Textfield(2), 'Backgroundcolor','red');
    set(Textfield(3), 'String' ,'Export not successful: Please enter a NAME','Foregroundcolor','red');
    return
end

load([calclib,'/FACPACKlibrary.mat']);
no = length(lib);
for i=1:no
    if strcmp(lib(i).name, name) == 1
        set(Textfield(3), 'String' ,'Export not successful: NAME exists already in the library','Foregroundcolor','red');
        return
    end
end
% if (isfield(data,'issol') == 1) && (data.issol == 1)
%     if get(popup(1),'Value') == 1
%         a = data.a;
%     else
%         a = data.SOL(get(popup(1),'Value')-1).a;
%     end
% else
%     a = data.SOL(get(popup(1),'Value')).a;
% end

for i = 1:data.noc
    if get(box(i),'Value')
        a = data.SOL(i).a;
    end
end

cln = clock;
cln = [num2str(cln(4),'%2.2d'), ':', num2str(cln(5),'%2.2d'), ':', num2str(round(cln(6)),'%2.2d')];

lib(no+1).name = name;
lib(no+1).x = data.x;
lib(no+1).a = a;
lib(no+1).date = date;
lib(no+1).clock = cln;
lib(no+1).module = 'PGA';
lib(no+1).dataset = [data.PathName, data.FileName];
lib(no+1).version = version;

set(Textfield(3), 'String' ,'Export successful','Foregroundcolor','black');

save([calclib,'/FACPACKlibrary.mat'],'lib');
close(parentfig);

function innerbuttoncancel(hobject, eventdata, parentfig)
close(parentfig);
