
(function($) {
    "use strict";

    
    careerplus_contactForm();
   
})(window.jQuery);

/* ------------------------------------------------------------------- */
/* 02.Contact Form
/* ------------------------------------------------------------------- */
 function careerplus_contactForm() {
    "use-strict";
    //  Validate Input Variables
   // var contactEmail    = $("input[name=contact_email]");
    var contactPhone    = $("input[name=contact_phone]");
    var formControl     = $('.contact-form-group .form-control');

    

    // Phone Validation
    contactPhone.on("keyup", function() {
        if ( ( $(this).val().trim().length ) > 0) {
            if (! ($(this).careerplusPhoneValidate() === true) ) {
                $(this).parent().find("span").removeClass("success").addClass("error");
            } else {
                $(this).parent().find("span").removeClass("error").addClass("success");
            }
        }else{
            $(this).parent().find("span").removeAttr("class");
            $(this).parent().find("span").addClass("error");  
        }
    });

    // Form Control Validate
    $(".form-control:not('[name=contact_phone]')").on("keyup", function() {
        if ($(this).val().trim().length > 0) {
            $(this).parent().find("span").removeClass("error").addClass("success");
        }else {
            $(this).parent().find("span").removeAttr("class");
            $(this).parent().find("span").addClass("error");
        }
    });

    // Popup Variables
    let termsAgree          = $('#termsAgree');

    // Terms Agree
    termsAgree.on("click",function(event){
        $("#termsCheckBox").prop("checked",true);
        $('input[name="contact_terms"]').prop("checked",true);
    });


    //  Captcha Variables    
    let textCaptcha     = $("#txtCaptcha");
    let textCaptchaSpan = $('#txtCaptchaSpan');
    let textInput       = $('#txtInput');

    // Generates the Random number function 
    function randomNumber(){
         
        let a = Math.ceil(Math.random() * 9) + '',
            b = Math.ceil(Math.random() * 9) + '',
            c = Math.ceil(Math.random() * 9) + '',
            d = Math.ceil(Math.random() * 9) + '',
            e = Math.ceil(Math.random() * 9) + '',
            code = a + b + c + d + e;
   
        textCaptcha.val(code);
        textCaptchaSpan.html(code);
    }

    // Called random number function
    randomNumber();

    // Validate the Entered input aganist the generated security code function   
    function validateCaptcha() {
        let str1 = textCaptcha.val();
        let str2 = textInput.val();
        if (str1 == str2) {
            return true;
        } else {
            return false;
        }
    }

    // Form Conttrol Captcha Validate
    textInput.on("keyup", function() {
        if (validateCaptcha() == true) {
            $(this).parent().find("span").removeClass("error").addClass("success");
        }else {
            $(this).parent().find("span").removeAttr("class");
            $(this).parent().find("span").addClass("error");
        }
    });

    // Contact Form Submit
    $("#contactForm").on("submit", function(event) {

        
        //  Contact Form Input Value 
        var $this         = $(this);
        
        var contact_name  = $this.find('input[name="contact_name"]').val().trim();
		
        //var contact_email = $this.find('input[name="contact_email"]').val().trim();
        var contact_phone = $this.find('input[name="contact_phone"]').val().trim();
        var contact_location = $this.find('input[name="contact_location"]').val().trim();
        var contact_subject = $this.find('select[name="contact_subject"]').val().trim();
       // var contact_message = $this.find('textarea[name="contact_message"]').val().trim();
        var termsCheckBox = $this.find('input[name="contact_terms"]').prop("checked");
        //var validateEmail = $this.find('input[name="contact_email"]').careerplusEmailValidate();
        var validatePhone = $this.find('input[name="contact_phone"]').careerplusPhoneValidate();
        var selectedNull  = $this.find('select[name="contact_subject"]').find("option").eq(0).val();


        if (contact_name =='' || contact_phone == '' || contact_location == '' || textInput == '') {
            $(this).parent().find("span").addClass("error");
            if($('.empty-form').css("display") == "none"){
                $('.empty-form').stop().slideDown().delay(1000).slideUp();
            }else {
                return false;
            }
      
	  
	  } else if (contact_subject == selectedNull) {
            if($('.subject-alert').css("display") == "none"){
                $('.subject-alert').stop().slideDown().delay(3000).slideUp();
            }else {
                return false;
            }
	  
	  
        } else if (termsCheckBox == false) {
            if($('.terms-alert').css("display") == "none"){
                $('.terms-alert').stop().slideDown().delay(1000).slideUp();
            }else {
                return false;
            }
        } else if (!validatePhone === true) {
            $('input[name="contact_phone"]').parent().find("span").removeClass("success").addClass("error");
            if($('.phone-invalid').css("display") == "none"){
                $('.phone-invalid').stop().slideDown().delay(1000).slideUp();
            }else {
                return false;
            }
        
        } else if (validateCaptcha() != true){
            $("#textInput").parent().find("span").removeClass("success").addClass("error");
            if($('.security-alert').css("display") == "none"){
                $('.security-alert').stop().slideDown().delay(1000).slideUp();
            }else {
                return false;
            }
        } else {
            $this.find(':submit').append('<span class="fa fa-spinner fa-pulse ml-3"></span>');
            $this.find(':submit').attr('disabled','true');
            $.ajax({
                url: "assets1/vendor/send_mail-inner.php?mail=request",
                data: {
                    contact_name: contact_name,                   
                    contact_phone: contact_phone,					
                    contact_location: contact_location, 
					contact_subject: contact_subject
                },
                type: "POST",
                success: function(response) {
                    $(".form-control").parent().find("span").removeAttr("class");
                    $("#contactForm")[0].reset();
                    $(".select-selected").html(selectedNull);
                    if (response == true) {
						
                        $this.find(':submit').removeAttr('disabled');
                        $this.find(':submit').find("span").fadeOut();
						$('#myModal').modal('hide');
                        $("#formSuccessModal").modal("show");
                        // Called random number function
                        randomNumber();
                    } else {
                        $this.find(':submit').removeAttr('disabled');
                        $this.find(':submit').find("span").fadeOut();
                        $("#formDangerModal").modal("show");
                        $("#formDangerModal #error_message").html(response);
                        // Called random number function
                        randomNumber();
                    }
                }
            });
        }
        event.preventDefault();
    });
}


