var operativedetails = function ()
{
    var hosturl = $('#hosturl').val();
    var opid = $("#opdetailsid").val();
    var patientId = $('#patientId').val();

    var operation = $("#ddd").val();
    var anasthesia = $("#anasthesaid").val();
    var child = $("#child").val();
    var anastxt = $("#anasthesaid option:selected").text();
    var incision = $("#jncisionid").val();
    var childj = $("#childj").val();
    var inctxt = $("#jncisionid option:selected").text();




    var drugs = function () {
        $('body').on('click', '#addNewOp', function ()
        {
            var patientId = $('#patientId').val();
            $.post(hosturl + '/controllers/operativedetails.php?ac=addnew', {patientId: patientId}, function (res)
            {
                window.location = hosturl + '/controllers/operativedetails.php?ac=showdata&patientid=' + patientId + '&id=' + res + '';
            });
        });


        $('.all').on('click', '#newDrug', function ()
        {
            var itr = $('#drugItr').val();
            var newItr = Number(itr) + 1;
            $('#drugItr').val(newItr);

            $.post(hosturl + '/controllers/operativedetails.php?ac=drawRow', {itr: newItr, opid: opid}, function (res)
            {
                $('#drugContent').append(res);
            });
        });

    }



    $("#child").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 anasthesa = $("#anasthesaid").val();
        var newitem = $('#newddTerm').val();
        $.post(hosturl + '/controllers/operativedetails.php?ac=addanasthesachild', {newitem: newitem, anasthesa: anasthesa, opid: opid}, function (data) {
            //alert(data);
            $('#child > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');//$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#child');
            $('#child').select2('val', Number(data)); // select the new term
            $('#child').select2('close'); // close the dropdown
            child = Number(data);
        });
    });

    $("#childj").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 jncisionid = $("#jncisionid").val();
        var newitem = $('#newddTerm').val();
        $.post(hosturl + '/controllers/operativedetails.php?ac=addjncisionchild', {newitem: newitem, jncision: jncisionid, opid: opid}, function (data) {
            //alert(data);
            $('#childj > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
            //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#childj');
            $('#childj').select2('val', Number(data)); // select the new term
            $('#childj').select2('close'); // close the dropdown
            childj = Number(data);
        });
    });

    $(".updateOp").live('change', function () {
        var id = $("#opdetailsid").val(); //$(this).attr("data-id");
        var colName = $(this).attr("data-colName");
        var tableName = $(this).attr("data-tableName");
        var maintableName = $(this).attr("data-maintable");
        var value = $(this).val();
        var controllName = $('#controllName').val();
        var patientId = $('#patientId').val();
        var anathessid = $("#anasthesaid").val();

        if (maintableName == "generalanasthesa")
        {
            anathessid = $("#anasthesaid").val();

        }
        if (maintableName == "midlinejncision")
        {
            anathessid = $("#jncisionid").val();

        }

        if (value == -2)
        {
            $.post(hosturl + '/controllers/operativedetails.php?ac=editselect', {tableName: maintableName, colName: colName, patientid: patientId, mainId: id, anathessid: anathessid}, function (data) {
                $("#alldatadiv").html(data);
                $("#edit_model").modal('show');

                if (colName == "jncisionid")
                {
                    //$("#jncisionid option[value='" + incision + "']").text(inctxt);
                    $('#jncisionid').select2('val', incision);
                }
                if (colName == "anasthesaid")
                {

                    //$("#anasthesaid option[value='" + anasthesia + "']").text(anastxt);
                    $('#anasthesaid').select2('val', anasthesia);
                }
                if (colName == "operationid")
                {
                    $('#ddd').select2('val', operation);
                }
                if (colName == "generalid")
                {
                    $('#child').select2('val', child);
                }
                if (colName == "midlineid")
                {
                    $('#childj').select2('val', childj);
                }
            });
        }
        else
        {
            $.post(hosturl + '/controllers/' + controllName + '.php?ac=update', {id: id, value: value, tableName: tableName, colName: colName, patientId: patientId}, function (res)
            {
                // alert(res);
                if (colName == "jncisionid")
                {
                    incision = value;
                    //inctxt = $("#jncisionid option:selected").text();
                    //alert(inctxt);
                }
                if (colName == "anasthesaid")
                {
                    anasthesia = value;
                    //anastxt = $("#anasthesaid option:selected").text();
                    //alert(anastxt);
                }
                if (colName == "operationid")
                {
                    operation = value;
                }
                if (colName == "generalid")
                {
                    child = value;
                }
                if (colName == "midlineid")
                {
                    childj = value;
                }

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

        var name = $(this).text();
        $("#newitem_name").val(name);
        $("#newitem_id").val(id);
    });

    $("#newitem_name").live('change', function ()
    {
        var controllName = $('#controllName').val();
        var value = $(this).val();
        var id = $(this).attr("data-id");

        var tablename = $("#tablename").val();
        var patientid = $("#patientid").val();
        var colName = $("#colName").val();
        var mainId = $("#mainId").val();

        if (id != '') {
            $.post(hosturl + '/controllers/' + controllName + '.php?ac=updateselect', {tablename: tablename, colName: colName, mainId: mainId, id: id, value: value, patientid: patientid}, function (data) {

                var mySelect = $(".operativeDiv").find("[data-maintable='" + tablename + "']");
                var selectId = mySelect.attr("id");
                $("#" + selectId + " option[value='" + id + "']").text(value);
                if ($("#" + selectId + "").val() == id)// || $("#"+selectId+"").val() == "")
                {
                    $('#' + selectId).select2('val', id);
                    if (selectId == "child")
                    {
                        child = id;
                    }
                    else if (selectId == "childj")
                    {
                        childj = id;
                    }
                }
                if (colName == "anasthesaid" && anasthesia == "")
                {

                    $.post(hosturl + '/controllers/operativedetails.php?ac=loadanasthesa', {thisvalue: id}, function (data) {
                        //   alert(data);

                        $("#chlid_anasthesa").html(data);
                        $("#child").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 anasthesa = $("#anasthesaid").val();
                            var newitem = $('#newddTerm').val();
                            if (anasthesa != "-2")
                            {
                                $.post(hosturl + '/controllers/operativedetails.php?ac=addanasthesachild', {newitem: newitem, anasthesa: anasthesa, opid: opid}, function (data) {
                                    //alert(data);
                                    $('#child > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');//$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#child');
                                    $('#child').select2('val', Number(data)); // select the new term
                                    $('#child').select2('close'); // close the dropdown
                                    child = Number(data);
                                });
                            } else
                            {
                                alert("please insert correct data!!");
                            }
                        });

                    })
                    anasthesia = id;
                    //anastxt = value;

                }
                else if (colName == "jncisionid" && incision == "")
                {
                    $.post(hosturl + '/controllers/operativedetails.php?ac=loadjncision', {thisvalue: id}, function (data) {
                        //   alert(data);

                        $("#chlid_jncision").html(data);
                        $("#childj").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 jncision = $('#jncisionid').val();
                            var newitem = $('#newddTerm').val();
                            if (jncision != -2)
                            {
                                $.post(hosturl + '/controllers/operativedetails.php?ac=addjncisionchild', {newitem: newitem, jncision: jncision, opid: opid}, function (data) {
                                    //alert(data);
                                    $('#childj > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                                    //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#childj');
                                    $('#childj').select2('val', Number(data)); // select the new term
                                    $('#childj').select2('close'); // close the dropdown
                                    childj = Number(data);
                                });
                            }
                            else
                            {
                                alert("please insert correct data!!");
                            }
                        });

                    })
                    incision = id;
                    //inctxt = value;
                }
                //$("#newitem_name").val("");
                //$('#edit_model').modal('hide');
            });
        }
    });

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

                            if (res == 1)
                            {
                                tr.parent().remove();
                                var mySelect = $(".operativeDiv").find("[data-maintable='" + tableName + "']");
                                var selectId = mySelect.attr("id");
                                $("#" + selectId + " option[value='" + itemid + "']").remove();
                                if (selectId == "anasthesaid")
                                {
                                    if (itemid == anasthesia)
                                    {
                                        //$("#anasthesaid option[value='']").text("choose");
                                        $('#anasthesaid').select2("val", "");
                                        $("#child").parent().remove();
                                        anasthesia = "";
                                    }
                                }
                                else if (selectId == "jncisionid")
                                {
                                    if (itemid == incision)
                                    {
                                        //$("#jncisionid option[value='']").text("choose");
                                        $('#jncisionid').select2('val', '');
                                        $("#childj").parent().remove();
                                        incision = "";
                                    }
                                }
                                else if (selectId == "ddd")
                                {
                                    if (itemid == operation)
                                    {
                                        //$("#jncisionid option[value='']").text("choose");
                                        $('#ddd').select2('val', '');
                                        operation = "";
                                    }
                                }
                                else if (selectId == "child")
                                {
                                    if (itemid == child)
                                    {
                                        //$("#jncisionid option[value='']").text("choose");
                                        $('#child').select2('val', '');
                                        child = "";
                                    }
                                }
                                else if (selectId == "childj")
                                {
                                    if (itemid == childj)
                                    {
                                        //$("#jncisionid option[value='']").text("choose");
                                        $('#childj').select2('val', '');
                                        childj = "";
                                    }
                                }
                            }
                            else
                            {
                                alert("!!!!!!!!!!!!! some thing wrong here");
                            }
                        });


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

    var operativedetails_new = function () {

        var hosturl = $('#hosturl').val();
        $('#ddd').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 newitem = $('#newddTerm').val();

            $.post(hosturl + '/controllers/operativedetails.php?ac=addselect', {newitem: newitem, opid: opid}, function (data) {
                //alert(data);
                $('#ddd > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#ddd');
                $('#ddd').select2('val', Number(data)); // select the new term
                $('#ddd').select2('close'); // close the dropdown
                operation = Number(data);
            });
        });

    }

    var addanasthesa = function () {

        var hosturl = $('#hosturl').val();
        $('#anasthesaid').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 newitem = $('#newddTerm').val();
            $.post(hosturl + '/controllers/operativedetails.php?ac=addanasthesa', {newitem: newitem, opid: opid}, function (data) {
                //alert(data);
                $('#anasthesaid > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#anasthesaid');
                $('#anasthesaid').select2('val', Number(data)); // select the new term
                $('#anasthesaid').select2('close'); // close the dropdown
                anasthesia = Number(data);
                //anastxt = $("#anasthesaid option:selected").text();

                $.post(hosturl + '/controllers/operativedetails.php?ac=loadanasthesa', {thisvalue: data}, function (data) {
                    //   alert(data);

                    $("#chlid_anasthesa").html(data);
                    $("#child").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 anasthesa = $("#anasthesaid").val();
                        var newitem = $('#newddTerm').val();
                        if (anasthesa != "-2")
                        {
                            $.post(hosturl + '/controllers/operativedetails.php?ac=addanasthesachild', {newitem: newitem, anasthesa: anasthesa, opid: opid}, function (data) {
                                //alert(data);
                                $('#child > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');//$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#child');
                                $('#child').select2('val', Number(data)); // select the new term
                                $('#child').select2('close'); // close the dropdown
                                child = Number(data);
                            });
                        } else
                        {
                            alert("please insert correct data!!");
                        }
                    });

                })

            });
        });


        $(".updateCheckbox").live('click', function () {

            // alert("aaaaaaaaaaaaaaaaaa");
            var id = $(this).attr("data-id");
            var colName = $(this).attr("data-colName");
            var tableName = $(this).attr("data-tableName");
            var value = $(this).attr('data-val');
            //alert(value);
            /*if ($(this).is(":checked"))
             {
             var value = 1;
             }
             else
             {
             var value = 0;
             }*/
            $.post(hosturl + '/controllers/' + controllName + '.php?ac=updatecheckbox', {id: id, value: value, tableName: tableName, colName: colName, patientId: patientId}, function (res)
            {
                //  alert("aaaaaaaaaaaaaa");
            });
        });



        $(".checkoutrole").change(function (e)
        {


            var thisvaluen = [];

            $('input:checkbox[name="assistant"]:checked').each(function (i, e) {

                thisvaluen.push($(this).val());

            });

            //  alert(thisvaluen);

            var id = $(this).attr("data-id");
            var colName = $(this).attr("data-colName");
            var tableName = $(this).attr("data-tableName");
            //var value = $(this).val();
            var controllName = $('#controllName').val();
            var patientId = $('#patientId').val();
            $.post(hosturl + '/controllers/' + controllName + '.php?ac=updatecheckbox', {id: id, value: thisvaluen, tableName: tableName, colName: colName, patientId: patientId}, function (res)
            {
                //alert(res);
            });
        });

        $(".checkoutrole1").change(function (e)
        {


            var thisvaluen = [];

            $('input:checkbox[name="assistant1"]:checked').each(function (i, e) {

                thisvaluen.push($(this).val());

            });

            //  alert(thisvaluen);

            var id = $(this).attr("data-id");
            var colName = $(this).attr("data-colName");
            var tableName = $(this).attr("data-tableName");
            //var value = $(this).val();
            var controllName = $('#controllName').val();
            var patientId = $('#patientId').val();
            $.post(hosturl + '/controllers/' + controllName + '.php?ac=updatecheckbox', {id: id, value: thisvaluen, tableName: tableName, colName: colName, patientId: patientId}, function (res)
            {
                //alert(res);
            });
        });


        $(".checkdocrole").change(function (e)
        {
            //  alert("aaaaaaaaaaaaaa");

            var thisvaluen = [];

            $('input:checkbox[name="anasthetsts"]:checked').each(function (i, e) {

                thisvaluen.push($(this).val());

            });

            // alert(thisvaluen);

            //  alert(thisvaluen);

            var id = $(this).attr("data-id");
            var colName = $(this).attr("data-colName");
            var tableName = $(this).attr("data-tableName");
            // var value = $(this).val();
            var controllName = $('#controllName').val();
            var patientId = $('#patientId').val();
            $.post(hosturl + '/controllers/' + controllName + '.php?ac=updatecheckbox', {id: id, value: thisvaluen, tableName: tableName, colName: colName, patientId: patientId}, function (res)
            {
            });
        });


        $(".checkdocrole1").change(function (e)
        {
            //  alert("aaaaaaaaaaaaaa");

            var thisvaluen = [];

            $('input:checkbox[name="anasthetsts1"]:checked').each(function (i, e) {

                thisvaluen.push($(this).val());

            });

            // alert(thisvaluen);

            //  alert(thisvaluen);

            var id = $(this).attr("data-id");
            var colName = $(this).attr("data-colName");
            var tableName = $(this).attr("data-tableName");
            // var value = $(this).val();
            var controllName = $('#controllName').val();
            var patientId = $('#patientId').val();
            $.post(hosturl + '/controllers/' + controllName + '.php?ac=updatecheckbox', {id: id, value: thisvaluen, tableName: tableName, colName: colName, patientId: patientId}, function (res)
            {
            });
        });

        $('#anasthesaid').live('change', function () {

            var thisvalue = $(this).val();
            if (thisvalue == "")
            {
                $("#child").parent().remove();
            }
            else if (thisvalue == -2)
            {

            }
            else
            {
                $.post(hosturl + '/controllers/operativedetails.php?ac=loadanasthesa', {thisvalue: thisvalue}, function (data) {
                    //   alert(data);

                    $("#chlid_anasthesa").html(data);
                    $("#child").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 anasthesa = $('#anasthesaid').val();
                        var newitem = $('#newddTerm').val();
                        if (anasthesa != -2)
                        {
                            $.post(hosturl + '/controllers/operativedetails.php?ac=addanasthesachild', {newitem: newitem, anasthesa: anasthesa, opid: opid}, function (data) {
                                //alert(data);
                                $('#child > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                                //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#child');
                                $('#child').select2('val', Number(data)); // select the new term
                                $('#child').select2('close'); // close the dropdown
                                child = Number(data);
                            });
                        } else
                        {
                            alert("please insert correct data!!");
                        }
                    });

                })
            }
        });

    }

    var addjncision = function () {

        var hosturl = $('#hosturl').val();
        $('#jncisionid').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 newitem = $('#newddTerm').val();
            $.post(hosturl + '/controllers/operativedetails.php?ac=addjncision', {newitem: newitem, opid: opid}, function (data) {
                //alert(data);
                $('#jncisionid > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#jncisionid');
                $('#jncisionid').select2('val', Number(data)); // select the new term
                $('#jncisionid').select2('close'); // close the dropdown
                incision = Number(data);
                //inctxt = $("#jncisionid option:selected").text();

                $.post(hosturl + '/controllers/operativedetails.php?ac=loadjncision', {thisvalue: data}, function (data) {
                    //   alert(data);

                    $("#chlid_jncision").html(data);
                    $("#childj").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 jncision = $('#jncisionid').val();
                        var newitem = $('#newddTerm').val();
                        if (jncision != -2)
                        {
                            $.post(hosturl + '/controllers/operativedetails.php?ac=addjncisionchild', {newitem: newitem, jncision: jncision, opid: opid}, function (data) {
                                //alert(data);
                                $('#childj > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                                //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#childj');
                                $('#childj').select2('val', Number(data)); // select the new term
                                $('#childj').select2('close'); // close the dropdown
                                childj = Number(data);
                            });
                        }
                        else
                        {
                            alert("please insert correct data!!");
                        }
                    });

                })

            });
        });

        $('#jncisionid').live('change', function () {

            var thisvalue = $(this).val();
            //   alert(thisvalue);
            if (thisvalue == "")
            {
                $("#childj").parent().remove();
            }
            else if (thisvalue == -2)
            {

            }
            else
            {
                $.post(hosturl + '/controllers/operativedetails.php?ac=loadjncision', {thisvalue: thisvalue}, function (data) {
                    //   alert(data);

                    $("#chlid_jncision").html(data);
                    $("#childj").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 jncision = $('#jncisionid').val();
                        var newitem = $('#newddTerm').val();
                        if (jncision != -2)
                        {
                            $.post(hosturl + '/controllers/operativedetails.php?ac=addjncisionchild', {newitem: newitem, jncision: jncision, opid: opid}, function (data) {
                                //alert(data);
                                $('#childj > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                                //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('#childj');
                                $('#childj').select2('val', Number(data)); // select the new term
                                $('#childj').select2('close'); // close the dropdown
                                childj = Number(data);
                            });
                        }
                        else
                        {
                            alert("please insert correct data!!");
                        }
                    });

                })
            }
        });

    }



    if ($("#controllName").val() == "operativedetails")
    {
        $('.removeMsg').live('click', function () {
            var itemid = $(this).attr('data-id');
            var controllName = $(this).attr('data-link');
            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 () {
                            //do something else
                            window.location.href = hosturl + "/controllers/" + controllName + ".php?ac=delData&id=" + itemid + "&patientid=" + patientId;
                        }
                    }
                }
            }).find('.modal-content').addClass('modal-sm');
        });

        $("#addnewRow").live('click', function () {
            var tablename = $("#tablename").val();
            var selectname;
            var fkid;
            var dtcount = $("#dtcount").val();
            dtcount++;

            if (tablename == "operation")
                selectname = "ddd";
            else if (tablename == "anasthesa")
                selectname = "anasthesaid";
            else if (tablename == "generalanasthesa")
            {
                selectname = "child";
                fkid = $("#anasthesaid").val();
            }
            else if (tablename == "jncision")
                selectname = "jncisionid";
            else if (tablename == "midlinejncision")
            {
                selectname = "childj";
                fkid = $("#jncisionid").val();
            }

            $.post(hosturl + "/controllers/operativedetails.php?ac=addNewRow", {tablename: tablename, fkid: fkid}, function (data) {
                var dataid = Number(data);
                $('#' + selectname + ' > .deleteoptincompo').before('<option value="' + dataid + '" ></option>');
                $("#dtAddTbl").prepend('<tr>\
                                            <td style="width: 100%">\
                                                <input type="text" class="form-control"  id="newitem_name" data-id="' + dataid + '" name="newamount_name" value="" >\
                                            </td>\
                                            <td>\
                                                <a href="#" class="btn red fleft deleteElementmodel" data-id="' + dataid + '" data-tableName="' + tablename + '" >\
                                                    <i class="fa fa-times" ></i>\
                                                </a>\
                                            </td>\
                                        </tr>');
                $("#dtcount").val(dtcount);
            });
        });
    }
    return {
        //main function to initiate the theme
        init: function () {

            if ($("#controllName").val() == "operativedetails")
            {
                operativedetails_new();
                addanasthesa();
                addjncision();
                drugs();
            }


        }
    };

}();

