/*脚本说明：数据绑定实用工具*/

var CtrlType = new Object();
    CtrlType.LoopUp = "下拉关联列表";
    CtrlType.TextBox = "微软编辑框";
    CtrlType.Label = "标签";
    CtrlType.CheckBox = "选择框";
    CtrlType.Unknown = "未知";

function DataBindingUtils()
{
}

DataBindingUtils.GetControlById = function(id) {
    var oEdit = null;
    if (oEdit == "undefined" || !oEdit) {
        oEdit = document.getElementById(id);
    }
    return oEdit;
}

DataBindingUtils.GetCtrlType = function(oEdit) {
    if (oEdit == null || oEdit == "undefined")
        return CtrlType.Unknown;
    if (typeof (oEdit.id) != "undefined")//LoopUp,TextBox,CheckBox,Label
    {
        if (typeof (oEdit.options) != "undefined")
            return CtrlType.LoopUp;
        else if (typeof (oEdit.checked) != "undefined" && oEdit.type != "undefined" && oEdit.type != "text" && oEdit.type != "hidden")//加入一个判断确认checkBox和textBox(input)
            return CtrlType.CheckBox;
        else if (typeof (oEdit.value) != "undefined")
            return CtrlType.TextBox;
        else if (typeof (oEdit.innerText) != "undefined")
            return CtrlType.Label;
    }

    alert("Unknown Control Type!");
    return CtrlType.Unknown;
}

DataBindingUtils.SetCtrlText = function(oEditId, text) {
    var oEdit = DataBindingUtils.GetControlById(oEditId);
    var ctrlType = DataBindingUtils.GetCtrlType(oEdit);

    if (oEdit == null)
        return;

    switch (ctrlType) {
        case CtrlType.LoopUp:
            for (var i = 0; i < oEdit.options.length; i++) {
                if (oEdit.options[i].text == text) {
                    oEdit.options[i].selected = true;
                    break;
                }
            }
            break;
        case CtrlType.Label:
            oEdit.innerText = text;
        case CtrlType.CheckBox:
            oEdit.checked = text;
        default:
            oEdit.value = text;
    }
}

DataBindingUtils.SetCtrlValue = function(oEditId, value) {
    var oEdit = DataBindingUtils.GetControlById(oEditId);
    var ctrlType = DataBindingUtils.GetCtrlType(oEdit);

    if (oEdit == null)
        return;

    switch (ctrlType) {
        case CtrlType.LoopUp:
            for (var i = 0; i < oEdit.options.length; i++) {
                if (oEdit.options[i].value == value) {
                    oEdit.options[i].selected = true;
                }
                else {
                    oEdit.options[i].selected = false;
                }
            }
            break;
        case CtrlType.Label:
            oEdit.innerText = value;
        case CtrlType.CheckBox:
            oEdit.checked = value;
        default:
            oEdit.value = value;
    }
}

DataBindingUtils.GetCtrlText = function(oEditId) {
    var result;
    var oEdit = DataBindingUtils.GetControlById(oEditId);
    var ctrlType = DataBindingUtils.GetCtrlType(oEdit);

    if (oEdit == null)
        return;
    switch (ctrlType) {
        case CtrlType.LoopUp: /*同步TextField和ValueField*/
            if (oEdit.selectedIndex > -1)//select
            {
                result = oEdit.options[oEdit.selectedIndex].text;
            }
            break;
        case CtrlType.Label:
            result = oEdit.innerText;
            break;
        case CtrlType.CheckBox:
            result = oEdit.value;
            break;
        default:
            result = oEdit.value;
    }
    return result;
}

DataBindingUtils.GetCtrlValue = function(oEditId) {
    var result;
    var oEdit = DataBindingUtils.GetControlById(oEditId);
    var ctrlType = DataBindingUtils.GetCtrlType(oEdit);

    if (oEdit == null)
        return;
    switch (ctrlType) {
        case CtrlType.LoopUp: /*同步TextField和ValueField*/
            if (oEdit.selectedIndex > -1) {
                result = oEdit.options[oEdit.selectedIndex].value;
            }
            break;
        case CtrlType.Label:
            result = oEdit.innerText;
            break;
        case CtrlType.CheckBox:
            result = oEdit.checked;
            break;
        default:
            result = oEdit.value;
    }

    return result;
}

DataBindingUtils.IsBlank = function(oEditId) {/*控件为空*/
    var text = DataBindingUtils.GetCtrlText(oEditId, false);
    if (text == "undefined" || text == null)
        return true;
    text = "" + text;
    if (text.isBlank())
        return true;
    return false;
}

DataBindingUtils.IsNumber = function(oEditId) {/*控件为空*/
    var text = DataBindingUtils.GetCtrlText(oEditId, false) + "";
    var reg = /^(-|\+)?\d+(\.\d+)?$/;
    var istrue = reg.test(text);
    return istrue;
}

DataBindingUtils.isDate = function(inputDate) {
    if (/(\d{4})-(\d{1,2})-(\d{1,2})/.test(inputDate)) {//判断日期格式
        var d = new Date(RegExp.$1, Round(RegExp.$2) - 1, RegExp.$3);
        if (d.getFullYear() == RegExp.$1 && d.getMonth() + 1 == RegExp.$2 && d.getDate() == RegExp.$3)
        //判断日期逻辑(如2007-02-30则不合逻辑)
            return true;
        else
            alert('日期不对！')
        return false;
    }
    else {
        alert('日期格式不对!');
        return false;
    }
}

DataBindingUtils.CheckCode = function(oEditId) {/*控件*/
    var text = DataBindingUtils.GetCtrlText(oEditId, false);
    if (text == "undefined" || text == null)
        return true;
    text = "" + text;

    var reg = /[a-zA-Z0-9]/;
    var istrue = reg.test(text);
    return istrue;
}

DataBindingUtils.EmailCheck = function(s) {
    var str = s;
    var reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    var istrue = reg.test(str);

    return istrue;
}

DataBindingUtils.CheckPhone = function(s) {
    var str = s;
    var reg = /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/;
    var istrue = reg.test(str);
    return istrue;
}

DataBindingUtils.CheckIdNO = function(idtype, idno) {
    var str = idno;
    var reg;
    var istrue;
    if (idtype == "身份证") {
        reg = /^[1-9][0-9]{14}$|^[1-9]{1}[0-9]{16}[0-9xX]{1}$/;
        istrue = reg.test(str);
    }
    else {
        istrue = true;
    }
    return istrue;
}

/*将以逗号隔开的选项列表绑定到列表控件上去*/
DataBindingUtils.BindStringToListControl = function(oEdit, strSource, bDefault) {
    while (oEdit.options.length > 0) {
        oEdit.options.remove(oEdit.options[0]);
    }
    if (strSource == null)
        return;

    var objSource = strSource.split(",");
    var i = 0;
    for (; i < objSource.length; i++) {
        oEdit.options.add(new Option(objSource[i], objSource[i]));
    }
    if (i > 0 && (bDefault == true))
        oEdit.selectIndex = 0;
}

/*将数据集绑定到列表控件上去*/
DataBindingUtils.BindDataSetToListControl = function(oEdit, objDataSet, strTextField, strValueField, iTableIndex, bDefault) {
    while (oEdit.options.length > 0) {
        oEdit.options.remove(oEdit.options[0]);
    }
    if (objDataSet == null)
        return;
    if (iTableIndex == null)
        iTableIndex = 0;
    if (strTextField == null)
        strTextField = "name";
    if (strValueField == null)
        strValueField = "id";
    var table = objDataSet.Tables[iTableIndex];
    var i = 0;
    for (; i < table.Rows.length; i++) {
        var Text = table.Rows[i][strTextField];
        var Value = table.Rows[i][strValueField];
        oEdit.options.add(new Option(Text, Value));
    }
    if (i > 0 && (bDefault == true))
        oEdit.selectedIndex = 0;
}

/*将数据集绑定到列表控件上去*/
DataBindingUtils.BindListControlDsById = function(oEditId, objDataSet, strTextField, strValueField, iTableIndex, bDefault) {
    var oEdit = DataBindingUtils.GetControlById(oEditId);
    if (oEdit != null) {
        DataBindingUtils.BindDataSetToListControl(oEdit, objDataSet, strTextField, strValueField, iTableIndex, bDefault);
    }
}

DataBindingUtils.GetControlId = function(oEdit) {
    if (oEdit == null)
        return "";
    if (typeof (oEdit.UniqueId) != "undefined") {
        return oEdit.UniqueId;
    }
    else if (typeof (oEdit.uniqueId) != "undefined") {
        return oEdit.uniqueId;
    }
    else if (typeof (oEdit.Id) != "undefined")
        return oEdit.Id;
    else
        return oEdit.id;
}
