$(function(){


    showRegistrationForm();

    function showRegistrationForm() {
        var sendData = {
            "action" : "showRegistrationForm"
        };

        $.ajax({
            type: 'POST',
            url: 'gateway.php',
            data: sendData,
            dataType: 'json',
            success : function(ret) {
                $('.step-holder').html(ret.data);
                validateForm();
            }
        });
    }

    function register() {

    }



    function validateForm() {

        var sendData = {
            "action" : "checkEmail",
            "data" :{
                "email" : $(".email").val()
            }
        };

        $("#register-form").validate({
            debug : true,
            rules: {
                first_name: {
                    required: true
                },
                last_name: {
                    required: true
                },
                email: {
                    required: true,
                    email: true,
                    remote : {
                        type : 'POST',
                        url : 'gateway.php',
                        data : sendData
                    }
                },
                city: {
                    required: true
                },
                address: {
                    required: true
                }
            },
            messages: {
                first_name:{
                    required: "Obavezno polje."
                },
                last_name:{
                    required: "Obavezno polje."
                },
                email:{
                    required: "Obavezno polje.",
                    email:"Neispravna E-mail adresa.",
                    remote: 'E-mail adresa već postoji'
                },
                city:{
                    required: "Obavezno polje."
                },
                address:{
                    required: "Obavezno polje."
                }
            }
        });
    }

    $(document).on("click",".submit",function(e){
        e.preventDefault();

        if ($('#register-form').valid()) {

            var sendData = {
                "action" : "registerUser",
                "data"   : {
                    "first_name" : $('.first_name').val(),
                    "last_name"  : $('.last_name').val(),
                    "email"      : $('.email').val(),
                    "phone"      : $('.phone').val(),
                    "city"       : $('.city').val(),
                    "address"    : $('.address').val()
                }
            };


            $.ajax({
                type: 'POST',
                url: 'gateway.php',
                data: sendData,
                dataType: 'json',
                success : function(ret) {
                    if(!ret.error) {
                        $('.register-form-box').remove();
                        $('.step-holder').html(ret.data);
                    } else {
                        console.log(ret.error);
                    }
                }
            });
        }
    });

    $(document).on("click",".choose-transport a",function(){

        var transport = $(this).attr('data-transport');
        var sendData = {
            "action" : "updateTransport",
            "data"   : {
                "transport" : transport
            }
        };

        $.ajax({
            type: 'POST',
            url: 'gateway.php',
            data: sendData,
            dataType: 'json',
            success : function(ret) {
                if(!ret.error) {
                    $('.step-holder').html(ret.data);
                    $('.choose-transport').remove();
                    //top.location.href = 'http://localhost/dn_registration_vip/changeavatar/';
                } else {
                    console.log(ret.error);
                }
            }
        });
    });


});