var embryoslabfuncion = function () {
    var hosturl = $('#hosturl').val();
    $(".bp_location").inputmask("(99,99,99,99)", {
        autoUnmask: true});
    var embryoslab = function () {
        $(".select2Lab").select2();
        if ($("input:radio[name='radio_type']:checked").val() == 0)
        {
            $('#scoringDiv').show()
            $('#transferDiv').hide();
            $('#freezingDiv').hide();
        }
        if ($("input:radio[name='radio_type']:checked").val() == 1) {
            $('#transferDiv').show()
            $('#scoringDiv').hide();
            $('#freezingDiv').hide();
        }
        if ($("input:radio[name='radio_type']:checked").val() == 2) {
            $('#freezingDiv').show()
            $('#scoringDiv').hide();
            $('#transferDiv').hide();
        }
//change type
        $('input[type=radio][name=radio_type]').change(function () {
            $typeis = $(this).val();
            if ($typeis == 0) {
                $('#scoringDiv').css({display: "block"});
                $('#transferDiv').css({display: "none"});
                $('#freezingDiv').css({display: "none"});
            }
            if ($typeis == 1) {
                $('#transferDiv').css({display: "block"});
                $('#scoringDiv').css({display: "none"});
                $('#freezingDiv').css({display: "none"});
            }

            if ($typeis == 2) {
                $('#freezingDiv').css({display: "block"});
                $('#scoringDiv').css({display: "none"});
                $('#transferDiv').css({display: "none"});
            }
        });
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        var hosturl = $('#hosturl').val();
        var patientId = $("#patientId").val();
//select Stage
        $(".selectStage").select2({
            width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newStageTerm' value='" + term + "'><a href='#' id='addNew1' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNew1', function () {
            /* add the new term */
            var stage = $('#newStageTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addstage", {stage: stage}, function (data) {

                $('.selectStage > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + stage + '</option>');
                //$('<option value="' + Number(data) + '" >' + education + '</option>').appendTo('#education');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.selectStage').select2('close'); // close the dropdown

                $('.update_item[id=' + thisId + ']').trigger("change");
                $('.update_item2[id=' + thisId + ']').trigger("change");
                $('.updateTransferTable[id=' + thisId + ']').trigger("change");
            });
        });
        //select Grade
        $(".selectGrade").select2({
            width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newGradeTerm' value='" + term + "'><a href='#' id='addNew2' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNew2', function () {
            /* add the new term */
            var grade = $('#newGradeTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addGrade", {grade: grade}, function (data) {
                $('.selectGrade > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + grade + '</option>');
                //$('<option value="' + Number(data) + '" >' + education + '</option>').appendTo('#education');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $(".selectGrade").select2('close'); // close the dropdown
                $('.update_item[id=' + thisId + ']').trigger("change");
                $('.update_item2[id=' + thisId + ']').trigger("change");
                $('.updateTransferTable[id=' + thisId + ']').trigger("change");
            });
        });
        ////////////////////////////////////////////////////////////// select2 of some of tables //////////////////////////////

        $(".selectTable").select2({
            width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                var tablename = $(this).attr("select-tableName");
                var main_table = $(this).attr("data-tableName");
                if (tablename == 'injection_embrologist' || tablename == 'growthmedia' || tablename == 'growthoil' || tablename == 'growthincubator') {
                    //   return '';
                } else {
                    return "<input class='form-control' id='newTableTerm' value='" + term + "'><a href='#' id='addNew' data-maintable='" + main_table + "' data-tablename='" + tablename + "' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                }
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNew', function () {
            /* add the new term */
            var name = $('#newTableTerm').val();
            var thisId = $(this).attr("data-id");
            var tablename = $(this).attr("data-tablename");
            var main_table = $(this).attr("data-maintable");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addToTable", {name: name, tablename: tablename}, function (data) {

                $('#' + thisId + ' > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + name + '</option>');
                //$('<option value="' + Number(data) + '" >' + education + '</option>').appendTo('#education');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.selectTable').select2('close'); // close the dropdown
                if (main_table === "embryoslab") {
                    $('.updateEmbryoLab[id=' + thisId + ']').trigger('change');
                } else if (main_table === "embryotransfer")
                {
                    $('.updateTransferTable[id=' + thisId + ']').trigger('change');
                }
            });
        });
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        $(".selectTable").live('change', function () {
// Do something wi.on('change', function () {
            var colName = "name";
            var tableName = $(this).attr("select-tableName");
            var data_selectid = $(this).attr("id");
            var thisvalue = $(this).val();
            if (tableName == 'growthmedia' || tableName == 'growthoil' || tableName == 'growthincubator') {
                var lastvalue;
                var values = new Array();
                $('#' + data_selectid + ' :selected').each(function (i, selected) {
                    values[i] = $(selected).val();
                    lastvalue = $(selected).val();
                });
                if (lastvalue == '-2') {
                    $("#" + data_selectid + " option[value='-2']").prop("selected", false);
                    $("#" + data_selectid + "").select2();
                    $.post(hosturl + '/controllers/embryoslab.php?ac=editModal', {tableName: tableName, colName: colName, data_selectid: data_selectid}, function (data) {
                        $('#' + data_selectid + '').select2("val", "");
                        $("#newitem_name").val('');
                        $("#newitem_id").val('');
                        $("#alldatadiv").html(data);
                        $("#edit_model").modal('show');
                    });
                }
            } else {
                if (thisvalue == '-2') {

                    $.post(hosturl + '/controllers/embryoslab.php?ac=editModal', {tableName: tableName, colName: colName, data_selectid: data_selectid}, function (data) {
                        $('#' + data_selectid + '').select2("val", "");
                        $("#newitem_name").val('');
                        $("#newitem_id").val('');
                        $("#alldatadiv").html(data);
                        $("#edit_model").modal('show');
                    });
                }
            }
        })

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        $(".selectStage").live('change', function () {
// Do something wi.on('change', function () {
            var colName = "stage"
            var tableName = $(this).attr("select-tableName");
            var data_selectid = $(this).attr("id");
            var thisvalue = $(this).val();
            if (thisvalue == '-2') {

                $.post(hosturl + '/controllers/embryoslab.php?ac=editModal', {tableName: tableName, colName: colName, data_selectid: data_selectid}, function (data) {
                    $('#' + data_selectid + '').select2("val", "");
                    $("#newitem_name").val('');
                    $("#newitem_id").val('');
                    $("#alldatadiv").html(data);
                    $("#edit_model").modal('show');
                });
            }

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

                $.post(hosturl + '/controllers/embryoslab.php?ac=editModal', {tableName: tableName, colName: colName, data_selectid: data_selectid}, function (data) {
                    $('#' + data_selectid + '').select2("val", "");
                    $("#newitem_name").val('');
                    $("#newitem_id").val('');
                    $("#alldatadiv").html(data);
                    $("#edit_model").modal('show');
                });
            }

        })

        //////////////////////////////////////////////// add new row in the modal ////////////////////////////////////////////////
        $("#addnewRowEmb").live('click', function () {
            var tablename = $(this).attr('data-tableName');
            var selectname = $(this).attr('data-selectClass');
            var colName = $("#colName").val();
            $.post(hosturl + "/controllers/embryoslab.php?ac=addNewRow", {tablename: tablename, colName: colName}, function (data) {

                var dataid = Number(data);
                $('.' + selectname + ' > .deleteoptincompo').before('<option value="' + dataid + '" ></option>');
                $("#dtAddTbl").prepend('<tr>'
                        + '<td>'
                        + ' <a href="#" >'
                        + '<input type="text" id="newitem_name"  class="form-control"   data-id="' + dataid + '"  value="" name="newamount_name"/>'
                        + '</a>'
                        + '</td>'
                        + '<td>'
                        + '<a class="btn  btn-xs red  deleteModalEmb wds100"  data-tableName="' + tablename + '"  data-id="' + dataid + '" ><i class="fa fa-times" ></i></a>'
                        + '</td>'
                        + '</tr>'
                        );
            });
        });
// edit the new row of modal
        $("#newitem_name").live('change', function ()
        {

            var value = $(this).val();
            var id = $(this).attr("data-id");
            var data_selectid = $("#data_selectid").val();
            var tablename = $("#tablename").val();
            var patientid = $("#patientid").val();
            var colName = $("#colName").val();
            if (id != '') {
                $.post(hosturl + '/controllers/embryoslab.php?ac=updateModal', {tablename: tablename, colName: colName, id: id, value: value, patientid: patientid}, function (data) {
                    if (tablename === "stage") {
                        $(".selectStage option[value='" + id + "']").text(value);
                        $(".selectStageFreeze option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "grade") {
                        $(".selectGrade option[value='" + id + "']").text(value);
                        $(".selectGradeFreeze option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "embryo_emb") {
                        $(".selectEmb option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id);
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "sperum_source") {
                        $(".selectSpSource option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "semen_process") {
                        $(".selectSemenProcess option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "cancel_reason") {
                        $(".selectCancelReason option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "embryo_result") {
                        $(".selectRes option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id);
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "embryo_lot_select") {
                        $(".selectLotSelect option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "embryo_catheter_select") {
                        $(".selectCatherSelect option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "embryo_device") {
                        $(".selectDevice option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "embryo_tank") {
                        $(".selectTank option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "embryo_kit_type") {
                        $(".selectKitType option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "treatprotocol") {
                        $(".treatProtocol option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "varic_select") {
                        $(".varicSelect option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "asp_select") {
                        $(".aspSelect option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "tb_select") {
                        $(".tbSelect option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else if (tablename === "mtb_select") {
                        $(".mtbSelect option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    } else {
                        $("#" + data_selectid + " option[value='" + id + "']").text(value);
                        $('#' + data_selectid + '').select2('val', id).trigger("change");
//                        $('#edit_model').modal('hide');
                    }
                });
                if (tablename == 'growthmedia' || tablename == 'growthoil' || tablename == 'growthincubator') {
                    $('.' + tablename).each(function () {
                        $(this).children('option[value = ' + id + ']').text(value);
                        if ($(this).val() == id) {
                            $(this).select2("val", id);
                        } else if ($(this).val() == -2) {
                            $(this).select2("val", '');
                        }
                    });
                    $('select').select2();
                } else {
                    $('.update_item[id=' + id + ']').trigger("change");
                    $('.update_item2[id=' + id + ']').trigger("change");
                    $('.updateItemH[id=' + id + ']').trigger("change");
                    $('.updateTransferTable[id=' + id + ']').trigger("change");
                }


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

        $('.deleteModalEmb').live('click', function () {
            var id = $(this).attr('data-id');
            var data_selectid = $("#data_selectid").val();
            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/embryoslab.php?ac=delRowsommedel', {id: id, tableName: tablename}, function (res) {

                                if (res == 1)
                                {
                                    tr.parent().remove();
                                    if (tablename === "stage") {
                                        $(".selectStage option[value='" + id + "']").remove();
                                    } else if (tablename === "grade") {
                                        $(".selectGrade option[value='" + id + "']").remove();
                                    } else if (tablename === "embryo_emb") {
                                        $(".selectEmb option[value='" + id + "']").remove();
                                    } else if (tablename === "sperum_source") {
                                        $(".selectSpSource option[value='" + id + "']").remove();
                                    } else if (tablename === "semen_process") {
                                        $(".selectSemenProcess option[value='" + id + "']").remove();
                                    } else if (tablename === "cancel_reason") {
                                        $(".selectCancelReason option[value='" + id + "']").remove();
                                    } else if (tablename === "embryo_result") {
                                        $(".selectRes option[value='" + id + "']").remove();
                                    } else {

                                        $("#" + data_selectid + " option[value='" + id + "']").remove();
                                    }
                                } else
                                {
                                    alert("!!!!!!!!!!!!! some thing wrong here");
                                }
                            });
                        }
                    }
                }
            }).find('.modal-content').addClass('modal-sm');
        });
////////////////////////////////////////////// update embryoslab table ///////////////////////////////////////////////////////
// edit the new row of modal
        $(".updateEmbryoLab").live('change', function ()
        {

            var value = $(this).val();
            var colName = $(this).attr("data-colName");
            var tablename = $(this).attr("data-tableName");
            var patientid = $("#patientid").val();
            var row_id = $("#row_id").val();
            var date = $("#date").val();
            var ref_by_dr_id = $("#ref_by_dr_id").val();
            var data_selectid = $(this).attr("id");
            var multiple = 0;
            if ($(this).attr('multiple')) {
                var values = new Array();
                $('#' + data_selectid + ' :selected').each(function (i, selected) {
                    values[i] = $(selected).val();
                });
                value = JSON.stringify(values);
                multiple = 1;
            }


            $.post(hosturl + '/controllers/embryoslab.php?ac=updateit',
                    {tablename: tablename, colName: colName, value: value, patientid: patientid
                        , row_id: row_id, date: date, ref_by_dr_id: ref_by_dr_id, multiple: multiple}, function (data) {


                $("#row_id").val(data);
            });
        });
        $(".updateEmbryoLab_thawing").live('change', function ()
        {

            var value = $(this).val();
            var colName = $(this).attr("data-colName");
            var tablename = $(this).attr("data-tableName");
            var table_row_id = $(this).attr("data-tableRow");
            $.post(hosturl + '/controllers/embryoslab.php?ac=updateitthawing', {tablename: tablename, colName: colName, value: value, table_row_id: table_row_id}, function (data) {


                $("#row_id").val(data);
            });
        });
        //////////////////////////////////////////////// add new row in the Table embryoscoring ////////////////////////////////////////////////
        $("#addnewRowscoring").live('click', function () {
            var tablename = $(this).attr('data-tableName');
            var row_id = $("#row_id").val();
            var myItra = Number($("#myItra").val());
            myItra++;
            $("#myItra").val(myItra);
            $.post(hosturl + "/controllers/embryoslab.php?ac=insertNewRow", {tablename: tablename, row_id: row_id, myItra: myItra}, function (data) {

                $("#scoringTable").append(data);
                $("select.selectStage").select2({
                    width: '240px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        var thisId = $(this).attr("id");
                        return "<input class='form-control' id='newStageTerm' value='" + term + "'><a href='#' id='addNew1' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '#addNew1', function () {
                    /* add the new term */
                    var stage = $('#newStageTerm').val();
                    var thisId = $(this).attr("data-id");
                    $.post(hosturl + "/controllers/embryoslab.php?ac=addstage", {stage: stage}, function (data) {

                        $('.selectStage > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + stage + '</option>');
                        //$('<option value="' + Number(data) + '" >' + education + '</option>').appendTo('#education');
                        $('#' + thisId + '').select2('val', Number(data)); // select the new term
                        $('.selectStage').select2('close'); // close the dropdown

                        $('.update_item[id=' + thisId + ']').trigger("change");
                        $('.update_item2[id=' + thisId + ']').trigger("change");
                        $('.updateTransferTable[id=' + thisId + ']').trigger("change");
                    });
                });
                //select Grade

                $("select.selectGrade").select2({
                    width: '240px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        var thisId = $(this).attr("id");
                        return "<input class='form-control' id='newGradeTerm' value='" + term + "'><a href='#' id='addNew2' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '#addNew2', function () {
                    /* add the new term */
                    var grade = $('#newGradeTerm').val();
                    var thisId = $(this).attr("data-id");
                    $.post(hosturl + "/controllers/embryoslab.php?ac=addGrade", {grade: grade}, function (data) {
                        $('.selectGrade > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + grade + '</option>');
                        //$('<option value="' + Number(data) + '" >' + education + '</option>').appendTo('#education');
                        $('#' + thisId + '').select2('val', Number(data)); // select the new term
                        $(".selectGrade").select2('close'); // close the dropdown
                        $('.update_item[id=' + thisId + ']').trigger("change");
                        $('.update_item2[id=' + thisId + ']').trigger("change");
                        $('.updateTransferTable[id=' + thisId + ']').trigger("change");
                    });
                });
            });
        });
        //////////////////////////////////////////////// add new row in the Table embryofreezing ////////////////////////////////////////////////
        $("#addnewRowfreezing").live('click', function () {
            var tablename = $(this).attr('data-tableName');
            var row_id = $("#row_id").val();
            var Itr = Number($("#Itr").val());
            Itr++;
            $("#Itr").val(Itr);
            $.post(hosturl + "/controllers/embryoslab.php?ac=insertNewFreezing", {tablename: tablename, row_id: row_id, Itr: Itr}, function (data) {

                $("#freezingTable").append(data);
                $("select.selectStage").select2({
                    width: '240px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        var thisId = $(this).attr("id");
                        return "<input class='form-control' id='newStageTerm' value='" + term + "'><a href='#' id='addNew1' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '#addNew1', function () {
                    /* add the new term */
                    var stage = $('#newStageTerm').val();
                    var thisId = $(this).attr("data-id");
                    $.post(hosturl + "/controllers/embryoslab.php?ac=addstage", {stage: stage}, function (data) {

                        $('.selectStage > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + stage + '</option>');
                        //$('<option value="' + Number(data) + '" >' + education + '</option>').appendTo('#education');
                        $('#' + thisId + '').select2('val', Number(data)); // select the new term
                        $('.selectStage').select2('close'); // close the dropdown

                        $('.update_item[id=' + thisId + ']').trigger("change");
                        $('.update_item2[id=' + thisId + ']').trigger("change");
                        $('.updateTransferTable[id=' + thisId + ']').trigger("change");
                    });
                });
                //select Grade
                $("select.selectGrade").select2({
                    width: '240px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        var thisId = $(this).attr("id");
                        return "<input class='form-control' id='newGradeTerm' value='" + term + "'><a href='#' id='addNew2' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '#addNew2', function () {
                    /* add the new term */
                    var grade = $('#newGradeTerm').val();
                    var thisId = $(this).attr("data-id");
                    $.post(hosturl + "/controllers/embryoslab.php?ac=addGrade", {grade: grade}, function (data) {
                        $('.selectGrade > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + grade + '</option>');
                        //$('<option value="' + Number(data) + '" >' + education + '</option>').appendTo('#education');
                        $('#' + thisId + '').select2('val', Number(data)); // select the new term
                        $(".selectGrade").select2('close'); // close the dropdown
                        $('.update_item[id=' + thisId + ']').trigger("change");
                        $('.update_item2[id=' + thisId + ']').trigger("change");
                        $('.updateTransferTable[id=' + thisId + ']').trigger("change");
                    });
                });
            });
        });
////////////////////////////////////////////// update embryoscoring table ///////////////////////////////////////////////////////
// edit the new row of modal
        $(".update_item").live('change', function ()
        {

            var value = $(this).val();
            var colName = $(this).attr("data-colName");
            var tablename = $("#addnewRowscoring").attr('data-tableName');
            var patientid = $("#patientid").val();
            var row_id = $(this).attr("data-rowid");
            var data_selectid = $(this).attr("id");
            var multiple = 0;
            if ($(this).attr('multiple')) {
                var values = new Array();
                $('#' + data_selectid + ' :selected').each(function (i, selected) {
                    values[i] = $(selected).val();
                });
                value = JSON.stringify(values);
                multiple = 1;
            }
            $.post(hosturl + '/controllers/embryoslab.php?ac=updateRow',
                    {tablename: tablename, colName: colName, value: value, patientid: patientid, row_id: row_id, multiple: multiple}, function (data) {
                console.log(data);
            });
        });
        $(".update_itemS").live('change', function ()
        {

            var value = $(this).val();
            var colName = $(this).attr("data-colName");
            var tablename = $(this).attr('data-tableName');
            var patientid = $("#patientid").val();
            var row_id = $(this).attr("data-rowid");
            var data_selectid = $(this).attr("id");
            var multiple = 0;
            if ($(this).attr('multiple')) {
                var values = new Array();
                $('#' + data_selectid + ' :selected').each(function (i, selected) {
                    values[i] = $(selected).val();
                });
                value = JSON.stringify(values);
                multiple = 1;
            }
            $.post(hosturl + '/controllers/embryoslab.php?ac=updateRow',
                    {tablename: tablename, colName: colName, value: value, patientid: patientid, row_id: row_id, multiple: multiple}, function (data) {
                console.log(data);
            });
        });
        ////////////////////////////////////////////// update embryoFreezing table ///////////////////////////////////////////////////////
// edit the new row of modal
        $(".update_item2").live('change', function () {

            var value = $(this).val();
            var colName = $(this).attr("data-colName");
            var tablename = $(this).attr('data-tableName');
            var patientid = $("#patientid").val();
            var row_id = $(this).attr("data-rowid");
            $.post(hosturl + '/controllers/embryoslab.php?ac=updateRow', {tablename: tablename, colName: colName, value: value, patientid: patientid, row_id: row_id}, function (data) {
                console.log(data);
            });
        });
        ///////////////////////////////////////////////////// delete row from embryoscoring table////////////////////////

        $('.delFromTable').live('click', function () {
            var id = $(this).attr('data-rowid');
            var tablename = $("#addnewRowscoring").attr('data-tableName');
            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 (event) {
                            event.preventDefault();
                            $.post(hosturl + '/controllers/embryoslab.php?ac=delRowFromTable', {id: id, tableName: tablename}, function (res) {

                                tr.parent().remove();
                            });
                        }
                    }
                }
            }).find('.modal-content').addClass('modal-sm');
        });
        ///////////////////////////////////////////////////// delete row from embryofreezing table////////////////////////

        $('.delFromTableFreezing').live('click', function () {
            var id = $(this).attr('data-rowid');
            var tablename = $("#addnewRowfreezing").attr('data-tableName');
            var tr = $(this).parent();
            var item_this = $(this);
            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 (event) {
                            event.preventDefault();
                            $.post(hosturl + '/controllers/embryoslab.php?ac=delRowFromTable', {id: id, tableName: tablename}, function (res) {
                                item_this.closest('tr').next().remove();
                                tr.parent().remove();
                            });
                        }
                    }
                }
            }).find('.modal-content').addClass('modal-sm');
        });
        ////////////////////////////////////////////// update embryoslab table ///////////////////////////////////////////////////////
// edit the new row of modal
        $(".updateTransferTable").live('change', function ()
        {
            var value = $(this).val();
            var colName = $(this).attr("data-colName");
            var tablename = $(this).attr("data-tableName");
            var patientid = $("#patientid").val();
            var row_id = $("#row_id").val();
            $.post(hosturl + '/controllers/embryoslab.php?ac=updateTransferTable', {tablename: tablename, colName: colName, value: value, patientid: patientid, row_id: row_id}, function (data) {

            });
        });
        ///////////////////////////////////////////////// print embryoslab data    ///////////////////////////////////////////////

        $(".printembryoslab").live('click', function () {
            var id = $(this).attr('data-intId');
            var datapatientId = $(this).attr('data-patientId');
            $.post(hosturl + '/controllers/embryoslab.php?ac=getprint', {id: id, patientid: datapatientId}, function (data)
            {
                $("#printDiv").html(data);
                $.print("#printDiv");
                $("#printDiv").html('');
            });
        });
        $('body').on('click', '#addNewthawingRow', function () {

            var itr = $('#thawitr').val();
//        var patientId = $('#patientId').val();
            var newItr = Number(itr) + 1;
            var tableName = $(this).attr("data-tableName");
            var lab_id = $('#embryoslab').val();
            $('#thawitr').val(newItr);
            $.post(hosturl + '/controllers/embryoslab.php?ac=drawRow', {
                type: "thawing",
                itr: newItr,
                tableName: tableName,
                lab_id: lab_id
            }, function (res) {
//            alert(res)
                $('#thawingContent').append(res);
                $("#clinician_" + newItr + "_9").select2();
                $("#embryol_" + newItr + "_8").select2();
                /* customize the no matches output */

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


                    var selectedcathetr = $("#newddTerm").val();
                    var data_itra = $(this).attr('data-itra');
                    $.post(hosturl + '/controllers/embryofreezing.php?ac=addCathetr', {selectedcathetr: selectedcathetr}, function (data) {
                        $('.allupdateselect > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + selectedcathetr + '</option>');
                        //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#childj');
                        $('#cathetr_' + data_itra + '').select2('val', Number(data)); // select the new term
                        $('#cathetr_' + data_itra + '').select2('close'); // close the dropdown
                    });
                });
                $("select.cathetrselect").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();
                    var data_itra = $(this).attr('data-itra');
                    if (thisvalue == '-2') {

                        $.post(hosturl + '/controllers/embryofreezing.php?ac=editModal', {tableName: tableName, colName: colName}, function (data) {
                            $('#cathetr_' + data_itra + '').select2("val", "");
                            $("#alldatadiv").html(data);
                            $("#edit_model").modal('show');
                        });
                    }

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

            var selectedcathetr = $("#newddTerm").val();
            var data_itra = $(this).attr('data-itra');
            $.post(hosturl + '/controllers/embryofreezing.php?ac=addCathetr', {selectedcathetr: selectedcathetr}, function (data) {
                $('.allupdateselect > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + selectedcathetr + '</option>');
                //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#childj');
                $('#cathetr_' + data_itra + '').select2('val', Number(data)); // select the new term
                $('#cathetr_' + data_itra + '').select2('close'); // close the dropdown
            });
        });
        $("select.cathetrselect").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();
            var data_itra = $(this).attr('data-itra');
            if (thisvalue == '-2') {

                $.post(hosturl + '/controllers/embryofreezing.php?ac=editModal', {tableName: tableName, colName: colName}, function (data) {
                    $('#cathetr_' + data_itra + '').select2("val", "");
                    $("#alldatadiv").html(data);
                    $("#edit_model").modal('show');
                });
            }

        })


        $(".updateItemH").live('change', function () {
            var value = $(this).val();
            var colName = $(this).attr("data-colName");
            var tablename = $(this).attr('data-tableName');
            var row_id = $(this).attr("data-rowid");
            $.post(hosturl + '/controllers/embryoslab.php?ac=updateRow',
                    {tablename: tablename, colName: colName, value: value, row_id: row_id}, function (data) {
            });
        });
        $('.delFromTableH').live('click', function () {
            var value = 1;
            var colName = $(this).attr("data-colName");
            var tablename = $(this).attr('data-tableName');
            var row_id = $(this).attr("data-rowid");
            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 (event) {
                            event.preventDefault();
                            $.post(hosturl + '/controllers/embryoslab.php?ac=updateRow',
                                    {tablename: tablename, colName: colName, value: value, row_id: row_id}, function (res) {
                                tr.parent().remove();
                            });
                        }
                    }
                }
            }).find('.modal-content').addClass('modal-sm');
        });
        $(".selectEmb").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newEmbTerm' value='" + term + "'><a href='#' id='addNewEmb' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewEmb', function () {
            /* add the new term */
            var Emb = $('#newEmbTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: Emb, table: 'embryo_emb'}, function (data) {

                $('.selectEmb > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + Emb + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.selectEmb').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });
        $(".selectRes").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newResTerm' value='" + term + "'><a href='#' id='addNewRes' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewRes', function () {
            /* add the new term */
            var Res = $('#newResTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: Res, table: 'embryo_result'}, function (data) {

                $('.selectRes > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + Res + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.selectRes').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });
        $(".selectLotSelect").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newLotSelectTerm' value='" + term + "'><a href='#' id='addNewLotSelect' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewLotSelect', function () {
            /* add the new term */
            var LotSelect = $('#newLotSelectTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: LotSelect, table: 'embryo_lot_select'}, function (data) {

                $('.selectLotSelect > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + LotSelect + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.selectLotSelect').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });
        $(".selectCatherSelect").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newCatherSelectTerm' value='" + term + "'><a href='#' id='addNewCatherSelect' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewCatherSelect', function () {
            /* add the new term */
            var CatherSelect = $('#newCatherSelectTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: CatherSelect, table: 'embryo_catheter_select'}, function (data) {

                $('.selectCatherSelect > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + CatherSelect + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.selectCatherSelect').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
                $('.updateElement[id=' + thisId + ']').trigger("change");
            });
        });
        $(".selectDevice").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newDeviceTerm' value='" + term + "'><a href='#' id='addNewDevice' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewDevice', function () {
            /* add the new term */
            var Device = $('#newDeviceTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: Device, table: 'embryo_device'}, function (data) {

                $('.selectDevice > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + Device + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.selectDevice').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });
        $(".selectTank").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newTankTerm' value='" + term + "'><a href='#' id='addNewTank' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewTank', function () {
            /* add the new term */
            var Tank = $('#newTankTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: Tank, table: 'embryo_tank'}, function (data) {

                $('.selectTank > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + Tank + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.selectTank').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });
        $(".selectKitType").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newKitTypeTerm' value='" + term + "'><a href='#' id='addNewKitType' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewKitType', function () {
            /* add the new term */
            var KitType = $('#newKitTypeTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: KitType, table: 'embryo_kit_type'}, function (data) {

                $('.selectKitType > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + KitType + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.selectKitType').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });
        $(".selectRes , .selectEmb , .selectLotSelect, .selectCatherSelect, .selectDevice, .selectTank\n\
                , .selectKitType , .varicSelect , .treatProtocol , .aspSelect , .tbSelect , .mtbSelect \n\
                , .selectSpSource   , .selectSemenProcess , .selectCancelReason").live('change', function () {
// Do something wi.on('change', function () {
            var colName = "name"
            var tableName = $(this).attr("select-tableName");
            var data_selectid = $(this).attr("id");
            var thisvalue = $(this).val();
            if (thisvalue == '-2') {

                $.post(hosturl + '/controllers/embryoslab.php?ac=editModal', {tableName: tableName, colName: colName, data_selectid: data_selectid}, function (data) {
                    $('#' + data_selectid + '').select2("val", "");
                    $("#newitem_name").val('');
                    $("#newitem_id").val('');
                    $("#alldatadiv").html(data);
                    $("#edit_model").modal('show');
                });
            }

        })

        $("#addnewRowSelection").live('click', function () {
            var tablename = $(this).attr('data-tableName');
            var row_id = $("#row_id").val();
            var selectItr = Number($("#selectItr").val());
            selectItr++;
            $("#selectItr").val(selectItr);
            $.post(hosturl + "/controllers/embryoslab.php?ac=insertNewRow",
                    {tablename: tablename, row_id: row_id, myItra: selectItr}, function (data) {
                $("#selectionTable").append(data);
                $("select.selectEmb").select2({
                    //   width: '240px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        var thisId = $(this).attr("id");
                        return "<input class='form-control' id='newEmbTerm' value='" + term + "'><a href='#' id='addNewEmb' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '#addNewEmb', function () {
                    /* add the new term */
                    var Emb = $('#newEmbTerm').val();
                    var thisId = $(this).attr("data-id");
                    $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: Emb, table: 'embryo_emb'}, function (data) {

                        $('.selectEmb > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + Emb + '</option>');
                        $('#' + thisId + '').select2('val', Number(data)); // select the new term
                        $('.selectEmb').select2('close'); // close the dropdown

                        $('.updateItemH[id=' + thisId + ']').trigger("change");
                    });
                });
                $("select.selectRes").select2({
                    //   width: '240px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        var thisId = $(this).attr("id");
                        return "<input class='form-control' id='newResTerm' value='" + term + "'><a href='#' id='addNewRes' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '#addNewRes', function () {
                    /* add the new term */
                    var Res = $('#newResTerm').val();
                    var thisId = $(this).attr("data-id");
                    $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: Res, table: 'embryo_result'}, function (data) {

                        $('.selectRes > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + Res + '</option>');
                        $('#' + thisId + '').select2('val', Number(data)); // select the new term
                        $('.selectRes').select2('close'); // close the dropdown

                        $('.updateItemH[id=' + thisId + ']').trigger("change");
                    });
                });
            });
        });
        $(".varicSelect").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newVaricSelectTerm' value='" + term + "'><a href='#' id='addNewVaricSelect' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewVaricSelect', function () {
            /* add the new term */
            var Varic = $('#newVaricSelectTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: Varic, table: 'varic_select'}, function (data) {

                $('.varicSelect > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + Varic + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.varicSelect').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });
        $(".aspSelect").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newaspSelectTerm' value='" + term + "'><a href='#' id='addNewaspSelect' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewaspSelect', function () {
            /* add the new term */
            var Varic = $('#newaspSelectTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: Varic, table: 'asp_select'}, function (data) {

                $('.aspSelect > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + Varic + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.aspSelect').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });
        $(".tbSelect").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newtbSelectTerm' value='" + term + "'><a href='#' id='addNewtbSelect' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewtbSelect', function () {
            /* add the new term */
            var Varic = $('#newtbSelectTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: Varic, table: 'tb_select'}, function (data) {

                $('.tbSelect > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + Varic + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.tbSelect').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });
        $(".mtbSelect").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newmtbSelectTerm' value='" + term + "'><a href='#' id='addNewmtbSelect' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewmtbSelect', function () {
            /* add the new term */
            var Varic = $('#newmtbSelectTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: Varic, table: 'mtb_select'}, function (data) {

                $('.mtbSelect > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + Varic + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.mtbSelect').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });
        $(".selectSpSource").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newSpSourceTerm' value='" + term + "'><a href='#' id='addNewSpSource' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewSpSource', function () {
            /* add the new term */
            var SpSource = $('#newSpSourceTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: SpSource, table: 'sperum_source'}, function (data) {

                $('.selectSpSource > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + SpSource + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.selectSpSource').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });
        $(".selectSemenProcess").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newSemenProcessTerm' value='" + term + "'><a href='#' id='addNewSemenProcess' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewSemenProcess', function () {
            /* add the new term */
            var SemenProcess = $('#newSemenProcessTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: SemenProcess, table: 'semen_process'}, function (data) {

                $('.selectSemenProcess > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + SemenProcess + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.selectSemenProcess').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });
        $(".selectCancelReason").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newCancelReasonTerm' value='" + term + "'><a href='#' id='addNewCancelReason' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewCancelReason', function () {
            /* add the new term */
            var CancelReason = $('#newCancelReasonTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: CancelReason, table: 'cancel_reason'}, function (data) {

                $('.selectCancelReason > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + CancelReason + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.selectCancelReason').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });

        $('body').on('change', '.fertilized', function () {
            var itr = $('#fertilized').val();
            var embryoslab_id = $("#embryoslab").val();
            //          if (itr > 0) {
            $.post(hosturl + '/controllers/embryoslab.php?ac=fertilizedData', {itr: itr, embryoslab_id: embryoslab_id}, function (data) {
                $("#scoringDaysDiv").html('');
                $("#scoringDaysDiv").html(data);
                $("select.selectStage").select2({
                    width: '240px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        var thisId = $(this).attr("id");
                        return "<input class='form-control' id='newStageTerm' value='" + term + "'><a href='#' id='addNew1' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '#addNew1', function () {
                    /* add the new term */
                    var stage = $('#newStageTerm').val();
                    var thisId = $(this).attr("data-id");
                    $.post(hosturl + "/controllers/embryoslab.php?ac=addstage", {stage: stage}, function (data) {

                        $('.selectStage > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + stage + '</option>');
                        //$('<option value="' + Number(data) + '" >' + education + '</option>').appendTo('#education');
                        $('#' + thisId + '').select2('val', Number(data)); // select the new term
                        $('.selectStage').select2('close'); // close the dropdown

                        $('.update_item[id=' + thisId + ']').trigger("change");
                        $('.update_item2[id=' + thisId + ']').trigger("change");
                        $('.updateTransferTable[id=' + thisId + ']').trigger("change");
                    });
                });
                $("select.selectGrade").select2({
                    width: '240px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        var thisId = $(this).attr("id");
                        return "<input class='form-control' id='newGradeTerm' value='" + term + "'><a href='#' id='addNew2' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '#addNew2', function () {
                    /* add the new term */
                    var grade = $('#newGradeTerm').val();
                    var thisId = $(this).attr("data-id");
                    $.post(hosturl + "/controllers/embryoslab.php?ac=addGrade", {grade: grade}, function (data) {
                        $('.selectGrade > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + grade + '</option>');
                        //$('<option value="' + Number(data) + '" >' + education + '</option>').appendTo('#education');
                        $('#' + thisId + '').select2('val', Number(data)); // select the new term
                        $(".selectGrade").select2('close'); // close the dropdown
                        $('.update_item[id=' + thisId + ']').trigger("change");
                        $('.update_item2[id=' + thisId + ']').trigger("change");
                        $('.updateTransferTable[id=' + thisId + ']').trigger("change");
                    });
                });
                $(".select2Lab").select2();

                $('#fromRow').attr('max', itr);
                $('#toRow').attr('max', itr);
                $('#saveScoringData').attr('data-rowsCount', itr);
            });
            //     }
        });

        $('body').on('change', '.straws', function () {
            var itr = $('#strawsNo').val();
            var embryoslab_id = $("#embryoslab").val();
            //   if (itr > 0) {
            $.post(hosturl + '/controllers/embryoslab.php?ac=strawsData', {itr: itr, embryoslab_id: embryoslab_id}, function (data) {
                $("#freezingTable").html('');
                $("#freezingTable").html(data);

                $("select.selectStage").select2({
                    width: '240px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        var thisId = $(this).attr("id");
                        return "<input class='form-control' id='newStageTerm' value='" + term + "'><a href='#' id='addNew1' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '#addNew1', function () {
                    /* add the new term */
                    var stage = $('#newStageTerm').val();
                    var thisId = $(this).attr("data-id");
                    $.post(hosturl + "/controllers/embryoslab.php?ac=addstage", {stage: stage}, function (data) {

                        $('.selectStage > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + stage + '</option>');
                        //$('<option value="' + Number(data) + '" >' + education + '</option>').appendTo('#education');
                        $('#' + thisId + '').select2('val', Number(data)); // select the new term
                        $('.selectStage').select2('close'); // close the dropdown

                        $('.update_item[id=' + thisId + ']').trigger("change");
                        $('.update_item2[id=' + thisId + ']').trigger("change");
                        $('.updateTransferTable[id=' + thisId + ']').trigger("change");
                    });
                });
                //select Grade
                $("select.selectGrade").select2({
                    width: '240px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        var thisId = $(this).attr("id");
                        return "<input class='form-control' id='newGradeTerm' value='" + term + "'><a href='#' id='addNew2' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '#addNew2', function () {
                    /* add the new term */
                    var grade = $('#newGradeTerm').val();
                    var thisId = $(this).attr("data-id");
                    $.post(hosturl + "/controllers/embryoslab.php?ac=addGrade", {grade: grade}, function (data) {
                        $('.selectGrade > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + grade + '</option>');
                        //$('<option value="' + Number(data) + '" >' + education + '</option>').appendTo('#education');
                        $('#' + thisId + '').select2('val', Number(data)); // select the new term
                        $(".selectGrade").select2('close'); // close the dropdown
                        $('.update_item[id=' + thisId + ']').trigger("change");
                        $('.update_item2[id=' + thisId + ']').trigger("change");
                        $('.updateTransferTable[id=' + thisId + ']').trigger("change");
                    });
                });

            });
            //     }
        });

        $('body').on('change', '.et_day', function () {
            var target = $(this).attr('data-target');
            var day = $(this).val();
            var embryoslab_id = $("#embryoslab").val();
            $.post(hosturl + '/controllers/embryoslab.php?ac=getDate', {day: day, embryoslab_id: embryoslab_id}, function (data) {
                $("#" + target).val(data);
                $("#" + target).trigger("change");
//                $('.updateTransferTable[id=' + target + ']').trigger("change");
            });
        });

        $('body').on('click', '#cancelSelection', function () {
            var value = 1;
            var colName = 'cancelled';
            var tablename = 'embryoslab';
            var row_id = $("#embryoslab").val();
            $.post(hosturl + '/controllers/embryoslab.php?ac=updateit',
                    {tablename: tablename, colName: colName, value: value, row_id: row_id}, function (data) {
                $("#cancelSelection").attr('disabled', true);
                $("#ReverseCancelSelection").show();
                $("#cancelReasonContainer").show();
                $("#rowCancelTab").tabs({});
                $("#rowCancelTab").tabs("option", "active", 0);
                $("#rowCancelTab").tabs("option", "disabled", [1]);
            });
        });

        $('body').on('click', '#ReverseCancelSelection', function () {
            var value = 0;
            var colName = 'cancelled';
            var tablename = 'embryoslab';
            var row_id = $("#embryoslab").val();
            $.post(hosturl + '/controllers/embryoslab.php?ac=updateit',
                    {tablename: tablename, colName: colName, value: value, row_id: row_id}, function (data) {
                $("#cancelSelection").attr('disabled', false);
                $("#ReverseCancelSelection").hide();
                $("#cancelReasonContainer").hide();
                $("#rowCancelTab").tabs({});
                $("#rowCancelTab").tabs("option", "active", 0);
                $("#rowCancelTab").tabs("option", "disabled", []);
            });
        });

        if ($("#isCancelled").val() == 1) {
            $("#rowCancelTab").tabs({});
            $("#rowCancelTab").tabs("option", "active", 0);
            $("#rowCancelTab").tabs("option", "disabled", [1]);
        }

        $(".selectinjection").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newinjectionTerm' value='" + term + "'><a href='#' id='addNewinjection' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewinjection', function () {
            /* add the new term */
            var injection = $('#newinjectionTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: injection, table: 'injection'}, function (data) {

                $('.selectinjection > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + injection + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.selectinjection').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });
        $(".selectinjection_location").select2({
            //   width: '240px',
            allowClear: true,
            formatNoMatches: function (term) {
                /* customize the no matches output */
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newinjection_locationTerm' value='" + term + "'><a href='#' id='addNewinjection_location' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNewinjection_location', function () {
            /* add the new term */
            var injection_location = $('#newinjection_locationTerm').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: injection_location, table: 'injection_location'}, function (data) {

                $('.selectinjection_location > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + injection_location + '</option>');
                $('#' + thisId + '').select2('val', Number(data)); // select the new term
                $('.selectinjection_location').select2('close'); // close the dropdown

                $('.updateItemH[id=' + thisId + ']').trigger("change");
            });
        });

        $('body').on('keyup', '.oocyteCollected', function () {
            var miiO = Number($('#miiO').val());
            var miO = Number($('#miO').val());
            var gvO = Number($('#gvO').val());
            var atreticO = Number($('#atreticO').val());
            var other1O = Number($('#other1O').val());

            var sum = miiO + miO + gvO + atreticO + other1O;
            $('#oocyteCollected').val(sum).trigger('change');
        });

        $('body').on('click', '#tankLocationBtn', function () {
            $.post(hosturl + '/controllers/embryoslab.php?ac=tankLocation', function (data) {
                $("#tankLocationDiv").html();
                $("#tankLocationDiv").html(data);
                $("#tankLocationModal").modal('show');
            });
        });

        $('body').on('click', '#tankSettingsBtn', function () {
            $.post(hosturl + '/controllers/embryoslab.php?ac=tankSetting', function (data) {
                $("#tankSettingDiv").html();
                $("#tankSettingDiv").html(data);
                $("#tankSettingModal").modal('show');
            });
        });

        $('body').on('click', '.tankCanBtn', function () {
            var embryosLabId = $("#embryoslab").val();
            var preview = $("#preview").val();
            var location = $(this).attr('data-location');
            var no = $(this).attr('data-no');

            $.post(hosturl + '/controllers/embryoslab.php?ac=tankCan'
                    , {embryosLabId: embryosLabId, location: location, no: no, preview: preview}, function (data) {
                if (no < 10) {
                    $("#upperCellsDiv").html('');
                    $("#lowerCellsDiv").html('');
                    $("#upperCellsDiv").html(data);
                } else {
                    $("#upperCellsDiv").html('');
                    $("#lowerCellsDiv").html('');
                    $("#lowerCellsDiv").html(data);
                }
//                $(".cellPop").hover(function () {
//                    var pop_value = $(this).attr('data-content');
//                    // alert(pop_value);
//                    $(this).attr({
//                        "data-toggle": "popover",
//                        "data-trigger": "hover",
//                        "data-placement": "bottom",
//                        "data-content": pop_value
//                    });
//                    $(this).popover("show");
//                });
            });
        });

        $("body").on('click', '.emptyCell', function () {
            $(this).removeClass('emptyCell');
            $(this).addClass('reserveMeCell');
        });

        $("body").on('click', '.reserveMeCell', function () {
            $(this).removeClass('reserveMeCell');
            $(this).addClass('emptyCell');
        });

        $('body').on('click', '#saveTankLocation', function () {
            $("#saveTankLocation").attr('disabled', true);
            var embryosLabId = $("#embryoslab").val();
            var location = $("#location").val();
            var no = $("#no").val();
            var selectedCells = new Array();
            var cellData = new Array();
            $('.reserveMeCell').each(function () {
                cellData = [];
                cellData[0] = $('#location').val();
                cellData[1] = $('#no').val();
                cellData[2] = $(this).attr('data-row');
                cellData[3] = $(this).attr('data-col');
                cellData[4] = $(this).attr('data-inner');

                selectedCells.push(cellData);
            });

            $.post(hosturl + '/controllers/embryoslab.php?ac=saveTankLocation',
                    {location: location, no: no, embryosLabId: embryosLabId
                        , selectedCells: JSON.stringify(selectedCells)}, function (data) {
                alert(data);
                $("#saveTankLocation").attr('disabled', false);
                loadLocationCells(embryosLabId);
            });
        });


        $('body').on('click', '.tankLocationSpan', function () {
            var location = $(this).attr('data-location');
            var no = $(this).attr('data-no');
            var specTabId = '#tabUpper';
            if (no > 9) {
                specTabId = '#tabLower';
            }
            var specCanId = '#' + location + '' + no;

            $.post(hosturl + '/controllers/embryoslab.php?ac=tankLocation'
                    , {specTabId: specTabId, specCanId: specCanId}, function (data) {
                $("#tankLocationDiv").html();
                $("#tankLocationDiv").html(data);
                $("#tankLocationModal").modal('show');
            });
        });

        $('body').on('click', '#tankLocationPreviewBtn', function () {
            $.post(hosturl + '/controllers/embryoslab.php?ac=tankLocation', {preview: 1}, function (data) {
                $("#tankLocationDiv").html();
                $("#tankLocationDiv").html(data);
                $("#tankLocationModal").modal('show');
            });
        });


        function loadLocationCells(embryosLabId) {
            $.post(hosturl + '/controllers/embryoslab.php?ac=tankLocationDisplay'
                    , {embryosLabId: embryosLabId}, function (data) {
                $("#cellsContainer").html('');
                $("#cellsContainer").html(data);
            });
        }

        $('body').on('click', '#addNewColor', function () {
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: '#ffffff', table: 'colors'}, function (data) {
                if (data > 0) {
                    $("#colorsContainer").append(' <tr> '
                            + '<th class="wds15"> Letter</th> <td class="wds25">  '
                            + '<input type="text" class="form-control  updateElement" '
                            + '   value=""  data-tableName="colors"   '
                            + '  data-id="' + data + '" data-colName="color_letter"   />'
                            + '  </td> '
                            + '  <th class="wds15"> Color</th>  <td > '
                            + '<input type="color"  class="form-control updateElement" '
                            + ' value="#ffffff"   data-tableName="colors"    '
                            + '    data-id="' + data + '" data-colName="color_name"  >'
                            + ' </td> '
                            + ' </tr> ');
                }
            });
        });

        $('body').on('change', '.updateRowColor', 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 select = $(this);
            var color = $('option:selected', this).css('background-color');

            $.post(hosturl + '/controllers/' + controllName + '.php?ac=update', {
                id: id, value: value, tableName: tableName, colName: colName,
            }, function (res) {
                select.css('background-color', color);
            });
        });

        $('body').on('click', '#saveScoringData', function () {
            //  $('#saveScoringData').attr('disabled', true);
            $('#saveScoringData').hide();
            $('#btnLoading').show();
            var day = Number($('#sDays').val());
            var stage = Number($('#sStages').val());
            var grade = Number($('#sGrades').val());
            var fromRow = Number($('#fromRow').val());
            var toRow = Number($('#toRow').val());
            var rowsCount = Number($(this).attr('data-rowsCount'));
            var thisId;

            if (day >= 2 && fromRow > 0 && toRow > 0 && fromRow <= rowsCount &&
                    toRow <= rowsCount && fromRow <= toRow &&
                    (stage > 0 || grade > 0)) {
                if (stage > 0) {
                    for (var i = fromRow; i <= toRow; i++) {
                        thisId = 's_' + day + '_' + i;
                        $('#' + thisId + '').select2('val', Number(stage)).trigger("change"); // select the new term
                        $('.update_itemS[id=' + thisId + ']').trigger("change");
                        if (i == toRow) {
                            $('#saveScoringData').show();
                            $('#btnLoading').hide();
                        }
                    }
                }

                if (grade > 0) {
                    for (var i = fromRow; i <= toRow; i++) {
                        thisId = 'g_' + day + '_' + i;
                        $('#' + thisId + '').select2('val', Number(grade)).trigger("change");// select the new term
                        $('.update_itemS[id=' + thisId + ']').trigger("change");
                        if (i == toRow) {
                            $('#saveScoringData').show();
                            $('#btnLoading').hide();
                        }
                    }
                }
            } else {
                $('#saveScoringData').attr('disabled', false);
                $('#saveScoringData').show();
                $('#btnLoading').hide();
            }
        });

    };
    var formatResultName = function (data) {
        return data.name;
    }

    function formatSelectionName(data) {
        return data.name;
    }


//###################   ajax loading selects ##############################
    var embroSelects = function () {

//###################        Clinician    ##############################
        $("#clinician_id").select2({
            placeholder: "اختر ",
            //    minimumInputLength: 3,
            width: '100%',
            allowClear: true,
            quietMillis: 100,
            ajax: {
                url: hosturl + '/controllers/embryoslab.php?ac=getUsersByRole&role_id=4',
                dataType: 'json',
                data: function (term, page) {

                    return {
                        term: term, //search term
                        page_limit: 10 // page size
                    };
                },
                results: function (data) {
                    //     alert(data);
                    return {results: data};
                }
            },
            formatResult: formatResultName,
            formatSelection: formatSelectionName,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {
                    id: item[0],
                    name: item[1]
                };
                callback(data);
            }
        });
        if ($("#clinician_id_value").val() > 0) {
            var clin_id = $("#clinician_id_value").val();
            var clin_name = $("#clinician_name").val();
            var option = new Option(clin_name, clin_id, true, true);
            $("#clinician_id").append(option);
            $('#clinician_id').select2("val", clin_id);
            var textConId = $("#s2id_clinician_id").children('.select2-choice').children(".select2-chosen").attr("id");
            $("#" + textConId).html(clin_name);
        }

//###################        Embryologist    ##############################
        $("#embryologist_id").select2({
            placeholder: "اختر ",
            //    minimumInputLength: 3,
            width: '100%',
            allowClear: true,
            quietMillis: 100,
            ajax: {
                url: hosturl + '/controllers/embryoslab.php?ac=getEmbrologist&role_id=11',
                dataType: 'json',
                data: function (term, page) {

                    return {
                        term: term, //search term
                        page_limit: 10 // page size
                    };
                },
                results: function (data) {
                    //     alert(data);
                    return {results: data};
                }
            },
            formatResult: formatResultName,
            formatSelection: formatSelectionName,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {
                    id: item[0],
                    name: item[1]
                };
                callback(data);
            },
            formatNoMatches: function (term) {
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newTerm_emb' value='" + term + "'><a href='#' id='addNew_emb' data-id='" + thisId + "'  class='btn blue custombtnstyle'>إضافة جديد</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNew_emb', function () {
            /* add the new term */
            var emb = $('#newTerm_emb').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: emb, table: 'embryologist'}, function (emb_id) {
                var option = new Option(emb, emb_id, true, true);
                $("#embryologist_id").append(option);
                $('#embryologist_id').select2("val", emb_id).trigger("change");
                var textConId = $("#s2id_embryologist_id").children('.select2-choice').children(".select2-chosen").attr("id");
                $("#" + textConId).html(emb);
            });
        });
        if ($("#embryologist_id_value").val() > 0) {
            var emb_id = $("#embryologist_id_value").val();
            var emb_name = $("#embryologist_name").val();
            var option = new Option(emb_name, emb_id, true, true);
            $("#embryologist_id").append(option);
            $('#embryologist_id').select2("val", emb_id);
            var textConId = $("#s2id_embryologist_id").children('.select2-choice').children(".select2-chosen").attr("id");
            $("#" + textConId).html(emb_name);
        }

//###################        ICSI done by    ##############################
        $("#icsi_id").select2({
            placeholder: "اختر ",
            //    minimumInputLength: 3,
            width: '100%',
            allowClear: true,
            quietMillis: 100,
            ajax: {
                url: hosturl + '/controllers/embryoslab.php?ac=getEmbrologist&role_id=11',
                dataType: 'json',
                data: function (term, page) {

                    return {
                        term: term, //search term
                        page_limit: 10 // page size
                    };
                },
                results: function (data) {
                    //     alert(data);
                    return {results: data};
                }
            },
            formatResult: formatResultName,
            formatSelection: formatSelectionName,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {
                    id: item[0],
                    name: item[1]
                };
                callback(data);
            },
            formatNoMatches: function (term) {
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newTerm_icsi' value='" + term + "'><a href='#' id='addNew_icsi' data-id='" + thisId + "'  class='btn blue custombtnstyle'>إضافة جديد</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNew_icsi', function () {
            /* add the new term */
            var icsi = $('#newTerm_icsi').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: icsi, table: 'embryologist'}, function (icsi_id) {
                var option = new Option(icsi, icsi_id, true, true);
                $("#icsi_id").append(option);
                $('#icsi_id').select2("val", icsi_id).trigger("change");
                var textConId = $("#s2id_icsi_id").children('.select2-choice').children(".select2-chosen").attr("id");
                $("#" + textConId).html(icsi);
                //       $('#embryologist_id').trigger("change");
            });
        });
        if ($("#icsi_id_value").val() > 0) {
            var emb_id = $("#icsi_id_value").val();
            var emb_name = $("#icsi_name").val();
            var option = new Option(emb_name, emb_id, true, true);
            $("#icsi_id").append(option);
            $('#icsi_id').select2("val", emb_id);
            var textConId = $("#s2id_icsi_id").children('.select2-choice').children(".select2-chosen").attr("id");
            $("#" + textConId).html(emb_name);
        }

//###################     transfer   Clinician    ##############################
        $("#transfer_clinician_id").select2({
            placeholder: "اختر ",
            //    minimumInputLength: 3,
            width: '100%',
            allowClear: true,
            quietMillis: 100,
            ajax: {
                url: hosturl + '/controllers/embryoslab.php?ac=getUsersByRole&role_id=4',
                dataType: 'json',
                data: function (term, page) {

                    return {
                        term: term, //search term
                        page_limit: 10 // page size
                    };
                },
                results: function (data) {
                    //     alert(data);
                    return {results: data};
                }
            },
            formatResult: formatResultName,
            formatSelection: formatSelectionName,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {
                    id: item[0],
                    name: item[1]
                };
                callback(data);
            }
        });
        if ($("#transfer_clinician_id_value").val() > 0) {
            var clin_id = $("#transfer_clinician_id_value").val();
            var clin_name = $("#transfer_clinician_name").val();
            var option = new Option(clin_name, clin_id, true, true);
            $("#transfer_clinician_id").append(option);
            $('#transfer_clinician_id').select2("val", clin_id);
            var textConId = $("#s2id_transfer_clinician_id").children('.select2-choice').children(".select2-chosen").attr("id");
            $("#" + textConId).html(clin_name);
        }

//###################    transfer    Embryologist    ##############################
        $("#transfer_embryologist_id").select2({
            placeholder: "اختر ",
            //    minimumInputLength: 3,
            width: '100%',
            allowClear: true,
            quietMillis: 100,
            ajax: {
                url: hosturl + '/controllers/embryoslab.php?ac=getEmbrologist&role_id=11&ext=1',
                dataType: 'json',
                data: function (term, page) {

                    return {
                        term: term, //search term
                        page_limit: 10 // page size
                    };
                },
                results: function (data) {
                    //     alert(data);
                    return {results: data};
                }
            },
            formatResult: formatResultName,
            formatSelection: formatSelectionName,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {
                    id: item[0],
                    name: item[1]
                };
                callback(data);
            },
            formatNoMatches: function (term) {
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newTerm_transfer_emb' value='" + term + "'><a href='#' id='addNew_transfer_emb' data-id='" + thisId + "'  class='btn blue custombtnstyle'>إضافة جديد</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNew_transfer_emb', function () {
            /* add the new term */
            var transfer_emb = $('#newTerm_transfer_emb').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: transfer_emb, table: 'embryologist'}, function (transfer_emb_id) {
                var option = new Option(transfer_emb, transfer_emb_id, true, true);
                $("#transfer_embryologist_id").append(option);
                $('#transfer_embryologist_id').select2("val", transfer_emb_id).trigger("change");
                var textConId = $("#s2id_transfer_embryologist_id").children('.select2-choice').children(".select2-chosen").attr("id");
                $("#" + textConId).html(transfer_emb);
                //       $('#embryologist_id').trigger("change");
            });
        });
        if ($("#transfer_embryologist_id_value").val() > 0) {
            var emb_id = $("#transfer_embryologist_id_value").val();
            var emb_name = $("#transfer_embryologist_name").val();
            var option = new Option(emb_name, emb_id, true, true);
            $("#transfer_embryologist_id").append(option);
            $('#transfer_embryologist_id').select2("val", emb_id);
            var textConId = $("#s2id_transfer_embryologist_id").children('.select2-choice').children(".select2-chosen").attr("id");
            $("#" + textConId).html(emb_name);
        }

        //###################    transfer    Embryologist    ##############################
        $("#freeze_embryologist_id").select2({
            placeholder: "اختر ",
            //    minimumInputLength: 3,
            width: '100%',
            allowClear: true,
            quietMillis: 100,
            ajax: {
                url: hosturl + '/controllers/embryoslab.php?ac=getEmbrologist&role_id=11&ext=3',
                dataType: 'json',
                data: function (term, page) {

                    return {
                        term: term, //search term
                        page_limit: 10 // page size
                    };
                },
                results: function (data) {
                    //     alert(data);
                    return {results: data};
                }
            },
            formatResult: formatResultName,
            formatSelection: formatSelectionName,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {
                    id: item[0],
                    name: item[1]
                };
                callback(data);
            },
            formatNoMatches: function (term) {
                var thisId = $(this).attr("id");
                return "<input class='form-control' id='newTerm_freeze_emb' value='" + term + "'><a href='#' id='addNew_freeze_emb' data-id='" + thisId + "'  class='btn blue custombtnstyle'>إضافة جديد</a>"
            }
        }).parent().find('.select2-with-searchbox').on('click', '#addNew_freeze_emb', function () {
            /* add the new term */
            var freeze_emb = $('#newTerm_freeze_emb').val();
            var thisId = $(this).attr("data-id");
            $.post(hosturl + "/controllers/embryoslab.php?ac=addRow", {value: freeze_emb, table: 'embryologist'}, function (freeze_emb_id) {
                var option = new Option(freeze_emb, freeze_emb_id, true, true);
                $("#freeze_embryologist_id").append(option);
                $('#freeze_embryologist_id').select2("val", freeze_emb_id).trigger("change");
                var textConId = $("#s2id_freeze_embryologist_id").children('.select2-choice').children(".select2-chosen").attr("id");
                $("#" + textConId).html(freeze_emb);
                //       $('#embryologist_id').trigger("change");
            });
        });
        if ($("#freeze_embryologist_id_value").val() > 0) {
            var emb_id = $("#freeze_embryologist_id_value").val();
            var emb_name = $("#freeze_embryologist_name").val();
            var option = new Option(emb_name, emb_id, true, true);
            $("#freeze_embryologist_id").append(option);
            $('#freeze_embryologist_id').select2("val", emb_id);
            var textConId = $("#s2id_freeze_embryologist_id").children('.select2-choice').children(".select2-chosen").attr("id");
            $("#" + textConId).html(emb_name);
        }

    };
    return {
//main function to initiate the theme
        init: function () {
            if ($("#embryoslab").length > 0) {
                embryoslab();
            }

            if ($("#embroSelects").length > 0) {
                embroSelects();
            }
        }
    };
}();