--get adress
local InetcomLogin = ''
local InetcomPass  = ''

function InetcomTryLogin(LoginAnyway)

 if m_simpleTV.user==nil then m_simpleTV.user={} end
 if m_simpleTV.user.inetcom==nil then m_simpleTV.user.inetcom={} end

 if m_simpleTV.user.inetcom.session==nil or LoginAnyway==true then
   if m_simpleTV.user.inetcom.session ~= nil then m_simpleTV.WinInet.Close(m_simpleTV.user.inetcom.session) end
   
   m_simpleTV.user.inetcom.session = m_simpleTV.WinInet.New("Opera/9.80 (Windows NT 6.1; U; ru) Presto/2.9.168 Version/11.52")
   if m_simpleTV.user.inetcom.session == nil then return false end
   
 end
 
   if InetcomLogin == '' or InetcomPass  == '' then return true end
 
local header = 'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\nReferer: http://inetcom.tv/auth/register\nConnection: Keep-Alive\nAccept: application/json, text/javascript, */*; q=0.01\nX-Requested-With: XMLHttpRequest\nContent-Type: application/x-www-form-urlencoded'
local body ='LoginForm[username]='.. InetcomLogin .. '&LoginForm[password]=' .. InetcomPass

local rc,asnwer=m_simpleTV.WinInet.Post(m_simpleTV.user.inetcom.session,'http://inetcom.tv/auth/login',header,body)

if rc~=200 then 
  InetcomError("Can't login")
  return false
end

--{"result":false,"errorCode":1}'
if string.match( answer, '"result":true' ) then return true end

 InetcomError("Can't login")
 return false
end
------------------------------------------------------------------------------------------------------------
function InetcomError(Message)
 
 m_simpleTV.OSD.ShowMessage('Inetcom ошибка - ' .. Message,0xFF,5)
 if m_simpleTV.user.inetcom.session ~= nil then 
    m_simpleTV.WinInet.Close(m_simpleTV.user.inetcom.session) 
	m_simpleTV.user.inetcom.session=nil	
 end
 
end
------------------------------------------------------------------------------------------------------------
function InetcomGetGetUrl(PageAddress)

 local rc,answer = m_simpleTV.WinInet.Get(m_simpleTV.user.inetcom.session,PageAddress)
 if rc ~= 200 then return nil end

 --debug_in_file(answer)

 local retAdr = findpattern(answer,"lnks = %[%'http(.-)%'%]",1,9,2)
 if retAdr == nil then return nil end
 
 return retAdr
end
------------------------------------------------------------------------------------------------------------
if m_simpleTV.Control.ChangeAdress ~= 'No' then return end

local inAdr =  m_simpleTV.Control.CurrentAdress

if inAdr==nil then return end
 
  
if (not string.match( inAdr, '^http://inetcom%.tv/channel' )) and (not string.match( inAdr, 'InetcomFreeSid')) then return end

m_simpleTV.Control.ChangeAdress='Yes'
m_simpleTV.Control.CurrentAdress =  ' '

if InetcomTryLogin(false) ~= true then return end

 if string.match( inAdr, 'InetcomFreeSid') then 
    local sid = InetcomGetGetUrl('http://inetcom.tv/channel/russia_1')  
	if sid == nil then return end
		
	sid = findpattern(sid,'sid=(.+)',1,4,0)
	
	if sid == nil then return end
	local retAdr = string.gsub(inAdr,'InetcomFreeSid',sid)
		
	m_simpleTV.OSD.ShowMessage(retAdr,0x00FF00,2)
    m_simpleTV.Control.CurrentAdress =  retAdr
	return
 end
 
 local retAdr = InetcomGetGetUrl(inAdr)  
 if retAdr == nil then return end

 m_simpleTV.OSD.ShowMessage(retAdr,0x00FF00,2)
 m_simpleTV.Control.CurrentAdress =  retAdr
 

 
 
 