/* Hotjar */
(function(h,o,t,j,a,r){
    h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
    h._hjSettings={hjid:807847,hjsv:6};
    a=o.getElementsByTagName('head')[0];
    r=o.createElement('script');r.async=1;
    r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
    a.appendChild(r);
})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');


/* Drift */
!function() {
    var t;
    if (t = window.driftt = window.drift = window.driftt || [], !t.init) return t.invoked ? void (window.console && console.error && console.error("Drift snippet included twice.")) : (t.invoked = !0, 
    t.methods = [ "identify", "config", "track", "reset", "debug", "show", "ping", "page", "hide", "off", "on" ], 
    t.factory = function(e) {
      return function() {
        var n;
        return n = Array.prototype.slice.call(arguments), n.unshift(e), t.push(n), t;
      };
    }, t.methods.forEach(function(e) {
      t[e] = t.factory(e);
    }), t.load = function(t) {
      var e, n, o, i;
      e = 3e5, i = Math.ceil(new Date() / e) * e, o = document.createElement("script"), 
      o.type = "text/javascript", o.async = !0, o.crossorigin = "anonymous", o.src = "https://js.driftt.com/include/" + i + "/" + t + ".js", 
      n = document.getElementsByTagName("script")[0], n.parentNode.insertBefore(o, n);
    });
  }();
  drift.SNIPPET_VERSION = '0.3.1';
  drift.load('c9twm4i9hgxa');

var Script = function () {

    $.validator.setDefaults({
        submitHandler: function() { 
           var username=$("#username").val();
           var password=$("#password").val();
           var role=$("#role").val();
           var verification_no=$("#verification_no").val();
           var dataString = 'username='+username+'&password='+password+'&verification_no='+verification_no+'&role='+role;
           if ($.trim(username).length > 0 && $.trim(password).length > 0){
             $.ajax({
                type: "POST",
                url: "/register/submit",
                data: dataString,
                cache: false,
                beforeSend: function(){ 
                    $("#status").html('<p>Connecting to server....</p>');
                },
                complete: function(e, xhr, settings){
                    if(e.status == 200){
                        $("#status").html("<span style='color: #00CC96;'>Success:</span> Please login again. ");
                        setTimeout(function () {
                            window.location.href = "/dashboard"; // The URL that will be redirected too.
                        }, 3000);
                    }else{
                        //Shake animation effect.
                        $('form').shake();
                        $("#status").html("<span style='color:#cc0000'>Error:</span> Invalid username and password. ");
                    }
                }
                
            }); 
		}
        return false;
        }
    });
	
    $().ready(function() {
        $("#register-form").validate({
			doNotHideMessage: true,
			errorElement: 'span', //default input error message container
			errorClass: 'help-block help-block-error', // default input error message class
			focusInvalid: false,
            rules: {
                username: {
                    required: true,
					remote: {
                        url: "/register/check",
                        type: "post",
                        data: {
							username: function(data) {
								return $( "#username" ).val();
							}
						}
                    }
                },
                confirm_password: {
                    equalTo: "#password",
                },
                verification_no: {
                    required: true,
					remote: {
                        url: "/register/check-verification",
                        type: "post",
                        data: {
							verification_no: function() {
								return $( "#verification_no" ).val();
							}, 
							username:function() {
								return $( "#username" ).val();
							}, 
						}
                    }
                }
            },
            messages: {
                
                username: {
					required: 'Username field is required!',
                    remote: "Please enter a correct phone number"
                    
                },
                confirm_password: {
                    equalTo: "Please enter the same Password as above"
                },
                verification_no: {
                    required: "Please provide the verification number that have been sent to your phone",
					remote: "This verification number is incorrect."
                    
                },
                
            }
        });

        // propose username by combining first- and lastname
       
    });
	$('#register-form input').on('keyup blur', function () { // fires on every keyup & blur
        if ($('#username').valid()) {                   // checks form for validity
            $('#send-verification').prop('disabled', false);        // enables button
        } else {
            $('#send-verification').prop('disabled', 'disabled');   // disables button
        }
		if ($('#register-form').valid()) {                   // checks form for validity
            $('#register').prop('disabled', false);        // enables button
        } else {
            $('#register').prop('disabled', 'disabled');   // disables button
        }
    });
	$('#send-verification').on('click', function () { 
		
	   var username=$("#username").val();
	   var dataString= 'username='+username;
		$.ajax({
			type: "POST",
			url: "/register/send-verification",
			data: dataString,
                cache: false,
				data: dataString,
                beforeSend: function(){ 
                    $("#status").html('<p>Connecting to server....</p>');
                },
				complete: function(e, xhr, settings){
                    console.log(e);
                    if(e.status == 200){
						$("#status").html("<span style='color: #00CC96;'>Success:</span> The verification code have been sent to your number");
						$('#send-verification').prop('disabled', 'disabled');   // disables button
                    }
                    else{
                        $('form').shake();
                        $("#status").html("<span style='color:#cc0000'>Error:</span> Please enter 10 digits phone number without +60. eg (0112332432) ");
                    }
				}
                
		}); 
		
	});

}();