function SaveLoad(varargin)

name=[];
if(nargin==1)
   action=varargin{1};
elseif(nargin==2);
   name=varargin{1};
   action=varargin{2};
elseif(nargin==3)
   name=varargin{1};
   action=varargin{2};
   type=varargin{3};
end

if(~isempty(name))
   if(length(name)==2 & isletter(name(1)) & name(2)==':')
      name(end+1)='\';
   end
end

saveload_handle=findobj('Tag','Fig_saveload');
files_handle=findobj(saveload_handle,'Tag','Listbox_files');
directory_handle=findobj(saveload_handle,'Tag','PopupMenu_directory');
file_handle=findobj(saveload_handle,'Tag','EditText_file');
curdir_handle=findobj(saveload_handle,'Tag','EditText_curdir');
title_handle=findobj(saveload_handle,'Tag','StaticText_title');

switch action
    
case 'DISPLAY_FILE'
    files=get(files_handle,'String');
    pos=get(files_handle,'Value');
    set(file_handle,'String',files{pos});
   
case 'INITIALIZE'
   set(gcf,'UserData',{name,name});
   if(strcmp(type,'SAVE'))
      set(title_handle,'String','SAVING DATA');
   elseif(strcmp(type,'LOAD'))
      set(title_handle,'String','LOADING DATA');
   end
         
case 'CHANGEDIR'
   directories = get(directory_handle,'String');
   name = directories{get(directory_handle,'Value')};
   set(file_handle,'String','');
   datadir=get(saveload_handle,'UserData');
	cd([datadir{1},'\',name]);
   curdir=cd;
   if(curdir(end)=='\')
      datadir{1}=curdir(1:end-1);
      directories=ConvertToString(dir([curdir,'*.']));
      files=ConvertToString(dir([curdir,'*.mat']));
   else
      datadir{1}=curdir;
      directories=ConvertToString(dir([curdir,'\*.']));
      files=ConvertToString(dir([curdir,'\*.mat']));
   end
   set(files_handle,'Value',1);
   set(files_handle,'String',files);
   set(directory_handle,'Value',1);
   set(directory_handle,'String',directories);
   set(curdir_handle,'String',curdir);
   cd(datadir{2});
   set(saveload_handle,'UserData',datadir);
   
case 'MANUALCHANGEDIR'
   name = get(curdir_handle,'String');
   if(exist(name,'dir'))
      set(file_handle,'String','');
      datadir=get(gcf,'UserData');
      cd(name);
      curdir=cd;
      if(curdir(end)=='\')
         datadir{1}=curdir(1:end-1);
         directories=ConvertToString(dir([curdir,'*.']));
         files=ConvertToString(dir([curdir,'*.mat']));
      else
         datadir{1}=curdir;
         directories=ConvertToString(dir([curdir,'\*.']));
         files=ConvertToString(dir([curdir,'\*.mat']));
      end
      set(files_handle,'Value',1);
      set(files_handle,'String',files);
      set(directory_handle,'Value',1);
      set(directory_handle,'String',directories);
      set(curdir_handle,'String',curdir);
      cd(datadir{2});
      set(gcf,'UserData',datadir);
   end
   
case 'FILE'
   set(file_handle,'String',name);
   
case 'OK'
   title_handle = findobj(saveload_handle,'Tag','StaticText_title');
   msg = get(title_handle,'String');
   
   switch msg
      
   case 'SAVING DATA'
      name=get(file_handle,'String');
      datadir=get(gcf,'UserData');
      file=[datadir{1},'\',name];
      Save_Population_Data(file);
      close(saveload_handle);
      
   case 'LOADING DATA'
      name=get(file_handle,'String');
      datadir=get(gcf,'UserData');
      file=[datadir{1},'\',name];
      Load_Population_Data(file);
      close(saveload_handle);
   end
   
end

%---------------------------------------------------------------
function str=ConvertToString(field)

L=length(field);
if(L==0)
   str={};
else
   for i=1:L
	   str(i)={getfield(field(i),'name')};
   end
end