﻿var Industry = {
Init: function() {
$("#ddlIndustryBigList").empty();
        $("<option></option>").val("").text("--请选择--").appendTo($("#ddlIndustryBigList"));
        $.each(industryData, function(i, item) {
            if (item["pid"].toString() == "0") {
                $("<option></option>")
                    .val(item["id"])
                    .text(item["name"])
                    .appendTo($("#ddlIndustryBigList"));
            }
        })
        var text = $("#" + IndustryIDHiddenFieldClientID).val();
        if (industryCurrentData == null && text != "") {
            industryCurrentData = this.ParseJson(text);

        }
        if (industryCurrentData != null) {
            $("#ddlSelectIndustryBigList").empty();
            $.each(industryCurrentData, function(i, item) {
                $("<li></li>")
                    .attr("id", item["id"])
                    .attr("text", item["name"])//get text
                    .attr("value", item["id"])//get value
                    .html("<img src='/Images/del.gif' onclick='Industry.Delete(" + item["id"] + ")'/>" + item["name"])
                    .appendTo($("#ddlSelectIndustryBigList"));
            })
        }
        this.SetValue();
    },
    ParseJson: function(text) {
        var arr1 = text.split('|');
        var json = "";
        for (var i = 0; i < arr1.length; i++) {
            var arr2 = arr1[i].split(',');
            json += "{\"id\":" + arr2[1] + ",\"name\":" + "\"" + arr2[0] + "\"},";
        }
        if (json != "")
            return eval("[" + json.substr(0, json.length - 1) + "]"); //you must change value is object type, use 'eval' function 
        else
            return null;
    },
    GetSub: function(sender) {
        $("#ddlSubIndustryBigList").empty();
        $.each(industryData, function(i, item) {
            if (item["pid"].toString() == sender.value) {
                $("<li></li>")
                    .val(item["id"])
                    .html("<input type='checkbox' id='c" + item["id"] + "' value='" + item["id"] + "' text='" + item["name"] + "' onclick='Industry.Check(this)'/>" + item["name"])
                    .appendTo($("#ddlSubIndustryBigList"));
            }
        })
    },
    Check: function(sender) {
        var ele = $("#ddlSelectIndustryBigList>li");
        var c = IndusTryMaxCount + 1;
        if (ele.length > IndusTryMaxCount) {
            alert("提示：最大条数为" + c + "!");
            sender.checked = false;
            return;
        }
        //check box is checked and have some item in item container
        if (sender.checked && $("#" + sender.value).text() == "") {
            $("<li></li>")
                    .val(sender.value)
                    .attr("id", sender.value)
                    .attr("text", sender.text)//get text
                    .attr("value", sender.value)//get value
                    .html("<img src='/Images/del.gif' onclick='Industry.Delete(" + sender.value + ")'/>" + sender.text)
                    .appendTo($("#ddlSelectIndustryBigList"));

            this.SetValue();
        } else {
            alert("提示：请选择或已经存在");
            //Delete(sender.value);
            sender.checked = false;
            return;
        }
    },
    Delete: function(id) {
        if ($("#c" + id).val() != "" && $("#c" + id).get(0) != undefined)
            $("#c" + id).get(0).checked = false;
        $("#ddlSelectIndustryBigList #" + id).remove();
        this.SetValue();
    },
    SetValue: function() {
        var ele = $("#ddlSelectIndustryBigList>li");
        var keyvalue = "";
        var names = "";
        var json = "";
        $.each(ele, function(i, item) {
            names += item.text + ",";
            keyvalue += item.text + "," + item.value + "|";
            json += "{\"id\":" + item.value + ",\"name\":" + "\"" + item.text + "\"},";
        });
        if (json != "")
            industryCurrentData = eval("[" + json.substr(0, json.length - 1) + "]"); //you must change value is object type, use 'eval' function 
        else
            industryCurrentData = null;

        if (names != "")
            $("#" + industryControlClientID).val(names.substr(0, names.length - 1));
        else
            $("#" + industryControlClientID).val("");

        if (keyvalue != "")
            $("#" + IndustryIDHiddenFieldClientID).val(keyvalue.substr(0, keyvalue.length - 1));
        else
            $("#" + IndustryIDHiddenFieldClientID).val("");
    }
}
