$(document).ready(function () {
    "use strict";

 

    /*----------------------------- Custom Js -----------------------*/

    $('#btnTrack').on('click', function () {
        debugger
        var IDs = [];
        var str = $('#txtTrack').val().replace(/(?:\r\n|\r|\n)/g, ',');
        var partsOfStr = str.split(',');
        for (var i = 0; i < partsOfStr.length; i++) {
            IDs.push(partsOfStr[i]);
        }
        var newArray = IDs.filter(function (v) { return v !== '' });


        if (newArray.length == 1) {
            $.ajax({
                url: "/Home/CheckExistence?TrackingId=" + newArray[0],
                method: "GET",
                beforeSend: function () {
                    $.blockUI({
                        css: {
                            border: 'none',
                            padding: '15px',
                            backgroundColor: '#000',
                            '-webkit-border-radius': '10px',
                            '-moz-border-radius': '10px',
                            opacity: .5,
                            color: '#fff'
                        }
                    });

                },
                success: function (data) {
                    if (data.IsShipmentExist) {
                        var _url = window.location.origin + "/Home/ViewSearch?Id=" + newArray[0];
                        window.open(_url, '_blank');
                    }
                    else {
                        alert("No Shipment Found");
                    }
                },
                error: function (err) {
                    alert(err);
                },
                complete: function () {
                    $.unblockUI();
                }
                
            })
           
        }
        else if (newArray.length > 1) {

            var postData = { values: newArray };
            $.ajax({
                type: "POST",
                url: "/Home/SearchResult",
                data: postData,
                dataType: "html",
                traditional: true,
                beforeSend: function () {
                    $.blockUI({
                        css: {
                            border: 'none',
                            padding: '15px',
                            backgroundColor: '#000',
                            '-webkit-border-radius': '10px',
                            '-moz-border-radius': '10px',
                            opacity: .5,
                            color: '#fff'
                        }
                    });
                    
                },
                success: function (data) {
                    $('#Content').empty();
                    $('#Content').html(data);
                    $('html, body').animate({
                        scrollTop: $("#Content").offset().top
                    }, 1000);

                },
                error: function (jqXhr) {
                    $('#Content').empty();
                    alert("Please Enter Correct Value");
                },
                complete: function () {
                    $.unblockUI();
                }
            });
        }
        else {
            alert("Please Enter Correct Credentials");
        }



    });


    $('#btnTrack1').on('click', function () {
        debugger
        var IDs = [];
        var str = $('#txtTrack').val().replace(/(?:\r\n|\r|\n)/g, ',');
        var partsOfStr = str.split(',');
        for (var i = 0; i < partsOfStr.length; i++) {
            IDs.push(partsOfStr[i]);
        }
        var newArray = IDs.filter(function (v) { return v !== '' });


        if (newArray.length == 1) {
            $.ajax({
                url: "/Home/CheckExistence?TrackingId=" + newArray[0],
                method: "GET",
                beforeSend: function () {
                    $.blockUI({
                        css: {
                            border: 'none',
                            padding: '15px',
                            backgroundColor: '#000',
                            '-webkit-border-radius': '10px',
                            '-moz-border-radius': '10px',
                            opacity: .5,
                            color: '#fff'
                        }
                    });

                },
                success: function (data) {
                    if (data.IsShipmentExist) {
                        var _url = window.location.origin + "/Home/ViewSearch?Id=" + newArray[0];
                        window.open(_url,"_parent");
                    }
                    else {
                        alert("No Shipment Found");
                    }
                },
                error: function (err) {
                    alert(err);
                },
                complete: function () {
                    $.unblockUI();
                }

            })

        }
        else if (newArray.length > 1) {

            var postData = { values: newArray };
            $.ajax({
                type: "POST",
                url: "/Home/SearchResult",
                data: postData,
                dataType: "html",
                traditional: true,
                beforeSend: function () {
                    $.blockUI({
                        css: {
                            border: 'none',
                            padding: '15px',
                            backgroundColor: '#000',
                            '-webkit-border-radius': '10px',
                            '-moz-border-radius': '10px',
                            opacity: .5,
                            color: '#fff'
                        }
                    });

                },
                success: function (data) {
                    $('#Content').empty();
                    $('#Content').html(data);
                    $('html, body').animate({
                        scrollTop: $("#Content").offset().top
                    }, 1000);

                },
                error: function (jqXhr) {
                    $('#Content').empty();
                    alert("Please Enter Correct Value");
                },
                complete: function () {
                    $.unblockUI();
                }
            });
        }
        else {
            alert("Please Enter Correct Credentials");
        }



    });








    /*-------------- Pre-loader ------------------*/
    $("#loading").delay(200).fadeOut(500);
    $("#loading-center").on('click', function () {
        $("#loading").fadeOut(500);
    });


}(jQuery));