var taskMsgForms = {};
var curTaskMsgInput = null;

/**
 * 表单类
 */
function TaskMsgForm() {
  this.id = null; //表单编号
  this.appCode = null; //表单对应的应用代码
  this.appDataId = null; //表单对应的应用数据编号
  this.getDataService = null; //获得数据的服务
  this.submitService = null; //提交数据的服务
  this.jQueryObj = null; //jQuery对象
  this.inputs = {}; //输入项
  this.inputIds = []; //输入项编号数组，排序用
  this.dataMember = null; //表单对应的数据源对象名
  this.dataSource = null; //数据源对象
  this.onSubmit = null; //提交完成之后需要执行的回调函数名称
  this.onInitFinish = null; //表单初始化之后调用的回调函数
  this.onLoadData = null; //加载数据前执行的回调函数
  this.onAfterLoadData = null; //加载数据后执行的回调函数
  this.isLoadedKindEditorCss = false; //是否已加载KindEditor控件的CSS文件
  this.isLoadedJeDateCss = false; //是否已加载jeDate控件的CSS文件

  var thisForm = this;

  /**
   * 初始化表单
   * @param  {[type]} obj 表单的jQuery对象
   */
  this.init = function (obj) {
    this.jQueryObj = obj;
    if (obj.attr("appCode")) this.appCode = obj.attr("appCode");
    if (obj.attr("appDataId")) this.appDataId = obj.attr("appDataId");
    if (obj.attr("getDataService")) this.getDataService = obj.attr("getDataService");
    if (obj.attr("submitService")) this.submitService = obj.attr("submitService");
    if (obj.attr("dataMember")) this.dataMember = obj.attr("dataMember");
    if (obj.attr("onSubmit")) this.onSubmit = obj.attr("onSubmit");
    if (obj.attr("onInitFinish")) this.onInitFinish = obj.attr("onInitFinish");
    if (obj.attr("onLoadData")) this.onLoadData = obj.attr("onLoadData");
    if (obj.attr("onAfterLoadData")) this.onAfterLoadData = obj.attr("onAfterLoadData");

    var haveInput = false;
    obj.find("input").each(function () {
      var ipt = new TaskMsgInput();
      ipt.parseFromTag(thisForm, $(this));
      haveInput = true;
    });

    if (haveInput) this.render();

    if (this.getDataService) {
      var service = new TaskMsgService(this.getDataService);
      service.request(null, function (req, res) {
        //if(haveInput) thisForm.render();
        thisForm.dataBind(req, res);
      });
    }

    if (this.onInitFinish) eval(this.onInitFinish + "(this)");
  };

  /**
   * 渲染表单，生成表单html代码
   */
  this.render = function () {
    this.jQueryObj.replaceWith("<ul id=\"" + this.id + "\" class=\"TaskMsgForm\">" +
      this.jQueryObj.html() + "</ul>");
    this.jQueryObj = $("#" + this.id);

    for (var i = 0; i < this.inputIds.length; i++) {
      var ipt = this.inputs[this.inputIds[i]];
      if (this.jQueryObj.find("#" + ipt.id).length > 0) {
        this.jQueryObj.find("#" + ipt.id).replaceWith("<li id=\"inputLi_" + ipt.id + "\"></li>");
      } else {
        this.jQueryObj.append("<li id=\"inputLi_" + ipt.id + "\"></li>");
      }
      ipt.jQueryObj = this.jQueryObj.find("#inputLi_" + ipt.id);
      ipt.render();
    }
  };

  /**
   * 绑定表单数据
   * @param  {[type]} res [description]
   */
  this.dataBind = function (req, res) {
    if (this.onLoadData) eval(this.onLoadData + "(req, res)");
    if (this.dataMember && res[this.dataMember]) {
      this.dataSource = res[this.dataMember];
    }
    for (var iptId in this.inputs) {
      var ipt = this.inputs[iptId];
      //if(ipt.getDataListService) continue;
      ipt.dataBind(res);
    }
    if (this.onAfterLoadData) eval(this.onAfterLoadData + "(req, res)");
  };

  /**
   * 解析表单数据
   * @return {[type]} [description]
   */
  this.parseData = function (service) {
    var args = {};
    for (var iptId in this.inputs) {
      var ipt = this.inputs[iptId];
      if (ipt.type == "submit") continue;
      var val = ipt.getValue();
      if (ipt.notNull && val!==0 && (!val || val == "")) {
        alert(ipt.label + "不能为空！");
        if (ipt.canFocus) {
          if (ipt.type == "textarea") {
            ipt.jQueryObj.find("textarea").addClass("invalidInput");
            ipt.jQueryObj.find("textarea").focus();
          } else {
            ipt.jQueryObj.find("input").addClass("invalidInput");
            ipt.jQueryObj.find("input").focus();
          }
        }
        return null;
      }
      if (val && val != "" && !ipt.isValid()) {
        alert(ipt.label + "的格式不正确！");
        if (ipt.canFocus) {
          ipt.jQueryObj.find("input").addClass("invalidInput");
          ipt.jQueryObj.find("input").focus();
        }
        return null;
      }
      if(ipt.enc) {
        service.addEncArg(ipt.id, val);
      } else {
        args[ipt.id] = val;
      }
    }
    return args;
  };

  /**
   * 提交数据
   */
  this.submit = function () {
    if (!this.submitService) {
      alert("未设置处理提交请求的后台服务！");
      return;
    }
    var service = new TaskMsgService(this.submitService);
    var args = this.parseData(service);
    if (args == null) return;
    service.request(args, function (req, res) {
      if (thisForm.onSubmit) eval(thisForm.onSubmit + "(req, res)");
    });
  };
}

/**
 * 输入项类
 */
function TaskMsgInput() {
  this.id = null; //控件编号
  this.type = null; //控件类型
  this.label = null; //控件的标签文本
  this.jQueryObj = null; //控件对应的jQuery对象
  this.value = null; //控件值
  this.notNull = false; //不能为空
  this.multi = false; //是否可以多选
  this.values = null; //值集合
  this.texts = null; //文本集合
  this.width = null; //输入框宽度
  this.height = null; //输入框高度
  this.style = "";    //样式
  this.imgwidth = null;
  this.imgheight = null;
  this.form = null; //当前表单
  this.maxlength = null; //最多可以输入的数量
  this.onChange = null; //控件值改变后执行的函数
  this.onClick = null; //控件点击后执行的函数
  this.canFocus = false; //控件是否可以获得焦点
  this.dataField = null; //输入项对应的数据源字段名称
  this.dataListMember = null; //数据列表对应的数据源成员
  this.valueField = null; //数据列表对应的数据源成员变量的值字段名
  this.textField = null; //数据列表对应的数据源成员变量的文本字段名
  this.readonly = false; //是否只读
  this.portrait = false; //是否纵向显示，适用于单选和多选输入项
  this.getDataListService = null; //获得数据列表的服务名称
  this.isRendered = false; //控件事否已渲染
  this.fileIndex = 0; //上传的文件索引
  this.fileFormat = null; //可以上传的文件格式
  this.notSession = false; //是否不传递sessionId
  this.onlyOne = false; //该附件是否仅保留一个，如果是则上传后会替换原来的，例如头像
  this.retainExt = false; //保留扩展名
  this.attributes = {}; //输入项标签的属性集合
  this.submitObjectValue = false; //提交对象型值，某些控件在选择完值后，值的类型是对象，默认仅提交编号或路径，不会提交整个对象，例如用户、部门、文件等
  this.enc = false;  //是否加密传输
  this.placeholder = null;

  var thisInput = this;

  /**
   * 解析控件属性
   * @param  {[type]} tform [description]
   * @param  {[type]} obj   [description]
   * @return {[type]}       [description]
   */
  this.parseFromTag = function (tform, obj) {
    if (!obj.attr("type") || !obj.attr("id")) {
      tmClient.debug("控件类型或编号为空！");
      return;
    }
    this.form = tform;

    for (var i = 0; i < obj.get(0).attributes.length; i++) {
      var attr = obj.get(0).attributes[i];
      var attrObj = {};
      attrObj.name = attr.name;
      attrObj.value = attr.value;
      this.attributes[attr.name] = attrObj;
    }
    this.id = obj.attr("id");
    this.type = obj.attr("type");
    if (obj.attr("label")) this.label = obj.attr("label");
    if (obj.attr("value")) {
      if (this.type == "user" || this.type == "dep" || this.type == "group" ||
        this.type == "file" || this.type == "photo" || this.type == "location" ||
        this.type == "voice" || this.type == "camera") {
        this.value = JSON.parse(obj.attr("value"));
      } else {
        this.value = exeExpress(obj.attr("value"));
      }
    }
    if (obj.attr("notNull") == "true") this.notNull = true;
    if (obj.attr("multi") == "true") this.multi = true;
    if (obj.attr("onChange")) this.onChange = obj.attr("onChange");
    if (obj.attr("onClick")) this.onClick = obj.attr("onClick");
    if (obj.attr("maxlength")) this.maxlength = obj.attr("maxlength");
    if (obj.attr("fileFormat")) this.fileFormat = obj.attr("fileFormat");
    if (obj.attr("notSession")) this.notSession = obj.attr("notSession");
    if (obj.attr("width")) this.width = obj.attr("width");
    if (obj.attr("height")) this.height = obj.attr("height");
    if (obj.attr("style")) this.style = obj.attr("style");
    if (obj.attr("imgwidth")) this.imgwidth = obj.attr("imgwidth");
    if (obj.attr("imgheight")) this.imgheight = obj.attr("imgheight");
    if (obj.attr("placeholder")) this.placeholder = obj.attr("placeholder");
    if (obj.attr("submitObjectValue") == "true") this.submitObjectValue = true;
    if (obj.attr("onlyOne") == "true") this.onlyOne = true;
    if (obj.attr("readonly")) this.readonly = true;
    if (obj.attr("enc") == "true") this.enc = true;
    if (obj.attr("portrait") == "true") this.portrait = true;
    if (obj.attr("getDataListService")) this.getDataListService = obj.attr("getDataListService");
    if (!this.maxlength) {
      if (this.type == "password") this.maxlength = 30;
      if (this.type == "email") this.maxlength = 50;
      if (this.type == "mobile") this.maxlength = 11;
      if (this.type == "tel") this.maxlength = 13;
      if (this.type == "idCard") this.maxlength = 18;
      if (this.type == "number") this.maxlength = 15;
      if (this.type == "int") this.maxlength = 15;
      if (this.type == "money") this.maxlength = 15;
      if (this.type == "postCode") this.maxlength = 6;
      //if(this.type=="varCode") this.maxlength = 6;
      if (this.type == "file") this.maxlength = 10;
      if (this.type == "photo") this.maxlength = 9;
    }
    if ((this.type == "file" || this.type == "photo") && obj.attr("retainExt") == "true") this.retainExt = true;
    if (obj.attr("dataField")) this.dataField = obj.attr("dataField");
    if (obj.attr("values")) this.values = (obj.attr("values") + "").split(",");
    if (obj.attr("texts")) this.texts = (obj.attr("texts") + "").split(",");
    if (obj.attr("dataListMember")) this.dataListMember = obj.attr("dataListMember");
    if (obj.attr("valueField")) this.valueField = obj.attr("valueField");
    if (obj.attr("textField")) this.textField = obj.attr("textField");
    if (obj.attr("parentIdField")) this.parentIdField = obj.attr("parentIdField");
    if (this.values && !this.texts) this.texts = this.values;
    if (this.type == "date" || this.type == "time" || this.type == "datetime") {
      /*
        if(!this.value) {
          if(this.type=="date") this.value = (new Date()).format("yyyy-MM-dd");
          if(this.type=="time") this.value = (new Date()).format("hh:mm");
          if(this.type=="datetime") this.value = (new Date()).format("yyyy-MM-dd hh:mm");
        }
      */
    } else if (this.type == "user") {
      this.selectSelf = false;
      if (obj.attr("selectSelf") == "true") this.selectSelf = true;
    } else if (this.type == "text" || this.type == "password" || this.type == "email" ||
      this.type == "tel" || this.type == "postCode" || this.type == "idCard" || this.type == "varCode" ||
      this.type == "number" || this.type == "money" || this.type == "int" ||
      this.type == "mobile") {
      this.canFocus = true;
    }

    tform.inputs[this.id] = this;
    tform.inputIds.push(this.id);
  };

  /**
   * 渲染控件
   */
  this.render = function () {
    //生成控件dom元素
    var iptHtml = "";
    iptHtml += "<label>" + this.label + "：</label>";
    if (this.type != "submit") {
      iptHtml += "<div class=\"notNullHint";
      if (this.notNull) {
        iptHtml += " notNullHintShow";
      }
      iptHtml += "\"></div>";
    }
    var style = "";
    //如果是只读
    if (this.type == "barcode" || this.type == "location" ||
      (this.readonly && !(this.type == "file" || this.type == "photo" ||
        this.type == "voice" || this.type == "textarea"))) {
      iptHtml += "<input type=\"text\" ctype=\"" + this.type + "\" readonly " +
        "style=\"border:0;margin-top:6px;";
      if (this.width) iptHtml += "width:" + this.width + ";";
      if (this.style) iptHtml += this.style;
      iptHtml += "\"";
      if (this.onClick) iptHtml += " onclick=\"" + this.onClick + "\"";
      if(this.placeholder) iptHtml += " placeholder=\"" + this.placeholder + "\"";
      iptHtml += " />";
    } else if (this.type == "text" || this.type == "password" || this.type == "email" ||
      this.type == "mobile" || this.type == "tel" || this.type == "idCard" ||
      this.type == "number" || this.type == "int" || this.type == "money" ||
      this.type == "postCode" || this.type == "varCode") {
      var inputType = "text";
      if (this.type == "password") inputType = "password";
      iptHtml += "<input type=\"" + inputType + "\" ctype=\"" + this.type + "\" onblur=\"" +
        "taskMsgForms['" + this.form.id + "'].inputs['" + this.id + "'].valueOnChange(this)\"";
      if (this.maxlength) iptHtml += " maxlength=" + this.maxlength;
      if (this.width || this.style) {
        iptHtml += " style=\"";
        if(this.width) iptHtml += "width:" + this.width + ";";
        if(this.style) iptHtml += this.style;
        iptHtml += "\" ";
      }
      if(this.placeholder) iptHtml += " placeholder=\"" + this.placeholder + "\"";
      if (this.onClick) iptHtml += " onclick=\"" + this.onClick + "\"";
      iptHtml += " />";
    } else if (this.type == "date" || this.type == "time" || this.type == "datetime") {
      iptHtml += "<input type=\"text\" id=\"input_" + this.id + "\" ctype=\"" + this.type +
        "\" onblur=\"taskMsgForms['" + this.form.id + "'].inputs['" + this.id + "'].valueOnChange(this)\"";
      iptHtml += " style=\"";
      if (this.width) {
        iptHtml += " width:" + this.width + ";";
      } else {
        if (this.type == "date") {
          iptHtml += " width:100px;";
        } else if (this.type == "time") {
          iptHtml += " width:60px;";
        } else if (this.type == "datetime") {
          iptHtml += " width:150px;";
        }
      }
      if(this.style) iptHtml += this.style;
      iptHtml += "\" />";
    } else if (this.type == "switch") {
      this.status = ["switchOn", "switchOff"];
      if (this.values) {
        if (!this.value) this.value = this.values[0];
        if (this.value == this.values[1]) {
          this.status = ["switchOff", "switchOn"];
        }
        iptHtml += "<div ctype=\"switch\"";
        if(this.style) iptHtml += " style=\""+this.style+"\"";
        iptHtml += ">";
        iptHtml += "<div class=\"" + this.status[0] + "\">" + this.texts[0] + "</div>" +
          "<div class=\"" + this.status[1] + "\">" + this.texts[1] + "</div></div>";
      }
    } else if (this.type == "radio" || this.type == "checkbox") {
      iptHtml += "<div class=\"rightContent\" style=\"vertical-align:middle;\"";
      if (this.width || this.style) {
        iptHtml += " style=\"";
        if(this.width) iptHtml += "width:" + this.width + ";";
        if(this.style) iptHtml += this.style;
        iptHtml += "\" ";
      }
      iptHtml += ">";
      if (this.values) {
        if (!this.texts) this.texts = this.values;
        for (var i = 0; i < this.values.length; i++) {
          if (this.portrait) iptHtml += "<div style=\"height:30px;line-height:30px;\">";
          iptHtml += "<input type=\"" + this.type + "\" id=\"" + this.type + "_" 
            + this.id + "_" + i + "\" name=\"" + this.type + "_" + this.id +
            "\" value=\"" + this.values[i] + "\" onclick=\"taskMsgForms['" + this.form.id +
            "'].inputs['" + this.id + "'].valueOnChange(this)\" "
            +"style=\"float:left;vertical-align:middle; margin-top:10px;\" />"
            +"<label for=" + this.type + "_" + this.id + "_" + i +
            " style=\"float:left; text-align: left;width:auto;line-height:30px;\">&nbsp;" + this.texts[i] + "&nbsp;</label>";
          if (this.portrait) iptHtml += "</div>";
        }
      }
      iptHtml += "</div>";
    } else if (this.type == "list") {
      iptHtml += "<select ctype=\"" + this.type + "\" onchange=\"taskMsgForms['" +
        this.form.id + "'].inputs['" + this.id + "'].valueOnChange(this)\"";
      iptHtml += " style=\"";
      if(this.width) iptHtml += "width:" + this.width + ";";
      if(this.style) iptHtml += this.style;
      iptHtml += "\" >";
      if(!this.notNull) iptHtml += "<option value=\"\"></option>";
      if (this.values) {
        if (!this.texts) this.texts = this.values;
        for (var i = 0; i < this.values.length; i++) {
          iptHtml += "<option value=\"" + this.values[i] + "\">" + this.texts[i] + "</option>";
        }
      }
      iptHtml += "</select>";
    } else if (this.type == "file") {
      iptHtml += "<div class=\"rightContent\"";
      if(this.style) iptHtml += " style=\""+this.style+"\"";
      iptHtml += ">";
      if (!this.readonly) {
        iptHtml += "<div style=\"height:30px; line-height:30px;\">" +
          "<a class=\"aUpload\" href=\"javascript:\" onclick=\"taskMsgForms['" +
          this.form.id + "'].inputs['" + this.id + "'].selectFile()\">添加附件</a></div>";
      }
      iptHtml += "</div>";
    } else if (this.type == "photo") {
      iptHtml += "<div class=\"rightContent\"";
      if(this.style) iptHtml += " style=\""+this.style+"\"";
      iptHtml += ">";
      if (!this.readonly) {
        iptHtml += "<div style=\"height:30px; line-height:30px;\">" +
          "<a class=\"aUpload\" href=\"javascript:\" onclick=\"taskMsgForms['" +
          this.form.id + "'].inputs['" + this.id + "'].selectPhoto()\">添加图片</a></div>";
      }
      iptHtml += "</div>";
    } else if (this.type == "voice") {
      iptHtml += "<input type=\"text\" ctype=\"" + this.type + "\" readonly";
      if (!this.readonly) iptHtml += " onClick=\"taskMsgForms['" + this.form.id +
        "''].inputs['" + this.id + "'].playAudio()\"";
      if(this.style) iptHtml += " style=\""+this.style+"\"";
      iptHtml += "/>";
    } else if (this.type == "textarea") {
      if (this.readonly) {
        if (this.value) {
          iptHtml += "<pre style=\""+this.style+"\">" + this.value + "</pre>";
        } else {
          iptHtml += "<pre style=\""+this.style+"\"></pre>";
        }
      } else {
        iptHtml += "<textarea ctype=\"" + this.type + "\" onblur=\"taskMsgForms['" +
          this.form.id + "'].inputs['" + this.id + "'].valueOnChange(this)\"";
        iptHtml += " style=\"";
        if (this.width) iptHtml += "width:" + this.width + ";";
        if (this.height) iptHtml += "height:" + this.height + ";";
        if(this.style) iptHtml += this.style;
        iptHtml += "\"></textarea>";
      }
    } else if (this.type == "richtext") {
      if (this.readonly) {
        if (this.value) {
          iptHtml += "<pre style=\""+this.style+"\">" + this.value + "</pre>";
        } else {
          iptHtml += "<pre style=\""+this.style+"\"></pre>";
        }
      } else {
        iptHtml += "<textarea id=\"editor_" + this.form.id + "_" + this.id +
          "\" ctype=\"" + this.type + "\" onblur=\"taskMsgForms['" +
          this.form.id + "'].inputs['" + this.id + "'].valueOnChange(this)\"";
        iptHtml += " style=\"margin-top:5px;margin-bottom:5px;";
        if (this.width) iptHtml += "width:" + this.width + ";";
        if (this.height) iptHtml += "height:" + this.height + ";";
        if(this.style) iptHtml += this.style;
        iptHtml += "\"></textarea>";
      }
    } else if (this.type == "dep" || this.type == "user" ||
      this.type == "role" || this.type == "group") {
      iptHtml += "<input type=\"text\" ctype=\"" + this.type + "\" readonly " +
        "onclick=\"taskMsgForms['" + this.form.id + "'].inputs['" +
        this.id + "'].selectOrgInfo()\" ";
      if(this.style) iptHtml += " style=\""+this.style+"\"";
      iptHtml += "/>";
    } else if (this.type == "dialog") {
      iptHtml += "<input type=\"text\" ctype=\"" + this.type + "\" readonly " +
        "onclick=\"taskMsgForms['" + this.form.id + "'].inputs['" +
        this.id + "'].openDialog(this)\" ";
      if(this.style) iptHtml += " style=\""+this.style+"\"";
      iptHtml += "/>";
    }
    this.jQueryObj.append(iptHtml);

    if (this.type == "richtext" && !this.readonly) {
      KindEditor.ready(function (K) {
        var options = {
          allowImageUpload: true,
          allowFlashUpload: false,
          allowMediaUpload: false,
          designMode: true,
          allowFileManager: false,
          uploadJson: "/TaskMsgUpload?sessionId=" + tmClient.getSessionId() + "&appCode=KindEditor"
        };
        thisInput.editor = K.create("#editor_" + thisInput.form.id + "_" + thisInput.id, options);
        if (thisInput.value) thisInput.editor.html(thisInput.value);
      });
    }

    //设置控件默认值
    if (this.value && this.type != "richtext") this.setValue(this.value);

    if (this.getDataListService || this.dataListMember) this.loadData();
    //this.loadData();

    //绑定控件事件
    if (!this.readonly) {
      if (this.type == "switch" || this.type == "switchs") {
        var tmptype = this.type;
        this.jQueryObj.children("div").click(function () {
          if (thisInput.onClick) {
            eval(thisInput.onClick + "('" + thisInput.form.id + "','" + thisInput.id + "')");
            return;
          }
          if (!thisInput.values || thisInput.readonly) return;
          if ((thisInput.value+'') == thisInput.values[0]) {
            thisInput.value = thisInput.values[1];
          } else {
            thisInput.value = thisInput.values[0];
          }
          if(thisInput.value=='true') {
            thisInput.value = true;
          } else if(thisInput.value=='false') {
            thisInput.value = false;
          } else if(thisInput.value=='0') {
            thisInput.value = 0;
          } else if(thisInput.value=='1') {
            thisInput.value = 1;
          }
          $(this).find("div").each(function () {
            if ($(this).attr("class") == tmptype + "On") {
              $(this).attr("class", tmptype + "Off");
            } else {
              $(this).attr("class", tmptype + "On");
            }
          });
        });
      } else if (this.type == "datetime") {
        jeDate({
          dateCell: "#input_" + this.id,
          format: "YYYY-MM-DD hh:mm",
          isTime: true
        });
      } else if (this.type == "date") {
        jeDate({
          dateCell: "#input_" + this.id,
          format: "YYYY-MM-DD",
          isTime: false
        });
      } else if (this.type == "time") {
        jeDate({
          dateCell: "#input_" + this.id,
          format: "hh:mm",
          isTime: true
        });
      }
    }
    this.isRendered = true;
  };

  /**
   * 重新加载控件数据
   */
  this.reload = function () {
    this.isRendered = false;
    this.setValue(null);
    this.loadData();
  };

  /**
   * 加载控件数据
   */
  this.loadData = function () {
    if (!this.getDataListService) this.getDataListService = this.form.getDataService;
    if (!this.getDataListService) return;
    var service = new TaskMsgService(this.getDataListService);
    service.request(null, function (req, res) {
      thisInput.dataBindList(res);
    });
  };

  this.dataBindList = function (res) {
    if (this.dataListMember) {
      if (this.type == "list" || this.type == "radio" || this.type == "checkbox" || this.type == "switch" || this.type == "switchs") {
        this.values = [];
        this.texts = [];
        var dataList = res[this.dataListMember];
        if (dataList) {
          if(this.type == "list" && this.parentIdField) {
            var rootItems = [];
            var items = {};
            if(dataList instanceof Array) {
              for(var i=0;i<dataList.length;i++) {
                var item = {};
                item.id = dataList[i][this.valueField];
                item.name = dataList[i][this.textField];
                item.parentId = dataList[i][this.parentIdField];
                item.childs = [];
                items[item.id] = item;
                if(item.parentId==0) rootItems.push(item.id);
                this.values.push(item.id);
                this.texts.push(item.name);
              }
            } else {
              for(objId in dataList) {
                var item = {};
                item.id = dataList[objId][this.valueField];
                item.name = dataList[objId][this.textField];
                item.parentId = dataList[objId][this.parentIdField];
                item.childs = [];
                items[item.id] = item;
                if(item.parentId==0) rootItems.push(item.id);
                this.values.push(item.id);
                this.texts.push(item.name);
              }
            }
            for(objId in items) {
              var item = items[objId];
              if(item.parentId>0) {
                var parent = items[item.parentId];
                if(!parent) continue;
                parent.childs.push(item.id);
              }
            }
            for(var i=0;i<rootItems.length;i++) {
              var item = items[rootItems[i]];
              var strOption = "<option value=\"" + item.id + "\"";
              if (this.value == item.id) strOption += " selected";
              strOption += ">" + item.name + "</option>";
              this.jQueryObj.append(strOption);
              this.addListItem(items, item, "--");
            }
          } else {
            if (dataList instanceof Array) {
              for (var i = 0; i < dataList.length; i++) {
                this.values.push(dataList[i][this.valueField]);
                this.texts.push(dataList[i][this.textField]);
              }
            } else {
              for (objId in dataList) {
                this.values.push(dataList[objId][this.valueField]);
                this.texts.push(dataList[objId][this.textField]);
              }
            }
            for (var i = 0; i < this.values.length; i++) {
              if (this.type == "list") {
                var strList = "<option value=\"" + this.values[i] + "\"";
                if (this.value == this.values[i]) strList += " selected";
                strList += ">" + this.texts[i] + "</option>";
                this.jQueryObj.find("select").append(strList);

              } else if (this.type == "radio" || this.type == "checkbox") {
                var iptHtml = "";
                if (this.portrait) iptHtml += "<div style=\"height:30px;line-height:30px;\">";
                iptHtml += "<input type=\"" + this.type + "\" id=\"" + this.type + "_" + this.id + "_" + i +
                  "\" name=\"" + this.type + "_" + this.id + "\" value=\"" + this.values[i] +
                  "\" onclick=\"taskMsgForms['" + this.form.id +
                  "'].inputs['" + this.id + "'].valueOnChange(this)\"";
                if (this.value == this.values[i]) iptHtml += " checked";
                iptHtml += "/><label for=" + this.type + "_" + this.id + "_" + i +
                  ">&nbsp;" + this.texts[i] + "&nbsp;</label>";
                if (this.portrait) iptHtml += "</div>";
                this.jQueryObj.find(".rightContent").append(iptHtml);
              }
            }
          }
        }
      }
    }
  };

  this.addListItem = function(items, parent, prefix) {
    if(parent.childs.length==0) return;
    for(var i=0;i<parent.childs.length;i++) {
      var item = items[parent.childs[i]];
      var strOption = "<option value=\"" + item.id + "\"";
      if (this.value == item.id) strOption += " selected";
      strOption += ">" +prefix+ item.name + "</option>";
      this.jQueryObj.append(strOption);
      this.addListItem(items, item, prefix+"--");
    }
  };

  /**
   * 绑定控件数据
   * @param  {[type]} res [description]
   */
  this.dataBind = function (res) {
    /*
    if(this.dataListMember) {
      if(this.type=="list"||this.type=="radio"||this.type=="checkbox"||this.type=="switch"||this.type=="switchs") {
        this.values = [];
        this.texts = [];
        var dataList = res[this.dataListMember];
        if(dataList) {
          if(dataList instanceof Array) {
            for(var i=0;i<dataList.length;i++) {
              this.values.push(dataList[i][this.valueField]);
              this.texts.push(dataList[i][this.textField]);
            }
          } else {
            for(objId in dataList) {
              this.values.push(dataList[objId][this.valueField]);
              this.texts.push(dataList[objId][this.textField]);
            }
          }
          for(var i=0;i<this.values.length;i++) {
            if(this.type=="list") {
              this.jQueryObj.find("select").append("<option value=\""
                +this.values[i]+"\">"+this.texts[i]+"</option>");
              
            } else if(this.type=="radio"||this.type=="checkbox") {
              var iptHtml = "";
              if(this.portrait) iptHtml += "<div style=\"height:30px;line-height:30px;\">";
              iptHtml += "<input type=\""+this.type+"\" id=\""+this.type+"_"+this.id+"_"+i+"\" name=\""+this.type+"_"+this.id
                +"\" value=\""+this.values[i]+"\" onclick=\"taskMsgForms['"+this.form.id
                +"'].inputs['"+this.id+"'].valueOnChange(this)\"/><label for="+this.type+"_"+this.id+"_"+i
                +">&nbsp;"+this.texts[i]+"&nbsp;</label>";
              if(this.portrait) iptHtml += "</div>";
              this.jQueryObj.find(".rightContent").append(iptHtml);
            }
          }
        }
      }
    }
    */

    var val;
    if (this.value || this.value === 0) val = this.value;
    if (!this.form.dataSource) return;
    if (this.dataField) {
      val = this.form.dataSource[this.dataField];
    } else if (this.id in this.form.dataSource) {
      val = this.form.dataSource[this.id];
    }

    if (val) {
      if (this.type == "date") val = (new Date(val)).format("yyyy-MM-dd");
      if (this.type == "time" && val.length!=5) val = (new Date(val)).format("hh:mm");
      if (this.type == "datetime") val = (new Date(val)).format("yyyy-MM-dd hh:mm");
    }
    this.setValue(val);
  };

  /**
   * 设置控件值
   * @param {[type]} value [description]
   */
  this.setValue = function (value) {
    this.value = value;
    if (this.type == "switch" || this.type == "switchs") {
      if (!this.values) return;
      if (!this.texts) this.texts = this.values;
      var txt = "";
      if (this.value == this.values[0]) {
        this.status = [this.type + "On", this.type + "Off"];
        txt = this.texts[0];
      } else {
        this.status = [this.type + "Off", this.type + "On"];
        txt = this.texts[1];
      }
      if (this.readonly) {
        this.jQueryObj.find("input").val(txt);
      } else {
        this.jQueryObj.children("div").children("div").each(function (index) {
          $(this).attr("class", thisInput.status[index]);
        });
      }
    } else if (this.type == "radio") {
      if (!this.values) return;
      if (!this.texts) this.texts = this.values;

      for (var i = 0; i < this.values.length; i++) {
        if (this.value == this.values[i]) {
          this.jQueryObj.find("[id='radio_" + this.id + "_" + i + "']").prop("checked", true);
        }
      }
    } else if (this.type == "checkbox") {
      if (!this.values) return;
      if (!this.texts) this.texts = this.values;

      for (var i = 0; i < this.values.length; i++) {
        if (("," + this.value + ",").indexOf("," + this.values[i] + ",") >= 0) {
          this.jQueryObj.find("[id='checkbox_" + this.id + "_" + i + "']").prop("checked", true);
        }
      }

    } else if (this.type == "list") {
      if (!this.values) return;
      if (!this.texts) this.texts = this.values;
      if (this.readonly) {
        for (var i = 0; i < this.values.length; i++) {
          if (this.value == this.values[i]) {
            this.jQueryObj.find("input").val(this.texts[i]);
            return;
          }
        }
        this.jQueryObj.find("input").val("");
        return;
      }
      for (var i = 0; i < this.values.length; i++) {
        if (this.value == this.values[i]) {
          this.jQueryObj.find("select").val(this.values[i]);
          return;
        }
      }
      this.jQueryObj.find("select").val("");
    } else if (this.type == "user" || this.type == "dep" || this.type == "role" || this.type == "group") {
      if (this.multi) {
        if (!this.value) this.value = [];
        var txts = "";
        for (var i = 0; i < this.value.length; i++) {
          if (txts !== "") txts += "、";
          txts += this.value[i].name;
        }
        this.jQueryObj.find("input").val(txts);
      } else {
        if (this.value) {
          this.jQueryObj.find("input").val(this.value.name);
        } else {
          this.jQueryObj.find("input").val("");
        }
      }
    } else if (this.type == "file") {
      if (!this.value) return;
      if (typeof (this.value) == "string") this.value = JSON.parse(this.value);
      var arr = this.value;
      if (!this.multi) arr = [this.value];
      for (var i = 0; i < arr.length; i++) {
        var file = arr[i];
        var html = "<div class=\"fileInfo\" style=\"height:24px;line-height:24px;\">" +
          "<p class=\"fileInfoFileName\">" + file.name;
        if (!this.readonly) html += "&nbsp;&nbsp;<a href=\"javascript:\" " +
          "onclick=\"taskMsgForms['" + this.form.id + "'].inputs['" + this.id +
          "'].delUploadFile(this,'" + file.code + "','file')\">删除</a>";
        html += "</p></div>";
        this.jQueryObj.find(".rightContent").append(html);
      }
      if (!this.multi) this.jQueryObj.find(".aUpload").hide();
    } else if (this.type == "photo" || this.type == "camera") {
      if (!this.value) return;
      if (typeof (this.value) == "string") this.value = JSON.parse(this.value);
      var arr = this.value;
      if (!this.multi) arr = [this.value];
      for (var i = 0; i < arr.length; i++) {
        var imgwidth = this.imgwidth;
        if (!imgwidth) imgwidth = "90";
        var imgheight = this.imgheight;
        if (!imgheight) imgheight = "90";
        var file = arr[i];
        var downUrl = "/TaskMsgDownload?fileCode=" + file.code;
        if(top.taskMsgAuthObj) downUrl = top.taskMsgAuthObj.getAuthUrl(downUrl);
        var html = "<div class=\"fileInfo\" style=\"height:124px;\">" +
          "<div class=\"fileInfoImageDiv\"><img width=\"" + imgwidth 
          + "\" height=\"" + imgheight + "\" src=\"" +downUrl+ "\"/>";
        if (!this.readonly) html += "<div><a href=\"javascript:\" onclick=\"taskMsgForms['" +
          this.form.id + "'].inputs['" + this.id + "'].delUploadFile(this,'" + file.code + "','photo')\">删除</a></div>";
        html += "</div></div>";
        this.jQueryObj.find(".rightContent").append(html);
      }
      if (!this.multi) this.jQueryObj.find(".aUpload").hide();
    } else if (this.type == "voice") {
      if (!this.value) return;
      this.showVoice();
    } else if (this.type == "location") {
      if (!this.value && this.value != 0) {
        this.jQueryObj.find("input").val("");
      } else {
        this.jQueryObj.find("input").val(this.value);
      }
    } else if (this.type == "textarea") {
      if (!this.value && this.value != 0) this.value = "";
      if (this.readonly) {
        this.jQueryObj.find("pre").html(this.value);
      } else {
        this.jQueryObj.find("textarea").val(this.value);
      }
    } else if (this.type == "richtext") {
      this.editor.html(this.value);
    } else if (this.type == "money") {
      if (!this.value && this.value != 0) {
        this.value = "";
      } else {
        this.value = formatMoney(this.value, 2);
      }
      this.jQueryObj.find("input").val(this.value);
    } else {
      if (!this.value && this.value != 0) this.value = "";
      this.jQueryObj.find("input").val(this.value);
    }
  };

  /**
   * 获得控件值
   * @return {[type]} [description]
   */
  this.getValue = function () {
    if (this.type == "checkbox" || this.type == "radio" || this.type == "switch" ||
      this.type == "dialog" || this.type == "user" || this.type == "dep" ||
      this.type == "role" || this.type == "group" || this.type == "file" ||
      this.type == "photo" || this.type == "voice" || this.type == "location") {
      //
    } else if (this.type == "textarea" || this.type == "barcode") {
      this.value = this.jQueryObj.find("textarea").val();
    } else if (this.type == "richtext") {
      this.value = this.editor.html();
    } else if (this.type == "list") {
      this.value = this.jQueryObj.find("select").val();
      //} else if(this.type=="checkbox") {
      //  var vvv = "";
      //  this.jQueryObj.find("input").each(function(){
      //    if($(this).is(':checked')){
      //      if(vvv)vvv+=",";
      //      vvv +=$(this).attr("value");
      //    }
      //  });
      //  this.value = vvv;
    } else {
      this.value = this.jQueryObj.find("input").val();
      if ((this.value || this.value == 0) && this.type == "money") this.value = this.value.replace(/,/g, "");
    }
    return this.value;
  };

  /**
   * 判断输入项的值是否有效
   * @return {Boolean} [description]
   */
  this.isValid = function () {
    if (!this.value && this.value != 0 && this.notNull) return false;
    if (this.value != "" && (this.value || this.value == 0)) {
      if (this.type == "email") return isEmail(this.value);
      if (this.type == "mobile") return isMobile(this.value);
      if (this.type == "tel") return isTel(this.value);
      if (this.type == "postCode") return isPostCode(this.value);
      //if(this.type=="varCode") return true;
      if (this.type == "idCard") return isIdCard(this.value);
      if (this.type == "number" || this.type == "money")
        return !isNaN(this.value.replace(/,/g, ""));
      if (this.type == "int") return isInt(this.value);
    }
    return true;
  };

  this.setReadOnly = function() {
    if(this.readonly) return;
    if(this.type=="file" || this.type=="photo") {
      this.jQueryObj.find("a").hide();
    } else if(this.type=="list") {
      this.jQueryObj.find("select").attr("disabled","true");
    } else if(this.type=="radio") {
      this.jQueryObj.find("input:radio").attr("disabled","true");
    } else if(this.type=="checkbox") {
      this.jQueryObj.find("input:checkbox").attr("disabled","true");
    } else {
      this.jQueryObj.css("border","0");
      this.jQueryObj.find("input").attr("readonly","true");
    }
  };

  this.setNotNull = function(notNull) {
    if(this.notNull==notNull) return;
    this.notNull = notNull;
    var divNotNullHint = this.jQueryObj.find("notNullHint");
    if(notNull) {
      if(!divNotNullHint.hasClass("notNullHintShow")) 
        divNotNullHint.addClass("notNullHintShow");
    } else {
      divNotNullHint.removeClass("notNullHintShow");
    }
  };

  /**
   * 选择组织结构信息
   * @return {[type]} [description]
   */
  this.selectOrgInfo = function () {
    curTaskMsgInput = this;
    if (this.type == "user") {
      if (this.multi) {
        tmClient.openDialog("选择人员", "/common/selectUser.html?getName=true&callback=TaskMsg_OnSelectOrgInfo", null, 540, 600, null, self);
      } else {
        tmClient.openDialog("选择人员", "/common/selectUserOne.html?getName=true&callback=TaskMsg_OnSelectOrgInfo", null, 540, 600, null, self);
      }
    } else if (this.type == "dep") {
      if (this.multi) {
        tmClient.openDialog("选择部门", "/common/selectDep.html?getName=true&callback=TaskMsg_OnSelectOrgInfo", null, 540, 600, null, self);
      } else {
        tmClient.openDialog("选择部门", "/common/selectDepOne.html?getName=true&callback=TaskMsg_OnSelectOrgInfo", null, 540, 600, null, self);
      }
    } else if (this.type == "role") {
      if (this.multi) {
        tmClient.openDialog("选择角色", "/common/selectRole.html?getName=true&callback=TaskMsg_OnSelectOrgInfo", null, 540, 600, null, self);
      } else {
        tmClient.openDialog("选择角色", "/common/selectRoleOne.html?getName=true&callback=TaskMsg_OnSelectOrgInfo", null, 540, 600, null, self);
      }
    }
  };

  this.openDialog = function (obj) {
    debugger;
    if (!this.attributes["url"]) {
      alert("请设置弹出页面的URL地址！");
      return;
    }
    var title = "";
    if (this.attributes["title"]) title = this.attributes["title"].value;
    var url = this.attributes["url"].value;
    var icon = null;
    if (this.attributes["icon"]) icon = this.attributes["icon"].value;
    var width = 540;
    if (this.attributes["width"]) width = parseInt(this.attributes["width"].value);
    var height = 540;
    if (this.attributes["height"]) height = parseInt(this.attributes["height"].value);
    tmClient.openDialog(title, url, icon, width, height, obj, self);
  };

  this.selectFile = function () {
    if (!this.multi && this.value) {
      alert("只允许上传一个文件！");
      return;
    }
    if (this.multi && this.maxlength && this.value && this.value.length >= this.maxlength) {
      alert("最多只能上传" + this.maxlength + "个文件！");
      return;
    }
    this.jQueryObj.find(".rightContent").append("<div class=\"fileInfo\" style=\"" +
      "height:48px;line-height:24px;display:none;\"><input type=\"file\" id=\"file_" +
      this.form.id + "_" + this.id + "_" + this.fileIndex + "\" onchange=\"taskMsgForms['" +
      this.form.id + "'].inputs['" + this.id + "'].uploadFile(this)\" style=\"display:none\" />" +
      "<p class=\"fileInfoFileName\"></p><p><progress value=\"0\" max=\"100\">" +
      "</progress>&nbsp;<span class=\"uploaderPercent\">0%</span></p></div>");
    $("#file_" + this.form.id + "_" + this.id + "_" + this.fileIndex).get(0).click();
    this.fileIndex++;
  };

  this.selectPhoto = function () {
    if (!this.multi && this.value) {
      alert("只允许上传一张图片！");
      return;
    }
    if (this.multi && this.maxlength && this.value && this.value.length >= this.maxlength) {
      alert("最多只能上传" + this.maxlength + "张图片！");
      return;
    }
    this.jQueryObj.find(".rightContent").append("<div class=\"fileInfo\" style=\"" +
      "height:124px;display:none;\"><input type=\"file\" id=\"file_" +
      this.form.id + "_" + this.id + "_" + this.fileIndex + "\" onchange=\"taskMsgForms['" +
      this.form.id + "'].inputs['" + this.id + "'].uploadFile(this,'photo')\" style=\"display:none\" />" +
      "<div class=\"fileInfoImageDiv\"></div><p><progress value=\"0\" max=\"100\">" +
      "</progress>&nbsp;<span class=\"uploaderPercent\">0%</span></p></div>");
    $("#file_" + this.form.id + "_" + this.id + "_" + this.fileIndex).get(0).click();
    this.fileIndex++;
  };

  this.uploadFile = function (obj, type) {
    var iptFile = $(obj);
    var filePath = iptFile.val();
    var fileName = filePath.substr(filePath.lastIndexOf("\\") + 1);
    if (type == "photo" && !this.fileFormat) this.fileFormat = ".jpg,.jpeg,.gif,.png,.bmp";
    if (this.fileFormat) {
      var isValidType = true;
      var end = fileName.lastIndexOf(".");
      if (end >= 0) {
        var filetype = fileName.substr(end);
        if (this.fileFormat.toLowerCase().indexOf(filetype.toLowerCase()) < 0) isValidType = false;
      } else {
        isValidType = false;
      }
      if (!isValidType) {
        alert("文件格式必须为：" + this.fileFormat + "。");
        iptFile.parent().remove();
        return;
      }
    }
    var fileObj = obj.files[0];
    if (type == "photo") {
      var imgwidth = taskMsgForms[this.form.id].inputs[this.id].imgwidth;
      if (!imgwidth) imgwidth = "90";
      var imgheight = taskMsgForms[this.form.id].inputs[this.id].imgheight;
      if (!imgheight) imgheight = "90";

      iptFile.parent().find(".fileInfoImageDiv").append(
        "<img width=\"" + imgwidth + "\" height=\"" + imgheight + "\" src=\"\"/>");
      var reader = new FileReader();
      reader.onload = function (e) {
        iptFile.parent().find("img").attr("src", e.target.result);
      };
      reader.readAsDataURL(fileObj);
    } else {
      iptFile.parent().find(".fileInfoFileName").html(fileName);
    }
    iptFile.parent().show();
    if(!this.multi) {
      this.jQueryObj.find(".aUpload").hide();
    }
    var uploaderUrl = "/TaskMsgUpload";
    if (this.form.appCode) {
      uploaderUrl += "?appCode=" + this.form.appCode;
      if (this.form.appDataId) uploaderUrl += "&appDataId=" + this.form.appDataId;
      if (this.onlyOne) uploaderUrl += "&onlyOne=true";
    }
    if (this.retainExt) uploaderUrl += "&retainExt=true";
    var TaskMsgSessionId = tmClient.getSessionId();
    if (TaskMsgSessionId && !this.notSession) {
      if (uploaderUrl == "/TaskMsgUpload") uploaderUrl += "?sessionId=" + TaskMsgSessionId;
      else uploaderUrl += "&sessionId=" + TaskMsgSessionId;
    }

    if(top.taskMsgAuthObj) {
      uploaderUrl = top.taskMsgAuthObj.getAuthUrl(uploaderUrl);
    }
    
    var form = new FormData();
    form.append("file", fileObj);
    var xhr = new XMLHttpRequest();
    xhr.open("post", uploaderUrl, true);
    var me = this;
    xhr.onload = function () {
      var response = null;
      try {
        response = JSON.parse(xhr.responseText);
      } catch (e) {
        alert("文件[" + fileName + "]上传失败，错误描述：" + e.message);
        iptFile.parent().hide();
        iptFile.parent().remove();
        return;
      }
      if (response.code != 0) {
        alert("文件[" + fileName + "]上传失败，错误描述：" + response.message);
        iptFile.parent().hide();
        iptFile.parent().remove();
        return;
      }
      if (type == "photo") {
        iptFile.parent().find(".fileInfoImageDiv").append(
          "<div><a href=\"javascript:\" onclick=\"taskMsgForms['" +
          me.form.id + "'].inputs['" + me.id + "'].delUploadFile(this,'" +
          response.path + "','photo')\">删除</a></div>");
      } else {
        iptFile.parent().find(".fileInfoFileName").append(
          "&nbsp;&nbsp;<a href=\"javascript:\" onclick=\"taskMsgForms['" +
          me.form.id + "'].inputs['" + me.id + "'].delUploadFile(this,'" +
          response.path + "','file')\">删除</a>");
      }

      var file = {};
      file.name = fileName;
      file.id = response.fileId;
      file.code = response.fileCode;
      file.size = response.fileSize;
      if (me.multi) {
        if (!me.value) me.value = [];
        me.value.push(file);
      } else {
        me.value = file;
      }
      iptFile.parent().find("progress").parent().remove();
      if (type == "photo") {
        iptFile.parent().css("height", "100px");
      } else {
        iptFile.parent().css("height", "24px");
      }
      iptFile.remove();
      if (me.onChange) eval(me.onChange);
    };
    xhr.upload.fileInput = iptFile;
    xhr.upload.addEventListener("progress", this.onUploaderProgressChange, false);
    xhr.send(form);
  };

  this.delUploadFile = function (emt, fileCode, type) {
    if (this.multi) {
      var file;
      for (var i = 0; i < this.value.length; i++) {
        if (this.value[i].code == fileCode) {
          file = this.value[i];
          break;
        }
      }
      if (file) this.value.deleteMember(file);
    } else {
      this.value = null;
      this.jQueryObj.find(".aUpload").show();
    }
    var div;
    if (type == "photo") {
      div = $(emt).parent().parent().parent();
    } else {
      div = $(emt).parent().parent();
    }
    div.hide();
    div.remove();
  };

  this.onUploaderProgressChange = function (evt) {
    var ipt = evt.srcElement.fileInput;
    var uploadProgress = ipt.parent().find("progress").get(0);
    var uploadPercent = ipt.parent().find(".uploaderPercent").get(0);
    if (evt.lengthComputable) {
      uploadProgress.max = evt.total;
      uploadProgress.value = evt.loaded;
      uploadPercent.innerHTML = Math.round(evt.loaded / evt.total * 100) + "%";
    }
  };

  this.valueOnChange = function (obj) {
    var ipt = $(obj);
    var val = "";
    if (this.type == "checkbox") {
      this.jQueryObj.find("input").each(function () {
        if ($(this).get(0).checked) {
          if (val != "") val += ",";
          val += $(this).val();
        }
      });
    } else {
      val = ipt.val();
    }
    //如果值没有发生变化，则不用执行后续操作
    if ((!this.value && this.value != 0 && val == "") || this.value + "" == val + "")
      return;
    if (val != "") {
      if (this.type == "money") {
        val = formatMoney(val, 2);
        val = val.replace(/,/g, "");
      } else if (this.type == "int") {
        val = parseInt(val);
      }
    }
    this.value = val;
    if (!(ipt.type == "list" || ipt.type == "radio" || ipt.type == "checkbox")) {
      if (!this.isValid()) {
        ipt.addClass("invalidInput");
        ipt.focus();
        return;
      }
      ipt.removeClass("invalidInput");
    }
    if (this.value || this.value == 0) {
      if (this.type == "money") {
        ipt.val(formatMoney(this.value, 2));
      } else if (this.type == "int") {
        ipt.val(parseInt(this.value));
      }
    }
    if (this.onChange) eval(this.onChange);
  };
}

/**
 * 选择完组织结构数据后
 * @param  {[type]} deps [description]
 * @return {[type]}      [description]
 */
function TaskMsg_OnSelectOrgInfo(objs) {
  var names = "";
  if (curTaskMsgInput.multi) {
    curTaskMsgInput.value = objs;
    for (var i = 0; i < objs.length; i++) {
      if (names != "") {
        names += "、";
      }
      var obj = objs[i];
      names += obj.name;
    }
  } else {
    curTaskMsgInput.value = objs;
    names = objs.name;
  }
  curTaskMsgInput.jQueryObj.find("input").val(names);
  if (curTaskMsgInput.onChange) eval(curTaskMsgInput.onChange);
}

$(function () {
  $(".TaskMsgForm").each(function (index) {
    var form = new TaskMsgForm();
    if ($(this).attr("id")) {
      form.id = $(this).attr("id");
    } else {
      form.id = "TaskMsgForm_" + index;
    }
    taskMsgForms[form.id] = form;
    form.init($(this));
  });
});