﻿function emailRecipeModal()
{
	$('#email-recipe').click(function(e)
    {
        e.preventDefault();
        
        $('#email-recipe-form').dialog('open');
    });
    
    // Attaching validation to the email form
    $('#email-form-validate').validate({
    	rules: {
    		youremail: {
    			required: true,
    			email: true
			},
			friendsemail: { required: true }
    	},
    	messages: {
    		youremail: { required: false },
    		friendsemail: { required: false }
    	},
    	submitHandler: function(form)
    	{
    		$('#email-form-validate').data('allowSend', true);
    	}
});

$('#submit-email-plan').click(function () {
    $('#email-form-validate').submit();
});
    
    // Listening to when the email form is submitted
$('#email-form-validate').submit(function () {
    if ($('#email-form-validate').data('allowSend') == true) {
        var emailObj = {};

        emailObj.YourEmail = $(this).find('input[name=youremail]').val();
        emailObj.FriendsEmails = $(this).find('input[name=friendsemail]').val();
        emailObj.YourName = $(this).find('input[name=yourname]').val();
        emailObj.YourMessage = $(this).find('textarea[name=yourmessage]').val();
        emailObj.RecipeId = $('#recipe-id').val();

        if (emailObj == null || emailObj.FriendsEmails == null || emailObj.FriendsEmails == '' || jQuery.trim(emailObj.FriendsEmails) == '') {
            return false;
        } else {

            // Sending email object to backend for emailing
            $.ajax({
                url: '/Tips-And-Tools/EmailRecipe/',
                type: 'POST',
                data: $.toJSON(emailObj),
                dataType: 'JSON',
                contentType: "application/json; charset=utf-8",
                // 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) {
                    /*
                    var returnObject = $.evalJSON(data);

                    // Double checking to make sure everything went through
                    if (returnObject.Result['ReturnCode'] == 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-recipe-form').dialog('close');
                        $('#email-thank-you').dialog('open');

                        $('#email-form-validate').data('allowSend', true);
                    }*/
                    $('#email-recipe-form').dialog('close');
                    $('#email-thank-you').dialog('open');

                    $('#email-form-validate').data('allowSend', true);
                }
            });
            // Close the dialog
            $('#email-recipe-form').dialog('close');
        }
    }
    // Preventing the form from refreshing the page
    return false;
});
    
    $('#email-recipe-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');
}

// Modal for when a user has emailed their plan
function thankYouModal()
{
	$('#close-thank-you-modal').click(function(e)
    {
        e.preventDefault();
        
        $('#email-thank-you').dialog('close');
    });
    
    $('#email-thank-you').dialog({
        autoOpen: false,
        modal: true,
        dialogClass: 'modal-popup png-fix'
    });
}

$(document).ready(function()
{
    // Disable ajax cache
    $.ajaxSetup({ cache: false });
    
    // Set up modals
	emailRecipeModal();
	thankYouModal();
    
    // Set email variable
    $('#email-form-validate').data('allowSend', false);
});
