﻿function AddItem(ci, refURL) {
    $.ajax({
        url: "include/g_MarketCar_S.aspx",
        type: "POST",
        data: "Action=AddItem&ci=" + encodeURIComponent(ci) + "&refURL=" + refURL + "&Flag=" + new Date().getTime(),
        cache: false,
        global: false,
        dataType: "xml",
        error: function () {
            $('#dialogtext').html("error");
            ShowMessage();
        },
        success: function (result) {
            $('#dialogtext').html($(result).find('msg').text());
            if ($(result).find('status').text() == "T") {
                ShowMessage();
                ShowMarketCarPnl();
                $("#dialog-message").dialog({
                    width: 350,
                    buttons: {
                        "查看購物車": function () {
                            document.location = "/MarketCar.aspx";
                        },
                        "開始結帳": function () {
                            document.location = "/Counter.aspx";
                        },
                        "繼續購物": function () {
                            $(this).dialog("close");
                        }
                    }
                });
            }
            else {
                ShowMessage();
            }
        }
    });
};

function AddBuyIt(ci, refURL) {
    $.ajax({
        url: "include/g_MarketCar_S.aspx",
        type: "POST",
        data: "Action=AddBuyIt&ci=" + encodeURIComponent(ci) + "&refURL=" + encodeURI(refURL) + "&Flag=" + new Date().getTime(),
        cache: false,
        global: false,
        dataType: "xml",
        error: function () {
            $('#dialogtext').html("error");
            ShowMessage();
        },
        success: function (result) {
            if ($(result).find('status').text() == "T") {
                document.location = "/CounterExpress.aspx";
            }
            else {
                $('#dialogtext').html($(result).find('msg').text());
                ShowMessage();
            }
        }
    });
};

function BuyIt(ci) {
    $.ajax({
        url: "include/g_MarketCar_S.aspx",
        type: "POST",
        data: "Action=GetItemCount&Flag=" + new Date().getTime(),
        cache: false,
        global: false,
        dataType: "xml",
        error: function () {
            $('#dialogtext').html("error");
            ShowMessage();
        },
        success: function (result) {
            if ($(result).find('status').text() == "T") {
                if ($(result).find('msg').text() > "0") {
                    $('#dialogtext').html("您的購物車中還有其他商品，是否置入購物車中一起結帳?");
                    $("#dialog-message").dialog({
                        modal: true,
                        draggable: false,
                        resizable: false,
                        width: 350,
                        buttons: {
                            "加入購物車": function () {
                                AddItem(ci);
                            },
                            "直接結帳": function () {
                                AddBuyIt(ci);
                            },
                            "繼續購物": function () {
                                $(this).dialog("close");
                            }
                        }
                    });
                }
                else {
                    AddBuyIt(ci);
                }
            }
            else {
                $('#dialogtext').html($(result).find('msg').text());
                ShowMessage();
            }
        }
    });
};

