/**
 * 数据字典
 */
function Dd(_groupid, _id, _setvalue, _dispno, _relating, _shengid, _cityid) {
    /** 对应数据表(odo_setconst)中的同名字段 */
    this.groupid = _groupid;
    this.id = _id;
    this.setvalue = _setvalue;
    this.dispno = _dispno;
    this.relating = _relating;
    this.shengid = _shengid;
    this.cityid = _cityid;

    /** 组对象 */    
    this.group = null;
    
    /**
     * 输出option对象
     * @param isSelected 是否选中
     * @param isDisabled 是否可用
     */
    this.showOption = function (isSelected, isDisabled) {
        var setValue = (this.group.isuseindex == 1) 
                ? this.dispno : this.setvalue.replace(/\"/g, '\\"') ;
        document.write('<option value="' + setValue + '"');         if (isSelected) {             document.write(" selected");         }         if (isDisabled) {             document.write(" disabled");         }         document.writeln(">" + this.setvalue + "</option>");
    }
    
    /**
     * 输出radio对象
     * @param name radio对象的名称 
     * @param isSelected 是否选中
     * @param isDisabled 是否可用
     */
    this.showRadio = function (name, isSelected, isDisabled) {
        document.write('<input type="radio" value="' + this.dispno + '" ');         document.write('id="' + name + this.id + '" name="' + name + '" ');         if (isSelected) {             document.write(" checked");         }         if (isDisabled) {             document.write(" disabled");         }         var setValue = (this.group.isuseindex == 1)                  ? this.dispno : this.setvalue.replace(/\"/g, '\\"') ;         document.write(' value="' + setValue + '"/>');
        document.writeln('<label for="' + name + this.id + '">' + this.setvalue 
                + '</label>');
    }
    
    /**
     * 输出checkbox对象
     * @param name radio对象的名称
     * @param isSelected 是否选中
     * @param isDisabled 是否可用
     */
    this.showCheckBox = function (name, isSelected, isDisabled) {
        if (!document.getElementById(name)) {
            document.write('<input type="hidden" id="' + name                     + '" name="' + name + '"/>');
        }
        document.write('<input type="checkbox" value="' + this.dispno + '" ');         document.write('id="' + name + this.id + '" name="chk' + name + '" ');         if (isSelected) {             document.write(" checked");         }         if (isDisabled) {             document.write(" disabled");         }         var setValue = (this.group.isuseindex == 1)                  ? this.dispno : this.setvalue.replace(/\"/g, '\\"') ;         document.write(' value="' + setValue + '"/ onclick="ddCheckBoxClick(\''                 + name + '\')">');
        document.writeln('<label for="' + name + this.id + '">' + this.setvalue 
                + '</label>');
    }
}

/**
 * 数据字典组处理类
 */
function DdGroup(_id, _groupname, _isuseindex, _isdisp) {
    /** 对应数据表(odo_setconstgroup)中的同名字段 */
    this.id = _id;
    this.groupname = _groupname;
    this.isuseindex = _isuseindex;
    this.isdisp = _isdisp;
    
    /** 字典项 */
    this.ddList = new Array();
    
    /**
     * 添加一个字典项
     */
    this.addDd = function (oDd) {
        var index = this.ddList.length;
        this.ddList[index] = oDd;
        oDd.group = this;
    }
    
    /**
     * 输出一个select组件
     * @param name select对象的名称
     * @param selectedValue 选中值
     * @param isDisabled 是否可用
     */
    this.showSelect = function (name, selectedValue, isDisabled) {
        document.write('<select id="' + name + '" name="' + name + '"');
        if (isDisabled) {
            document.write(" disabled");
        }
        document.writeln('>');
        for (var i=0; i<this.ddList.length; i++) {
            if (this.isuseindex == 1) {
                var isSelected = (this.ddList[i].dispno == selectedValue);
            } else {
                var isSelected = (this.ddList[i].setvalue == selectedValue);
            }
            this.ddList[i].showOption(isSelected, isDisabled);
        }
        document.writeln('</select>');
    }
    
    /**
     * 输出radio对象
     * @param name radio对象的名称
     * @param selectedValue 选中值
     * @param isDisabled 是否可用
     */
    this.showRadio = function (name, selectedValue, isDisabled) {
        for (var i=0; i<this.ddList.length; i++) {
            if (this.isuseindex == 1) {
                var isSelected = (this.ddList[i].dispno == selectedValue);
            } else {
                var isSelected = (this.ddList[i].setvalue == selectedValue);
            }
            this.ddList[i].showRadio(name, isSelected, isDisabled);
        }
    }
    
    /**
     * 输出checkbox对象
     * @param name radio对象的名称
     * @param selectedValue 选中值
     * @param isDisabled 是否可用
     */
    this.showCheckBox = function (name, selectedValue, isDisabled) {
        for (var i=0; i<this.ddList.length; i++) {
            var isSelected = false;
            if (selectedValue) {
	            for (var j=0; j<selectedValue.length; j++) {
		            if (this.isuseindex == 1) {
		                if (this.ddList[i].dispno == selectedValue[j]) {
                            isSelected = true;
                            break;
                        }
		            } else {
                        if (this.ddList[i].setvalue == selectedValue[j]) {
	                        isSelected = true;
	                        break;
                        }
		            }
	            }
            }
            this.ddList[i].showCheckBox(name, isSelected, isDisabled);
        }
    }
}

function ddCheckBoxClick(name) {
    var oSelectedList = document.getElementById(name);
    if (!oSelectedList) {
        return false;
    }
    var checkBoxArray = document.getElementsByName("chk" + name);
    var valueArray = new Array();
    for (var i=0, j=0; i<checkBoxArray.length; i++) {
        var formElement = checkBoxArray[i];
        if (formElement.type == "checkbox" && formElement.checked) {
            valueArray[j] = formElement.value;
            j++;
        }
    }
    oSelectedList.value = valueArray.join("\t");
}