$(document).ready(function () {
    debugger;
    loaddata();
    LoadBranch();
    //LoadRole();
    // LoadUser();
    Loadpagename_op();
    Formtype();
    Loadmoduletype();
    Loadmodulesubtype();
    $('#div_Type_of_Admission').hide();
   

})

function acces_rights(event, Formid, Form_Name, Branch_id) {
    debugger;
    $('#Hdpt_id').val(Branch_id);
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {

                     return;
                 }
                 else {
                     debugger;
                     $('#access_rights_op').modal('toggle');
                     $('#tbl_access_rights_op').empty();
                     //$('#access_rights_table_op').DataTable().destroy();
                     $('#formid_op').html(Formid)
                     $('#formname_op').html(Form_Name)

                     $('#Hformid').val(Formid);
                     //LoadBranch(Branch_id)
                     //LoadCategory();
                     //LoadDepartment();
                    
                     // LoadUser();
                     loaddata_op();
                     //Formid: $('#formid_op').html(Formid)
                     //Form_Name: $('#formname_op').html(Form_Name)
                     //Id: $('#Id_op').html(usr)

                  //one master concept
                   //  $('#op_access_branch').val(Branch_id);
                     //   $('#op_access_branch').attr('disabled', true);

                     var Branchid = $('#HospitalBranchID').val()
                     $('#op_access_branch').val(Branchid);
                     LoadRole();
                 }
             }
         }
     })

}

////btn x close  op access tab
$('#Btn_x_op_acc').click(function () {
    debugger;
    clear_op();
    cleardata_op();
})

function clear_op() {

    //$('#Hformid').val("");
    $('#Hcate_id').val("").change();
    $('#Hscate_id').val("").change();
    $('#Hdpt_id').val("").change();
    $('#Hsdpt_id').val("").change();
    $('#Husr_id').val("").change();
    $("#btn_add_op").html("Add");
    //LoadBranch();
    //LoadRole();

}

///btn for op close_the_access_tab
$('#Btn_clr_op_acc').click(function () {
    debugger;
    clear_op();
    cleardata_op();
})

//map op onclick function

function map(event, Formid) {
    debugger;
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {

                     return;
                 }
                 else {
                     $('#map_op').modal('toggle');
                     //  LoadBranch_map();

                     LoadDepartment_map();
                     LoadSubDepartment_map();
                     // Loadpagename_op();
                     loaddata_mapping_op();
                     $('#Hformid').val(Formid);
                 }
             }
         }
     })
}

////btn x close  op map tab
$('#Btn_x_op_map').click(function () {
    debugger;
    cleardata_op_map();
})

///btn for op close_the_map_tab
$('#Btn_clr_op_map').click(function () {
    debugger;
    cleardata_op_map();
})


/////delete//active//inactive---------_Loadtable_op_ip_patient_survey-----------
function FormBuilderdel(event, Formid, indicatorid) {
    debugger;
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {

                     return;
                 }
                 else {
                     var status = "";
                     if (indicatorid == 'DE') {
                         status = "Are You Sure, You Want to Delete?";
                     }
                     else if (indicatorid == 'INACT') {
                         status = "Are You Sure, You Want to In-Activate?";
                     }
                     else if (indicatorid == 'ACT') {

                         status = "Are You Sure,You Want to Activate?";
                     }
                     toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", status,
                           {

                               closeButton: true,
                               progressBar: true,
                               allowHtml: true,
                               timeOut: 20000000,

                               onShown: function (toast) {
                                   $("#confirmationRevertYes").click(function () {

                                       var Delete = {
                                           Dml_Indicator: indicatorid,
                                           Formid: Formid
                                       }

                                       $.ajax({
                                           async: true,
                                           type: 'POST',
                                           url: "/Form_builder/insert_update_deleteFormMaster",
                                           contentType: 'application/json; charset=UTF-8',
                                           data: JSON.stringify(Delete),
                                           success: function (data) {
                                               debugger;
                                               var data1 = JSON.parse(data);

                                               if (indicatorid == 'DE') {

                                                   toastr.success("Deleted Successfully");
                                               }
                                               else if (indicatorid == 'INACT') {

                                                   toastr.success("Inactivated Successfully");
                                               }
                                               else if (indicatorid == 'ACT') {

                                                   toastr.success("Activated Successfully");
                                               }

                                               loaddata();
                                               loaddata1();
                                               loaddata2();
                                               loaddata3();
                                           }
                                       });
                                   });
                               }
                           })

                 }
             }
         }

     })

};
/////loadtable
function loaddata(Fb_branch_id, Fb_Type) {
    debugger;
    var load = ''
    if ($('#hiddenRole_id').val() == 2) {
        load = {
            Dml_Indicator: "OP",
            Hospitalid: $('#HospitalId').val(),
            Branch_id: Fb_branch_id,
            Moduletype: Fb_Type
        }
    }
    else {
        load = {
            Dml_Indicator: "OP",
            Hospitalid: $('#HospitalId').val(),
            Branch_id: $('#HospitalBranchID').val(),
            Moduletype: Fb_Type
        }
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMaster",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_op_formbuilder').empty();
                    $('#op_formbuilder_table').dataTable().fnClearTable();
                    $('#op_formbuilder_table').dataTable().fnDraw();
                    $('#op_formbuilder_table').dataTable().fnDestroy();

                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;

                        var status = '';
                        var edit = '', Form_Image = '';

                        status = '<span class="label label-success">Active</span>';


                        //designform = '<a  title="Design New Form" class="btn btn-edit btn-xs" data-id="FB_A1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  id="form_'+i+'" onclick=FormBuildNewDesign(this.id,"' + emp.Formid + '") href="https://formbuilder.mocerohealth.in/form-builder/' + emp.Formid + '"><i class="fas fa-marker" aria-hidden="true"></i></a>';

                        designform = '<a  title="Design New Form" class="btn btn-edit btn-xs btn_r" data-id="FB_A1" data-type="role_rights_add" data-nav="redirect" data-name="https://formbuilder.mocerohealth.in/form-builder/' + emp.Formid + '"  id="form_' + i + '"  ><i class="fas fa-marker" aria-hidden="true"></i></a>';

                        //edit = '<a  title="Edit" class="btn btn-edit btn-xs" data-id="FB_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" data-toggle="modal" data-target="#form-builder-popup" onclick=FormBuildereditop(this,"' + emp.Formid + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        if ($('#hstatus').val == 1) {
                            edit = '<a  title="Edit" class="btn btn-edit btn-xs" data-id="FB_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" data-toggle="modal" data-target="#form-builder-popup-new" onclick=FormBuildereditop(this,"' + emp.Formid + '","' + emp.Branch_id + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        }
                        else {
                            edit = '<a  title="Edit" class="btn btn-edit btn-xs" data-id="FB_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" data-toggle="modal" data-target="#form-builder-popup-new" onclick=FormBuildereditop(this,"' + emp.Formid + '","' + emp.Branch_id + '")><i class="fa fa-eye" aria-hidden="true"></i></a>';
                        }


                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="FB_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=FormBuilderdel(this,"' + emp.Formid + '","DE")><i class="fa fa-trash" aria-hidden="true"></i></a>';


                        //View_old_version = '<p data-toggle="modal" data-target="#view_old_version_op" aria-hidden="true" ><a class="btn-blue"  data_id="FB_F1" onclick=\'view_old_version_op(this,"' + emp.Form_Name + '")\'>View old version</a></p>'

                        //Access_Rights = '<p data-toggle="modal" data-target="#access_rights_op" aria-hidden="true"><a id="access_rights_tt" class="btn-blue"  data_id="FB_F1" onclick=\'acces_rights(this,"' + emp.Formid + '","' + emp.Form_Name + '")\'>Access Rights</a></p>'

                        //Map = '<p data-toggle="modal" data-target="#map_op" aria-hidden="true"><a class="btn-blue" id="mapping"  data_id="FB_F1" onclick=\'map(this,"' + emp.Formid + '")\'>Map</a></p>'

                        //Send = '<p data-toggle="modal" data-target="#send_op" data_id="FB_F1" aria-hidden="true"><a class="btn-blue">Send</a></p>'
                        //Copy_to_other_branch = '<p data-toggle="modal" data-target="#copypopup" aria-hidden="true"><a id="copy_branch" class="btn-blue" onclick=\'copy_branch(this,"' + emp.Formid + '","' + emp.Form_Name + '")\'>Copy to Other Branch</a></p>'

                        View_old_version = '<a data-toggle="modal" title="View_old_version" class="btn-blue" data-id="FB_F1" data-type="role_rights_function"   data-target="view_old_version_op"   data-nav="sp_div" data-name="c_details_form" href="javascript:void(0);"  onclick=\'view_old_version_op(this,"' + emp.Form_Name + '","' + emp.Formid + '")\' aria-hidden="true" >View old version</a>'

                        Access_Rights = '<a data-toggle="modal" title="Access_Rights" class="btn-blue" id="access_rights_tt" data-id="FB_F1" data-type="role_rights_function"   data-target="access_rights_op"   data-nav="sp_div" data-name="c_details_form" href="javascript:void(0);"  onclick=\'acces_rights(this,"' + emp.Formid + '","' + emp.Form_Name + '","' + emp.Branch_id + '")\' aria-hidden="true" >Access Rights</a>'

                        Copy_to_other_branch = '<a data-toggle="modal" title="Copy_to_other_branch" class="btn-blue"  data-target="#form-builder-popup" data-id="FB_F1" data-type="role_rights_function" aria-hidden="true"  id="copy_branch" data-nav="sp_div" data-name="c_details_form" href="javascript:void(0);" onclick=\'copy_branch(this,"' + emp.Formid + '","' + emp.Form_Name + '")\'>Copy to Other Branch</a>'

                        Map = '<a data-toggle="modal" data-target="" title="Map" class="btn-blue hide" data-id="FB_F1"  id="mapping"  data-type="role_rights_function"   data-target=""   data-nav="sp_div" data-name="c_details_form" href="javascript:void(0);" onclick=\'map(this,"' + emp.Formid + '")\'  aria-hidden="true">Map</a>'

                        Send = '<p data-toggle="modal" data-target="#send_op" data-id="FB_F1" aria-hidden="true"><a class="btn-blue">Send</a></p>'

                      //  Download_Print = '<p data-toggle="modal" data-target="#download/print" data_id="FB_F1" aria-hidden="true"><a class="btn-blue">Download/Print</a</p>'

                        Print = '<p <a data-toggle="tooltip" data-placement="bottom" title="Print" type="button" class="btn btn-edit btn-xs btn_r" data-id="FB_F1" data-type="role_rights_edit" data-nav="redirect" data-name=""><i class="fa fa-print" aria-hidden="true"></i></a></p>'

                        Download = '<p <a data-toggle="tooltip" data-placement="bottom" title="Download" type="button" class="btn btn-edit btn-xs btn_r" data-id="FB_F1" data-type="role_rights_edit" data-nav="redirect" data-name=""><i class="fa fa-download" aria-hidden="true"></i></a></p>'

                        if (emp.status == '1') {

                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="FB_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=FormBuilderdel(this,"' + emp.Formid + '","INACT")><i class="fa fa-minus-square-o mr-5" aria-hidden="true"> In-Active</i></a>';

                    //one master concept //other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + designform + ' ' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""><div class="text-left">' + Copy_to_other_branch + '</div><div class="text-left">' + View_old_version + '</div><div class="text-left">' + Access_Rights + '</div><div class="text-left">' + Map + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + designform + ' ' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""><div class="text-left">' + View_old_version + '</div><div class="text-left">' + Access_Rights + '</div><div class="text-left">' + Map + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';

                        }
                        else {
                            status = '<span class="label label-warning">In Active</span>';

                            active = '<a  data-toggle="tooltip" title="Make Active!" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="FB_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=FormBuilderdel(this,"' + emp.Formid + '","ACT")><i class="fa fa-check-square-o mr-5" aria-hidden="true"> Active</i></a>';

                   //one master concept    // other_info = '<div class="col-md-12 padding0"><div class="disp_if"> ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""><div class="text-left">' + Copy_to_other_branch + '</div><div class="text-left">' + View_old_version + '</div><div class="text-left">' + Access_Rights + '</div><div class="text-left">' + Map + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if"> ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""><div class="text-left">' + View_old_version + '</div><div class="text-left">' + Access_Rights + '</div><div class="text-left">' + Map + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';

                        }

                        if (emp.FormImage != '' && emp.FormImage != '-' && parseInt(emp.FormImage) != 0)
                        {
                            Form_Image = '<a target="_blank" href="' + $('#hdnCDfilepath').val() + "/" + $('#HospitalId').val() + "/" + "FormBuilderFileUpload/" + emp.FormImage + '">Form Image</a>';
                        }
                        else
                        {
                            Form_Image = '';
                        }

                    //one master concept
                     //   tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Form_Name + '</td><td>' + emp.Form_Type + '</td><td>' + emp.moduletype + '</td><td>' + emp.modulesubtype + '</td><td>' + emp.branch_name + '</td><td>' + emp.LastestversionId + '</td><td>' + status + '</td><td class="hide">' + emp.MappedPages + '</td><td>' + Form_Image + '</td><td>' + other_info + '</td></tr>';
                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Form_Name + '</td><td>' + emp.Form_Type + '</td><td>' + emp.moduletype + '</td><td>' + emp.modulesubtype + '</td><td>' + emp.LastestversionId + '</td><td>' + status + '</td><td class="hide">' + emp.MappedPages + '</td><td>' + Form_Image + '</td><td>' + other_info + '</td></tr>';

                    });
                    $('#tbl_op_formbuilder').html(tr);

                    datatable('op_formbuilder_table', 'op_form', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}
/////delete//active//inactive_for_Mapping
function Formbulider_map(event, Id, indicatorid) {
    debugger;

    var status = "";
    if (indicatorid == 'DEL') {
        status = "Are You Sure, You Want to Delete?";
    }
    else if (indicatorid == 'IA') {
        status = "Are You Sure, You Want to In-Activate?";
    }
    else if (indicatorid == 'AC') {

        status = "Are You Sure,You Want to Activate?";
    }
    toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", status,
          {

              closeButton: true,
              progressBar: true,
              allowHtml: true,
              timeOut: 20000000,

              onShown: function (toast) {
                  $("#confirmationRevertYes").click(function () {

                      var Delete = {
                          Dml_Indicator: indicatorid,
                          Id: Id
                      }

                      $.ajax({
                          async: true,
                          type: 'POST',
                          url: "/Form_builder/insert_update_deleteFormMapping",
                          contentType: 'application/json; charset=UTF-8',
                          data: JSON.stringify(Delete),
                          success: function (data) {
                              debugger;
                              var data1 = JSON.parse(data);

                              loaddata_mapping_op();
                              loaddata_mapping_ip();
                              loaddata_mapping_patient();
                              loaddata_mapping_survey();
                          }
                      });
                  });
              }
          })
};

///loaddata_mapping_op
function loaddata_mapping_op() {
    debugger;
    var load = {
        Dml_Indicator: "MOP"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMapping",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_mapping_op').empty();
                    $('#map_table_op').dataTable().fnClearTable();
                    $('#map_table_op').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var status = '';

                        status = '<span class="label label-success">Active</span>';
                        edit = '<a data-toggle="tooltip" title="Edit" class="btn btn-edit btn-xs" data-id="FB_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" onclick=FormBuilder_map_op(this,"' + emp.Id + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=Formbulider_map(this,"' + emp.Id + '","DEL")><i class="fa fa-trash" aria-hidden="true"></i></a>';
                        if (emp.status == '1') {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Formbulider_map(this,"' + emp.Id + '","IA")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';

                        }
                        else {
                            status = '<span class="label label-warning">In Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Formbulider_map(this,"' + emp.Id + '","AC")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';
                        }


                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Branchid + '</td><td>' + emp.Departmentid + '</td><td>' + emp.Subdepartmentid + '</td><td>' + emp.pageid + '</td><td>' + emp.Activefrom + '</td><td>' + emp.ActiveTo + '</td><td>' + status + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_mapping_op').html(tr);

                    datatable('map_table_op', 'map_title_op', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}

///////insert_access_rights_op
$('#btn_add_op').click(function () {
    debugger;

    if ($.trim($('#op_access_branch').val()) == '0') {
        $('#Pop_access_branch').show();
        $('#Pop_access_branch').fadeOut(8000);
        $('#Pop_access_branch').html('Select Branch');
        $('#op_access_branch').focus();
        return false;
    }

    if ($.trim($('#op_access_userrole').val()) == '0') {
        $('#Pop_access_userrole').show();
        $('#Pop_access_userrole').fadeOut(8000);
        $('#Pop_access_userrole').html('Select User Role');
        $('#op_access_userrole').focus();
        return false;
    }

    if ($.trim($('#op_access_user').val()) == '0') {
        $('#Pop_access_user').show();
        $('#Pop_access_user').fadeOut(8000);
        $('#Pop_access_user').html('Select User');
        $('#op_access_user').focus();
        return false;
    }

    if ($.trim($('#op_access_from').val()) == '') {
        $('#Pop_access_from').show();
        $('#Pop_access_from').fadeOut(8000);
        $('#Pop_access_from').html('Select Access From');
        $('#op_access_from').focus();
        return false;
    }

    if ($.trim($('#op_access_to').val()) == '0') {
        $('#Pop_access_to').show();
        $('#Pop_access_to').fadeOut(8000);
        $('#Pop_access_to').html('Select Access To');
        $('#op_access_to').focus();
        return false;
    }

    var oldUserid = $('#HoldUserid').val();
    var Userid = $('#op_access_user').val();
    var Branchid = $('#op_access_branch').val();

    var rowcount = $('#access_rights_table_op >tbody >tr').length;

    if (oldUserid != Userid) {
        for (var i = 0; i < rowcount; i++) {

            if (($('#tdUserid_' + i).val() == Userid) && ($('#tdbranchid_' + i).val() == Branchid)) {

                $('#Pop_access_user').show();
                $('#Pop_access_user').fadeOut(8000);
                $('#Pop_access_user').html('User already Inserted.');
                $('#op_access_user').val(0);
                $('#op_access_user').focus();
                return false;
            }

        }
    }

    var Formid = $('#Hformid').val();
    var frm_date = $('#op_access_from').val();
    var to_date = $('#op_access_to').val();

    var date_split_fd = frm_date.split('/');
    var f_date = date_split_fd[2] + '-' + date_split_fd[1] + '-' + date_split_fd[0];

    var date_split_td = to_date.split('/');
    var t_date = date_split_td[2] + '-' + date_split_td[1] + '-' + date_split_td[0];



    var Submit = ''
    if ($('#autodevice_Id').val() == '') {
        Submit = {
            Branch_id: $('#op_access_branch').val().toString(),
            //Category_id: $('#op_access_category').val().toString(),
            //Subcategory_id: $('#op_access_subcategory').val().toString(),
            //Department_id: $('#op_access_department').val().toString(),
            //Subdepartment_id: $('#op_access_subdepartment').val().toString(),
            Userrole_id: $('#op_access_userrole').val().toString(),
            User_id: $('#op_access_user').val().toString(),
            Access_from: f_date,
            Access_to: t_date,
            // Id: $("#autodevice_Id").val(),
            Formid: Formid,
            Dml_Indicator: 'INS'
        }
    }
    else {
        Submit = {

            Branch_id: $('#op_access_branch').val().toString(),
            //Category_id: $('#op_access_category').val().toString(),
            //Subcategory_id: $('#op_access_subcategory').val().toString(),
            //Department_id: $('#op_access_department').val().toString(),
            //Subdepartment_id: $('#op_access_subdepartment').val().toString(),
            Userrole_id: $('#op_access_userrole').val().toString(),
            User_id: $('#op_access_user').val().toString(),
            Access_from: f_date,
            Access_to: t_date,
            Id: $("#autodevice_Id").val(),
            Formid: Formid,
            Dml_Indicator: 'UPD'
        }
    }


    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormAccess",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Submit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var data2 = data1.data.Table;

            if (data1.msg == 'Success') {
                if (data2[0].msg == 'inserted') {
                    toastr.success("Added Successfully");
                    cleardata_op();
                    clear_op();
                    //LoadBranch();
                    //LoadRole();
                    loaddata_op();
                }
                else if (data2[0].msg == 'already') {
                    toastr.warning("Device Model Name already exists");
                    //  $("#Txtname").val("");
                    //  return false;
                }
                else if (data2[0].msg == 'updated') {
                    toastr.success("Updated Successfully");
                    cleardata_op();
                    clear_op();
                    //LoadBranch();
                    //LoadRole();
                    $("#btn_add_op").html("Add");

                    // location.reload();
                    loaddata_op();
                }
                else {
                    console.log(data1)
                }
            }
            else {
                console.log(data1)
                loaddata_op();
            }
        }

    })
});


function cleardata_op() {
    debugger;
    //$('#op_access_branch').val("").change();
    //$('#op_access_category').val("").change();
    //$('#op_access_subcategory').val("").change();
    //$('#op_access_department').val("").change();
    //$('#op_access_subdepartment').val("").change();
    $('#op_access_userrole').val("").change();
    //$('#op_access_user').val("").change();
    $('#autodevice_Id').val("");
    $('#op_access_from').val("");
    $('#op_access_to').val("");
    $('#op_access_user').val("0").change();
    $('#HoldUserid').val('');
    $("#btn_add_op").html("Add");
    $('#op_access_userrole').prop('disabled', false);
    $('#op_access_user').prop('disabled', false);
    $('#op_access_branch').val(0)
    $('#op_access_branch').prop('disabled', false);
    //LoadBranch();
    //LoadRole();

}

function Devicemodeledit_op(event, Id) {
    debugger;
    $('#autodevice_Id').val(Id)

    debugger;

    var Edit = {

        Id: Id,

        Dml_Indicator: 'EDI'
    };
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormAccess",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Edit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var emp = data1.data.Table;
            $("#op_access_branch").val(emp[0].Branchid);
            $('#op_access_branch').prop('disabled', true);
            //$('#Hcate_id').val(emp[0].Categoryid);
            //$('#Hscate_id').val(emp[0].Subcategoryid);
            //$('#Hdpt_id').val(emp[0].Departmentid);
            //$('#Hsdpt_id').val(emp[0].Subdepartmentid);
            //$("#op_access_branch").change();
            $('#Husr_id').val(emp[0].Userid);
            //$("#op_access_category").change();

            //$("#op_access_subdepartment").change();
            //$("#op_access_department").change();
            //$("#op_access_category").val(emp[0].Categoryid);
            //$("#op_access_subcategory").val(emp[0].Subcategoryid);
            //$("#op_access_department").val(emp[0].Departmentid);
            //$("#op_access_subdepartment").val(emp[0].Subdepartmentid);
            $("#op_access_userrole").val(emp[0].Userroleid).change();
            $('#op_access_userrole').prop('disabled', true);

            //$("#op_access_user").val(emp[0].Userid);
            $('#op_access_user').prop('disabled', true);
            $("#op_access_from").val(emp[0].Accessfrom);
            $("#op_access_to").val(emp[0].Accessto);
            $("#btn_add_op").html("Update");
            //$('#op_access_branch').focus();
            $('#HoldUserid').val(emp[0].Userid);
            LoadUser(emp[0].Userid);
        }

    })


}

/////delete//active//inactive_for_AccessRightstable
function Devicemodeldel(event, Id, indicatorid) {
    debugger;

    var status = "";
    if (indicatorid == 'DL') {
        status = "Are You Sure, You Want to Delete?";
    }
    else if (indicatorid == 'Inactive') {
        status = "Are You Sure, You Want to In-Activate?";
    }
    else if (indicatorid == 'Active') {

        status = "Are You Sure,You Want to Activate?";
    }
    toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", status,
          {

              closeButton: true,
              progressBar: true,
              allowHtml: true,
              timeOut: 20000000,

              onShown: function (toast) {
                  $("#confirmationRevertYes").click(function () {

                      var Delete = {
                          Dml_Indicator: indicatorid,
                          Id: Id
                      }

                      $.ajax({
                          async: true,
                          type: 'POST',
                          url: "/Form_builder/insert_update_deleteFormAccess",
                          contentType: 'application/json; charset=UTF-8',
                          data: JSON.stringify(Delete),
                          success: function (data) {
                              debugger;
                              var data1 = JSON.parse(data);

                              if (indicatorid == 'DL') {

                                  toastr.success("Deleted Successfully");
                              }
                              else if (indicatorid == 'Inactive') {

                                  toastr.success("In-Activate Successfully");
                              }
                              else if (indicatorid == 'Active') {

                                  toastr.success("Activate Successfully");
                              }

                              loaddata_op();
                              //loaddata_ip();
                              //loaddata_patient();
                              loaddata_survey;
                          }
                      });
                  });
              }
          })
};



/////Loaddata-Access_rights_op
function loaddata_op() {
    debugger;
    var Formid = $('#Hformid').val();
    var load = {
        Dml_Indicator: "ARO",
        Formid: Formid,
        Branch_id: $('#HospitalBranchID').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormAccess",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_access_rights_op').empty();
                    //$('#access_rights_table_op').DataTable().destroy();
                    //$('#access_rights_table_op').dataTable().fnClearTable();
                    //$('#access_rights_table_op').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var no_user = data1.Table2[0].no_of_user;
                    $('#no_users_op').html(no_user)
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var status = '';

                        status = '<span class="label label-success">Active</span>';
                        edit = '<a data-toggle="tooltip" title="Edit" class="btn btn-edit btn-xs" data-id="FB_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" onclick=Devicemodeledit_op(this,"' + emp.Id + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=Devicemodeldel(this,"' + emp.Id + '","DL")><i class="fa fa-trash" aria-hidden="true"></i></a>';
                        if (emp.status == '1') {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a class="btn-blue"  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Devicemodeldel(this,"' + emp.Id + '","Inactive")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';

                        }
                        else {
                            status = '<span class="label label-warning">In Active</span>';
                            active = '<a class="btn-blue" data-toggle="tooltip" title="Make Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Devicemodeldel(this,"' + emp.Id + '","Active")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';
                        }


                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td><input type="hidden" id="tdbranchid_' + i + '" value="' + emp.Branchid + '" />' + emp.Branchid + '</td><td>' + emp.Userroleid + '</td><td><input type="hidden" id="tdUserid_' + i + '" value="' + emp.user_id + '" />' + emp.Userid + '</td><td>' + emp.Accessfrom + '</td><td>' + emp.Accessto + '</td><td>' + status + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_access_rights_op').html(tr);

                    //datatable('access_rights_table_op', 'access_rights_title_op', 'Legal');
                    $('#access_rights_table_op').DataTable();
                }
            }
            else {
                console.log(data);
            }
        }
    });
}
//access rights ip onclick function
function access_rights_ip(event, Formid, Form_Name) {
    debugger;

    //loaddata_ip();

    LoadCategory_ip();
    LoadDepartment_ip();
    LoadUser_ip();
    Formid: $('#formid_ip').html(Formid)
    Form_Name: $('#formname_ip').html(Form_Name)
    $('#Hformid').val(Formid);
}

////btn x close  ip access tab
$('#Btn_x_ip_acc').click(function () {
    debugger;
    cleardata_ip();
    clear_ip()
})

///btn for ip close_the_access_tab
$('#Btn_clr_ip_acc').click(function () {
    debugger;
    cleardata_ip();
    clear_ip()
})


///onclick function map ip
function map_ip(event, Formid) {
    debugger;
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {

                     return;
                 }
                 else {
                     //  LoadBranch_map_ip();

                     LoadDepartment_map_ip();
                     LoadSubDepartment_map_ip();
                     // Loadpagename_ip();
                     loaddata_mapping_ip();
                     $('#Hformid').val(Formid);
                 }
             }
         }
     })
}
////btn x close  ip map tab
$('#Btn_x_ip_map').click(function () {
    debugger;
    cleardata_ip_map();


})

///btn for ip close_the_map_tab
$('#Btn_clr_ip_map').click(function () {
    debugger;
    cleardata_ip_map();
})

////////  IP
$('#ip_formbuilder_tab').click(function () {
    debugger;
    loaddata1()
})


/////loadtable IP
function loaddata1() {
    debugger;
    var load = {
        Dml_Indicator: "IP"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMaster",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_ip').empty();
                    $('#ip_formbuilder_table').dataTable().fnClearTable();
                    $('#ip_formbuilder_table').dataTable().fnDestroy();

                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;



                        var status = '';

                        status = '<span class="label label-success">Active</span>';

                        edit = '<a title="Edit" class="btn btn-edit btn-xs" data-id="FB_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" data-toggle="modal" data-target="#form-builder-popup" onclick=FormBuildereditip(this,"' + emp.Formid + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';

                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=FormBuilderdel(this,"' + emp.Formid + '","DE")><i class="fa fa-trash" aria-hidden="true"></i></a>';

                        View_old_version = '<p data-toggle="modal" data-target="#view_old_version_ip" aria-hidden="true"><a class="btn-blue" onclick=\'view_old_version_ip(this,"' + emp.Form_Name + '")\'>View old version</a></p>'

                        Access_Rights = '<p data-toggle="modal" data-target="#access_rights_ip" aria-hidden="true"><a class="btn-blue" id="access_rights_tt" onclick= \'access_rights_ip(this,"' + emp.Formid + '","' + emp.Form_Name + '")\'>Access Rights</a></p>'

                        Map = '<p data-toggle="modal" data-target="#map_ip" aria-hidden="true"><a class="btn-blue" id="mapping" onclick=\'map_ip(this,"' + emp.Formid + '")\'>Map</a></p>'

                        Send = '<p data-toggle="modal" data-target="#send_ip" aria-hidden="true"><a class="btn-blue">Send</a></p>'

                        Download_Print = '<p data-toggle="modal" data-target="#download/print" aria-hidden="true"><a class="btn-blue">Download/Print</a</p>'

                        Print = '<p <a data-toggle="tooltip" data-placement="bottom" title="Print" type="button" class="btn btn-edit btn-xs btn_r" data-id="" data-type="role_rights_edit" data-nav="redirect" data-name=""><i class="fa fa-print" aria-hidden="true"></i></a></p>'

                        Download = '<p <a data-toggle="tooltip" data-placement="bottom" title="Download" type="button" class="btn btn-edit btn-xs btn_r" data-id="" data-type="role_rights_edit" data-nav="redirect" data-name=""><i class="fa fa-download" aria-hidden="true"></i></a></p>'

                        if (emp.status == '1') {

                            status = '<span class="label label-success"> Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=FormBuilderdel(this,"' + emp.Formid + '","INACT")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';

                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + View_old_version + '</div><div class="text-left">' + Access_Rights + '</div><div class="text-left">' + Map + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + Download_Print + '</div><div class="text-left">' + active + '</div></div></div></div>';
                        }
                        else if (emp.status == '2') {
                            status = '<span class="label label-warning"> In Active</span>';

                            active = '<a  data-toggle="tooltip" title="Make Active!" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=FormBuilderdel(this,"' + emp.Formid + '","ACT")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';


                            other_info = '<div class="col-md-12 padding0"><div class="disp_if"> ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + View_old_version + '</div><div class="text-left">' + Access_Rights + '</div><div class="text-left">' + Map + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + Download_Print + '</div><div class="text-left">' + active + '</div></div></div></div>';
                        }
                        else {

                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=FormBuilderdel(this,"' + emp.Formid + '","INACT")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';

                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + View_old_version + '</div><div class="text-left">' + Access_Rights + '</div><div class="text-left">' + Map + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + Download_Print + '</div><div class="text-left">' + active + '</div></div></div></div>';
                        }



                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Form_Name + '</td><td>' + emp.Form_Type + '</td><td>' + emp.LastestversionId + '</td><td>' + status + '</td><td>' + emp.MappedPages + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_ip').html(tr);

                    datatable('ip_formbuilder_table', 'ip_form', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}
///loaddata_mapping_ip
function loaddata_mapping_ip() {
    debugger;
    var load = {
        Dml_Indicator: "MIP"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMapping",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_mapping_ip').empty();
                    $('#map_table_ip').dataTable().fnClearTable();
                    $('#map_table_ip').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var status = '';

                        status = '<span class="label label-success">Active</span>';
                        edit = '<a data-toggle="tooltip" title="Edit" class="btn btn-edit btn-xs" data-id="FB_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" onclick=FormBuilder_map_ip(this,"' + emp.Id + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=Formbulider_map(this,"' + emp.Id + '","DEL")><i class="fa fa-trash" aria-hidden="true"></i></a>';
                        if (emp.status == '1') {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Formbulider_map(this,"' + emp.Id + '","IA")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';

                        }
                        else {
                            status = '<span class="label label-warning">In Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Formbulider_map(this,"' + emp.Id + '","AC")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';
                        }


                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Branchid + '</td><td>' + emp.Departmentid + '</td><td>' + emp.Subdepartmentid + '</td><td>' + emp.pageid + '</td><td>' + emp.Activefrom + '</td><td>' + emp.ActiveTo + '</td><td>' + status + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_mapping_ip').html(tr);

                    datatable('map_table_ip', 'map_title_ip', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}

//// insert_Access_rights_ip

$('#btn_add_ip').click(function () {
    debugger;
    var Formid = $('#Hformid').val();
    var frm_date = $('#ip_access_from').val();
    var to_date = $('#ip_access_to').val();

    var date_split_fd = frm_date.split('/');
    var f_date = date_split_fd[2] + '-' + date_split_fd[1] + '-' + date_split_fd[0];

    var date_split_td = to_date.split('/');
    var t_date = date_split_td[2] + '-' + date_split_td[1] + '-' + date_split_td[0];
    var Submit = ''
    if ($('#autodevice_Id').val() == '') {
        Submit = {
            Branch_id: $('#ip_access_branch').val(),
            Category_id: $('#ip_access_category').val().toString(),
            Subcategory_id: $('#ip_access_sub_category').val().toString(),
            Department_id: $('#ip_access_department').val().toString(),
            Subdepartment_id: $('#ip_access_sub_department').val().toString(),
            Userrole_id: $('#ip_access_user_role').val().toString(),
            User_id: $('#ip_access_user').val().toString(),
            Access_from: f_date,
            Access_to: t_date,
            Formid: Formid,
            Dml_Indicator: 'INS'
        }
    }
    else {
        Submit = {

            Branch_id: $('#ip_access_branch').val().toString(),
            Category_id: $('#ip_access_category').val().toString(),
            Subcategory_id: $('#ip_access_sub_category').val().toString(),
            Department_id: $('#ip_access_department').val().toString(),
            Subdepartment_id: $('#ip_access_sub_department').val().toString(),
            Userrole_id: $('#ip_access_user_role').val().toString(),
            User_id: $('#ip_access_user').val().toString(),
            Access_from: f_date,
            Access_to: t_date,
            Id: $("#autodevice_Id").val(),
            Formid: Formid,
            Dml_Indicator: 'UPD'

        }
    }


    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormAccess",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Submit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var data2 = data1.data.Table;

            if (data1.msg == 'Success') {
                if (data2[0].msg == 'inserted') {
                    toastr.success("Added Successfully");
                    cleardata_ip()
                    clear_ip()
                    LoadBranch();
                    //loaddata_ip();
                }
                else if (data2[0].msg == 'already') {
                    toastr.warning("Device Model Name already exists");
                    //  $("#Txtname").val("");
                    //  return false;
                }
                else if (data2[0].msg == 'updated') {
                    toastr.success("Updated Successfully");
                    $("#btn_add_ip").html("Add");
                    cleardata_ip()
                    clear_ip()
                    LoadBranch();
                    // location.reload();
                    //loaddata_ip();
                }
                else {
                    console.log(data1)
                }
            }
            else {
                console.log(data1)
                //loaddata_ip();
            }
        }

    })
});

function cleardata_ip() {
    $('#ip_access_branch').val("").empty();
    $('#ip_access_category').val("").empty();
    $('#ip_access_sub_category').val("").empty();
    $('#ip_access_department').val("").empty();
    $('#ip_access_sub_department').val("").empty();
    $('#ip_access_user_role').val("").empty();
    $('#ip_access_user').val("").empty();
    $('#ip_access_from').val("");
    $('#ip_access_to').val("");
    $('#autodevice_Id').val("");
    //$('#Hcate_id_ip').val('').change();
    //$('#Hscate_id_ip').val('').change();
    //$('#Hdpt_id_ip').val('').change();
    //$('#Hsdpt_id_ip').val('').change();
    //$('#Husr_id_ip').val('').change();
    $("#btn_add_ip").html("Add");
    LoadBranch();
    LoadRole();
}
function clear_ip() {

    $('#Hcate_id_ip').val('').change();
    $('#Hscate_id_ip').val('').change();
    $('#Hdpt_id_ip').val('').change();
    $('#Hsdpt_id_ip').val('').change();
    $('#Husr_id_ip').val('').change();
    $("#btn_add_ip").html("Add");
    LoadBranch();
    LoadRole();
}





function Devicemodeledit_ip(event, Id) {
    $('#autodevice_Id').val(Id)

    debugger;
    // $("#Device_model").show();
    //  $("#access_rights_table").hide();
    // $("#top_header").hide();
    var Edit = {

        Id: Id,

        Dml_Indicator: 'EDI'
    };
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormAccess",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Edit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var emp = data1.data.Table;
            $("#ip_access_branch").val(emp[0].Branchid);
            $('#Hcate_id_ip').val(emp[0].Categoryid);
            $('#Hscate_id_ip').val(emp[0].Subcategoryid);
            $('#Hdpt_id_ip').val(emp[0].Departmentid);
            $('#Hsdpt_id_ip').val(emp[0].Subdepartmentid);
            $('#Husr_id_ip').val(emp[0].Userid);
            $("#ip_access_category").change();
            $("#ip_access_branch").change();
            $("#ip_access_sub_department").change();
            $("#ip_access_department").change();
            $("#ip_access_user_role").val(emp[0].Userroleid);
            $("#ip_access_from").val(emp[0].Accessfrom);
            $("#ip_access_to").val(emp[0].Accessto);
            $("#btn_add_ip").html("Update");



        }

    })
    $('#ip_access_branch').focus();
}



/////Loaddata-Access_rights_ip
function loaddata_ip() {
    debugger;
    var load = {
        Dml_Indicator: "ARI"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormAccess",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_access_rights_ip').empty();
                    $('#access_rights_table_ip').dataTable().fnClearTable();
                    $('#access_rights_table_ip').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var no_user = data1.Table2[0].no_of_user;
                    $('#no_users_ip').html(no_user)
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var status = '';

                        status = '<span class="label label-success">Active</span>';
                        edit = '<a data-toggle="tooltip" title="Edit" class="btn btn-edit btn-xs" data-id="FB_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" onclick=Devicemodeledit_ip(this,"' + emp.Id + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=Devicemodeldel(this,"' + emp.Id + '","DL")><i class="fa fa-trash" aria-hidden="true"></i></a>';
                        if (emp.status == '1') {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a class="btn-blue" data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Devicemodeldel(this,"' + emp.Id + '","Inactive")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';

                        }
                        else {
                            status = '<span class="label label-warning">In Active</span>';
                            active = '<a class="btn-blue" data-toggle="tooltip" title="Make Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Devicemodeldel(this,"' + emp.Id + '","Active")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';
                        }


                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Branchid + '</td><td>' + emp.Categoryid + '</td><td>' + emp.Subcategoryid + '</td><td>' + emp.Departmentid + '</td><td>' + emp.Subdepartmentid + '</td><td>' + emp.Userroleid + '</td><td>' + emp.Userid + '</td><td>' + emp.Accessfrom + '</td><td>' + emp.Accessto + '</td><td>' + status + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_access_rights_ip').html(tr);

                    datatable('access_rights_table_ip', 'access_rights_title_ip', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}
/////delete//active//inactive_for_AccessRightstable
//function Devicemodeldel(event, Id, indicatorid) {
//    debugger;

//    var status = "";
//    if (indicatorid == 'DL') {
//        status = "Are You Sure, You Want to Delete?";
//    }
//    else if (indicatorid == 'Inactive') {
//        status = "Are You Sure, You Want to In-Activate?";
//    }
//    else if (indicatorid == 'Active') {

//        status = "Are You Sure,You Want to Activate?";
//    }
//    toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", status,
//          {

//              closeButton: true,
//              progressBar: true,
//              allowHtml: true,
//              timeOut: 20000000,

//              onShown: function (toast) {
//                  $("#confirmationRevertYes").click(function () {

//                      var Delete = {
//                          Dml_Indicator: indicatorid,
//                          Id: Id
//                      }

//                      $.ajax({
//                          async: true,
//                          type: 'POST',
//                          url: "/Form_builder/insert_update_deleteFormAccess",
//                          contentType: 'application/json; charset=UTF-8',
//                          data: JSON.stringify(Delete),
//                          success: function (data) {
//                              debugger;
//                              var data1 = JSON.parse(data);

//                              loaddata_op();
//                              //loaddata_ip();
//                              //loaddata_patient();
//                              loaddata_survey;
//                          }
//                      });
//                  });
//              }
//          })
//};
//access rights patient onclick function
function access_rights_patient(event, Formid, Form_Name) {
    debugger;

    //loaddata_patient();
    //  LoadBranch_patient();
    LoadCategory_patient();
    LoadDepartment_patient();
    //  LoadRole_patient();
    LoadUser_patient();
    Formid: $('#formid_patient').html(Formid)
    Form_Name: $('#formname_patient').html(Form_Name)
    $('#Hformid').val(Formid);

}

////btn x close  patient access tab
$('#Btn_x_pat_acc').click(function () {
    debugger;
    cleardata_patient();
    clear_patient();
})

///btn for patient close_the_access_tab
$('#Btn_clr_pat_acc').click(function () {
    debugger;
    cleardata_patient();
    clear_patient();
})

///onclick function map patient
function map_patient(event, Formid) {
    debugger;
    // LoadBranch_map_patient();

    LoadDepartment_map_patient();
    LoadSubDepartment_map_patient();
    //  Loadpagename_patient();
    loaddata_mapping_patient();
    $('#Hformid').val(Formid);
}
////btn x close  patient map tab
$('#Btn_x_pat_map').click(function () {
    debugger;
    cleardata_patient_map();
})

///btn for patient close_the_map_tab
$('#Btn_clr_pat_map').click(function () {
    debugger;
    cleardata_patient_map();
})



////////  Patient
$("#pateint_formbuilder_tab").click(function () {
    debugger;
    loaddata2()
})

function loaddata2() {
    debugger;
    var load = {
        Dml_Indicator: "PT"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMaster",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_patient').empty();
                    $('#patient_formbuilder_table').dataTable().fnClearTable();
                    $('#patient_formbuilder_table').dataTable().fnDestroy();

                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;



                        var status = '';

                        status = '<span class="label label-success">Active</span>';

                        edit = '<a title="Edit" class="btn btn-edit btn-xs" data-id="FB_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" data-toggle="modal" data-target="#form-builder-popup" onclick=FormBuildereditpat(this,"' + emp.Formid + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';

                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=FormBuilderdel(this,"' + emp.Formid + '","DE")><i class="fa fa-trash" aria-hidden="true"></i></a>';

                        View_old_version = '<p data-toggle="modal" data-target="#view_old_version_patient" aria-hidden="true"><a class="btn-blue" onclick=\'view_old_version_pat(this,"' + emp.Form_Name + '")\'>View old version</a></p>'

                        Access_Rights = '<p data-toggle="modal" data-target="#access_rights_patient" aria-hidden="true"><a class="btn-blue" id="access_rights_tt" onclick=\'access_rights_patient(this,"' + emp.Formid + '","' + emp.Form_Name + '")\'>Access Rights</a></p>'

                        Map = '<p data-toggle="modal" data-target="#map_patient" aria-hidden="true"><a class="btn-blue" id="mapping" onclick=\'map_patient(this,"' + emp.Formid + '")\'>Map</a></p>'

                        Send = '<p data-toggle="modal" data-target="#send_patient" aria-hidden="true"><a class="btn-blue">Send</a></p>'

                        Download_Print = '<p data-toggle="modal" data-target="#download/print" aria-hidden="true"><a class="btn-blue">Download/Print</a</p>'

                        Print = '<p <a data-toggle="tooltip" data-placement="bottom" title="Print" type="button" class="btn btn-edit btn-xs btn_r" data-id="" data-type="role_rights_edit" data-nav="redirect" data-name=""><i class="fa fa-print" aria-hidden="true"></i></a></p>'

                        Download = '<p <a data-toggle="tooltip" data-placement="bottom" title="Download" type="button" class="btn btn-edit btn-xs btn_r" data-id="" data-type="role_rights_edit" data-nav="redirect" data-name=""><i class="fa fa-download" aria-hidden="true"></i></a></p>'

                        if (emp.status == '1') {

                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=FormBuilderdel(this,"' + emp.Formid + '","INACT")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';


                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + View_old_version + '</div><div class="text-left">' + Access_Rights + '</div><div class="text-left">' + Map + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + Download_Print + '</div><div class="text-left">' + active + '</div></div></div></div>';
                        }
                        else if (emp.status == '2') {
                            status = '<span class="label label-warning">In Active</span>';

                            active = '<a  data-toggle="tooltip" title="Make Active!" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=FormBuilderdel(this,"' + emp.Formid + '","ACT")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';


                            other_info = '<div class="col-md-12 padding0"><div class="disp_if"> ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + View_old_version + '</div><div class="text-left">' + Access_Rights + '</div><div class="text-left">' + Map + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + Download_Print + '</div><div class="text-left">' + active + '</div></div></div></div>';
                        }
                        else {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=FormBuilderdel(this,"' + emp.Formid + '","INACT")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';


                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + View_old_version + '</div><div class="text-left">' + Access_Rights + '</div><div class="text-left">' + Map + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + Download_Print + '</div><div class="text-left">' + active + '</div></div></div></div>';
                        }



                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Form_Name + '</td><td>' + emp.Form_Type + '</td><td>' + emp.LastestversionId + '</td><td>' + status + '</td><td>' + emp.MappedPages + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_patient').html(tr);

                    datatable('patient_formbuilder_table', 'pateint_form', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}



///loaddata_mapping_patient
function loaddata_mapping_patient() {
    debugger;
    var load = {
        Dml_Indicator: "MPA"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMapping",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_mapping_patient').empty();
                    $('#map_table_patient').dataTable().fnClearTable();
                    $('#map_table_patient').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var status = '';

                        status = '<span class="label label-success">Active</span>';
                        edit = '<a data-toggle="tooltip" title="Edit" class="btn btn-edit btn-xs" data-id="FB_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" onclick=FormBuilder_map_patient(this,"' + emp.Id + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=Formbulider_map(this,"' + emp.Id + '","DEL")><i class="fa fa-trash" aria-hidden="true"></i></a>';
                        if (emp.status == '1') {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Formbulider_map(this,"' + emp.Id + '","IA")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';

                        }
                        else {
                            status = '<span class="label label-warning">In Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Formbulider_map(this,"' + emp.Id + '","AC")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';
                        }


                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Branchid + '</td><td>' + emp.Departmentid + '</td><td>' + emp.Subdepartmentid + '</td><td>' + emp.pageid + '</td><td>' + emp.Activefrom + '</td><td>' + emp.ActiveTo + '</td><td>' + status + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_mapping_patient').html(tr);

                    datatable('map_table_Patient', 'map_title_patient', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}

//// insert_Access_rights_Patient  



$('#btn_add_patient').click(function () {
    debugger;
    var Formid = $('#Hformid').val();
    var frm_date = $('#patient_access_from').val();
    var to_date = $('#patient_access_to').val();

    var date_split_fd = frm_date.split('/');
    var f_date = date_split_fd[2] + '-' + date_split_fd[1] + '-' + date_split_fd[0];

    var date_split_td = to_date.split('/');
    var t_date = date_split_td[2] + '-' + date_split_td[1] + '-' + date_split_td[0];
    var Submit = ''
    if ($('#autodevice_Id').val() == '') {
        Submit = {
            Branch_id: $('#patient_access_branch').val().toString(),
            Category_id: $('#patient_access_category').val().toString(),
            Subcategory_id: $('#patient_access_sub_category').val().toString(),
            Department_id: $('#patient_access_department').val().toString(),
            Subdepartment_id: $('#patient_access_sub_department').val().toString(),
            Userrole_id: $('#patient_access_user_role').val().toString(),
            User_id: $('#patient_access_user').val().toString(),
            Access_from: f_date,
            Access_to: t_date,
            Formid: Formid,
            Dml_Indicator: 'INS'
        }
    }
    else {
        Submit = {

            Branch_id: $('#patient_access_branch').val().toString(),
            Category_id: $('#patient_access_category').val().toString(),
            Subcategory_id: $('#patient_access_sub_category').val().toString(),
            Department_id: $('#patient_access_department').val().toString(),
            Subdepartment_id: $('#patient_access_sub_department').val().toString(),
            Userrole_id: $('#patient_access_user_role').val().toString(),
            User_id: $('#patient_access_user').val().toString(),
            Access_from: f_date,
            Access_to: t_date,
            Formid: Formid,
            Id: $("#autodevice_Id").val(),
            Dml_Indicator: 'UPD'

        }
    }


    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormAccess",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Submit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var data2 = data1.data.Table;

            if (data1.msg == 'Success') {
                if (data2[0].msg == 'inserted') {
                    toastr.success("Added Successfully");
                    cleardata_patient();
                    clear_patient();
                    LoadBranch();
                    //loaddata_patient();
                }
                else if (data2[0].msg == 'already') {
                    toastr.warning("Device Model Name already exists");
                    //  $("#Txtname").val("");
                    //  return false;
                }
                else if (data2[0].msg == 'updated') {
                    toastr.success("Updated Successfully");
                    $("#btn_add_patient").html("Add");
                    cleardata_patient();
                    clear_patient();
                    LoadBranch();
                    // location.reload();
                    //loaddata_patient();
                }
                else {
                    console.log(data1)
                }
            }
            else {
                console.log(data1)
                //loaddata_patient();
            }
        }

    })
});

function cleardata_patient() {
    debugger;
    $('#patient_access_branch').val("").empty();
    $('#patient_access_category').val("").empty();
    $('#patient_access_sub_category').val("").empty();
    $('#patient_access_department').val("").empty();
    $('#patient_access_sub_department').val("").empty();
    $('#patient_access_user_role').val("").empty();
    $('#patient_access_user').val("").empty();
    $('#patient_access_from').val("");
    $('#patient_access_to').val("");
    $('#autodevice_Id').val("");
    //$('#Hcate_id_patient').val('');
    //$('#Hscate_id_patient').val('');
    //$('#Hdpt_id_patient').val('');
    //$('#Hsdpt_id_patient').val('');
    //$('#Husr_id_patient').val('');
    $("#btn_add_patient").html("Add");
    LoadBranch();
    LoadRole();

}
function clear_patient() {
    debugger;
    $('#Hcate_id_patient').val('').change();
    $('#Hscate_id_patient').val('').change();
    $('#Hdpt_id_patient').val('').change();
    $('#Hsdpt_id_patient').val('').change();
    $('#Husr_id_patient').val('').change();
    $("#btn_add_patient").html("Add");
    LoadBranch();
    LoadRole();

}



function Devicemodeledit_patient(event, Id) {
    $('#autodevice_Id').val(Id)

    debugger;
    // $("#Device_model").show();
    //  $("#access_rights_table").hide();
    // $("#top_header").hide();
    var Edit = {

        Id: Id,

        Dml_Indicator: 'EDI'
    };
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormAccess",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Edit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var emp = data1.data.Table;
            $("#patient_access_branch").val(emp[0].Branchid);
            $('#Hcate_id_patient').val(emp[0].Categoryid);
            $('#Hscate_id_patient').val(emp[0].Subcategoryid);
            $('#Hdpt_id_patient').val(emp[0].Departmentid);
            $('#Hsdpt_id_patient').val(emp[0].Subdepartmentid);
            $('#Husr_id_patient').val(emp[0].Userid);
            $("#patient_access_category").change();
            $("#patient_access_branch").change();
            $("#patient_access_sub_department").change();
            $("#patient_access_department").change();
            $("#patient_access_user_role").val(emp[0].Userroleid);
            $("#patient_access_from").val(emp[0].Accessfrom);
            $("#patient_access_to").val(emp[0].Accessto);
            $("#btn_add_patient").html("Update");



        }

    })
    $('#patient_access_branch').focus();
}


/////Loaddata-Access_rights_patient
function loaddata_patient() {
    debugger;
    var load = {
        Dml_Indicator: "ARP"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormAccess",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_access_rights_patient').empty();
                    $('#access_rights_table_patient').dataTable().fnClearTable();
                    $('#access_rights_table_patient').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var no_user = data1.Table2[0].no_of_user;
                    $('#no_users_pat').html(no_user)
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var status = '';

                        status = '<span class="label label-success">Active</span>';
                        edit = '<a data-toggle="tooltip" title="Edit" class="btn btn-edit btn-xs" data-id="FB_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" onclick=Devicemodeledit_patient(this,"' + emp.Id + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=Devicemodeldel(this,"' + emp.Id + '","DL")><i class="fa fa-trash" aria-hidden="true"></i></a>';
                        if (emp.status == '1') {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a class="btn-blue"  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Devicemodeldel(this,"' + emp.Id + '","Inactive")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';

                        }
                        else {
                            status = '<span class="label label-warning">In Active</span>';
                            active = '<a class="btn-blue" data-toggle="tooltip" title="Make Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Devicemodeldel(this,"' + emp.Id + '","Active")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';
                        }


                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Branchid + '</td><td>' + emp.Categoryid + '</td><td>' + emp.Subcategoryid + '</td><td>' + emp.Departmentid + '</td><td>' + emp.Subdepartmentid + '</td><td>' + emp.Userroleid + '</td><td>' + emp.Userid + '</td><td>' + emp.Accessfrom + '</td><td>' + emp.Accessto + '</td><td>' + status + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_access_rights_patient').html(tr);

                    datatable('access_rights_table_patient', 'access_rights_title_patient', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}

//access rights servey onclick function
function access_rights_survey(event, Formid, Form_Name) {
    debugger;

    //loaddata_survey();
    // LoadBranch_survey();
    LoadCategory_survey();
    LoadDepartment_survey();
    //  LoadRole_survey();
    LoadUser_survey();
    Formid: $('#formid_survey').html(Formid)
    Form_Name: $('#formname_survey').html(Form_Name)
    $('#Hformid').val(Formid);
}

////btn x close  suvey access tab
$('#Btn_x_sur_acc').click(function () {
    debugger;
    cleardata_survey();
    clear_survey();
})

///btn for survey close_the_access_tab
$('#Btn_clr_sur_acc').click(function () {
    debugger;
    cleardata_survey();
    clear_survey();
})

///onclick function map survey
function map_survey(event, Formid) {
    debugger;
    // LoadBranch_map_survey();

    LoadDepartment_map_survey();
    LoadSubDepartment_map_survey();
    // Loadpagename_survey();
    loaddata_mapping_survey();
    $('#Hformid').val(Formid);
}

////btn x close  suvey map tab
$('#Btn_x_sur_map').click(function () {
    debugger;
    cleardata_survey_map();
})

///btn for survey close_the_map_tab
$('#Btn_clr_sur_map').click(function () {
    debugger;
    cleardata_survey_map();
})



//////Survey table
$("#survey_formbuilder_tab").click(function () {
    debugger;
    loaddata3()
})

function loaddata3() {
    debugger;
    var load = {
        Dml_Indicator: "SV"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMaster",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_survey').empty();
                    $('#survey_formbuilder_table').dataTable().fnClearTable();
                    $('#survey_formbuilder_table').dataTable().fnDestroy();

                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;


                        var status = '';

                        status = '<span class="label label-success">Active</span>';

                        edit = '<a title="Edit" class="btn btn-edit btn-xs" data-id="FB_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" data-toggle="modal" data-target="#form-builder-popup" onclick=FormBuildereditsur(this,"' + emp.Formid + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';

                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=FormBuilderdel(this,"' + emp.Formid + '","DE")><i class="fa fa-trash" aria-hidden="true"></i></a>';

                        View_old_version = '<p data-toggle="modal" data-target="#view_old_version_survey" aria-hidden="true"><a class="btn-blue" onclick=\'view_old_version_sur(this,"' + emp.Form_Name + '")\'>View old version</a></p>'

                        Access_Rights = '<p data-toggle="modal" data-target="#access_rights_survey" aria-hidden="true"><a class="btn-blue" id="access_rights_tt" onclick=\'access_rights_survey(this,"' + emp.Formid + '","' + emp.Form_Name + '")\'>Access Rights</a></p>'

                        Map = '<p data-toggle="modal" data-target="#map_survey" aria-hidden="true"><a class="btn-blue" id="mapping" onclick=\'map_survey(this,"' + emp.Formid + '")\'>Map</a></p>'

                        Send = '<p data-toggle="modal" data-target="#send_survey" aria-hidden="true"><a class="btn-blue">Send</a></p>'

                        Download_Print = '<p data-toggle="modal" data-target="#download/print" aria-hidden="true"><a>Download/Print</a</p>'

                        Print = '<p <a data-toggle="tooltip" data-placement="bottom" title="Print" type="button" class="btn btn-edit btn-xs btn_r" data-id="" data-type="role_rights_edit" data-nav="redirect" data-name=""><i class="fa fa-print" aria-hidden="true"></i></a></p>'

                        Download = '<p <a data-toggle="tooltip" data-placement="bottom" title="Download" type="button" class="btn btn-edit btn-xs btn_r" data-id="" data-type="role_rights_edit" data-nav="redirect" data-name=""><i class="fa fa-download" aria-hidden="true"></i></a></p>'

                        if (emp.status == '1') {

                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=FormBuilderdel(this,"' + emp.Formid + '","INACT")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';


                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + View_old_version + '</div><div class="text-left">' + Access_Rights + '</div><div class="text-left">' + Map + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + Download_Print + '</div><div class="text-left">' + active + '</div></div></div></div>';
                        }
                        else if (emp.status == '2') {
                            status = '<span class="label label-warning">In Active</span>';

                            active = '<p  data-toggle="tooltip" title="Make Active!" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=FormBuilderdel(this,"' + emp.Formid + '","ACT")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';

                            other_info = '<div class="col-md-12 padding0"><div class="disp_if"> ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + View_old_version + '</div><div class="text-left">' + Access_Rights + '</div><div class="text-left">' + Map + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + Download_Print + '</div><div class="text-left">' + active + '</div></div></div></div>';
                        }
                        else {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=FormBuilderdel(this,"' + emp.Formid + '","INACT")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';


                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + View_old_version + '</div><div class="text-left">' + Access_Rights + '</div><div class="text-left">' + Map + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + Download_Print + '</div><div class="text-left">' + active + '</div></div></div></div>';
                        }


                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Form_Name + '</td><td>' + emp.Form_Type + '</td><td>' + emp.LastestversionId + '</td><td>' + status + '</td><td>' + emp.MappedPages + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_survey').html(tr);

                    datatable('survey_formbuilder_table', 'survey_form', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}

///loaddata_mapping_survey
function loaddata_mapping_survey() {
    debugger;
    var load = {
        Dml_Indicator: "MSU"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMapping",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_mapping_survey').empty();
                    $('#map_table_survey').dataTable().fnClearTable();
                    $('#map_table_survey').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var status = '';

                        status = '<span class="label label-success">Active</span>';
                        edit = '<a data-toggle="tooltip" title="Edit" class="btn btn-edit btn-xs" data-id="FB_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" onclick=FormBuilder_map_survey(this,"' + emp.Id + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=Formbulider_map(this,"' + emp.Id + '","DEL")><i class="fa fa-trash" aria-hidden="true"></i></a>';
                        if (emp.status == '1') {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Formbulider_map(this,"' + emp.Id + '","IA")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';

                        }
                        else {
                            status = '<span class="label label-warning">In Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Formbulider_map(this,"' + emp.Id + '","AC")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';
                        }


                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Branchid + '</td><td>' + emp.Departmentid + '</td><td>' + emp.Subdepartmentid + '</td><td>' + emp.pageid + '</td><td>' + emp.Activefrom + '</td><td>' + emp.ActiveTo + '</td><td>' + status + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_mapping_survey').html(tr);

                    datatable('map_table_survey', 'map_title_survey', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}

//// insert_Access_rights_survey  

$('#btn_add_survey').click(function () {
    debugger;
    var Formid = $('#Hformid').val();
    var frm_date = $('#survey_access_from').val();
    var to_date = $('#survey_access_to').val();

    var date_split_fd = frm_date.split('/');
    var f_date = date_split_fd[2] + '-' + date_split_fd[1] + '-' + date_split_fd[0];

    var date_split_td = to_date.split('/');
    var t_date = date_split_td[2] + '-' + date_split_td[1] + '-' + date_split_td[0];

    var Submit = ''
    if ($('#autodevice_Id').val() == '') {
        Submit = {
            Branch_id: $('#survey_access_branch').val().toString(),
            Category_id: $('#survey_access_category').val().toString(),
            Subcategory_id: $('#survey_access_sub_category').val().toString(),
            Department_id: $('#survey_access_department').val().toString(),
            Subdepartment_id: $('#survey_access_sub_department').val().toString(),
            Userrole_id: $('#survey_access_user_role').val().toString(),
            User_id: $('#survey_access_user').val().toString(),
            Access_from: f_date,
            Access_to: t_date,
            Formid: Formid,
            Dml_Indicator: 'INS'
        }
    }
    else {
        Submit = {

            Branch_id: $('#survey_access_branch').val().toString(),
            Category_id: $('#survey_access_category').val().toString(),
            Subcategory_id: $('#survey_access_sub_category').val().toString(),
            Department_id: $('#survey_access_department').val().toString(),
            Subdepartment_id: $('#survey_access_sub_department').val().toString(),
            Userrole_id: $('#survey_access_user_role').val().toString(),
            User_id: $('#survey_access_user').val().toString(),
            Access_from: f_date,
            Access_to: t_date,
            Formid: Formid,
            Id: $("#autodevice_Id").val(),
            Dml_Indicator: 'UPD'

        }
    }


    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormAccess",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Submit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var data2 = data1.data.Table;

            if (data1.msg == 'Success') {
                if (data2[0].msg == 'inserted') {
                    toastr.success("Added Successfully");
                    cleardata_survey();
                    clear_survey();
                    LoadBranch();
                    //loaddata_survey();
                }
                else if (data2[0].msg == 'already') {
                    toastr.warning("Device Model Name already exists");
                    //  $("#Txtname").val("");
                    //  return false;
                }
                else if (data2[0].msg == 'updated') {
                    toastr.success("Updated Successfully");
                    $("#btn_add_survey").html("Add");
                    clear_survey();
                    cleardata_survey();
                    LoadBranch();
                    // location.reload();
                    //loaddata_survey();
                }
                else {
                    console.log(data1)
                }
            }
            else {
                console.log(data1)
                //loaddata_survey();
            }
        }

    })
});

function cleardata_survey() {
    $('#survey_access_branch').val("").empty();
    $('#survey_access_category').val("").empty();
    $('#survey_access_sub_category').val("").empty();
    $('#survey_access_department').val("").empty();
    $('#survey_access_sub_department').val("").empty();
    $('#survey_access_user_role').val("").empty();
    $('#survey_access_user').val("").empty();
    $('#survey_access_from').val("");
    $('#survey_access_to').val("");
    $('#autodevice_Id').val("");
    //$('#Hcate_id_survey').val('');
    //$('#Hscate_id_survey').val('');
    //$('#Hdpt_id_survey').val('');
    //$('#Hsdpt_id_survey').val('');
    //$('#Husr_id_survey').val('');
    $("#btn_add_survey").html("Add");
    LoadBranch();
    LoadRole();

}
function clear_survey() {

    $('#Hcate_id_survey').val('');
    $('#Hscate_id_survey').val('');
    $('#Hdpt_id_survey').val('');
    $('#Hsdpt_id_survey').val('');
    $('#Husr_id_survey').val('');
    $("#btn_add_survey").html("Add");
    LoadBranch();
    LoadRole();

}



function Devicemodeledit_survey(event, Id) {
    $('#autodevice_Id').val(Id)

    debugger;
    // $("#Device_model").show();
    //  $("#access_rights_table").hide();
    // $("#top_header").hide();
    var Edit = {

        Id: Id,

        Dml_Indicator: 'EDI'
    };
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormAccess",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Edit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var emp = data1.data.Table;
            $("#survey_access_branch").val(emp[0].Branchid);
            $('#Hcate_id_survey').val(emp[0].Categoryid);
            $('#Hscate_id_survey').val(emp[0].Subcategoryid);
            $('#Hdpt_id_survey').val(emp[0].Departmentid);
            $('#Hsdpt_id_survey').val(emp[0].Subdepartmentid);
            $('#Husr_id_survey').val(emp[0].Userid);
            $("#survey_access_category").change();
            $("#survey_access_branch").change();
            $("#survey_access_sub_department").change();
            $("#survey_access_department").change();
            $("#survey_access_user_role").val(emp[0].Userroleid);
            $("#survey_access_from").val(emp[0].Accessfrom);
            $("#survey_access_to").val(emp[0].Accessto);
            $("#btn_add_survey").html("Update");



        }

    })
    $('#survey_access_branch').focus();
}


/////Loaddata-Access_rights_survey
function loaddata_survey() {
    debugger;
    var load = {
        Dml_Indicator: "ARS"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormAccess",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_access_rights_survey').empty();
                    $('#access_rights_table_survey').dataTable().fnClearTable();
                    $('#access_rights_table_survey').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var no_user = data1.Table2[0].no_of_user;
                    $('#no_users_sur').html(no_user)
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var status = '';

                        status = '<span class="label label-success">Active</span>';
                        edit = '<a data-toggle="tooltip" title="Edit" class="btn btn-edit btn-xs" data-id="FB_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" onclick=Devicemodeledit_survey(this,"' + emp.Id + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=Devicemodeldel(this,"' + emp.Id + '","DL")><i class="fa fa-trash" aria-hidden="true"></i></a>';
                        if (emp.status == '1') {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a class="btn-blue" data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Devicemodeldel(this,"' + emp.Id + '","Inactive")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';

                        }
                        else {
                            status = '<span class="label label-warning">In Active</span>';
                            active = '<a class="btn-blue" data-toggle="tooltip" title="Make Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Devicemodeldel(this,"' + emp.Id + '","Active")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + active + '</div></div></div></div>';
                        }


                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Branchid + '</td><td>' + emp.Categoryid + '</td><td>' + emp.Subcategoryid + '</td><td>' + emp.Departmentid + '</td><td>' + emp.Subdepartmentid + '</td><td>' + emp.Userroleid + '</td><td>' + emp.Userid + '</td><td>' + emp.Accessfrom + '</td><td>' + emp.Accessto + '</td><td>' + status + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_access_rights_survey').html(tr);

                    datatable('access_rights_table_survey', 'access_rights_title_survey', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}


//Load Branch
function LoadBranch(Branch_id) {
    debugger;


    var dml = ''
    if (parseInt($('#hiddenRole_id').val()) == 1) {
        dml = 'DLBRAN'
    }
    else {
        dml = 'LBRAN'
    }

    var subdepartment = {
        //Dml_Indicator: "LBRAN",
        Dml_Indicator: dml,
        name: $('#HiddenDoctorID').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment = "";
                    $('.Access_branch').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('.Access_branch').append(SubDepartment);
                        branch_change();

                    }
                    else {
                        $('.Access_branch').append(default_opt + SubDepartment);

                    }

                    //if ($('#hiddenRole_id').val() == 1) {
                    //    $('.Access_branch').val($('#HospitalBranchID').val());
                    //}
                    //else if (parseInt($('#hiddenRole_id').val()) != 2) {
                    //    $('.Access_branch').val($('#HospitalBranchID').val());
                    //    $('.Access_branch').attr('disabled', true);

                    //}
                    if ($('#hiddenRole_id').val() != 2) {
                        $('.Access_branch').val($('#HospitalBranchID').val());
                        $('.Access_branch').attr('disabled', true);
                    }
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}
$('#op_access_branch').change(function () {
    debugger;
    //LoadUser();
    var user = $('#Husr_id').val();
    var category_id = $('#Hcate_id').val();
    var subcategory_id = $('#Hscate_id').val();
    var department_id = $('#Hdpt_id').val();
    var subdepartment_id = $('#Hsdpt_id').val();
    // var user = ('#Husr_id').val();
    //if (user_id != '')
    //{
    //    $("#op_access_subdepartment").change();
    //    LoadUser(user_id)
    //}
    LoadRole();
    branch_change_op($('#op_access_branch').val(), category_id, subcategory_id, department_id, subdepartment_id, user);
})


function branch_change_op(branch, category_id, subcategory_id, department_id, subdepartment_id, user) {
    debugger;

    LoadDepartment(branch, department_id, subdepartment_id);

    LoadCategory(branch, category_id, subcategory_id);

}


// Load Department
function LoadDepartment(branch, department_id, subdepartment_id) {
    debugger;
    var department = {
        Dml_Indicator: "DP",
        branch_id: $('.Access_branch').val(),
        name: $('#DoctorUser_Id').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                console.log(data1)
                $('#op_access_subdepartment').empty();
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#op_access_department').html('');
                    $('#op_access_department').val('');
                    var Department;
                    $('#op_access_department').empty();
                    var default_opt = '<option value="0" disabled>--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    $('#op_access_department').append(default_opt + Department);
                    $('#op_access_department').prop('multiple', true);
                    $('#op_access_department').val('').change();
                }
            }
            var dpt = department_id;
            if (dpt != '' && dpt != null) {
                var d_id = '';
                var arr = [];
                if (dpt != null) {

                    d_id = dpt.split(',');

                    for (var i = 0; i < d_id.length; i++) {
                        arr[i] = $.trim(d_id[i]);
                    }
                    $('#op_access_department').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

//Used for Loading the Sub Department DropDown in Doctor Creation and in edit profile
$("#op_access_department").change(function () {
    debugger;
    document.getElementById("op_access_subdepartment").innerHTML = null;
    var condition = '(', dep_length = 0;
    dep_length = $('#op_access_department').val().length;
    for (var i = 0; i < dep_length; i++) {
        condition = condition + 'master_category_department_id=' + $('#op_access_department').val()[i]

        if (i != (dep_length - 1)) {
            condition = condition + ' or '
        }
    }
    condition = condition + ') and status=1'
    console.log(condition);
    var subdepartment = {
        Dml_Indicator: "SDM",
        condition: condition,
        name: $('#DoctorUser_Id').val()

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    $('#op_access_subdepartment').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        debugger;
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    $('#op_access_subdepartment').html(SubDepartment);
                    $('#op_access_subdepartment').prop('multiple', true);

                    $('#op_access_subdepartment').val('').change();
                }
            }

            //
            // LoadUser(user)

            var sdpt = $('#Hsdpt_id').val();
            if (sdpt != '' && sdpt != null) {
                var sd_id = '';
                var arr = [];
                if (sdpt != null) {

                    sd_id = sdpt.split(',');

                    for (var i = 0; i < sd_id.length; i++) {
                        arr[i] = $.trim(sd_id[i]);
                    }
                    $('#op_access_subdepartment').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
});
//Load UserRole

function LoadRole() {
    var RoleData = {
        Dml_Indicator: "SS",
        created_by: $('#HiddenDoctorID').val(),
       // branch_id: $('#op_access_branch').val()       //one master concept
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/CreateRole/DoctorRole',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(RoleData),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;
                debugger;
                if (jQuery.isEmptyObject((data))) {

                }
                else {
                    var tr = '';
                    var SelRole = '';
                    $('.Access_userrole').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    var j = 0;
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            j++;
                            SelRole = SelRole + '<option value="' + data[i].doctor_user_role_id + '">' + data[i].doctor_user_role_name + '</option>';
                        }

                        $('.Access_userrole').append(default_opt + SelRole);
                        if ($('#user_role_id').val() != '' && $('#user_role_id').val() != null) {
                            $('.Access_userrole').val($('#user_role_id').val());

                            if ($('.Access_userrole').val() == '1') {

                                var date = new Date();

                                date.setFullYear(new Date().getFullYear() - 24);
                                $('.DOB').datepicker('destroy').datepicker({
                                    format: 'dd/mm/yyyy',
                                    endDate: date,
                                    todayHighlight: true,
                                    autoclose: true
                                })
                            }
                            else {
                                var date = new Date();

                                date.setFullYear(new Date().getFullYear() - 18);
                                $('.DOB').datepicker('destroy').datepicker({
                                    format: 'dd/mm/yyyy',
                                    endDate: date,
                                    todayHighlight: true,
                                    autoclose: true
                                })

                            }
                        }
                        if ($('#myHiddenVar1').val() != '' && $('#myHiddenVar1').val() != null) {
                            $('.Access_userrole').attr('disabled', true);
                        }
                    }


                }

            }
            else {
                console.log(data);
            }

        }

    });

}
//If the role is doctor need to check the license
$('#op_access_userrole').change(function () {
    debugger;
    LoadUser();
    if ($('#op_access_userrole').val() == '1') {

        var date = new Date();

        date.setFullYear(new Date().getFullYear() - 24);
        $('.DOB').datepicker('destroy').datepicker({
            format: 'dd/mm/yyyy',
            endDate: date,
            todayHighlight: true,
            autoclose: true
        })

        $('#divtxtfree').show();


        $('#op_access_department').prop('multiple', false);
        $('#op_access_department').val('').change();

        $('#div_NPI').removeClass('hide');



    }
    else {
        var date = new Date();

        date.setFullYear(new Date().getFullYear() - 18);
        $('.DOB').datepicker('destroy').datepicker({
            format: 'dd/mm/yyyy',
            endDate: date,
            todayHighlight: true,
            autoclose: true
        })

        $('#divtxtfree').hide();

        $('#op_access_department').prop('multiple', true);
        $('#op_access_subdepartment').prop('multiple', true);

        $('#op_access_department').val('').change();
        $('#div_NPI').addClass('hide');


    }


});

//Load Category
function LoadCategory(branch, category_id, subcategory_id) {
    debugger;
    var department = {
        Dml_Indicator: "CT",
        branch_id: $('.Access_branch').val(),
        //  name: $('#DoctorUser_Id').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                console.log(data1)
                $('#op_access_subcategory').empty();
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#op_access_category').html('');
                    $('#op_access_category').val('');
                    var Department;
                    $('#op_access_category').empty();
                    var default_opt = '<option value="0" disabled>--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    $('#op_access_category').append(default_opt + Department);
                    $('#op_access_category').prop('multiple', true);

                    $('#op_access_category').val('').change();
                }
            }
            var ctr = category_id;
            if (ctr != '' && ctr != null) {
                var c_id = '';
                var arr = [];
                if (ctr != null) {

                    c_id = ctr.split(',');

                    for (var i = 0; i < c_id.length; i++) {
                        arr[i] = $.trim(c_id[i]);
                    }
                    $('#op_access_category').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }

        }
    });
}

//Used for Loading the Sub Department DropDown in Doctor Creation and in edit profile
$("#op_access_category").change(function () {
    debugger;
    document.getElementById("op_access_subcategory").innerHTML = null;
    var condition = '(', dep_length = 0;

    dep_length = $('#op_access_category').val().length;
    for (var i = 0; i < dep_length; i++) {
        condition = condition + 'master_category_department_id=' + $('#op_access_category').val()[i]

        if (i != (dep_length - 1)) {
            condition = condition + ' or '
        }
    }



    condition = condition + ') and status=1'
    console.log(condition);
    var subdepartment = {
        Dml_Indicator: "SCTM",
        condition: condition,
        name: $('#DoctorUser_Id').val()

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    $('#op_access_subcategory').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        debugger;
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    $('#op_access_subcategory').html(SubDepartment);
                    $('#op_access_subcategory').prop('multiple', true);
                    $('#op_access_subcategory').val('').change();
                }
            }
            var sctr = $('#Hscate_id').val();
            if (sctr != '' && sctr != null) {
                var sc_id = '';
                var arr = [];
                if (sctr != null) {

                    sc_id = sctr.split(',');

                    for (var i = 0; i < sc_id.length; i++) {
                        arr[i] = $.trim(sc_id[i]);
                    }
                    $('#op_access_subcategory').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
});

//Load User

//function LoadUser() {
//    debugger;
//    var hhh = $('#op_access_subdepartment').val();
//    var department = {
//        Dml_Indicator: "UR",
//        //branch_id: $('#op_access_branch').val(),
//       // subdepartment: $('#op_access_subdepartment').val().toString(),

//    }
//    $.ajax({
//        async: true,
//        type: 'POST',
//        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
//        contentType: 'application/json; charset=UTF-8',
//        data: JSON.stringify(department),
//        success: function (data) {
//            var data1 = JSON.parse(data);
//            if (data1.msg == "Success") {
//                debugger;
//                var data1 = data1.data;
//                console.log(data1)

//                if (jQuery.isEmptyObject((data1))) {
//                }
//                else {
//                    $('#op_access_user').html('');
//                    $('#op_access_user').val('');
//                    var Department;
//                    $('#op_access_user').empty();
//                    var default_opt = '<option value="0" disabled>--Select--</option>';
//                    $.each(data1.Table, function (i, emp) {
//                        Department = Department + '<option value="' + emp.doctor_user_id + '">' + emp.Doctor + '</option>';
//                    });
//                    $('#op_access_user').append(default_opt + Department);
//                    $('#op_access_user').prop('multiple', true);
//                    $('#op_access_user').val('').change();
//                }
//            }
//              var usr = user;      
//            if (usr != '' && usr != null) {
//                var u_id = '';
//                var arr = [];
//                if (usr != null) {

//                    u_id = usr.split(',');

//                    for (var i = 0; i < u_id.length; i++) {
//                        arr[i] = $.trim(u_id[i]);
//                    }
//                    $('#op_access_user').val(arr).change();
//                }
//            }
//            else {
//                console.log(data1);
//            }
//        }
//    });
//}
function LoadUser(user) {
    debugger;
    var department = {
        Dml_Indicator: "UR",
       // Dml_Indicator: "BRUR",
       // branch_id: $('#op_access_branch').val(),         
        mobile_description: $('#op_access_userrole').val().toString()
    }
    //if ($('#hiddenRole_id').val() != 2) {
    //    department.type=$('#hiddenUser_id').val()
    //}
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            debugger
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Department;
                    $('#op_access_user').empty();

                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.doctor_user_id + '" data-id=' + emp.doctor_user_role_id +'>' + emp.Doctor + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#op_access_user').append(Department);
                        $("#op_access_user").change();

                    }
                    else {
                        $('#op_access_user').append(default_opt + Department);

                    }

                    if (user != '' && user != null) {
                        $('#op_access_user').val(user);
                    }

                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

//$('#op_access_subdepartment').change(function () {
//    debugger;
//    var user = $("#Husr_id").val();
//    LoadUser(user);
//})

/// on change for ip_access_branch

$('#ip_access_branch').change(function () {
    debugger;
    var category_id = $('#Hcate_id_ip').val();
    var subcategory_id = $('#Hscate_id_ip').val();
    var department_id = $('#Hdpt_id_ip').val();
    var subdepartment_id = $('#Hsdpt_id_ip').val();
    branch_change_ip($('#ip_access_branch').val(), category_id, subcategory_id, department_id, subdepartment_id);
})


function branch_change_ip(branch, category_id, subcategory_id, department_id, subdepartment_id) {
    debugger;
    //LoadRole();
    LoadDepartment_ip(branch, department_id, subdepartment_id);
    LoadCategory_ip(branch, category_id, subcategory_id);
}

// Load Department ip
function LoadDepartment_ip(branch, department_id, subdepartment_id) {
    debugger;
    var department = {
        Dml_Indicator: "DP",
        branch_id: $('#ip_access_branch').val(),
        name: $('#DoctorUser_Id').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                console.log(data1)
                $('#ip_access_sub_department').empty();
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#ip_access_department').html('');
                    $('#ip_access_department').val('');
                    var Department;
                    $('#ip_access_department').empty();
                    var default_opt = '<option value="0" disabled>--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    $('#ip_access_department').append(default_opt + Department);
                    $('#ip_access_department').prop('multiple', true);
                    $('#ip_access_department').val('').change();
                }
            }
            var dpt = department_id;
            if (dpt != '' && dpt != null) {
                var d_id = '';
                var arr = [];
                if (dpt != null) {

                    d_id = dpt.split(',');

                    for (var i = 0; i < d_id.length; i++) {
                        arr[i] = $.trim(d_id[i]);
                    }
                    $('#ip_access_department').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

//Used for Loading the Sub Department DropDown in Doctor Creation and in edit profile
$("#ip_access_department").change(function () {
    debugger;
    document.getElementById("ip_access_sub_department").innerHTML = null;
    var condition = '(', dep_length = 0;
    dep_length = $('#ip_access_department').val().length;
    for (var i = 0; i < dep_length; i++) {
        condition = condition + 'master_category_department_id=' + $('#ip_access_department').val()[i]

        if (i != (dep_length - 1)) {
            condition = condition + ' or '
        }
    }
    condition = condition + ') and status=1'
    console.log(condition);
    var subdepartment = {
        Dml_Indicator: "SDM",
        condition: condition,
        name: $('#DoctorUser_Id').val()

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    $('#ip_access_sub_department').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        debugger;
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    $('#ip_access_sub_department').html(SubDepartment);
                    $('#ip_access_sub_department').prop('multiple', true);

                    $('#ip_access_sub_department').val('').change();
                }
            }
            var sdpt = $('#Hsdpt_id_ip').val();
            if (sdpt != '' && sdpt != null) {
                var sd_id = '';
                var arr = [];
                if (sdpt != null) {

                    sd_id = sdpt.split(',');

                    for (var i = 0; i < sd_id.length; i++) {
                        arr[i] = $.trim(sd_id[i]);
                    }
                    $('#ip_access_sub_department').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
});

//load category ip

function LoadCategory_ip(branch, category_id, subcategory_id) {
    debugger;
    var department = {
        Dml_Indicator: "CT",
        branch_id: $('#ip_access_branch').val(),
        //  name: $('#DoctorUser_Id').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                console.log(data1)
                $('#ip_access_sub_category').empty();
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#ip_access_category').html('');
                    $('#ip_access_category').val('');
                    var Department;
                    $('#ip_access_category').empty();
                    var default_opt = '<option value="0" disabled>--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    $('#ip_access_category').append(default_opt + Department);
                    $('#ip_access_category').prop('multiple', true);

                    $('#ip_access_category').val('').change();
                }
            }
            var ctr = category_id;
            if (ctr != '' && ctr != null) {
                var c_id = '';
                var arr = [];
                if (ctr != null) {

                    c_id = ctr.split(',');

                    for (var i = 0; i < c_id.length; i++) {
                        arr[i] = $.trim(c_id[i]);
                    }
                    $('#ip_access_category').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

//Used for Loading the Sub Department DropDown in Doctor Creation and in edit profile
$("#ip_access_category").change(function () {
    debugger;
    document.getElementById("ip_access_sub_category").innerHTML = null;
    var condition = '(', dep_length = 0;

    dep_length = $('#ip_access_category').val().length;
    for (var i = 0; i < dep_length; i++) {
        condition = condition + 'master_category_department_id=' + $('#ip_access_category').val()[i]

        if (i != (dep_length - 1)) {
            condition = condition + ' or '
        }
    }



    condition = condition + ') and status=1'
    console.log(condition);
    var subdepartment = {
        Dml_Indicator: "SCTM",
        condition: condition,
        //name: $('#DoctorUser_Id').val()

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    $('#ip_access_sub_category').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        debugger;
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    $('#ip_access_sub_category').html(SubDepartment);
                    $('#ip_access_sub_category').prop('multiple', true);
                    $('#ip_access_sub_category').val('').change();
                }
            }
            var sctr = $('#Hscate_id_ip').val();
            if (sctr != '' && sctr != null) {
                var sc_id = '';
                var arr = [];
                if (sctr != null) {

                    sc_id = sctr.split(',');

                    for (var i = 0; i < sc_id.length; i++) {
                        arr[i] = $.trim(sc_id[i]);
                    }
                    $('#ip_access_sub_category').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
});


//If the role is doctor need to check the license
$('#ip_access_user_role').change(function () {
    debugger;

    if ($('#ip_access_user_role').val() == '1') {

        var date = new Date();

        date.setFullYear(new Date().getFullYear() - 24);
        $('.DOB').datepicker('destroy').datepicker({
            format: 'dd/mm/yyyy',
            endDate: date,
            todayHighlight: true,
            autoclose: true
        })

        $('#divtxtfree').show();


        $('#ip_access_department').prop('multiple', false);
        $('#ip_access_department').val('').change();

        $('#div_NPI').removeClass('hide');



    }
    else {
        var date = new Date();

        date.setFullYear(new Date().getFullYear() - 18);
        $('.DOB').datepicker('destroy').datepicker({
            format: 'dd/mm/yyyy',
            endDate: date,
            todayHighlight: true,
            autoclose: true
        })

        $('#divtxtfree').hide();

        $('#ip_access_department').prop('multiple', true);
        $('#ip_access_sub_department').prop('multiple', true);

        $('#ip_access_department').val('').change();
        $('#div_NPI').addClass('hide');


    }


});

//Load user ip

function LoadUser_ip(user) {
    debugger;
    var hhh = $('#ip_access_sub_department').val();
    var department = {
        Dml_Indicator: "UR",
        // branch_id: $('#ip_access_branch').val(),
        subdepartment: $('#ip_access_sub_department').val().toString(),

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                console.log(data1)

                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#ip_access_user').html('');
                    $('#ip_access_user').val('');
                    var Department;
                    $('#ip_access_user').empty();
                    var default_opt = '<option value="0" disabled>--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.doctor_user_id + '">' + emp.Doctor + '</option>';
                    });
                    $('#ip_access_user').append(default_opt + Department);
                    $('#ip_access_user').prop('multiple', true);
                    $('#ip_access_user').val('').change();
                }
            }
            var usr = $('#Husr_id_ip').val();
            if (usr != '' && usr != null) {
                var u_id = '';
                var arr = [];
                if (usr != null) {

                    u_id = usr.split(',');

                    for (var i = 0; i < u_id.length; i++) {
                        arr[i] = $.trim(u_id[i]);
                    }
                    $('#ip_access_user').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

$('#ip_access_sub_department').change(function () {
    debugger;
    var user = $("#Husr_id_ip").val();
    LoadUser_ip(user);
})


/// on change for patient_access_branch

$('#patient_access_branch').change(function () {
    debugger;
    var category_id = $('#Hcate_id_patient').val();
    var subcategory_id = $('#Hscate_id_patient').val();
    var department_id = $('#Hdpt_id_patient').val();
    var subdepartment_id = $('#Hsdpt_id_patient').val();
    branch_change_patient($('#patient_access_branch').val(), category_id, subcategory_id, department_id, subdepartment_id);
})


function branch_change_patient(branch, category_id, subcategory_id, department_id, subdepartment_id) {
    debugger;
    //  LoadRole();
    LoadDepartment_patient(branch, department_id, subdepartment_id);
    LoadCategory_patient(branch, category_id, subcategory_id);
}

// Load Department patient
function LoadDepartment_patient(branch, department_id, subdepartment_id) {
    debugger;
    var department = {
        Dml_Indicator: "DP",
        branch_id: $('#patient_access_branch').val(),
        name: $('#DoctorUser_Id').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                console.log(data1)
                $('#patient_access_sub_department').empty();
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#patient_access_department').html('');
                    $('#patient_access_department').val('');
                    var Department;
                    $('#patient_access_department').empty();
                    var default_opt = '<option value="0" disabled>--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    $('#patient_access_department').append(default_opt + Department);
                    $('#patient_access_department').prop('multiple', true);
                    $('#patient_access_department').val('').change();
                }
            }
            var dpt = department_id;
            if (dpt != '' && dpt != null) {
                var d_id = '';
                var arr = [];
                if (dpt != null) {

                    d_id = dpt.split(',');

                    for (var i = 0; i < d_id.length; i++) {
                        arr[i] = $.trim(d_id[i]);
                    }
                    $('#patient_access_department').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

//Used for Loading the Sub Department DropDown in Doctor Creation and in edit profile
$("#patient_access_department").change(function () {
    debugger;
    document.getElementById("patient_access_sub_department").innerHTML = null;
    var condition = '(', dep_length = 0;
    dep_length = $('#patient_access_department').val().length;
    for (var i = 0; i < dep_length; i++) {
        condition = condition + 'master_category_department_id=' + $('#patient_access_department').val()[i]

        if (i != (dep_length - 1)) {
            condition = condition + ' or '
        }
    }
    condition = condition + ') and status=1'
    console.log(condition);
    var subdepartment = {
        Dml_Indicator: "SDM",
        condition: condition,
        name: $('#DoctorUser_Id').val()

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        debugger;
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    $('#patient_access_sub_department').html(SubDepartment);
                    $('#patient_access_sub_department').prop('multiple', true);

                    $('#patient_access_sub_department').val('').change();
                }
            }
            var sdpt = $('#Hsdpt_id_patient').val();
            if (sdpt != '' && sdpt != null) {
                var sd_id = '';
                var arr = [];
                if (sdpt != null) {

                    sd_id = sdpt.split(',');

                    for (var i = 0; i < sd_id.length; i++) {
                        arr[i] = $.trim(sd_id[i]);
                    }
                    $('#patient_access_sub_department').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
});

//load category patient

function LoadCategory_patient(branch, category_id, subcategory_id) {
    debugger;
    var department = {
        Dml_Indicator: "CT",
        branch_id: $('#patient_access_branch').val(),
        //  name: $('#DoctorUser_Id').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                console.log(data1)
                $('#patient_access_sub_category').empty();
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#patient_access_category').html('');
                    $('#patient_access_category').val('');
                    var Department;
                    $('#patient_access_category').empty();
                    var default_opt = '<option value="0" disabled>--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    $('#patient_access_category').append(default_opt + Department);
                    $('#patient_access_category').prop('multiple', true);

                    $('#patient_access_category').val('').change();
                }
            }
            var ctr = category_id;
            if (ctr != '' && ctr != null) {
                var c_id = '';
                var arr = [];
                if (ctr != null) {

                    c_id = ctr.split(',');

                    for (var i = 0; i < c_id.length; i++) {
                        arr[i] = $.trim(c_id[i]);
                    }
                    $('#patient_access_category').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

//Used for Loading the Sub Department DropDown in Doctor Creation and in edit profile
$("#patient_access_category").change(function () {
    debugger;
    document.getElementById("patient_access_sub_category").innerHTML = null;
    var condition = '(', dep_length = 0;

    dep_length = $('#patient_access_category').val().length;
    for (var i = 0; i < dep_length; i++) {
        condition = condition + 'master_category_department_id=' + $('#patient_access_category').val()[i]

        if (i != (dep_length - 1)) {
            condition = condition + ' or '
        }
    }



    condition = condition + ') and status=1'
    console.log(condition);
    var subdepartment = {
        Dml_Indicator: "SCTM",
        condition: condition,
        //name: $('#DoctorUser_Id').val()

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    $('#patient_access_sub_category').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        debugger;
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    $('#patient_access_sub_category').html(SubDepartment);
                    $('#patient_access_sub_category').prop('multiple', true);
                    $('#patient_access_sub_category').val('').change();
                }
            }
            var sctr = $('#Hscate_id_patient').val();
            if (sctr != '' && sctr != null) {
                var sc_id = '';
                var arr = [];
                if (sctr != null) {

                    sc_id = sctr.split(',');

                    for (var i = 0; i < sc_id.length; i++) {
                        arr[i] = $.trim(sc_id[i]);
                    }
                    $('#patient_access_sub_category').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
});


//If the role is doctor need to check the license
$('#patient_access_user_role').change(function () {
    debugger;

    if ($('#patient_access_user_role').val() == '1') {

        var date = new Date();

        date.setFullYear(new Date().getFullYear() - 24);
        $('.DOB').datepicker('destroy').datepicker({
            format: 'dd/mm/yyyy',
            endDate: date,
            todayHighlight: true,
            autoclose: true
        })

        $('#divtxtfree').show();


        $('#patient_access_department').prop('multiple', false);
        $('#patient_access_department').val('').change();

        $('#div_NPI').removeClass('hide');



    }
    else {
        var date = new Date();

        date.setFullYear(new Date().getFullYear() - 18);
        $('.DOB').datepicker('destroy').datepicker({
            format: 'dd/mm/yyyy',
            endDate: date,
            todayHighlight: true,
            autoclose: true
        })

        $('#divtxtfree').hide();

        $('#patient_access_department').prop('multiple', true);
        $('#patient_access_sub_department').prop('multiple', true);

        $('#patient_access_department').val('').change();
        $('#div_NPI').addClass('hide');


    }


});

//Load user patient

function LoadUser_patient(user) {
    debugger;
    var hhh = $('#patient_access_sub_department').val();
    var department = {
        Dml_Indicator: "UR",
        branch_id: $('#patient_access_branch').val(),
        subdepartment: $('#patient_access_sub_department').val().toString(),

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                console.log(data1)

                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#patient_access_user').html('');
                    $('#patient_access_user').val('');
                    var Department;
                    $('#patient_access_user').empty();
                    var default_opt = '<option value="0" disabled>--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.doctor_user_id + '">' + emp.Doctor + '</option>';
                    });
                    $('#patient_access_user').append(default_opt + Department);
                    $('#patient_access_user').prop('multiple', true);
                    $('#patient_access_user').val('').change();
                }
            }
            var usr = $('#Husr_id_patient').val();
            if (usr != '' && usr != null) {
                var u_id = '';
                var arr = [];
                if (usr != null) {

                    u_id = usr.split(',');

                    for (var i = 0; i < u_id.length; i++) {
                        arr[i] = $.trim(u_id[i]);
                    }
                    $('#patient_access_user').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

$('#patient_access_sub_department').change(function () {
    debugger;
    var user = $("#Husr_id_patient").val();
    LoadUser_patient(user);
})

// on change for survey_access_branch
$('#survey_access_branch').change(function () {
    debugger;
    var category_id = $('#Hcate_id_survey').val();
    var subcategory_id = $('#Hscate_id_survey').val();
    var department_id = $('#Hdpt_id_survey').val();
    var subdepartment_id = $('#Hsdpt_id_survey').val();
    branch_change_survey($('#survey_access_branch').val(), category_id, subcategory_id, department_id, subdepartment_id);
})


function branch_change_survey(branch, category_id, subcategory_id, department_id, subdepartment_id) {
    debugger;
    // LoadRole();
    LoadDepartment_survey(branch, department_id, subdepartment_id);
    LoadCategory_survey(branch, category_id, subcategory_id);
}

// Load Department survey
function LoadDepartment_survey(branch, department_id, subdepartment_id) {
    debugger;
    var department = {
        Dml_Indicator: "DP",
        branch_id: $('#survey_access_branch').val(),
        name: $('#DoctorUser_Id').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                console.log(data1)
                $('#survey_access_sub_department').empty();
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#survey_access_department').html('');
                    $('#survey_access_department').val('');
                    var Department;
                    var default_opt = '<option value="0" disabled>--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    $('#survey_access_department').append(default_opt + Department);
                    $('#survey_access_department').prop('multiple', true);
                    $('#survey_access_department').val('').change();
                }
            }
            var dpt = department_id;
            if (dpt != '' && dpt != null) {
                var d_id = '';
                var arr = [];
                if (dpt != null) {

                    d_id = dpt.split(',');

                    for (var i = 0; i < d_id.length; i++) {
                        arr[i] = $.trim(d_id[i]);
                    }
                    $('#survey_access_department').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

//Used for Loading the Sub Department DropDown in Doctor Creation and in edit profile
$("#survey_access_department").change(function () {
    debugger;
    //   document.getElementById("survey_access_sub_department").innerHTML = null;
    var condition = '(', dep_length = 0;
    dep_length = $('#survey_access_department').val().length;
    for (var i = 0; i < dep_length; i++) {
        condition = condition + 'master_category_department_id=' + $('#survey_access_department').val()[i]

        if (i != (dep_length - 1)) {
            condition = condition + ' or '
        }
    }
    condition = condition + ') and status=1'
    console.log(condition);
    var subdepartment = {
        Dml_Indicator: "SDM",
        condition: condition,
        name: $('#DoctorUser_Id').val()

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        debugger;
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    $('#survey_access_sub_department').html(SubDepartment);
                    $('#survey_access_sub_department').prop('multiple', true);

                    $('#survey_access_sub_department').val('').change();
                }
            }
            var sdpt = $('#Hsdpt_id_survey').val();
            if (sdpt != '' && sdpt != null) {
                var sd_id = '';
                var arr = [];
                if (sdpt != null) {

                    sd_id = sdpt.split(',');

                    for (var i = 0; i < sd_id.length; i++) {
                        arr[i] = $.trim(sd_id[i]);
                    }
                    $('#survey_access_sub_department').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
});

//load category survey

function LoadCategory_survey(branch, category_id, subcategory_id) {
    debugger;
    var department = {
        Dml_Indicator: "CT",
        branch_id: $('#survey_access_branch').val(),
        //  name: $('#DoctorUser_Id').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                console.log(data1)
                $('#survey_access_sub_category').empty();
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#survey_access_category').html('');
                    $('#survey_access_category').val('');
                    var Department;
                    var default_opt = '<option value="0" disabled>--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    $('#survey_access_category').append(default_opt + Department);
                    $('#survey_access_category').prop('multiple', true);

                    $('#survey_access_category').val('').change();
                }
            }
            var ctr = category_id;
            if (ctr != '' && ctr != null) {
                var c_id = '';
                var arr = [];
                if (ctr != null) {

                    c_id = ctr.split(',');

                    for (var i = 0; i < c_id.length; i++) {
                        arr[i] = $.trim(c_id[i]);
                    }
                    $('#survey_access_category').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

//Used for Loading the Sub Department DropDown in Doctor Creation and in edit profile
$("#survey_access_category").change(function () {
    debugger;
    //  document.getElementById("survey_access_sub_category").innerHTML = null;
    var condition = '(', dep_length = 0;

    dep_length = $('#survey_access_category').val().length;
    for (var i = 0; i < dep_length; i++) {
        condition = condition + 'master_category_department_id=' + $('#survey_access_category').val()[i]

        if (i != (dep_length - 1)) {
            condition = condition + ' or '
        }
    }



    condition = condition + ') and status=1'
    console.log(condition);
    var subdepartment = {
        Dml_Indicator: "SCTM",
        condition: condition,
        //name: $('#DoctorUser_Id').val()

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    $('#survey_access_sub_category').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        debugger;
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    $('#survey_access_sub_category').html(SubDepartment);
                    $('#survey_access_sub_category').prop('multiple', true);
                    $('#survey_access_sub_category').val('').change();
                }
            }
            var sctr = $('#Hscate_id_survey').val();
            if (sctr != '' && sctr != null) {
                var sc_id = '';
                var arr = [];
                if (sctr != null) {

                    sc_id = sctr.split(',');

                    for (var i = 0; i < sc_id.length; i++) {
                        arr[i] = $.trim(sc_id[i]);
                    }
                    $('#survey_access_sub_category').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
});


//If the role is doctor need to check the license
$('#survey_access_user_role').change(function () {
    debugger;

    if ($('#survey_access_user_role').val() == '1') {

        var date = new Date();

        date.setFullYear(new Date().getFullYear() - 24);
        $('.DOB').datepicker('destroy').datepicker({
            format: 'dd/mm/yyyy',
            endDate: date,
            todayHighlight: true,
            autoclose: true
        })

        $('#divtxtfree').show();


        $('#survey_access_department').prop('multiple', false);
        $('#survey_access_department').val('').change();

        $('#div_NPI').removeClass('hide');



    }
    else {
        var date = new Date();

        date.setFullYear(new Date().getFullYear() - 18);
        $('.DOB').datepicker('destroy').datepicker({
            format: 'dd/mm/yyyy',
            endDate: date,
            todayHighlight: true,
            autoclose: true
        })

        $('#divtxtfree').hide();

        $('#survey_access_department').prop('multiple', true);
        $('#survey_access_department').prop('multiple', true);

        $('#survey_access_department').val('').change();
        $('#div_NPI').addClass('hide');


    }


});

//Load user survey

function LoadUser_survey(user) {
    debugger;
    var hhh = $('#survey_access_sub_department').val();
    var department = {
        Dml_Indicator: "UR",
        branch_id: $('#survey_access_branch').val(),
        subdepartment: $('#survey_access_sub_department').val().toString(),

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                console.log(data1)

                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#survey_access_user').html('');
                    $('#survey_access_user').val('');
                    var Department;
                    $('#survey_access_user').empty();
                    var default_opt = '<option value="0" disabled>--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.doctor_user_id + '">' + emp.Doctor + '</option>';
                    });
                    $('#survey_access_user').append(default_opt + Department);
                    $('#survey_access_user').prop('multiple', true);
                    $('#survey_access_user').val('').change();
                }
            }
            var usr = $('#Husr_id_survey').val();
            if (usr != '' && usr != null) {
                var u_id = '';
                var arr = [];
                if (usr != null) {

                    u_id = usr.split(',');

                    for (var i = 0; i < u_id.length; i++) {
                        arr[i] = $.trim(u_id[i]);
                    }
                    $('#survey_access_user').val(arr).change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

$('#survey_access_sub_department').change(function () {
    debugger;
    var user = $("#Husr_id_survey").val();
    LoadUser_survey(user);
})


// on change for op_map_branch

$('#op_map_branch').change(function () {
    debugger;
    var department_id = $('#Hop_Hdepartmentid').val();

    branch_change_map($('#op_map_branch').val(), department_id);
})
function branch_change_map(branch, department_id) {
    debugger;
    LoadDepartment_map(branch, department_id);
}

function LoadDepartment_map(branch, department_id) {
    debugger;
    var department = {
        Dml_Indicator: "DP",
        branch_id: branch
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            debugger
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Department;
                    $('#op_map_department').empty();

                    var default_opt = '<option value="">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#op_map_department').append(Department);
                        $("#op_map_department").change();

                    }
                    else {
                        $('#op_map_department').append(default_opt + Department);

                    }

                    $('#op_map_department').val(department_id)

                }
            }

            else {
                console.log(data1);
            }
        }
    });
}
$("#op_map_department").change(function () {
    debugger;
    var subdepartment_id = $('#Hop_Hsdepartmentid').val();
    LoadSubDepartment_map($('#op_map_department').val(), 0, subdepartment_id);
    $('#op_map_subdepartment').prop('disabled', false);
})

function LoadSubDepartment_map(id, val, subdepartment_id) {
    debugger;
    var val = val;
    document.getElementById("op_map_subdepartment").innerHTML = null;
    var subdepartment = {
        Dml_Indicator: "SDP",
        Master_cd_id: id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var SubDepartment = '';
                    $('#op_map_subdepartment').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#op_map_subdepartment').append(SubDepartment);
                        $("#op_map_subdepartment").change();
                    }
                    else {
                        $('#op_map_subdepartment').append(default_opt + SubDepartment);
                    }

                    if (val != '' && val != 0) {
                        $('#op_map_subdepartment').val(val);
                    }

                    $('#op_map_subdepartment').val(subdepartment_id);
                }

            }

            else {
                console.log(data1);
            }
        }
    });
}

//loadpage_op_map
function Loadpagename_op() {
    debugger;
    var department = {
        Dml_Indicator: "PN",
        // branch_id: branch
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            debugger
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Department;
                    $('.Map_pagename').empty();

                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.form_id + '">' + emp.form_name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('.Map_pagename').append(Department);
                        $(".Map_pagename").change();

                    }
                    else {
                        $('.Map_pagename').append(default_opt + Department);

                    }

                }
            }
            else {
                console.log(data1);
            }
        }
    });
}


///// on change for ip_map_branch

$('#ip_map_branch').change(function () {
    debugger;
    var department_id = $('#Hip_Hdepartmentid').val();

    branch_change_map_ip($('#ip_map_branch').val(), department_id);
})
function branch_change_map_ip(branch, department_id) {
    debugger;
    LoadDepartment_map_ip(branch, department_id);
}

function LoadDepartment_map_ip(branch, department_id) {
    debugger;
    var department = {
        Dml_Indicator: "DP",
        branch_id: branch
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            debugger
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Department;
                    $('#ip_map_department').empty();

                    var default_opt = '<option value="">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#ip_map_department').append(Department);
                        $("#ip_map_department").change();

                    }
                    else {
                        $('#ip_map_department').append(default_opt + Department);

                    }

                    $('#ip_map_department').val(department_id)

                }
            }

            else {
                console.log(data1);
            }
        }
    });
}
$("#ip_map_department").change(function () {
    debugger;
    var subdepartment_id = $('#Hip_Hsdepartmentid').val();
    LoadSubDepartment_map_ip($('#ip_map_department').val(), 0, subdepartment_id);
    $('#ip_map_subdepartment').prop('disabled', false);
})

function LoadSubDepartment_map_ip(id, val, subdepartment_id) {
    debugger;
    var val = val;
    document.getElementById("ip_map_subdepartment").innerHTML = null;
    var subdepartment = {
        Dml_Indicator: "SDP",
        Master_cd_id: id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var SubDepartment = '';
                    $('#ip_map_subdepartment').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#ip_map_subdepartment').append(SubDepartment);
                        $("#ip_map_subdepartment").change();
                    }
                    else {
                        $('#ip_map_subdepartment').append(default_opt + SubDepartment);
                    }

                    if (val != '' && val != 0) {
                        $('#ip_map_subdepartment').val(val);
                    }

                    $('#ip_map_subdepartment').val(subdepartment_id);
                }

            }

            else {
                console.log(data1);
            }
        }
    });
}

////on change for patient_map_branch
$('#patient_map_branch').change(function () {
    debugger;
    var department_id = $('#Hpat_Hdepartmentid').val();

    branch_change_map_patient($('#patient_map_branch').val(), department_id);
})
function branch_change_map_patient(branch, department_id) {
    debugger;
    LoadDepartment_map_patient(branch, department_id);
}

function LoadDepartment_map_patient(branch, department_id) {
    debugger;
    var department = {
        Dml_Indicator: "DP",
        branch_id: branch
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            debugger
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Department;
                    $('#patient_map_department').empty();

                    var default_opt = '<option value="">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#patient_map_department').append(Department);
                        $("#patient_map_department").change();

                    }
                    else {
                        $('#patient_map_department').append(default_opt + Department);

                    }

                    $('#patient_map_department').val(department_id)

                }
            }

            else {
                console.log(data1);
            }
        }
    });
}
$("#patient_map_department").change(function () {
    debugger;
    var subdepartment_id = $('#Hpat_Hsdepartmentid').val();
    LoadSubDepartment_map_patient($('#patient_map_department').val(), 0, subdepartment_id);
    $('#patient_map_subdepartment').prop('disabled', false);
})

function LoadSubDepartment_map_patient(id, val, subdepartment_id) {
    debugger;
    var val = val;
    document.getElementById("patient_map_subdepartment").innerHTML = null;
    var subdepartment = {
        Dml_Indicator: "SDP",
        Master_cd_id: id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var SubDepartment = '';
                    $('#patient_map_subdepartment').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#patient_map_subdepartment').append(SubDepartment);
                        $("#patient_map_subdepartment").change();
                    }
                    else {
                        $('#patient_map_subdepartment').append(default_opt + SubDepartment);
                    }

                    if (val != '' && val != 0) {
                        $('#patient_map_subdepartment').val(val);
                    }

                    $('#patient_map_subdepartment').val(subdepartment_id);
                }

            }

            else {
                console.log(data1);
            }
        }
    });
}

///on change for survey_map_branch

$('#survey_map_branch').change(function () {
    debugger;
    var department_id = $('#Hsur_Hdepartmentid').val();

    branch_change_map_survey($('#survey_map_branch').val(), department_id);
})
function branch_change_map_survey(branch, department_id) {
    debugger;
    LoadDepartment_map_survey(branch, department_id);
}

function LoadDepartment_map_survey(branch, department_id) {
    debugger;
    var department = {
        Dml_Indicator: "DP",
        branch_id: branch
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            debugger
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Department;
                    $('#survey_map_department').empty();

                    var default_opt = '<option value="">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#survey_map_department').append(Department);
                        $("#survey_map_department").change();

                    }
                    else {
                        $('#survey_map_department').append(default_opt + Department);

                    }

                    $('#survey_map_department').val(department_id)

                }
            }

            else {
                console.log(data1);
            }
        }
    });
}
$("#survey_map_department").change(function () {
    debugger;
    var subdepartment_id = $('#Hsur_Hsdepartmentid').val();
    LoadSubDepartment_map_survey($('#survey_map_department').val(), 0, subdepartment_id);
    $('#survey_map_subdepartment').prop('disabled', false);
})

function LoadSubDepartment_map_survey(id, val, subdepartment_id) {
    debugger;
    var val = val;
    // document.getElementById("survey_map_subdeparment").innerHTML = null;
    var subdepartment = {
        Dml_Indicator: "SDP",
        Master_cd_id: id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var SubDepartment = '';
                    $('#survey_map_subdepartment').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#survey_map_subdepartment').append(SubDepartment);
                        $("#survey_map_subdepartment").change();
                    }
                    else {
                        $('#survey_map_subdepartment').append(default_opt + SubDepartment);
                    }

                    if (val != '' && val != 0) {
                        $('#survey_map_subdepartment').val(val);
                    }

                    $('#survey_map_subdepartment').val(subdepartment_id);
                }

            }

            else {
                console.log(data1);
            }
        }
    });
}



//add btn map_op

$('#btn_add_map_op').click(function () {
    debugger;

    var Formid = $('#Hformid').val();
    var frm_date = $('#op_map_fromdate').val();
    var to_date = $('#op_map_todate').val();

    var date_split_fd = frm_date.split('/');
    var f_date = date_split_fd[2] + '-' + date_split_fd[1] + '-' + date_split_fd[0];

    var date_split_td = to_date.split('/');
    var t_date = date_split_td[2] + '-' + date_split_td[1] + '-' + date_split_td[0];
    var Submit = ''
    if ($('#autodevice_Id').val() == '') {
        Submit = {
            Branch_id: $('#op_map_branch').val(),
            Department_id: $('#op_map_department').val(),
            Subdepartment_id: $('#op_map_subdepartment').val(),
            pageid: $('#op_map_pagename').val(),
            Activefrom: f_date,
            ActiveTo: t_date,
            Formid: Formid,
            Dml_Indicator: 'MI'
        }
    }
    else {
        Submit = {

            Branch_id: $('#op_map_branch').val(),
            Department_id: $('#op_map_department').val(),
            Subdepartment_id: $('#op_map_subdepartment').val(),
            pageid: $('#op_map_pagename').val(),
            Activefrom: f_date,
            ActiveTo: t_date,
            Id: $('#autodevice_Id').val(),
            Formid: Formid,
            Dml_Indicator: 'MU'

        }
    }


    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMapping",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Submit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var data2 = data1.data.Table;

            if (data1.msg == 'Success') {
                if (data2[0].msg == 'inserted') {
                    toastr.success("Added Successfully");
                    cleardata_op_map()
                    loaddata_mapping_op();
                }
                else if (data2[0].msg == 'already') {
                    toastr.warning("Device Model Name already exists");
                    //  $("#Txtname").val("");
                    //  return false;
                }
                else if (data2[0].msg == 'updated') {
                    toastr.success("Updated Successfully");
                    $("#btn_add_map_op").html("Add");
                    cleardata_op_map()
                    // location.reload();
                    loaddata_mapping_op();
                }
                else {
                    console.log(data1)
                }
            }
            else {
                console.log(data1)
                loaddata_mapping_op();
            }
        }

    })
});

function cleardata_op_map() {
    $('#op_map_branch').val("");
    $('#op_map_department').val("");
    $('#op_map_subdepartment').val("");
    $('#op_map_pagename').val("");
    $('#op_map_fromdate').val("");
    $('#op_map_todate').val("");
    $('#autodevice_Id').val("");
    $('#Hop_Hdepartmentid').val("");
    $('#Hop_Hsdepartmentid').val("");
    $("#btn_add_map_op").html("Add");
    LoadBranch();
    Loadpagename_op();


}

function FormBuilder_map_op(event, Id) {
    $('#autodevice_Id').val(Id)

    debugger;

    var Edit = {

        Id: Id,

        Dml_Indicator: 'ED'
    };
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMapping",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Edit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var emp = data1.data.Table;
            $("#op_map_branch").val(emp[0].Branchid);
            $("#op_map_department").val(emp[0].Departmentid);
            $('#Hop_Hdepartmentid').val(emp[0].Departmentid)
            $("#op_map_subdepartment").val(emp[0].Subdepartmentid);
            $("#Hop_Hsdepartmentid").val(emp[0].Subdepartmentid);
            $("#op_map_branch").change();
            $("#op_map_department").change();
            $("#op_map_subdepartment").change();
            $("#op_map_pagename").val(emp[0].pageid);
            $("#op_map_fromdate").val(emp[0].Activefrom);
            $("#op_map_todate").val(emp[0].ActiveTo);
            $("#btn_add_map_op").html("Update");
        }

    })
    $("#op_map_branch").focus();
}


//add btn map_ip

$('#btn_add_map_ip').click(function () {
    debugger;

    var Formid = $('#Hformid').val();
    var frm_date = $('#ip_map_fromdate').val();
    var to_date = $('#ip_map_todate').val();

    var date_split_fd = frm_date.split('/');
    var f_date = date_split_fd[2] + '-' + date_split_fd[1] + '-' + date_split_fd[0];

    var date_split_td = to_date.split('/');
    var t_date = date_split_td[2] + '-' + date_split_td[1] + '-' + date_split_td[0];
    var Submit = ''
    if ($('#autodevice_Id').val() == '') {
        Submit = {
            Branch_id: $('#ip_map_branch').val(),
            Department_id: $('#ip_map_department').val(),
            Subdepartment_id: $('#ip_map_subdepartment').val(),
            pageid: $('#ip_map_pagename').val(),
            Activefrom: f_date,
            ActiveTo: t_date,
            Formid: Formid,
            Dml_Indicator: 'MI'
        }
    }
    else {
        Submit = {

            Branch_id: $('#ip_map_branch').val(),
            Department_id: $('#ip_map_department').val(),
            Subdepartment_id: $('#ip_map_subdepartment').val(),
            pageid: $('#ip_map_pagename').val(),
            Activefrom: f_date,
            ActiveTo: t_date,
            Formid: Formid,
            Id: $('#autodevice_Id').val(),
            Dml_Indicator: 'MU'

        }
    }


    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMapping",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Submit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var data2 = data1.data.Table;

            if (data1.msg == 'Success') {
                if (data2[0].msg == 'inserted') {
                    toastr.success("Added Successfully");
                    cleardata_ip_map()
                    loaddata_mapping_ip();
                }
                else if (data2[0].msg == 'already') {
                    toastr.warning("Device Model Name already exists");
                    //  $("#Txtname").val("");
                    //  return false;
                }
                else if (data2[0].msg == 'updated') {
                    toastr.success("Updated Successfully");
                    $("#btn_add_map_ip").html("Add");
                    cleardata_ip_map()
                    // location.reload();
                    loaddata_mapping_ip();
                }
                else {
                    console.log(data1)
                }
            }
            else {
                console.log(data1)
                loaddata_mapping_ip();
            }
        }

    })
});

function cleardata_ip_map() {
    $('#ip_map_branch').val("");
    $('#ip_map_department').val("");
    $('#ip_map_subdepartment').val("");
    $('#ip_map_pagename').val("");
    $('#ip_map_fromdate').val("");
    $('#ip_map_todate').val("");
    $('#autodevice_Id').val("");
    $('#Hip_Hdepartmentid').val("");
    $('#Hip_Hsdepartmentid').val("");
    $("#btn_add_map_ip").html("Add");[]
    LoadBranch();
    Loadpagename_op();
}

function FormBuilder_map_ip(event, Id) {
    $('#autodevice_Id').val(Id)

    debugger;

    var Edit = {

        Id: Id,

        Dml_Indicator: 'ED'
    };
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMapping",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Edit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var emp = data1.data.Table;
            $("#ip_map_branch").val(emp[0].Branchid);
            $("#ip_map_department").val(emp[0].Departmentid);
            $('#Hip_Hdepartmentid').val(emp[0].Departmentid)
            $("#ip_map_subdepartment").val(emp[0].Subdepartmentid);
            $("#Hip_Hsdepartmentid").val(emp[0].Subdepartmentid);
            $("#ip_map_branch").change();
            $("#ip_map_department").change();
            $("#ip_map_subdepartment").change();
            $("#ip_map_pagename").val(emp[0].pageid);
            $("#ip_map_fromdate").val(emp[0].Activefrom);
            $("#ip_map_todate").val(emp[0].ActiveTo);
            $("#btn_add_map_ip").html("Update");
        }

    })
    $("#ip_map_branch").focus();
}

//add btn map_patient

$('#btn_add_map_patient').click(function () {
    debugger;

    var Formid = $('#Hformid').val();
    var frm_date = $('#patient_map_fromdate').val();
    var to_date = $('#patient_map_todate').val();

    var date_split_fd = frm_date.split('/');
    var f_date = date_split_fd[2] + '-' + date_split_fd[1] + '-' + date_split_fd[0];

    var date_split_td = to_date.split('/');
    var t_date = date_split_td[2] + '-' + date_split_td[1] + '-' + date_split_td[0];
    var Submit = ''
    if ($('#autodevice_Id').val() == '') {
        Submit = {
            Branch_id: $('#patient_map_branch').val(),
            Department_id: $('#patient_map_department').val(),
            Subdepartment_id: $('#patient_map_subdepartment').val(),
            pageid: $('#patient_map_pagename').val(),
            Activefrom: f_date,
            ActiveTo: t_date,
            Formid: Formid,
            Dml_Indicator: 'MI'
        }
    }
    else {
        Submit = {

            Branch_id: $('#patient_map_branch').val(),
            Department_id: $('#patient_map_department').val(),
            Subdepartment_id: $('#patient_map_subdepartment').val(),
            pageid: $('#patient_map_pagename').val(),
            Activefrom: f_date,
            ActiveTo: t_date,
            Formid: Formid,
            Id: $('#autodevice_Id').val(),
            Dml_Indicator: 'MU'

        }
    }


    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMapping",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Submit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var data2 = data1.data.Table;

            if (data1.msg == 'Success') {
                if (data2[0].msg == 'inserted') {
                    toastr.success("Added Successfully");
                    cleardata_patient_map()
                    loaddata_mapping_patient();
                }
                else if (data2[0].msg == 'already') {
                    toastr.warning("Device Model Name already exists");
                    //  $("#Txtname").val("");
                    //  return false;
                }
                else if (data2[0].msg == 'updated') {
                    toastr.success("Updated Successfully");
                    $("#btn_add_map_patient").html("Add");
                    cleardata_patient_map()
                    // location.reload();
                    loaddata_mapping_patient();
                }
                else {
                    console.log(data1)
                }
            }
            else {
                console.log(data1)
                loaddata_mapping_patient();
            }
        }

    })
});

function cleardata_patient_map() {
    $('#patient_map_branch').val("");
    $('#patient_map_department').val("");
    $('#patient_map_subdepartment').val("");
    $('#patient_map_pagename').val("");
    $('#patient_map_fromdate').val("");
    $('#patient_map_todate').val("");
    $('#Hpat_Hdepartmentid').val("");
    $('#Hpat_Hsdepartmentid').val("");
    $('#autodevice_Id').val("");
    //$('#Hformid').val("");
    $("#btn_add_map_patient").html("Add");
    LoadBranch();
    Loadpagename_op();
}

function FormBuilder_map_patient(event, Id) {
    $('#autodevice_Id').val(Id)

    debugger;

    var Edit = {

        Id: Id,

        Dml_Indicator: 'ED'
    };
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMapping",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Edit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var emp = data1.data.Table;
            $("#patient_map_branch").val(emp[0].Branchid);
            $("#patient_map_department").val(emp[0].Departmentid);
            $('#Hpat_Hdepartmentid').val(emp[0].Departmentid)
            $("#patient_map_subdepartment").val(emp[0].Subdepartmentid);
            $("#Hpat_Hsdepartmentid").val(emp[0].Subdepartmentid);
            $("#patient_map_branch").change();
            $("#patient_map_department").change();
            $("#patient_map_subdepartment").change();
            $("#patient_map_pagename").val(emp[0].pageid);
            $("#patient_map_fromdate").val(emp[0].Activefrom);
            $("#patient_map_todate").val(emp[0].ActiveTo);
            $("#btn_add_map_patient").html("Update");
        }

    })
    $("#patient_map_branch").focus();
}

//add btn map_survey

$('#btn_add_map_survey').click(function () {
    debugger;

    var Formid = $('#Hformid').val();
    var frm_date = $('#survey_map_fromdate').val();
    var to_date = $('#survey_map_todate').val();

    var date_split_fd = frm_date.split('/');
    var f_date = date_split_fd[2] + '-' + date_split_fd[1] + '-' + date_split_fd[0];

    var date_split_td = to_date.split('/');
    var t_date = date_split_td[2] + '-' + date_split_td[1] + '-' + date_split_td[0];
    var Submit = ''
    if ($('#autodevice_Id').val() == '') {
        Submit = {
            Branch_id: $('#survey_map_branch').val(),
            Department_id: $('#survey_map_department').val(),
            Subdepartment_id: $('#survey_map_subdepartment').val(),
            pageid: $('#survey_map_pagename').val(),
            Activefrom: f_date,
            ActiveTo: t_date,
            Formid: Formid,
            Dml_Indicator: 'MI'
        }
    }
    else {
        Submit = {

            Branch_id: $('#survey_map_branch').val(),
            Department_id: $('#survey_map_department').val(),
            Subdepartment_id: $('#survey_map_subdepartment').val(),
            pageid: $('#survey_map_pagename').val(),
            Activefrom: f_date,
            ActiveTo: t_date,
            Formid: Formid,
            Id: $('#autodevice_Id').val(),
            Dml_Indicator: 'MU'

        }
    }


    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMapping",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Submit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var data2 = data1.data.Table;

            if (data1.msg == 'Success') {
                if (data2[0].msg == 'inserted') {
                    toastr.success("Added Successfully");
                    cleardata_survey_map()
                    loaddata_mapping_survey();
                }
                else if (data2[0].msg == 'already') {
                    toastr.warning("Device Model Name already exists");
                    //  $("#Txtname").val("");
                    //  return false;
                }
                else if (data2[0].msg == 'updated') {
                    toastr.success("Updated Successfully");
                    $("#btn_add_map_survey").html("Add");
                    cleardata_survey_map()
                    // location.reload();
                    loaddata_mapping_survey();
                }
                else {
                    console.log(data1)
                }
            }
            else {
                console.log(data1)
                loaddata_mapping_survey();
            }
        }

    })
});

function cleardata_survey_map() {
    $('#survey_map_branch').val("");
    $('#survey_map_department').val("");
    $('#survey_map_subdepartment').val("");
    $('#survey_map_pagename').val("");
    $('#survey_map_fromdate').val("");
    $('#survey_map_todate').val("");
    $('#Hsur_Hdepartmentid').val("");
    $('#Hsur_Hsdepartmentid').val("");
    $('#autodevice_Id').val("");
    //$('#Hformid').val("");
    $("#btn_add_map_survey").html("Add");
    LoadBranch();
    Loadpagename_op();
}

function FormBuilder_map_survey(event, Id) {
    $('#autodevice_Id').val(Id)

    debugger;

    var Edit = {

        Id: Id,

        Dml_Indicator: 'ED'
    };
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormMapping",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Edit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var emp = data1.data.Table;
            $("#survey_map_branch").val(emp[0].Branchid);
            $("#survey_map_department").val(emp[0].Departmentid);
            $('#Hsur_Hdepartmentid').val(emp[0].Departmentid)
            $("#survey_map_subdepartment").val(emp[0].Subdepartmentid);
            $("#Hsur_Hsdepartmentid").val(emp[0].Subdepartmentid);
            $("#survey_map_branch").change();
            $("#survey_map_department").change();
            $("#survey_map_subdepartment").change();
            $("#survey_map_pagename").val(emp[0].pageid);
            $("#survey_map_fromdate").val(emp[0].Activefrom);
            $("#survey_map_todate").val(emp[0].ActiveTo);
            $("#btn_add_map_survey").html("Update");
        }

    })
    $("#survey_map_branch").focus();
}
// load view old version op
function view_old_version_op(event, Form_Name,form_id) {
    debugger;
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         debugger;
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 debugger;
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {

                     return;
                 }
                 else {
                     $('#view_old_version_op').modal('toggle');

                     loaddata_version_op(form_id);
                     Form_Name: $('#formname_op_vov').html(Form_Name)
                 }
             }
         }
     })
}
///loaddata_view_old_version _op
function loaddata_version_op(form_id) {

    debugger;
    var load = {
        Dml_Indicator: "VOV",
        Form_id: form_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormversion",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data2 = JSON.parse(data);
            console.log(data2);
            if (data2.msg == "Success") {
                debugger;
                var data1 = data2.data;
                $('#last_update_op_vov').html('');
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_version_op').empty();
                    $('#view_old_version_table_op').dataTable().fnClearTable();
                    $('#view_old_version_table_op').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var no_version = data1.Table2[0].No_of_version;
                    $('#no_of_version_op_vov').html(no_version)
                    var last_version = data1.Table3[0].Last_Version;
                    $('#last_update_op_vov').html(last_version)
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var status = '';

                        status = '<span class="label label-success">Active</span>';
                        Print = '<a data-toggle="tooltip" data-placement="bottom" title="Print" type="button" class="btn btn-edit btn-xs btn_r" data-id="" data-type="role_rights_edit" data-nav="redirect" data-name=""><i class="fa fa-print" aria-hidden="true"></i></a>';
                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="FB_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=Deviceversion(this,"' + emp.Version_Id + '","DL")><i class="fa fa-trash" aria-hidden="true"></i></a>';
                       
                        Restore = '<p data-toggle="modal" data-target="#view_restore_op" aria-hidden="true"><a class="btn-blue" onclick=\'Restore_op_vov(this,"' + emp.Version_Id + '","RS","' + emp.Form_id + '")\'>Restore</a></p>'

                       // Restore_As_New_Form = '<p data-toggle="modal" data-target="#view_restore_new_op" aria-hidden="true"><a class="btn-blue">Restore As New Form</a></p>'

                      //  Send = '<p data-toggle="modal" data-target="#view_send_op" aria-hidden="true"><a class="btn-blue">Send</a></p>'

                        if (emp.status == '1') {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Deviceversion(this,"' + emp.Version_Id + '","IA")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + Restore + '</div><div class="text-left">' + Restore_As_New_Form + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';

                        }
                        else if (emp.status == '2') {
                            status = '<span class="label label-warning">In Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make Active!" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Deviceversion(this,"' + emp.Version_Id + '","AC")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if"> ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + Restore + '</div></div></div></div>';

                           // other_info = '<div class="col-md-12 padding0"><div class="disp_if"> ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + Restore + '</div><div class="text-left">' + Restore_As_New_Form + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';
                        }
                        else {
                            status = '<span class="label label-warning">In Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs btn-blue mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Deviceversion(this,"' + emp.Version_Id + '","IA")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + Restore + '</div></div></div></div>';

                           // other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + Print + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + Restore + '</div><div class="text-left">' + Restore_As_New_Form + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';

                        }


                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Form_Name + '</td><td>' + emp.Version_No + '</td><td>' + status + '</td><td>' + emp.Updatedby + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_version_op').html(tr);

                    //datatable('view_old_version_table_op', 'view_old_version_title_op', 'Legal');
                    $('#view_old_version_table_op').DataTable();

                }
            }
            else {
                console.log(data);
            }
        }
    });

}




////onclick view _old_version_ip
function view_old_version_ip(event, Form_Name) {
    debugger;
    loaddata_version_ip();
    Form_Name: $('#formname_ip_vov').html(Form_Name)

}
///loaddata_view_old_version _ip
function loaddata_version_ip() {
    debugger;
    var load = {
        Dml_Indicator: "VOV"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormversion",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_version_ip').empty();
                    $('#view_old_version_table_ip').dataTable().fnClearTable();
                    $('#view_old_version_table_ip').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var no_version = data1.Table2[0].No_of_version;
                    $('#no_of_version_ip_vov').html(no_version)
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var status = '';

                        status = '<span class="label label-success">Active</span>';
                        Print = '<a data-toggle="tooltip" data-placement="bottom" title="Print" type="button" class="btn btn-edit btn-xs btn_r" data-id="" data-type="role_rights_edit" data-nav="redirect" data-name=""><i class="fa fa-print" aria-hidden="true"></i></a>';
                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=Deviceversion(this,"' + emp.Version_Id + '","DL")><i class="fa fa-trash" aria-hidden="true"></i></a>';

                        Restore = '<p data-toggle="modal" data-target="#view_restore_op" aria-hidden="true"><a onclick=\'Restore_ip_vov(this,"' + emp.Version_Id + '","RS")\'>Restore</a></p>'

                        Restore_As_New_Form = '<p data-toggle="modal" data-target="#view_restore_new_op" aria-hidden="true"><a>Restore As New Form</a></p>'

                        Send = '<p data-toggle="modal" data-target="#view_send_op" aria-hidden="true"><a>Send</a></p>'

                        if (emp.status == '1') {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Deviceversion(this,"' + emp.Version_Id + '","IA")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + Print + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + Restore + '</div><div class="text-left">' + Restore_As_New_Form + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';

                        }
                        else if (emp.status == '2') {
                            status = '<span class="label label-warning">In Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Deviceversion(this,"' + emp.Version_Id + '","AC")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if"> ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + Restore + '</div><div class="text-left">' + Restore_As_New_Form + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';
                        }
                        else {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Deviceversion(this,"' + emp.Version_Id + '","IA")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + Print + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + Restore + '</div><div class="text-left">' + Restore_As_New_Form + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';

                        }


                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Form_Name + '</td><td>' + emp.Version_No + '</td><td>' + status + '</td><td>' + emp.MappedPages + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_version_ip').html(tr);

                    datatable('view_old_version_table_ip', 'view_old_version_title_ip', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}


////onclick view _old_version_patient
function view_old_version_pat(event, Form_Name) {
    debugger;
    loaddata_version_pat();
    Form_Name: $('#formname_pat_vov').html(Form_Name)

}
///loaddata_view_old_version _pat
function loaddata_version_pat() {
    debugger;
    var load = {
        Dml_Indicator: "VOV"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormversion",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_version_pat').empty();
                    $('#view_old_version_table_pat').dataTable().fnClearTable();
                    $('#view_old_version_table_pat').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var no_version = data1.Table2[0].No_of_version;
                    $('#no_of_version_pat_vov').html(no_version)
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var status = '';

                        status = '<span class="label label-success">Active</span>';
                        Print = '<a data-toggle="tooltip" data-placement="bottom" title="Print" type="button" class="btn btn-edit btn-xs btn_r" data-id="" data-type="role_rights_edit" data-nav="redirect" data-name=""><i class="fa fa-print" aria-hidden="true"></i></a>';
                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=Deviceversion(this,"' + emp.Version_Id + '","DL")><i class="fa fa-trash" aria-hidden="true"></i></a>';

                        Restore = '<p data-toggle="modal" data-target="#view_restore_op" aria-hidden="true"><a onclick=\'Restore_pat_vov(this,"' + emp.Version_Id + '","RS")\'>Restore</a></p>'

                        Restore_As_New_Form = '<p data-toggle="modal" data-target="#view_restore_new_op" aria-hidden="true"><a>Restore As New Form</a></p>'

                        Send = '<p data-toggle="modal" data-target="#view_send_op" aria-hidden="true"><a>Send</a></p>'

                        if (emp.status == '1') {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Deviceversion(this,"' + emp.Version_Id + '","IA")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + Print + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + Restore + '</div><div class="text-left">' + Restore_As_New_Form + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';

                        }
                        else if (emp.status == '2') {
                            status = '<span class="label label-warning">In Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Deviceversion(this,"' + emp.Version_Id + '","AC")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if"> ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + Restore + '</div><div class="text-left">' + Restore_As_New_Form + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';
                        }
                        else {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Deviceversion(this,"' + emp.Version_Id + '","IA")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + Print + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + Restore + '</div><div class="text-left">' + Restore_As_New_Form + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';

                        }


                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Form_Name + '</td><td>' + emp.Version_No + '</td><td>' + status + '</td><td>' + emp.MappedPages + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_version_pat').html(tr);

                    datatable('view_old_version_table_pat', 'view_old_version_title_pat', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}

////onclick view _old_version_survey
function view_old_version_sur(event, Form_Name) {
    debugger;
    loaddata_version_sur();
    Form_Name: $('#formname_sur_vov').html(Form_Name)

}
///loaddata_view_old_version _sur
function loaddata_version_sur() {
    debugger;
    var load = {
        Dml_Indicator: "VOV"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormversion",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_version_sur').empty();
                    $('#view_old_version_table_sur').dataTable().fnClearTable();
                    $('#view_old_version_table_sur').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var no_version = data1.Table2[0].No_of_version;
                    $('#no_of_version_sur_vov').html(no_version)
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var status = '';

                        status = '<span class="label label-success">Active</span>';
                        Print = '<a data-toggle="tooltip" data-placement="bottom" title="Print" type="button" class="btn btn-edit btn-xs btn_r" data-id="" data-type="role_rights_edit" data-nav="redirect" data-name=""><i class="fa fa-print" aria-hidden="true"></i></a>';
                        del = '<a  data-toggle="tooltip" title="Delete" class="btn deleteicon-btn btn-xs" data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" href="javascript:void(0);" onclick=Deviceversion(this,"' + emp.Version_Id + '","DL")><i class="fa fa-trash" aria-hidden="true"></i></a>';

                        Restore = '<p data-toggle="modal" data-target="#view_restore_op" aria-hidden="true"><a onclick=\'Restore_sur_vov(this,"' + emp.Version_Id + '","RS")\'>Restore</a></p>'

                        Restore_As_New_Form = '<p data-toggle="modal" data-target="#view_restore_new_op" aria-hidden="true"><a >Restore As New Form</a></p>'

                        Send = '<p data-toggle="modal" data-target="#view_send_op" aria-hidden="true"><a>Send</a></p>'

                        if (emp.status == '1') {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Deviceversion(this,"' + emp.Version_Id + '","IA")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + Print + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + Restore + '</div><div class="text-left">' + Restore_As_New_Form + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';

                        }
                        else if (emp.status == '2') {
                            status = '<span class="label label-warning">In Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Deviceversion(this,"' + emp.Version_Id + '","AC")><i class="fa fa-check-square-o mr-5" aria-hidden="true">Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if"> ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + Restore + '</div><div class="text-left">' + Restore_As_New_Form + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';
                        }
                        else {
                            status = '<span class="label label-success">Active</span>';
                            active = '<a  data-toggle="tooltip" title="Make In Active!" class="btn btn-xs mr-10" href="javascript:void(0);"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form" onclick=Deviceversion(this,"' + emp.Version_Id + '","IA")><i class="fa fa-minus-square-o mr-5" aria-hidden="true">In-Active</i></a>';
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + Print + ' ' + del + '<a href="#" class="dropdown-togglve btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"> <span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown" id=""> <div class="text-left">' + Restore + '</div><div class="text-left">' + Restore_As_New_Form + '</div><div class="text-left">' + Send + '</div><div class="text-left">' + active + '</div></div></div></div>';

                        }

                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.Form_Name + '</td><td>' + emp.Version_No + '</td><td>' + status + '</td><td>' + emp.MappedPages + '</td><td>' + other_info + '</td></tr>';
                    });
                    $('#tbl_version_sur').html(tr);

                    datatable('view_old_version_table_sur', 'view_old_version_title_sur', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}





/////delete//active//inactive_for_view_old_version
function Deviceversion(event, Version_Id, indicatorid) {
    debugger;
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {

                     return;
                 }
                 else {
                     var status = "";
                     if (indicatorid == 'DL') {
                         status = "Are You Sure, You Want to Delete?";
                     }
                     else if (indicatorid == 'IA') {
                         status = "Are You Sure, You Want to In-Activate?";
                     }
                     else if (indicatorid == 'AC') {

                         status = "Are You Sure,You Want to Activate?";
                     }
                     toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", status,
                           {

                               closeButton: true,
                               progressBar: true,
                               allowHtml: true,
                               timeOut: 20000000,

                               onShown: function (toast) {
                                   $("#confirmationRevertYes").click(function () {

                                       var Delete = {
                                           Dml_Indicator: indicatorid,
                                           Version_Id: Version_Id
                                       }

                                       $.ajax({
                                           async: true,
                                           type: 'POST',
                                           url: "/Form_builder/insert_update_deleteFormversion",
                                           contentType: 'application/json; charset=UTF-8',
                                           data: JSON.stringify(Delete),
                                           success: function (data) {
                                               debugger;
                                               var data1 = JSON.parse(data);

                                               loaddata_version_op();
                                               loaddata_version_ip();
                                               loaddata_version_patient();
                                               loaddata_version_survey();
                                           }
                                       });
                                   });
                               }
                           })
                 }
             }
         }
     })
};

// restore op vov

function Restore_op_vov(event, Version_Id, indicatorid,form_id) {
    debugger;
    var status = "";

    if (indicatorid == 'RS') {
        status = "Restored Successfully";
    }

    toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", "Are You sure you Want to Restore ? ",
          {

              closeButton: true,
              progressBar: true,
              allowHtml: true,
              timeOut: 20000000,

              onShown: function (toast) {
                  $("#confirmationRevertYes").click(function () {

                      var Delete = {
                          Dml_Indicator: indicatorid,
                          Version_Id: Version_Id,
                          Form_id:form_id
                      }

                      $.ajax({
                          async: true,
                          type: 'POST',
                          url: "/Form_builder/insert_update_deleteFormversion",
                          contentType: 'application/json; charset=UTF-8',
                          data: JSON.stringify(Delete),
                          success: function (data) {
                              debugger;
                              var data1 = JSON.parse(data);

                              toastr.success('Restored Successfully');

                              loaddata_version_op(form_id);
                              loaddata();

                          }
                      });
                  });
              }
          })

}

// restore ip vov

function Restore_ip_vov(event, Version_Id, indicatorid) {
    debugger;
    var status = "";

    if (indicatorid == 'RS') {
        status = "Restored Successfully";
    }

    toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", status,
          {

              closeButton: true,
              progressBar: true,
              allowHtml: true,
              timeOut: 20000000,

              onShown: function (toast) {
                  $("#confirmationRevertYes").click(function () {

                      var Delete = {
                          Dml_Indicator: indicatorid,
                          Version_Id: Version_Id
                      }

                      $.ajax({
                          async: true,
                          type: 'POST',
                          url: "/Form_builder/insert_update_deleteFormversion",
                          contentType: 'application/json; charset=UTF-8',
                          data: JSON.stringify(Delete),
                          success: function (data) {
                              debugger;
                              var data1 = JSON.parse(data);

                              loaddata_version_ip();

                          }
                      });
                  });
              }
          })

}

// restore pat vov
function Restore_pat_vov(event, Version_Id, indicatorid) {
    debugger;
    var status = "";

    if (indicatorid == 'RS') {
        status = "Restored Successfully";
    }

    toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", status,
          {

              closeButton: true,
              progressBar: true,
              allowHtml: true,
              timeOut: 20000000,

              onShown: function (toast) {
                  $("#confirmationRevertYes").click(function () {

                      var Delete = {
                          Dml_Indicator: indicatorid,
                          Version_Id: Version_Id
                      }

                      $.ajax({
                          async: true,
                          type: 'POST',
                          url: "/Form_builder/insert_update_deleteFormversion",
                          contentType: 'application/json; charset=UTF-8',
                          data: JSON.stringify(Delete),
                          success: function (data) {
                              debugger;
                              var data1 = JSON.parse(data);

                              loaddata_version_pat();

                          }
                      });
                  });
              }
          })

}

// restore sur vov
function Restore_sur_vov(event, Version_Id, indicatorid) {
    debugger;
    var status = "";

    if (indicatorid == 'RS') {
        status = "Restored Successfully";
    }

    toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", status,
          {

              closeButton: true,
              progressBar: true,
              allowHtml: true,
              timeOut: 20000000,

              onShown: function (toast) {
                  $("#confirmationRevertYes").click(function () {

                      var Delete = {
                          Dml_Indicator: indicatorid,
                          Version_Id: Version_Id
                      }

                      $.ajax({
                          async: true,
                          type: 'POST',
                          url: "/Form_builder/insert_update_deleteFormversion",
                          contentType: 'application/json; charset=UTF-8',
                          data: JSON.stringify(Delete),
                          success: function (data) {
                              debugger;
                              var data1 = JSON.parse(data);

                              loaddata_version_sur();

                          }
                      });
                  });
              }
          })

}
//Load Form Type

function Formtype() {
    debugger;
    var department = {
        Dml_Indicator: "FT",
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            debugger
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Department;
                    $('#Form_Type').empty();

                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.Formtypeid + '">' + emp.Formtype + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#Form_Type').append(Department);
                        $("#Form_Type").change();

                    }
                    else {
                        $('#Form_Type').append(default_opt + Department);

                    }

                }
            }
            else {
                console.log(data1);
            }
        }
    });
}
$('#documents').change(function () {
    debugger;
    doctorimageupload();
});

function doctorimageupload() {
    debugger;
    var uploadpath = $('#documents').val();
    var fileExtension = uploadpath.substring(uploadpath.lastIndexOf(".") + 1, uploadpath.length);
    if (fileExtension == "png" || fileExtension == "PNG") {
    }
    else {
        $('#Pdocuments').show();
        $('#Pdocuments').fadeOut(8000);
        $('#Pdocuments').html('Please upload file having extensions .png only.');
        $('#documents').val('');
        $('#documents').focus();
        return false;
    }
    if (window.FormData !== undefined) {
        var fileUpload = $("#documents").get(0);
        var files = fileUpload.files;
        var fileData = new FormData();

        $.when(checkimagedimensionvalidation(fileUpload.files[0], 240, 256)).
        done(function (Results) {
            if (Results == 'Success') {
                for (var i = 0; i < files.length; i++) {
                    fileData.append(files[i].name, files[i]);

                }
                fileData.append('username', 'Manas');
                $.ajax({
                    url: '/Form_builder/UploadPhotoFiles',
                    type: "POST",
                    contentType: false,
                    processData: false,
                    data: fileData,
                    success: function (result) {
                        $.each(result, function (i, emp) {
                            $('#profile-photo').empty();
                            $('#profile-photo').show();
                            var d, n, drloimage;
                            d = new Date();
                            n = d.getTime();
                            drloimage = $('#hdnCDfilepath').val() + $('#HospitalId').val() + '/FormBuilderFileUpload/' + emp + '?' + n;
                            $('#profile-photo').append('<a href="' + drloimage + '" target="_blank" id="formimage">Image</a>');
                            //$('#documents').val(emp);
                            $('#hformimg').val(emp);
                            //var drphotoSrc = $('#hdnfilepath').val() + $('#HospitalId').val() + "/Photos/" + emp;
                            //$('#ProfilePhotoTop,#ProfilePhoto').attr('src', drphotoSrc);
                        });
                    }
                });
            }
            else {
                $('#documents').val('');

            }
            //this code is executed when all ajax calls are done
        });

    }
}
//New button click function
$('#btn_new_op').click(function () {
    debugger;
    var form = ''
    form = '1'
    $('#Htype_form').val(form)
    $('#Htype_form').val(form)

    //one master concept
    //if ($('#hiddenRole_id').val() == 2) {
    //    $("#Form_Branch").attr('disabled', false);
    //}
    //else
    //{
    //    $("#Form_Branch").val($('#HospitalBranchID').val());

    //}
    
    $("#Form_Type").attr('disabled', false);
    $("#Module_Type").attr('disabled', false);
    $("#Module_subtype").attr('disabled', false);
})
$('#btn_new_ip').click(function () {
    debugger;
    var form = ''
    form = '2'
    $('#Htype_form').val(form)
})
$('#btn_new_pat').click(function () {
    debugger;
    var form = ''
    form = '3'
    $('#Htype_form').val(form)
})
$('#btn_new_sur').click(function () {
    debugger;
    var form = ''
    form = '4'
    $('#Htype_form').val(form)
})


//New button after submit click function

//$('#Form_submit').click(function () {
//    debugger;
//    //one master concept
//    //if ($("#Form_Branch").val() == 0 || $("#Form_Branch").val() == '') {
//    //    $('#P_Form_Branch').show();
//    //    $('#P_Form_Branch').fadeOut(8000);
//    //    $('#P_Form_Branch').html('Select branch ');
//    //    $('#Form_Branch').val(0);
//    //    $('#Form_Branch').focus();
//    //    return false;
//    //}
//    if ($("#Module_Type").val() == 0 || $("#Module_Type").val() == null) {
//        $('#P_Module_Type').show();
//        $('#P_Module_Type').fadeOut(8000);
//        $('#P_Module_Type').html('Select Module Type');
//        $('#Module_Type').val(0);
//        $('#Module_Type').focus();
//        return false;
//    }
//    if ($('#Module_Type').val() == "2") { 
//    if ($("#Type_of_Admission").val() == 0 || $("#Type_of_Admission").val() == null) {
//        $('#P_Type_of_Admission').show();
//        $('#P_Type_of_Admission').fadeOut(8000);
//        $('#P_Type_of_Admission').html('Select Admission Type');
//        $('#P_Type_of_Admission').val(0);
//        $('#P_Type_of_Admission').focus();
//        return false;
//    }
//}
//    //if ($("#Module_subtype").val() == 0 || $("#Module_subtype").val() == null) {
//    //    $('#P_Module_subtype').show();
//    //    $('#P_Module_subtype').fadeOut(8000);
//    //    $('#P_Module_subtype').html('Select Module Sub Type ');
//    //    $('#Module_subtype').val(0);
//    //    $('#Module_subtype').focus();
//    //    return false;
//    //}
//    if ($("#Form_Type").val() == 0 || $("#Module_subtype").val() == null) {
//        $('#P_Form_Type').show();
//        $('#P_Form_Type').fadeOut(8000);
//        $('#P_Form_Type').html('Select Module Sub Type ');
//        $('#Form_Type').val(0);
//        $('#Form_Type').focus();
//        return false;
//    }
//    if ($("#Form_Name").val() == '') {
//        $('#P_Form_Name').show();
//        $('#P_Form_Name').fadeOut(8000);
//        $('#P_Form_Name').html('Select Module Name ');
//        $('#Form_Name').val(0);
//        $('#Form_Name').focus();
//        return false;
//    }
//    //var is_multiple = 0;
//    //if ($('#ChkIS_mulitiple').prop("checked") == true) {
//    //    is_multiple = 1;
//    //}

//    var is_multiple = $('#ChkIS_mulitiple').val() == '' ? 0 : $('#ChkIS_mulitiple').val();
//    var Submit = ''
//    var uploadfile = $('#hformimg').val()
//    if ($('#Form_submit').html() == "Submit") {
//        Submit = {
//            //Branch_id: $('#Form_Branch').val(),            //one master concept
//            Form_Type: $('#Form_Type').val(),
//            //  Form_Structure: $('#Form_Structure').val(),
//            Moduletype: $('#Module_Type').val(),
//            type_of_admission: ($('#Type_of_Admission').val() == '' ? 0 : $('#Type_of_Admission').val()).toString(),
//            Module_subtype: $('#Module_subtype').val(),
//            Form_Name: $('#Form_Name').val(),
//            Description: $('#Description').val(),
//            Photo: uploadfile,
//            is_multiple: is_multiple,
//            Type_form: $('#Htype_form').val(),
//            Hospitalid: $('#HospitalId').val(),
//            Dml_Indicator: 'FIN'
//        }
//    }
//    else {
//        Submit = {
//            //Branch_id: $('#Form_Branch').val(),            //one master concept
//            Form_Type: $('#Form_Type').val(),
//            // Form_Structure: $('#Form_Structure').val(),
//            Moduletype: $('#Module_Type').val(),
//            type_of_admission: ($('#Type_of_Admission').val() == '' ? 0 : $('#Type_of_Admission').val()).toString(),
//            Module_subtype: $('#Module_subtype').val(),
//            Form_Name: $('#Form_Name').val(),
//            Description: $('#Description').val(),
//            Photo: uploadfile,
//            is_multiple: is_multiple,
//            Formid: $('#autodevice_Id').val(),
//            // Type_form: $('#Htype_form').val(),
//            Hospitalid: $('#HospitalId').val(),
//            Dml_Indicator: 'FUP'
//        }
//    }


//    $.ajax({
//        async: true,
//        type: 'POST',
//        url: "/Form_builder/insert_update_deleteFormMaster",
//        contentType: 'application/json; charset=UTF-8',
//        data: JSON.stringify(Submit),
//        success: function (data) {
//            debugger;
//            var data1 = JSON.parse(data);
//            var data2 = data1.data.Table;

//            if (data1.msg == 'Success') {
//                if (data2[0].msg == 'inserted') {
//                    toastr.success("Added Successfully");
//                    clearform();
//                    loaddata();
//                    loaddata1();
//                    loaddata2();
//                    loaddata3();
//                }
//                else if (data2[0].msg == 'already') {
//                    toastr.warning("Device Model Name already exists");
//                }
//                else if (data2[0].msg == 'updated') {
//                    toastr.success("Updated Successfully");
//                    clearform();
//                    loaddata();
//                    loaddata1();
//                    loaddata2();
//                    loaddata3();
//                }
//                else {
//                    console.log(data1)
//                }
//            }
//            else {
//                console.log(data1)

//            }
//        }

//    })
//});



//op edit

//function FormBuildereditop(event, Formid) {
//    $('#autodevice_Id').val(Formid)

//    debugger;

//    var Edit = {

//        Formid: Formid,

//        Dml_Indicator: 'FED'
//    };
//    $.ajax({
//        async: true,
//        type: 'POST',
//        url: "/Form_builder/insert_update_deleteFormMaster",
//        contentType: 'application/json; charset=UTF-8',
//        data: JSON.stringify(Edit),
//        success: function (data) {
//            debugger;
//            var data1 = JSON.parse(data);
//            var emp = data1.data.Table;
//            $("#Form_Branch").val(emp[0].Branch_id);
//            $('#Form_Type').val(emp[0].Form_Type);
//            $('#Form_Name').val(emp[0].Form_Name);
//            $('#Form_Structure').val(emp[0].Form_Structure);
//            $('#Description').val(emp[0].Description);
//            $('#Module_Type').val(emp[0].Module_Type);
//            $('#Module_subtype').val(emp[0].Module_Subtype);
//            $("#Form_submit").html("Update");
//            $('#Form_Branch').focus();
//        }

//    })
//}

//function FormBuildereditop(event, Formid, Branch_id) {

//    $('#shade_slot').show();
//    $('#op_formbuilder_table').dataTable().fnDraw();
//    $('#autodevice_Id').val(Formid)

//    debugger;
//    $('#Hdpt_id').val(Branch_id);
//    var did = $(event).attr("data-id");
//    var navigation = $(event).attr("data-nav");
//    var name = $(event).attr("data-name");
//    var columntype = $(event).attr("data-type");
//    debugger;
//    var chkprevilige = check_previlige(did, navigation, name, columntype);
//    $.when(chkprevilige)
//     .then(function (data) {
//         var data1 = JSON.parse(data);
//         $('#shade_slot').hide();
//         if (data1.msg == "Success") {
//             var data1 = data1.data;
//             if (jQuery.isEmptyObject((data1))) {
//             }
//             else {
//                 var j = 0;
//                 var status = data1.Table[0].role_rights_add
//                 $('#hstatus').val(status)
//                 if (status == 0) {
//                     debugger;

//                     return;
//                 }
//                 else {

//                     debugger;
//                     $('#form-builder-popup').modal('toggle');
//                     var Edit = {

//                         Dml_Indicator: 'FED',
//                         Formid: Formid,
//                     }
//                     $.ajax({
//                         async: true,
//                         type: 'POST',
//                         url: "/Form_builder/insert_update_deleteFormMaster",
//                         contentType: 'application/json; charset=UTF-8',
//                         data: JSON.stringify(Edit),
//                         success: function (data) {
//                             debugger;
//                             var data2 = JSON.parse(data);

//                             var emp = data2.data.Table;
//                             //One master concept
//                             //$("#Form_Branch").val(emp[0].Branch_id);
//                             //$("#Form_Branch").attr('disabled', true);
//                             $('#Form_Type').val(emp[0].Form_Type);
//                             $("#Form_Type").attr('disabled', true);
//                             $('#Form_Name').val(emp[0].Form_Name);
//                             $('#Form_Structure').val(emp[0].Form_Structure);
//                             $('#Description').val(emp[0].Description);
//                             $('#Module_Type').val(emp[0].Module_Type);                          
//                             $("#Module_Type").attr('disabled', true);
//                             $('#Module_subtype').val(emp[0].Module_Subtype);
//                             $("#Module_subtype").attr('disabled', true);
//                             if (emp[0].Module_Type == 2) {
//                                 $('#div_Type_of_Admission').show()
//                                 $('#Type_of_Admission').val(emp[0].Type_of_Admission.split(',')).change();
//                             }
//                             else {
//                                 $('#div_Type_of_Admission').hide()
//                             }

                                               
//                            //$('#formimage').val(emp[0].Photo);

//                             if (emp[0].form_image != null && emp[0].form_image != "") {
//                                 //$('#HospitalLogo_photo').show();
//                                 //$('#HospitalLogo_photo').append('<a href="' + $('#hdnHospitalfilepath').val() + "HospitalandBranchesImage/" + emp.hospital_logo + '" target="_blank">View</a>');
//                                 $('#profile-photo').show();
//                                 drloimage = $('#hdnCDfilepath').val() + $('#HospitalId').val() + '/FormBuilderFileUpload/' + emp[0].form_image;
//                                 $('#profile-photo').append('<a href="' + drloimage + '" target="_blank" id="formimage">Image</a>');
//                                 $('#hformimg').val(emp[0].form_image);
//                             }

                            
//                             if (emp[0].is_multiple == 1) {
//                                 $('a[data-toggle=ChkIS_mulitiple][data-title="1"]').removeClass('not-active');
//                                 $('a[data-toggle=ChkIS_mulitiple][data-title="1"]').addClass('active');
//                                 $('a[data-toggle=ChkIS_mulitiple][data-title="0"]').removeClass('active');
//                                 $('a[data-toggle=ChkIS_mulitiple][data-title="0"]').addClass('not-active');
//                             }
//                             else {
//                                 $('a[data-toggle=ChkIS_mulitiple][data-title="1"]').removeClass('active');
//                                 $('a[data-toggle=ChkIS_mulitiple][data-title="1"]').addClass('not-active');
//                                 $('a[data-toggle=ChkIS_mulitiple][data-title="0"]').removeClass('not-active');
//                                 $('a[data-toggle=ChkIS_mulitiple][data-title="0"]').addClass('active');
//                             }
//                             $('#ChkIS_mulitiple').val(emp[0].is_multiple);
//                             $("#Form_submit").html("Update");
//                             //$('#Form_Branch').focus();          //one master concept
//                             $('#autodevice_Id').val(Formid);
//                         }

//                     })

//                 }
//             }
//         }
//     })

//}


//function FormBuildereditip(event, Formid) {
//    debugger;
//    $('#autodevice_Id').val(Formid)

//    var Edit = {

//        Formid: Formid,

//        Dml_Indicator: 'FED'
//    };
//    $.ajax({
//        async: true,
//        type: 'POST',
//        url: "/Form_builder/insert_update_deleteFormMaster",
//        contentType: 'application/json; charset=UTF-8',
//        data: JSON.stringify(Edit),
//        success: function (data) {
//            debugger;
//            var data1 = JSON.parse(data);
//            var emp = data1.data.Table;
//            //$("#Form_Branch").val(emp[0].Branch_id);     //one master concept
//            $('#Form_Type').val(emp[0].Form_Type);
//            $('#Form_Name').val(emp[0].Form_Name);
//            $('#Form_Structure').val(emp[0].Form_Structure);
//            $('#Description').val(emp[0].Description);
//            $('#Type_of_Admission').val(emp[0].Type_of_Admission);
//            $("#Form_submit").html("Update");
//            //$('#Form_Branch').focus();          //one master concept
           
//        }

//    })
//}

//function FormBuildereditpat(event, Formid) {
//    $('#autodevice_Id').val(Formid)

//    debugger;

//    var Edit = {

//        Formid: Formid,

//        Dml_Indicator: 'FED'
//    };
//    $.ajax({
//        async: true,
//        type: 'POST',
//        url: "/Form_builder/insert_update_deleteFormMaster",
//        contentType: 'application/json; charset=UTF-8',
//        data: JSON.stringify(Edit),
//        success: function (data) {
//            debugger;
//            var data1 = JSON.parse(data);
//            var emp = data1.data.Table;
//            //$("#Form_Branch").val(emp[0].Branch_id);           //one master concept
//            $('#Form_Type').val(emp[0].Form_Type);
//            $('#Form_Name').val(emp[0].Form_Name);
//            $('#Form_Structure').val(emp[0].Form_Structure);
//            $('#Description').val(emp[0].Description);
//            $("#Form_submit").html("Update");
//            //$('#Form_Branch').focus();             //one master concept
//        }

//    })
//}

//function FormBuildereditsur(event, Formid) {
//    $('#autodevice_Id').val(Formid)

//    debugger;

//    var Edit = {

//        Formid: Formid,

//        Dml_Indicator: 'FED'
//    };
//    $.ajax({
//        async: true,
//        type: 'POST',
//        url: "/Form_builder/insert_update_deleteFormMaster",
//        contentType: 'application/json; charset=UTF-8',
//        data: JSON.stringify(Edit),
//        success: function (data) {
//            debugger;
//            var data1 = JSON.parse(data);
//            var emp = data1.data.Table;
//            //$("#Form_Branch").val(emp[0].Branch_id);             //one master concept 
//            $('#Form_Type').val(emp[0].Form_Type);
//            $('#Form_Name').val(emp[0].Form_Name);
//            $('#Form_Structure').val(emp[0].Form_Structure);
//            $('#Description').val(emp[0].Description);
//            $("#Form_submit").html("Update");
//            //$('#Form_Branch').focus();               //one master concept 
//        }

//    })
//}

//clear function

//$('#Form_close').click(function () {
//    clearform();
//    //LoadBranch();
//})
//$('#Form_CloseX').click(function () {
//    clearform();
//    //LoadBranch();
//})
//function clearform() {
//    debugger;
//    //one master concept 
//    //if (parseInt($('#hiddenRole_id').val()) == 2) {
//    //    $("#Form_Branch").val(0);
//    //}
    
//    $("#Form_Type").val(0);
//    $("#Form_Name").val('');
//    $("#Module_subtype").val(0);
//    $("#Module_Type").val(0);
//    $("#Type_of_Admission").val(0).change();
//    $("#Form_Structure").val('');
//    $("#Description").val('');
//    $('#hformimg').val('');
//    $('#profile-photo').empty();
//    $('#formimage').val('');
//    // $('#ChkIS_mulitiple').prop("checked", false);
//    $('a[data-toggle=ChkIS_mulitiple][data-title="1"]').removeClass('active');
//    $('a[data-toggle=ChkIS_mulitiple][data-title="1"]').addClass('not-active');
//    $('a[data-toggle=ChkIS_mulitiple][data-title="0"]').removeClass('active');
//    $('a[data-toggle=ChkIS_mulitiple][data-title="0"]').addClass('not-active');

//    $('#ChkIS_mulitiple').val('');
//    $("#Form_submit").html("Submit");
//    $('#autodevice_Id').val('');
//}

//Load Module Type

function Loadmoduletype() {
    debugger;
    var department = {
        Dml_Indicator: "MT",
        // branch_id: branch
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            debugger
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Department;
                    $('#Module_Type').empty();
                    $('#type_filter').empty();

                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.moduleid + '">' + emp.moduletype + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#Module_Type').append(Department);
                        // $("#Module_Type").change();
                        $('#type_filter').append(Department);
                        $("#type_filter").change();
                    }
                    else {
                        $('#Module_Type').append(default_opt + Department);
                        $('#type_filter').append(default_opt + Department);

                    }

                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

$("#Module_Type").change(function () {
    if ($('#Module_Type').val() == "2") {       
        $('#div_Type_of_Admission').show()
        $('#div_image').hide()
    }
    else {
        $('#div_Type_of_Admission').hide()
        $('#div_image').show()
        
    }
    Loadmodulesubtype()
})

//load module subtype

function Loadmodulesubtype() {
    debugger;
    var department = ''
    if ($('#Module_Type').val() == 1) {
        debugger;
        department = {
            Dml_Indicator: "OP_ASS"
        }
    }
    else {
        department = {
            Dml_Indicator: "MST"
            // branch_id: branch
        }
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            debugger
            $('#Module_subtype').empty();
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Department;
                    $('#Module_subtype').empty();

                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.moduleid + '">' + emp.module_subtype + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#Module_subtype').append(Department);
                        $("#Module_subtype").change();

                    }
                    else {
                        $('#Module_subtype').append(default_opt + Department);

                    }

                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

function copy_branch(event, Formid, Form_Name) {
    debugger;
    $('#Hcate_id').val(Formid);
    $('#Hscate_id').val(Form_Name);
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {

                     return;
                 }
                 else {
                     $('#copypopup').modal('toggle');
                 }
             }
         }
     })
}

$('#copy_close').click(function () {
    debugger;
    $('#Copy_Branch').empty()
})

$('#copy_submit').click(function () {
    debugger;
    var submit = {
        Branch_id: $('#Copy_Branch').val().toString(),
        Formid: $('#Hcate_id').val(),
        Dml_Indicator: 'CP'

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Form_builder/insert_update_deleteFormAccess",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(submit),
        success: function (data) {
            debugger;

            var data1 = JSON.parse(data);
            var data2 = data1.data.Table;

            if (data1.msg == 'Success') {
                toastr.success("Added Successfully");
                loaddata();
            }
            else {
                console.log(data1)

            }
        }

    })
})
$('#btnfilter').on('click', function () {
    debugger;
  //  var Fb_branch_id = $('#fbranch_id').val();
    var Fb_Type = $('#type_filter').val();
    loaddata(Fb_Type)
});
$('#btnclear').click(function () {
    debugger;
    $('#fbranch_id').val(0),
    $('#type_filter ').val(0),
    loaddata();

});










