var arrSorts = new Array(3);
arrSorts[0] = ['1', '1111', '0'];
arrSorts[1] = ['2', '111AB', '1'];
arrSorts[2] = ['3', '2222', '0'];
arrSorts[3] = ['4', '222A', '3'];

function initSorts(os1, os2) {    //初始化選項，主類別
    var s = "";
	s = s + "<select onchange=\"setSorts(this, '" + os1 + "', '" + os2 + "');\">";
	s = s + "<option value=\"\">Please Select→</option>";
	for (var i = 0; i < arrSorts.length; i++) {
	    if (arrSorts[i][2] == 0) {
		    s = s + "<option value=\"" + arrSorts[i][0] + "\">" + arrSorts[i][1] + "</option>";
		}
	}
	s = s + "</select>";
	
	document.getElementById(os2).innerHTML = s;
}

function setSorts(o, os1, os2) {
	//清除下級選項
	while (o.nextSibling) {
	    o.parentNode.removeChild(o.nextSibling);
	}
	
	//獲取當前選項 ID ，并設置選擇值
    var iValue = o.options[o.selectedIndex].value;
	if (iValue == "") {
		if (o.previousSibling) {
		    document.getElementById(os1).value = o.previousSibling.options[o.previousSibling.selectedIndex].value;
		}
		else {
		    document.getElementById(os1).value = 0;
		}
		return false;
	}
	document.getElementById(os1).value = iValue;
	
	//去掉原來的選擇狀態
	var ss1 = o.innerHTML;
	var ss2 = "";
	ss2 = ss1.replace(" selected=\"selected\"", "");
	
	//將選中的選項對應代碼更改為 selected
	var s = document.getElementById(os2).innerHTML;
	s = s.replace(ss1, ss2);
	s = s .replace("<option value=\"" + iValue + "\">", "<option value=\"" + iValue + "\" selected=\"selected\">");
	
	//新的下拉列表代碼
	var s1 = "";
	for (var i = 0; i < arrSorts.length; i++) {
	    if (arrSorts[i][2] == iValue) {
		    if (s1 == "") {
			    s1 = s1 + "<select onchange=\"setSorts(this, '" + os1 + "', '" + os2 + "');\">";
				s1 = s1 + "<option value=\"\">Please Select→</option>";
				s1 = s1 + "<option value=\"" + arrSorts[i][0] + "\">" + arrSorts[i][1] + "</option>";
			}
			else {
			    s1 = s1 + "<option value=\"" + arrSorts[i][0] + "\">" + arrSorts[i][1] + "</option>";
			}
		}
	}
	if (s1 != "") s1 = s1 + "</select>";
	
	//顯示代碼
	document.getElementById(os2).innerHTML = s + s1;
}

function newInit(os1, os2, sID) {
	if (sID == 0) {
	    initSorts(os1, os2);
		return false;
	}
	
    var pID;
	var s1 = "<select onchange=\"setSorts(this, '" + os1 + "', '" + os2 + "');\">";
	s1 = s1 + "<option value=\"\">Please Select→</option>";
	
	for (var i = 0; i < arrSorts.length; i++) {
	    if (sID == arrSorts[i][0]) {
		    pID = arrSorts[i][2];
			break;
		}
	}
	
	for (var i = 0; i < arrSorts.length; i++) {
	    if (arrSorts[i][2] == pID) {
		    if (arrSorts[i][0] == sID) {
			    s1 = s1 + "<option value=\"" + arrSorts[i][0] + "\" selected=\"selected\">" + arrSorts[i][1] + "</option>";
			}
			else {
			    s1 = s1 + "<option value=\"" + arrSorts[i][0] + "\">" + arrSorts[i][1] + "</option>";
			}
		}
	}
	
	s1 = s1 + "</select>";
	
	document.getElementById(os2).innerHTML = s1 + document.getElementById(os2).innerHTML;
	
	if (pID > 0) {
	    newInit(os1, os2, pID);
	}
}