var examination = function () {

    var examination_new = function () {
        var hosturl = $('#hosturl').val();
        var patientId = $("#patientId").val();

        var exam_itr = $('#exam_itr').val();

//###########################################################################
        for ($i = 1; $i <= exam_itr; $i++) {
            if (Number($('#headSelect').val()) == 1)
            {
                $('#examinationhead' + $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='newheadTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addheadnew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "'  class='btn btn-default addnewheadselect'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addnewheadselect', function () {
                    $thisitr = $(this).attr('data-itr');
                    var newitem = $('#newheadTerm' + $thisitr).val();
                    var selectid = $(this).attr('data-id');
                    $.post(hosturl + '/controllers/examination.php?ac=addexitem&id=' + patientId, {newitem: newitem, id: selectid, inline: 1, table: 'examinationhead', colum: 'headandneck'}, function (data) {
                        $('select[name="headandneck"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        $('#examinationhead' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#examinationhead' + $thisitr).select2('close'); // close the dropdown

                    });
                });
            }

            if (Number($('#chestSelect').val()) == 1)
            {
                $('#examinationchest' + $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='newchestTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addchestnew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "'  class='btn btn-default addnewchestselect'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addnewchestselect', function () {
                    $thisitr = $(this).attr('data-itr');
                    var newitem = $('#newchestTerm' + $thisitr).val();
                    var selectid = $(this).attr('data-id');
                    $.post(hosturl + '/controllers/examination.php?ac=addexitem&id=' + patientId, {newitem: newitem, id: selectid, inline: 1, table: 'examinationchest', colum: 'chestandheart'}, function (data) {
                        $('select[name="chestandheart"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        $('#examinationchest' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#examinationchest' + $thisitr).select2('close'); // close the dropdown

                    });
                });
            }

            if (Number($('#abdomenSelect').val()) == 1)
            {
                $('#examinationabdomen' + $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='newabdomenTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addabdomennew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "'  class='btn btn-default addnewabdomenselect'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addnewabdomenselect', function () {
                    $thisitr = $(this).attr('data-itr');
                    var newitem = $('#newabdomenTerm' + $thisitr).val();
                    var selectid = $(this).attr('data-id');
                    $.post(hosturl + '/controllers/examination.php?ac=addexitem&id=' + patientId, {newitem: newitem, id: selectid, inline: 1, table: 'examinationabdomen', colum: 'abdomen'}, function (data) {
                        $('select[name="abdomen"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        $('#examinationabdomen' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#examinationabdomen' + $thisitr).select2('close'); // close the dropdown

                    });
                });
            }

            if (Number($('#pelvisSelect').val()) == 1)
            {
                $('#examinationpelvis' + $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='newpelvisTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addpelvisnew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "'  class='btn btn-default addnewpelvisselect'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addnewpelvisselect', function () {
                    $thisitr = $(this).attr('data-itr');
                    var newitem = $('#newpelvisTerm' + $thisitr).val();
                    var selectid = $(this).attr('data-id');
                    $.post(hosturl + '/controllers/examination.php?ac=addexitem&id=' + patientId, {newitem: newitem, id: selectid, inline: 1, table: 'examinationpelvis', colum: 'pelvis'}, function (data) {
                        $('select[name="pelvis"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        $('#examinationpelvis' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#examinationpelvis' + $thisitr).select2('close'); // close the dropdown

                    });
                });
            }

            if (Number($('#extremitisSelect').val()) == 1)
            {
                $('#examinationextremitis' + $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='newextremitisTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addextremitisnew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "'  class='btn btn-default addnewextremitisselect'>Add</a>"
                    }
                }).parent().find('.select2-with-searchbox').on('click', '.addnewextremitisselect', function () {
                    $thisitr = $(this).attr('data-itr');
                    var newitem = $('#newextremitisTerm' + $thisitr).val();
                    var selectid = $(this).attr('data-id');
                    $.post(hosturl + '/controllers/examination.php?ac=addexitem&id=' + patientId, {newitem: newitem, id: selectid, inline: 1, table: 'examinationextremitis', colum: 'extremitis'}, function (data) {
                        $('select[name="extremitis"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                        $('#examinationextremitis' + $thisitr).select2('val', Number(data)); // select the new term
                        $('#examinationextremitis' + $thisitr).select2('close'); // close the dropdown

                    });
                });
            }

        }

        $(".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');
        });

        $("#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);
        });
//###########################################################################
        $(".bp_mask").inputmask("999/999", {
            autoUnmask: true});
        //// add new row by smarty in another html
        $('#btn_exmination').on("click", function () {

            var exam_itr = $("#exam_itr").val();
            exam_itr++;
            $.post(hosturl + '/controllers/examination.php?ac=addNewRow&id=' + patientId + ' &itr=' + exam_itr, function (data) {

                $('#div_exmination').prepend(data);

                $(".bp_mask").inputmask("999/999", {
                    autoUnmask: true});

                //// for calender
                $(".datepicker").datepicker({
                    todayBtn: "linked",
                    // autoclose: true,
                    todayHighlight: true,
                    format: 'dd/mm/yy',
                    isRTL: true
                });

                $('.calcbm').live('keyup', function () {

                    var patientId = $("#patientId").val();
                    var hosturl = $("#hosturl").val();
                    var id = $(this).attr('data-id');
                    var wt = $('#weight' + id).val();
                    var ht = $('#height' + id).val();
                    if (wt != '' && ht != '' && ht > 0) {
                        var res = wt / (ht * ht);
                        var newres = Math.ceil(res)
                        //var newres = res.toFixed(2);
                        $('#bmi' + id).val(newres);


                        $.post(hosturl + '/controllers/examination.php?ac=update', {id: id, value: newres, tableName: 'examination', colName: 'bmi', patientId: patientId}, function (res)
                        {

                        });
                    }
                    //newres = $('#hidden_bmi').val();
                });

                $(".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');
                        });
                    }

                });

                if (Number($('#headSelect').val()) == 1)
                {
                    $('#examinationhead' + exam_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='newheadTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addheadnew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "'  class='btn btn-default addnewheadselect'>Add</a>"
                        }
                    }).parent().find('.select2-with-searchbox').on('click', '.addnewheadselect', function () {
                        $thisitr = $(this).attr('data-itr');
                        var newitem = $('#newheadTerm' + $thisitr).val();
                        var selectid = $(this).attr('data-id');
                        $.post(hosturl + '/controllers/examination.php?ac=addexitem&id=' + patientId, {newitem: newitem, id: selectid, inline: 1, table: 'examinationhead', colum: 'headandneck'}, function (data) {
                            $('select[name="headandneck"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                            $('#examinationhead' + $thisitr).select2('val', Number(data)); // select the new term
                            $('#examinationhead' + $thisitr).select2('close'); // close the dropdown

                        });
                    });
                }

                if (Number($('#chestSelect').val()) == 1)
                {
                    $('#examinationchest' + exam_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='newchestTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addchestnew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "'  class='btn btn-default addnewchestselect'>Add</a>"
                        }
                    }).parent().find('.select2-with-searchbox').on('click', '.addnewchestselect', function () {
                        $thisitr = $(this).attr('data-itr');
                        var newitem = $('#newchestTerm' + $thisitr).val();
                        var selectid = $(this).attr('data-id');
                        $.post(hosturl + '/controllers/examination.php?ac=addexitem&id=' + patientId, {newitem: newitem, id: selectid, inline: 1, table: 'examinationchest', colum: 'chestandheart'}, function (data) {
                            $('select[name="chestandheart"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                            $('#examinationchest' + $thisitr).select2('val', Number(data)); // select the new term
                            $('#examinationchest' + $thisitr).select2('close'); // close the dropdown

                        });
                    });
                }

                if (Number($('#abdomenSelect').val()) == 1)
                {
                    $('#examinationabdomen' + exam_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='newabdomenTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addabdomennew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "'  class='btn btn-default addnewabdomenselect'>Add</a>"
                        }
                    }).parent().find('.select2-with-searchbox').on('click', '.addnewabdomenselect', function () {
                        $thisitr = $(this).attr('data-itr');
                        var newitem = $('#newabdomenTerm' + $thisitr).val();
                        var selectid = $(this).attr('data-id');
                        $.post(hosturl + '/controllers/examination.php?ac=addexitem&id=' + patientId, {newitem: newitem, id: selectid, inline: 1, table: 'examinationabdomen', colum: 'abdomen'}, function (data) {
                            $('select[name="abdomen"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                            $('#examinationabdomen' + $thisitr).select2('val', Number(data)); // select the new term
                            $('#examinationabdomen' + $thisitr).select2('close'); // close the dropdown

                        });
                    });
                }

                if (Number($('#pelvisSelect').val()) == 1)
                {
                    $('#examinationpelvis' + exam_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='newpelvisTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addpelvisnew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "'  class='btn btn-default addnewpelvisselect'>Add</a>"
                        }
                    }).parent().find('.select2-with-searchbox').on('click', '.addnewpelvisselect', function () {
                        $thisitr = $(this).attr('data-itr');
                        var newitem = $('#newpelvisTerm' + $thisitr).val();
                        var selectid = $(this).attr('data-id');
                        $.post(hosturl + '/controllers/examination.php?ac=addexitem&id=' + patientId, {newitem: newitem, id: selectid, inline: 1, table: 'examinationpelvis', colum: 'pelvis'}, function (data) {
                            $('select[name="pelvis"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                            $('#examinationpelvis' + $thisitr).select2('val', Number(data)); // select the new term
                            $('#examinationpelvis' + $thisitr).select2('close'); // close the dropdown

                        });
                    });
                }

                if (Number($('#extremitisSelect').val()) == 1)
                {
                    $('#examinationextremitis' + exam_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='newextremitisTerm" + $thisitr + "' value='" + term + "' data-itr='" + $thisitr + "' data-id='" + $id + "'><a href='#' id='addextremitisnew" + $thisitr + "' data-itr='" + $thisitr + "' data-id='" + $id + "'  class='btn btn-default addnewextremitisselect'>Add</a>"
                        }
                    }).parent().find('.select2-with-searchbox').on('click', '.addnewextremitisselect', function () {
                        $thisitr = $(this).attr('data-itr');
                        var newitem = $('#newextremitisTerm' + $thisitr).val();
                        var selectid = $(this).attr('data-id');
                        $.post(hosturl + '/controllers/examination.php?ac=addexitem&id=' + patientId, {newitem: newitem, id: selectid, inline: 1, table: 'examinationextremitis', colum: 'extremitis'}, function (data) {
                            $('select[name="extremitis"] > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + newitem + '</option>');
                            $('#examinationextremitis' + $thisitr).select2('val', Number(data)); // select the new term
                            $('#examinationextremitis' + $thisitr).select2('close'); // close the dropdown

                        });
                    });
                }


            });
            $("#exam_itr").val(exam_itr);
        });

        $('.deleteExamination').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();
            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 + "&tableName=" + tableName;


                            $.post(hosturl + '/controllers/' + controllName + '.php?ac=delData', {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');
        });

        $('.calcbm').live('keyup', function (e) {
            var patientId = $("#patientId").val();
            var hosturl = $("#hosturl").val();
            var id = $(this).attr('data-id');
            var wt = $('#weight' + id).val();
            var ht = $('#height' + id).val();
            if (wt != '' && ht != '' && ht > 0) {
                var res = wt / (ht * ht);
                var newres = Math.ceil(res)
                $('#bmi' + id).val(newres);
                $('#patientsBMI').html('BMI : ' + newres);


                $.post(hosturl + '/controllers/examination.php?ac=update', {id: id, value: newres, tableName: 'examination', colName: 'bmi', patientId: patientId}, function (res)
                {

                });
            }
            //newres = $('#hidden_bmi').val();
        });

        $(".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');
                });
            }

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

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

}();
