// Restricts input for the set of matched elements to the given inputFilter function.
(function($) {
  $.fn.inputFilter = function(inputFilter) {
    return this.on("input keydown keyup mousedown mouseup select contextmenu drop", function() {
      if (inputFilter(this.value)) {
        this.oldValue = this.value;
        this.oldSelectionStart = this.selectionStart;
        this.oldSelectionEnd = this.selectionEnd;
      } else if (this.hasOwnProperty("oldValue")) {
        this.value = this.oldValue;
        this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
      } else {
        this.value = "";
      }
    });
  };
}(jQuery));

jQuery(document).ready(function($) { 



if ( parseInt( $('#markerid').val() ) == 0 ) {
	/*
		setInterval(function() {
			$.post( "//ap.markergroup.info/cabinet_api.php", {
				do: 'api_check_qr', 
				zc: $('#qrsecret').val()
			}).done(function( data ) {	
				var j = jQuery.parseJSON( data );
				if ( j.status == '+' ) {
					//document.location.href = 'https://ap.markergroup.info/cabinet_inc.php?mode=api&marker_id='+j.uid+'&do=get';
					document.location.reload();
					return false;
					return false;
				}
			});
		}, 4000);
	*/
}	
		$(document).on("click", ".auth_step1", function(){
			$("#login .error").removeClass("error");
			if ( $('.phone input').val() == '' ) {
				$('.phone').addClass('error');return false;
			}
			if ( $('.cabi_password').val() == '' ) {
				$('.cabi_password').parent().addClass('error');return false;
			}
			if ( $('.cabi_password').val()!='' && $('.phone input').val()!='' ) {
			$.post( "//ap.markergroup.info/cabinet_api.php", {
				do: "api_login", 
				login:    $(".phone .actual span").text() + $('.phone input').val(),
				password: $(".cabi_password").val()
			}).done(function( data ) {
				var j = jQuery.parseJSON( data );
				if ( j.status == "+" ) {
					//$(".f2a .state").html( j.text );
					$(".f1a").hide();
					$(".qr_login").hide();
					$(".f2a").show();
					$("#qruid").val(j.uid);
					$('.f2a .title').html(j.ttxt);
				} else {
					$(".f1a .errmsg").html( j.text );
					$(".f1a .errmsg").addClass("error");
				}
			});
			}
		}).on("click", ".cab0_confirm", function(){
			$("#login .f2a .error").removeClass("error");
			$("#login .f2a .state").html("");
			$.post( "/mpi_map.php", {
				do: "api_login_a2f", 
				uid: $("#qruid").val(),
				secret: $(".f2a .inputs .t1").val() + $(".f2a .inputs .t2").val() + $(".f2a .inputs .t3").val() + $(".f2a .inputs .t4").val()
			}).done(function( data ) {
				var j = jQuery.parseJSON( data );
				if ( j.status == "+" ) {
					$("#login .f2a .state").html( j.text );
					setTimeout(function(){
						document.location.reload();
						return false;
					}, 1000);
				} else {
					$("#login .f2a .state").html( j.text );
					$("#login .f2a .state").addClass("error");
				}
			});
		}).on("click", "#manual_login .f1a .phone .cntr .actual", function(){
			$(this).next().toggleClass('active');
		}).on("click", "#manual_login .f1a .phone .cntr .select ins", function(){
			$(this).parent().removeClass('active');
			$class = $(this).find('i').attr('class');
			$code  = $(this).find('span').html();
			$('#manual_login .f1a .phone .cntr .actual i').attr('class', $class);
			$('#manual_login .f1a .phone .cntr .actual span').html($code);
		}).on("keyup", ".___inputs input___", function(){
			if (this.value.length == 1 ) {
				$(this).next().focus();
			}
		}).on("keydown", ".authcdnm", function(e){
			var charLimit = 1;
			
			//if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105
			
        	var keys = [8, 9, /*16, 17, 18,*/ 19, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 144, 145, 96,97,98,99,100,101,102,103,104,105];
	        if (e.which == 8 && this.value.length == 0) {
    	        $(this).prev(".authcdnm").focus();
        	} else if ($.inArray(e.which, keys) >= 0) {
            	return true;
        	} else if (this.value.length >= charLimit) {
            	$(this).next(".authcdnm").focus();
	            return false;
    	    } else if (e.shiftKey || e.which <= 48 || e.which >= 58) {
        	    return false;
        	}
		}).on("keyup", ".authcdnm", function(e){
			var charLimit = 1;
        	if (this.value.length >= charLimit) {
            	$(this).next(".authcdnm").focus();
            	return false;
        	}
		});
    
    /*
    $(".authcdnm").keydown(function(e) {

        var keys = [8, 9, 19, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 144, 145];

        if (e.which == 8 && this.value.length == 0) {
            $(this).prev(".authcdnm").focus();
        } else if ($.inArray(e.which, keys) >= 0) {
            return true;
        } else if (this.value.length >= charLimit) {
            $(this).next(".authcdnm").focus();
            return false;
        } else if (e.shiftKey || e.which <= 48 || e.which >= 58) {
            return false;
        }
    }).keyup (function () {
        if (this.value.length >= charLimit) {
            $(this).next(".authcdnm").focus();
            return false;
        }
    });
    */
 
		
		
		
});