<!-- include jquery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <!-- add custom styles -->
    <style type="text/css">
        .success{color:#2ECC40;}
        .mailgun.error{color:#FF4136;}
        .mailgun.warning{color:#FF851B;}
    </style>
    <script type="text/javascript">
        // document ready
        $(function() {
            
            // capture all enter and do nothing
            // email id
            $('#6298ade6b5e3e390724b7fc33bc40eb9-f85JodfkSepFzrZwU2dDvU').keypress(function(e) {
                if(e.which == 13) {
                    $('#6298ade6b5e3e390724b7fc33bc40eb9-f85JodfkSepFzrZwU2dDvU').trigger('focusout');
                    return false;
                }
            });

            $(".lp-button-react.w-00b2cef2-fa7d-fe8e-d123-a6c1cc5ad3e6").on( 'click', function() {
                
                var check_name =  $('#a4024ef1af8229eed93cefbaf3511a00-f85JodfkSepFzrZwU2dDvU').val();
                var check_email = $('#6298ade6b5e3e390724b7fc33bc40eb9-f85JodfkSepFzrZwU2dDvU').val();
                
                if (($.trim(check_name).length == 0) || ($.trim(check_email).length == 0) ){
                    if ( ! $('.mailgun.error').length ) {
                        $('#input-confirmation-f85JodfkSepFzrZwU2dDvU').prepend('<span class="mailgun error">Please enter Name and Email address.</span>');
                    }
                    if ( $('.mailgun.error').length ) {
                        $('.mailgun.error').remove();
                        $('#input-confirmation-f85JodfkSepFzrZwU2dDvU').prepend('<span class="mailgun error">Please enter Name and Email address.</span>');
                    }
                    
                    console.log("Please enter Name and Email address.");
                    return false;
                }
                else{
                var thisElement = $('#6298ade6b5e3e390724b7fc33bc40eb9-f85JodfkSepFzrZwU2dDvU');
                auto_validator(thisElement);
                return false;
                }
             });
             
            $('#6298ade6b5e3e390724b7fc33bc40eb9-f85JodfkSepFzrZwU2dDvU').each(function() {
                var thisElement = $(this);
                auto_validator(thisElement);
            });
            
            function auto_validator(thisElement) {
            
                var options = {
                    api_key     : "pubkey-14757c55e89bb8cd03da97eac21e053c",
                    in_progress : validation_in_progress,
                    success        : validation_success,
                    error        : validation_error
                };

                // trim string and autocorrect whitespace issues
                var elementValue = thisElement.val();
                elementValue = $.trim(elementValue);
                thisElement.val(elementValue);

                run_validator(elementValue, options, thisElement);

                return true;
            }
            
            // while the lookup is performing
            function validation_in_progress() {
                //$('#input-confirmation-f85JodfkSepFzrZwU2dDvU').prepend("<img src='loading.gif' height='50'/>");
            }
            
            // if email successfull validated
            function validation_success(data) {
                $('.mailgun.error').remove();
                $('.mailgun.warning').remove();
                $('#input-confirmation-f85JodfkSepFzrZwU2dDvU').prepend(get_suggestion_str(data['is_valid'], data['did_you_mean']));
            }

            // if email is invalid
            function validation_error(error_message) {
                $('.mailgun.error').remove();
                $('.mailgun.warning').remove();
                $('#input-confirmation-f85JodfkSepFzrZwU2dDvU').prepend(error_message);
            }

            function run_validator(address_text, options, element) {
                // abort existing AJAX Request to prevent flooding
                if(element.mailgunRequest) {
                    element.mailgunRequest.abort();
                    element.mailgunRequest = null;
                }

                // don't run validator without input
                if (!address_text) {
                    return;
                }

                // validator is in progress
                if (options && options.in_progress) {
                    options.in_progress(options.e);
                }

                // don't run dupicate calls
                if (element.mailgunLastSuccessReturn) {
                    if (address_text == element.mailgunLastSuccessReturn.address) {
                        if (options && options.success) {
                            options.success(element.mailgunLastSuccessReturn, options.e);
                        }
                        return;
                    }
                }

                // length and @ syntax check
                var error_message = false;
                if (address_text.length > 512) {
                    error_message = 'Email address exceeds maxiumum allowable length of 512.';
                } else if (1 !== address_text.split('@').length-1) {
                    error_message = '<span class="mailgun warning">Email address must contain only one @.</span>';
                }

                // confirm if error
                if (error_message) {
                $('.mailgun.error').remove();
                $('.mailgun.warning').remove();
                    if (options && options.error) {
                        options.error(error_message, options.e);
                    }
                    else {
                        if (console) console.log(error_message);
                    }
                    return;
                }

                // require api key
                if (options && options.api_key == undefined) {
                    if (console) console.log('Please pass in api_key to mailgun_validator.');
                }

                // timeout incase of some kind of internal server error
                var timeoutID = setTimeout(function() {
                    error_message = 'Error occurred, unable to validate address.';
                    if (!success) {
                        //Abort existing AJAX Request for a true timeout
                        if(element.mailgunRequest) {
                            element.mailgunRequest.abort();
                            element.mailgunRequest = null;
                        }

                        if (options && options.error) {
                            options.error(error_message, options.e);
                        }
                        else {
                            if (console) console.log(error_message);
                        }
                    }
                }, 30000); // 30 seconds

                // make ajax call to get validation results
                element.mailgunRequest = $.ajax({
                    type: "GET",
                    url: 'https://api.mailgun.net/v2/address/validate?callback=?',
                    data: { address: address_text, api_key: options.api_key },
                    dataType: "jsonp",
                    crossDomain: true,
                    success: function(data, status_text) {
                        clearTimeout(timeoutID);

                        element.mailgunLastSuccessReturn = data;
                        if (options && options.success) {
                            options.success(data, options.e);
                        }
                    },
                    error: function(request, status_text, error) {
                        clearTimeout(timeoutID);
                        error_message = 'Error occurred, unable to validate address.';

                        if (options && options.error) {
                            options.error(error_message, options.e);
                        }
                        else {
                            if (console) console.log(error_message);
                        }
                    }
                });            
            }
        });
            
        // suggest a valid email
        function get_suggestion_str(is_valid, alternate) {
            if (is_valid) {
                // implement custom validation script
                var valid_email = $('#6298ade6b5e3e390724b7fc33bc40eb9-f85JodfkSepFzrZwU2dDvU').val();        
                var result_v = validateEmail(valid_email);
                if (result_v == 'invalid') {
                    var result = '<span class="mailgun warning">Please try again with a real email address.</span>';
                                        
                } else if (result_v == 'notbusiness') {
                    var result = '<span class="mailgun warning">Please input business email only.</span>';
                    
                } 
                else if (result_v == 'valid'){
                    //$(".lp-form-react__error.lp-form-react__error--invalid").closest('form').submit();
                    $(".lp-button-react-wrapper").closest('form').submit();
                }
                    
                if (alternate) {
                    result += '<span class="mailgun warning"> (Though did you mean <em>' + alternate + '</em>?)</span>';
                }
                return result;
            } else if (alternate) {
                return '<span class="mailgun warning">Did you mean <em>' +  alternate + '</em>?</span>';
            } else {
                
                return '<span class="mailgun error">Address is invalid.</span>';
            }
        }
          
        // custom script call here 
        function validateEmail(email) {
            var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
            if (filter.test(email)) {
                var regex = /^([\w-\.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)(?!yahoo.co.in)(?!aol.com)(?!abc.com)(?!xyz.com)(?!pqr.com)(?!test.com)(?!rediffmail.com)(?!live.com)(?!outlook.com)(?!me.com)(?!msn.com)(?!ymail.com)([\w-]+\.)+[\w-]{2,4})?$/;
                if (regex.test(email)) {
                    return 'valid';
                } else {
                    return 'notbusiness';
                }
            } else {
                return 'invalid';
            }
        }
    </script>