﻿
function CheckShopCartIsZero() {
    if ($('.miniCartBuyAmount').length == 0) {
        alert('很抱歉, 購物車己沒商品無法結帳!');
        window.location = '../index.aspx';
    }

}

function SetTotalFeeInfo() {
    //商品金額總計
    var totalBuyAmount = 0;
    var totalBoxCount = 0;
    var pdfTransport = 0;
    var handFee = 0;
    $('.subtotal').each(function(i) {
        totalBuyAmount += parseInt($(this).text());
    });
    $("#total").html(totalBuyAmount);
    //取得所有盒數,

    $(".buySelect").each(function(i) {
        totalBoxCount += parseInt($(this).val());
    });
    $("#totalBoxCount").html(totalBoxCount);
    if (totalBoxCount > 9) pdfTransport = 0;
    else if (totalBoxCount > 7)
        pdfTransport = 460;
    else if (totalBoxCount > 6)
        pdfTransport = 400;
    else if (totalBoxCount > 3)
        pdfTransport = 260;
    else if (totalBoxCount > 1)
        pdfTransport = 200;
    else
        pdfTransport = 140;
    //手續費
    if ($('input[type=radio][name=paymethod]:checked').val() == 'blackcat') {
        if (totalBuyAmount > 20000)
            handFee = 150;
        else if (totalBuyAmount > 10000)
            handFee = 120;
        else if (totalBuyAmount > 5000)
            handFee = 90;
        else if (totalBuyAmount > 2000)
            handFee = 60;
        else
            handFee = 30;
    } else {
        handFee = 0;
    }
    $("#TdHandFee").html(handFee);
    $("#PDTransportFee").html(pdfTransport);
    //購買費用          //運費         //手續費
    var totalFee = parseInt(totalBuyAmount) + parseInt(pdfTransport) + parseInt(handFee);
    $("#orderPrice").html(totalFee);
    if(   $("#TotalPrice_str").length >0){
       $("#TotalPrice_str").html('NT ' + totalFee);
    }

}
function CalHandCost(isCalHandCost,totalBuyAmount) {
    if (!isCalHandCost) return 0;

    var handFee = 0;
       if (totalBuyAmount > 20000)
            handFee = 150;
        else if (totalBuyAmount > 10000)
            handFee = 120;
        else if (totalBuyAmount > 5000)
            handFee = 90;
        else if (totalBuyAmount > 2000)
            handFee = 60;
        else
            handFee = 30;
        return handFee;
}
function CalShippingCost(totalBoxCount) {
    var pdfTransport = 0;
    if (totalBoxCount > 9) pdfTransport = 0;
    else if (totalBoxCount > 7)
        pdfTransport = 460;
    else if (totalBoxCount > 6)
        pdfTransport = 400;
    else if (totalBoxCount > 3)
        pdfTransport = 260;
    else if (totalBoxCount > 1)
        pdfTransport = 200;
    else
        pdfTransport = 140;
        
    return pdfTransport;    
 }
function deleteItem(parItemno) {
    if (!confirm("確定要刪除?")) {
        return false;
    }
    var deleteFromShopServiceList = "../service/shopservice.aspx?cmd=deletefrombasket";
    $.ajax({
        type: "POST",
        url: deleteFromShopServiceList,
        data: { itemno: parItemno },
        dataType: "json",
        async: false,
        success: function(data) {
            if (!data.Success) {
                alert('error');
            } else {
                // alert('刪除成功');
                GetShopCartList();
                GetShopCartInfo();

            }
        }

    });
}

function GetShopCartList() {
    var getShopServiceList = "../service/shopservice.aspx?cmd=getshopcartlist";
    $.post(getShopServiceList, function(data) {
      $("#shop_list_body").html(data);
    
        setTimeout(function() { CheckShopCartIsZero() }, 1000);
        //取得總金額
        SetTotalFeeInfo();
    });
}
function GetShopCartListNotCheckIsZero() {
    var getShopServiceList = "../service/shopservice.aspx?cmd=getshopcartlist";
    $.post(getShopServiceList, function(data) {
        $("#shop_list_body").html(data);
         //取得總金額
       // SetTotalFeeInfo();
    });
}
function switchAuthorStatus(authorstatus) {
    var ss = '';
    if (!authorstatus) {
       // ss= "您好,歡迎來到<span style=\"color: blue;\">阿拉兄果園</span>";
        ss = "     <a href=\"<%=Request.ApplicationPath%>" + "/Home/login.aspx\">登入</a>";
        
        $("#ctl00_lbLoginInfo").html(ss);
    }
    else {
        ss = "     <a href=\"<%=Request.ApplicationPath%>" + "/Home/loginout.aspx\">登出</a>";
    }
    $("#ctl00_lbLoginInfo").html(ss);
}

          

