$(document).ready(function () {
//    $(".tbody").on("blur", '.rate', autoAddRows);
    $("body").on('keyup change', '.adult, .child, .infant, .semiinf, .totpax', Calculat);
//    $("body").on('keyup change', '.mobile', getdetail);
    $("body").on('keyup change', '.mobile', gettravhist);
    $("body").on('change', '.mobile', checkNum);
    $("body").on('change', '.travdate,.vouchdt', checkDate);
//    $("body").on('click', '.excelbutn', Runfile);
    $("body").on('click', '.excelupload', uploadImgBox);
//    $("body").on('click', '.excelupload', uploadImgBox);

//    function Runfile() {
//        WshShell = new ActiveXObject("WScript.Shell");
//        WshShell.Run("c:/windows/system32/notepad.exe", 1, false);
//    }
//    $("body").on('click', '.exceltest', test);
    function checkDate() {
        var travdate = $(".travdate").val();
        var toddate = $(".vouchdt").val();
//        var toddate = date("Y-m-d");
//        var toddate = new Date();
        var now = new Date(travdate);

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

        var today = now.getFullYear();
//        var newtod = (day) + "-" + (month) + "-" + now.getFullYear();
        if (today >= 2016) {
            if (travdate < toddate) {
                alert("Travel Date Cannot be Less than Today's Date");
                $(".travdate").focus();
            }
        }
    }

    function gettravhist() {
        var mobile = $(".mobile").val();

        $.post('get_travhist_dtl.php', {mobile: mobile}, function (data) {
//                alert(data);
            $(".mytable").html(data);
        });
    }

    function checkNum() {
        var mmobile = $(".mobile").val();
        if (mmobile.length < 10) {
            alert("Please Enter Valid Mobile No.");
            $(".mobile").focus();
        }
    }

    function Calculat() {
        var madult = parseFloat($(".adult").val());
        var mchild = parseFloat($(".child").val());
        var minfant = parseFloat($(".infant").val());
        var msemiinf = parseFloat($(".semiinf").val());

        var mtotpax = parseFloat($(".totpax").val());

        mtotpax = madult + mchild + minfant + msemiinf;

//        $(".totpax").val(mtotpax);

        if (isNaN(mtotpax)) {
            $(".totpax").val("0");
            ;
        } else {
            $(".totpax").val(mtotpax);
            /*$row.find('.final').val(fintot.toFixed(2));-*/
        }

    }

//    function getdetail() {
////            var $row = $(this).closest("tr");
//        var mname = $(".name").val();
//        var mobile = $(".mobile").val();
//        var data = {mobile: mobile};
//        $.ajax({
//            type: 'GET',
//            dataType: 'json',
//            url: "get_cust_dtl.php",
//            data: data,
//            success: function (response) {
//                if (mname == '') {
//                    //            var option_value = document.getElementById("status").value;
//                    $('.mobile2').val(response.mobile2);
//                    $('.name').val(response.name);
//                    $('.lastname').val(response.lastname);
//                    $('.tel').val(response.tel);
//                    $('.tel2').val(response.tel2);
//                    $('.email').val(response.email);
//                    $('.city').val(response.city);
////                        $("#emailModal").modal("show");
//                }
//
//            },
//            error: function (request, textStatus, errorThrown) {
//                alert(errorThrown);
//            }
//            //dataType:'json'
//        });
////                    $.get('get_followup_date.php', {vouchno: vouchno}, function (data) {
////
////
////
////                    });
//    }

    function uploadImgBox() {
//            $(".challanupload1").click(function () {
        var vouchno = $("#vouchno").val();

        ShowUpload('upload');
        $('#vouchno').val(vouchno);
        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 vouchno = $("#vouchno").val();
            $.post('lead_excpath.php', {vouchno: 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 evouchno = _("vouchno").value;
//        alert(file.name+" | "+file.size+" | "+file.type);
            var formdata = new FormData();
            formdata.append("file1", file);
            formdata.append("evouchno", evouchno);
            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_excel_file.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'});
        });
    }

    $(document).keydown(function (event) {
        if (!(String.fromCharCode(event.which).toLowerCase() == 'c' && event.altKey) && !(event.which == 19))
            return true;
        $("#cancel").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");
        $("#submit").click();
        event.preventDefault();
        return false;
    });
});