if ($("#ultrasoundPage").val() == 1)
{
    var hosturl = $('#hosturl').val();
    var patientId = $('#patientId').val();
    var controllName = $("#controllName").val();
    var mcount = $("#model_count").val();

    $(".updateCheckbox").live('click', function ()
    {
        var id = $(this).attr("data-id");
        var colName = $(this).attr("data-colName");
        var tableName = $(this).attr("data-tableName");
        var value;

        if ($(this).is(":checked"))
        {
            var value = 1;
            if ($(this).attr('id') == "conp")
            {
                $("#plansDiv").css('display', 'inline');

                if ($('#followplan').is(':checked'))
                {
                    $("#weeks").css('display', 'inline');
                    $("#refto").css('display', 'none');
                    $("#otherptxt").css('display', 'none');
                }
                else if ($('#otherpl').is(':checked'))
                {
                    $("#weeks").css('display', 'none');
                    $("#refto").css('display', 'none');
                    $("#otherptxt").css('display', 'inline');
                }
                else if ($('#referplan').is(':checked'))
                {
                    $("#weeks").css('display', 'none');
                    $("#refto").css('display', 'inline');
                    $("#otherptxt").css('display', 'none');
                }
            }
        }
        else
        {
            var value = 0;
            if ($(this).attr('id') == "conp")
            {
                $("#plansDiv").css('display', 'none');
            }
        }

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

    $(".sradio").live('click', function () {
        var id = $(this).attr("data-id");
        var colName = $(this).attr("data-colName");
        var tableName = $(this).attr("data-tableName");
        var value = $(this).attr('data-val');

        var thisId = $(this).attr('id');
        if (colName == "based" && thisId == "otherb")
        {
            $("#bother").css('display', 'inline');
        }
        else if (colName == "based" && thisId != "otherb")
        {
            $("#bother").css('display', 'none');
        }

        if (colName == "tcondition" && thisId == "limitedt")
        {
            $("#limitcond").css('display', 'inline');
        }
        else if (colName == "tcondition" && thisId != "limitedt")
        {
            $("#limitcond").css('display', 'none');
        }

        if (colName == "babyno" && thisId == "many")
        {
            $("#babyNo").css('display', 'inline');
        }
        else if (colName == "babyno" && thisId != "many")
        {
            if (mcount < 2)
            {
                $("#babyNo").css('display', 'none');
            }
            else
            {
                $("#many").attr('checked', 'checked');
                return;
            }

        }

        if (colName == "relationc" && thisId == "cover")
        {
            $("#coverDiv").css('display', 'inline');
        }
        else if (colName == "relationc" && thisId != "cover")
        {
            $("#coverDiv").css('display', 'none');
        }
        if (colName == "conclusion" && thisId == "conp")
        {
            $("#plansDiv").css('display', 'inline');

            if ($('#followplan').is(':checked'))
            {
                $("#weeks").css('display', 'inline');
                $("#refto").css('display', 'none');
                $("#otherptxt").css('display', 'none');
            }
            else if ($('#otherpl').is(':checked'))
            {
                $("#weeks").css('display', 'none');
                $("#refto").css('display', 'none');
                $("#otherptxt").css('display', 'inline');
            }
            else if ($('#referplan').is(':checked'))
            {
                $("#weeks").css('display', 'none');
                $("#refto").css('display', 'inline');
                $("#otherptxt").css('display', 'none');
            }
        }
        else if (colName == "conclusion" && thisId != "conp")
        {
            $("#plansDiv").css('display', 'none');
        }
        if (thisId == "followplan")
        {
            $("#weeks").css('display', 'inline');
            $("#refto").css('display', 'none');
            $("#otherptxt").css('display', 'none');
        }
        else if (thisId == "referplan")
        {
            $("#weeks").css('display', 'none');
            $("#refto").css('display', 'inline');
            $("#otherptxt").css('display', 'none');
        }
        else if (thisId == "otherpl")
        {
            $("#weeks").css('display', 'none');
            $("#refto").css('display', 'none');
            $("#otherptxt").css('display', 'inline');
        }
        else if (thisId == "noscan")
        {
            $("#weeks").css('display', 'none');
            $("#refto").css('display', 'none');
            $("#otherptxt").css('display', 'none');
        }

        $.post(hosturl + '/controllers/' + controllName + '.php?ac=updateradio', {id: id, value: value, tableName: tableName, colName: colName}, function (res)
        {
            if (colName == "stype")
            {
                $('#sName').html();
                $('#sName').html(res);
            }
        });
    });

    $(".showMod").live('click', function () {
        var id = $(this).attr('data-id');
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=showdetails', {id: id}, function (data)
        {
            $("#yy").html(data);
            //$("#ShowBabyModel").modal("show");
        });
    });
    $(".closeDiv").live('click', function () {
        $("#yy").html('');
    });
    $("#single").live('click', function () {
        var id = $(this).attr('data-id');
        if (mcount > 1)
        {
            alert("use Delete!!");
        }
    });
    $("#twinz").live('click', function () {
        var id = $(this).attr('data-id');
        if (mcount > 2)
        {
            alert("use Delete!!");
        }
        else
        {
            mcount = 2;
            $("#model_count").val(2);

            $.post(hosturl + '/controllers/' + controllName + '.php?ac=addDeteail', {id: id}, function (data)
            {
                var detailid = Number(data);
                $("#mainForm").append('<a class="btn btn-default btn-sm showMod" data-id="' + detailid + '">\
                                       <i class="fa fa-plus"></i> Baby </a>\
                                       <button type="button" class="btn btn-xs red fright removeMsg" style="position: absolute; margin-left: -5px; height:29px;" data-tableName="ultrasounddetail" data-id="' + detailid + '"><i class="fa fa-close"></i></button>');
            });

        }
    });
    $("#many").live('click', function () {
        if (mcount < 3)
        {
            $("#babyNotxt").val('3');
            $("#babyNotxt").trigger('change');
        }
    });
    $("#babyNotxt").on('change', function () {
        var id = $(this).attr('data-id');
        var colName = $(this).attr("data-colName");
        var tableName = $(this).attr("data-tableName");
        var oldc = $("#model_count").val();
        mcount = $(this).val();
        if (mcount > 2 && mcount > oldc)
        {
            for (var i = +oldc + +1; i <= mcount; i++)
            {
                $.post(hosturl + '/controllers/' + controllName + '.php?ac=addDeteail', {id: id}, function (data)
                {
                    var detailid = Number(data);
                    $("#mainForm").append('<a class="btn btn-default btn-sm showMod" style="margin-left: 30px;" data-id="' + detailid + '">\
                                           <i class="fa fa-plus"></i> Baby </a>\
                                           <button type="button" class="btn btn-xs red fright removeMsg" style="position: absolute; margin-left: -5px; height:29px;" data-tableName="ultrasounddetail" data-id="' + detailid + '"><i class="fa fa-close"></i></button>');
                });
            }
        }
        else
        {
            alert("use other choices Or delete");
            mcount = oldc;
            $.post(hosturl + '/controllers/' + controllName + '.php?ac=update', {id: id, value: mcount, tableName: tableName, colName: colName, patientId: patientId}, function (res)
            {
            });
        }
        $("#model_count").val(mcount);
        $(this).val(mcount);
    });


    $('.removeMsg').live('click', function () {
        var itemid = $(this).attr('data-id');
        var tableName = $(this).attr('data-tableName');
        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
                        mcount = +mcount + -1;
                        $("#model_count").val(mcount);
                        $("#babyNotxt").val(mcount);

                        window.location.href = hosturl + "/controllers/" + controllName + ".php?ac=delData&id=" + itemid + "&tableName=" + tableName + "&patientid=" + patientId;
                    }
                }
            }
        }).find('.modal-content').addClass('modal-sm');
    });

    $('.delMsg').live('click', function () {
        var itemid = $(this).attr('data-id');
        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=delRow&id=" + itemid + "&patientid=" + patientId;
                    }
                }
            }
        }).find('.modal-content').addClass('modal-sm');
    });

    $("#doneBtn").on('click', function () {
        var id = $(this).attr('data-id');
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=update', {id: id, value: 1, tableName: "ultrasound", colName: "done", patientId: patientId}, function (res)
        {
            window.location.href = hosturl + "/controllers/" + controllName + ".php?ac=index&patientid=" + patientId;
        });
    });
    /*$('#gprint').live('click', function () {
     $(this).hide();
     $(".hidPrint").hide();
     $.print("#gprinModel");
     $(".hidPrint").show();
     $(this).show();
     });*/

    $(".showPrint").live('click', function () {
        var id = $(this).attr('data-detailid');
        // window.location.href = hosturl + "/controllers/" + controllName + ".php?ac=showprint&id=" + id+"&patientid="+patientId;
        $.post(hosturl + '/controllers/' + controllName + '.php?ac=showprint', {id: id, patientid: patientId}, function (data)
        {
            $("#vvv").html(data);
            $.print("#vvv");
            $("#vvv").html('');
        });



    });
    $('.datepickertoday').datepicker('setDate', '+0');
    $(".datepicker").datepicker({
        todayBtn: "linked",
        // autoclose: true,
        todayHighlight: true,
        format: 'yy/mm/dd',
        isRTL: true
    });
}