function innerbuttonexport2libPGA(hObject, eventdata, Empty_Button, boxcheck, basicfsz, sty, calclib, version)
data = get(Empty_Button,'UserData');

fig = figure;
pause(.1);

scrsz = get(0,'ScreenSize');

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

set(fig,'Name','Export a computed spectrum to the library','NumberTitle','off','MenuBar','none','Toolbar','none','color','white');

Textfield(1) = uicontrol('Style','text','String','library files:','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.05, 0.75, 0.25, 0.15]);
% set(Textfield(1),'Visible','off');

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

Textfield(3) = uicontrol('Style','edit','String','','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.35, 0.3, 0.64, 0.1]);

Textfield(4) = uicontrol('Style','text','String','','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'FontWeight','bold','Position',[0, 0.25, 1, 0.2]);
set(Textfield(4),'Visible','off');


load([calclib,'/FACPACKlibrary.mat']);
for i =1:length(lib)
    dat(i) = lib(i).name+".mat";
end

popup(1) = uicontrol('Style','listbox','String',dat,'HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.35, 0.5, 0.64, 0.45]);

Button(1) = uicontrol('Style','pushbutton','UserData',0,'String','Add to library','HorizontalAlignment','center','Fontsize',1.5*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.51, 0.04, 0.48, 0.2],...
    'Callback',{@innerbuttonadd, Empty_Button, boxcheck, Textfield, fig, calclib, version});

Button(2) = uicontrol('Style','pushbutton','UserData',0,'String','Cancel','HorizontalAlignment','center','Fontsize',1.5*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.04, 0.48, 0.2],...
    'Callback',{@innerbuttoncancel, fig});



function innerbuttonadd(hobject, eventdata, Empty_Button, box, Textfield, parentfig, calclib, version)
data = get(Empty_Button,'UserData');
name = get(Textfield(3), 'String');
if strcmp(name,'') == 1
    set(Textfield(4), '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(4), 'String' ,'Export not successful: NAME exists already in the library','Foregroundcolor','red');
        return
    end
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(4), 'String' ,'Export successful','Foregroundcolor','black');

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


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