<!--#include file="const.asp"-->
<%
Dim classlist,Parentlist,childlist
Dim Node,CNode,Nodes,i,ii,iii,n,m
Dim classid,classname,depth,child,parentid,parentstr,m_strParent
Dim class_level_1,class_level_2,class_level_3
Dim iCount,iClass,NowClassID_1,NowClassID_2,NowClassID_3

iCount = NewAsp.ChkNumeric(Request.Querystring("count"))
iClass = NewAsp.ChkNumeric(Request.Querystring("class"))
NowClassID_1 = 0
NowClassID_2 = 0
NowClassID_3 = 0
Set classlist = Application(NewAsp.CacheName&"_classlist")
If Not classlist Is Nothing Then
	i = 0
	ii = 0
	iii = 0
	n = 0
	m = 0
	If iClass > 0 Then
		For Each Node in classlist.documentElement.SelectNodes("row[@classid="&iClass&"]")
			m_strParent = "," & Node.selectSingleNode("@classid").text & "," & Node.selectSingleNode("@parentstr").text & ","
		Next
		Node = Null
	End If
	For Each Node in classlist.documentElement.SelectNodes("row")
		classid = CLng(Node.selectSingleNode("@classid").text)
		depth = CLng(Node.selectSingleNode("@depth").text)
		parentid = CLng(Node.selectSingleNode("@parentid").text)
		parentstr = Node.selectSingleNode("@parentstr").text
		classname = Replace(Replace(Node.selectSingleNode("@classname").text, "'", ""), """", "")
		If depth = 0 Then
			If i = 0 Then
				class_level_1 = class_level_1 & vbCrLf & "new Array(""" & classid & """,""" & classname & """)" & vbCrLf
			Else
				class_level_1 = class_level_1 & ",new Array(""" & classid & """,""" & classname & """)" & vbCrLf
			End If
			If InStr(m_strParent,","&classid&",") > 0 And iClass > 0 Then
				NowClassID_1 = i
			End If
			i = i + 1
		End If
		If depth = 1 Then
			If ii = 0 Then
				class_level_2 = class_level_2 & vbCrLf & "new Array(""" & parentid & """,""" & classid & """,""" & classname & """)" & vbCrLf
			Else
				class_level_2 = class_level_2 & ",new Array(""" & parentid & """,""" & classid & """,""" & classname & """)" & vbCrLf
			End If
			If InStr(m_strParent,","&parentid&",") > 0 And iClass > 0 Then
				If InStr(m_strParent,","&classid&",") > 0 And iClass > 0 Then
					NowClassID_2 = n + iCount
				End If
				n = n + 1
			End If
			ii = ii + 1
		End If
		If depth = 2 Then
			If iii = 0 Then
				class_level_3 = class_level_3 & vbCrLf & "new Array(""" & parentid & """,""" & classid & """,""" & classname & """)" & vbCrLf
			Else
				class_level_3 = class_level_3 & ",new Array(""" & parentid & """,""" & classid & """,""" & classname & """)" & vbCrLf
			End If
			If InStr(m_strParent,","&parentid&",") > 0 And iClass > 0 Then
				If InStr(m_strParent,","&classid&",") > 0 And iClass > 0 Then
					NowClassID_3 = m + iCount
				End If
				m = m + 1
			End If
			iii = iii + 1
		End If
	Next
End If

If iCount=0 Or iClass=0 Then
	NowClassID_1 = 0
	iCount = 0
End If
NewAsp.CloseConn()
%>
//alert('<%=NowClassID_1%>,<%=NowClassID_2%>,<%=NowClassID_3%>');
if(typeof(oAskform)!='object')
oAskform = document.askedform

function getCidValue()
{
	var _cl1 = oAskform.classLevel1;
	var _cl2 = oAskform.classLevel2;
	var _cl3 = oAskform.classLevel3;
	var _cid = oAskform.cid;
	if(typeof(_cid)!='object'){
		var _cid = oAskform.classid;
	}
	if(_cl1.value!=0) _cid.value = _cl1.value;
	if(_cl2.value!=0) _cid.value = _cl2.value;
	if(_cl3.value!=0) _cid.value = _cl3.value;
	
}
var g_classLevel1;
var g_classLevel2;
var g_classLevel3;
 
var class_level_1=new Array(<%=class_level_1%>);
var class_level_2=new Array(<%=class_level_2%>);
var class_level_3=new Array(<%=class_level_3%>);
 
function FillclassLevel1(classLevel1)
{
	classLevel1.options[0] = new Option("aa", "0");
    for(i=0; i<class_level_1.length; i++) {
        classLevel1.options[i] = new Option(class_level_1[i][1], class_level_1[i][0]);
    }
    classLevel1.options[<%=NowClassID_1%>].selected = true;
    classLevel1.length = i;
}
 
function FillclassLevel2(classLevel2, class_level_1_id)
{
    classLevel2.options[0] = new Option("²»Ñ¡", "");
    count = <%=iCount%>;
    for(i=0; i<class_level_2.length; i++) {
        if(class_level_2[i][0].toString() == class_level_1_id) {
            classLevel2.options[count] = new Option(class_level_2[i][2], class_level_2[i][1]);
            count = count+1;
        }
    }
    classLevel2.options[0].selected = true;
    classLevel2.length = count;
}
 
function FillclassLevel3(classLevel3, class_level_2_id)
{
    classLevel3.options[0] = new Option("²»Ñ¡", "");
    count = <%=iCount%>;
    for(i=0; i<class_level_3.length; i++) {
        if(class_level_3[i][0].toString() == class_level_2_id) {
            classLevel3.options[count] = new Option(class_level_3[i][2], class_level_3[i][1]);
            count = count+1;
        }
    }
    classLevel3.options[0].selected = true;
    classLevel3.length = count;       
}
 
function classLevel2_onchange()
{
    FillclassLevel3(g_classLevel3, g_classLevel2.value); 
    if (g_classLevel3.length <= 1) {  
     g_classLevel3.style.display = "none";
	 document.getElementById("lastarrow").style.display = "none";
    }
    else {
     g_classLevel3.style.display = "";     
	 document.getElementById("lastarrow").style.display = "";	 
    }     
	getCidValue();
  
}
 
function classLevel1_onchange()
{
	FillclassLevel2(g_classLevel2, g_classLevel1.value);
    classLevel2_onchange();
    getCidValue();	
}
 
function InitclassLevelList(classLevel1, classLevel2, classLevel3)
{
	g_classLevel1=classLevel1;
    g_classLevel2=classLevel2;
    g_classLevel3=classLevel3;
    
    g_classLevel1.onchange = Function("classLevel1_onchange();");
    g_classLevel2.onchange = Function("classLevel2_onchange();");
    FillclassLevel1(g_classLevel1);
    
	var height = (g_classLevel1.options.length * 14 + (!(window.attachEvent && !window.opera) ? 30 : 10)) + 'px';
	g_classLevel1.style.height=height;
	g_classLevel2.style.height=height;
	g_classLevel3.style.height=height;
    classLevel1_onchange();
    
    
}
 
InitclassLevelList(oAskform.classLevel1, oAskform.classLevel2, oAskform.classLevel3);

// auto-select the init class if need

var selected_id_list="0"
var blank_pos = selected_id_list.indexOf(" ");
var find_blank = true;
if (blank_pos == -1) {
    find_blank = false;
    blank_pos = selected_id_list.length;
}
var id_str = selected_id_list.substr(0, blank_pos);
g_classLevel1.value = id_str;
classLevel1_onchange();

if (find_blank == true) {
    selected_id_list = selected_id_list.substr(blank_pos + 1, 
        selected_id_list.length - blank_pos - 1);
    blank_pos = selected_id_list.indexOf(" ");
    if (blank_pos == -1) {
        find_blank = false;
        blank_pos = selected_id_list.length;
    }
    id_str = selected_id_list.substr(0, blank_pos);
    g_classLevel2.value = id_str;
    classLevel2_onchange();

    if (find_blank == true) {
        selected_id_list = selected_id_list.substr(blank_pos + 1, 
            selected_id_list.length - blank_pos - 1);
        blank_pos = selected_id_list.indexOf(" ");
        if (blank_pos == -1) {
            find_blank = false;
            blank_pos = selected_id_list.length;
        }
        id_str = selected_id_list.substr(0, blank_pos);
        g_classLevel3.value = id_str;
    }
}
