/*==============================================================*/
// Contact Form  JS
/*==============================================================*/
(function ($) {
    "use strict"; // Start of use strict
    $("#homeForm").validator().on("submit", function (event) {
        if (event.isDefaultPrevented()) {
            // handle the invalid form...
            formError();
            submitMSG(false, "Did you fill in the form properly?");
        } else {
            // everything looks good!
            event.preventDefault();
            submitForm();
        }
    });


    function submitForm(){
        // Initiate Variables With Form Content
        var name = $("#name").val();
        var mobile = $("#mobile").val();
        var email = $("#email").val();
        var service = $("#service").val();


        $.ajax({
            type: "POST",
            url: "./form-process.php",
            data: "name=" + name + "&email=" + email + "&mobile=" + mobile + "&service=" + service,
            success : function(text){
              formSuccess();
            },
            error: function(error){
              formError();
              submitMSG(false,text);
            }
        });
    }

    function formSuccess(){
        $("#homeForm")[0].reset();
        submitMSG(true, "Query Submitted!");
        createCaptcha();
    }

    function formError(){
        $("#homeForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
            $(this).removeClass();
        });
    }

    function submitMSG(valid, msg){
        if(valid){
            var msgClasses = "h5 tada animated text-success";
        } else {
            var msgClasses = "h5 text-white";
        }
        $("#bookSubmit").removeClass().addClass(msgClasses).text(msg);
    }
}(jQuery)); // End of use strict
