
// Handles when a user clicks the email my plan button
function emailRecipeModal() {
    $('#email_recipe').click(function(e) {
        e.preventDefault();

        $('#email-my-plan-form').dialog('open');
    });

    // Attaching validation to the email form
    $('#email-form-validate').validate({
        rules: {
            youremail: {
                required: true,
                email: true
            },
            yourname: {
                required: true
            },
            yourfriendname: {
                required: true
            },
            friendsemail: {
                required: true
            }
        },
        messages: {
            youremail: { required: false },
            yourname: { required: false},
            yourfriendname: { required: false },
            friendsemail: { required: false}      
        }
    });

    // Listening to when the email form is submitted
    $('#email-form-validate').submit(function() {
        if ($('#email-form-validate').data('allowSend') == true) {
            var yourEmail = $(this).find('input[name=youremail]').val();
            var friendsEmails = $(this).find('input[name=friendsemail]').val();
            var yourName = $(this).find('input[name=yourname]').val();
            var yourMessage = ''; //$(this).find('textarea[name=yourmessage]').val();

            if (jQuery.trim(friendsEmails) == '') {
                friendsEmails = 'Your Friend';
            }


            // Sending email object to backend for emailing
            $.ajax({

                type: "POST",
                url: '/Controllers/Actions/CA_EmailPlan.aspx',
                data: 'yourEmail=' + yourEmail + '&friendsEmails=' + friendsEmails + '&yourName=' + yourName + '&yourMessage=' + yourMessage + '&url=' + window.location,
                // Preventing users from triggering the email event until finished
                beforeSend: function() {
                    $('#email-form-validate').data('allowSend', false);
                },
                // After email object has been sent successfully
                success: function(data) {


                    // Double checking to make sure everything went through
                    if (data == "1") {
                        $('#email-form-validate').find('input[name=youremail]').val('');
                        $('#email-form-validate').find('input[name=friendsemail]').val('');
                        $('#email-form-validate').find('input[name=yourname]').val('');
                        //$('#email-form-validate').find('textarea[name=yourmessage]').val('');

                        $('#email-my-plan-form').dialog('close');
                        $('#email-thank-you').dialog('open');

                        $('#email-form-validate').data('allowSend', true);
                    } else {

                        if (data == '0') {
                            $('#email-form-validate').data('allowSend', true);
                        }
                    }
                }
            });
        }

        // Preventing the form from refreshing the page
        return false;
    });

    $('#email-my-plan-form').dialog({
        autoOpen: false,
        modal: true,
        dialogClass: 'contact-modal-popup png-fix'
    });

    // Show character count for email modal
    //$('#email-plan-textarea').limit('400', '#email-plan-textarea-counter');
}

function thankYouModal() {
    $('#close-thank-you-modal').click(function(e) {
        e.preventDefault();

        $('#email-thank-you').dialog('close');
    });

    $('#email-more-friends').click(function(e) {
        e.preventDefault();

        $('#email-thank-you').dialog('close');

        $('#email-my-plan-form').dialog('open');

    });

    $('#email-thank-you').dialog({
        autoOpen: false,
        modal: true,
        dialogClass: 'modal-popup png-fix'
    });
}
