﻿
$(document).ready(function() {
    pageLoadFunc();
});

function pageLoadFunc() {
    
    var originalFreight = $(".tbFreightCharge").val();
    var totalline = $(".totalline");
    var total = $(".tbTotalAfterDiscountOriginal").val();
    
    $(".tbTotalAfterDiscountOriginal2").val($(".tbTotalAfterDiscountOriginal").val());
    $(".tbFreightChargeOriginal2").val($(".tbFreightChargeOriginal").val());
    $(".tbFreightChargeOriginalCalculated2").val($(".tbFreightChargeOriginalCalculated").val());
    var calcFreight = $("#calcFreight").val();
    $(".tbCreditAdjustment").blur(function() {
        RecalcTotal(total, totalline, originalFreight, $(".tbFreightCharge").val());
        checkForFreeFreight(true);
    });

    $(".tbDeliverySurcharge").blur(function() {
        RecalcTotal(total, totalline, originalFreight, $(".tbFreightCharge").val());
        checkForFreeFreight(true);
    });

    $(".tbFreightCharge").blur(function() {
        try {
            var mfc = parseFloat($(".tbFreightCharge").val());
            if (!mfc) {
                $(".tbFreightCharge").val("0.00");
            }
        }
        catch (e) { $(".tbFreightCharge").val("0.00"); }
        RecalcTotal(total, totalline, originalFreight, $(".tbFreightCharge").val());
        checkForFreeFreight(true);
    });


    $(".tbOrderAdjustment").blur(function() {
        RecalcTotal(total, totalline, originalFreight, $(".tbFreightCharge").val());
        checkForFreeFreight(true);
    });

    $(".tbCreditAdjustment").focus(function() {
        this.select();
    });

    $(".tbDeliverySurcharge").focus(function() {
        this.select();
    });

    $(".tbFreightCharge").focus(function() {
        this.select();
    });

    $(".tbOrderAdjustment").focus(function() {
        this.select();
    });


    $(".chkRecalcFreight").click(function() {
        if ($('.chkRecalcFreight').is(':checked')) {
            RecalcTotal(total, totalline, originalFreight, calcFreight);
            checkForFreeFreight(false);
        }
        else {
            $(".tbFreightCharge").val($(".tbFreightChargeDB").val());
            RecalcTotal(total, totalline, originalFreight, calcFreight);
            checkForFreeFreight(true);
        }
    });

    $(".chkExpeditedFee").click(function() {
        RecalcTotal(total, totalline, originalFreight, calcFreight);
        checkForFreeFreight();
    });

    $(".fnTD:empty").parent().remove();
    $.each($(".fnTD"), function(i, v) {
        var theElement = $(v);
        if ($.trim(theElement.text()).length == 0) {
            theElement.parent().remove();
        }
    });

    $(".fnTD:empty").each()
    $("td:empty").parent().remove();


    if ($("#docalc").val() == "1") {
        total = total - calcFreight;
    }

    try {

        checkForFreeFreight(true);
    }
    catch (e) {
        RecalcTotal(total, totalline, originalFreight, $(".tbFreightChargeOriginalCalculated").val());
    }
}


function RecalcTotal(total, totalline, originalFreight, calculatedFreight) {
    //var total = totalline.html().replace("$", "").replace("", "").replace("", "");
    var deliverySurcharge = $(".tbDeliverySurcharge").val();
    var creditAdjustment = $(".tbCreditAdjustment").val();
    var orderAdjustment = $(".tbOrderAdjustment").val();
    var freightCharge = $(".tbFreightCharge").val();
    var totalAvailableCredit = $(".tbTotalAvailableCredit").val();
    var dsNum = 0.00;
    var caNum = 0.00;
    var oaNum = 0.00;
    var expFeeNum = 0.00;
    var newTotal = 0.00;
    var fcNum = 0.00;
    var ofcNum = 0.00;
    var calcFreightNum = 0.00;
    var totAvailCredit = 0.00;


    try {
        if ($('.chkExpeditedFee').is(':checked')) {
            var expFee = ($(".expFee").html());
        }
    }
    catch (e) { }
    dsNum = parseFloat(deliverySurcharge);
    caNum = parseFloat(creditAdjustment);
    oaNum = parseFloat(orderAdjustment);
    expFeeNum = parseFloat(expFee);
    fcNum = parseFloat(freightCharge);
    ofcNum = parseFloat(originalFreight);
    calcFreightNum = parseFloat(calculatedFreight);
    totAvailCredit = parseFloat(totalAvailableCredit);

    if (!dsNum) dsNum = 0.00;
    if (!caNum) caNum = 0.00;
    if (!oaNum) oaNum = 0.00;
    if (!expFeeNum) expFeeNum = 0.00;
    if (!fcNum) fcNum = 0.00;
    if (!ofcNum) ofcNum = 0.00;
    if (!calcFreightNum) calcFreightNum = 0.00;

    fcNum = calcFreightNum;

    //}
    if (!totAvailCredit) totAvailCredit = 0.00;

    if (!deliverySurcharge) {
        $(".tbDeliverySurcharge").val('');
    }
    else {
        $(".tbDeliverySurcharge").val(dsNum);
    }
    if (!creditAdjustment) {
        $(".tbCreditAdjustment").val('');
    }
    else {
        $(".tbCreditAdjustment").val(caNum);
        $(".lblCreditAdjustmentValue").html("$" + caNum.toFixed(2) + "");
    }
    if (!orderAdjustment) {
        $(".tbOrderAdjustment").val('');
    }
    else {
        $(".tbOrderAdjustment").val(oaNum);
    }

    if ($(".tbFreightCharge").length) {
        try {
            $(".lblFreightCharge").html("")
        }
        catch (e) { }
    }
    else {
        if ($("#docalc").val() == "1") {
            try {
                $(".lblFreightCharge").html("$" + fcNum.toFixed(2) + "")
            }
            catch (e) { }
        }
    }
    $(".lblExpeditedFee").html("$" + expFeeNum.toFixed(2) + "")

    var newTotalWOCredit = parseFloat(parseFloat(total) + oaNum + fcNum + dsNum + expFeeNum);

    if (newTotalWOCredit < totAvailCredit) {
        caNum = newTotalWOCredit;
    }
    else {
        caNum = totAvailCredit;
    }

    $(".tbCreditAdjustment").val(caNum);
    $(".lblCreditAdjustmentValue").html("$" + caNum.toFixed(2) + "");
    newTotal = newTotalWOCredit - caNum;
    totalline.html("$" + newTotal.toFixed(2) + "");
}
    
    

