--dofile(_SourceFolder.."\\AutoPlay\\Scripts\\SaveP.lua");
--[[ *** *** *** *** *** *** *** *** *** *** *** *** *** ]]--
--[[ *** This code contains a very important fnction *** ]]--
--[[ *** that is used in PlayMeNow. Do not touch!    *** ]]--
--[[ *** If you are really good in AMS & LUA then    *** ]]--
--[[ *** you can make any changes, but be careful!   *** ]]--
--[[ *** *** *** *** *** *** *** *** *** *** *** *** *** ]]--

-- ask for the folder
PlayListFolder = Dialog.FolderBrowse("Please select the folder where your M3U playlist will be saved", _DesktopFolder);
if PlayListFolder ~= "CANCEL" then
	FolderFound = Folder.DoesExist(PlayListFolder);
	PlayList = nil;
	Files = ListBox.GetCount("ListBox1");
	if Files == 0 then
		-- say if the playlist is empty
		Dialog.Message("Notice", "The playlist is empty.", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
	else
		-- ask for the file name
		PlayList = Dialog.Input("Please enter the file name.", "File Name:", "PMN_playlist", MB_ICONQUESTION);
		PlayListFound = File.DoesExist(PlayListFolder.."\\"..PlayList..".m3u");
		if PlayListFound == true then
			-- say if the playlist can be overwritten
			proceed = Dialog.Message("Notice", "The playlist is not empty. Do you want to overwrite?", MB_YESNO, MB_ICONINFORMATION, MB_DEFBUTTON1);
			if proceed == IDYES then
				Out = nil;
				TextFile.WriteFromString(PlayListFolder.."\\"..PlayList..".m3u", Out, false);
				for i=1,Files do
					Data = ListBox.GetItemData("ListBox1", i);
					Out = Data.."\r\n";
					TextFile.WriteFromString(PlayListFolder.."\\"..PlayList..".m3u", Out, true);
				end
				elseif proceed == IDNO then
				PlayList = nil;
				-- ask if save with another name or no
				note = Dialog.Message("Notice", "Do you want to proceed with another file name?", MB_YESNO, MB_ICONINFORMATION, MB_DEFBUTTON1);
				if note == IDYES then
					-- say if the file already exists
					PlayList = Dialog.Input("File exists", "Please choose another file name :", nil, MB_ICONQUESTION);
					for i=1,Files do
						Data = ListBox.GetItemData("ListBox1", i);
						Out = Data.."\r\n";
						TextFile.WriteFromString(PlayListFolder.."\\"..PlayList..".m3u", Out, true);
		end	end	end
			
		else
			for i=1,Files do
				Data = ListBox.GetItemData("ListBox1", i);
				Out = Data.."\r\n";
				TextFile.WriteFromString(PlayListFolder.."\\"..PlayList..".m3u", Out, true);			
end	end	end	end

