/**********/
var _dis = ".user_district";

var elm_provinces = "<select name='district_user_provinces' onchange='get_city_district(this)' id='district_user_provinces'><option value='-1'>所在省份</option></select>";               /*省*/
var elm_city = "<select name='district_user_city' onchange='get_county_district(this)' id='district_user_city'><option value='-1'>所在城市</option></select>";                          /*市*/
var elm_county = "<select name='district_user_county' onchange='get_village_district(this)' id='district_user_county'><option value='-1'>所在县/区</option></select>";                  /*县*/
var elm_village = "<select name='district_user_village' id='district_user_village'><option value='-1'>所在乡镇</option></select>";                                                      /*村*/

/** 初始化数据列表 ***/
function district_init() {
    $.ajax({
        url: "/api/jsoncity.aspx?op=" + Math.random(),
        error: function (p) { console.log(p); $(_dis).html(""); },
        dataType: "json",
        success: function (jon) {
            var provinces_html = "<option value='-1'>所在省份</option>";
            $(jon).each(function (i, d) { provinces_html += "<option value='" + d.ID + "'>" + d.name + "</option>"; })
            $(_dis).append(elm_provinces);
            $("#district_user_provinces").html(provinces_html);
        }
    })
}
/** 获取到城市 ***/
function get_city_district(slf) {
    $("#district_user_city").remove();
    $("#district_user_county").remove();
    $("#district_user_village").remove();
    var select_id = $(slf).val();
    if (select_id == -1) { return; }
    $.ajax({
        url: "/api/jsoncity.aspx?op=" + Math.random(),
        error: function (p) { console.log(p); $(_dis).html(""); },
        data: { "ui": select_id },
        dataType: "json",
        success: function (jon) {

            var city_html = "<option value='-1'>所在城市</option>";
            $(jon).each(function (i, d) { city_html += "<option value='" + d.ID + "'>" + d.name + "</option>"; })
            if ($(jon).size() > 0) {
                $(_dis).append(elm_city);
                $("#district_user_city").html(city_html);
            }

        }
    })
}
/** 获取到区/县 ***/
function get_county_district(slf) {
    $("#district_user_county").remove();
    $("#district_user_village").remove();
    var select_id = $(slf).val();
    if (select_id == -1) { return; }
    $.ajax({
        url: "/api/jsoncity.aspx?op=" + Math.random(),
        error: function (p) { console.log(p); $(_dis).html(""); },
        data: { "ui": select_id },
        dataType: "json",
        success: function (jon) {
            var county_html = "<option value='-1'>所在县/区</option>";
            $(jon).each(function (i, d) { county_html += "<option value='" + d.ID + "'>" + d.name + "</option>"; })
            if ($(jon).size() > 0) {
                $(_dis).append(elm_county);
                $("#district_user_county").html(county_html);
            }
        }
    })
}

/** 获取到村/镇 ***/
function get_village_district(slf) {
    $("#district_user_village").remove()
    var select_id = $(slf).val();
    if (select_id == -1) { return; }
    $.ajax({
        url: "/api/jsoncity.aspx?op=" + Math.random(),
        error: function (p) { console.log(p); $(_dis).html(""); },
        data: { "ui": select_id },
        dataType: "json",
        success: function (jon) {
            var village_html = "<option value='-1'>所在乡镇</option>";
            $(jon).each(function (i, d) { village_html += "<option value='" + d.ID + "'>" + d.name + "</option>"; })
            if ($(jon).size() > 0) {
                $(_dis).append(elm_village);
                $("#district_user_village").html(village_html);
            }
        }
    })
}

$(document).ready(function () {

    AddFile("/misc/css/common_district.css", "css");
    district_init();

})

function AddFile(path, tp) {
    if (tp == "js") {
        var fileref = document.createElement('script');
        fileref.setAttribute("type", "text/javascript");
        fileref.setAttribute("src", path);
        document.getElementsByTagName("head")[0].appendChild(fileref);
    }
    if (tp == "css") {
        var fileref = document.createElement('link');
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", path);
        fileref.setAttribute("rel", "stylesheet");
        document.getElementsByTagName("head")[0].appendChild(fileref);
    }
}