$(document).ready(function () {
  

    $("#jqxLoader").jqxLoader({ width: 100, height: 60, imagePosition: 'top' });
    var gridf5view = $("#gridf5view");

    var txtbillno = $("#txtbillno");
    txtbillno.jqxInput({ height: 20, width: '60%', disabled: true });
    var txtserialno = $("#txtserialno");
    txtserialno.jqxInput({ height: 20, width: '60%', disabled: true });
    var dtpfromdate1 = $("#dtpfromdate1");
    dtpfromdate1.jqxDateTimeInput({ formatString: 'dd/MM/yyyy', height: '20px', width: '60%' });
    var cmbmahalname = $("#cmbmahalname");
    cmbmahalname.jqxComboBox({ width: '87%', height: '25px' });
    var txtaddress1 = $("#txtaddress1");
    txtaddress1.jqxInput({ height: 20, width: '60%' });
    var txtregistrant = $("#txtregistrant");
    txtregistrant.jqxInput({ height: 20, width: '60%' });
    var txtcity = $("#txtcity");
    txtcity.jqxInput({ height: 20, width: '60%' });
    var txtcontactno = $("#txtcontactno");
    txtcontactno.jqxInput({ height: 20, width: '60%' });
    var txtfunctionname = $("#txtfunctionname");
    txtfunctionname.jqxInput({ height: 20, width: '60%' });
    var dtpfunctiondate = $("#dtpfunctiondate");
    dtpfunctiondate.jqxDateTimeInput({ formatString: 'dd/MM/yyyy', height: '25px', width: '61%' });
    var txtfunctionday = $("#txtfunctionday");
    txtfunctionday.jqxInput({ height: 20, width: '60%', disabled: true, });
    $("#chkWithout").jqxCheckBox({ width: 135, height: 25 });
    var cmbfunctiontime = $("#cmbfunctiontime");
    cmbfunctiontime.jqxComboBox({ width: '87%', height: '25px' });
    var txtrent = $("#txtrent");
    txtrent.jqxInput({ height: 20, width: '60%' });
    var txtdeposit = $("#txtdeposit");
    txtdeposit.jqxInput({ height: 20, width: '60%' });
    var txtadvance = $("#txtadvance");
    txtadvance.jqxInput({ height: 20, width: '60%' });
    var txtcurrentamt = $("#txtcurrentamt");
    txtcurrentamt.jqxInput({ height: 20, width: '60%' });
    var txtgenerator = $("#txtgenerator");
    txtgenerator.jqxInput({ height: 20, width: '60%' });
    var cmbpayment = $("#cmbpayment");
    cmbpayment.jqxComboBox({ width: '87%', height: '25px' });
    var dtpfunctionclosedate = $("#dtpfunctionclosedate");
    dtpfunctionclosedate.jqxDateTimeInput({ formatString: 'dd/MM/yyyy', height: '25px', width: '61%' });
    var txtreceiveamt = $("#txtreceiveamt");
    txtreceiveamt.jqxInput({ height: 20, width: '60%' });
    var txttotunit = $("#txttotunit");
    txttotunit.jqxInput({ height: 20, width: '60%' });
    var txttothour = $("#txttothour");
    txttothour.jqxInput({ height: 20, width: '60%' });
    var txtfunctiontime = $("#txtfunctiontime");
    txtfunctiontime.jqxInput({ height: 20, width: '60%' });
    var lbltotdepositamt = $("#lbltotdepositamt");
    var lbltotgeneratoramt = $("#lbltotgeneratoramt");
    var lbltotcurrentamt = $("#lbltotcurrentamt");
    var lblbalanceamt = $("#lblbalanceamt");
    var lbltotdeposit = $("#lbltotdeposit");
    var lbltotadvance = $("#lbltotadvance");
    var lblrent = $("#lblrent");
    lbltotdepositamt.text("0.00");
    lbltotgeneratoramt.text("0.00");
    lbltotcurrentamt.text("0.00");
    lblbalanceamt.text("0.00");
    lbltotdeposit.text("0.00");
    lbltotadvance.text("0.00");
    lblrent.text("0.00");
    var MahalId;
    var EditId = 0;
    var Without = 0;
    var Withoutfunction = "";
    var functiontime = "";
    txtfunctiontime.hide();
    txtserialno.hide();
    var FunctionTime = [
        "4P.M to 4P.M",
        "6A.M to 4P.M",
        "4P.M to 12A.M"
    
    ];
    var Payment = [
        "CASH",
        "CHEQUE",
        "RTGS"

    ];
    var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var date = $("#dtpfunctiondate").jqxDateTimeInput('getDate');
    var Toformmatedate = $.jqx.dataFormat.formatdate(date, 'MM/dd/yyyy');
    ToDate = Toformmatedate;
    var day = weekday[date.getDay()];
    txtfunctionday.val(day);
   
    //alert(weekday[date.getDay()]);
    //Widgets_Id
    $("#FunctionClose").hide();
    txtfunctionname.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {

            dtpfunctiondate.jqxDateTimeInput('focus');
        }
    });
    cmbmahalname.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {
            if (cmbmahalname.jqxComboBox('getSelectedIndex') != -1) {
                var item = cmbmahalname.jqxComboBox('getSelectedItem');
                MahalId = item.value;
            }
            txtregistrant.jqxInput('focus');
        }
    });
    txtregistrant.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {

            txtaddress1.jqxInput('focus');
        }
    });

    txtaddress1.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {

            txtcity.jqxInput('focus');
        }
    });
    txtcity.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {

            txtcontactno.jqxInput('focus');
        }
    });
    txtcontactno.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {

            txtfunctionname.jqxInput('focus');
        }
    });
    txtfunctionname.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {

            dtpfunctiondate.jqxDateTimeInput('focus');
        }
    });
    dtpfunctiondate.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {
            var date = $("#dtpfunctiondate").jqxDateTimeInput('getDate');
            var Toformmatedate = $.jqx.dataFormat.formatdate(date, 'MM/dd/yyyy');
            ToDate = Toformmatedate;
            var day = weekday[date.getDay()];
            txtfunctionday.val(day);
            cmbfunctiontime.jqxDropDownList('focus');
        }
    });
    dtpfromdate1.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {
            cmbmahalname.jqxComboBox('focus');
        }
    });
    cmbfunctiontime.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {

           
        }
    });
    txtrent.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {
            var rent = txtrent.val();
          
            txtdeposit.jqxInput('focus');
        }
    });
    txtdeposit.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {

            txtadvance.jqxInput('focus');
        }
    });
    txtadvance.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {

            txtcurrentamt.jqxInput('focus');
        }
    });
    txtreceiveamt.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {
            methods.Calculation();
            txttotunit.jqxInput('focus');
        }
    });
    txttotunit.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {
            methods.Calculation();
            txttothour.jqxInput('focus');
        }
    });
    txttothour.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {
            methods.Calculation();
            dtpfunctionclosedate.jqxDateTimeInput('focus');
        }
    });
    txtcurrentamt.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {

            txtgenerator.jqxInput('focus');
        }
    });
    txtgenerator.bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {

            cmbpayment.jqxComboBox('focus');
        }
    });
    $('#chkWithout').on('change', function (event) {
        var checked = event.args.checked; var type = event.args.type; // keyboard, mouse or null depending on how the value was changed. 
        if (checked == false) {
            cmbfunctiontime.show();
            txtfunctiontime.hide();
        }
        else {
            cmbfunctiontime.hide();
            txtfunctiontime.show();
        }
    });
    cmbfunctiontime.on('change', function (event) {
        var args = event.args;
        if (args) {
            
            $('#jqxTabs').jqxTabs({
               
                selectedItem: 1,
                
            });
            txtrent.jqxInput('focus');
            setTimeout(function () {
                txtrent.jqxInput('focus');
                }, 250);
          
        }
    });
    $(document).on('keydown', function (e) {
        var flag = 1;
        // F1
        if (e.keyCode === 112) {
            e.preventDefault();
            document.getElementById("windowmain").style.zIndex = "60"
            var str = "Do yo Want to Save The Mahal Register Deatils??"
            MsgBoxYesNo(str).then(function (replay) {
                if (replay == "Yes") {
                    methods.MahalSaveMaster();
                }
                else {
                    return;
                }
            });
           
        }
        //F2 Press
        if (e.keyCode === 113) {
            e.preventDefault();
           
        }
         //F5 Press
        if (e.keyCode === 116) {

            e.preventDefault();

            $("#dtpfromdate").jqxDateTimeInput({ formatString: 'dd/MM/yyyy', height: '22px', width: '100%' });
            $("#dtptodate").jqxDateTimeInput({ formatString: 'dd/MM/yyyy', height: '22px', width: '100%' });
            $("#btnview").jqxButton({ width: 100, height: 25, value: "View", textPosition: "center" });
            $("#dtpfromdate").jqxDateTimeInput('setDate', new Date());
            $("#dtptodate").jqxDateTimeInput('setDate', new Date());
            var objlist = [{ fromdate: $("#dtpfromdate").jqxDateTimeInput('getDate'), todate: $("#dtptodate").jqxDateTimeInput('getDate'), Id: 0 }];
            methods.F5View(objlist);

        }
         //F6 Press
        if (e.keyCode === 117) {
            e.preventDefault();
            var value = prompt("Enter the BillNo Number", "");
            if (ValNum(value) != 0) {
                $('#jqxTabs').jqxTabs({
                    disabled: false,
                    selectedItem: 2

                });
                lbltotadvance.text(txtadvance.val());
                lblrent.text(txtrent.val());
                lbltotdepositamt.text(txtdeposit.val());
                var Rent = ValNum(txtrent.val());
                var Advance = ValNum(txtadvance.val());
                var deposit = ValNum(txtdeposit.val());
                var balance = ValNum(Rent - (Advance + deposit));
                lblbalanceamt.text(balance);
                txtreceiveamt.jqxInput('focus');
            }
            else {

            }
        }
          //F9 Press
        if (e.keyCode === 120) {
            e.preventDefault();
            Presskey = "F9"
            PasswordType = 1;
            EditPasswordWindow(1);
        }
           //F10 Press
        if (e.keyCode === 121) {
            e.preventDefault();
            document.getElementById("windowmain").style.zIndex = "60"
            var str = "Do you Want Clear The Form";
            MsgBoxYesNo(str).then(function (replay) {
                if (replay == "Yes") {
                    methods.clear();
                }
                else {
                    return;
                }
            });
          
        }
        //Esc Press
        if (e.keyCode === 27) {
            e.preventDefault();
            document.getElementById("windowmain").style.zIndex = "50";
            var str = 'Do You Want To Quit Page?'
            MsgBoxYesNo(str).then(function (reply) {
                if (reply == "Yes") {

                    window.location.href = "/Login/Home";

                }
            });
        }
        //F12
        if (e.keyCode === 123) {

        }
    });
    $("#txtEditpassword").jqxPasswordInput({ width: 70, height: 15 });
    $("#txtEditpassword").bind('keydown', function (event) {
        var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
        if (key == '13') {
            if ($("#txtEditpassword").val() != "" && $("#txtEditpassword").val() != null) {
                var type = "";
                if (PasswordType == 1) {
                    type = "EditPassword";
                }
                else if (PasswordType == 0) {
                    type = "FormConfig";
                }
                else if (PasswordType == 2) {
                    type = "AdminPower";
                }
                $.ajax({
                    async: false,
                    url: "/Master/EditPassword",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: '{"password":"' + $("#txtEditpassword").val() + '","type":"' + type + '"}',
                    //data: datavalue,
                    success: function (data) {
                        if (data.ok == true) {
                            $('#LockEditWindow').jqxWindow('Close');
                        }
                        else {
                            alert("Invaild Password !!!.");
                        }
                    },
                    error: function (error) {
                        MsgBox('Technical Fault Contact Software Vendor  !!!.');
                    }
                });
            }
        }
    });
    var PasswordType = 0;
    function EditPasswordWindow(type) {
        var titlenew = "";
        if (type == 1) {
            titlenew = "Edit Pwd";
        }
        else if (type == 0) {
            titlenew = "Form Pwd";
        }
        else if (type == 2) {
            titlenew = "Admin Pwd";
        }
        let centerX = (screen.width - 450) / 2;
        let centerY = (screen.height - 450) / 2;

        $('#LockEditWindow').jqxWindow({
            position: { x: centerX, y: centerY },
            showCollapseButton: false,
            height: 150,
            width: 120,
            showCloseButton: false,
            title: titlenew,
            isModal: true,
            keyboardCloseKey: 0,
            resizable: false,
            initContent: function () {
            }
        });
        $('#LockEditWindow').jqxWindow('Open');
        setTimeout(function () {
            $('#txtEditpassword').val('');
            $("#txtEditpassword").focus();
        }, 250);

    }
    var Presskey = "";
    $('#LockEditWindow').on('close', function (event) {


        setTimeout(function () {
            if (Presskey == "ESC") {
            }
            if (Presskey == "F3") {
                var value = prompt("Enter the Stock Repacking Number", "");
                if (ValNum(value) != 0) {

                    var Pid = 0;
                    var objlist = ServiceCallById("/Transaction/RepackingId", '{"No":' + value + '}');
                    if (objlist != null)
                        if (objlist.length != 0) {
                            if (objlist.data != 0) {
                                Pid = objlist.data;
                            }

                        }
                    if (Pid == 0) {
                        MsgBox("Invaild Repacking No !!!.");
                        return;
                    }


                    methods.MahalEdit(Pid);


                }
                else {

                    MsgBox("Enter Vaild Repacking Number !!!.");
                    return;
                }

            }
            if (Presskey == "F5") {


                methods.MahalEdit(F5id);
                $("#F5Viewwindow").jqxWindow('Close');
            }
            if (Presskey == "F9") {
                if (EditId != 0) {
                    document.getElementById("windowmain").style.zIndex = "60"
                    var str = "Do You Want TO Delete ProductionDetails.This is Production No " + txtbillno.val() + "?"
                    MsgBoxYesNo(str).then(function (reply) {
                        if (reply == "Yes") {
                            $('#jqxLoader').jqxLoader('open');
                            $.ajax({
                                url: "/Transaction/DeleteMahalRegister",
                                type: "POST",
                                dataType: "json",
                                contentType: "application/json; charset=utf-8",
                                data: '{"Id":' + EditId + '}',
                                success: function (data) {
                                    $('#jqxLoader').jqxLoader('close');
                                    if (data.ok) {
                                        NotificationSuccess(data.message);
                                        methods.clear();

                                    }
                                    else {
                                        // notification("topright", "error", "fa fa-exclamation-circle vd_red", "Error Notification", data.message);
                                        MsgBox(data.message);
                                    }


                                },
                                error: function (error) {
                                    $('#jqxLoader').jqxLoader('close');
                                    MsgBox('Technical Fault Contact Software Vendor  !!!.');
                                }
                            }).done(function () {
                            });
                        }
                    });
                }
                else {
                    MsgBox("No Delete Id!!!!!!.");
                    return;
                }
            }
        }, 250);
    });
    gridf5view.on('rowdoubleclick', function (event) {
        //var args = event.args;
        //loaderopen();
        //var rowindex = args.rowindex;
        //F5id = gridf5view.jqxGrid('getcellvalue', rowindex, 'Id');
        //Presskey = "F5"
        //PasswordType = 1;
        //EditPasswordWindow(1);
    });
    $(document).on('click', "#btnview", function () {
        var sid = 0;
        if (dateformat(dateformatIN($("#dtpfromdate").jqxDateTimeInput('getDate'))) > dateformat(dateformatIN($("#dtptodate").jqxDateTimeInput('getDate')))) {
            MsgBox("From Date Is Greater Than To Date");
            return;
        }
        var objlist = [{ fromdate: $("#dtpfromdate").jqxDateTimeInput('getDate'), todate: $("#dtptodate").jqxDateTimeInput('getDate'), Id: sid }];
        methods.F5View(objlist);
    });
    methods = (function () {
        return {
            init: function () {
                $("#frmpage").show();
                methods.createElements();
                methods.loadcombo();
                methods.clear();
               
            },
            createElements: function () {
                $('#windowmain').jqxWindow({
                    resizable: false,
                    minWidth: 700,
                    minHeight: 200,
                    width: 480,
                    maxWidth: 1000,
                    height: 525,
                    zIndex: 50
                });
                $('#windowmain').jqxWindow('focus');
                $('#jqxTabs').jqxTabs({ width: 980, scrollPosition: 'both', theme: 'classic', selectionTracker: true, selectedItem: 0 });
                cmbfunctiontime.jqxDropDownList({
                    source: FunctionTime,
                    selectedIndex: 0,
                    theme: 'energyblue'
                });
                cmbpayment.jqxDropDownList({
                    source: Payment,
                    selectedIndex: 0,
                    theme: 'energyblue'
                });
                //setTimeout(function () {
                //    cmbfunctiontime.jqxDropDownList('selectIndex', 0);
                //}, 250);


            },
            loadcombo: function () {
                loadMahalNamecombo("#cmbmahalname");
            },
            clear: function () {
                EditId = 0;
                Without = 0;
                cmbmahalname.jqxComboBox('clearSelection');
                txtregistrant.val("");
                txtaddress1.val("");
                txtcity.val("");
                txtcontactno.val("");
                txtfunctionname.val("");
                txtrent.val("");
                txtdeposit.val("");
                txtadvance.val("");
                txtcurrentamt.val("");
                txtgenerator.val("");
                txtreceiveamt.val("");
                txtfunctiontime.val("");
                txtfunctiontime.hide();
                txttothour.val("");
                txttotunit.val("");
                lblbalanceamt.text("0.00");
                lblrent.text("0.00");
                lbltotadvance.text("0.00");
                lbltotcurrentamt.text("0.00");
                lbltotdeposit.text("0.00");
                lbltotdepositamt.text("0.00");
                lbltotgeneratoramt.text("0.00");
                dtpfromdate1.jqxDateTimeInput('setDate', new Date());
                dtpfunctionclosedate.jqxDateTimeInput('setDate', new Date());
                dtpfunctiondate.jqxDateTimeInput('setDate', new Date());
                txtfunctionday.val(day);
                $('#jqxTabs').jqxTabs('disableAt', 2);
                $('#jqxTabs').jqxTabs({

                    selectedItem: 0,

                });
                var ObjCounter = {};
                var Gdata = {};
                $.ajax({
                    url: "/Transaction/MaxMahalRegisterNo",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(ObjCounter),
                    success: function (data) {
                        if (data.ok) {
                            $('#jqxLoader').jqxLoader('close');
                            Gdata = data;
                            txtbillno.val(Gdata.data)
                            txtserialno.val(Gdata.data)
                        }
                        else {
                            $('#jqxLoader').jqxLoader('close');
                            MsgBox(data.message);
                        }
                    },
                    error: function (error) {
                        $('#jqxLoader').jqxLoader('close');
                        MsgBox('Technical Fault Contact Software Vendor  !!!.');
                    }
                }).done(function () {
                });
                dtpfromdate1.jqxDateTimeInput('focus');
            },
            MahalSaveMaster: function () {
                if (cmbmahalname.jqxComboBox('getSelectedIndex') != -1) {
                    var item = cmbmahalname.jqxComboBox('getSelectedItem');
                    MahalId = item.value;
                }
                else {
                    MsgBox("Plese Select Mahal!!!..");
                    return;
                }
                if (txtserialno.val() == 0) {
                    return;
                }
                if ($("#chkWithout").jqxCheckBox('val') == true) {
                    Without = 1;
                    Withoutfunction = txtfunctiontime.val();
                    functiontime = "";
                    var date = $("#dtpfunctiondate").jqxDateTimeInput('getDate');
                    var Toformmatedate = $.jqx.dataFormat.formatdate(date, 'MM/dd/yyyy');
                    ToDate = Toformmatedate;
                    var item = cmbmahalname.jqxComboBox('getSelectedItem');
                   var MName = item.label;
                    getdata = [];
                    if (MahalId != 0) {
                        $.ajax({
                            async: false,
                            url: "/Transaction/SelectMahalWithFunctionTime",
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                            data: '{"Id":"' + MahalId + '","date":"' + ToDate + '","fuctiontime":"' + Withoutfunction + '"}',
                            success: function (data) {
                                getdata = data;
                            },
                            error: function (error) {
                                MsgBox('Technical Fault Contact Software Vendor  !!!.');
                            }
                        });
                        if (getdata.data1 != 0) {
                            MsgBox(MName + " Not Available For that Date and Time !!!.");
                            return;
                        }
                        if (getdata.data2 != 0) {
                            MsgBox(MName + " Not Available For that Date and Time !!!.");
                            return;
                        }
                        if (getdata.data3 != 0) {
                            MsgBox(MName + " Not Available For that Date and Time !!!.");
                            return;
                        }
                        if (getdata.data4 != 0) {
                            MsgBox(MName + " Not Available For that Date and Time !!!.");
                            return;
                        }
                    }

                }
                else {
                    functiontime = cmbfunctiontime.val();
                    var date = $("#dtpfunctiondate").jqxDateTimeInput('getDate');
                    var Toformmatedate = $.jqx.dataFormat.formatdate(date, 'MM/dd/yyyy');
                    ToDate = Toformmatedate;
                    var item = cmbmahalname.jqxComboBox('getSelectedItem');
                    var MName = item.label;
                    getdata = [];
                    if (MahalId != 0) {
                        $.ajax({
                            async: false,
                            url: "/Transaction/SelectMahalFunctionTime",
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                            data: '{"Id":"' + MahalId + '","date":"' + ToDate + '","fuctiontime":"' + functiontime + '"}',
                            success: function (data) {
                                getdata = data;
                            },
                            error: function (error) {
                                MsgBox('Technical Fault Contact Software Vendor  !!!.');
                            }
                        });
                        if (getdata.data1 != 0) {
                            MsgBox(MName + " Not Available For that Date and Time !!!.");
                            return;
                        }
                        if (getdata.data2 != 0) {
                            MsgBox(MName+ " Not Available For that Date and Time !!!.");
                            return;
                        }
                        if (getdata.data3 != 0) {
                            MsgBox(MName + " Not Available For that Date and Time !!!.");
                            return;
                        }
                        if (getdata.data4 != 0) {
                            MsgBox(MName + " Not Available For that Date and Time !!!.");
                            return;
                        }
                    }
                }
               
                var MahalRegister = [
                    {
                        Id: EditId,
                        RefNo: txtbillno.val(),
                        WithoutFunction: Without,
                        MahalMasterRefid: MahalId,
                        SlNo: txtserialno.val(),
                        WithoutFunctionRemarks: Withoutfunction,
                        PaymentMode: cmbpayment.val(),
                        RefDate: dateformat(dtpfromdate1.jqxDateTimeInput('getText')),
                        FunctionDate: dateformat(dtpfunctiondate.jqxDateTimeInput('getText')),
                        FinishedDate: dateformat(dtpfunctionclosedate.jqxDateTimeInput('getText')),
                        CustomerName: txtregistrant.val().toUpperCase(),
                        Address1: txtaddress1.val().toUpperCase(),
                        Address2: txtfunctionday.val().toUpperCase(),
                        City: txtcity.val().toUpperCase(),
                        MobileNo: txtcontactno.val(),
                        FunctionName: txtfunctionname.val().toUpperCase(),
                        FunctionTime: functiontime,
                        HalaRent: txtrent.val(),
                        DepositAmount: txtdeposit.val(),
                        CurrentUnitAmt: txtcurrentamt.val(),
                        GeneratorPerHour: txtgenerator.val(),
                        TotalCurrentUnit: txttotunit.val(),
                        TotalGeneratorHour: txttothour.val(),
                        TotalCurrentAmount: txtcurrentamt.val(),
                        TotalGeneratorAmount: lbltotgeneratoramt.text(),
                        AdvanceAmount: txtadvance.val(),
                        NetAmount: txtrent.val(),
                        ReceiveAmount: txtreceiveamt.val()
                    
                    }
                ];
                var getdata = { MahalRegister };
                var combined = {
                    MahalRegister: MahalRegister
                };
                var ObjCounter = {};
                var Gdata = {};
                $('#jqxLoader').jqxLoader('open');
                $.ajax({

                    url: "/Transaction/MahalRegisterSave",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(MahalRegister),
                    success: function (data) {
                        if (data.ok) {
                            $('#jqxLoader').jqxLoader('close');
                            NotificationSuccess(data.message);
                            methods.clear();
                        }
                        else {
                            $('#jqxLoader').jqxLoader('close');
                            MsgBox(data.message);
                        }
                    },
                    error: function (error) {
                        $('#jqxLoader').jqxLoader('close');
                        MsgBox('Technical Fault Contact Software Vendor  !!!.');
                    }
                }).done(function () {
                });
                dtpfromdate1.jqxDateTimeInput('focus');
            },
            F5View: function (objlist) {
                var masterdata = {};
                var detailsdata = {};
                $.ajax({
                    async: false,
                    url: "/Transaction/MahalRegisterF5View",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(objlist),
                    success: function (data) {
                        masterdata = data[0].MahalMaster;
                        detailsdata = data[0].MahalMasterTest;
                    },
                    error: function (error) {
                        MsgBox('Technical Fault Contact Software Vendor  !!!.');
                    }
                });

                var source =
                {
                    localdata: masterdata,
                    datatype: "local",
                    datafields:
                        [
                            { name: 'Id', type: 'number' },
                            { name: 'SerialNo', type: 'string' },
                            { name: 'FunctionDate', type: 'date' },
                            { name: 'Name', type: 'string' },
                            { name: 'MahalName', type: 'string' },
                            { name: 'Rent', type: 'number' },
                            { name: 'NetAmount', type: 'number' },

                        ],
                    id: 'Id' //MasterData Id
                };
                var masterdataAdapter = new $.jqx.dataAdapter(source);
                //Details Source
                //Details are creating two times **I it Orders **II Creates Columns

                var detailssource =
                {
                    localdata: detailsdata,
                    datatype: "local",
                    datafields:
                        [
                            { name: 'Id', type: 'number' },
                            { name: 'Name', type: 'string' },
                            { name: 'MahalName', type: 'string' },
                        ],
                    id: 'Id' //Details Id
                };
                var detailsDataAdapter = new $.jqx.dataAdapter(detailssource, { autoBind: true });
                var orders = detailsDataAdapter.records;
                var nestedGrids = new Array();
                var initrowdetails = function (index, parentElement, gridElement, record) {
                    var id = record.uid.toString();
                    var grid = $($(parentElement).children()[0]);
                    nestedGrids[index] = grid;
                    var filtergroup = new $.jqx.filter();
                    var filter_or_operator = 1;
                    var filtervalue = id;
                    var filtercondition = 'equal';
                    var filter = filtergroup.createfilter('stringfilter', filtervalue, filtercondition);
                    var ordersbyid = [];
                    for (var m = 0; m < orders.length; m++) {
                        var result = filter.evaluate(orders[m]["Id"]);
                        if (result)
                            ordersbyid.push(orders[m]); //ordering
                    }
                    var orderssource = {
                        datafields:
                            [                                                 //Details list
                                { name: 'Id', type: 'number' },
                                { name: 'Name', type: 'string' },
                                { name: 'MahalName', type: 'string' },
                            ],
                        id: 'Id',
                        localdata: ordersbyid
                    }


                    var nestedGridAdapter = new $.jqx.dataAdapter(orderssource);
                    if (grid != null) {                                 //Details Grid Columns Creating
                        grid.jqxGrid({
                            source: nestedGridAdapter, width: '100%',
                            height: '95%',
                            columnsautoresize: true,
                            columnsresize: true,
                            enablekeyboarddelete: false,
                            columns: [
                                { text: 'Customer Name', datafield: 'Name', width: 150 },
                                { text: 'Mahal Name', datafield: 'MahalName', width: 150 },
                                { text: 'Id', datafield: 'Id', width: 90, hidden: true },

                            ]
                        });
                        localizationobj.thousandsseparator = "";
                        grid.jqxGrid('localizestrings', localizationobj);
                    }
                }

                //Master Grid Creating
                gridf5view.jqxGrid(
                    {
                        width: '100%',
                        height: '85%',
                        rowsheight: 25,
                        columnsheight: 25,
                        columnsautoresize: true,
                        columnsresize: true,
                        scrollmode: 'deferred',
                        editable: false,
                        theme: 'classic',
                        showaggregates: true,
                        editmode: 'selectedcell',
                        keyboardnavigation: true,
                        selectionmode: 'singlerow',
                        rowdetails: true,
                        initrowdetails: initrowdetails,
                        enablekeyboarddelete: false,
                        rowdetailstemplate: { rowdetails: "<div id='grid' style='margin: 10px;'></div>", rowdetailsheight: 220, rowdetailshidden: true },
                        source: source,
                        columns: [
                            { text: 'Id', datafield: 'Id', hidden: true },
                            { text: 'SerialNo', datafield: 'SerialNo', width: 80 },
                            { text: 'FunctionDate', datafield: 'FunctionDate', cellsformat: 'dd/MM/yyyy', width: 150 },
                            { text: 'Customer Name', datafield: 'Name', width: 250 },
                            { text: 'Mahal Name', datafield: 'MahalName', width: 150 },
                            { text: 'Rent', datafield: 'Rent', width: 80 },
                            { text: 'NetAmount', datafield: 'NetAmount', width: 80 },
                        ]
                    });
                var localizationobj = {};
                localizationobj.thousandsseparator = "";
                gridf5view.jqxGrid('localizestrings', localizationobj);
                F5ViewWindow("#F5Viewwindow");
            },
            MahalEdit: function (Sid) {
                $.ajax({

                    url: "/Transaction/SelectMahalRegisterId",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: '{"Id":' + Sid + '}',
                    success: function (data) {
                        getdata = data;
                        $('#jqxLoader').jqxLoader('close');
                        if (getdata.length != 0) {
                            var EditData = getdata.master;
                            if (EditData != null)
                                if (EditData.length != 0) {
                                    txtbillno.val(EditData[0].RefNo);
                                    txtserialno.val(EditData[0].SlNo);
                                    dtpfromdate1.jqxDateTimeInput('setDate', jsondate(EditData[0].RefDate));
                                    dtpfunctiondate.jqxDateTimeInput('setDate', jsondate(EditData[0].FunctionDate));
                                    dtpfunctionclosedate.jqxDateTimeInput('setDate', jsondate(EditData[0].FinishedDate));
                                    txtregistrant.val(EditData[0].CustomerName);
                                    txtaddress1.val(EditData[0].Address1);
                                    txtcity.val(EditData[0].City);
                                    txtcontactno.val(EditData[0].MobileNo);
                                    txtfunctionname.val(EditData[0].FunctionName);
                                    txtfunctionday.val(EditData[0].Address2);
                                    cmbmahalname.jqxComboBox('selectItem', EditData[0].MahalMasterRefid);
                                    cmbpayment.jqxDropDownList('val', EditData[0].PaymentMode);
                                    txtrent.val(EditData[0].HalaRent);
                                    txtdeposit.val(EditData[0].DepositAmount);
                                    txtadvance.val(EditData[0].AdvanceAmount);
                                    txtcurrentamt.val(EditData[0].CurrentUnitAmt);
                                    txtgenerator.val(EditData[0].GeneratorPerHour);
                                    EditId = EditData[0].Id;
                                    if (EditData[0].WithoutFunction == 0) {
                                        cmbfunctiontime.show();
                                        txtfunctiontime.hide();
                                        cmbfunctiontime.jqxDropDownList('val',EditData[0].functiontime);
                                       
                                    }
                                    else {
                                       


                                        $("#chkWithout").jqxCheckBox({ checked: true });
                                        cmbfunctiontime.hide();
                                        txtfunctiontime.show();
                                        txtfunctiontime.val(EditData[0].WithoutFunctionRemarks)
                                    }
                                }
                          
                            dtpfromdate1.jqxDateTimeInput('focus');
                        }
                    },
                    error: function (error) {
                        $('#jqxLoader').jqxLoader('close');
                        MsgBox('Technical Fault Contact Software Vendor  !!!.');
                    }
                }).done(function () {
                });
            },
            Calculation: function () {
                var Rent = ValNum(txtrent.val());
                var Advance = ValNum(txtadvance.val());
                var deposit = ValNum(txtdeposit.val());
                var Receive = ValNum(txtreceiveamt.val());
                var balance = ValNum(Rent - (Advance + deposit + Receive));
                lblbalanceamt.text(balance);
                var currentper = ValNum(txttotunit.val());
                var TotCurrent = ValNum(txtcurrentamt.val());
                var TotUnit = ValNum(currentper * TotCurrent);
                var Genratorper = ValNum(txttothour.val());
                var totGenratorper = ValNum(txtgenerator.val());
                var TotGen = ValNum(Genratorper * totGenratorper);
                lbltotcurrentamt.text(TotUnit);
                lbltotgeneratoramt.text(TotGen);
                lbltotdepositamt.text(deposit);
            },

        };

    })();
    window.methods = methods;
    methods.init();
});