﻿
$(document).ready(function () {

    var href;
    href = window.location.protocol + "//" + window.location.host + window.location.pathname.substring(0, window.location.pathname.indexOf("/", 1)) + "/";

    /*Autocompelete du textbox nego*/
    $("#txt_VilleCPDep").autocomplete({

        source: function (request, response) {
            $.ajax({
                url: href + "Ville.asmx/fville_fr",
                data: "{ 'ThePrefix': '" + request.term + "' }",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                // dataFilter: function (data) { return data; },
                success: function (data) {
                    //$("#json").html(data.d);
                    response($.map(data.d, function (item) {
                        return {
                            //label: item.value,
                            value: item.value,
                            ville:item.ville,
                            CP: item.CP,
                        }
                    }))

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
        },
        minLength: 2,
        select: function (event, ui) {

            var e = document.getElementById('hid_insee');
            e.value = ui.item.CP;

            var e = document.getElementById('hid_Ville');
            e.value = ui.item.ville;
            //alert(Error);
            //$("#hid_insee").html(ui.item.Insee);

        },
        open: function () {
            $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },
        close: function () {
            $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        },
        width: "600px"
    }).data("autocomplete")._renderItem = function (ul, item) {
        return $("<li></li>")
			    .data("item.autocomplete", item)
			    .append("<a>" + item.value + "</a>")
			    .appendTo(ul);
    };
});

