$(document).ready(function () {
    $("body").on('keyup', '#travdate', getday);
    $("body").on('keyup', '#deptdt1,#deptdt2', getday2);
//    $("body").on('keyup, blur', '.amt1,.amt2,.amt3,.amt4,.amt5,.roe1,.roe2,.roe3,.roe4,.roe5,.amtrs1,.amtrs2,.amtrs3,.amtrs4,.amtrs5,.subtotal', calculat);
//    $("body").on('keyup blur focus change', '.margperc,.margamt,.subtotal', calculatMarg);
    $("body").on('keyup blur focus', '.taxperc,.amtrs,.taxamt,.maragamt,.subtotal,.automarg,.acntamt', calculateTot);
    $("body").on("keyup blur focus change", '.amt, .roe, .amtrs, .subtotal,.acntamt', calculateRow);
    $("body").on('click', '.pdfupload', uploadImgBox);
    $("body").on('click', '.billupload', uploadBillBox);
    $("body").on('click', '.ledgupload', uploadLedgBox);
    $("body").on('click', '.zipupload', uploadZip);
    $("body").on('change blur keyup', '.confadlt,.confchld,.conftot', calConfPax);
    $("body").on("focus blur keyup", ".supplier, .particulars", validatePass);


    $(window).keydown(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            return false;
        }
    });

    $(".leaddone").change(function () {

        var now = new Date();

        var day = ("0" + now.getDate()).slice(-2);
        var month = ("0" + (now.getMonth() + 1)).slice(-2);

        var today = now.getFullYear() + "-" + (month) + "-" + (day);
//        var newtod = (day) + "-" + (month) + "-" + now.getFullYear();

        var leaddone = $(".leaddone").val();
        if (leaddone == 'Y') {
            $(".leaddndt").val(today);
        } else {
            $(".leaddndt").val('');
        }

    });

    $(".billdone").change(function () {

        var now = new Date();

        var day = ("0" + now.getDate()).slice(-2);
        var month = ("0" + (now.getMonth() + 1)).slice(-2);

        var today = now.getFullYear() + "-" + (month) + "-" + (day);
//        var newtod = (day) + "-" + (month) + "-" + now.getFullYear();

        var billdone = $(".billdone").val();
        if (billdone == 'Y') {
            $(".billdndt").val(today);
        } else {
            $(".billdndt").val('');
        }

    });

    $(".bilaprov").change(function () {

        var now = new Date();

        var day = ("0" + now.getDate()).slice(-2);
        var month = ("0" + (now.getMonth() + 1)).slice(-2);

        var today = now.getFullYear() + "-" + (month) + "-" + (day);
//        var newtod = (day) + "-" + (month) + "-" + now.getFullYear();

        var bilaprov = $(".bilaprov").val();
        if (bilaprov == 'Y') {
            $(".aprovbldt").val(today);
        } else {
            $(".aprovbldt").val('');
        }
    });

    function calConfPax() {
        if ($(".confchld").val() != '') {
            var confchld = $(".confchld").val();
        } else {
            confchld = 0;
        }
        if ($(".confadlt").val() != '') {
            var confadlt = $(".confadlt").val();
        } else {
            confadlt = 0;
        }



        var mconftot = parseFloat(confchld) + parseFloat(confadlt);
        $(".conftot").val(mconftot);
    }



//    $(document).keydown(function (event) {
//        if (!(String.fromCharCode(event.which).toLowerCase() == 'a' && event.altKey) && !(event.which == 19))
//            return true;
//        $("#addnew").click();
//        event.preventDefault();
//        return false;
//    });

    $(document).keydown(function (event) {
        //19 for Mac Command+S
        if (!(String.fromCharCode(event.which).toLowerCase() == 's' && event.altKey) && !(event.which == 19))
            return true;
        //alert("Ctrl-s pressed");
        $("#update").click();
        event.preventDefault();
        return false;
    });

    function calculateRow() {
        var fintot = 0;

        var $row = $(this).closest("tr");
//        $row.find('.subtotal').val("0");
//        $row.find('.amtrs').val("0");

        var mamt = parseFloat($row.find('.amt').val());
        var mroe = parseFloat($row.find('.roe').val());
//        var mamtrs  = parseFloat($row.find('.amtrs').val());

        var totamt = mamt * mroe;
        $row.find('.amtrs').val(totamt);

        calculateTot();
    }

    function calculateTot() {
        var amt = 0;
        var amt1 = 0;
        var val = 0;
        var val1 = 0;
        var mactmarg = 0;
        var mtaxamt = 0;
        var msubtotal = parseFloat($("#subtotal").val());
        var mmargamt = parseFloat($("#margamt").val());
        var mtaxperc = parseFloat($("#taxperc").val());
        /*********Total Part************/

        $(".amtrs").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                val += parseFloat(this.value);
            }
        });
        amt = parseFloat(val);

        $("#leadtot").val(amt.toFixed(2));

        $(".acntamt").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                val1 += parseFloat(this.value);
            }
        });
        amt1 = parseFloat(val1);

        $("#subtotal").val(amt1.toFixed(2));
//        $("#netamt").val(amt.toFixed(2));
        var actsub = $("#subtotal").val();

        var automarg = $(".automarg").val();
        if (automarg == 'A') {
            mactmarg = (mmargamt * 100) / (100 + mtaxperc);

            $('#actmarg').val(mactmarg.toFixed(2));

            mtaxamt = mmargamt - mactmarg;

            if (!isNaN(mtaxamt)) {
                $('#taxamt').val(mtaxamt.toFixed(2));
            } else {
                $('#taxamt').val(0);
            }


            var mnetamt = msubtotal + mmargamt;
            $('#netamt').val(mnetamt.toFixed(2));
        } else {
            mtaxamt = actsub * mtaxperc / 100;
            if (!isNaN(mtaxamt)) {
                $('#taxamt').val(mtaxamt.toFixed(2));
            } else {
                $('#taxamt').val(0);
            }

//            $('#taxamt').val(mtaxamt.toFixed(2));

            var mnetamt = msubtotal + parseFloat($("#taxamt").val());
            $('#netamt').val(mnetamt.toFixed(2));
        }
//        calculatMarg();
    }

    $(".billupload").attr("disabled", "true");

    $(".ledgupload").attr("disabled", "true");

    function validatePass() {
        var tr = $(this).closest("tr"),
                supplier = tr.find(".supplier").val(),
                particulars = tr.find(".particulars").val();


        if (supplier != "" && particulars != "") {
            tr.find(".billupload").removeAttr("disabled");
            tr.find(".ledgupload").removeAttr("disabled");
        }

    }
    loadValidate();
    function loadValidate() {
        $(".supplier").each(function () {

            var supplier = $(".supplier").val(),
                    particulars = $(".particulars").val();


            if (supplier != "" && particulars != "") {
                $(".billupload").removeAttr("disabled");
                $(".ledgupload").removeAttr("disabled");
            }
        });
    }




    ////////////////////////////ZIP UPLOAD - START////////////////////////////

    $(".zipupload").click(function () {
        var name = $(".name").val(),
                lastname = $(".lastname").val();

        $("#mname").val(name);
        $("#mlastname").val(lastname);
    });

    function uploadZip() {
        $('#zipModal').modal('show');
        function _(el) {
            return document.getElementById(el);
        }
        $("#uploadbtn2").click(function () {
            zipUpload();
        });
        function zipUpload() {
            var file = _("file2").files[0];
            var evouchno = _("evouchno2").value;
            var mname = _("mname").value;
            var mlastname = _("mlastname").value;
            var formdata = new FormData();
            formdata.append("file2", file);
            formdata.append("evouchno", evouchno);
            formdata.append("mname", mname);
            formdata.append("mlastname", mlastname);
            var ajax = new XMLHttpRequest();
            ajax.upload.addEventListener("progress", progressHandler, false);
            ajax.addEventListener("load", completeHandler, false);
            ajax.addEventListener("error", errorHandler, false);
            ajax.addEventListener("abort", abortHandler, false);
            ajax.open("POST", "upload_zip.php");
            ajax.send(formdata);
        }

        function progressHandler(event) {
            _("loaded_n_total2").innerHTML = "Uploaded " + event.loaded + " bytes of " + event.total;
            var percent = (event.loaded / event.total) * 100;
            _("progressBar2").value = Math.round(percent);
            _("status2").innerHTML = Math.round(percent) + "% uploaded... please wait";
        }

        function completeHandler(event) {
            _("status2").innerHTML = event.target.responseText;
            _("progressBar2").value = 0;
            //        setTimeout(function(){ GetImage(); },3000);
        }

        function errorHandler(event) {
            _("status2").innerHTML = "Upload Failed";
        }

        function abortHandler(event) {
            _("status2").innerHTML = "Upload Aborted";
        }

        $("#zipModal").find("#file1").change(function () {
            readIMG(this);
        });
        $('#zipModal').off("shown.bs.modal").on("shown.bs.modal", function () {
            $("#status2").css({'background-image': 'none'});
        });
    }

    ////////////////////////////ZIP UPLOAD - END  ////////////////////////////








    function uploadImgBox() {
//            currentEle = $(this).closest("tr");
//            debugger
        //            $(".challanupload1").click(function () {
        var $row = $(this).closest("tr");
        var vouchno = $("#vouchno").val();
        var passno = $row.find('#passport').val();
        ShowUpload('upload');
        $('#evouchno').val(vouchno);
        $('#epassno').val(passno);
        setTimeout(function () {
            GetImage();
        }, 2000);
        //            });
        $(".challanview").click(function () {
            ShowUpload('view');
        });
        function ShowUpload(para) {
            $('#formModal').modal('show');
            if (para == 'upload') {
                $("#formModal").find(".upload").show();
                $("#formModal").find(".mview").hide();
            } else {
                GetImage();
                ShowView();
            }
        }
        function ShowView() {
            $("#formModal").find(".mview").show();
            $("#formModal").find(".upload").hide();
        }
        function GetImage() {
//            var $row = $(this).closest("tr");
            //            var vouchno = $row.find("#vouchno").val();
            //            var passno = $row.find("#passno").val();
            var vouchno = $("#vouchno").val();
//            var passno = $('#epassno').val();
            //            $('#epassno').focus();
            $.post('lead_imgpath.php', {evouchno: vouchno}, function (data) {
                $("#mpreview").attr("src", data);
                $("#preview").attr("src", data);
                //                $(".print_image").attr("href", "print_chal_img.php?path=" + data);
                readIMG(this);
            });
        }

        function _(el) {
            return document.getElementById(el);
        }
        $("#uploadbtn").click(function () {
            ChallanImgUpload();
        });
        function ChallanImgUpload() {
            var file = _("file1").files[0];
            //            var $row = $(this).closest("tr");
//            var epassno = _("epassno").value;
            var evouchno = _("vouchno").value;
            //        alert(file.name+" | "+file.size+" | "+file.type);
            var formdata = new FormData();
            formdata.append("file1", file);
            formdata.append("evouchno", evouchno);
//            formdata.append("epassno", epassno);
            var ajax = new XMLHttpRequest();
            ajax.upload.addEventListener("progress", progressHandler, false);
            ajax.addEventListener("load", completeHandler, false);
            ajax.addEventListener("error", errorHandler, false);
            ajax.addEventListener("abort", abortHandler, false);
            ajax.open("POST", "lead_upd_pdf.php");
            ajax.send(formdata);
        }

        function progressHandler(event) {
            _("loaded_n_total").innerHTML = "Uploaded " + event.loaded + " bytes of " + event.total;
            var percent = (event.loaded / event.total) * 100;
            _("progressBar").value = Math.round(percent);
            _("status").innerHTML = Math.round(percent) + "% uploaded... please wait";
        }

        function completeHandler(event) {
            _("status").innerHTML = event.target.responseText;
            _("progressBar").value = 0;
            //        setTimeout(function(){ GetImage(); },3000);
        }

        function errorHandler(event) {
            _("status").innerHTML = "Upload Failed";
        }

        function abortHandler(event) {
            _("status").innerHTML = "Upload Aborted";
        }

        function readIMG(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();
                reader.onload = function (e) {
                    $('#preview').attr('src', e.target.result);
                }

                reader.readAsDataURL(input.files[0]);
            }
        }


        $("#formModal").find("#file1").change(function () {
            readIMG(this);
        });
        $('#formModal').off("shown.bs.modal").on("shown.bs.modal", function () {
            $("#status").css({'background-image': 'none'});
        });
    }

//    BIll Upload Start
    function uploadBillBox() {
//            currentEle = $(this).closest("tr");
//            debugger
        //            $(".challanupload1").click(function () {
        var $row = $(this).closest("tr");
        var vouchno = $("#vouchno").val();
        var leadvouch = $("#leadvouch").val();
        var srno = $row.find('.srno').val();
        var supplier = $row.find('.supplier').val();
        ShowUpload('upload');
        $('#evouchno').val(vouchno);
        $('#eleadvouch').val(leadvouch);
        $('#esrno').val(srno);
        $('#esupplier').val(supplier);
        setTimeout(function () {
            GetImage();
        }, 2000);
        //            });
        $(".challanview").click(function () {
            ShowUpload('view');
        });
        function ShowUpload(para) {
            $('#billModal').modal('show');
            if (para == 'upload') {
                $("#billModal").find(".upload").show();
                $("#billModal").find(".mview").hide();
            } else {
                GetImage();
                ShowView();
            }
        }
        function ShowView() {
            $("#billModal").find(".mview").show();
            $("#billModal").find(".upload").hide();
        }
        function GetImage() {
            var $row = $(this).closest("tr");
            var vouchno = $("#vouchno").val();
            var leadvouch = $("#leadvouch").val();
            var srno = $("#esrno").val();
            var supplier = $("#esupplier").val();

//            var srno = $row.find('.srno').val();
//            var supplier = $row.find('.supplier').val();


            //            $('#epassno').focus();

            $.post('bill_pdfpath.php', {evouchno: vouchno, eleadvouch: leadvouch, esrno: srno, esupplier: supplier}, function (data) {
                if (data != "") {
                    $("#mpreview").val(data);
                    $("#preview").val(data);
                } else {
                    $("#mpreview").val("File Not Uploaded");
                    $("#preview").val("File Not Uploaded");
                    $("#status").val("");
                }
//                $("#mpreview").attr("src", data);
//                $("#preview").attr("src", data);
                //                $(".print_image").attr("href", "print_chal_img.php?path=" + data);
                readIMG(this);
            });
        }

        function _(el) {
            return document.getElementById(el);
        }
        var evouchno = _("vouchno").value;
        var eleadvouch = _("leadvouch").value;
        var esrno = _("esrno").value;
        var esupplier = _("esupplier").value;
        var link = "billpdf.php?vouchno=" + evouchno + "&leadvouch=" + eleadvouch + "&srno=" + esrno + "&supplier=" + esupplier;
        $(".billdown").attr("href", link);
//        alert($(".billdown").attr("href"));
        $("#mpreview").val("");
        $("#preview").val("");
        $("#uploadbtn3").click(function () {


            ChallanImgUpload2();
        });
        function ChallanImgUpload2() {
            var file = _("file3").files[0];
            //            var $row = $(this).closest("tr");
            var evouchno = _("vouchno").value;
            var eleadvouch = _("leadvouch").value;
            var esrno = _("esrno").value;
            var esupplier = _("esupplier").value;
            //        alert(file.name+" | "+file.size+" | "+file.type);
            var formdata = new FormData();
            formdata.append("file3", file);
            formdata.append("evouchno", evouchno);
            formdata.append("eleadvouch", eleadvouch);
            formdata.append("esrno", esrno);
            formdata.append("esupplier", esupplier);
            var ajax = new XMLHttpRequest();
            ajax.upload.addEventListener("progress", progressHandler, false);
            ajax.addEventListener("load", completeHandler, false);
            ajax.addEventListener("error", errorHandler, false);
            ajax.addEventListener("abort", abortHandler, false);
            ajax.open("POST", "bill_upd_img.php");
            ajax.send(formdata);

//            alert($(".billdown").attr("href"));
        }

        function progressHandler(event) {
            _("loaded_n_total").innerHTML = "Uploaded " + event.loaded + " bytes of " + event.total;
            var percent = (event.loaded / event.total) * 100;
            _("progressBar").value = Math.round(percent);
            _("status").innerHTML = Math.round(percent) + "% uploaded... please wait";
        }

        function completeHandler(event) {
            _("status").innerHTML = event.target.responseText;
            _("progressBar").value = 0;
            //        setTimeout(function(){ GetImage(); },3000);
        }

        function errorHandler(event) {
            _("status").innerHTML = "Upload Failed";
        }

        function abortHandler(event) {
            _("status").innerHTML = "Upload Aborted";
        }

        function readIMG(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();
                reader.onload = function (e) {
                    $('#preview').attr('src', e.target.result);
                }

                reader.readAsDataURL(input.files[0]);
            }
        }


        $("#billModal").find("#file3").change(function () {
            readIMG(this);
        });
        $('#billModal').off("shown.bs.modal").on("shown.bs.modal", function () {
            $("#status").css({'background-image': 'none'});
        });
    }
//    BIll Upload End

//    Ledger Upload Start
    function uploadLedgBox() {
//            currentEle = $(this).closest("tr");
//            debugger
        //            $(".challanupload1").click(function () {
        var $row = $(this).closest("tr");
        var vouchno = $("#vouchno").val();
        var leadvouch = $("#leadvouch").val();
        var srno = $row.find('.srno').val();
        var supplier = $row.find('.supplier').val();
        ShowUpload('upload');
        $('#fvouchno').val(vouchno);
        $('#fleadvouch').val(leadvouch);
        $('#fsrno').val(srno);
        $('#fsupplier').val(supplier);
        setTimeout(function () {
            GetImage();
        }, 2000);
        //            });
        $(".challanview").click(function () {
            ShowUpload('view');
        });
        function ShowUpload(para) {
            $('#ledgModal').modal('show');
            if (para == 'upload') {
                $("#ledgModal").find(".upload").show();
                $("#ledgModal").find(".mview").hide();
            } else {
                GetImage();
                ShowView();
            }
        }
        function ShowView() {
            $("#billModal").find(".mview").show();
            $("#billModal").find(".upload").hide();
        }
        function GetImage() {
            var $row = $(this).closest("tr");
            var vouchno = $("#vouchno").val();
            var leadvouch = $("#leadvouch").val();
            var srno = $("#fsrno").val();
            var supplier = $("#fsupplier").val();

//            var srno = $row.find('.srno').val();
//            var supplier = $row.find('.supplier').val();


            //            $('#epassno').focus();

            $.post('ledg_pdfpath.php', {evouchno: vouchno, eleadvouch: leadvouch, esrno: srno, esupplier: supplier}, function (data) {
                if (data != "") {
                    $("#mpreview1").val(data);
                    $("#preview1").val(data);
//                    $("#status1").hide();
                } else {
                    $("#mpreview1").val("File Not Uploaded");
                    $("#preview1").val("File Not Uploaded");
//                    $("#status1").hide("");
                }
//                $("#mpreview").attr("src", data);
//                $("#preview").attr("src", data);
                //                $(".print_image").attr("href", "print_chal_img.php?path=" + data);
                readIMG(this);
            });
        }

        function _(el) {
            return document.getElementById(el);
        }
        var evouchno = _("vouchno").value;
        var eleadvouch = _("leadvouch").value;
        var esrno = _("fsrno").value;
        var esupplier = _("fsupplier").value;
        var link = "ledgpdf.php?vouchno=" + evouchno + "&leadvouch=" + eleadvouch + "&srno=" + esrno + "&supplier=" + esupplier;
        $(".ledgdown").attr("href", link);
//        alert($(".billdown").attr("href"));
        $("#uploadbtn4").click(function () {
            $("#mpreview").val("");
            $("#preview").val("");
            $("#status1").val("");
            ChallanImgUpload4();
        });
        function ChallanImgUpload4() {
            var file = _("file4").files[0];
            //            var $row = $(this).closest("tr");
            var evouchno = _("vouchno").value;
            var eleadvouch = _("leadvouch").value;
            var esrno = _("fsrno").value;
            var esupplier = _("fsupplier").value;
            //        alert(file.name+" | "+file.size+" | "+file.type);
            var formdata = new FormData();
            formdata.append("file4", file);
            formdata.append("fvouchno", evouchno);
            formdata.append("fleadvouch", eleadvouch);
            formdata.append("fsrno", esrno);
            formdata.append("fsupplier", esupplier);
            var ajax = new XMLHttpRequest();
            ajax.upload.addEventListener("progress", progressHandler, false);
            ajax.addEventListener("load", completeHandler, false);
            ajax.addEventListener("error", errorHandler, false);
            ajax.addEventListener("abort", abortHandler, false);
            ajax.open("POST", "ledger_upd_img.php");
            ajax.send(formdata);

//            alert($(".billdown").attr("href"));
        }

        function progressHandler(event) {

            _("loaded_n_total").innerHTML = "Uploaded " + event.loaded + " bytes of " + event.total;
            var percent = (event.loaded / event.total) * 100;
            _("progressBar1").value = Math.round(percent);
            _("status1").innerHTML = Math.round(percent) + "% uploaded... please wait";
        }

        function completeHandler(event) {
            _("status1").innerHTML = event.target.responseText;
            _("progressBar1").value = 0;
            //        setTimeout(function(){ GetImage(); },3000);
        }

        function errorHandler(event) {
            _("status1").innerHTML = "Upload Failed";
        }

        function abortHandler(event) {
            _("status1").innerHTML = "Upload Aborted";
        }

        function readIMG(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();
                reader.onload = function (e) {
                    $('#preview1').attr('src', e.target.result);
                }

                reader.readAsDataURL(input.files[0]);
            }
        }


        $("#ledgModal").find("#file4").change(function () {
            readIMG(this);
        });
        $('#ledgModal').off("shown.bs.modal").on("shown.bs.modal", function () {
            $("#status1").css({'background-image': 'none'});
        });
    }
//    Ledger Upload End



    function addrows() {
        var ctr = $('#items').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }

        $.post('confrecd_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('#data');
            ctr++;
            $('#items').val(ctr);
        });
    }

    $('#addnew').click(function () {
        addrows();
    });


    function getday() {
        var date = $("#deptdate").val();
        $.post('getday.php', {date: date}, function (data) {
            $("#nonights").val(data)
        });
        $("#retndate").val(date);
    }
    ;
    (function ($, window, document, undefined) {
        $("#nonights").on("change keyup", function () {
            var date = new Date($("#deptdate").val()),
                    days = parseInt($("#nonights").val(), 10);

            if (!isNaN(date.getTime())) {
                date.setDate(date.getDate() + days);

                $("#retndate").val(date.toInputFormat());
            } else {
                alert("Invalid Date");
            }
        });



        Date.prototype.toInputFormat = function () {
            var yyyy = this.getFullYear().toString();
            var mm = (this.getMonth() + 1).toString(); // getMonth() is zero-based
            var dd = this.getDate().toString();
            return yyyy + "-" + (mm[1] ? mm : "0" + mm[0]) + "-" + (dd[1] ? dd : "0" + dd[0]); // padding
        };
    })(jQuery, this, document);


    function getday2() {
        var date1 = $("#deptdt2").val();
//        $.post('getday1.php', {date: date1}, function (data) {
//            $("#nonights1").val(data)
//        });
        $("#deptdt2").val(date1);
    }
    ;
    (function ($, window, document, undefined) {
        $("#nonights1").on("change keyup", function () {
            var date = new Date($("#deptdt1").val()),
                    days = parseInt($("#nonights1").val(), 10);

            if (!isNaN(date.getTime())) {
                date.setDate(date.getDate() + days);

                $("#deptdt2").val(date.toInputFormat());
            } else {
                alert("Invalid Date");
            }
        });
        $("#nonights2").on("change keyup", function () {
            var date1 = new Date($("#deptdt2").val()),
                    days = parseInt($("#nonights2").val(), 10);

            if (!isNaN(date1.getTime())) {
                date1.setDate(date1.getDate() + days);

                $("#deptdt3").val(date1.toInputFormat());
            } else {
                alert("Invalid Date");
            }
        });
        $("#nonights3").on("change keyup", function () {
            var date2 = new Date($("#deptdt3").val()),
                    days = parseInt($("#nonights3").val(), 10);

            if (!isNaN(date2.getTime())) {
                date2.setDate(date2.getDate() + days);

                $("#deptdt4").val(date2.toInputFormat());
            } else {
                alert("Invalid Date");
            }
        });
        $("#nonights4").on("change keyup", function () {
            var date3 = new Date($("#deptdt4").val()),
                    days = parseInt($("#nonights4").val(), 10);

            if (!isNaN(date3.getTime())) {
                date3.setDate(date3.getDate() + days);

                $("#deptdt5").val(date3.toInputFormat());
            } else {
                alert("Invalid Date");
            }
        });
        Date.prototype.toInputFormat = function () {
            var yyyy = this.getFullYear().toString();
            var mm = (this.getMonth() + 1).toString(); // getMonth() is zero-based
            var dd = this.getDate().toString();
            return yyyy + "-" + (mm[1] ? mm : "0" + mm[0]) + "-" + (dd[1] ? dd : "0" + dd[0]); // padding
        };
    })(jQuery, this, document);



});