﻿function TreeViewinit(p_thisInherits) {
    thisInherits = p_thisInherits.replace("_aspx", "");

    var TothisInherits = GettoUpperCaseStr(thisInherits);

    var divTreeView = document.getElementById("divTreeView");

    var execBigStr = TothisInherits + ".GetBigClass()";
    var result = eval(execBigStr);
    if (result != null && result.value != null && result != "") {

        var bigClassObjID = "";
        var bigClassName = "";

        var smallClassObjID = "";
        var smallClassName = "";

        //遍历大类，根据行数构造row
        for (i = 0; i < result.value.Rows.length; i++) {
            //创建行
            bigClassObjID = result.value.Rows[i].ObjID;
            bigClassName = result.value.Rows[i].ClassName;

            var tabbig = document.createElement("table");
            tabbig.id = "tabbig" + i.toString();
            tabbig.cellpadding = 0;
            tabbig.cellspacing = 0;
            tabbig.width = 196;
            tabbig.height = 30;
            //tabbig.style.borderBottom
            CreateBigClassRow(tabbig, bigClassName, "cellbig" + i.toString());
            divTreeView.appendChild(tabbig);

            var execSmallStr = TothisInherits + ".GetSmallClass(bigClassObjID)";
            var resultsmall = eval(execSmallStr);
            if (resultsmall != null && resultsmall.value != null && resultsmall != "") {

                var tabsmall = document.createElement("table");
                tabsmall.id = "tabsmall" + i.toString();
                tabsmall.cellpadding = 0;
                tabsmall.cellspacing = 0;
                tabsmall.width = 196;

                //遍历小类，然后构造row
                for (j = 0; j < resultsmall.value.Rows.length; j++) {
                    smallClassObjID = resultsmall.value.Rows[j].ObjID;
                    smallClassName = resultsmall.value.Rows[j].ClassName;
                    CreateSmallClassRow(tabsmall, smallClassName, smallClassObjID);
                    divTreeView.appendChild(tabsmall);

                }
                tabsmall.style.display = "none";

            }

            var tabtreeline = document.createElement("table");
            var rowtreeline = tabtreeline.insertRow();
            var celltreeline = rowtreeline.insertCell();
            celltreeline.height = 1;
            celltreeline.width = 190;
            celltreeline.align = "center";
            celltreeline.innerHTML = "<img src='../Pic/treeviewline.gif' border=0 />";
            divTreeView.appendChild(tabtreeline);
        }
    }
}

function CreateBigClassRow(p_table, p_text, cellname) {
    var row = p_table.insertRow();
    var cell = row.insertCell();
    cell.innerHTML = "<img src='../Pic/dian2.jpg' />&nbsp;&nbsp;" + p_text;
    cell.id = cellname;
    cell.style.paddingLeft = 18;
    cell.style.cursor = "hand";
    cell.style.verticalAlign = "middle";
    cell.className = "treeview";

    row.attachEvent("onclick", menuChange);
}

function CreateSmallClassRow(p_table, p_text, p_smallclsID) {
    var row = p_table.insertRow();
    var cell = row.insertCell();
    var a = document.createElement("a");
    a.href = "ListProducts.aspx?SmallClassID=" + p_smallclsID + "&page=0";
    a.style.textDecoration = "none";
    cell.innerHTML = "<img src='../Pic/point.gif' border=0 />&nbsp;&nbsp;" + p_text;
    cell.applyElement(a, "inside");
    cell.height = 25;
    cell.style.verticalAlign = "middle";
    cell.style.paddingLeft = 33;
    cell.className = "treeviewsmall";
}

function menuChange() {
    var row = event.srcElement;
    if (row != null) {
        var bigid = row.id;
        var smallid = bigid.replace("cellbig", "tabsmall");

        var menu = document.getElementById(smallid);
        if (menu != null) {
            if (menu.style.display == "") {
                menu.style.display = "none";
            } else {
                menu.style.display = "";
            }
        }
    }
}

function GettoUpperCaseStr(thisInherits) {
    var tothisInherits = "";
    var strleg = thisInherits.length;
    var xhxwz = 0;
    var str1 = "";
    var str2 = "";
    str1 = thisInherits.substring(0, 1).toUpperCase();
    xhxwz = thisInherits.indexOf("_");
    str2 = thisInherits.substr(xhxwz + 1, 1).toUpperCase();
    tothisInherits = str1 + thisInherits.substr(1, xhxwz - 1) + "_" + str2 + thisInherits.substr(xhxwz + 2, thisInherits.length);
    return tothisInherits;
}
