﻿		            /**
		             * Class MyTable
		             * -------------
		             * Usage: 
		             *  key -- obj
		             */
		            function MyTable() {
		                this.key = new Array();
		                this.obj = new Array();
		                this.length = 0;
		            }
		            
		            // add an item into MyTable:
		            MyTable.prototype.add = function(key, obj) {
		                try {
		                    this.key.push(key);
		                    this.obj.push(obj);
		                    // remember the length of MyTable:
		                    this.length++;
		                } catch (e) {
		                    alert(e);
		                }
		            }
		            
		            // 判断key 是否在this.key中
		            MyTable.prototype.isExsist = function(key) {
		                try {
		                    if (this.findIndexByKey(key) >= 0)
		                        return true;
		                    else
		                        return false;
		                } catch (e) {
		                    alert("errors occured in \n" + this.isExsist + "\n>> " + e);
		                    return false;
		                }
		            }
		            
		            // 查找this.key == key 的第一个索引
		            MyTable.prototype.findIndexByKey = function(key) {
		                // 查找到this.key为key的记录
		                var index;
		                for (index = 0; index < this.length; index++) {
		                    if (key == this.key[index]) {  // 找到，跳出
		                        return index;
		                    }
		                }
		                
		                if (index >= this.length) { //没有找到
		                    return -1;
		                }
		            }		            
	            
		            // edit an item:
		            MyTable.prototype.edit = function(key, obj) {
		                // 查找到this.key为key的记录
		                var index;
		                for (index = 0; index < this.length; index++) {
		                    if (key == this.key[index]) {  // 找到，跳出
		                        break;
		                    }
		                }
		                
		                if (index >= this.length) { //没有找到
		                    this.add(key, obj);
		                    return false;
		                } else {            // 找到，开始修改
		                    this.obj[index] = obj;
		                    return true;
		                }
		            }
		            
		            // clear All:
		            MyTable.prototype.clearAll = function() {
		                for (; this.length > 0; this.length--) {
		                    this.key.pop();
		                    this.obj.pop();
		                }
		            }
		            
		            /*
		             * Function 
		             * ---------
		             * Usage:
		             *  select T.index
		             *      from MyTable T
		             *      where destKey = T.key
		             */
		            MyTable.prototype.findIndexes = function(destKey) {
		                // an array for return:
		                var indexes = new Array();
		                
		                try {
		                    // begin find in the sequence way:
		                    for (var i = 0; i < this.length; i++) {
		                        if (destKey == this.key[i]) {
		                            // remember the index:
		                            indexes.push(i);
		                        }
		                    }
		                } catch (e) {
		                    alert(e);
		                }
		                
		                return indexes;
		            }
		            
		            /*
		             * Function
		             * --------
		             * Usage: 
		             *  select T.key T.obj
		             *      from MyTable T
		             *      where destKey = T.key
		             */
		             MyTable.prototype.findSubTable = function(destKey) {
		                var subtable = new MyTable();
		                var indexes = new Array();
		                
		                try {
		                    indexes = this.findIndexes(destKey);
		                    for (var i = 0; i < indexes.length; i++) {
		                        subtable.add(this.key[indexes[i]], this.obj[indexes[i]]);
		                    }    
		                } catch(e) {
		                    alert(e);
		                }
		                return subtable;
		             }
		             
		            // search objs by key
		            MyTable.prototype.findObj = function(destKey) {
		                var index = this.findIndexByKey(destKey);
		                if (index >= 0) {
		                    return this.obj[index];
		                    
		                } else {
		                    return "";
		                }
		            }
		            /** end Class MyTable */		            

		                
		            /**
		             * Class MySelect
		             */
                    function MySelect(selectObj) {
                        try {
                            this.selectObj = selectObj;
                        } catch (e) {
                            alert(e);
                        }
                    }
                    
                    // add an option into a specified select box in the end.
                    MySelect.prototype.addOption = function(optionStr, optionValue) {
                        try {
                            var newIndex = this.selectObj.options.length;
                            this.selectObj.options[newIndex] = new Option(optionStr, optionValue);
                        } catch(e) {
                            alert("errors occured in \n" + this.addOption + "\n>> " + e);
                        }
                    }
                    
                    // fill options with a table:
                    MySelect.prototype.fillOptions = function(mytable) {
                        try {
                            for (var i = 0; i < mytable.length; i++) {
                                this.addOption(mytable.obj[i], mytable.key[i]);
                            }
                        } catch (e) {
                            alert(e);
                        }
                    }
                    
                    // clear all options:
                    MySelect.prototype.clear = function() {
                        try {
                            this.selectObj.length = 0;
                        } catch (e) {
                            alert("errors occured in \n" + this.clear + "\n>> " + e);
                        }
                    }
                    
                    // set width:
                    MySelect.prototype.setWidth = function(width) {
                        try {
                            this.selectObj.style.width = width;
                        } catch(e) {
                            alert(e);
                        }
                    }
                    
                    // get Current selected option value:
                    MySelect.prototype.getSelectedValue = function() {
                        var selValue;
                        
                        try {
                            selValue = this.selectObj.value;  
                            return selValue;
                        } catch (e) {
                            alert(e);
                        }
                    }
                    
                    // select an option
                    MySelect.prototype.select = function(value) {
                        this.selectObj.value = value;
                    }
                    
                    /** end Class MySelect */
                    
                    /**
                     * Class RelationSelect2:
                     */
                    function RelationSelect2(s1, s2, t1, t2) {
                        this.tb1 = new MyTable();
                        this.tb2 = new MyTable();
                        try {
                            this.sel1 = new MySelect(s1);
                            this.sel2 = new MySelect(s2);
                            this.tb1 = t1;
                            this.tb2 = t2;
                        } catch (e) {
                            alert(e);
                        }
                    }
                    
                    // init 2 select boxes:
                    RelationSelect2.prototype.init = function() {
                        this.sel1.clear;
                        this.sel1.setWidth("auto");
                        this.sel1.fillOptions(this.tb1);
                        this.connect();
                        try {
                            
                        } catch (e) {
                            alert(e);
                        }
                    }
                    
                    // connect 2 select boxes:
                    RelationSelect2.prototype.connect = function() {
                        try {
                            var curSelected = this.sel1.getSelectedValue();
                            this.sel2.clear();
                            this.sel2.setWidth("auto");
                            
                            var newTable = new MyTable();
                            newTable = this.tb2.findSubTable(curSelected);
                            // 更改于2007-3-31： 田杰。
                            // fillOption会导致所有二级下拉列表的value值全一样（其父下拉列表的选项值），为使其value值以1，2，3……这样来记，故将其改为一个循环。
                            //this.sel2.fillOptions(newTable);
                            for (var i = 0; i < newTable.length; i++) {
                                this.sel2.addOption(newTable.obj[i], i+1);
                            }
                            // 更改完毕
                        } catch (e) {
                            alert(e);
                        }
                    }
                    // 
                    /** end Class RelationSelect2 */