function DoPost(TargetURL) {
    var Datas=$('#ModalForm').serialize();
    $.ajax({
        type:'post',
        url:TargetURL,
        data:Datas,
        success:function(result, status, jqXHR){
            var regexp = /(javascript)/gi;
            if (!jqXHR.getResponseHeader('content-type').match(regexp))
                $("#AjaxModal").html(result);
        },
        error:function(result, error){
            alert(error);
        },        
    });
}

function DoPostLieu(TargetURL) {
    var Datas=$('#ModalPlaceForm').serialize();
    $.ajax({
        type:'post',
        url:TargetURL,
        data:Datas,
        success:function(result, status, jqXHR){
            var regexp = /(javascript)/gi;
            if (!jqXHR.getResponseHeader('content-type').match(regexp))
                $("#AjaxPlaceModal").html(result);
        },
        error:function(result, error){
            alert(error);
        },        
    });
}

function DoGet(TargetURL) {
    $.ajax({
        url:TargetURL,
        success:function(result, status, jqXHR){
            var regexp = /(javascript)/gi;
            if (!jqXHR.getResponseHeader('content-type').match(regexp)) {
                $("#AjaxModal").html(result);
                $("#AjaxModal").modal('show');
            }
        },
        error:function(result, error){
            alert(error);
        },        
    });
}

function DoGetLieu(TargetURL) {
    $('#AjaxModal').modal("hide");        
    $.ajax({
        url:TargetURL,
        success:function(result, status, jqXHR){
            var regexp = /(javascript)/gi;
            if (!jqXHR.getResponseHeader('content-type').match(regexp)) {
                $("#AjaxPlaceModal").html(result);
                $("#AjaxPlaceModal").modal('show');
            }
        },
        error:function(result, error){
            alert(error);
        },        
    });
}

$(document).ready(function() { 
    $.fn.modal.Constructor.prototype.enforceFocus = function() {};

    $('[handle="modal"]').click(function(e) {
        e.preventDefault();  
        var url = $(this).attr('Myhref');
        var target = $(this).attr('data-target');
        $(target).html('');
        $(target).addClass("Loading");
        $(target).modal("show");
        $(target).load(url, function() {
            $(target).removeClass("Loading");
        });
        return false;
    });

    $('#AjaxModal').on('hidden', function(bla){
        if (bla.target.id.endsWith('-help'))
            return;
        if (bla.target.id=='AjaxModal') {
            if ($(this).children().length)
                $(this).children()[0].remove();
            if (typeof map === "undefined") 
                return;
            if (map)
                map.remove();
        }
    });

    $('#AjaxPlaceModal').on('hidden', function(bla){
        $("#place_type").select2("destroy");
        if (bla.target.id.endsWith('-help'))
            return;
        if ($(this).children().length)    
            $(this).children()[0].remove();
        $("#place_type");
    });

    $('#AjaxPlaceModal').on('shown', function(bla){
     if (map)
          setTimeout(function() {
            map.invalidateSize();
          }, 10);    
    });

    $('#AjaxModal').on('shown', function(bla){
    if (bla.target.id=="ItinMap")
          setTimeout(function() {
            map.invalidateSize();
            toast.route();
            $('#map_Itineraire').eq(0).html( $('.leaflet-routing-alt').eq(0).html() );
            $('.leaflet-routing-container').eq(0).attr("style","display:none;")
            $('#map_Itineraire > table').eq(0).attr("style","width:100%;");
          }, 10);
    });

    $('[data-toggle="modal"]').click(function(e) {
      e.preventDefault();
      var url = $(this).attr('href');
      //var modal_id = $(this).attr('data-target');
      $.get(url, function(data) {
          $(data).modal();
      });
    });
    
    if (location.hash !== '') {
        var SavHash = location.hash;
        if (Array('#Miam','#Covoiturage','#Hebergement',
            '#Materiel','#ResumeInt').indexOf(location.hash)>=0)
            $('a[href="#Logistique"]').tab('show')
            .on('shown', function(e) {
                $('a[href="' + SavHash + '"]').tab('show');
            });
        else {
            if (Array('#ResumePart','#Conference','#Stand','#Atelier',
                '#TableRonde','#Organisation').indexOf(location.hash)>=0)
                $('a[href="#Interventions"]').tab('show')
                .on('shown', function(e) {
                    $('a[href="' + SavHash + '"]').tab('show');            
                });
            else
                $('a[href="' + SavHash + '"]').tab('show');
        }
    }
        
    jQuery(function() {
        jQuery('.repeat').each(function() {
            jQuery(this).repeatable_fields(
                {
                is_ready: function(container, therow) {
                    var numrow=$(container).attr('data-rf-row-count');
                    $("#tiersship-"+ (numrow-1) +"-tiers_uid").select2(
                        {
                            placeholder: 'Entrez ici votre entité',
                            minimumInputLength: 2, allowClear: true,
                            ajax: {
                                quietMillis: 250, url: "/json-tiers", dataType: 'json',
                                data: function (term, page) {
                                    return { pageSize: 8, pageNum: page, searchTerm: term };
                                },
                                results: function (data, page) {
                                    var more = (page * 8) < data.Total; 
                                    return { results: data.Results, more: more };
                                }
                            }
                        });
                }
            });
        });
    });

    $('a[data-toggle="tab"]')
        .on('shown', function(e) {
            stateObj = { tab: $(e.target).attr('href').substr(1) };
            history.replaceState(stateObj, "", "/MesJM2L" + $(e.target).attr('href') );
        }); 
    
    });

