function jsTelParameterSet(){

};

jsTelParameterSet.CurPageIndex=1;

$(function() {
  jsTelParameterSet.LoadDataInfo();
});

/*初始化数据*/
jsTelParameterSet.LoadDataInfo=function(){
    var PhoneTypeName=PhoneHelper.Telphone_PhoneTypeName();
    if(PhoneTypeName=="E8")
    {
        document.getElementById("trPhoneShowText").style.display="none";
        document.getElementById("trNumberGroup").style.display="none";
    }
    else if(PhoneTypeName=="E6" || PhoneTypeName=="E7")
    {
        document.getElementById("trPhoneShowText").style.display="";
        document.getElementById("trNumberGroup").style.display="";
    }
      jsTelParameterSet.ShowQuickNumberPage();
    jsTelParameterSet.LoadDefaultData();/*加载数据*/
    jsTelParameterSet.RecordSettingsField(); /*查找录音设置字段如没有则增加字段*/
    jsTelParameterSet.LoadRecordSettingsTable();/*读取表*/
    jsTelParameterSet.LoadRecordSettingsField();/*读取录音设置字段*/
    var tbWidth = document.getElementById("tdContent").clientWidth;
    /*document.getElementById("btnSaveAudioTable").style.marginLeft = tbWidth - document.getElementById("btnSaveAudioTable").clientWidth-10;*/
    var divHeight=$(document.documentElement).height();
    document.getElementById("divAudioNumberList").style.height=divHeight-500;
    //    jsTelParameterSet.ShowAudioNumberTable();
    tbWidth=null;
    divHeight=null;
    CollectGarbage();
}

/*启动录音*/
jsTelParameterSet.EnableAudioClick=function(){
    var isEnable=$("#chkEnableAudio").attr("checked");
    if(isEnable){
        $("#rdoEntireCall").attr("disabled",false);
        $("#rdoSendCallOnly").attr("disabled",false);
        $("#rdoGoCallOnly").attr("disabled",false);
        $("#chkOutLine").attr("disabled",false);
        $("#rdoOnlyBeNO").attr("disabled",false);
    }else{
        $("#rdoEntireCall").attr("disabled","disabled");
        $("#rdoSendCallOnly").attr("disabled","disabled");
        $("#rdoGoCallOnly").attr("disabled","disabled");
        $("#chkOutLine").attr("disabled","disabled");
        $("#rdoOnlyBeNO").attr("checked",false);
        jsTelParameterSet.ShowAudioNumberTable(document.getElementById("rdoOnlyBeNO"));
         $("#rdoOnlyBeNO").attr("disabled","disabled");
    }
};

/*加载数据*/
jsTelParameterSet.LoadDefaultData=function(){
    var strSql="SELECT ParName,ParValue FROM T_TelParameterSet";
    var obj= DBReader.selectSqlForJson(strSql);
    var result;
    if(obj!=null)
    {
        result=eval("[{"+obj+"}]");
        if(result[0].result==1 && result[0].table!=undefined)
        {
            for(var i=0;i<result[0].table.length;i++)
            {
                var parName=result[0].table[i].ParName;
                var parValue=result[0].table[i].ParValue;
                if(parName=="AreaNum")
                {
                    $("#htxt_AreaNum").val(parValue);   
                }
                else if(parName=="ExitCode")
                {
                     $("#htxt_ExitCode").val(parValue);
                }
                else if(parName=="StandbyText")
                {
                    $("#htxt_StandbyText").val(parValue);
                }
                else if(parName=="InLineExitCode")
                {
                    $("#htxt_InLineExitCode").val(parValue);
                }
                else 
                {
                    if($("#htxtQuickTel_"+parName)!=undefined)
                    {
                        $("#htxtQuickTel_"+parName).val(parValue);
                    }
                }
            }
        }
    }        
};

/*上一页*/
jsTelParameterSet.PrePageClick=function(){
    jsTelParameterSet.CurPageIndex--;
    jsTelParameterSet.ShowQuickNumberPage();
};
/*下一页*/
jsTelParameterSet.NextPageClick=function(){
    jsTelParameterSet.CurPageIndex++;
    jsTelParameterSet.ShowQuickNumberPage();
};
/*按钮及表显示*/
jsTelParameterSet.ShowQuickNumberPage=function(){
    if(jsTelParameterSet.CurPageIndex==1){
      $("#btnPrePage").attr("disabled",true);
      $("#btnNextPage").attr("disabled",false);
    }
    else if(jsTelParameterSet.CurPageIndex==3){
     $("#btnPrePage").attr("disabled",false);
     $("#btnNextPage").attr("disabled",true);
    }
    else{
        $("#btnPrePage").attr("disabled",false);
        $("#btnNextPage").attr("disabled",false);
    }
    if(jsTelParameterSet.CurPageIndex==1){
        $("#tbPageIndex01").show();
        $("#tbPageIndex02").hide();
        $("#tbPageIndex03").hide();
    }
    else if(jsTelParameterSet.CurPageIndex==2){
        $("#tbPageIndex01").hide();
        $("#tbPageIndex02").show();
        $("#tbPageIndex03").hide();
    }
    else if(jsTelParameterSet.CurPageIndex==3){
        $("#tbPageIndex01").hide();
        $("#tbPageIndex02").hide();
        $("#tbPageIndex03").show();
    }
};

/*设置快捷号码*/
jsTelParameterSet.SaveQuickTelNumber=function(){
   $(document.body).mask("数据保存中,请稍后...");
        var tbObj=document.getElementById("tbPageIndex0"+jsTelParameterSet.CurPageIndex);
        var inputObjList=tbObj.getElementsByTagName("INPUT");
        var entity=jsEntity.CreateEntityByTableName_access("T_TemporaryTable","","","","ID");   
      
        for(var i=0;i<inputObjList.length;i++)
        {
            var number=inputObjList[i].value;
            number=jsCommonLogic.stripscript(number);
            var keyID=inputObjList[i].KeyID;              
/*           if(number!="")
              {*/
               var quickNumEntity=jsEntity.CreateEntityByTableName_access("T_TelParameterSet");   
               quickNumEntity.Fields.ParName=keyID;
               quickNumEntity.Fields.ParValue=number;
               var curID=jsCommonLogic.selColumnData("ID","T_TelParameterSet"," WHERE ParName='"+keyID+"'");
               if(curID>0)
               {
                    quickNumEntity.Fields.ID=curID;
                    entity.appendEntity(quickNumEntity,App.SqlAction.Update);
               }
               else
               {
                     entity.appendEntity(quickNumEntity,App.SqlAction.Insert);
               }
               PhoneHelper.SetStoreKey(keyID,number);/*设置话机*/
/*            }*/
        }
        entity.Fields.DataID=1;
        var result=jsCommonLogic.rcTemporary_Update(entity);
        if(result)
        {
            alert("保存成功！");
        }
        else
        {
            alert("保存没有成功！");
        }
   $(document.body).unmask();
};

/*设置区号*/
jsTelParameterSet.SetAreaNumClick=function(){
    var AreaCode=$.trim($("#htxt_AreaNum").val()).split('\"').join('“').split('\'').join('‘');  
    var curID=jsCommonLogic.selColumnData("ID","T_TelParameterSet"," WHERE ParName='AreaNum'");
    var entity=jsEntity.CreateEntityByTableName_access("T_TelParameterSet");   
    entity.Fields.ParValue=AreaCode;
    entity.Fields.ParName="AreaNum";
    if(curID>0)
    {
        entity.Fields.ID=curID;
        entity.Update(entity);
    }
    else
    {
        entity.Insert(entity);
    }
    PhoneHelper.SetAreaCode(AreaCode);/*设置到话机*/
    curID=null;
    DBReader.DisposeEntity(entity);
};
/*设置出局码*/
jsTelParameterSet.SetExitCodeClick=function(){
    var ExitCode=$.trim($("#htxt_ExitCode").val()).split('\"').join('“').split('\'').join('‘');
    var curID=jsCommonLogic.selColumnData("ID","T_TelParameterSet"," WHERE ParName='ExitCode'");
    var entity=jsEntity.CreateEntityByTableName_access("T_TelParameterSet");   
    entity.Fields.ParValue=ExitCode;     
    entity.Fields.ParName="ExitCode";
    if(curID>0)
    {
       entity.Fields.ID=curID;
       entity.Update(entity);
     }
    else
    {
        entity.Insert(entity);
     }
     PhoneHelper.SetOutCode(ExitCode);   /*设置到话机*/
     curID=null;
     DBReader.DisposeEntity(entity);
};
/*设置待机文字*/
jsTelParameterSet.SetStandbyTextClick=function(){ 
    var StandbyText=$.trim($("#htxt_StandbyText").val()).split('\"').join('“').split('\'').join('‘');
    var curID=jsCommonLogic.selColumnData("ID","T_TelParameterSet"," WHERE ParName='StandbyText'");
    var entity=jsEntity.CreateEntityByTableName_access("T_TelParameterSet");   
    entity.Fields.ParValue=StandbyText;    
    entity.Fields.ParName="StandbyText";
    if(curID>0)
    {
       entity.Fields.ID=curID;
       entity.Update(entity);
     }
    else
    {
        entity.Insert(entity);
     }
    PhoneHelper.SetWaitTxt(StandbyText);/*设置到话机*/
     curID=null;
     DBReader.DisposeEntity(entity);
};
/*显示指定号码录音*/
jsTelParameterSet.ShowAudioNumberTable = function(obj) {
    //    var selTime= $("input[name='callRecord'][checked]").val();
    if (obj.checked) {
        $("#btnAddRow").attr("disabled", false);
        var tableInput = document.getElementById("tdContent").getElementsByTagName("INPUT");
        var tableSelect = document.getElementById("tdContent").getElementsByTagName("SELECT");
        for (var i = 0; i < tableInput.length; i++) {
            tableInput[i].disabled = false;
        }
        for (var j = 0; j < tableSelect.length; j++) {
            tableSelect[j].disabled = false;
        }
//        $("#btnSaveAudioTable").attr("disabled", false);
    }
    else {
        $("#btnAddRow").attr("disabled", true);
        var tableInput = document.getElementById("tdContent").getElementsByTagName("INPUT");
        var tableSelect = document.getElementById("tdContent").getElementsByTagName("SELECT");
        for (var i = 0; i < tableInput.length; i++) {
            tableInput[i].disabled = "disabled";
        }
        for (var j = 0; j < tableSelect.length; j++) {
            tableSelect[j].disabled = "disabled";
        }
//        $("#btnSaveAudioTable").attr("disabled", true);
    }
};

/*条件*/
jsTelParameterSet.QueryString = "<select style='width:80%'><option value='1'>等于</option><option value='2'>包含</option><option value='3'>开头为</option></select>";
/*号码值*/
jsTelParameterSet.FieldNumString = "<input type='text' class='ipt'onblur='jsCommonLogic.dataValidate(3,this);'/>";
/*操作串*/
jsTelParameterSet.ConditionOperateString="<input type='button' class='button' value='删除' onclick='jsTelParameterSet.deleteCondition(this);'/>";
/*设置录音设置字段*/
jsTelParameterSet.RecordSettingsField = function() {
    try {
        var objStr = DBReader.selectSqlForJson('select * from T_TelParameterSet');
        var obj = $.parseJSON("[{" + objStr + "}]");
        var fieldArray = ["IsOutLine", "CallType", "DesignatedRecord","EnableAudio"];
        if (obj[0].table != undefined && obj[0].table.length > 0) {
            for (var i = 0; i < obj[0].table.length; i++) {
                for (var j = 0; j < fieldArray.length; j++) {
                    if (obj[0].table[i].ParName == fieldArray[j]) {
                        break;
                    }
                    if (j == fieldArray.length) {
                        var entity = jsEntity.CreateEntityByTableName_access("T_TelParameterSet");
                        entity.Fields.ParName = fieldArray[j];
                        entity.Fields.ParValue = 0;
                        entity.Insert(entity);
                        DBReader.DisposeEntity(entity);
                    }
                }
            }
        } else {
            for (var j = 0; j < fieldArray.length; j++) {
                var entity = jsEntity.CreateEntityByTableName_access("T_TelParameterSet");
                entity.Fields.ParName = fieldArray[j];
                entity.Fields.ParValue = 0;
                entity.Insert(entity);
                DBReader.DisposeEntity(entity);
            }
        }
        objStr = null;
        obj = null;
        fieldArray = null;
    } catch (e) {
        alert("ERROR FOR jsTelParameterSet.RecordSettingsField" + e.message);
    }
    CollectGarbage();
}

/*保存录音设置*/
jsTelParameterSet.SaveRecordSettings = function() {
    try {
        var fieldArray = ["IsOutLine", "CallType", "DesignatedRecord","EnableAudio"];
        var selTime = $("input[name='callRecord'][checked]").val();
        var outLine = $("#chkOutLine").attr("checked") ? 1 : 0;
        var onlyBeNO = $("#rdoOnlyBeNO").attr("checked") ? 1 : 0;
        var enableAudio=$("#chkEnableAudio").attr("checked") ? 1 : 0;
        for (var j = 0; j < fieldArray.length; j++) {
            var entity = jsEntity.CreateEntityByTableName_access("T_TelParameterSet");
            var curID = jsCommonLogic.selColumnData("ID", "T_TelParameterSet", " WHERE ParName='" + fieldArray[j] + "'");
            entity.Fields.ParName = fieldArray[j];
            if (fieldArray[j] == "IsOutLine") {
                entity.Fields.ParValue = outLine;
            } else if (fieldArray[j] == "CallType") {
                entity.Fields.ParValue = selTime;
            } else if (fieldArray[j] == "DesignatedRecord") {
                entity.Fields.ParValue = onlyBeNO;
            }else if(fieldArray[j]=="EnableAudio"){
                entity.Fields.ParValue=enableAudio;
            }
            if(curID>0)
            {
                entity.Fields.ID = curID;
                entity.Update(entity);
            }
            else
            {
                entity.Insert(entity);
            }
            DBReader.DisposeEntity(entity);
        }
        if (onlyBeNO == 1) {
            DBReader.execSql("delete from T_RecordNumberSettings");
            $("tr", tdContent).each(function() {
                if (this.ParentID != undefined) {
                    var entity = jsEntity.CreateEntityByTableName_access("T_RecordNumberSettings");
                    entity.Fields.Condition = this.cells[0].childNodes[0].value;
                    entity.Fields.NumberValue = this.cells[1].childNodes[0].value;
                    entity.Fields.DesignatedRecord = 1;
                    entity.Insert(entity);
                    DBReader.DisposeEntity(entity);
                }
            });
        }
        alert("保存成功！");
    } catch (e) {
        alert("ERROR FOR jsTelParameterSet.SaveRecordSettings" + e.message);
    }
}
/*读取录音设置字段*/
jsTelParameterSet.LoadRecordSettingsField = function() {
    try {
        var obj = $.parseJSON("[{" + DBReader.selectSqlForJson('select * from T_TelParameterSet') + "}]");
        for (var i = 0; i < obj[0].table.length; i++) {
            if (obj[0].table[i].ParName == "IsOutLine") {
                if (obj[0].table[i].ParValue.toLowerCase() == "true" || obj[0].table[i].ParValue == 1) {
                    $("#chkOutLine").attr("checked", "checked");
                } else {
                    $("#chkOutLine").removeAttr("checked");
                }
            } else if (obj[0].table[i].ParName == "CallType") {
                if (obj[0].table[i].ParValue == 0) {
                    $("#rdoEntireCall").attr("checked", "checked");
                } else if (obj[0].table[i].ParValue == 1) {
                    $("#rdoSendCallOnly").attr("checked", "checked");
                } else if (obj[0].table[i].ParValue == 2) {
                    $("#rdoGoCallOnly").attr("checked", "checked");
                }
            } else if (obj[0].table[i].ParName == "DesignatedRecord") {
                if (obj[0].table[i].ParValue.toLowerCase() == "true" || obj[0].table[i].ParValue == 1) {
                    $("#rdoOnlyBeNO").attr("checked", "checked");
                } else {
                    $("#rdoOnlyBeNO").removeAttr("checked");
                }
                jsTelParameterSet.ShowAudioNumberTable(document.getElementById("rdoOnlyBeNO"));
            }else if(obj[0].table[i].ParName =="EnableAudio")
            {
                if (obj[0].table[i].ParValue.toLowerCase() == "true" || obj[0].table[i].ParValue == 1) {
                    $("#chkEnableAudio").attr("checked", "checked");
                } else {
                    $("#chkEnableAudio").removeAttr("checked");
                }
                jsTelParameterSet.EnableAudioClick();
            }
        }
    } catch (e) {
        alert("ERROR FOR jsTelParameterSet.LoadRecordSettingsField" + e.message);
    }
}

/*读取表*/
jsTelParameterSet.LoadRecordSettingsTable = function() {
    try {
        var objStr = DBReader.selectSqlForJson('select * from T_RecordNumberSettings');
        var obj = $.parseJSON("[{" + objStr + "}]");
        if (obj[0].table != undefined && obj[0].table.length > 0) {
            var rowObj = document.getElementById("trSheetTitle");
            for (var i = 0; i < obj[0].table.length; i++) {
                var newRowObj = null;
                var insertIndex = parseInt(parseInt(rowObj.rowIndex) + parseInt(rowObj.ItemCount) + 1);
                newRowObj = document.getElementById("tdContent").insertRow(insertIndex);

                /*本组条件个数*/
                rowObj.ItemCount = parseInt(rowObj.ItemCount) + 1;
                var newItemIndex = rowObj.ItemCount - 1;
                newRowObj.ItemIndex = newItemIndex;
                newRowObj.id = "ItemCondition_" + rowObj.Path + "_" + newRowObj.ItemIndex;
                newRowObj.ParentID = rowObj.id;
                newRowObj.Path = rowObj.Path + "_" + (newRowObj.ItemIndex + 1);
                var ParentPath = rowObj.Path;

                var newCellObj = newRowObj.insertCell(0);
                newCellObj.innerHTML = jsTelParameterSet.QueryString;
                newCellObj.childNodes[0].value = obj[0].table[i].Condition;
                newCellObj = null;
                newCellObj = newRowObj.insertCell(1);
                newCellObj.innerHTML = jsTelParameterSet.FieldNumString;
                newCellObj.childNodes[0].value = obj[0].table[i].NumberValue;
                newCellObj = null;
                newCellObj = newRowObj.insertCell(2);
                newCellObj.innerHTML = jsTelParameterSet.ConditionOperateString;
                newCellObj = null;

                newRowObj = null;
            }
        }
    } catch (e) {
        alert("ERROR FOR jsTelParameterSet.LoadRecordSettingsTable" + e.message);
    }
    CollectGarbage();
}
/*添加行*/
jsTelParameterSet.AddRows=function(){
     var rowObj=document.getElementById("trSheetTitle");
     try
     {        
        var newRowObj = null;
        var insertIndex=parseInt(parseInt(rowObj.rowIndex) + parseInt(rowObj.ItemCount) + 1);
        newRowObj = document.getElementById("tdContent").insertRow(1);
        
        /*本组条件个数*/
        rowObj.ItemCount = parseInt(rowObj.ItemCount) + 1;
        var newItemIndex=rowObj.ItemCount - 1;
        newRowObj.ItemIndex =newItemIndex;
        newRowObj.id = "ItemCondition_" + rowObj.Path + "_" + newRowObj.ItemIndex;
        newRowObj.ParentID = rowObj.id;
        newRowObj.Path = rowObj.Path + "_" + (newRowObj.ItemIndex + 1);
        var ParentPath=rowObj.Path;
        
        var newCellObj=newRowObj.insertCell(0);
        newCellObj.innerHTML=jsTelParameterSet.QueryString;
        newCellObj=null;        
        newCellObj=newRowObj.insertCell(1);
        newCellObj.innerHTML=jsTelParameterSet.FieldNumString;
        newCellObj=null;
        newCellObj=newRowObj.insertCell(2);
        newCellObj.innerHTML=jsTelParameterSet.ConditionOperateString;
        newCellObj=null; 
        
        newRowObj=null;

  //     window.clipboardData.setData ("text",document.getElementById("tdContent").innerHTML);
    }
    catch(e)
    {
        alert(e.message);
    }
    rowObj=null;
    CollectGarbage();
};

/*删除行*/
jsTelParameterSet.deleteCondition=function(obj){
     var rowObj = obj.parentElement.parentElement;
    var itemCount=document.getElementById("trSheetTitle").ItemCount;
    var currentIndex = rowObj.rowIndex;
    document.getElementById("tdContent").deleteRow(currentIndex);
    document.getElementById("trSheetTitle").ItemCount=itemCount-1;
};
/*设置内线加拨*/
jsTelParameterSet.SetInLineExitCodeClick=function()
{
    try
    {
        var ExitCode=$.trim($("#htxt_InLineExitCode").val()).split('\"').join('“').split('\'').join('‘');
        var curID=jsCommonLogic.selColumnData("ID","T_TelParameterSet"," WHERE ParName='InLineExitCode'");
        var entity=jsEntity.CreateEntityByTableName_access("T_TelParameterSet");   
        entity.Fields.ParValue=ExitCode;     
        entity.Fields.ParName="InLineExitCode";
        if(curID>0)
        {
           entity.Fields.ID=curID;
           entity.Update(entity);
        }
        else
        {
            entity.Insert(entity);
        }
        DBReader.DisposeEntity(entity);
        curID=null;
        ExitCode=null;
        alert("设置成功");
    }
    catch(e)
    {
        alert("jsTelParameterSet.SetInLineExitCodeClick:"+e.message);
    }
    CollectGarbage();
};
