jQuery(document).ready(function ($) {	
	
	// ######### ON SUBMIT OF FORM JS VALIDATION STARTS #########//
	
	$('#submit_unify_pro').click(function(){

		jQuery.validator.addMethod("privacy_policy_chkbox_function", function(value, element){
		    var valid = true;
		    if ($("#privacy_policy_chkbox").prop("checked") == true) {
		        $("#privacy_policy_chkbox-error").remove(); 
		    } else {
		        valid = false;
		        $("#privacy_policy_chkbox-error").remove(); 
		        $("#privacy_policy_chkbox").after('<label id="privacy_policy_chkbox-error" class="text-danger" for="privacy_policy_chkbox">Privacy Policy is a required field.</label>');
		    }
		      return valid;
				}, "Privacy Policy is a required field.");

		$.validator.addMethod("customemail", 
		    function(value, element) {
		        return /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(value);
		    }, 
		    ""
		);


		$("#request_unify_pro_form").validate({ // initialize the plugin
			rules: {
				first_name: {
					required: true
				},
				last_name: {
					required: true
				},
				company_name: {
					required: true
				},
				email_address: {
					required: true,
					customemail: true,	
				},
				phone_number: {
					required : true,
					number: true,
					minlength: 10,
					maxlength: 15,
				},
				comment: {
					required: true
				},
				privacy_policy_chkbox: {
					privacy_policy_chkbox_function: true
				},

			},
			messages :{
				first_name: {
					required: 'First Name is a required field.'
				},
				last_name: {
					required: 'Last Name is a required field.'
				},
				company_name: {
					required: 'Company Name is a required field.'
				},
				email_address: {
					required: 'Email Address is a required field.',
					email: 'Please provide a valid email address.',
				},
				phone_number: {
					required : 'Phone Number is a required field.',
					digits: 'Please provide a valid phone number.'
				},
				comment: {
					required: 'Comment is a required field.'
				},
			},
			errorClass:'error',
			errorPlacement: function (error, element) {
				$(this).addClass('error');
			}
		});
		
		if($("#request_unify_pro_form").valid()){
			requestUnifyPro();
			return true;	
		}
		return false;
	});

	// ######### ON SUBMIT OF FORM JS VALIDATION ENDS #########//

});

/*terms and condition checkbox validation*/
    function agreement_checkbox_validation() {

        var valid = true;
        if ($("#privacy_policy_chkbox").prop("checked") == true) {
                $("#privacy_policy_chkbox-error").remove(); 
            } else {
                valid = false;
                $("#privacy_policy_chkbox-error").remove(); 
                $("#privacy_policy_chkbox").after('<label id="privacy_policy_chkbox-error" class="text-danger" for="privacy_policy_chkbox">Privacy Policy is a required field.</label>');
            }
        return valid;
    }


function requestUnifyPro(){
	$.ajax({
		beforeSend: function () {
			$('.overlayDiv').show();
		},
		data: {
			'action': 'unify_pro_request',
			'x': $('#request_unify_pro_form').serialize()
		},
		dataType: 'json',
		type: 'POST',
		url: ajaxurl,
		success: function (response) {
			const status = response.status;
			var msg = response.msg;
			var color = "";
			if(status == 1){
				$(".request_pro_plugin_form").css("display","none");
				$(".upgrade-request").css("display","flex");
			}
			$(".validated_msg").html(msg);$(".validated_msg").css("color",color);
			$(".validated_msg").css("display",'inline-block');
			$(".validated_msg").delay(5000).fadeOut('slow');
		},
		error: function (response){
			color = "red";msg = "Invalid Credential <i class='fa fa-exclamation-triangle' aria-hidden='true'></i>";
			$(".validated_msg").html(msg);
			$(".validated_msg").css("color",color);
			$(".validated_msg").css("display",'inline-block');
			$('.validated_msg').delay(5000).fadeOut('slow');
		},
		complete: function (response) {
      		$('.overlayDiv').hide(); 
     	}
	});
}