if ($("#ovum_transfer").val() == 1) {
    var hosturl = $('#hosturl').val();
    var patientId = $('#patientId').val();
    var instructionArray = Array();
    $(".bp_mask").inputmask("999/999", {
        autoUnmask: true});
    if ($("select.ut_cx").length) {
        $("select.ut_cx").select2({
            width: '170px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */

                return "<input class='form-control' id='newddTerm'  value='" + term + "'><a href='#' id='addddNew'  class='btn btn-default'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addddNew', function () {
            var colName = $("#ut_cx").attr("data-colName");
            var tableName = $("#ut_cx").attr("select-tableName");
            var selectedut = $("#newddTerm").val();
            $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=addUt', {selectedut: selectedut}, function (data) {
                $('.ut_cx > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + selectedut + '</option>');
                //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#childj');
                $('#ut_cx').select2('val', Number(data)); // select the new term
                $('#ut_cx').select2('close'); // close the dropdown
            });
        });
    }
    $("select.avf").select2({
        width: '170px',
        allowClear: true,
        formatNoMatches: function (term) {
            /* customize the no matches output */

            return "<input class='form-control' id='newddTerm'  value='" + term + "'><a href='#' id='addddNew'  class='btn btn-default'>ADD</a>"
        }
    }).parent().find('.select2-with-searchbox').on('click', '#addddNew', function () {
        var colName = $("#avf").attr("data-colName");
        var tableName = $("#avf").attr("select-tableName");
        var selectedavf = $("#newddTerm").val();
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=addAvf', {selectedavf: selectedavf}, function (data) {
            $('.avf_selected > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + selectedavf + '</option>');
            //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#childj');
            $('#avf').select2('val', Number(data)); // select the new term
            $('#avf').select2('close'); // close the dropdown
        });
    });
    $("select.easy_difficult").select2({
        width: '170px',
        allowClear: true,
        formatNoMatches: function (term) {
            /* customize the no matches output */

            return "<input class='form-control' id='newddTerm'  value='" + term + "'><a href='#' id='addddNew'  class='btn btn-default'>ADD</a>"
        }
    }).parent().find('.select2-with-searchbox').on('click', '#addddNew', function () {
        var colName = $("#easy_difficult").attr("data-colName");
        var tableName = $("#easy_difficult").attr("select-tableName");
        var selectedeasydifficult = $("#newddTerm").val();
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=addEasy', {selectedeasydifficult: selectedeasydifficult}, function (data) {
            $('.easy_difficult > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + selectedeasydifficult + '</option>');
            //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#childj');
            $('#easy_difficult').select2('val', Number(data)); // select the new term
            $('#easy_difficult').select2('close'); // close the dropdown
        });
    });
    $("select.easy_difficult").on('change', function () {
// Do something wi.on('change', function () {
        var colName = $(this).attr("data-colName");
        var tableName = $(this).attr("select-tableName");
        var thisvalue = $(this).val();
        if (thisvalue == '-2') {

            $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=editModal', {
                tableName: tableName,
                colName: colName
            }, function (data) {

                $("select.easy_difficult").select2("val", "");
                $("#alldatadiv").html(data);
                $("#edit_model").modal('show');
            });
        }

    })
    $("select.avf").on('change', function () {
// Do something wi.on('change', function () {
        var colName = $(this).attr("data-colName");
        var tableName = $(this).attr("select-tableName");
        var thisvalue = $(this).val();
        if (thisvalue == '-2') {

            $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=editModal', {
                tableName: tableName,
                colName: colName
            }, function (data) {

                $("select.avf").select2("val", "");
                $("#alldatadiv").html(data);
                $("#edit_model").modal('show');
            });
        }

    })
    $("select.ut_cx").on('change', function () {
// Do something wi.on('change', function () {
        var colName = $(this).attr("data-colName");
        var tableName = $(this).attr("select-tableName");
        var thisvalue = $(this).val();
        if (thisvalue == '-2') {

            $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=editModal', {
                tableName: tableName,
                colName: colName
            }, function (data) {

                $("select.ut_cx").select2("val", "");
                $("#alldatadiv").html(data);
                $("#edit_model").modal('show');
            });
        }

    })



    $("#op_date").on('change', function () {
        var value = $(this).val();
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=getDayName', {value: value}, function (data) {
            $("#op_day").val(data);
        });
    });
    $('.updateDiagnosis').live('change', function ()
    {
        var id = $(this).attr("data-id");
        var colName = $(this).attr("data-colName");
        var tableName = $(this).attr("data-tableName");
        //var value = $(this).val();
        var controllName = $('#controllName').val();
        var patientId = $('#patientId').val();
        var dated = 0;
        var realvalues = new Array(); //storing the selected values inside an array
        var lastvalue;
        var slcttable;
        if (colName == "diagnosis")
        {
            $('#diagnosis :selected').each(function (i, selected) {
                realvalues[i] = $(selected).val();
                lastvalue = $(selected).val();
            });
            slcttable = "diagnosis";
        }

        if (colName == "drugs")
        {
            $('#follow_drugs :selected').each(function (i, selected) {
                realvalues[i] = $(selected).val();
                lastvalue = $(selected).val();
            });
            slcttable = "follow_drugs";
        }


        if (lastvalue == -2)
        {
            var selectId = $(this).attr('id');
            $("#" + selectId + " option[value='-2']").prop("selected", false);
            $("#" + selectId + "").select2();
            $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=alldiagnosis', {colName: colName}, function (data)
            {
                $("#alldatadiv").html(data);
                $("#newitem_name").val("");
                $("#newitem_id").val("");
                $("#slctTbl").val(slcttable);
                $("#edit_model").modal('show');
                $("#edtSlct").attr("data-tableName", slcttable);
                realvalues.pop();
            });
        }

        if ($.inArray("-1", realvalues) != -1)
        {
            var selectId = $(this).attr('id');
            $("#" + selectId + " option[value='-1']").prop("selected", false);
            $("#" + selectId + "").select2();
            $("#add_model").modal('show');
            realvalues.pop();
            $("#diagnosisName").val('');
            $("#diagnosisName").attr("data-select", selectId);
            $("#diagnosisName").attr("data-tablename", slcttable);
            $("#diagnosisName").attr("data-id", id);
        }


        if (lastvalue != '-1' && lastvalue != '-2' && $.inArray("-1", realvalues) == -1)
        {
            $.ajax({
                url: hosturl + '/controllers/' + controllName + '.php?ac=update',
                data: {
                    value: JSON.stringify(realvalues),
                    id: id,
                    tableName: tableName,
                    colName: colName,
                    patientId: patientId,
                    dated: dated
                },
                type: 'POST',
                success: function (data) {

                }
            });
        }
    });
    $(".addDiagnosisName").live('change', function () {
        var hosturl = $('#hosturl').val();
        var selectId = $(this).attr('data-select');
        var table = $(this).attr('data-tablename');
        var id = $(this).attr("data-id");
        var newitem = $(this).val();
        var select;
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=adddiagnosis', {diagnosisName: newitem, id: id, table: table}, function (data) {

            $('.' + table + ' > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
            $('#' + selectId + ' option[value="' + Number(data) + '"]').attr('selected', true);
            $('select').select2();
            //$('#diagnosis' + $thisitr).select2('val', Number(data)); // select the new term
        });
    });
//////////////////////new row model/////////////////////

    $("#addnewRow").live('click', function () {
        var tableName = $(this).attr('data-tableName');
        var selectname = $(this).attr('data-selectClass');
        var colName = $("#colName").val();
        $.post(hosturl + "/controllers/ovumpickupembryotrans.php?ac=addNewRow", {
            tableName: tableName,
            colName: colName
        }, function (data) {
            var dataid = Number(data);
            $('.' + selectname + ' > .deleteoptincompo').before('<option value="' + dataid + '" ></option>');
            $("#dtAddTbl").prepend('<tr>\
                                            <td style="width: 100%">\
                                                <a href="#" >\
                                                    <input type="text" id="newitem_name" class="form-control updateElementSelect" data-selectClass="' + tableName + '"  data-select2="yes" data-id="' + dataid + '" data-colName="name" data-tableName="' + tableName + '" value="" name="newamount_name"/>\
                                                </a>\
                                            </td>\
                                            <td>\
                                                <a class="delrow fa fa-times btn red fleft" data-colName="conditions" data-link="gyna" data-tableName="' + tableName + '" data-fn="deldiagnosis" data-id="' + dataid + '" ></a>\
                                            </td>\
                                        </tr>');
        });
    });
//// update model  ////
    $("#newitem_name").live('change', function () {

        var value = $(this).val();
        var id = $(this).attr("data-id");
        var tablename = $("#tablename").val();
        var patientid = $("#patientid").val();
        var colName = $("#colName").val();
        if (id != '') {
            $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=updateModal', {
                tablename: tablename,
                colName: colName,
                id: id,
                value: value,
                patientid: patientid
            }, function (data) {
                if (tablename === "easydifficult") {
                    $(".easy_difficult option[value='" + id + "']").text(value);
                    $('#easy_difficult').select2('val', id);
                } else if (tablename === "avf") {
                    $(".avf option[value='" + id + "']").text(value);
                    $('#avf').select2('val', id);
                } else if (tablename === "ut") {
                    $(".ut_cx option[value='" + id + "']").text(value);
                    $('#ut_cx').select2('val', id);
                }


            });
        }
    });
    ///////////////////////////////////////////////////// delete row from modal and database and select2////////////////////////

    $('.deleteModal').live('click', function () {
        var id = $(this).attr('data-id');
        var tablename = $("#tablename").val();
        var tr = $(this).parent();
        bootbox.dialog({
            message: "Do you want to delete this item?",
            title: "Delete confirmation",
            buttons: {
                danger: {
                    label: "Cancel",
                    className: "btn-primary"
                },
                main: {
                    label: "Delete",
                    className: "btn-danger",
                    callback: function () {

                        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=delRowsommedel', {
                            id: id,
                            tableName: tablename
                        }, function (res) {

                            if (res == 1) {
                                tr.parent().remove();
                                if (tablename === "easydifficult") {
                                    $(".easy_difficult option[value='" + id + "']").remove();
                                } else if (tablename === "ut") {
                                    $(".ut_cx option[value='" + id + "']").remove();
                                } else if (tablename === "avf") {
                                    $(".avf option[value='" + id + "']").remove();
                                }
                            } else {
                                alert("!!!!!!!!!!!!! some thing wrong here");
                            }

                        });
                    }
                }
            }
        }).find('.modal-content').addClass('modal-sm');
    });

    $('#operation_follow').select2({
        width: '170px',
        allowClear: true,
        formatNoMatches: function (term) {
            /* customize the no matches output */
            $id = $(this).attr('data-id');
            return "<input class='form-control' id='newoperation_followTerm' value='" + term + "'  data-id='" + $id + "'><a href='#' id='addoperation_followNew'  data-id='" + $id + "' class='btn btn-default addoperation_followselected'>Add</a>"
        }
    }).parent().find('.select2-with-searchbox').on('click', '.addoperation_followselected', function () {

        var selectid = $(this).attr('data-id');
        var newitem = $('#newoperation_followTerm').val();
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=addnewoperation', {selectedoperation: newitem, id: selectid, inline: 1}, function (data) {
            $('#operation_follow > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
            //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#operation_follow');
            $('#operation_follow').select2('val', Number(data)); // select the new term
            $('#operation_follow').select2('close'); // close the dropdown
        });
    });

    $(".datetimepicker").datetimepicker({
        autoclose: true
    });

    $(".timepicker").timepicker({autoclose: !0, minuteStep: 5, showSeconds: !1, showMeridian: !1})


    $("#follow_print").on('click', function () {
        var id = $(this).attr('data-id');
        var preFor;
        if ($('#forHusbandF').is(':checked')) {
            preFor = 1;
        } else {
            preFor = 0;
        }

        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=printFollow', {id: id, preFor: preFor}, function (data) {
            $('#printSheetDiv').html(data);
            $("#patient_profile_print").print({
                deferred: $.Deferred().done(function () {
                    $('#printSheetDiv').html('');
                })
            });
        });
    });

    $("#follow_print_sheet").on('click', function () {
        var id = $(this).attr('data-id');
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=printSheet', {id: id}, function (data) {
            $('#printSheetDiv').html(data);
            $("#patient_profile_print").print({
                deferred: $.Deferred().done(function () {
                    $('#printSheetDiv').html('');
                })
            });
        });
    });

    $(".addPre").on('click', function () {
        var id = $(this).attr('data-id');
        var tableName = $(this).attr('data-tableName');
        var itrName = $(this).attr('data-count');
        var itr = $('#' + itrName).val();
        var newItr = Number(itr) + 1;
        $('#' + itrName).val(newItr);
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=addPreRow', {newItr: newItr, id: id, tableName: tableName}, function (data) {
            $('#' + tableName + '_div').prepend(data);
        });
    });

    $('#follow_rout').select2({
        width: '170px',
        allowClear: true,
        formatNoMatches: function (term) {
            /* customize the no matches output */
            $id = $(this).attr('data-id');
            return "<input class='form-control' id='newfollow_routTerm' value='" + term + "'  data-id='" + $id + "'><a href='#' id='addfollow_routNew'  data-id='" + $id + "' class='btn btn-default addfollow_routselected'>Add</a>"
        }
    }).parent().find('.select2-with-searchbox').on('click', '.addfollow_routselected', function () {

        var selectid = $(this).attr('data-id');
        var newitem = $('#newfollow_routTerm').val();
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=addnewoRecord', {selectedoperation: newitem, id: selectid, inline: 1, tableName: "follow_rout", colName: "rout"}, function (data) {
            $('#follow_rout > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
            //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#follow_rout');
            $('#follow_rout').select2('val', Number(data)); // select the new term
            $('#follow_rout').select2('close'); // close the dropdown
        });
    });

    $('#follow_semen').select2({
        width: '170px',
        allowClear: true,
        formatNoMatches: function (term) {
            /* customize the no matches output */
            $id = $(this).attr('data-id');
            return "<input class='form-control' id='newfollow_semenTerm' value='" + term + "'  data-id='" + $id + "'><a href='#' id='addfollow_semenNew'  data-id='" + $id + "' class='btn btn-default addfollow_semenselected'>Add</a>"
        }
    }).parent().find('.select2-with-searchbox').on('click', '.addfollow_semenselected', function () {

        var selectid = $(this).attr('data-id');
        var newitem = $('#newfollow_semenTerm').val();
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=addnewoRecord', {selectedoperation: newitem, id: selectid, inline: 1, tableName: "follow_semen", colName: "semen"}, function (data) {
            $('#follow_semen > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
            //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#follow_semen');
            $('#follow_semen').select2('val', Number(data)); // select the new term
            $('#follow_semen').select2('close'); // close the dropdown
        });
    });
//#######################################################################
    var postItr = $('#postItr').val();
    for (var i = 1; i <= postItr; i++) {
        $('#uop' + i).select2({
            width: '170px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                $id = $(this).attr('data-id');
                $thisitr = $(this).attr('data-itr');
                return "<input class='form-control' id='newuopTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "'   data-id='" + $id + "'><a href='#' id='adduopNew" + $thisitr + "' data-itr='" + $thisitr + "'  data-id='" + $id + "' class='btn btn-default adduopselected'>Add</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '.adduopselected', function () {
            $thisitr = $(this).attr('data-itr');
            var selectid = $(this).attr('data-id');
            var newitem = $('#newuopTerm' + $thisitr).val();
            $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=addnewoRecord', {selectedoperation: newitem, id: selectid, inline: 2, tableName: "follow_uop", colName: "uop"}, function (data) {
                $('select[name="uop"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                $('#uop' + $thisitr).select2('val', Number(data)); // select the new term
                $('#uop' + $thisitr).select2('close'); // close the dropdown
            });
        });
        $('#drain' + i).select2({
            width: '170px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                $id = $(this).attr('data-id');
                $thisitr = $(this).attr('data-itr');
                return "<input class='form-control' id='newdrainTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "'   data-id='" + $id + "'><a href='#' id='adddrainNew" + $thisitr + "' data-itr='" + $thisitr + "'  data-id='" + $id + "' class='btn btn-default adddrainselected'>Add</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '.adddrainselected', function () {
            $thisitr = $(this).attr('data-itr');
            var selectid = $(this).attr('data-id');
            var newitem = $('#newdrainTerm' + $thisitr).val();
            $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=addnewoRecord', {selectedoperation: newitem, id: selectid, inline: 2, tableName: "follow_drain", colName: "drain"}, function (data) {
                $('select[name="drain"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                $('#drain' + $thisitr).select2('val', Number(data)); // select the new term
                $('#drain' + $thisitr).select2('close'); // close the dropdown
            });
        });
        $('#vpack' + i).select2({
            width: '170px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                $id = $(this).attr('data-id');
                $thisitr = $(this).attr('data-itr');
                return "<input class='form-control' id='newvpackTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "'   data-id='" + $id + "'><a href='#' id='addvpackNew" + $thisitr + "' data-itr='" + $thisitr + "'  data-id='" + $id + "' class='btn btn-default addvpackselected'>Add</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '.addvpackselected', function () {
            $thisitr = $(this).attr('data-itr');
            var selectid = $(this).attr('data-id');
            var newitem = $('#newvpackTerm' + $thisitr).val();
            $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=addnewoRecord', {selectedoperation: newitem, id: selectid, inline: 2, tableName: "follow_vpack", colName: "vpack"}, function (data) {
                $('select[name="vpack"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                $('#vpack' + $thisitr).select2('val', Number(data)); // select the new term
                $('#vpack' + $thisitr).select2('close'); // close the dropdown
            });
        });
    }

//#######################################################################

    $('body').on('click', '#followAddTTT', function () {

        var id = $(this).attr('data-id');
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=followTTT', {id: id}, function (data) {
            $("#tttDisplay").html("");
            $('#tttDisplay').html(data);
            $("#addFollowTTT").modal('show');
        });
    });

    $('body').on('click', '#followShowTTT', function () {

        var id = $(this).attr('data-id');
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=showFllowTTT', {id: id}, function (data) {
            $("#tttDisplay").html("");
            $('#tttDisplay').html(data);
            $("#showFollowTTT").modal('show');
        });
    });

    $('body').on('change', '#docId', function () {
        var id = $(this).val();
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=getInsByDoctor', {id: id}, function (data) {
            $("#instDiv").html("");
            $('#instDiv').html(data);
        });
    });

    $('body').on('change', '.ttttttAll', function () {
        var groupClass = 'ttt';
        if ($(this).is(":checked")) {
            $('.' + groupClass).attr('checked', true);
            $('.' + groupClass).parent().addClass('checked');
        } else {
            $('.' + groupClass).removeAttr('checked');
            $('.' + groupClass).parent().removeClass('checked');
        }
    });

    $('body').on('change', '.ttt', function () {

        if ($(this).is(":checked")) {
            $(this).attr('checked', true);
            $(this).parent().addClass('checked');
        } else {
            $(this).removeAttr('checked');
            $(this).parent().removeClass('checked');
        }
    });

    $('body').on('click', '.printTTTInstruction', function () {
        var followupId = $('#followupId').val();
        var send = $(this).attr('data-send');
        var onlyOne = $(this).attr('data-onlyOne');
        var id = $(this).attr('data-intId');

        if (send == 1) {
            $('#sendLoading' + id).show();
            $('#sendDone' + id).hide();
            $('#sendError' + id).hide();
        }


        if (onlyOne == 1) {
            instructionArray.push($(this).attr('data-intId'));
        } else {
            $('.ttt').each(function () {
                if ($(this).parent().attr("class") === 'checked') {
                    instructionArray.push($(this).attr('data-intId'));
                }
            });
        }


        var jsonString = JSON.stringify(instructionArray);
        if ($('#forHusband').is(':checked')) {
            preFor = 1;
        } else {
            preFor = 0;
        }

        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=getprint', {
            jsonString: jsonString, preFor: preFor, send: send, followupId: followupId

        }, function (res) {
            if (send == 1) {
                if (Number(res) > 0) {
                    $('#sendLoading' + id).hide();
                    $('#sendDone' + id).show();
                    $('#sendError' + id).hide();
                } else {
                    $('#sendLoading' + id).hide();
                    $('#sendDone' + id).hide();
                    $('#sendError' + id).show();
                }
                instructionArray = [];
            } else {
                $('#instPrint').html("");
                $('#instPrint').html(res);
                $.print("#instPrint");
                $('#instPrint').html("");
                instructionArray = [];
            }
        });
    });

    $('body').on('click', '#addInsToFollow', function () {
        $("#addTTTInst").modal('show');
    });

    $('body').on('click', '#addTTTInstBtn', function () {

        CKEDITOR.instances.content.updateElement();
        var docId = $('#docAddId').val();
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=addTTTInst', $('#addTTTInstForm').serialize(), function (res) {
            if (Number(res) > 0) {
                CKEDITOR.instances.content.setData('');
                $('#docAddId').select2("val", '');
                $('#fileName').val('');
                $('#docId').select2("val", docId).trigger("change");
                $('#addTTTInst').modal('hide');
            }
        });
    });

    $('body').on('click', '.editInsToFollow', function () {
        var id = $(this).attr('data-id');
        var save = $(this).attr('data-save');
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=editTTTInst', {id: id, save: save}, function (data) {
            $("#editInsDiv").html("");
            $('#editInsDiv').html(data);
            $("#editTTTInst").modal('show');
        });
    });

    $('body').on('click', '.followInsDetails', function () {
        var id = $(this).attr('data-id');
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=followInsDetails', {id: id}, function (data) {
            var obj = JSON.parse(data);
            $('#showFileName').html(obj.fileName);
            $('#showContent').html(obj.content);
            $("#followInsDetails").modal('show');
        });
    });

    $('body').on('click', '#printFollowInstruction', function () {
        var followupId = $('#followupId').val();
        $('.ttt').each(function () {
            if ($(this).parent().attr("class") === 'checked') {
                instructionArray.push($(this).attr('data-intFId'));
            }
        });
        var jsonString = JSON.stringify(instructionArray);
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=getprintFollow', {
            jsonString: jsonString,
            followupId: followupId

        }, function (res) {
            $('#instPrint').html("");
            $('#instPrint').html(res);
            $.print("#instPrint");
            $('#instPrint').html("");
            instructionArray = [];
        });
    });

    $('body').on('click', '#updateTTTInstBtn', function () {

        CKEDITOR.instances.contentEdit.updateElement();
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=updateTTTInst', $('#editTTTInstForm').serialize(), function (res) {
            if (Number(res) > 0) {
                $('#docId').select2().trigger("change");
                $('#editTTTInst').modal('hide');
            }
        });
    });

    $('body').on('click', '#printInstWithoutSave ,  #sendInstWithoutSave', function () {
        var followupId = $('#followupId').val();
        var send = $(this).attr('data-send');
        $('#insFollowId').val(followupId);
        $('#insSend').val(send);

        if (send == 1) {
            $('#sendLoadingW').show();
            $('#sendDoneW').hide();
            $('#sendErrorW').hide();
        }

        CKEDITOR.instances.contentEdit.updateElement();
        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=printInstWithoutSave', $('#editTTTInstForm').serialize(), function (res) {
            if (send == 1) {
                if (Number(res) > 0) {
                    $('#sendLoadingW').hide();
                    $('#sendDoneW').show();
                    $('#sendErrorW').hide();
                } else {
                    $('#sendLoadingW').hide();
                    $('#sendDoneW').hide();
                    $('#sendErrorW').show();
                }
            } else {
                $('#instPrintE').html("");
                $('#instPrintE').html(res);
                $.print("#instPrintE");
                $('#instPrintE').html("");
            }
        });
    });

    $('body').on('click', '.sendFollowPre', function () {
        var id = $(this).attr('data-id');

        $('#sendLoading' + id).show();
        $('#sendDone' + id).hide();
        $('#sendError' + id).hide();

        $.post(hosturl + '/controllers/ovumpickupembryotrans.php?ac=sendFollowPre', {id: id}, function (res) {
            //   console.log(res);
            if (Number(res) == 1) {
                $('#sendLoading' + id).hide();
                $('#sendDone' + id).show();
                $('#sendError' + id).hide();
            } else {
                $('#sendLoading' + id).hide();
                $('#sendDone' + id).hide();
                $('#sendError' + id).show();
            }
        });
    });
}
