<?xml version="1.0" encoding="UTF-8"?>
<component name="LoginTask" extends="Task" >
	
	<script type="text/brightscript" uri="pkg:/components/generalUtils.brs"/>
  	<script type="text/brightscript">
		<![CDATA[
		sub init()
		    m.top.functionName = "ValidateLogin"
		end sub
		
		sub ValidateLogin()
		  'print "in ValidateLogin"
		  user = m.top.user
		  pass = m.top.pass
		  sn = GetDeviceESN()

	      url = m.global.serverURL + "player_api.php?username=" + user.Escape() + "&password=" + pass.Escape()
    print "Url comprueba user = "; url
	      data = requestData(url)
		  json = ParseJSON(data)
		
		  if json <> invalid then

            if json.user_info.auth then
                saveStrValToReg("user_tv", json.user_info.username, "datos_tv")
                saveStrValToReg("pass_tv", json.user_info.password, "datos_tv")    
                m.global.user = json.user_info.username
                m.global.pass = json.user_info.password
                m.global.expire = json.user_info.exp_date
                m.top.validated = true
            else
                m.top.validated = false
            end if
			
		  end if
		end sub
		]]>
	</script>
  	<interface>
	  	<field id="user" type="string"/>
	    <field id="pass" type="string"/>
	    <field id="validated" type="boolean"/>
  	</interface>
</component>
