﻿(function($) {

    function OnFocusClear() {
        $(".OnFocusClear").focus(function() {
            if ($(this).attr("title") == $(this).val()) {
                OnFocusValue = $(this).val();
                $(this).val('');
            }
        }).blur(function() {
            if ($(this).val() == '') {
                $(this).val(OnFocusValue);
            }
            OnFocusValue = '';
        });
    }



    var OnFocusValue = '';
    var MonthActivities;
    var CurrentMonth = 0;
    $(document).ready(function() {
        OnFocusClear();

        $("#datepicker").datepicker({
            firstDay: 0,
            dateFormat: "yy-mm-dd",
            onSelect: function(dateText, inst) {
                window.location = "/calendrier/" + dateText;
            },
            onChangeMonthYear: function(year, month, inst) {
                MonthActivities = "";
                CurrentMonth = month - 1;
                $.ajax({
                    type: "POST",
                    cache: true,
                    url: "/modules/Calendrier/Client/GetEvents.aspx",
                    data: ({ Year: year, Month: month }),
                    dataType: "json",
                    async: false,
                    success: function(data) { MonthActivities = data; }
                });
            },
            beforeShowDay: function(date) {
                if (CurrentMonth == date.getMonth()) {
                    return new Array((MonthActivities.items[date.getDate() - 1].IsSelectable == "True"), MonthActivities.items[date.getDate() - 1].CssClass, MonthActivities.items[date.getDate() - 1].Activites);
                } else {
                    return new Array(false, "", "");
                }
            }
        });

        $("table.ui-datepicker-calendar tbody *[title]").monnaTip();

        $(".InfoLettre .ButtonLink span").click(function() {
            var Name = "#[id*=tbxName]";
            var Courriel = "#[id*=_tbxEmail]";
            var FieldFill = true;

            if ($(Name).attr("title") == $(Name).val()) { FieldFill = false; }
            if ($(Courriel).attr("title") == $(Courriel).val()) { FieldFill = false; }
            if (FieldFill == true) {
                $(".ModuleBox .InfoLettre").block({ message: null });

                $.ajax({
                    type: "POST",
                    url: "/modules/mailingList/Client/AddEmailToMailingList.aspx",
                    data: ({ MailingListName: MailingListName, Name: $(Name).val(), Email: $(Courriel).val() }),
                    dataType: "html",
                    success: function(msg) {
                        $(".ModuleBox .InfoLettre").unblock();

                        if (msg != "error") {
                            $(".InfoLettre").fadeOut("3000");
                            $(".InfoLettre").html("<br />  Merci pour votre inscription!")
                            $(".InfoLettre").fadeIn("3000");
                        }
                        else {
                            alert("Une erreur est survenue, s.v.p recommencer plus tard.");
                        }

                    },
                    failure: function(err) {
                        $(".ModuleBox .InfoLettre").unblock();

                        alert("Une erreur est survenue, s.v.p recommencer plus tard (" + err + ").");
                    }
                });

            }
            else {
                alert('Vous devez entrer un nom et une adresse de courriel.')
            }
            return false;
        });

    });


})(jQuery);

