if ($("#embryofreezing_report").val() == 1)
{
    var hosturl = $('#hosturl').val();
    var patientId = $('#patientId').val();


    $(".bp_location").inputmask("(**,**,**,**)", {
        autoUnmask: true});

    $('select.stage').select2({width: '170px', allowClear: true, formatNoMatches: function (term) {
            /* customize the no matches output */
            var data_itra = $(this).attr('data-itra');
            //            var data_itra = $('#embryo').val();
//            alert(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 (e) {
        e.preventDefault();
        var colName = $(".stage").attr("data-colName");
        var tableName = $(".stage").attr("select-tableName");
        var selectedstage = $("#newddTerm").val();
        var data_itra = $(this).attr('data-itra');
//                alert(data_itra)
        //alert('data_itra = ' + data_itra);
        $.post(hosturl + '/controllers/embryofreezing.php?ac=addStage', {selectedstage: selectedstage}, function (data) {
            $('.allupdateselect > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + selectedstage + '</option>');
            //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#childj');
            $('#stage_' + data_itra + '').select2('val', Number(data)); // select the new term
            $('#stage_' + data_itra + '').select2('close'); // close the dropdown
        });
    });

    $('select.grade').select2({width: '170px', allowClear: true, formatNoMatches: function (term) {
            /* customize the no matches output */
            var data_itra = $(this).attr('data-itra');
//            var data_itra = $('#embryo').val();
            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 (e) {
        e.preventDefault();

        var selectedstage = $("#newddTerm").val();
        var data_itra = $(this).attr('data-itra');

        //alert('data_itra = ' + data_itra);
        $.post(hosturl + '/controllers/embryofreezing.php?ac=addStage', {selectedstage: selectedstage}, function (data) {
            $('.allupdateselect > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + selectedstage + '</option>');
            //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#childj');
            $('#grade_' + data_itra + '').select2('val', Number(data)); // select the new term
            $('#grade_' + data_itra + '').select2('close'); // close the dropdown
        });
    });

    $("select.stageselect").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');
//                alert(tableName)
        if (thisvalue == '-2') {

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

            });
        }

    })

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

            $.post(hosturl + '/controllers/embryofreezing.php?ac=editModal', {tableName: tableName, colName: colName}, function (data) {
                $(".grade").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');

            });
        }

    })

    $("#device").select2({
        width: '170px',
        allowClear: true,
        formatNoMatches: function (term) {
            /* customize the no matches output */

            return "<input class='form-control' id='newddTerm'  value='" + term + "'><a href='#' id='addddNew'  class='btn btn-default'>ADD</a>"
        }
    }).parent().find('.select2-with-searchbox').on('click', '#addddNew', function () {
        var colName = $("#device").attr("data-colName");
        var tableName = $("#device").attr("select-tableName");
        var selecteddevice = $("#newddTerm").val();
        $.post(hosturl + '/controllers/embryofreezing.php?ac=addDevice', {selecteddevice: selecteddevice}, function (data) {
            $('.deviceselect > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + selecteddevice + '</option>');
            //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#childj');
            $('#device').select2('val', Number(data)); // select the new term
            $('#device').select2('close'); // close the dropdown
        });
    });

    $("#kit").select2({
        width: '170px',
        allowClear: true,
        formatNoMatches: function (term) {
            /* customize the no matches output */

            return "<input class='form-control' id='newddTerm'  value='" + term + "'><a href='#' id='addddNew'  class='btn btn-default'>ADD</a>"
        }
    }).parent().find('.select2-with-searchbox').on('click', '#addddNew', function () {

        var colName = $("#kit").attr("data-colName");
        var tableName = $("#kit").attr("select-tableName");
        var selectedkit = $('#newddTerm').val();
        $.post(hosturl + '/controllers/embryofreezing.php?ac=addkit', {selectedkit: selectedkit}, function (data) {
            //alert(data);
            $('.kit > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + selectedkit + '</option>');
            //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#childj');
            $('#kit').select2('val', Number(data)).trigger("change");
            $('#kit').select2('close'); // close the dropdown
        });
    });

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

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

            });
        }

    })

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

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

            });
        }

    })

    $("#addnewRowFreez").live('click', function () {
        var tableName = $(this).attr('data-tableName');
        var selectname = $(this).attr('data-selectClass');

        var colName = $("#colName").val();
//
        $.post(hosturl + "/controllers/embryofreezing.php?ac=addNewRow", {tableName: tableName, colName: colName}, function (data) {
            var dataid = Number(data);
            $('.' + selectname + ' > .deleteoptincompo').before('<option value="' + dataid + '" ></option>');

            $("#dtAddTbl").prepend('<tr>\
                                            <td style="width: 100%">\
                                                <a href="#" >\
                                                    <input type="text" id="newitem_name" class="form-control updateElementSelect" data-selectClass="' + selectname + '"  data-select2="yes" data-id="' + dataid + '" data-colName="' + colName + '" data-tableName="' + tableName + '" value="" name="newamount_name"/>\
                                                </a>\
                                            </td>\
                                            <td>\
                                                <a class="delrow fa fa-times btn red fleft" data-colName="conditions" data-link="gyna" data-tableName="' + tableName + '" data-fn="deldiagnosis" data-id="' + dataid + '" ></a>\
                                            </td>\
                                        </tr>');
        });

    });

    $('.deleteModal').live('click', function () {
        var id = $(this).attr('data-id');

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

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

                            if (res == 1)
                            {
                                tr.parent().remove();
                                if (tablename === "kit") {
                                    $(".kitselect option[value='" + id + "']").remove();

                                } else if (tablename === "device") {
                                    $(".deviceselect option[value='" + id + "']").remove();

                                } else if (tablename === "cathetr") {
                                    $(".cathetrselect option[value='" + id + "']").remove();

                                } else if (tablename === "stage") {
                                    $(".stageselect option[value='" + id + "']").remove();

                                } else if (tablename === "grade") {
                                    $(".gradeselect option[value='" + id + "']").remove();

                                }
                            } else
                            {
                                alert("!!!!!!!!!!!!! some thing wrong here");
                            }

                        });


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



    $(".selectStageFreeze").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='addNewStage' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
        }
    }).parent().find('.select2-with-searchbox').on('click', '#addNewStage', function () {
        /* add the new term */
        var Stage = $('#newStageTerm').val();
        var thisId = $(this).attr("data-id");
        $.post(hosturl + "/controllers/embryofreezing.php?ac=addRow", {value: Stage, table: 'stage', name: 'stage'}, function (data) {

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

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

    $(".selectGradeFreeze").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='addNewGrade' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
        }
    }).parent().find('.select2-with-searchbox').on('click', '#addNewGrade', function () {
        /* add the new term */
        var Grade = $('#newGradeTerm').val();
        var thisId = $(this).attr("data-id");
        $.post(hosturl + "/controllers/embryofreezing.php?ac=addRow", {value: Grade, table: 'grade', name: 'grade'}, function (data) {

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

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

    $("#addNewembryoRow").live('click', function () {
        var tablename = $(this).attr('data-tableName');
        var row_id = $("#row_id").val();
        var selectItr = Number($("#embryosItr").val());
        selectItr++;
        $("#embryosItr").val(selectItr);
        $.post(hosturl + "/controllers/embryofreezing.php?ac=insertNewRow",
                {tablename: tablename, row_id: row_id, myItra: selectItr}, function (data) {
            $("#embryoContent").append(data);

            $("select.selectStageFreeze").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='addNewStage' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '#addNewStage', function () {
                /* add the new term */
                var Stage = $('#newStageTerm').val();
                var thisId = $(this).attr("data-id");
                $.post(hosturl + "/controllers/embryofreezing.php?ac=addRow", {value: Stage, table: 'stage', name: 'stage'}, function (data) {

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

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

            $("select.selectGradeFreeze").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='addNewGrade' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '#addNewGrade', function () {
                /* add the new term */
                var Grade = $('#newGradeTerm').val();
                var thisId = $(this).attr("data-id");
                $.post(hosturl + "/controllers/embryofreezing.php?ac=addRow", {value: Grade, table: 'grade', name: 'grade'}, function (data) {

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

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

        });

    });

    $(".selectStageFreeze, .selectGradeFreeze").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/embryofreezing.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');
            });
        }

    })


    $("#addNewthawingRowFreeze").live('click', function () {
        var tablename = $(this).attr('data-tableName');
        var row_id = $("#row_id").val();
        var selectItr = Number($("#embryothawingItr").val());
        selectItr++;
        $("#embryothawingItr").val(selectItr);
        $.post(hosturl + "/controllers/embryofreezing.php?ac=insertNewRow",
                {tablename: tablename, row_id: row_id, myItra: selectItr}, function (data) {
            $("#thawingContent").append(data);
            $("#embryol_oigist").select2();
        });

    });

}
