﻿$(document).ready(function() {
    $("#ville").autocomplete("/ajax/ville.aspx",
{
    max: 10,
    minChars: 2,
    width: 250,
    cacheLength: 0,
    delay: 100,
    selectFirst: true,
    extraParams: { simple: true }
});

    $.fn.regex = function(pattern) {
        return ($(this[0]).val().match(pattern)) ? true : false;
    }


    $("#cree_alerte").click(function() {
        if ($('#alerte_email').is(':visible'))
            $("#alerte_email").hide();
        else
            $("#alerte_email").show();
    });

    $("#btAlerte").click(function() {
        if (!$("#emailAlerte").regex(/[\w\.-]+(\+[\w-]*)?@([\w-]+\.)+[\w-]+/g)) {
            $("#emailAlerte").addClass("select_champ_erreur");
            $('#emailAlerte').focus();
            return false;
        } else {
            $("#emailAlerte").removeClass("select_champ_erreur");
        }

        var url = "/ajax/SaveAlerte.ashx?";
        url += 'email=' + $("#emailAlerte").val() + $("#urlAlerte").val();


        $.get(url, function(data) {
            if (data == "0") {
                $("#confEmail").html($("#emailAlerte").val());
                $("#confirmation_div").show();
                $("#alerte_email").hide();
                $("#cree_alerte").unbind('click');
            }
            if (data == "1") {
                $("#action_div").show();
                $("#alerte_email").hide();
                $("#cree_alerte").unbind('click');
            }
            if (data == "9") {
                $("#erreur_div").show();
                $("#alerte_email").hide();
            }
        });
    });

    $('#emailAlerte').keypress(function(e) {
        if (e.which == 13) {
            $(this).blur();
            $('#btAlerte').focus().click();
            return false;
        }
    });


    $("#btAlerte2").click(function() {
        if (!$("#emailAlerte2").regex(/[\w\.-]+(\+[\w-]*)?@([\w-]+\.)+[\w-]+/g)) {
            $("#emailAlerte2").addClass("select_champ_erreur");
            $('#emailAlerte2').focus();
            return false;
        } else {
            $("#emailAlerte2").removeClass("select_champ_erreur");
        }

        var url = "/ajax/SaveAlerte.ashx?";
        url += 'email=' + $("#emailAlerte2").val() + $("#urlAlerte").val();


        $.get(url, function(data) {
            if (data == "0") {
                $("#confEmail2").html($("#emailAlerte2").val());
                $("#confirmation_div2").show();
                $("#liens_bas").hide();
            }
            if (data == "1") {
                $("#action_div2").show();
                $("#liens_bas").hide();
            }
            if (data == "9") {
                $("#erreur_div2").show();
            }
        });
    });


    $('#emailAlerte2').keypress(function(e) {
        if (e.which == 13) {
            $(this).blur();
            $('#btAlerte2').focus().click();
            return false;
        }
    });

});


