function jsAddressBookColSet(){

};
/*列设置列表*/
jsAddressBookColSet.ColumnSetGrid=null;/*列表*/
jsAddressBookColSet.IsHaveColSetFile=null;/*是否已经存在列设置JS文件*/

jsAddressBookColSet.IsHavePerInfoSetJs=null;
jsAddressBookColSet.IsHaveComInfoSetJs=null;

$(function(){
    jsAddressBookColSet.LoadJsColumnSetInfo();
    jsAddressBookColSet.initColumnSetGrid();
    jsAddressBookColSet.LoadPersionalInfoDom(); /*始终以模板内容为主*/
});

/*注入用户设置结果JS*/
jsAddressBookColSet.LoadJsColumnSetInfo=function(){
   if(App.FileIsExist(escape(App.ApplicationPath()+"Application\\TeleSecretary\\js\\jsColSetFile.js")))   
   {
        App.SetNullHref(0);
        var obj=document.getElementsByTagName("HEAD");
        var scriptObj=document.createElement("script");
        scriptObj.src="../js/jsColSetFile.js";
        scriptObj.id="js_ColumnSetFile";
        obj[0].appendChild(scriptObj);
        jsAddressBookColSet.IsHaveColSetFile=1;
//        setTimeout("jsAddressBookColSet.LoadColSetInfoByJsFile()",100);        
//        obj[0].removeChild(document.getElementById(scriptObj.id)); 
        scriptObj=null;
        obj=null;
        App.SetNullHref(1);
   }
};

/*直接加载用户设置结果——当前没有使用*/
jsAddressBookColSet.LoadColSetInfoByJsFile=function(){
    var setObj=eval("["+ jsColSetFile.ColSetString+"]");
    var i=setObj.length-1;
    for(;i>-1;i--)
    {
        var newRowObj=  jsAddressBookColSet.ColumnSetGrid.addRow(0);
        var initValue = [{'TableName':setObj[i].TbName,'DataField':setObj[i].DataField , 'Title': unescape(setObj[i].Title),'IsDisplay':setObj[i].IsDisplay,'IsOrder':setObj[i].IsOrder,'IsExport':setObj[i].IsExport}];
        jsAddressBookColSet.ColumnSetGrid.setRowData(newRowObj,initValue);
        
        newRowObj=null;
        initValue=null;
        CollectGarbage();
    }
};

/*列设置列表*/
jsAddressBookColSet.initColumnSetGrid=function(){
  try {
        var w = $( document.documentElement).width()-2;
         var gh = $( document.documentElement).height()-80;
         var option = {
             height: gh,
             width: w,
             colModel:[
                { display: '标题', name: 'Title', width: (w * 0.3), sortable: true, align: 'left'},
                { display: '显示', name: 'IsDisplay', width: (w * 0.1), sortable: true, align: 'center',process:jsAddressBookColSet.AllowDisplayOrNot,processType:'check'},
                { display: '排序', name: 'IsOrder', width: (w * 0.1), sortable: true, align: 'center',process:jsAddressBookColSet.AllowOrderOrNot,processType:'check'},                  
	            { display: '操作', name: 'ID', width: (w * 0.35), sortable: true,process: jsAddressBookColSet.FormatGridOperate,align:'center'},
                { display: '导出', name: 'IsExport', width: (w * 0.1), sortable: true, align: 'center',process:jsAddressBookColSet.AllowExportOrNOt,processType:'check',hide:true},
                { display: '表名', name: 'TableName', width: (w * 0.1), sortable: true, align: 'left',hide:true},
                { display: '字段名', name: 'DataField', width: (w * 0.1), sortable: true, align: 'left',hide:true}
		     ],           
             customFunction: jsAddressBookColSet.ShowColumnSet,
//             rowhandler: jsAddressBookColSet.ColSetRowClick,             
//             showcheckbox: true,
//             rowcheckboxselect: false,
//             selectedonclick: true,
             rowbinddata:true,
             rp: 10000,
             usepager:false,
             rowHeight:24
         };
        jsAddressBookColSet.ColumnSetGrid = $("#tbColSetGrid").ikegrid(option);
    }
    catch (e) {
        alert("jsAddressBookColSet.initColumnSetGrid:" + e.message);
    }
};

/*单击行*/
jsAddressBookColSet.ColSetRowClick=function(target){
    $(target).click(function(){
        var obj = event.srcElement;
        if (obj.childNodes[0] != null && (obj.childNodes[0].className != "itemchk" && obj.className != "chboxtd") || obj.tagName == "IMG") {
            /*单选行改行样式*/
            $("tr", target.parentElement.parentElement).each(function() {
                this.childNodes[0].childNodes[0].children[0].checked = false;
                $(this).removeClass("trSelected");
            });
        }
        $(target).addClass("trSelected");
        target.childNodes[0].childNodes[0].children[0].checked = true;
        /*===============*/
    });
}

/*是否显示*/
jsAddressBookColSet.AllowDisplayOrNot=function(value,pid,rows){
    return ' <input id="chkAllowDisplay_'+pid+'" type="checkbox" />';
}
/*是否排序*/
jsAddressBookColSet.AllowOrderOrNot=function(value,pid,rows){
    return ' <input id="chkAllowOrder_'+pid+'" type="checkbox" />';
}
/*是否导出*/
jsAddressBookColSet.AllowExportOrNOt=function(value,pid,rows){
    return ' <input id="chkAllowExport_'+pid+'" type="checkbox" />';
}
/*列表操作：上移|下移*/
jsAddressBookColSet.FormatGridOperate=function(value,pid,rows,rowIndex){
      return '<input id="btnUp_'+pid+'" type="button" value="上移" class="buttonList" onclick="jsAddressBookColSet.UpCurrRow(\''+pid+'\')"/>&nbsp;<input id="btnNext_'+pid+'" type="button" value="下移" class="buttonList" onclick="jsAddressBookColSet.DownCurrRow(\''+pid+'\')"/>';
};

/*上移*/
jsAddressBookColSet.UpCurrRow=function(rID){
    try {
        var upRow =  jsAddressBookColSet.ColumnSetGrid.getRowByID(rID);
        var index = upRow.rowIndex;
        if (index > 0) {
            var RowObj =  jsAddressBookColSet.ColumnSetGrid.addRow(index - 1);
            RowObj.id = rID;
            for (var i = 0; i < upRow.cells.length; i++) {
                RowObj.cells(i).innerHTML = upRow.cells(i).innerHTML
            }
             jsAddressBookColSet.ColumnSetGrid.deleteRow(index + 1);
            RowObj = null;
        }
    }
    catch (e) {
        alert("Error for UpCurrRow:" + e.message);
    }
    rID = null;
    index = null;
    upRow = null;
    CollectGarbage();
//    window.clipboardData.setData("text",document.getElementById("tbColSetGrid").outerHTML)
};

/*下移*/
jsAddressBookColSet.DownCurrRow=function(rID){
     try {
        var DownRow = jsAddressBookColSet.ColumnSetGrid.getRowByID(rID);
        var index = DownRow.rowIndex;
        var upRow = jsAddressBookColSet.ColumnSetGrid.getRowByIndex(index + 1);
        if (upRow != undefined) {
            var RowObj = jsAddressBookColSet.ColumnSetGrid.addRow(index + 2);
            RowObj.id = rID;
            for (var i = 0; i < DownRow.cells.length; i++) {
                RowObj.cells(i).innerHTML = DownRow.cells(i).innerHTML
            }
            jsAddressBookColSet.ColumnSetGrid.deleteRow(index);
            RowObj = null;
        }
    }
    catch (e) {
        alert("Error for DownCurrRow:" + e.message);
    }
    rID = null;
    DownRow = null;
    index = null;
    upRow = null;
    CollectGarbage();
};

/*列表获得数据源*/
jsAddressBookColSet.ShowColumnSet=function(){
       var result=$.parseJSON('[{"result" :"0","returnval" :"暂时没有结果"}]');
       return result[0];
};

/*注入系统系统JS结果*/
jsAddressBookColSet.LoadPersionalInfoDom=function(){
   jsAddressBookColSet.IsHaveComInfoSetJs=null;
   jsAddressBookColSet.IsHavePerInfoSetJs=null;
   if(App.FileIsExist(escape(App.ApplicationPath()+"Application\\TeleSecretary\\js\\jsDepartmentInformation.js")))   
   {
        App.SetNullHref(0);
        var comObj=document.getElementsByTagName("HEAD");
        var comScriptObj=document.createElement("script");
        comScriptObj.src="../js/jsDepartmentInformation.js";
        comScriptObj.id="js_DepartmentInformation";
        comObj[0].appendChild(comScriptObj);
//        alert(unescape(jsDepartmentInformation.dataJSON));
        //setTimeout("jsAddressBookColSet.AddPersionalOrCompanyInfo('com')",100);
        jsAddressBookColSet.IsHaveComInfoSetJs=1;
        
//        comObj[0].removeChild(document.getElementById(comScriptObj.id)); 
        comScriptObj=null;
        comObj=null;
        App.SetNullHref(1);
   }  
   if(App.FileIsExist(escape(App.ApplicationPath()+"Application\\TeleSecretary\\js\\jsPersonalInformation.js")))
   {
        App.SetNullHref(0);
        var divObj=document.getElementsByTagName("HEAD");
        var scriptObj=document.createElement("script");
        scriptObj.src="../js/jsPersonalInformation.js";
        scriptObj.id="js_PersonalInformation";
        divObj[0].appendChild(scriptObj);
//        alert(unescape(jsPersonalInformation.dataJSON));
    //    setTimeout("jsAddressBookColSet.AddPersionalOrCompanyInfo('per')",150);
        jsAddressBookColSet.IsHavePerInfoSetJs=1;
        
//       divObj[0].removeChild(document.getElementById(scriptObj.id)); 
        scriptObj=null;
        divObj=null;
        App.SetNullHref(1);
   }
   /*加载设置内容*/
   setTimeout("jsAddressBookColSet.GetStrTableArr()",100);
};

jsAddressBookColSet.GetStrTableArr=function()
{
    var strTbArr="";
    if(jsAddressBookColSet.IsHaveComInfoSetJs==1)
    {
        strTbArr +=   jsAddressBookColSet.AddPersionalOrCompanyInfo("per");
    }
    if(jsAddressBookColSet.IsHavePerInfoSetJs==1)
    {
        if(strTbArr!="")
        {
            strTbArr+=",";
        }
         strTbArr+=jsAddressBookColSet.AddPersionalOrCompanyInfo("com");
    }
//    alert(strTbArr);
    jsAddressBookColSet.BindDataInfoBySetInfo(strTbArr);
}

/*增加行到列表*/
jsAddressBookColSet.BindDataInfoBySetInfo=function(strTableArr){
if(strTableArr!="")
{
    /*模板*/
    var obj=eval("["+ strTableArr +"]");
    if( jsAddressBookColSet.IsHaveColSetFile==1&& jsColSetFile.ColSetString!="")
    {         
        /*用户设置结果*/ 
        var colSetObj=eval("["+jsColSetFile.ColSetString+"]"); 
        var k=colSetObj.length-1;
        var isAddColumn="";
        if(jsColSetFile.ColSetString.indexOf(escape("单位名称"))==-1 && jsColSetFile.ColSetString.indexOf("SIMPLE_NAME")==-1){
            var defRowObj=jsAddressBookColSet.ColumnSetGrid.addRow(0);
            var defValue=[{'TableName':'T_COMPANY','DataField': 'SIMPLE_NAME','Title': '单位名称','IsDisplay':0,'IsOrder':1,'IsExport':1}];
            jsAddressBookColSet.ColumnSetGrid.setRowData(defRowObj,defValue);
            defRowObj=null;
            defValue=null;
        }
        for(;k>-1;k--)
        {
            /*根据用户设置的结果集顺序判断当前字段仍在模板中则直接加载用户设置*/
            if(strTableArr.indexOf('"Title":"'+unescape(colSetObj[k].Title)+'"')!=-1 && strTableArr.indexOf(colSetObj[k].DataField)!=-1)
            {
                var newRowObj=  jsAddressBookColSet.ColumnSetGrid.addRow(0);
                var initValue = [{'TableName':colSetObj[k].TbName,'DataField': colSetObj[k].DataField , 'Title': unescape(colSetObj[k].Title),'IsDisplay':colSetObj[k].IsDisplay,'IsOrder':colSetObj[k].IsOrder,'IsExport':colSetObj[k].IsExport}];
                jsAddressBookColSet.ColumnSetGrid.setRowData(newRowObj,initValue);
                newRowObj=null;
                initValue=null;
                if(isAddColumn!="")
                {
                    isAddColumn+=",";
                }
                isAddColumn+=colSetObj[k].DataField+":"+unescape(colSetObj[k].Title);
            } 
             if(unescape(colSetObj[k].Title)=="单位名称" && colSetObj[k].DataField=="SIMPLE_NAME"){
                    var defRowObj=jsAddressBookColSet.ColumnSetGrid.addRow(0);
                    var defValue=[{'TableName':colSetObj[k].TbName,'DataField': colSetObj[k].DataField , 'Title': unescape(colSetObj[k].Title),'IsDisplay':colSetObj[k].IsDisplay,'IsOrder':colSetObj[k].IsOrder,'IsExport':colSetObj[k].IsExport}];
                    jsAddressBookColSet.ColumnSetGrid.setRowData(defRowObj,defValue);
                    defRowObj=null;
                    defValue=null;
             }
        }

        for(var j=0;j<obj.length;j++)
        {   
            /*根据模板顺序判断模板中当前字段在用户设置集中没有则添加到第一行*/                    
            if(isAddColumn.indexOf(obj[j].Title)==-1 && isAddColumn.indexOf(obj[j].DataField)==-1)
            {
                var newRowObj=  jsAddressBookColSet.ColumnSetGrid.addRow(0);
                    var initValue = [{'TableName':obj[j].TbName,'DataField': obj[j].DataField,'Title': obj[j].Title,'IsDisplay':0,'IsOrder':1,'IsExport':1}];
                    jsAddressBookColSet.ColumnSetGrid.setRowData(newRowObj,initValue);
                    newRowObj=null;
                    initValue=null;
            }
        }    
        colSetObj=null;
        k=null;
        isAddColumn=null;    
    }
    else
    {    
        /*用户还没有进行列设置，直接加载模板数据*/
        var i=obj.length-1;
        for(;i>-1;i--)
        {
            var newRowObj=  jsAddressBookColSet.ColumnSetGrid.addRow(0);
            var initValue = [{'TableName':obj[i].TbName,'DataField': obj[i].DataField , 'Title': obj[i].Title,'IsDisplay':0,'IsOrder':1,'IsExport':1}];
            jsAddressBookColSet.ColumnSetGrid.setRowData(newRowObj,initValue);
            newRowObj=null;
            initValue=null;
        }
        var defRowObj=jsAddressBookColSet.ColumnSetGrid.addRow(0);
        var defValue=[{'TableName':'T_COMPANY','DataField': 'SIMPLE_NAME', 'Title': '单位名称','IsDisplay':0,'IsOrder':1,'IsExport':1}];
        jsAddressBookColSet.ColumnSetGrid.setRowData(defRowObj,defValue);
        defRowObj=null;
        defValue=null;
    }

}       
obj=null;
strTableArr="";
CollectGarbage();  
};


/*加载列表数据*/
jsAddressBookColSet.AddPersionalOrCompanyInfo=function(strType){//alert(strType)
    var obj;
    var strTableName="";
    if(strType=="per")
    {
        obj=eval(unescape(jsPersonalInformation.dataJSON));
        strTableName="T_EMPLOYEE";
    }
    else if(strType=="com")
    {
        obj=eval(unescape(jsDepartmentInformation.dataJSON));
        strTableName="T_COMPANY";
    }       
    var strTbArr="";
    if(obj!=null)
    {
        //var i=obj.length-1;  

        for(var i=0;i<obj.length;i++)
        {  
          //  var j=obj[i].length-1;
            for(var j=0 ; j<obj[i].length ;j++)
            {
                var strTitle = unescape(obj[i][j].title); //alert(unescape(obj[i][j].content));
                var ItemObj=document.createElement(unescape(obj[i][j].content));
                var strField=ItemObj.Field;  
                if(strTitle!="" && strField!=undefined && strField!="")
                 {                
                    strTbArr+=',{"TbName":"'+strTableName+'",';
                    strTbArr+='"DataField":"'+strField+'",';
                    strTbArr+='"Title":"'+strTitle+'",';
                    strTbArr+='"IsDisplay":"0",';
                    strTbArr+='"IsOrder":"1",';
                    strTbArr+='"IsExport":"1"}';      
                 }
                 strTitle=null;
                 strField=null;
                 ItemObj=null;           
            }      
        }
    //alert(strTbArr);  

      //window.clipboardData.setData("text",document.getElementById("tbColSetGrid").outerHTML)
    //    strTbArr="";
        obj.length=0;
        obj=null;
        CollectGarbage();
    }     
     return (strTbArr.substring(1));
};

/*保存*/
jsAddressBookColSet.SaveAddressBookColSet=function(){
    var store= jsAddressBookColSet.ColumnSetGrid.getRowData();
    var strArr="";
    var strShowCol="";
    var strHideCol="";
    for(var i=0;i<store.length;i++)
    {
        strArr+=',{"TbName":"'+store[i].TableName+'",';
        strArr+='"DataField":"'+store[i].DataField+'",';
        strArr+='"Title":"'+store[i].Title+'",';
        strArr+='"IsDisplay":"'+store[i].IsDisplay+'",';
        strArr+='"IsOrder":"'+store[i].IsOrder+'",';
        strArr+='"IsExport":"'+store[i].IsExport+'",'
        strArr+='"RowIndex":"'+i+'"}';      
        if(store[i].IsDisplay==0){
           strHideCol+=","+store[i].DataField;
        }
        else if(store[i].IsDisplay==1){
            strShowCol+=","+store[i].DataField;
        }
    }
    strArr=strArr.substring(1);
    
    var jsFile=[];
    jsFile.push("function jsColSetFile()");
    jsFile.push("{};");
    /*设置的结果字符串*/
    jsFile.push("jsColSetFile.ColSetString='"+strArr+"';");
    /*本页面设计内容HTML
    jsFile.push("jsColSetFile.tbSetInnerHTML='"+escape(document.getElementById("divContainer").innerHTML)+"';");*/
    
    var jsSource=jsFile.join("");
    if (App.FileIsExist(escape(App.ApplicationPath()+"Application\\TeleSecretary\\js\\jsColSetFile.js")))
    { 
         /*删除原有的本地文件*/
         App.FileDelete(App.ApplicationPath() + "Application\\TeleSecretary\\js\\jsColSetFile.js");  
    }
    /*创建JS文件*/
    var result = App.CreateFile(App.ApplicationPath() + "Application\\TeleSecretary\\js\\", "jsColSetFile.js", jsSource); 
    if(result)
    {      
        var formIndex=App.query("FormIndex");
        var loadAction=App.query("LoadAction");   
        ShowWindow.opener.execScript(formIndex,loadAction.replace("@hideStr",strHideCol.substring(1)).replace("@showStr",strShowCol.substring(1)));      
         alert("保存成功！");     
        ShowWindow.close();        
        formIndex=null;
        loadAction=null;
    }
    else
    {
        alert("保存没有成功！");
    }
    store.length=0;
    store=null;
    strArr="";
    jsFile.length=0;
    jsFile=null;
    jsSource.length=0;
    jsSource=null;
    result=null;
    CollectGarbage();
};