function OnNavigateComplete(Object) 
  m_simpleTV.Config.AddEventHandler(Object,'OnClick','btnok','OnClickLoadPls')
end
-----------------------------------------------------------------
function OnClickLoadPls(Object)

m_simpleTV.Config.SetElementText(Object, 'Mess', '')
local login = m_simpleTV.Config.GetElementValueString_UTF8(Object,'login')
local pass = m_simpleTV.Config.GetElementValueString_UTF8(Object,'pass')

if login == nil or pass == nil or login == '' or pass == '' then 
  m_simpleTV.Config.SetElementText(Object, 'Mess', 'Введите логин и пароль')
         return 
end
----------------------------------------------------------------
local session = m_simpleTV.WinInet.New("Opera/9.80 (Windows NT 6.1; U; ru) Presto/2.9.168 Version/11.52")
if session == nil then return end

 --INTERNET_OPTION_CONNECT_TIMEOUT
 m_simpleTV.WinInet.SetOptionInt(session,2,3000,0)
 --INTERNET_OPTION_RECEIVE_TIMEOUT
 m_simpleTV.WinInet.SetOptionInt(session,6,5000,0)
--INTERNET_FLAG_SECURE
m_simpleTV.WinInet.SetOpenRequestFlags(session, 0x00800000 + 0x00002000 + 0x00001000)
---------------------------------------------------------------
function trim(str)
str = string.match(str,'^%s*(.-)%s*$')
  return str
end
-----------------
 local fhandle = io.open(m_simpleTV.MainScriptDir .. "user/tvplaylists/misc/filter.default")
 if fhandle == nil then return end
 local value = fhandle:read("*a")
 fhandle:close()
-------------------------------------------------------
local function String2Upper(s)
  for i = 224, 255 do
    s = s:gsub(string.char(i), string.char(i - 32))
  end
  s = s:gsub(string.char(184), string.char(168))
  return s:upper()
end
-------------------------------------------------------
local function GetFilterTable(value)
     local FilterTable = {}
     local i=1
     for w in string.gmatch(value,'"(.-)"') do 
         if w == nil then break end
         w = trim(w)
         local CurrenName,NewName,GroupName,EpgSource = string.match(w,'(.-),(.-),(.-),(.-)$')
         if CurrenName == nil or NewName == nil or GroupName == nil or EpgSource == nil then break end
   --debug_in_file(CurrenName .. ' ,' .. NewName .. ' ,' .. GroupName .. ' ,' .. EpgSource .. '\n')
   
         FilterTable[i] = {}
         FilterTable[i][1] = CurrenName
         FilterTable[i][2] = NewName
         FilterTable[i][3] = GroupName
         FilterTable[i][4] = EpgSource
     
        i=i+1
     end
  return FilterTable
end

-----------------------------------------------------------------
local function FilterChannels(name,filter)

if filter == nil then filter = GetFilterTable(value) end

if filter == nil then return nil end

local group=''
local epgSource =''

name = m_simpleTV.Common.string_fromUTF8(name)

local upname = String2Upper(name)
upname = m_simpleTV.Common.string_toUTF8(upname)
--debug_in_file(upname .. '\n')

name = m_simpleTV.Common.string_toUTF8(name)

  for k,v in ipairs(filter) do
     if upname == v[1] then name = v[2] group = v[3] epgSource = v[4] end
  end
  return name,group,epgSource
end
-------------------------------------------------------

local body = 'email=' .. login .. '&password=' .. pass
local host = 'https://edem.tv/account/login'
local head = 
'Accept-Language:  ru-RU,ru;q=0.9,en;q=0.8\n' ..
'Accept:  text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1\n' ..
'Referer:' .. host .. '\n' ..
'Content-Type:	application/x-www-form-urlencoded' 

rc,answer=m_simpleTV.WinInet.Post(session,host,nil,head,body)

 if rc~=200 then
     m_simpleTV.WinInet.Close(session)
	  m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
	  return
 end

 answer = m_simpleTV.Common.string_fromUTF8(answer)
 --debug_in_file(answer ..  '\n')

if string.match(answer,'alert') then
  m_simpleTV.OSD.ShowMessage("Edem.tv - Неверный логин или пароль" ,255,10)
  m_simpleTV.Config.SetElementText(Object, 'Mess', 'Неверный логин или пароль')
         return 
end

m_simpleTV.Dialog.Close(Object)

if not string.match(answer,'/playlist') then
   m_simpleTV.OSD.ShowMessage("Edem.tv - ошибка скрипта" ,255,10)
  return
end

local url = 'https://edem.tv/playlist'

rc,answer=m_simpleTV.WinInet.Get(session,url)
 if rc~=200 then return end

local cockie = m_simpleTV.WinInet.GetCookies (session,url,'')
--debug_in_file(cockie ..  '\n')

 answer = m_simpleTV.Common.string_fromUTF8(answer)
--debug_in_file(answer ..  '\n')

answer = string.gsub(answer,'<!%-%-.-%-%->','')

answer = findpattern(answer,'</option>(.-)</select>',1,0,0)
if answer == nil then return end

--debug_in_file(answer ..  '\n')

 local t ={}
 local i=1
 local name,adr,sid
 
 for w in string.gmatch(answer,'<option(.-)option>') do
 
    adr,name= string.match(w,'value=(.-)>(.-)<')
    if name == nil or adr == nil then break end
 
      t[i] = {}
      t[i].Id   =  i
      t[i].Name =  name
      t[i].Adress  = adr
 --debug_in_file(t[i].Name .. '  ' .. t[i].Adress .. '\n\n')
 i=i+1
end

if i>1 then
    local ret,id = m_simpleTV.OSD.ShowSelect('Выберите сервер',0,t,10000,1+4+8)
	    if ret==1 then  
                sid = t[id].Adress
            end

local body = 'server_id=' .. sid
--debug_in_file(body)
local host = 'https://edem.tv/ajax/user_server'
local head = 

'Host: edem.tv\n' ..
'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
'Referer: https://edem.tv/playlist\n' ..
'Cookie: ' .. cockie .. '\n' ..
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8\n' ..
'Accept: */*\n' ..
'X-Requested-With: XMLHttpRequest\n' ..
'Content-Transfer-Encoding: binary'

rc,answer=m_simpleTV.WinInet.Post(session,host,nil,head,body)
if rc~=200 then return end
--debug_in_file(rc .. '  ' .. answer ..  '\n')

local head  = 
'Host: edem.tv\n' .. 
'Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1\n' ..
'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
'Referer: https://edem.tv/playlist\n' ..
'Cookie: ' .. cockie

url = 'https://edem.tv/account'

local rc,answer=m_simpleTV.WinInet.Get(session,url,nil,0,head)
if rc~=200 then return end

 answer = m_simpleTV.Common.string_fromUTF8(answer)
--debug_in_file(answer ..  '\n')
--do return end

local url  =  string.match(answer, 'https://edem.tv/playlists/uplist/.+/edem_pl.m3u8')
--debug_in_file(url)

if url == nil then
   m_simpleTV.OSD.ShowMessage("Edem.tv - плейлист не найден" ,255,10)
         return 
end

      local edem = m_simpleTV.MainScriptDir .. 'user\\tvplaylists\\edemtv\\edem.txt'
      local fhandle = io.open (edem , "w+")
      if fhandle == nil then return end
      fhandle:write(url)
      fhandle:close()

m_simpleTV.OSD.ShowMessage("Edem.tv - начало обновления" ,0xFF00,10)

rc,answer=m_simpleTV.WinInet.Get(session,url)
m_simpleTV.WinInet.Close(session)
 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
	   return
 end
-- answer = m_simpleTV.Common.string_fromUTF8(answer)

if not string.match(answer,'EXTM3U') then 
  m_simpleTV.OSD.ShowMessage("Edemm.tv - ошибка обновления",255,10)
 return 
end

local  tmpName = m_simpleTV.Common.GetTmpName()
if tmpName == nil then return end 

 local tfile = io.open(tmpName,'w+')
 if tfile == nil then 
     os.remove(tmpName)
     return
 end
 
 tfile:write('#EXTM3U\n')
 tfile:close() 

local title = m_simpleTV.Common.string_toUTF8('Плейлист Edem.tv')
local qst = m_simpleTV.Common.string_toUTF8('Конвертировать плейлист в rtmp?')
local rtmp = m_simpleTV.Interface.MessageBox(qst,title,1,0)

local m3ustr,name,grp,link
local svr,key,chid

 if rtmp == 1 then
  for w in string.gmatch(answer, '#EXTINF(.-)m3u8') do
	name = findpattern (w,',(.-)\n',1,1,1)
	grp = findpattern (w,'#EXTGRP(.-)\n',1,8,1)
         svr,key,chid = string.match(w,'http://(.-)/iptv/(.-)/(.-)/')
      if svr == nil or key == nil or chid == nil then link = findpattern (w,'http(.-)$',1,0,0) end
        link = 'rtmp://' .. svr ..'/iptv/' .. chid .. '?key=' .. key
      if name == nil or grp == nil or link == nil then break end
	grp = trim(grp)
	m3ustr = '#EXTINF:-1,' .. grp .. ':' .. name .. '\n' .. link .. '\n'
--debug_in_file(m3ustr)
 tfile = io.open(tmpName,'a+')
 tfile:write(m3ustr)
 tfile:close()
  end

   elseif rtmp ~=1 then

    for w in string.gmatch(answer, '#EXTINF(.-)m3u8') do
    	name = findpattern (w,',(.-)\n',1,1,1)
    	grp = findpattern (w,'#EXTGRP(.-)\n',1,8,1)
    	link = findpattern (w,'http(.-)$',1,0,0)
    	if name == nil or grp == nil or link == nil then break end
    	grp = trim(grp)
    	--grp = grp:gsub("^%l", string.upper)
    --debug_in_file(grp .. '\n')
    	m3ustr = '#EXTINF:-1,' .. grp .. ':' .. name .. '\n' .. link .. 'm3u8\n'
    --debug_in_file(m3ustr)
     tfile = io.open(tmpName,'a+')
     tfile:write(m3ustr)
     tfile:close()
    end
end
--do return end

   --опции  для загрузки плейлиста 
local p={}
p.TypeSourse = 1
p.DeleteBeforeLoad = 0
p.TypeSkip   = 1
p.TypeFind =   0
p.AutoSearch = 1
p.NumberM3U =  1
p.Find_Group = 1
p.TypeCoding = 1  --  -1-auto  0 - plane text  1- UTF8  2- unicode
p.BorpasFileFormat=0
p.AutoNumber = 0
p.ExtFilter = 'Edem.tv'
p.ExtFilterLogo =  '..\\Channel\\logo\\extFiltersLogo\\EdemTV.png'   -- the logo of extended filter (from ver. 0.4.8 b9)
p.ExtFilterLogoForce = 0


local err,add,ref,names = m_simpleTV.Common.LoadPlayList (tmpName,p,0,true,false)
os.remove(tmpName)

if err==true then
     local mess = "Edem.tv - каналы обновлены (" .. add .. ")"
	 if add > 0 and add < 25 then 
	    names = string.gsub(names,'%$end','\n')
		mess = mess .. '\n' .. names
	 end
	 
	 m_simpleTV.OSD.ShowMessage(mess,0xFF00,5)
	 
end


  end
 end

-----------------------------------------------------------------
