﻿var LocationDropDownList = {
    IsFirst: true,
    DefaultValue: 0,
    //五级下拉菜单编号
    LocationIds: [
            { id: "ProvinceLoaction", text: "请选择省份" },
            { id: "CityLoaction", text: "请选择地/市" },
            { id: "CountyLoaction", text: "请选择区/县" },
            { id: "TownLocation", text: "请选择乡镇/街道" },
            { id: "VillageLocation", text: "请选择村" }
            ],
    //省
    Province: function()
    {
        this.Init(0);
        if (this.DefaultValue == 0)
        {
            this.DataChange(-1, 0, 0);
        }
        else
        {
            this.DataChange(-1, 0, this.GetPlaceValue(1));
        }
    },
    //地市
    City: function()
    {
        if (!this.IsHaveObj(0)) return;
        this.Init(1);
        if (this.DefaultValue != 0 && this.DefaultValue.toString().substr(0, 2) != "00")
        {
            this.DataChange(0, this.GetPlaceValue(1), this.GetPlaceValue(2));
        }
    },
    //区县
    County: function()
    {
        if (!this.IsHaveObj(1)) return;
        this.Init(2);
        if (this.DefaultValue != 0 && this.DefaultValue.toString().substr(2, 2) != "00")
        {
            this.DataChange(1, this.GetPlaceValue(2), this.GetPlaceValue(3));
        }
    },
    //乡街道
    Town: function()
    {
        if (!this.IsHaveObj(2)) return;
        this.Init(3);
        if (this.DefaultValue != 0 && this.DefaultValue.toString().substr(4, 2) != "00")
        {
            this.DataChange(2, this.GetPlaceValue(3), this.GetPlaceValue(4));
        }
    },
    //村
    Village: function()
    {
        if (!this.IsHaveObj(3)) return;
        this.Init(4);
        if (this.DefaultValue != 0 && this.DefaultValue.toString().substr(6, 2) != "00")
        {
            this.DataChange(3, this.GetPlaceValue(4), this.GetPlaceValue(5));
        }
    },
    Init: function(index)
    {
        var str = '<select id="' + this.LocationIds[index].id + '"';
        if (typeof (this.LocationIds[index].width) == 'number')
        {
            str += ' style="width:' + this.LocationIds[index].width + 'px"';
        }
        str += ' onchange="LocationDropDownList.DataChange(' + index + ',this.value,0)"';
        str += '><option value="0">' + this.LocationIds[index].text + '</option></select>';
        document.write(str);
    },
    GetPlaceValue: function(index)
    {
        var str = this.DefaultValue.toString().substr(0, index * 2);
        for (var i = 0; i < (5 - index); i++)
        {
            str += "00";
        }
        return str;
    },
    IsHaveObj: function(index)
    {
        var obj = $("#" + this.LocationIds[index].id);
        if (typeof (obj.get(0)) == "undefined")
        {
            return false;
        }
        else
        {
            return obj;
        }
    },
    DataChange: function(index, parentId, value)
    {
        index++;
        var obj = this.IsHaveObj(index);
        if (!obj) return;
        for (var i = index; i < this.LocationIds.length; i++)
        {
            var tempObj = this.IsHaveObj(i);
            if (!tempObj) break;
            tempObj.get(0).options.length = 1;
        }
        if (parentId == 0 && index != 0) return;

        var url = "/Location/GetList";
        //obj.find("option:first").text("正在加载...");
        obj.html("<option value='0'>正在加载...</option>");
        var tempText = LocationDropDownList.LocationIds[index].text;
        jQuery.getJSON(url, { parentId: parentId }, function(result)
        {
            //obj.find("option:first").text(tempText);
            obj.html("<option value='0'>"+ tempText +"</option>");
            if (result.length == 0)
            {
                return;
            }

            for (var i = 0; i < result.length; i++)
            {
                var option = $('<option value="' + result[i].Id + '">' + result[i].Name + '</option>').appendTo(obj);
            }
            obj.val(value);
        });
    },
    GetValue: function()
    {
        var value = 0;
        for (var i = this.LocationIds.length - 1; i >= 0; i--)
        {
            var obj = this.IsHaveObj(i);
            if (!obj) continue;
            if (obj.val() != 0)
            {
                value = obj.val();
                break;
            }
        }
        return value;
    },
    DisableAll: function(disabled)
    {
        for (var i = 0; i < this.LocationIds.length; i++)
        {
            var tempObj = this.IsHaveObj(i);
            if (!tempObj) break;
            if (disabled)
            {
                tempObj.attr("disabled", "disabled");
            }
            else
            {
                tempObj.removeAttr("disabled");
            }
        }
    }
}

/////////////////////////////////////////////
//
/////////////////////////////////////////////
var LocationDropDownList1 = {
    IsFirst: true,
    DefaultValue: 0,
    //五级下拉菜单编号
    LocationIds: [
            { id: "ProvinceLoaction1", text: "请选择省份" },
            { id: "CityLoaction1", text: "请选择地/市" },
            { id: "CountyLoaction1", text: "请选择区/县" },
            { id: "TownLocation1", text: "请选择乡镇/街道" },
            { id: "VillageLocation1", text: "请选择村" }
            ],
    //省
    Province: function() {
        this.Init(0);
        if (this.DefaultValue == 0) {
            this.DataChange(-1, 0, 0);
        }
        else {
            this.DataChange(-1, 0, this.GetPlaceValue(1));
        }
    },
    //地市
    City: function() {
        if (!this.IsHaveObj(0)) return;
        this.Init(1);
        if (this.DefaultValue != 0 && this.DefaultValue.toString().substr(0, 2) != "00") {
            this.DataChange(0, this.GetPlaceValue(1), this.GetPlaceValue(2));
        }
    },
    //区县
    County: function() {
        if (!this.IsHaveObj(1)) return;
        this.Init(2);
        if (this.DefaultValue != 0 && this.DefaultValue.toString().substr(2, 2) != "00") {
            this.DataChange(1, this.GetPlaceValue(2), this.GetPlaceValue(3));
        }
    },
    //乡街道
    Town: function() {
        if (!this.IsHaveObj(2)) return;
        this.Init(3);
        if (this.DefaultValue != 0 && this.DefaultValue.toString().substr(4, 2) != "00") {
            this.DataChange(2, this.GetPlaceValue(3), this.GetPlaceValue(4));
        }
    },
    //村
    Village: function() {
        if (!this.IsHaveObj(3)) return;
        this.Init(4);
        if (this.DefaultValue != 0 && this.DefaultValue.toString().substr(6, 2) != "00") {
            this.DataChange(3, this.GetPlaceValue(4), this.GetPlaceValue(5));
        }
    },
    Init: function(index) {
        var str = '<select id="' + this.LocationIds[index].id + '"';
        if (typeof (this.LocationIds[index].width) == 'number') {
            str += ' style="width:' + this.LocationIds[index].width + 'px"';
        }
        str += ' onchange="LocationDropDownList1.DataChange(' + index + ',this.value,0)"';
        str += '><option value="0">' + this.LocationIds[index].text + '</option></select>';
        document.write(str);
    },
    GetPlaceValue: function(index) {
        var str = this.DefaultValue.toString().substr(0, index * 2);
        for (var i = 0; i < (5 - index); i++) {
            str += "00";
        }
        return str;
    },
    IsHaveObj: function(index) {
        var obj = $("#" + this.LocationIds[index].id);
        if (typeof (obj.get(0)) == "undefined") {
            return false;
        }
        else {
            return obj;
        }
    },
    DataChange: function(index, parentId, value) {
        index++;
        var obj = this.IsHaveObj(index);
        if (!obj) return;
        for (var i = index; i < this.LocationIds.length; i++) {
            var tempObj = this.IsHaveObj(i);
            if (!tempObj) break;
            tempObj.get(0).options.length = 1;
        }
        if (parentId == 0 && index != 0) return;

        var url = "/Location/GetList";
        //obj.find("option:first").text("正在加载...");
        obj.html("<option value='0'>正在加载...</option>");
        var tempText = LocationDropDownList.LocationIds[index].text;
        jQuery.getJSON(url, { parentId: parentId }, function(result) {
            //obj.find("option:first").text(tempText);
            obj.html("<option value='0'>" + tempText + "</option>");
            if (result.length == 0) {
                return;
            }

            for (var i = 0; i < result.length; i++) {
                var option = $('<option value="' + result[i].Id + '">' + result[i].Name + '</option>').appendTo(obj);
            }
            obj.val(value);
        });
    },
    GetValue: function() {
        var value = 0;
        for (var i = this.LocationIds.length - 1; i >= 0; i--) {
            var obj = this.IsHaveObj(i);
            if (!obj) continue;
            if (obj.val() != 0) {
                value = obj.val();
                break;
            }
        }
        return value;
    },
    DisableAll: function(disabled) {
        for (var i = 0; i < this.LocationIds.length; i++) {
            var tempObj = this.IsHaveObj(i);
            if (!tempObj) break;
            if (disabled) {
                tempObj.attr("disabled", "disabled");
            }
            else {
                tempObj.removeAttr("disabled");
            }
        }
    }
}