﻿/// References for Visual Studio Intellisense
/// <reference path="../js/jquery-1.3.1.js" />

Cufon.replace("h1");

/* FCL EXPORT READY FUNCTIONALITY */
function submitFCLExportReady(
sender,
Id
) {
    $('.' + Id + '_FCLExportReady').css("display", "none");

    var input_index = 0;
    $(sender).parent().each(function() {
        $(':input[type=text]', this).each(function() {
            if (input_index == 0) {
                $.post(
                    "TransportCommand.ashx",
                    { "Id": Id, "CommandName": "FREADY" },
                    function(data, textStatus) {
                        $('.' + Id + '_showFCLExportReady').text(data);
                    },
                    "text"
                );
            }
            input_index++;
        });
    });
}
/* END */


/* REQUEST DELIVERY DATE FUNCTIONALITY */
function showRequestDeliveryDate(
    sender,
    Id
) {
    var pos = $(sender).offset();
    var postop = pos.top;

    $('.' + Id + '_requestDeliveryDate').css("top", postop);
    $('.' + Id + '_requestDeliveryDate').css("display", "block");
}

function hideRequestDeliveryDate(
sender,
Id
) {
    $('.' + Id + '_requestDeliveryDate').css("display", "none");
}

function submitRequestDeliveryDate(
sender,
Id
) {
    $('.' + Id + '_requestDeliveryDate').css("display", "none");

    var input_index = 0;
    $(sender).parent().each(function() {
        $(':input[type=text]', this).each(function() {
            if (input_index == 0) {
                $.post(
                "LogisticsRequestDelivery.ashx",
                { "Id": Id, "RequestedDelivery": $(this).val() },
                function(data, textStatus) {
                    $('.' + Id + '_showRequestedDelivery').text(data);
                },
                "text"
            );
            }
            input_index++;
        });
    });
}
/* END */

function setAsPriority
(
sender,
Id,
cliRef,
notification
) {
    if (confirm("Set '" + cliRef + "' as priority?"))
        {
            $(sender).attr("disabled", "disabled");
            $.post(
            "ReportSetPriority.ashx",
            { "Id": Id, "Priority": "true", "SendNotification": notification },
            function(data, textStatus) {
                $('.' + Id + '_showRequestedDelivery').text(data);
            },
            "text"
        );
    }
    return false;
}
/* END */

function reportSectionToggle(
    sender,
    dataSourceUrl,
    Id,
    params
) {
    if (params == undefined)
        params = {};

    params.Id = Id;

    if ($(sender).data("loaded") == undefined) {
       
        var itemsTableCell = $(sender).parents(".itemRow").next(".subItemRow").find("td");
        $(itemsTableCell).load(
            dataSourceUrl + ' .items',
            params,
            function () {
                $(sender).data("loaded", true);
                reportSectionToggleDisplay(sender);                
            }
        );
    }
    else {
        reportSectionToggleDisplay(sender);
    }

    //reportSectionToggleSiblings(sender);

    return false;
}

function reportSectionToggleDisplay(sender) {

    var subItemsRow = $(sender).parents(".itemRow").next(".subItemRow");

    subItemsRow.toggle();
    //$(sender).removeClass("show");
    
    if ($(sender).val() == "+") {
        $(sender).val("-");
        //$(sender).addClass("show");
    }
    else {
        $(sender).val("+");
    }
}

//only works for freight station and maf reports
function reportSectionToggleSiblings(sender) {

    var sibilingsToggleButtons = $(sender).parents(".itemRow").siblings(".itemRow").find(".show");
    reportSectionToggleDisplay(sibilingsToggleButtons);
}


/* TRANSPORT MARK AS EMPTY FUNCTIONALITY */
function markItemAsEmpty(
    sender,
    Id,
    command,
    cliRef,
    tapperRef
) {
    if (confirm("You are about to mark consignment '" + cliRef + "', Tapper Ref '" + tapperRef + "' as empty ready. Are you sure?")) {

        $(sender).attr("disabled", "disabled");

        $.post(
            "TransportCommand.ashx",
            { "Id": Id, "CommandName": command },
            function(data, textStatus) {
                $(sender).parent().text(data);
            },
            "text"
        );
    }

    return false;
}
/* END */

/* TRANSPORT FCL Export Ready FUNCTIONALITY */
function markAsFReady(
    sender,
    Id,
    cliRef,
    tapperRef
) {
    if (confirm("You are about to mark consignment '" + cliRef + "', Tapper Ref '" + tapperRef + "' as FCL export ready. Are you sure?")) {

        ShowPopup(cliRef, tapperRef, Id);
    }

    return false;
}

function submitFReady() {
    
    var Id = $('.ExportReadyId').val();
    var Command = $('.CommandValue').val();
    var DateTime = $('.radEnabledCss_Default').val();
    var ContainerWeight = $('.WeightValue').val();

    if (ContainerWeight.length == 0) {
        alert("Container Weight is required.");
        return false;
    }
    
    $('.' + Id + '_ExportReadyButton').attr("disabled", "disabled");

    $.post(
        "TransportCommand.ashx",
        { "Id": Id, "CommandName": Command, "DateTime": DateTime, "ContainerWeight": ContainerWeight },
        function(data, textStatus) {
            $('.' + Id + '_ExportReady').text(data);
        },
        "text"
    );

    ClosePopup();
    return false;
}

//popup functions
function ShowPopup(cliRef, tapperRef, Id) {
    if ($('#FCLEReadyPopup').css('display') == 'none') {

        $('#FCLEReadyPopup').animate({ height: 'show', opacity: 'show' }, 'slow');
        $('.title').html("Updating consignment '" + cliRef + "', Tapper Ref '" + tapperRef + "' as FCL export ready.");
        $('.ExportReadyId').val(Id);
        
    }
}

function ClosePopup() {
    $('#FCLEReadyPopup').animate({ height: 'hide', opacity: 'hide' }, 'slow');
}
/* END */

/* ITEM STATUS NOTIFICATION FUNCTIONALITY */
function notifyStatus(
    sender,
    tapperRef,
    itemSequence,
    status,
    reportItem,
    skin
) {
    if (sender.checked) {
        if (confirm("Receive an email every time there is a status change on job '" + tapperRef + "'?")) {

            $.post(
                "../ReportUpdateStatus.ashx",
                { "ItemSequence": itemSequence, "Subscribe": sender.checked, "Status": status, "ReportItem": reportItem, "Skin": skin }
            );

            return true;
        }
        else {
            return false;
        }
    }
    else {

        //if unchecked, update the report subscription
        $.post(
                "../ReportUpdateStatus.ashx",
                { "ItemSequence": itemSequence, "Subscribe": sender.checked, "Status": status, "ReportItem": reportItem, "Skin": skin }
            );        
    }
}
/* END */

/* FREIGHT STATION RELEASE FUNCTIONALITY */
function releaseItem(
    sender,
    Id,
    command,
    cliRef,
    tapperRef
) {
    if (confirm("You are about to release consignment '" + cliRef + "', Tapper Ref '" + tapperRef + "'. Are you sure?")) {

        $(sender).attr("disabled", "disabled");

        $.post(
            "FreightStationRelease.ashx",
            { "Id": Id, "CommandName": command },
            function(data, textStatus) {
                $(sender).parent().text(data);
            },
            "text"
        );
    }

    return false;
}
/* END */

/* FREIGHT STATION RELEASE AUTHORITIES */
function downloadAuthDocs(
    sender,
    tapperRef
) {
    if (confirm("You are about to download release authorities for Tapper Ref# '" + tapperRef + "'. Are you sure?")) {

        /*$(sender).attr("disabled", "disabled");

        $.post(
            "/Reports/ReleaseAuthorities.ashx",
            { "TapperReference": tapperRef },
            function (data, textStatus) {
                alert(data);
            },
            "text"
        );*/
    }

    return false;
}
/* END */

/* FREIGHT STATION DELIVERY REQUEST and QUERY FUNCTIONALITY */

function sendDespatchRequest(
    confirmMessage,    
    subjectLine,
    body,
    footer,
    emailTo
) {
    if (confirm(confirmMessage)) {           
        var mailto_link = 'mailto:' + emailTo + '?subject=' + subjectLine + '&body=' + escape(body + '\n\n' + footer);
        
        win = window.open(mailto_link,'emailWindow');
        if (win && win.open &&!win.closed) win.close();
    }
}

/* END */

/*Show/Hide additional Report Subscription Emails*/
$(document).ready(function() {
    $(".ShowHide").click(function() {
        $(".ReportSubscriptionEmails").toggle("normal");
    });
});

/* TRANSPORT PORT TRACKING ITEM */
function pollThisJobNow(
    sender,
    itemSequence,
    container,
    isMetroport
) { 

    $.post(
        "PortTracking.ashx",
        { "Container": container, "ItemSequence": itemSequence, "isMetroport": isMetroport },
        function(data, textStatus) {

            if (data.indexOf(',') == -1) {
                $(sender).parent().text(data);
            }
            else {
                var items = data.split(',');
                $('.location_' + itemSequence).text(items[0]);
                $('.stops_' + itemSequence).text(items[1]);
                $('.maf_' + itemSequence).text(items[2]);
                $('.customs_' + itemSequence).text(items[3]);
                $('.op_' + itemSequence).text(items[4]);
                $('.check_' + itemSequence).text(items[5]);
                $('.lastpolled_' + itemSequence).text(items[6]);
                $('.eta_' + itemSequence).text(items[7]);
            }
        },
        "text"
    );

    return false;
}

/* MAF notification */
function mafNotify(sender, Id, container, tapperRef, type) {

    var message = "You are about to set container '"+ container +"' on hold, Tapper Ref '" + tapperRef + "'. Are you sure?";
    if (type == "Release")
        message = "You are about to release container '" + container + "', Tapper Ref '" + tapperRef + "'. Are you sure?";
    if (type == "Complete")
        message = "You are about to set container '" + container + "' to MAF complete, Tapper Ref '" + tapperRef + "'. Are you sure?";

    if (confirm(message)) {

        $(sender).attr("disabled", "disabled");

        $.post(
            "MAFNotification.ashx",
            { "Id": Id, "Type": type },
            function (data, textStatus) {
                $(sender).parent().text(data);
            },
            "text"
        );
    }

    return false;
};

function setMAFComplete(sender, Id, container, tapperRef, type) {    
    if (sender.checked) {
        var message = "You are about to set container '" + container + "' to MAF complete, Tapper Ref '" + tapperRef + "'. Are you sure?";
        if (confirm(message)) {

            $.post("MAFNotification.ashx", { "Id": Id, "Type": type, "IsComplete": sender.checked });
            return true;
        }
        else {
            return false;
        }
    }
    else { //if unchecked, update the status        
        $.post("MAFNotification.ashx", { "Id": Id, "Type": type, "IsComplete": sender.checked });
    }
}

/* Freight Station customs delivery reference */
function setReferenceNumber(sender) {
    if ($(sender).val() == "Type in reference here")
        $(sender).val("");
}

function blurReferenceNumber(sender, id) {
    var value = $(sender).val();
    if (value == "") {
        $(sender).val("Type in reference here")
    } else {        
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Reports/FreightStation/FreightStationService.asmx/SetReferenceNumber",
            data: "{\"cilid\": " + id + ", \"value\":\"" + value + "\"}",
            dataType: "json",
            success: function (msg) {
                if (msg.d != "") {
                    alert("Error: " + msg.d);
                }
            }
        });
    }
}

