"use strict";
var cb = document;
$(cb).ready(function() {
	
	$('#phone').mask('000000000000000');
	$('#phone').focus();
	$("#phone").keydown(function(e) {
		if (e.keyCode === 13) {
			$("#btn-primary").click();
		}
    });
	
	$("#btn-primary").click(function() {
		var error = false;
		$(".flash").hide();
		var phone = $('#phone').val();
		var code = $('#calling_code').html();
		if(phone.length < 7){
			$(".flash").slideDown().delay(5000).slideUp();
			error = true;
		}
		if(error == false){
			$("#btn-primary").prop('disabled', true);
			$("#btn-primary").html('Processing...');
			$.ajax({
					type: 'POST',
					url: cb_endpoint + '/cb-ajax/phone/submit',
					data: {
						phone: code + phone
					},
					success: function(res) {
						if(res == "ok"){
						location.replace(cb_endpoint + "/oauth/authorize/oauth_signin?client_id=" + session_id);
						}else{
							$("#btn-primary").prop('disabled', false);
							$("#btn-primary").html('Continue');
							$(".flash").show();
							$('#phone').val("");
							$('#phone').focus();
						}
					}
			});
		}
	});
	
});