var patienthistory = function () {

    var patienthistory_new = function () {
        var hosturl = $('#hosturl').val();
        var patientId = $("#patientId").val();
        var menstrual_itr = Number($('#menstrual_itr').val());
        menstrual_itr++;

        $('body').on('change', '.arttypeExtra', function ()
        {
            var itr = $(this).attr("data-itr");
            var value = $(this).val();
            if (value == 0 )
            {
                $('#extraArt' + itr).show();
                $('#extraArtother' + itr).hide();
            }
            else if(value == 3 || value == 2)
            {
                $('#extraArtother' + itr).show();
                $('#extraArt' + itr).hide();
                $('.date-picker').datepicker({rtl:Metronic.isRTL(),autoclose:true});
            } else
            {
                $('#extraArt' + itr).hide();
                $('#extraArtother' + itr).hide();
            }
        });


        /*========================================== menstrual ==================================================*/
//// add new row by smarty in another html
        $('#btn_menstrual').on("click", function () {
            $.post(hosturl + '/controllers/patienthistory.php?ac=addnewmenstural&id=' + patientId, {itr: menstrual_itr}, function (data) {

                $('#menstrual_div').prepend(data);
                //// for calender
                $(".datepicker").datepicker({
                    todayBtn: "linked",
                    // autoclose: true,
                    todayHighlight: true,
                    format: 'dd/mm/yy',
                    isRTL: true
                });
                $('#phregularity' + menstrual_itr).select2({
                    width: '170px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        $thisitr = $(this).attr('data-itr');
                        $id = $(this).attr('data-id');
                        return "<input class='form-control' id='newregTerm" + $thisitr + "' value='" + term + "' data-id='" + $id + "'><a href='#' id='addregnew" + $thisitr + "' data-id=" + $id + "  data-itr=" + $thisitr + " class='btn btn-default addnewselect'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addnewselect', function () {
                    $thisitr = $(this).attr('data-itr');
                    var selectid = $(this).attr('data-id');
                    var newitem = $('#newregTerm' + $thisitr).val();
                    $.post(hosturl + '/controllers/patienthistory.php?ac=addRegualrity&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                        $('select[name="phregularity"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="phregularity"]');
                        $('#phregularity' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#phregularity' + $thisitr).select2('close'); // close the dropdown


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

                    $thisitr = $(this).attr('data-itr');
                    var selectid = $(this).attr('data-id');
                    var newitem = $('#newamountTerm' + $thisitr).val();
                    $.post(hosturl + '/controllers/patienthistory.php?ac=addphamount&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {
                        $('select[name="phamount"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="phamount"]');
                        $('#phamount' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#phamount' + $thisitr).select2('close'); // close the dropdown

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

                    $thisitr = $(this).attr('data-itr');
                    var selectid = $(this).attr('data-id');
                    var newitem = $('#newdysTerm' + $thisitr).val();
                    $.post(hosturl + '/controllers/patienthistory.php?ac=addphdysmenorrhoea&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {
                        $('select[name="phdysmenorrhoea"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="phdysmenorrhoea"]');
                        $('#phdysmenorrhoea' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#phdysmenorrhoea' + $thisitr).select2('close'); // close the dropdown

                    });
                });
                /*=========================================updateselect ==========================================================================*/
                $(".allupdateselect").on('change', function () {
// Do something wi.on('change', function () {
                    var thisvalue = $(this).val();
                    var tableName = $(this).attr('select-tableName');
                    var itr = $(this).attr('data-itr');
                    var name = $(this).attr('name');
                    var pervious = $(this).attr('data-val');
                    var patientid = $("#patientId").val();
                    var rowoftableid = $(this).attr('data-id');
                    var maintableName = $(this).attr('data-tableName');
                    if (thisvalue == '-2') {


                        $('#' + name + '' + itr).select2('val', pervious);
                        $.post(hosturl + '/controllers/patienthistory.php?ac=editselect', {tableName: tableName, itr: itr, name: name, rowoftableid: rowoftableid, patientid: patientid, maintableName: maintableName}, function (data) {


                            $("#newitem_name").val('');
                            $("#newitem_id").val('');
                            $("#alldatadiv").html(data);
                            $("#edit_model").modal('show');
                        });
                    }

                })



                $('#menstrual_itr').val(menstrual_itr);
                menstrual_itr++;
            });
        });
        /*========================================edit menstural ==================================================================*/
        var allmenstural = $('#menstrual_itr').val();
        for ($i = 1; $i <= allmenstural; $i++) {
/// make select select2
            $('#phregularity' + $i).select2({
                width: '170px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    $thisitr = $(this).attr('data-itr');
                    $id = $(this).attr('data-id');
                    return "<input class='form-control' id='newregTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addregnew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "'  class='btn btn-default addnewselect'>Add</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '.addnewselect', function () {
                $thisitr = $(this).attr('data-itr');
                var newitem = $('#newregTerm' + $thisitr).val();
                var selectid = $(this).attr('data-id');
                $.post(hosturl + '/controllers/patienthistory.php?ac=addRegualrity&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                    $('select[name="phregularity"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                    //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="phregularity"]'); //'#phregularity' + $thisitr
                    $('#phregularity' + $thisitr).select2('val', Number(data)); // select the new term
                    $('#phregularity' + $thisitr).select2('close'); // close the dropdown


                });
            });
            $('#phamount' + $i).select2({
                width: '170px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    $thisitr = $(this).attr('data-itr');
                    $id = $(this).attr('data-id');
                    return "<input class='form-control' id='newamountTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addamountnew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "'  class='btn btn-default addnewamountselect'>Add</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '.addnewamountselect', function () {
                $thisitr = $(this).attr('data-itr');
                var newitem = $('#newamountTerm' + $thisitr).val();
                var selectid = $(this).attr('data-id');
                $.post(hosturl + '/controllers/patienthistory.php?ac=addphamount&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {
                    $('select[name="phamount"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                    //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="phamount"]');  //'#phamount' + $thisitr
                    $('#phamount' + $thisitr).select2('val', Number(data)); // select the new term
                    $('#phamount' + $thisitr).select2('close'); // close the dropdown

                });
            });
            $('#phdysmenorrhoea' + $i).select2({
                width: '170px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    $thisitr = $(this).attr('data-itr');
                    $id = $(this).attr('data-id');
                    return "<input class='form-control' id='newdysTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='adddysnew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "'  class='btn btn-default addnewdysselect'>Add</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '.addnewdysselect', function () {
                $thisitr = $(this).attr('data-itr');
                var newitem = $('#newdysTerm' + $thisitr).val();
                var selectid = $(this).attr('data-id');
                $.post(hosturl + '/controllers/patienthistory.php?ac=addphdysmenorrhoea&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {
                    $('select[name="phdysmenorrhoea"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                    //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="phdysmenorrhoea"]'); //'#phdysmenorrhoea' + $thisitr
                    $('#phdysmenorrhoea' + $thisitr).select2('val', Number(data)); // select the new term
                    $('#phdysmenorrhoea' + $thisitr).select2('close'); // close the dropdown

                });
            });
        }


        /*======================================delete eleement ====================*/


        $('.deleteElement').live('click', function () {
            var itemid = $(this).attr('data-id');
            var tableName = $(this).attr('data-tableName');
            var controllName = $('#controllName').val();
            var tr = $(this).parent().parent().parent().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 () {
                            //do something else
                            //window.location.href = hosturl + "/controllers/" + controllName + ".php?ac=delRows&id=" + itemid + "&tableName=" + tableName;
                            $.post(hosturl + '/controllers/' + controllName + '.php?ac=delRows', {id: itemid, tableName: tableName}, function (res) {

                                if (res == 1)
                                {
                                    //  alert(res);
                                    tr.remove();
                                }
                                else
                                {
                                    alert("!!!!!!!!!!!!! some thing wrong here");
                                }
                            });
                        }
                    }
                }
            }).find('.modal-content').addClass('modal-sm');
        });

        $(".deleteElementmodelnew").live('click', function () {
            var itemid = $(this).attr('data-id');
            var urlgo = $(this).attr('data-link');
            var fngo = $(this).attr('data-fn');
            var tableName = $(this).attr('data-tableName');
            var selectname = $("#selectname").val();
            var tr = $(this).parent().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/' + urlgo + '.php?ac=' + fngo, {id: itemid, tableName: tableName}, function (res) {
                                if (res == 1)
                                {

                                    $("." + selectname + " option[value='" + itemid + "']").remove();
                                    tr.remove();

                                }
                                else
                                {
                                    alert("!!!!!!!!!!!!! some thing wrong here");
                                }
                            });
                        }
                    }
                }
            }).find('.modal-content').addClass('modal-sm');
        });




        /*==========================================================contraction ================================================================*/

        var contract_itr = Number($('#contract_itr').val());
        contract_itr++;
        $('#contraception_btn').on("click", function () {
            $.post(hosturl + '/controllers/patienthistory.php?ac=addnewconstract&id=' + patientId, {itr: contract_itr}, function (data) {

                $('#contraception_div').prepend(data);
                $('#contype' + contract_itr).select2({
                    width: '170px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        $thisitr = $(this).attr('data-itr');
                        $id = $(this).attr('data-id');
                        return "<input class='form-control' id='newcontractTerm" + $thisitr + "' value='" + term + "' data-id='" + $id + "' data-itr='" + $thisitr + "'><a href='#' id='addNew" + $thisitr + "' data-id='" + $id + "' data-itr='" + $thisitr + "' class='btn btn-default addnewcontracselect'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addnewcontracselect', function () {
                    $thisitr = $(this).attr('data-itr');
                    var selectid = $(this).attr('data-id');
                    var newitem = $('#newcontractTerm' + $thisitr).val();
                    $.post(hosturl + '/controllers/patienthistory.php?ac=addphcontype&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                        $('select[name="contype"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="contype"]');
                        $('#contype' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#contype' + $thisitr).select2('close'); // close the dropdown

                    });
                });
                /*=========================================updateselect ==========================================================================*/
                $(".allupdateselect").on('change', function () {
// Do something wi.on('change', function () {
                    var thisvalue = $(this).val();
                    var tableName = $(this).attr('select-tableName');
                    var itr = $(this).attr('data-itr');
                    var name = $(this).attr('name');
                    var pervious = $(this).attr('data-val');
                    var patientid = $("#patientId").val();
                    var rowoftableid = $(this).attr('data-id');
                    var maintableName = $(this).attr('data-tableName');
                    if (thisvalue == '-2') {


                        $('#' + name + '' + itr).select2('val', pervious);
                        $.post(hosturl + '/controllers/patienthistory.php?ac=editselect', {tableName: tableName, itr: itr, name: name, rowoftableid: rowoftableid, patientid: patientid, maintableName: maintableName}, function (data) {


                            $("#newitem_name").val('');
                            $("#newitem_id").val('');
                            $("#alldatadiv").html(data);
                            $("#edit_model").modal('show');
                        });
                    }

                })
                $('#contract_itr').val(contract_itr);
                contract_itr++;
            });
        });
        var constractlength = Number($('#contract_itr').val());
        /*==========================================================edit contraction ================================================================*/
        for ($j = 1; $j <= constractlength; $j++) {
            $('#contype' + $j).select2({
                width: '170px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    $thisitr = $(this).attr('data-itr');
                    $id = $(this).attr('data-id');
                    return "<input class='form-control' id='newcontractTerm" + $thisitr + "' value='" + term + "' data-id='" + $id + "' data-itr='" + $thisitr + "'><a href='#' id='addNew" + $thisitr + "' data-id='" + $id + "' data-itr='" + $thisitr + "' class='btn btn-default addnewcontracselect'>Add</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '.addnewcontracselect', function () {
                $thisitr = $(this).attr('data-itr');
                var selectid = $(this).attr('data-id');
                var newitem = $('#newcontractTerm' + $thisitr).val();
                $.post(hosturl + '/controllers/patienthistory.php?ac=addphcontype&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                    $('select[name="contype"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                    //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="contype"]');
                    $('#contype' + $thisitr).select2('val', Number(data)); // select the new term
                    $('#contype' + $thisitr).select2('close'); // close the dropdown

                });
            });
        }

        /*==========================================================================obstretic ============================================================*/
        $(document).ready(function () {
            var hreff = window.location.href;
            var res = hreff.split("endpreg");
            if (res[1] == "=1#tab_2_6")
            {
                $('#obstertic_btn').click();
            }
        });
        var obsteric_itr = Number($('#obsteric_itr').val());
        obsteric_itr++;
        $('#obstertic_btn').on("click", function () {
            $.post(hosturl + '/controllers/patienthistory.php?ac=addnewobstetric&id=' + patientId, {itr: obsteric_itr}, function (data) {

                $('#obstertic_div').prepend(data);
                //// for calender
                $(".datepicker").datepicker({
                    todayBtn: "linked",
                    // autoclose: true,
                    todayHighlight: true,
                    format: 'dd/mm/yy',
                    isRTL: true
                });
                ////// obterminate select
                $('#obstermination' + obsteric_itr).select2({
                    width: '170px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        $thisitr = $(this).attr('data-itr');
                        $id = $(this).attr('data-id');
                        return "<input class='form-control' id='newterminateTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addterminateNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addobsterminateselect'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addobsterminateselect', function () {
                    $thisitr = $(this).attr('data-itr');
                    var selectid = $(this).attr('data-id');
                    var newitem = $('#newterminateTerm' + $thisitr).val();
                    $.post(hosturl + '/controllers/patienthistory.php?ac=addphobstericterminate&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                        $('select[name="obstermination"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="obstermination"]');
                        $('#obstermination' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#obstermination' + $thisitr).select2('close'); // close the dropdown

                    });
                });
                ////// obsplace select
                $('#obsplace' + obsteric_itr).select2({
                    width: '170px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        $thisitr = $(this).attr('data-itr');
                        $id = $(this).attr('data-id');
                        return "<input class='form-control' id='newplaceTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addplceNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addobsplaceselected'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addobsplaceselected', function () {
                    $thisitr = $(this).attr('data-itr');
                    var selectid = $(this).attr('data-id');
                    var newitem = $('#newplaceTerm' + $thisitr).val();
                    $.post(hosturl + '/controllers/patienthistory.php?ac=addphobstericterplace&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                        $('select[name="obsplace"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="obsplace"]');
                        $('#obsplace' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#obsplace' + $thisitr).select2('close'); // close the dropdown

                    });
                });
                /*=====================================================updateselect ==========================================================================================*/
                $(".allupdateselect").on('change', function () {
// Do something wi.on('change', function () {
                    var thisvalue = $(this).val();
                    var tableName = $(this).attr('select-tableName');
                    var itr = $(this).attr('data-itr');
                    var name = $(this).attr('name');
                    var pervious = $(this).attr('data-val');
                    var patientid = $("#patientId").val();
                    var rowoftableid = $(this).attr('data-id');
                    var maintableName = $(this).attr('data-tableName');
                    if (thisvalue == '-2') {


                        $('#' + name + '' + itr).select2('val', pervious);
                        $.post(hosturl + '/controllers/patienthistory.php?ac=editselect', {tableName: tableName, itr: itr, name: name, rowoftableid: rowoftableid, patientid: patientid, maintableName: maintableName}, function (data) {


                            $("#newitem_name").val('');
                            $("#newitem_id").val('');
                            $("#alldatadiv").html(data);
                            $("#edit_model").modal('show');
                        });
                    }

                })



                /*===========================================================babay_type===========================================================================================*/




                $('#obsteric_itr').val(obsteric_itr);
                obsteric_itr++;
            });
        });
        /*==========================================================================edit obstretic ============================================================*/
        var obsterticlengt = Number($('#obsteric_itr').val());
        for ($z = 1; $z <= obsterticlengt; $z++) {
            $('#obstermination' + $z).select2({
                width: '170px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    $thisitr = $(this).attr('data-itr');
                    $id = $(this).attr('data-id');
                    return "<input class='form-control' id='newterminateTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addterminateNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addobsterminateselect'>Add</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '.addobsterminateselect', function () {
                $thisitr = $(this).attr('data-itr');
                var selectid = $(this).attr('data-id');
                var newitem = $('#newterminateTerm' + $thisitr).val();
                $.post(hosturl + '/controllers/patienthistory.php?ac=addphobstericterminate&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                    $('select[name="obstermination"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                    //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="obstermination"]');
                    $('#obstermination' + $thisitr).select2('val', Number(data)); // select the new term
                    $('#obstermination' + $thisitr).select2('close'); // close the dropdown

                });
            });
            ////// obsplace select
            $('#obsplace' + $z).select2({
                width: '170px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    $thisitr = $(this).attr('data-itr');
                    $id = $(this).attr('data-id');
                    return "<input class='form-control' id='newplaceTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addplceNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addobsplaceselected'>Add</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '.addobsplaceselected', function () {
                $thisitr = $(this).attr('data-itr');
                var selectid = $(this).attr('data-id');
                var newitem = $('#newplaceTerm' + $thisitr).val();
                $.post(hosturl + '/controllers/patienthistory.php?ac=addphobstericterplace&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                    $('select[name="obsplace"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                    //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="obsplace"]');
                    $('#obsplace' + $thisitr).select2('val', Number(data)); // select the new term
                    $('#obsplace' + $thisitr).select2('close'); // close the dropdown

                });
            });
        }

        /*=====================================past medical ===========================*/
        var pastmedical_itr = Number($('#pastmedical_itr').val());
        pastmedical_itr++;
        $('#pastmedical_btn').on("click", function () {
            $.post(hosturl + '/controllers/patienthistory.php?ac=addnewpasthmedical&id=' + patientId, {itr: pastmedical_itr}, function (data) {

                $('#pastmedical_div').prepend(data);
                ////// medicaldisease select
                $('#medicaldisease' + pastmedical_itr).select2({
                    width: '170px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        $thisitr = $(this).attr('data-itr');
                        $id = $(this).attr('data-id');
                        return "<input class='form-control' id='newmedicalTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addmedicalNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addnewmedicalselected'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addnewmedicalselected', function () {

                    $thisitr = $(this).attr('data-itr');
                    var selectid = $(this).attr('data-id');
                    var newitem = $('#newmedicalTerm' + $thisitr).val();
                    $.post(hosturl + '/controllers/patienthistory.php?ac=addphpastmedical&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                        $('select[name="medicaldisease"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="medicaldisease"]');
                        $('#medicaldisease' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#medicaldisease' + $thisitr).select2('close'); // close the dropdown

                    });
                });
                /*=====================================================updateselect ==========================================================================================*/
                $(".allupdateselect").on('change', function () {
// Do something wi.on('change', function () {
                    var thisvalue = $(this).val();
                    var tableName = $(this).attr('select-tableName');
                    var itr = $(this).attr('data-itr');
                    var name = $(this).attr('name');
                    var pervious = $(this).attr('data-val');
                    var patientid = $("#patientId").val();
                    var rowoftableid = $(this).attr('data-id');
                    var maintableName = $(this).attr('data-tableName');
                    if (thisvalue == '-2') {


                        $('#' + name + '' + itr).select2('val', pervious);
                        $.post(hosturl + '/controllers/patienthistory.php?ac=editselect', {tableName: tableName, itr: itr, name: name, rowoftableid: rowoftableid, patientid: patientid, maintableName: maintableName}, function (data) {


                            $("#newitem_name").val('');
                            $("#newitem_id").val('');
                            $("#alldatadiv").html(data);
                            $("#edit_model").modal('show');
                        });
                    }

                })

                $('#pastmedical_itr').val(pastmedical_itr);
                pastmedical_itr++;
            });
        });
        /*===============================================edit past medical ====================================================*/
        var pastmedicallength = Number($('#pastmedical_itr').val());
        for ($w = 1; $w <= pastmedicallength; $w++) {
            $('#medicaldisease' + $w).select2({
                width: '170px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    $thisitr = $(this).attr('data-itr');
                    $id = $(this).attr('data-id');
                    return "<input class='form-control' id='newmedicalTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addmedicalNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addnewmedicalselected'>Add</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '.addnewmedicalselected', function () {

                $thisitr = $(this).attr('data-itr');
                var selectid = $(this).attr('data-id');
                var newitem = $('#newmedicalTerm' + $thisitr).val();
                $.post(hosturl + '/controllers/patienthistory.php?ac=addphpastmedical&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                    $('select[name="medicaldisease"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                    //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="medicaldisease"]');
                    $('#medicaldisease' + $thisitr).select2('val', Number(data)); // select the new term
                    $('#medicaldisease' + $thisitr).select2('close'); // close the dropdown

                });
            });
        }


        /*=====================================past surgical ===========================*/
        var pastsurgical_itr = Number($('#pastsurgical_itr').val());
        pastsurgical_itr++;
        $('#pastsurgical_btn').on("click", function () {
            $.post(hosturl + '/controllers/patienthistory.php?ac=addnewpasthsurgical&id=' + patientId, {itr: pastsurgical_itr}, function (data) {

                $('#pastsurgical_div').prepend(data);
                ////// medicaldisease select
                $('#surgicaloperation' + pastsurgical_itr).select2({
                    width: '170px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        $thisitr = $(this).attr('data-itr');
                        $id = $(this).attr('data-id');
                        return "<input class='form-control' id='newsurgicalTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addsurgicalNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addsurgicalselected'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addsurgicalselected', function () {
                    $thisitr = $(this).attr('data-itr');
                    var selectid = $(this).attr('data-id');
                    var newitem = $('#newsurgicalTerm' + $thisitr).val();
                    $.post(hosturl + '/controllers/patienthistory.php?ac=addphpastsurgical&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                        $('select[name="surgicaloperation"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="surgicaloperation"]');
                        $('#surgicaloperation' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#surgicaloperation' + $thisitr).select2('close'); // close the dropdown

                    });
                });
                /*=====================================================updateselect ==========================================================================================*/
                $(".allupdateselect").on('change', function () {
// Do something wi.on('change', function () {
                    var thisvalue = $(this).val();
                    var tableName = $(this).attr('select-tableName');
                    var itr = $(this).attr('data-itr');
                    var name = $(this).attr('name');
                    var pervious = $(this).attr('data-val');
                    var patientid = $("#patientId").val();
                    var rowoftableid = $(this).attr('data-id');
                    var maintableName = $(this).attr('data-tableName');
                    if (thisvalue == '-2') {


                        $('#' + name + '' + itr).select2('val', pervious);
                        $.post(hosturl + '/controllers/patienthistory.php?ac=editselect', {tableName: tableName, itr: itr, name: name, rowoftableid: rowoftableid, patientid: patientid, maintableName: maintableName}, function (data) {


                            $("#newitem_name").val('');
                            $("#newitem_id").val('');
                            $("#alldatadiv").html(data);
                            $("#edit_model").modal('show');
                        });
                    }

                })
                $('#pastsurgical_itr').val(pastsurgical_itr);
                pastsurgical_itr++;
            });
        });
        /*=========================================================================edit surgical =========================================================*/
        var surgicallength = Number($('#pastsurgical_itr').val());
        for ($q = 1; $q <= surgicallength; $q++) {
            $('#surgicaloperation' + $q).select2({
                width: '170px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    $thisitr = $(this).attr('data-itr');
                    $id = $(this).attr('data-id');
                    return "<input class='form-control' id='newsurgicalTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addsurgicalNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addsurgicalselected'>Add</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '.addsurgicalselected', function () {
                $thisitr = $(this).attr('data-itr');
                var selectid = $(this).attr('data-id');
                var newitem = $('#newsurgicalTerm' + $thisitr).val();
                $.post(hosturl + '/controllers/patienthistory.php?ac=addphpastsurgical&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                    $('select[name="surgicaloperation"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                    //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="surgicaloperation"]');
                    $('#surgicaloperation' + $thisitr).select2('val', Number(data)); // select the new term
                    $('#surgicaloperation' + $thisitr).select2('close'); // close the dropdown

                });
            });
        }
        /*=====================================================================past Gynecological =============== ===============================================*/
        var gynecological_itr = Number($('#gynecological_itr').val());
        gynecological_itr++;
        $('#gynecological_btn').on("click", function () {
            $.post(hosturl + '/controllers/patienthistory.php?ac=addnewpastgynecological&id=' + patientId, {itr: gynecological_itr}, function (data) {

                $('#gynecological_div').prepend(data);
                ////// medicaldisease select
                $('#gynecologicaloperation' + gynecological_itr).select2({
                    width: '170px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        $thisitr = $(this).attr('data-itr');
                        $id = $(this).attr('data-id');
                        return "<input class='form-control' id='newsurgicalTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addsurgicalNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addsurgicalselected'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addsurgicalselected', function () {

                    $thisitr = $(this).attr('data-itr');
                    var selectid = $(this).attr('data-id');
                    var newitem = $('#newsurgicalTerm' + $thisitr).val();
                    $.post(hosturl + '/controllers/patienthistory.php?ac=addphgynecologicaloperation&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                        $('select[name="gynecologicaloperation"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="gynecologicaloperation"]');
                        $('#gynecologicaloperation' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#gynecologicaloperation' + $thisitr).select2('close'); // close the dropdown

                    });
                });
                /*=====================================================updateselect ==========================================================================================*/
                $(".allupdateselect").on('change', function () {
// Do something wi.on('change', function () {
                    var thisvalue = $(this).val();
                    var tableName = $(this).attr('select-tableName');
                    var itr = $(this).attr('data-itr');
                    var name = $(this).attr('name');
                    var pervious = $(this).attr('data-val');
                    var patientid = $("#patientId").val();
                    var rowoftableid = $(this).attr('data-id');
                    var maintableName = $(this).attr('data-tableName');
                    if (thisvalue == '-2') {


                        $('#' + name + '' + itr).select2('val', pervious);
                        $.post(hosturl + '/controllers/patienthistory.php?ac=editselect', {tableName: tableName, itr: itr, name: name, rowoftableid: rowoftableid, patientid: patientid, maintableName: maintableName}, function (data) {


                            $("#newitem_name").val('');
                            $("#newitem_id").val('');
                            $("#alldatadiv").html(data);
                            $("#edit_model").modal('show');
                        });
                    }

                })

                $('#gynecological_itr').val(gynecological_itr);
                gynecological_itr++;
            });
        });
        /*======================================================edit Gynecological=================================================================================================================*/
        var gynecologicallength = Number($('#gynecological_itr').val());
        for ($e = 1; $e <= gynecologicallength; $e++) {
            $('#gynecologicaloperation' + $e).select2({
                width: '170px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    $thisitr = $(this).attr('data-itr');
                    $id = $(this).attr('data-id');
                    return "<input class='form-control' id='newsurgicalTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addsurgicalNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addsurgicalselected'>Add</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '.addsurgicalselected', function () {

                $thisitr = $(this).attr('data-itr');
                var selectid = $(this).attr('data-id');
                var newitem = $('#newsurgicalTerm' + $thisitr).val();
                $.post(hosturl + '/controllers/patienthistory.php?ac=addphgynecologicaloperation&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                    $('select[name="gynecologicaloperation"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                    //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="gynecologicaloperation"]');
                    $('#gynecologicaloperation' + $thisitr).select2('val', Number(data)); // select the new term
                    $('#gynecologicaloperation' + $thisitr).select2('close'); // close the dropdown

                });
            });
        }


        /*===============================================================================past ART =================================================================================*/
        var pastart_itr = Number($('#pastart_itr').val());
        pastart_itr++;
        $('#pastart_btn').on("click", function () {
            $.post(hosturl + '/controllers/patienthistory.php?ac=addnewpastart&id=' + patientId, {itr: pastart_itr}, function (data) {

                $('#pastart_div').prepend(data);
                ////// medicaldisease select
                $('#arttype' + pastart_itr).select2({
                    width: '170px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        $thisitr = $(this).attr('data-itr');
                        $id = $(this).attr('data-id');
                        return "<input class='form-control' id='newarttypeTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addarttypeNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addartselected'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addartselected', function () {
                    $thisitr = $(this).attr('data-itr');
                    var selectid = $(this).attr('data-id');
                    var newitem = $('#newarttypeTerm' + $thisitr).val();
                    $.post(hosturl + '/controllers/patienthistory.php?ac=addphpastarttype&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                        $('select[name="arttype"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="arttype"]');
                        $('#arttype' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#arttype' + $thisitr).select2('close'); // close the dropdown

                    });
                });
                $('#artcenter' + pastart_itr).select2({
                    width: '170px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        $thisitr = $(this).attr('data-itr');
                        $id = $(this).attr('data-id');
                        return "<input class='form-control' id='newartcenterTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addartcenterNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addartcenterselect'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addartcenterselect', function () {
                    $thisitr = $(this).attr('data-itr');
                    var selectid = $(this).attr('data-id');
                    var newitem = $('#newartcenterTerm' + $thisitr).val();
                    $.post(hosturl + '/controllers/patienthistory.php?ac=addphpastartcenter&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                        $('select[name="artcenter"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="artcenter"]');
                        $('#artcenter' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#artcenter' + $thisitr).select2('close'); // close the dropdown

                    });
                });
                /*=====================================================updateselect ==========================================================================================*/
                $(".allupdateselect").on('change', function () {
// Do something wi.on('change', function () {
                    var thisvalue = $(this).val();
                    var tableName = $(this).attr('select-tableName');
                    var itr = $(this).attr('data-itr');
                    var name = $(this).attr('name');
                    var pervious = $(this).attr('data-val');
                    var patientid = $("#patientId").val();
                    var rowoftableid = $(this).attr('data-id');
                    var maintableName = $(this).attr('data-tableName');
                    if (thisvalue == '-2') {


                        $('#' + name + '' + itr).select2('val', pervious);
                        $.post(hosturl + '/controllers/patienthistory.php?ac=editselect', {tableName: tableName, itr: itr, name: name, rowoftableid: rowoftableid, patientid: patientid, maintableName: maintableName}, function (data) {

                            //alert(data);
                            $("#newitem_name").val('');
                            $("#newitem_id").val('');
                            $("#alldatadiv").html(data);
                            $("#edit_model").modal('show');
                        });
                    }

                })

                $('#pastart_itr').val(pastart_itr);
                pastart_itr++;
            });
        });
        /*===============================================================================edit past ART =================================================================================*/
        var pastartlength = Number($('#pastart_itr').val());
        for ($x = 1; $x <= pastartlength; $x++) {
            $('#arttype' + $x).select2({
                width: '170px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    $thisitr = $(this).attr('data-itr');
                    $id = $(this).attr('data-id');
                    return "<input class='form-control' id='newarttypeTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addarttypeNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addartselected'>Add</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '.addartselected', function () {
                $thisitr = $(this).attr('data-itr');
                var selectid = $(this).attr('data-id');
                var newitem = $('#newarttypeTerm' + $thisitr).val();
                $.post(hosturl + '/controllers/patienthistory.php?ac=addphpastarttype&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                    $('select[name="arttype"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                    //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="arttype"]');
                    $('#arttype' + $thisitr).select2('val', Number(data)); // select the new term
                    $('#arttype' + $thisitr).select2('close'); // close the dropdown

                });
            });
            $('#artcenter' + $x).select2({
                width: '170px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    $thisitr = $(this).attr('data-itr');
                    $id = $(this).attr('data-id');
                    return "<input class='form-control' id='newartcenterTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addartcenterNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addartcenterselect'>Add</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '.addartcenterselect', function () {
                $thisitr = $(this).attr('data-itr');
                var selectid = $(this).attr('data-id');
                var newitem = $('#newartcenterTerm' + $thisitr).val();
                $.post(hosturl + '/controllers/patienthistory.php?ac=addphpastartcenter&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                    $('select[name="artcenter"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                    //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="artcenter"]');
                    $('#artcenter' + $thisitr).select2('val', Number(data)); // select the new term
                    $('#artcenter' + $thisitr).select2('close'); // close the dropdown

                });
            });
        }


        /*==========================ph family ===================================================================================================*/

        var family_itr = Number($('#family_itr').val());
        family_itr++;
        $('#family_btn').on("click", function () {
            $.post(hosturl + '/controllers/patienthistory.php?ac=addnewFamily&id=' + patientId, {itr: family_itr}, function (data) {

                $('#family_div').prepend(data);
                ////// obterminate select
                $('#family_disease' + family_itr).select2({
                    width: '170px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        $thisitr = $(this).attr('data-itr');
                        $id = $(this).attr('data-id');
                        return "<input class='form-control' id='newfamDiseaseTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addfamDiseaseNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addfamDiseaseselected'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addfamDiseaseselected', function () {

                    $thisitr = $(this).attr('data-itr');
                    var selectid = $(this).attr('data-id');
                    var newitem = $('#newfamDiseaseTerm' + $thisitr).val();
                    $.post(hosturl + '/controllers/patienthistory.php?ac=addphFamilyDisease&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                        $('select[name="family_disease"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="family_disease"]');
                        $('#family_disease' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#family_disease' + $thisitr).select2('close'); // close the dropdown

                    });
                });
                ////// obsplace select
                $('#family_relative' + family_itr).select2({
                    width: '170px',
                    allowClear: true,
                    formatNoMatches: function (term) {
                        /* customize the no matches output */
                        $thisitr = $(this).attr('data-itr');
                        $id = $(this).attr('data-id');
                        return "<input class='form-control' id='newfamRelativeTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addfamRelativeNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addfamRelativeselected'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addfamRelativeselected', function () {
                    $thisitr = $(this).attr('data-itr');
                    var selectid = $(this).attr('data-id');
                    var newitem = $('#newfamRelativeTerm' + $thisitr).val();
                    $.post(hosturl + '/controllers/patienthistory.php?ac=addphFamilyRelative&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                        $('select[name="family_relative"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="family_relative"]');
                        $('#family_relative' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#family_relative' + $thisitr).select2('close'); // close the dropdown

                    });
                });
                /*=====================================================updateselect ==========================================================================================*/
                $(".allupdateselect").on('change', function () {
// Do something wi.on('change', function () {
                    var thisvalue = $(this).val();
                    var tableName = $(this).attr('select-tableName');
                    var itr = $(this).attr('data-itr');
                    var name = $(this).attr('name');
                    var pervious = $(this).attr('data-val');
                    var patientid = $("#patientId").val();
                    var rowoftableid = $(this).attr('data-id');
                    var maintableName = $(this).attr('data-tableName');
                    if (thisvalue == '-2') {


                        $('#' + name + '' + itr).select2('val', pervious);
                        $.post(hosturl + '/controllers/patienthistory.php?ac=editselect', {tableName: tableName, itr: itr, name: name, rowoftableid: rowoftableid, patientid: patientid, maintableName: maintableName}, function (data) {


                            $("#newitem_name").val('');
                            $("#newitem_id").val('');
                            $("#alldatadiv").html(data);
                            $("#edit_model").modal('show');
                        });
                    }

                })
                $('#family_itr').val(family_itr);
                family_itr++;
            });
        });
        /*======================================================================================edit family======================================================================*/
        var familylength = Number($('#family_itr').val());
        for ($f = 1; $f <= familylength; $f++) {
            $('#family_disease' + $f).select2({
                width: '170px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    $thisitr = $(this).attr('data-itr');
                    $id = $(this).attr('data-id');
                    return "<input class='form-control' id='newfamDiseaseTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addfamDiseaseNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addfamDiseaseselected'>Add</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '.addfamDiseaseselected', function () {

                $thisitr = $(this).attr('data-itr');
                var selectid = $(this).attr('data-id');
                var newitem = $('#newfamDiseaseTerm' + $thisitr).val();
                $.post(hosturl + '/controllers/patienthistory.php?ac=addphFamilyDisease&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                    $('select[name="family_disease"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                    //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="family_disease"]');
                    $('#family_disease' + $thisitr).select2('val', Number(data)); // select the new term
                    $('#family_disease' + $thisitr).select2('close'); // close the dropdown

                });
            });
            ////// obsplace select
            $('#family_relative' + $f).select2({
                width: '170px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    $thisitr = $(this).attr('data-itr');
                    $id = $(this).attr('data-id');
                    return "<input class='form-control' id='newfamRelativeTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addfamRelativeNew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "' class='btn btn-default addfamRelativeselected'>Add</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '.addfamRelativeselected', function () {
                $thisitr = $(this).attr('data-itr');
                var selectid = $(this).attr('data-id');
                var newitem = $('#newfamRelativeTerm' + $thisitr).val();
                $.post(hosturl + '/controllers/patienthistory.php?ac=addphFamilyRelative&id=' + patientId, {newitem: newitem, id: selectid, inline: 1}, function (data) {

                    $('select[name="family_relative"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                    //$('<option value="' + Number(data) + '" >' + newitem + '</option>').appendTo('select[name="family_relative"]');
                    $('#family_relative' + $thisitr).select2('val', Number(data)); // select the new term
                    $('#family_relative' + $thisitr).select2('close'); // close the dropdown

                });
            });
        }




        /*=======================================update select ============================================================================================================*/

        $(".allupdateselect").on('change', function () {
// Do something wi.on('change', function () {
            var thisvalue = $(this).val();
            var tableName = $(this).attr('select-tableName');
            var itr = $(this).attr('data-itr');
            var name = $(this).attr('name');
            var pervious = $(this).attr('data-val');
            var patientid = $("#patientId").val();
            var rowoftableid = $(this).attr('data-id');
            var maintableName = $(this).attr('data-tableName');
            if (thisvalue == '-2') {


                $('#' + name + '' + itr).select2('val', pervious);
                $.post(hosturl + '/controllers/patienthistory.php?ac=editselect', {tableName: tableName, itr: itr, name: name, rowoftableid: rowoftableid, patientid: patientid, maintableName: maintableName}, function (data) {


                    $("#newitem_name").val('');
                    $("#newitem_id").val('');
                    $("#alldatadiv").html(data);
                    $("#edit_model").modal('show');
                });
            }

        })



        $(".cl_choosename").live('click', function ()
        {
            var id = $(this).attr('data-id');
            var name = $(this).text();
            $("#newitem_name").val(name);
            $("#newitem_id").val(id);
        });
        $("#editmodel_submit").live('click', function () {


            var value = $("#newitem_name").val();
            var id = $("#newitem_id").val();
            var tablename = $("#tablename").val();
            var itr = $("#itr").val();
            var selectname = $("#selectname").val();
            var patientid = $("#patientid").val();
            var rowoftableid = $("#rowoftableid").val();
            var maintableName = $("#maintableName").val();
            if (id != '') {
                $.post(hosturl + '/controllers/patienthistory.php?ac=updateselect', {tablename: tablename, id: id, value: value, rowoftableid: rowoftableid, patientid: patientid, maintableName: maintableName, selectname: selectname}, function (data) {


                    $("." + selectname + " option[value='" + id + "']").text(value);
                    $('#' + selectname + "" + itr).select2('val', id);
                    $('#edit_model').modal('hide');
                });
            }
        });
        /*===========================================================babay_type===========================================================================================*/

        $('.outcomeid').live('change', function (e) {
            var i = $(this).attr('data-itr');
            var babyvalue = $(this).val();
            if (babyvalue == 0 || babyvalue == 1)
            {
                $("#baby_div" + i).show();
                $("#baby_div1" + i).hide();
                $("#baby_div2" + i).hide();
                $("#genderdiv" + i).css('display', 'none');
                $("#babyidcolnew" + i).removeClass('col-md-2');
                $("#babyidcolnew" + i).addClass('col-md-4');
            }

            else if (babyvalue == 2)
            {
                $("#baby_div" + i).show();
                $("#baby_div1" + i).show();
                $("#baby_div2" + i).hide();
                $("#genderdiv" + i).css('display', 'inline');
                $("#babyidcolnew" + i).removeClass('col-md-4');
                $("#babyidcolnew" + i).addClass('col-md-2');
            }

            else if (babyvalue == 3)
            {
                $("#baby_div" + i).show();
                $("#baby_div1" + i).show();
                $("#baby_div2" + i).show();
                $("#genderdiv" + i).css('display', 'inline');
                $("#babyidcolnew" + i).removeClass('col-md-4');
                $("#babyidcolnew" + i).addClass('col-md-2');
            }

            else {
                $("#baby_div" + i).hide();
                $("#baby_div1" + i).hide();
                $("#baby_div2" + i).hide();
                $("#babyidcolnew" + i).removeClass('col-md-4');
                $("#babyidcolnew" + i).addClass('col-md-2');
            }

        });
        $('.obstermination').live('change', function (e) {

            var terminatevalue = $(this).val();

            var i = $(this).attr('data-itr');

            if (terminatevalue == 1 || terminatevalue == 2)
            {


                $('#allbabydivs' + i).css('display', 'block');
                $('#baby_div' + i).css('display', 'block');
                $("#nooutdata" + i).css('display', 'none');
                $('#baby_div1' + i).css('display', 'none');
                $('#baby_div2' + i).css('display', 'none');
                $('#obstermination' + i).select2('val', terminatevalue);


            } else
            {
                $('#allbabydivs' + i).css('display', 'none');
                $("#nooutdata" + i).css('display', 'block');
                $('#baby_div' + i).css('display', 'none');
                $('#baby_div1' + i).css('display', 'none');
                $('#baby_div2' + i).css('display', 'none');
                //  $("#nooutdatavv" + i).css('display', 'block');
            }
        });


        if ($('.outcomeid').length > 0)
        {

            $('.outcomeid').each(function () {
                var id = $(this).attr('data-itr');
                var thisvalue = $(this).val();

                if (thisvalue == 0 || thisvalue == 1) {
                    $("#babyidcolnew" + id).removeClass('col-md-2');
                    $("#babyidcolnew" + id).addClass('col-md-4');
                }
                else {

                    $("#babyidcolnew" + id).removeClass('col-md-4');
                    $("#babyidcolnew" + id).addClass('col-md-2');
                }
            })
        }

        $("#addnewRow").live('click', function () {
            var tablename = $("#tablename").val();
            var selectname = $("#selectname").val();
            var dtcount = $("#dtcount").val();
            dtcount++;
            $.post(hosturl + "/controllers/patienthistory.php?ac=addNewRow", {tablename: tablename}, function (data) {
                var dataid = Number(data);
                $('.' + selectname + ' > .deleteoptincompo').before('<option value="' + dataid + '" ></option>');
                if (tablename == "phobstericterminate")
                {
                    $("#dtAddTbl").prepend('<tr>\
                                                <td style="width: 100%">\
                                                    <a href="#" >\
                                                        <input type="text" class="form-control updateElementSelect" data-selectClass="' + selectname + '"  data-select2="yes" data-id="' + dataid + '" data-colName="name" data-tableName="' + tablename + '" value=""/>\
                                                    </a>\
                                                </td>\
                                                <td>\
                                                    <a class="fa fa-times btn red fleft deleteElementmodelnew"  data-link="patienthistory" data-fn="delRowsommedel" data-id="' + dataid + '" data-tableName="' + tablename + '"></a>\
                                                    </a>\
                                                </td>\
                                            </tr>');
                }
                else
                {
                    $("#dtAddTbl").prepend('<tr>\
                                                <td style="width: 100%">\
                                                    <a href="#" >\
                                                        <input type="text" class="form-control updateElementSelect" data-selectClass="' + selectname + '"  data-select2="yes" data-id="' + dataid + '" data-colName="name" data-tableName="' + tablename + '" value=""/>\
                                                    </a>\
                                                </td>\
                                                <td>\
                                                    <a class=" fa fa-times btn red fleft deleteElementmodelnew"  data-link="patienthistory" data-fn="delRowsommedel" data-id="' + dataid + '" data-tableName="' + tablename + '" ></a>\
                                                </td>\
                                            </tr>');
                }
            });
            $("#dtcount").val(dtcount);
        });


    }
    return {
//main function to initiate the theme
        init: function () {

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