﻿////////////////////////////////////////////////////////////////////////////////
//  File Name		:  Tracking_HomePage.js 
//	File Revision	:  1.2
//  File Date		:  November 7, 2011
//  Copyright © 2011 Prestige Delivery Systems - All rights reserved.
//
//  General description:
//		The javascript file handles the AJAX package tracking requests
// 
//  File History
//      Revision 1.0 - (Forked from Tracking.js) - 2011-11-03
//      Revision 1.1 - Modifications to tracking formating - 2011-11-04
//      Revision 1.2 - Support for displaying multiple barcodes - 2011-11-07
////////////////////////////////////////////////////////////////////////////////


$(document).ready(function () {
    $("a#a1").fancybox({
        'showCloseButton': true,
        'hideOnOverlayClick': false,
        'enableEscapeButton': false
    });
	 
	 $("#txtBarcode").keyup(function(event){   if(event.keyCode == 13){     $("#btnTrack").click();   } });
});

    


function TrackPackage_HomePage() {




    var text = $('#txtBarcode').val();


    //Don't send send text to server if no tracking numbers have been entered
    if (text == "" || text == "  " || text == "   " || text == "   " || text == "   " || text == "\n") {
        $('#TrackingResults').text('');
        $('#TrackingResults').children().remove();
        $('#TrackingResults').html('');
        $('#TrackingErrors').text('');
        return;
    }



    $('#TrackingAjaxImage').show();
    $('#txtBarcode').hide();
    $('#btnTrack').hide();

    //The line feeds get lost when sent to the TrackingHandler.ashx page so replace with a comma
    text = text.replace(/\n/g, ",");

    //Clear old tracking results if there are any
    $('#TrackingResults').text('');
    $('#TrackingResults').children().remove();
    $('#TrackingResults').html('');
    $('#TrackingErrors').text('');



    $.ajax({
        'async': true,
        'global': false,
        'cache': false,
        'url': 'TrackingHandler.ashx?trackingNumbers=' + text,
        'error': function () {
            alert('Tracking Error')
            $('#TrackingAjaxImage').hide();
            $('#txtBarcode').show();
            $('#btnTrack').show();
        },
        'success': function (data) {


            //IE6 returns with small amount of data - use IE7, IE8, Chrome, Safari, Opera, or Firefox
            if (data.length < 100) {
                alert('Tracking Error');
                $('#TrackingAjaxImage').hide();
                $('#txtBarcode').show();
                $('#btnTrack').show();
                return;
            }

            var trackingResults = JSON.parse(data);
            var html;

            if (trackingResults != null) {

                var a = 0;
                for (a = 0; a < trackingResults.length; a++) {
                    //Get position of last item in history
                    var packageHistory = trackingResults[a].TrackingEventHistory[0];
                    if (packageHistory.ELCity == null && packageHistory.PDHistory == null) {
                        //Bad tracking number, don't display until the end
                    }
                    else {

                        var historyLength = trackingResults[a].TrackingEventHistory.length;
                        var LatestEvent = ""; //Leave empty if no history
                        if (historyLength > 0) {
                            LatestEvent = trackingResults[a].TrackingEventHistory[0].EventCodeDesc;
                        }

                        //Add header for Tracking
                        var headerId = "trackingHeader" + a.toString();
                        if (a == 0) {
                            $('#TrackingResults').append('<div style="margin-top:5px; padding-top:3px; " ><span class="navMenu" style="display: inline-block; width: 184px">Tracking Number: </span><span style="font-weight:normal;" class="navMenu">' + trackingResults[a].TrackingNumber + '</span><br /><span class="navMenu" style="display: inline-block; width: 184px">Status: </span><span style="font-weight:normal;" class="navMenu">' + LatestEvent + '</span><br /><span class="navMenu" style="display: inline-block; width: 184px">Estimated Delivery Date: </span><span style="font-weight:normal;" class="navMenu">' + trackingResults[a].TrackingEventHistory[0].EstimatedDeliveryDate + '</span><br /><span class="navMenu" style="display: inline-block; width: 184px">Deliver To: </span><span style="font-weight:normal;" class="navMenu">' + trackingResults[a].TrackingEventHistory[0].PDCity + ", " + trackingResults[a].TrackingEventHistory[0].PDState + '</span></div>');
                        }
                        else {
                            $('#TrackingResults').append('<div style="margin-top:10px; border-top:1px dotted #666; padding-top:3px; " ><span class="navMenu" style="display: inline-block; width: 184px">Tracking Number: </span><span style="font-weight:normal;" class="navMenu">' + trackingResults[a].TrackingNumber + '</span><br /><span class="navMenu" style="display: inline-block; width: 184px">Status: </span><span style="font-weight:normal;" class="navMenu">' + LatestEvent + '</span><br /><span class="navMenu" style="display: inline-block; width: 184px">Estimated Delivery Date: </span><span style="font-weight:normal;" class="navMenu">' + trackingResults[a].TrackingEventHistory[0].EstimatedDeliveryDate + '</span><br /><span class="navMenu" style="display: inline-block; width: 184px">Deliver To: </span><span style="font-weight:normal;" class="navMenu">' + trackingResults[a].TrackingEventHistory[0].PDCity + ", " + trackingResults[a].TrackingEventHistory[0].PDState + '</span></div>');
 
                        }
                        if (historyLength > 0) {

                            var tableIdStr = 'historyTable' + a.toString();
                            var table = '<div style="padding-top:20px"><table id=\'' + tableIdStr + '\'></table></div>';
                            $('#TrackingResults').append(table);

                            var b;

                            html = '<tbody>';


                            //Add table headers to history table
                            html += "<tr>";
                            html += "<td style=\"width:100px\"><span><strong>" + "Date" + "</strong></span></td>";
                            html += "<td style=\"width:100px\"><span><strong>" + "Time" + "</strong></span></td>";
                            html += "<td style=\"width:200px\"><span><strong>" + "Location" + "</strong></span></td>";
                            html += "<td style=\"width:275px\"><span><strong>" + "Event Details" + "</strong></span></td>";
                            html += "</tr>";



                            //Add table values to history table
                            for (b = 0; b < historyLength; b++) {
                                var h = trackingResults[a].TrackingEventHistory[b];

                                // Alternate row background colors										  
                                html += "<tr>";
                                if (b % 2) {
                                    html += "<td><span>" + trackingResults[a].TrackingEventHistory[b].serverDate + "</span></td>";
                                    html += "<td><span>" + trackingResults[a].TrackingEventHistory[b].serverTime + "</span></td>";
                                    html += "<td><span>" + GetLocation(h) + "</span></td>";
                                    html += "<td><span>" + h.EventCodeDesc + "</span></td>";
                                }
                                else {
                                    html += "<td style=\"background-color:#EEE\"><span>" + trackingResults[a].TrackingEventHistory[b].serverDate + "</span></td>";
                                    html += "<td style=\"background-color:#EEE\"><span>" + trackingResults[a].TrackingEventHistory[b].serverTime + "</span></td>";
                                    html += "<td style=\"background-color:#EEE\"><span>" + GetLocation(h) + "</span></td>";
                                    html += "<td style=\"background-color:#EEE\"><span>" + h.EventCodeDesc + "</span></td>";
                                }
                                html += "</tr>";

                            }
                            html += '</tbody>';
                            $('#' + tableIdStr).append(html);

                        }
                        else {
                            //No History
                            var NoHistory = '<div class="verdanasmall">' + 'No Tracking History' + '</div>';
                            $('#TrackingResults').append(NoHistory);
                        }
                    }

                    $('#a1').trigger('click');


                }


                //Show bad barcodes at the bottom
                for (a = 0; a < trackingResults.length; a++) {
                    //Get position of last item in history
                    var packageHistory = trackingResults[a].TrackingEventHistory[0];
                    if (packageHistory.ELCity == null && packageHistory.PDHistory == null) {

                        var headerId = "trackingError" + a.toString();
                        $('#TrackingResults').append('<h3 style="margin-top:10px; border-top:1px dotted #666; padding-top:3px;"><a class="navMenu" style="color:Red" href="#">' + packageHistory.TrackingNumber + ' - Could not track this barcode.</a></h3>');

                        $('#TrackingResults').append('<div class="verdanasmall" style="color:Black">Invalid Tracking Number</div>');

                    }


                }







            }
            else {
                alert('No tracking information')
            }






            $('#TrackingAjaxImage').hide();
            $('#txtBarcode').show();
            $('#btnTrack').show();
        }
    });

}



function GetLocation(history) {
    if (history.EventCode == "EVENT_101") {
        return history.CountryCode;
    }
    else if (history.EventCode == "EVENT_301") { //Delivered
        return history.PDCity + "-" + history.PDState + " " + history.CountryCode;
    }
    else {
        return history.ELCity + "-" + history.ELState + " " + history.CountryCode;
    }
}


