loadSubCategoryMultipleSelect=function(fieldId){
    $('#commonValidAlert').remove();
    $(".slick-cell.active").removeClass("redAlert");
    var moduleId = $("#moduleId").val();
    var projectId=$("#projectId").val();
    var grid =$('#derivedFieldListingGrid').data("gridInstance1");
    var row = grid.getActiveCell().row;
    var cell = grid.getActiveCell().cell;
    var serviceSelecteddata=grid.getData()[row].service;
    var subCategorySelecteddata=grid.getData()[row].subCategories;
    var derivedFld1NameSelecteddata=grid.getData()[row].derivedFld1Name;
    var derivedFld2NameSelecteddata=grid.getData()[row].derivedFld2Name;
    var derivedFieldId=grid.getData()[row].derivedFieldId;
    var $input;
    if(fieldId=="service"){
        $.ajax({
            url: "getParentServicesList.do",
            type: "POST",
            dataType: 'json',
            async: false,
            data:"projectId="+projectId,
            success:function(result){
                $input=$("#"+fieldId);
                $input.empty();
                $(".select2-results__options").empty();
                $.each(result, function(key, value){
                    var selected = "";
                    if((","+serviceSelecteddata+",").indexOf(value.serviceName) != -1){
                        selected = "selected";
                    }
                    $input.append("<option value='"+value.serviceId+"'"+selected+">"+value.serviceName+"</option>");

                });
            },complete:function(){

            }
        });
    }else if(fieldId=="subCategories"){
        $.ajax({
            url: "getDerivedFldSubCategories.do",
            type: "POST",
            dataType: 'json',
            async: false,
            data:"projectId="+projectId,
            success:function(result){
                $input=$("#"+fieldId);
                $input.empty();
                $(".select2-results__options").empty();
                $.each(result, function(key, value){
                    var optGroup = $("<optgroup label='" + value.categoryLabel + "'>");
                    $.each(value.options, function (key1, value1) {
                        var selected = "";
                        if((","+subCategorySelecteddata+",").indexOf(value1.subCategoryName) != -1){
                            selected = "selected";
                        }
                        optGroup.append("<option value='" + value1.subCategoryID + "'"+selected+">" + value1.subCategoryName + "</option>");
                    });
                    $input.append(optGroup);
                });
            },complete:function(){

            }
        });
    }else  if(fieldId=="derivedFld1Name" ||fieldId=="derivedFld2Name"){
        if(fieldId=="derivedFld1Name"){
            derivedFieldType=1;
        }else{
            derivedFieldType=2;
        }
        $.ajax({
            url: "getDerivedFieldsList.do",
            type: "POST",
            dataType: 'json',
            async: false,
            data:"projectId="+projectId+"&derivedFieldType="+derivedFieldType+"&derivedFieldId="+derivedFieldId,
            success:function(result){
                $input=$("#"+fieldId);
                $input.empty();
                $(".select2-results__options").empty();
                $.each(result, function(key, value){
                    var selected = "";
                    if(derivedFieldType==1){
                        if((","+derivedFld1NameSelecteddata+",").indexOf(value.derivedFld1Name) != -1){
                            selected = "selected";
                        }
                        $input.append("<option value='"+value.derivedFld1Id + "'"+selected +">"+value.derivedFld1Name+"</option>");
                    }else if(derivedFieldType==2){
                        if((","+derivedFld2NameSelecteddata+",").indexOf(value.derivedFld2Name) != -1){
                            selected = "selected";
                        }
                        $input.append("<option value='"+value.derivedFld2Id + "'"+selected +">"+value.derivedFld2Name+"</option>");
                    }
                });
            },complete:function(){


            }
        });
    }
}
loadCategorySelectCell=function(fieldId){
    $('#commonValidAlert').remove();
    $(".slick-cell.active").removeClass("redAlert");
    var moduleId = $("#moduleId").val();
    var projectId=$("#projectId").val();
    var $input;
    var grid =$('#listingGrid').data("gridInstance1");
    var row = grid.getActiveCell().row;
    var rowdata=grid.getDataItem(row);
    var categorySelecteddata=grid.getData()[row].category;
    var serviceSelecteddata=grid.getData()[row].service;

    if (rowdata.service && Array.isArray(rowdata.service) && rowdata.service.length > 0 && rowdata.service[0].value) {
        var ServiceParam = '';
        for (var i = 0; i < rowdata.service.length; i++) {
            if (rowdata.service[i].id) {
                if (ServiceParam !== '') {
                    ServiceParam += ',';
                }
                ServiceParam += rowdata.service[i].id;
            }
        }
    } else if (rowdata.service) {
        ServiceParam = rowdata.service;
    }
    if(fieldId=="category"){
        $.ajax({
            url: "getCategoryListByService.do",
            type: "POST",
            dataType: 'json',
            async: false,
            data:"projectId="+projectId+"&serviceId="+ServiceParam,
            success:function(result){
                $input=$("#"+fieldId);
                $input.empty();
                $(".select2-results__options").empty();
                $.each(result, function(key, value){
                    var selected = "";
                    if((","+categorySelecteddata+",").indexOf(value.categoryName) != -1){
                        selected = "selected";
                    }
                    $input.append("<option value='"+value.categoryId+"'"+selected+">"+value.categoryName+"</option>");

                });
            },complete:function(){


            }
        });

    }else if(fieldId=="service"){
        $.ajax({
            url: "getParentServicesList.do",
            type: "POST",
            dataType: 'json',
            async: false,
            data:"projectId="+projectId,
            success:function(result){
                $input=$("#"+fieldId);
                $input.empty();
                $(".select2-results__options").empty();
                var selectedValues = [];
                if (Array.isArray(serviceSelecteddata)) {
                    selectedValues = serviceSelecteddata.map(function(item) {
                        return item.value;
                    });
                    }
                $.each(result, function(key, value){
                    var selected = selectedValues.includes(value.serviceName) ? "selected" : "";
                    $input.append("<option value='"+value.serviceId+"' "+selected+">"+value.serviceName+"</option>");

                });
            },complete:function(){

            }
        });
    }
}
var gridReload=true;
saveSubCategoryDetails = function (subcategoryId, field, projectId, dataToSave){
    $('#commonValidAlert').remove();
    $(".slick-cell.active").removeClass("redAlert");
    var pageNumber=$("#listingGridPager").attr("data-pageno");
    var moduleId = $("#moduleId").val();
    var pageSize=$(".gridViewPort .recordLimit").val()
    var grid =$('#listingGrid').data("gridInstance1");
    var row = grid.getActiveCell().row;
    var cell = grid.getActiveCell().cell;
    var isDuplicateData=0;
    if(subcategoryId == undefined){
        subcategoryId = 0;
        if(dataToSave.subCategoryName==""){
            cell=1;
            field="Sub Category Name";
            checkIsSelected(field,"",cell);
            return;
        }else if(dataToSave.service== null ){
            cell=2;
            field="Service";
            checkIsSelected(field,"",cell);
            return;
        }else if(dataToSave.category==null ){
            cell=3;
            field="Category";
            checkIsSelected(field,"",cell);
            return;
        }else{
            var myRow=row+1;
            var selectedServiceID=$(".slick-row:nth-child("+myRow+") .slick-cell:nth-child(3) span").attr("data-select2-id");
            var selectedCategoryID=$(".slick-row:nth-child("+myRow+") .slick-cell:nth-child(4) span").attr("data-select2-id");
            urlParameters="parameterType="+field+"&"+field+"=" + selectedCategoryID+"&service="+selectedServiceID +"&subCategoryName="+dataToSave.subCategoryName;
        }
    }else{
        if(dataToSave.service== null ){
            cell=2;
            field="service";
            checkIsSelected(field,"",cell);
            return;
        }else if(dataToSave.category== null ){
            cell=3;
            field="category";
            checkIsSelected(field,"",cell);
            return;
        }else{
            var dataToSave= dataToSave[field];
            if(field == "Category" ){
                var myRow=row+1;
                dataToSave=$(".slick-row:nth-child("+myRow+") .slick-cell:nth-child(4) span").attr("data-select2-id");
            }

            urlParameters= "parameterType=" + field + "&"+field+"=" + dataToSave;
        }
    }
    if(field=="subCategoryName"){
        if(!isDoubleQuoteExists(field, dataToSave, "Sub Category Name", "SubCategoryGridBlock")){
            return false;
        }
        if(dataToSave.length>65){
            grid.gotoCell(row, cell, true);
            $(".slick-cell.active #subCategoryName").addClass("redAlert");
            $('#SubCategoryGridBlock').append('<p id="commonValidAlert" class="fldErr"><span class="topCurve"></span>Please limit your Sub Category Name content less than or equal to 65 characters</p>');
            $('#commonValidAlert').position({
                my: "left top",
                at: "left top+30",
                of: ".redAlert",
                collision: "none"
            });
            return false;
        }
        $.ajax({
            url: "checkDuplicateSubCategory.do",
            type: "POST",
            async: false,
            data: "subCategoryName=" + dataToSave +"&projectId=" + projectId +"&categoryName=" + dataToSave.category +"&subCategoryId="+subcategoryId,
            success: function (result) {
                isDuplicateData=result;
            }
        });
    }
   // gridReload=true;
    if (field!="subCategoryName" || (field=="subCategoryName" && isDuplicateData == 0)) {
        $.ajax({
            url: "saveSubCategoryDetails.do",
            type: "POST",
            dataType: 'json',
            async: false,
            data: "subCategoryId=" + subcategoryId + "&projectId=" + projectId + "&" + urlParameters,
            success: function (result) {
                if(result.status== 1){
                    dataToSave.subCategoryId = result;
                    if(field=="service"){
                        cell=3;
                        field="Category";
                        checkIsSelected(field,"",cell);
                    }
                }
                if(result.isServiceChanged=="Yes"){
                    if(field=="service"){
                        cell=3;
                        field="Category";
                        checkIsSelected(field,"",cell);
                    }
                }
                loadSdGridData(projectId, moduleId, pageNumber, pageSize,activeSortColumn,activeSortOrder);
            },
            complete:function(){
            }
        });
        if(gridReload==true){
            loadSdGridData(projectId, moduleId, pageNumber, pageSize,activeSortColumn,activeSortOrder);
        }
    } else if(field=="subCategoryName" && isDuplicateData == 1){

        grid.gotoCell(row, cell, true);
        $(".slick-cell.active #subCategoryName").addClass("redAlert");
        $('#SubCategoryGridBlock').append('<p id="commonValidAlert" class="fldErr"><span class="topCurve"></span>Sub Category Name already exists</p>');
        $('#commonValidAlert').position({
            my: "left top",
            at: "left top+30",
            of: ".redAlert",
            collision: "none"
        });
    }
}
loadDerivedFieldGrid=function(derivedFieldType,isfromMain, pageNo,sortColumn,sortDir){
    $("#derivedFieldListingGrid").removeClass("disableSorting");
    if(sortColumn==undefined)
        sortColumn="";
    if(sortDir==undefined)
        sortDir="";
    $(".gridViewPort--derivedFieldConfiguration .headerTab--ul li").removeClass("selected");
    if(derivedFieldType==1){
        $(".gridViewPort--derivedFieldConfiguration .headerTab--ul #derivedFieldTab1").addClass("selected");
    }else if(derivedFieldType==2){
        $(".gridViewPort--derivedFieldConfiguration .headerTab--ul #derivedFieldTab2").addClass("selected");
    }else{
        $(".gridViewPort--derivedFieldConfiguration .headerTab--ul #derivedFieldTab3").addClass("selected");
    }
    $(".gridViewPort--main").hide();
    $(".gridViewPort--derivedFieldConfiguration").show();
    columntest=[];
    pageSize=$("#derivedFldListLimit").val();
    //pageNo=$("#derivedFieldListingGridPager").attr("data-pageno");
    if(pageNo == undefined){
        pageNo = $("#derivedFieldListingGridPager").attr("data-pageno");
    }
    if(pageNo == undefined) {
        pageNo = 1;
    }
    $.ajax({
        url: "getJSONDerivedFieldsListRecords.do",
        type: "POST",
        dataType: 'json',
        async: false,
        data: "projectId="+$("#projectId").val()+"&pageSize="+pageSize+"&pageNo="+pageNo+"&derivedFieldType="+derivedFieldType+"&columnToSort="+sortColumn+"&sortDirection="+sortDir,
        success: function (result) {
            jsonContent = result;
        },
        complete:function(){
            var checkboxSelector = new Slick.CheckboxSelectColumn({
                cssClass: "slick-cell-checkboxsel",
                elementId: derivedFieldType + "_derivedCheckbox"
            });
            columntest.push(checkboxSelector.getColumnDefinition());
            for (var i = 0; i < jsonContent.header.length; i++) {
                columntest.push(getGridSlickColumnAttribute(jsonContent.header[i].id, jsonContent.header[i].name, jsonContent.header[i].minWidth, jsonContent.header[i].cssClass, "", jsonContent.header[i].sortable, "", getEditorBasedonColumnType(jsonContent.header[i].editor, ""), "1", "",false,jsonContent.header[i].isMandatory));
            }
            var rowtest = [];
            inActiveRow=[];
            $.each(jsonContent.rows, function (key, grid) {
                var currentRow=key+1;
                var parentData;
                var rowData = new Object();
                $.each(grid, function (key, gridField) {
                    rowData [key] = gridField;
                    if(key=="status" && gridField=="Not Active"){
                        inActiveRow.push(currentRow);
                    }
                    if(key=="isChild" && gridField=="No"){
                        parentData="disabled"
                    }
                    if(parentData=="disabled" && key=="parentService"){
                        parentDisabledRow.push(currentRow);
                    }
                });
                rowtest.push(rowData);
            });
            if($("#moduleId").val()=="3"){
                editableGridOptions.frozenColumn=2;
                editableGridOptions.forceFitColumns=false;
            }
            gridData = new Slick.Grid("#derivedFieldListingGrid", rowtest, columntest, editableGridOptions);
            $("#derivedFieldListingGrid").data("gridInstance1", gridData);
            $("#derivedFieldListingGrid").data("gridInstance1").getEditorLock().cancelCurrentEdit();

            $.each(rowtest, function (index) {
                var isPreshipped=rowtest[index].isPreshipped;
                if(isPreshipped){
                    index++;
                    $(".slick-row:nth-child("+ index +") .slick-cell.slick-cell-checkboxsel").addClass("disabled");
                    $(".slick-row:nth-child("+ index +")  .slick-cell.slick-cell-checkboxsel input[type=checkbox]").remove();
                }
            });
            gridData.onCellChange.subscribe(function (e, args) {
                var gridSlick = $("#derivedFieldListingGrid").data("gridInstance1");
                var dataToSave=gridSlick.getDataItem(args.row);
                var field = gridSlick.getColumns()[args.cell].field;
                var projectId=$("#projectId").val();
                //var type=$("#changeDerivedField").val();
                var derivedFieldType = document.querySelector('#DerivedFieldGridBlock .headerTab--ul li.selected').id;
                var type =0;
                if(derivedFieldType == "derivedFieldTab1"){
                    type = 1;
                }else if(derivedFieldType == "derivedFieldTab2"){
                    type = 2;
                }else{
                    type = 3;
                }
                saveDerivedFieldDetails(dataToSave.derivedFieldId, field, projectId,type,dataToSave[field]);
            });
            gridData.onSort.subscribe(function (e, args) {
                console.log("subscribe");
                if ( ! $("#derivedFieldListingGrid").hasClass("disableSorting") )
                {
                    var cols = args.sortCols;
                    var field = cols[0].sortCol.field;
                    var sign = "";
                    for (var i = 0; i < cols.length; i++)
                    {
                        if(cols[i].sortAsc){
                            sign="ASC"
                        }
                        else{
                            sign="DESC";
                        }
                    }
                    activeSortOrder=sign;
                    activeSortColumn=field;
                    var projectId=$("#projectId").val();
                    var moduleId=$("#moduleId").val();
                    var pageNo=$("#derivedFieldListingGridPager").attr("data-pageno");
                    var pageSize=$(".gridViewPort .recordLimit").val();
                    $("#derivedFieldListingGrid").addClass("disableSorting");
                    loadDerivedFieldGrid(derivedFieldType,isfromMain,pageNo,field,sign);
                    setBackGridSortColumn(field,sign,"derivedFieldListingGrid");
                }
                return 0;
            });
            gridData.render();
            var totalHt=(rowtest.length+1)*30;
            $("#derivedFieldListingGrid").css("min-height", totalHt+"px");
            var listingGrid=$("#derivedFieldListingGrid").data("gridInstance1");
            listingGrid.resizeCanvas();
            if(parentDisabledRow!=""){
                for( var i=0;i<parentDisabledRow.length;i++){
                    $("#derivedFieldListingGrid .slick-row:nth-child("+parentDisabledRow[i]+") .slick-cell:nth-child(10)").addClass("disabled");
                }
            }
            if(inActiveRow!=""){
                for( var i=0;i<inActiveRow.length;i++){
                    $("#derivedFieldListingGrid .slick-row:nth-child("+inActiveRow[i]+") .slick-cell:not(.slick-cell-checkboxsel):not(.toggleCell)").addClass("disabled");
                }
            }
            var dataView = new Slick.Data.DataView();
            dataView.setPagingOptions({pageNum: jsonContent.pageNo, pageSize: jsonContent.pageSize, totalPages: jsonContent.NoOfPages, totalRows:jsonContent.TotalRecords, testRun: 5});
            var pager = new Slick.Controls.Pager(dataView, gridData, $("#derivedFieldListingGridPager"));
            $("#derivedFieldListingGridPager").attr("data-totalrecords",""+jsonContent.TotalRecords);
            $("#derivedFieldListingGridPager").attr("data-totalpages",""+jsonContent.NoOfPages);
            $("#derivedFieldListingGridPager").attr("data-pageno",""+jsonContent.pageNo);
            $("#derivedFieldListingGridPager").find(".slick-pager .slick-pager-status input[type='text']").attr("title", jsonContent.pageNo);
            disableNextOrPrevPagingSelection("derivedFieldListingGridPager");
            $("#derivedFieldListingGridPager #pageTotalPageNumber").html(jsonContent.NoOfPages);
            $("#derivedFieldListingGridPager").siblings("div.rightPagn").find("#slickGridRecordInfo2").html("Displaying records "+jsonContent.startPoint+" - "+jsonContent.endpoint+" of "+jsonContent.TotalRecords);
            $(".slick-viewport.slick-viewport-top.slick-viewport-left").perfectScrollbar();
            if($("#moduleId").val()=="3"){
                $(".slick-viewport.slick-viewport-top.slick-viewport-right").perfectScrollbar();
            }
            if(isfromMain=="yes"){
                setTimeout(function(){
                    $("#changeDerivedField").val($("#selectedDerivedField").val());
                    $("#changeDerivedField").multipleSelect("refresh");
                },100);
            }
        }
    });
    if(sortColumn==""){
        $(".slick-header-column:nth-child(2) .slick-sort-indicator").addClass("slick-sort-indicator-asc");
    }
    else if(sortDir=="ASC"){
        $(".slick-header-column[id$='"+sortColumn+"'] .slick-sort-indicator").addClass("slick-sort-indicator-asc");
    }else{
        $(".slick-header-column[id$='"+sortColumn+"'] .slick-sort-indicator").addClass("slick-sort-indicator-desc");
    }
}

var gridReload=true;
saveDerivedFieldDetails=function(derivedFieldId, field, projectId,type,dataToSave){
    $('#commonValidAlert').remove();
    $(".slick-cell.active").removeClass("redAlert");
    var grid =$('#derivedFieldListingGrid').data("gridInstance1");
    var row = grid.getActiveCell().row;
    var cell = grid.getActiveCell().cell;
    var isDuplicateData=0;
    if(derivedFieldId == undefined){
        derivedFieldId = 0;
    }
    if(field=="derivedFieldName"){
        if(!isDoubleQuoteExists(field, dataToSave, "Derived Field Value", "DerivedFieldGridBlock")){
            return false;
        }
        if(dataToSave.length>65){
            grid.gotoCell(row, cell, true);
            $(".slick-cell.active #derivedFieldName").addClass("redAlert");
            $('#DerivedFieldGridBlock').append('<p id="commonValidAlert" class="fldErr"><span class="topCurve"></span>Please limit your Derived Field Value content less than or equal to 65 characters</p>');
            $('#commonValidAlert').position({
                my: "left top",
                at: "left top+30",
                of: ".redAlert",
                collision: "none"
            });
            return false;
        }
        $.ajax({
            url: "checkDuplicatederivedFld.do",
            type: "POST",
            async: false,
            data: "projectId=" + projectId + "&type="+type+"&derivedFieldName=" + dataToSave +"&derivedFieldId="+derivedFieldId,
            success: function (result) {
                isDuplicateData=result;
            }
        });
    }
    if(field=="derivedFieldName" && isDuplicateData == 1){
        grid.gotoCell(row, cell, true);
        $(".slick-cell.active #derivedFieldName").addClass("redAlert");
        $('#DerivedFieldGridBlock').append('<p id="commonValidAlert" class="fldErr"><span class="topCurve"></span>Derived Field '+type+' already exist</p>');
        $('#commonValidAlert').position({
            my: "left top",
            at: "left top+30",
            of: ".redAlert",
            collision: "none"
        });
        return false;
    }else if(field=="derivedFldShortName" && dataToSave.length>15){
        grid.gotoCell(row, cell, true);
        $(".slick-cell.active #shortSubCategoryName").addClass("redAlert");
        $('#DerivedFieldGridBlock').append('<p id="commonValidAlert" class="fldErr"><span class="topCurve"></span>Please limit your Short Name content less than or equal to 15 characters</p>');
        $('#commonValidAlert').position({
            my: "left top",
            at: "left top+30",
            of: ".redAlert",
            collision: "none"
        });
        return false;
    }else if (field!="derivedFieldName" || (field=="derivedFieldName" && isDuplicateData == 0)) {
        gridReload=true;
        var alteredRow = $(".slick-row.active").index();
        var gridSlick = $("#derivedFieldListingGrid").data("gridInstance1");
        var dataToSave1=gridSlick.getDataItem(alteredRow);
        $.ajax({
            url: "saveDerivedFieldDetails.do",
            type: "POST",
            dataType: 'json',
            async: false,
            data: "derivedFieldId=" + derivedFieldId + "&projectId=" + projectId + "&type="+type+"&parameterType=" + field +"&"+field+"=" + dataToSave,
            success: function (result) {
                derivedFieldId = result;
                dataToSave1.derivedFieldId = result;
            },
            complete:function(){
                var gridData=grid.getDataItem(row);

                if((field=="derivedFieldName" || field=="subCategories") && gridData.subCategories==undefined){
                    gridReload=false;
                    grid.gotoCell(row, 3, true);
                    $(".slick-cell.active #subCategories+.select2").addClass("redAlert");
                    $('#DerivedFieldGridBlock').append('<p id="commonValidAlert" class="fldErr"><span class="topCurve"></span>Please select the sub category</p>');
                    $('#commonValidAlert').position({
                        my: "left top",
                        at: "left top+30",
                        of: ".redAlert",
                        collision: "none"
                    });
                    return false;
                }else if((field=="derivedFieldName" || field=="subCategories") && (gridData.subCategories[0]==""||gridData.subCategories[0]==undefined)){
                    grid.gotoCell(row, 3, true);
                    $(".slick-cell.active #subCategories+.select2").addClass("redAlert");
                    $('#DerivedFieldGridBlock').append('<p id="commonValidAlert" class="fldErr"><span class="topCurve"></span>Please select the sub category</p>');
                    $('#commonValidAlert').position({
                        my: "left top",
                        at: "left top+30",
                        of: ".redAlert",
                        collision: "none"
                    });
                    gridReload=false;
                    return false;
                }
            }
        });
        if(gridReload==true){
            loadDerivedFieldGrid(type);
        }
    }

}
saveSubCategoryDescription=function(){
    $('#commonValidAlert').hide();
    $('#cke_subCategorydescription').removeClass("redAlert");
    var desc = CKEDITOR.instances['subCategorydescription'].getData();
    $(currSelectedcell).html(desc);
    var alteredRow = $(".slick-row.active").index();
    var gridSlick = $("#listingGrid").data("gridInstance1");
    var dataToSave = gridSlick.getDataItem(alteredRow);
    var projectId = $("#projectId").val();
    var pageNumber=$("#listingGridPager").attr("data-pageno");
    var pageSize=$(".gridViewPort .recordLimit").val()
    var moduleId = $("#moduleId").val();
    var commentSize =1000;
    if(desc.length > commentSize){
        validationAlertShown=true;
        $('#cke_subCategorydescription').addClass("redAlert");
        $('#cke_subCategorydescription').append('<p id="commonValidAlert" class="fldErr" style="bottom:15px"><span class="topCurve"></span>Please limit your Sub Category Description content less than or equal to '+commentSize+' characters</p>');
        $('#commonValidAlert').show();
        return false;
    }
    $.ajax({
        url: "saveSubCategoryDetails.do",
        type: "POST",
        dataType: 'json',
        async: false,
        data: "subCategoryId=" +dataToSave.subCategoryId + "&projectId=" + projectId + "&parameterType=subCatDescription&subCategoryDesc=" + encodeURIComponent(desc),
        success: function (result) {

        }
    });
    $(".descriptionBlk #subCategorydescription,.descriptionBlk .saveDes,.descriptionBlk .cancelDes,.descriptionBlk .closeBlk").remove();
    CKEDITOR.instances["subCategorydescription"].destroy();
    $(".descBlkOverlay,.descriptionBlk").hide();
    $(".ckEditorCol").removeClass("clicked");
    var pageNo = $("#listingGridPager").attr("data-pageno");
    var pageSize = $("#recordLimit5[name='recordLimit5']").val();
    var moduleId = $("#moduleId").val();
    loadSdGridData(projectId, moduleId, pageNo, pageSize,activeSortColumn,activeSortOrder);
}
$(document).on("click","#SubCategoryGridBlock #activateRow",function(){
    if($("#commonValidAlert").is(":visible")==false){
        $("#ConfirmationBlk").remove();
        if ($('input[name=check]:checked').length == 0) {
            $("#listingGrid").before('<div id="ConfirmationBlk" class="confirmBlk" style="display: block;"><p>To activate the record(s), at least one row must be selected<a href="javascript:void(0)" id="hideAlertBox" class="yesBtn">OK</a></p></div>');
        }else{
            $("#listingGrid").before('<div id="ConfirmationBlk" class="confirmBlk" style="display: block;"><p>Do you really want to activate the selected service(s) ?<a href="javascript:void(0)" id="ConfirmCatActivateBtn" class="yesBtn">Yes</a><a href="javascript:void(0)" id="hideAlertBox" class="noBtn">No</a></p></div>');
        }
    }else{
        $(".redAlert").focus();
    }
});
$(document).on("click","#SubCategoryGridBlock #ConfirmActivateBtn",function(){
    var moduleId = $("#moduleId").val();
    var projectId = $("#projectId").val();
    var checkboxValues = [];
    $('input[name=check]:checked').map(function () {
        checkboxValues.push($(this).val());
    });
    $.ajax({
        url: "bulkSubCategoryAction.do",
        type: "POST",
        dataType: 'json',
        async: false,
        data: "subCategoryList=" + checkboxValues + "&projectId=" + projectId + "&isActive=0",
        success: function (result) {

        },
        complete: function () {
            $(document).click();
            $("#hideAlertBox").click();
            var pageNo = $("#listingGridPager").attr("data-pageno");
            var pageSize = $("#recordLimit5[name='recordLimit5']").val();
            loadSdGridData(projectId, moduleId, pageNo, pageSize,activeSortColumn,activeSortOrder);
        }
    });
});
$(document).on("click","#SubCategoryGridBlock #deactivateRow",function(){
    if($("#commonValidAlert").is(":visible")==false){
        $("#ConfirmationBlk").remove();
        if ($('input[name=check]:checked').length == 0) {
            $("#listingGrid").before('<div id="ConfirmationBlk" class="confirmBlk" style="display: block;"><p>To deactivate the record(s), at least one row must be selected<a href="javascript:void(0)" id="hideAlertBox" class="yesBtn">OK</a></p></div>');
        }else{
            $("#listingGrid").before('<div id="ConfirmationBlk" class="confirmBlk" style="display: block;"><p>Do you really want to deactivate the selected records?<a href="javascript:void(0)" id="ConfirmCatDeactivateBtn" class="yesBtn">Yes</a><a href="javascript:void(0)" id="hideAlertBox" class="noBtn">No</a></p></div>');
        }
    }else{
        $(".redAlert").focus();
    }
});
$(document).on("click","#SubCategoryGridBlock #ConfirmDeactivateBtn",function(){
    var moduleId = $("#moduleId").val();
    var projectId = $("#projectId").val();
    var checkboxValues = [];
    $('input[name=check]:checked').map(function () {
        checkboxValues.push($(this).val());
    });
    $.ajax({
        url: "bulkSubCategoryAction.do",
        type: "POST",
        dataType: 'json',
        async: false,
        data: "subCategoryList=" + checkboxValues + "&projectId=" + projectId + "&isActive=1",
        success: function (result) {

        },
        complete: function () {
            $(document).click();
            $("#hideAlertBox").click();
            var pageNo = $("#listingGridPager").attr("data-pageno");
            var pageSize = $("#recordLimit5[name='recordLimit5']").val();
            loadSdGridData(projectId, moduleId, pageNo, pageSize,activeSortColumn,activeSortOrder);
        }
    });
});

SaveSubCategoryDetails = function(subCategoryId, projectId, isActive){
    var pageSize=$(".gridViewPort .recordLimit").val()
    var moduleId = $("#moduleId").val();
    var projectId = $("#projectId").val();
    $.ajax({
        url: "saveSubCategoryDetails.do",
        type: "POST",
        dataType: 'json',
        async: false,
        data: "subCategoryId=" + subCategoryId + "&projectId=" + projectId + "&parameterType=status&isActive=" +isActive,
        success: function (result) {
        },
        complete: function () {
            $(document).click();
            var pageNo = $("#listingGridPager").attr("data-pageno");
            var pageSize = $("#recordLimit5[name='recordLimit5']").val();
            loadSdGridData(projectId, moduleId, pageNo, pageSize,activeSortColumn,activeSortOrder);
        }
    });
}

$(document).on("click","#DerivedFieldGridBlock #activateDerivedFldRow",function(){
    if($("#commonValidAlert").is(":visible")==false){
        $("#ConfirmationBlk").remove();
        if ($('input[name=check]:checked').length == 0) {
            $("#derivedFieldListingGrid").before('<div id="ConfirmationBlk" class="confirmBlk"><p>To activate the record(s), at least one row must be selected<a href="javascript:void(0)" id="hideAlertBox" class="yesBtn">OK</a></p></div>');
            $("#ConfirmationBlk").slideDown(300, "easeInOutExpo");
        }else{
            $("#derivedFieldListingGrid").before('<div id="ConfirmationBlk" class="confirmBlk" style="display: block;"><p>Do you really want to activate the selected service(s) ?<a href="javascript:void(0)" id="ConfirmActivateBtn" class="yesBtn">Yes</a><a href="javascript:void(0)" id="hideAlertBox" class="noBtn">No</a></p></div>');
        }
    }else{
        $(".redAlert").focus();
    }
});
$(document).on("click", "#derivedFieldListingGridPager .ui-icon-seek-next:not(.disabled)", function (e)
{
    var pageNumber=$("#derivedFieldListingGridPager").attr("data-pageno");
    var nextPgNumber=parseInt(pageNumber)+1;
    var projectId = $("#projectId").val();
    changeDerivedFieldsPagination(projectId, nextPgNumber);
    e.preventDefault();
});
$(document).on("click", "#derivedFieldListingGridPager .ui-icon-seek-prev:not(.disabled)", function (e)
{
    var pageNumber=$("#derivedFieldListingGridPager").attr("data-pageno");
    var nextPgNumber=parseInt(pageNumber)-1;
    var projectId = $("#projectId").val();
    changeDerivedFieldsPagination(projectId, nextPgNumber);
    e.preventDefault();
});
$(document).on("click", "#derivedFieldListingGridPager .ui-icon-seek-end:not(.disabled)", function (e)
{
    $(this).addClass("disabled");
    var pageSize=$(".gridViewPort .recordLimit").val();
    var projectId = $("#projectId").val();
    var totalRecords=$("#derivedFieldListingGridPager").attr("data-totalrecords");
    var lastPageNumber=Math.ceil(totalRecords/pageSize);
    changeDerivedFieldsPagination(projectId, lastPageNumber);
    e.preventDefault();
});
$(document).on("click", "#derivedFieldListingGridPager .ui-icon-seek-first:not(.disabled)", function (e)
{
    $(this).addClass("disabled");
    var projectId = $("#projectId").val();
    changeDerivedFieldsPagination(projectId, 1);
    e.preventDefault();
});
changeDerivedFieldsPagination = function(projectId, pageNo){
    var selectedId = document.querySelector('#DerivedFieldGridBlock .headerTab--ul li.selected').id;
    var derivedFieldType =0;
    if(selectedId == "derivedFieldTab1"){
        derivedFieldType = 1;
    }else if(selectedId == "derivedFieldTab2"){
        derivedFieldType = 2;
    }else{
        derivedFieldType = 3;
    }
    loadDerivedFieldGrid(derivedFieldType,"",pageNo);
}
$(document).on("click","#DerivedFieldGridBlock #ConfirmActivateBtn",function(){
    var moduleId = $("#moduleId").val();
    var projectId = $("#projectId").val();
    var checkboxValues = [];
    $('input[name=check]:checked').map(function () {
        checkboxValues.push($(this).val());
    });
    var derivedFldType=$("#changeDerivedField").val();
    $.ajax({
        url: "bulkDerivedFieldAction.do",
        type: "POST",
        dataType: 'json',
        async: false,
        data: "derivedFld1List=" + checkboxValues + "&projectId=" + projectId +"&isActive=0",
        success: function (result) {

        },
        complete: function () {
            $(document).click();
            $("#hideAlertBox").click();
            var pageNo = $("#derivedFieldListingGridPager").attr("data-pageno");
            var pageSize = $("#recordLimit5[name='recordLimit5']").val();
            var selectedId = document.querySelector('#DerivedFieldGridBlock .headerTab--ul li.selected').id;
            var derivedFieldType =0;
            if(selectedId == "derivedFieldTab1"){
                derivedFieldType = 1;
            }else if(selectedId == "derivedFieldTab2"){
                derivedFieldType = 2;
            }else{
                derivedFieldType = 3;
            }
            loadDerivedFieldGrid(derivedFieldType);
        }
    });
});
$(document).on("click","#DerivedFieldGridBlock #deactivateDerivedDlfRow",function(){
    if($("#commonValidAlert").is(":visible")==false){
        $("#ConfirmationBlk").remove();
        if ($('input[name=check]:checked').length == 0) {
            $("#derivedFieldListingGrid").before('<div id="ConfirmationBlk" class="confirmBlk"><p>To deactivate the record(s), at least one row must be selected<a href="javascript:void(0)" id="hideAlertBox" class="yesBtn">OK</a></p></div>');
            $("#ConfirmationBlk").slideDown(300, "easeInOutExpo");
        }else{
            $("#derivedFieldListingGrid").before('<div id="ConfirmationBlk" class="confirmBlk" style="display: block;"><p>Do you really want to deactivate the selected service(s) ?<a href="javascript:void(0)" id="ConfirmDeactivateBtn" class="yesBtn">Yes</a><a href="javascript:void(0)" id="hideAlertBox" class="noBtn">No</a></p></div>');
        }
    }else{
        $(".redAlert").focus();
    }
});
$(document).on("click","#DerivedFieldGridBlock #ConfirmDeactivateBtn",function(){
    var moduleId = $("#moduleId").val();
    var projectId = $("#projectId").val();
    var checkboxValues = [];
    $('input[name=check]:checked').map(function () {
        checkboxValues.push($(this).val());
    });
    var derivedFldType=$("#changeDerivedField").val();
    $.ajax({
        url: "bulkDerivedFieldAction.do",
        type: "POST",
        dataType: 'json',
        async: false,
        data: "derivedFld1List=" + checkboxValues + "&projectId=" + projectId+"&isActive=1",
        success: function (result) {

        },
        complete: function () {
            $(document).click();
            $("#hideAlertBox").click();
            var pageNo = $("#derivedFieldListingGridPager").attr("data-pageno");
            var pageSize = $("#recordLimit5[name='recordLimit5']").val();
            var selectedId = document.querySelector('#DerivedFieldGridBlock .headerTab--ul li.selected').id;
            var derivedFieldType =0;
            if(selectedId == "derivedFieldTab1"){
                derivedFieldType = 1;
            }else if(selectedId == "derivedFieldTab2"){
                derivedFieldType = 2;
            }else{
                derivedFieldType = 3;
            }
            loadDerivedFieldGrid(derivedFieldType);
        }
    });
});
$(document).on("click", "#DerivedFieldGridBlock .toggleBtn input[type=checkbox]", function () {
    if($("#commonValidAlert").is(":visible")==false){
        var alteredRow=$(this).parents(".slick-row").index();
        var gridSlick = $("#derivedFieldListingGrid").data("gridInstance1");
        var dataToSave=gridSlick.getDataItem(alteredRow);
        var projectId=$("#projectId").val();
        var isActive=$(this).prop("checked");
        var type = document.querySelector('#DerivedFieldGridBlock .headerTab--ul li.selected').id;
        var derivedFldType =0;
        if(type == "derivedFieldTab1"){
            derivedFldType = 1;
        }else if(type == "derivedFieldTab2"){
            derivedFldType = 2;
        }else{
            derivedFldType = 3;
        }
        if(isActive==true){
            isActive=0;
        }else{
            isActive=1;
        }
        $.ajax({
            url: "saveDerivedFieldDetails.do",
            type: "POST",
            dataType: 'json',
            async: false,
            data: "derivedFieldId=" +dataToSave.derivedFieldId +"&projectId=" + projectId+"&parameterType=status&type="+derivedFldType+"&isActive=" +isActive,
            success: function (result) {

            },
            complete: function () {
                loadDerivedFieldGrid(derivedFldType);
            }
        });
    }else{
        $(this).click();
    }
});
$(document).on("click","#DerivedFieldGridBlock .backBtn",function(){
    $("#selectedDerivedField").val("");
    $("#selectedDerivedField").multipleSelect("refresh");
});