jQuery(document).ready(function () {
    jQuery("#btnSubscribeOnly").click(function () {
        // validation for editing recipe folder name and description
        jQuery("#aspnetForm").validate().element("#txtEmailSubscribe");
        jQuery("#txtEmailSubscribe").rules(
                                                "add", {
                                                    nowhitespace: true,
                                                    minlength: 1,
                                                    maxlength: 49,
                                                    QAsEmailValidator: true,
                                                    messages: {
                                                        minlength: javascript91,
                                                        maxlength: javascript45,
                                                        nowhitespace: javascript44,
                                                        QAsEmailValidator: javascript44
                                                    }
                                                });

        var checkIt = jQuery("#aspnetForm").validate().element("#txtEmailSubscribe");
        if ((jQuery("#txtEmailSubscribe").val().length > 0) && (checkIt == 1)) {
            //OLD FUNCTIONALITY
            jQuery('#txtEmail').val(jQuery("#txtEmailSubscribe").val());

            //show the registration modal after setting up
            //jQuery('#dialog').jqmShow();
            jQuery("#imgGF").hide();
            jQuery("#chkGFNewsletter").show();
            jQuery("#content_joinGFLMD").show();
            RegisterDialog();
        }

        // remove the rules so they don't conflict with profile, or others
        jQuery("#txtEmailSubscribe").rules("remove");
    }
    );


    jQuery("#btnGFSubscribeOnly").click(function () {
        // validation for editing recipe folder name and description
        jQuery("#aspnetForm").validate().element("#txtGFEmailSubscribe");
        jQuery("#txtGFEmailSubscribe").rules(
                                                "add", {
                                                    nowhitespace: true,
                                                    minlength: 1,
                                                    maxlength: 49,
                                                    QAsEmailValidator: true,
                                                    messages: {
                                                        minlength: javascript91,
                                                        maxlength: javascript45,
                                                        nowhitespace: javascript44,
                                                        QAsEmailValidator: javascript44
                                                    }
                                                });

        var checkIt = jQuery("#aspnetForm").validate().element("#txtGFEmailSubscribe");

        if ((jQuery("#txtGFEmailSubscribe").val().length > 0) && (checkIt == 1)) {
            //OLD FUNCTIONALITY
            jQuery('#txtEmail').val(jQuery("#txtGFEmailSubscribe").val());

            //show the registration modal after setting up
            //jQuery('#dialog').jqmShow();
            jQuery("#imgGF").show();
            jQuery("#chkGFNewsletter").show();
            jQuery("#content_joinGFLMD").show();

            RegisterDialog();
        }


        // remove the rules so they don't conflict with profile, or others
        jQuery("#txtGFEmailSubscribe").rules("remove");
    }
    );
});

function IsUserSubscribed(TrueResultCode, FalseResultCode) {
    CallServiceMethod("/services/UserManagement.asmx/IsNewsletterSubscribed", '{}',
				      function (msg) {
				          var subscribed = eval(msg.d);

				          if (subscribed) {
				              TrueResultCode();
				          }
				          else {
				              FalseResultCode();
				          }
				      },
				      function (xhr, status, error) {
				          var err = xhr.responseText;
				          document.write(err);
				      });
}

function IsGFUserSubscribed(TrueResultCode, FalseResultCode) {
    CallServiceMethod("/services/UserManagement.asmx/IsGFNewsletterSubscribed", '{}',
				      function (msg) {
				          var subscribed = eval(msg.d);

				          if (subscribed) {
				              TrueResultCode();
				          }
				          else {
				              FalseResultCode();
				          }
				      },
				      function (xhr, status, error) {
				          var err = xhr.responseText;
				          document.write(err);
				      });
}

function SubscribeToNewsletter(emailAddress) {
    CallServiceMethod("/services/UserManagement.asmx/SubscribeToNewsletter", '{"emailAddress":"' + emailAddress + '"}',
				      function (msg) {
				          jQuery("#subscribeBox").hide();
				          jQuery("#subscribeBoxCompleted").show();
				      },
				      function (xhr, status, error) {
				          var err = xhr.responseText;
				          document.write(err);
				      });
}

function SetupNewsletterWidget(innerCopy) {
    jQuery('#content_subscribeWidget1').html(content_subscribeWidget1);
    jQuery('#content_subscribeWidget2').html(innerCopy);
    jQuery('#content_subscribeWidget3').html(content_subscribeWidget3);
    jQuery('#content_subscribeWidget4').html(content_subscribeWidget4);

    IsUserSignedIn(
        function () {
            jQuery("#subscribeBox").hide();
            jQuery("#subscribeBoxCompleted").hide();
        },
        function () {
            jQuery("#subscribeBox").show();
            jQuery("#subscribeBoxCompleted").hide();
        }
    );
}

function SetupGFNewsletterWidget(innerCopy) {
    jQuery('#content_GFsubscribeWidget1').html(content_GFsubscribeWidget1);
    jQuery('#content_GFsubscribeWidget2').html(innerCopy);
    jQuery('#content_GFsubscribeWidget3').html(content_GFsubscribeWidget3);
    jQuery('#content_GFsubscribeWidget4').html(content_GFsubscribeWidget4);

    IsUserSignedIn(
        function () {
            jQuery("#GFsubscribeBox").hide();
            jQuery("#GFsubscribeBoxCompleted").hide();
        },
        function () {
            jQuery("#GFsubscribeBox").show();
            jQuery("#GFsubscribeBoxCompleted").hide();
        }
    );
}



function SetupNewsletterWidgetWithTitle(innerCopy, titleCopy) {
    jQuery('#content_subscribeWidget1').html(titleCopy);
    jQuery('#content_subscribeWidget2').html(innerCopy);
    jQuery('#content_subscribeWidget3').html(content_subscribeWidget3);
    jQuery('#content_subscribeWidget4').html(content_subscribeWidget4);

    IsUserSignedIn(
        function () {
            jQuery("#subscribeBox").hide();
            jQuery("#subscribeBoxCompleted").hide();
        },
        function () {
            jQuery("#subscribeBox").show();
            jQuery("#subscribeBoxCompleted").hide();
        }
    );
}

function SetupGFNewsletterWidgetWithTitle(innerCopy, titleCopy) {
    jQuery('#content_GFsubscribeWidget1').html(titleCopy);
    jQuery('#content_GFsubscribeWidget2').html(innerCopy);
    jQuery('#content_GFsubscribeWidget3').html(content_GFsubscribeWidget3);
    jQuery('#content_GFsubscribeWidget4').html(content_GFsubscribeWidget4);

    IsUserSignedIn(
        function () {
            jQuery("#GFsubscribeBox").hide();
            jQuery("#GFsubscribeBoxCompleted").hide();
        },
        function () {
            jQuery("#GFsubscribeBox").show();
            jQuery("#GFsubscribeBoxCompleted").hide();
        }
    );
}
