function jsSendMesage(){

}
jsSendMesage.grid=null;
jsSendMesage.hideSID=null;

$(function(){
    App.SetNullHref(0);
    $("#txtAreaMessage").ikeEditor({ width: $(document.documentElement).width()-200, height: 150, HiddentToolBar: true, ValueMode: 'Text' });
    App.SetNullHref(1);
    jsSendMesage.initToolBar();
    jsSendMesage.LoadSetGrid();
    setTimeout("jsSendMesage.LoadTxtMessage()",150);  
});

/*携带签名*/
jsSendMesage.changesSignatur = function(obj) {
    if (obj.checked) {
        document.getElementById("txtSign").disabled = false;
    } else {
        document.getElementById("txtSign").disabled = true;
    }
}

/*由草稿列表转到短信发送时加载短信内容*/
jsSendMesage.LoadTxtMessage=function(){
    if(App.query("PreID")!=undefined && App.query("PreID")>0){
        var strSql="SELECT * FROM T_SMSSEND WHERE SID="+App.query("PreID");
        var strResult=DBReader.selectSqlForJson(strSql);
        if(strResult!=null)
        {
            var result=eval("[{"+strResult+"}]");
            if(result[0].table!=undefined){
                jsSendMesage.hideSID=result[0].table[0].SID;
                if(result[0].table[0].INC_SIGN==1 || result[0].table[0].INC_SIGN=="True"){
                     $("#chkSign").attr("checked","checked") ;
                     $("#txtSign").val(result[0].table[0].SIGNNAME);
                     document.getElementById("txtSign").disabled = false;
                }
                if(result[0].table[0].INC_NICK==1|| result[0].table[0].INC_NICK=="True"){
                     $("#chkNickName").attr("checked","checked") ;
                }
                $("#txtAreaMessage").value(result[0].table[0].CONTENT);
            }   
        }    
//        var txtSql="DELETE * FROM T_SMSPHRASE WHERE ID="+App.query("PreID");
//        DBReader.execSql(txtSql);
//        txtSql=null;
//        CollectGarbage();
    }
};

/*选择模板*/
jsSendMesage.OpenModelWin=function(){
    ShowWindow.ShowMoledDialog("Application/TeleSecretary/SMSMessage/frmModelList.htm?formIdx=" + ShowWindow.getFormIdx() + "&action=jsIndexPage.freshItemItemPage('tab-messageSend','jsSendMesage.SetSMSContent(\"@Mid\")')&pageStatus=sel", "选择短信模板", 800, 560, 0);
}

jsSendMesage.SetSMSContent=function(MID){
     $("#txtAreaMessage").value(jsCommonLogic.selColumnData("SMSTXT","T_SMSPHRASE"," WHERE ID ="+MID));
};

/*号码列表*/
jsSendMesage.LoadSetGrid=function(){
    try {
         var w = $(document.documentElement).width()-200;
         var gh = $(document.documentElement).height() - $("#divOperate").height()-320;
    /*<-------------------------------------------------------------主列表---------------------------------------------------->*/
         var option = {
             height: gh,
             width: w,
             colModel:
             [
                { display: '手机号', name: 'MOBILE', width: (w * 0.2), sortable: false, align: 'left'},
                { display: '联系人', name: 'NAME', width: (w * 0.1), sortable: false, align: 'left'},
	            { display: '昵称', name: 'NICKNAME', width: (w * 0.1), sortable: false, align: 'left' },	                  
	            { display: '公司简称', name: 'SIMPLE_NAME', width: (w * 0.3), sortable: false, align: 'left'},	          
	            { display: '删除', name: 'ID', width: (w * 0.08), align: 'center',sortable: false,process: jsSendMesage.delButton},
	            { display: '公司ID', name: 'COMPANY_ID', width: (w * 0.3), sortable: false, align: 'left',hide:true}  
		     ],
             customFunction: jsSendMesage.ShowPage,
             rp: 10000,
             usepager:false,
             rowHeight:24,
             rowbinddata:true,
             title:"接收号码列表"
         };
         jsSendMesage.grid = $("#setGrid").ikegrid(option);
    }
    catch (e) {
        alert("jsSendMesage.LoadSetGrid:" + e.message);
    }
}

jsSendMesage.delButton=function(value, pid,rows) {
    return '<div onclick="jsSendMesage.delMethod(\''+pid+'\');" style="cursor:pointer;"><img src="../../AppFrame/APICss/default/ico/user_delete.gif" />删除</div>';
}
/*---删除行方法---------------------------------------------------------------------------------------*/
jsSendMesage.delMethod = function(rowID) {
    var rowObj=jsSendMesage.grid.getRowByID(rowID);
    jsSendMesage.grid.deleteRow(rowObj.rowIndex);
}

jsSendMesage.ShowPage=function(){
    var result=$.parseJSON('[{"result" :"0","returnval" :"暂时没有结果"}]');
    return result[0];
}

/*手动添加号码到列表*/
jsSendMesage.AddRowToGrid=function(){
    var strMobile=$("#txtMobile").val().split('\"').join('“').split('\'').join('‘');
    if(strMobile!=""){
        var isHave=false;
        var store=jsSendMesage.grid.getRowData();
        for(var i=0;i<store.length;i++){
            if(store[i].MOBILE==strMobile){
                isHave=true;
            }
        }
        if(!isHave){
            var newRowObj=jsSendMesage.grid.addRow(0);    
            var initValue = [{ 'MOBILE': strMobile, 'NAME': '', 'NICKNAME': '', 'SIMPLE_NAME': '','COMPANY_ID':''}];
            jsSendMesage.grid.setRowData(newRowObj,initValue);
            newRowObj=null;
            initValue=null;
            $("#txtMobile").val("");
        }else{
            alert("已经存在该号码！");
        }
    }
    strMobile=null;
    CollectGarbage();    
}

/*操作按钮*/
jsSendMesage.initToolBar=function(){
     var potion = {
            items: [
                   { type: 'button', text: '查询', className: 'tableAdd', useable: 'T',
                        handler: function() {
                           ShowWindow.ShowMoledDialog("Application/TeleSecretary/ConditionSetting/frmConditionSetting.htm?formIdx=" + ShowWindow.getFormIdx() + "&action=jsIndexPage.freshItemItemPage('tab-messageSend','jsSendMesage.QueryOperating(\"@str\")')", "高级查询", 1000, 560, 0);
                        }
                   },
                   { type: 'button', text: '清空', className: 'tableCancel', useable: 'T',
                        handler: function() {
                            jsSendMesage.grid.clearRow();
                        }
                   }
          ]
     }
    document.all("divOperate").innerHTML = "";
    $("#divOperate").toolbar(potion);
}
/*查询返回结果处理*/
jsSendMesage.QueryOperating=function(dataStr){
    var strSql="SELECT T_EMPLOYEE.MOBILE,T_EMPLOYEE.NICKNAME,T_EMPLOYEE.NAME,T_COMPANY.COMPANY_ID,T_COMPANY.SIMPLE_NAME FROM T_EMPLOYEE LEFT JOIN T_COMPANY ON T_EMPLOYEE.COMPANY_ID=T_COMPANY.COMPANY_ID where "+unescape(dataStr);
    var strResult = DBReader.selectSqlForJson(strSql); //window.clipboardData.setData("text",strSql);
    if (strResult != null) {
        var  result = eval("[{" + strResult + "}]");
        if (result[0].table != undefined) {
            for(var i=0;i<result[0].table.length;i++){
                if(result[0].table[i].MOBILE!=""){
                    var newRowObj=jsSendMesage.grid.addRow(0);    
                    var initValue = [{ 'MOBILE': result[0].table[i].MOBILE, 'NAME':result[0].table[i].NAME, 'NICKNAME': result[0].table[i].NICKNAME, 'SIMPLE_NAME': result[0].table[i].SIMPLE_NAME,'COMPANY_ID':result[0].table[i].COMPANY_ID}];
                    jsSendMesage.grid.setRowData(newRowObj,initValue);
                    newRowObj=null;
                    initValue=null;
                }
            }
        }
        result=null;
    }
    strResult=null;
    strSql=null;
    CollectGarbage();
}

/*保存发送短信信息*/
jsSendMesage.SaveSendSMSMessage = function() {
    var result=-1;
    var strContent = $("#txtAreaMessage").value().split('\"').join('“').split('\'').join('‘');
    var conSubsection=(strContent.length/48).toString().split(".")[0];
    conSubsection = parseInt(conSubsection)+1;
    var strSign = $("#txtSign").val().split('\"').join('“').split('\'').join('‘');
    var store = jsSendMesage.grid.getRowData();
    if (strContent != "" && store != null && store.length > 0) {
        /*短信发送记录表*/
        var sendEntity = jsEntity.CreateEntityByTableName_access("T_SMSSEND", "", "", "", "SID");
        sendEntity.Fields.USERID = 1;
        sendEntity.Fields.SIGNNAME= strSign;
        sendEntity.Fields.INC_NICK =$("#chkNickName").attr("checked") == false ? 0 : 1;/*是否包含昵称*/
        sendEntity.Fields.INC_SIGN = $("#chkSign").attr("checked") == false ? 0 : 1;/*是否包含签名*/
        sendEntity.Fields.ISDRAFT=0 ;/*是否草稿*/
        sendEntity.Fields.ISTIMER=0;/*是否定时发送*/
        sendEntity.Fields.CONTENT = escape(strContent);
        sendEntity.Fields.ACCOUNTID=strContent.length;

        for (var i = 0; i < store.length; i++) {
            /*短信接收人记录*/
            var toEntity = jsEntity.CreateEntityByTableName_access("T_SMSTO", "SID", "T_SMSSEND", "SMSID", "TOID");
            if(jsSendMesage.hideSID>0){
                toEntity.Fields.SMSID=jsSendMesage.hideSID;
            }
            toEntity.Fields.CUSTOMERID=store[i].COMPANY_ID;
            toEntity.Fields.NAME = unescape(store[i].NAME);
            toEntity.Fields.NICKNAME = unescape(store[i].NICKNAME);
            toEntity.Fields.MOBILE = store[i].MOBILE;
            //toEntity.Fields.COMPANYNAME=unescape(store[i].SIMPLE_NAME);
            toEntity.Fields.MAKEDATE = DBReader.ServerTime().ServerFormat("yyyy-MM-dd hh:mm:ss");
            toEntity.Fields.SUBSECTION=conSubsection;
            sendEntity.appendEntity(toEntity, App.SqlAction.Insert);
            DBReader.DisposeEntity(toEntity);
        }
        if(jsSendMesage.hideSID>0){
            sendEntity.Fields.SID=jsSendMesage.hideSID;
            result = sendEntity.Update(sendEntity);
        }else{
            result = sendEntity.Insert(sendEntity);
        }
        if(result){
            alert("保存成功，等待发送！");
            jsSendMesage.grid.clearRow();
            jsSendMesage.hideSID=null;     
            $("#txtAreaMessage").value("");      
        } else {
            alert("发送失败！");
        }
        DBReader.DisposeEntity(sendEntity);
    } else {
        alert("用户列表和短信内容不能为空！");
    }
    strContent = null;
    strSign = null;
    conSubsection=null;
    store = null;
    result=null;
    CollectGarbage();
};

/*保存到草稿*/
jsSendMesage.SavePreSendMessage=function(){
    var result=-1;
    var strContent = $("#txtAreaMessage").value().split('\"').join('“').split('\'').join('‘');
    var strSign = $("#txtSign").val().split('\"').join('“').split('\'').join('‘');
    if (strContent != "" ) {
        var entity = jsEntity.CreateEntityByTableName_access("T_SMSSEND", "", "", "", "SID");
        entity.Fields.USERID = 1;
        entity.Fields.SIGNNAME = strSign;
        entity.Fields.INC_NICK =$("#chkNickName").attr("checked") == false ? 0 : 1;/*是否包含昵称*/
        entity.Fields.INC_SIGN = $("#chkSign").attr("checked") == false ? 0 : 1;/*是否包含签名*/
        entity.Fields.ISDRAFT=1;/*是否草稿*/
        entity.Fields.ISTIMER=0;/*是否定时发送*/
        entity.Fields.CONTENT = escape(strContent);
        entity.Fields.ACCOUNTID=strContent.length;
        if(jsSendMesage.hideSID>0){
            entity.Fields.SID=jsSendMesage.hideSID;
            result=entity.Update(entity);
        }else{
            result = entity.Insert(entity);
        }
        if(result){
            alert("已保存到草稿箱！");
            $("#txtAreaMessage").value("");
        }
        DBReader.Dispose(entity);
        result=null;
    }else{
        alert("短信内容不能为空！");
    }      
    strContent=null;
    strSign=null;
    CollectGarbage();
}