
// 总金额
function CalculateTotalPrice() {
    var cartTotalPrice = $("#SubmitOrder_CartTotalPriceLabel_v").val();
    var shippmodePrice = $("#SubmitOrder_ShippingModePrice_v").val();
    var orderOptionPrice = $("#SubmitOrder_OrderOptionPriceLabel_v").val();
    var couponPrice = $("#SubmitOrder_CouponPriceLabel_v").val();

    var total = eval(cartTotalPrice) - couponPrice;

    if ($("#SubmitOrder_ShipChargeFeeName").html() == "")
        total = total + eval(shippmodePrice);
    if ($("#SubmitOrder_PackingChargeFreeName").html() == "")
        total = total + eval(orderOptionPrice);
    // 计算支付手续费
    var paymentModeId = $('#SubmitOrder_inputPaymentModeId').val();
    $.ajax({
        url: "SubmmitOrderHandler.aspx",
        type: 'post', dataType: 'json', timeout: 10000,
        data: { Action: 'ProcessorPaymentMode', ModeId: paymentModeId, CartTotalPrice: total },
        success: function(resultData) {
            if (resultData.Status == "OK") {
                if ($("#SubmitOrder_ServiceChargeFreeName").html() == "") {
                    $("#SubmitOrder_PaymentPriceLabel").html(resultData.Charge);
                    $("#SubmitOrder_PaymentPriceLabel_v").val(resultData.Charge_v);
                    total = total + eval(resultData.Charge_v);
                }
                else {
                    $("#SubmitOrder_PaymentPriceLabel").html("￥0.00");
                    $("#SubmitOrder_PaymentPriceLabel_v").val("0");
                }
                // 计算订单总金额
                $.ajax({
                    url: "SubmmitOrderHandler.aspx",
                    type: 'post', dataType: 'json', timeout: 10000,
                    data: { Action: 'FormatMoney', value: total },
                    cache: false,
                    success: function(resultData) {
                        $("#SubmitOrder_OrderTotalPriceLabel").html(resultData.Money);
                        $("#SubmitOrder_OrderTotalPriceLabel_v").val(resultData.Money_v);
                        $("#SubmitOrder_PointTotal").html(resultData.Point);
                    }
                });
            }
        }
    });

}

// 重新计算运费
function CalculateFreight(regionId) {
    var weight = $("#SubmitOrder_Weight").val();
    var shippingModeId = $("#hdmodeId").val();
    $.ajax({
        url: "SubmmitOrderHandler.aspx",
        type: 'post', dataType: 'json', timeout: 10000,
        data: { Action: 'CalculateFreight', ModeId: shippingModeId, Weight: weight, RegionId: regionId },
        success: function(resultData) {
            if (resultData.Status == "OK") {
                if ($("#SubmitOrder_ShipChargeFeeName").html() == "") {
                    $("#SubmitOrder_ShippingModePrice").html(resultData.Price);
                    $("#SubmitOrder_ShippingModePrice_v").val(resultData.Price_v);
                }
                else {
                    $("#SubmitOrder_ShippingModePrice").html("￥0.00");
                    $("#SubmitOrder_ShippingModePrice_v").val("0");
                }
                CalculateTotalPrice();
            }
        }
        , error: function() { alert("Error"); }
    });
}



//处理订单选项异步处理
function ProcessorOrderOption(lookupItemId, id) {
    $.ajax({
        url: "SubmmitOrderHandler.aspx",
        type: "post",
        dataType: "json",
        data: { Action: 'ProcessorOrderOption', LookupItemId: lookupItemId, CarTotal: $("#SubmitOrder_CartTotalPriceLabel_v").val() },
        cache: false,
        success: function(resultData) {
            if (resultData.Status == "OK") {
                if (resultData.IsUserInputRequired == "true") {
                    //如果需要用户填写
                    $("#" + id + "tdOption").css({ 'display': '' });
                    $("#" + id + "litInputTitle").val(resultData.UserInputTitle);
                    $("#" + id + "txtInputContent").val(resultData.UserInputContent);

                    var inputcontent = id + 'txtInputContent';
                    if ($("#" + inputcontent).val().length < 3 || $("#" + inputcontent).val().length > 20) {
                        alert("可选项录入值字符限制在3-20之间");
                    }
                }

                else {
                    //如果不需要用户填写
                    $("#" + id + "tdOption").css({ 'display': 'none' });
                }
                document.getElementById(id + "tdOption").getElementsByTagName('input')[2].value = resultData.AppendMoney_v;

                if ($("#SubmitOrder_PackingChargeFreeName").html() == "") {
                    ResetOptionPrice();
                }
                else {
                    $("#SubmitOrder_OrderOptionPriceLabel_v").val("0.00");
                    $("#SubmitOrder_OrderOptionPriceLabel").html("￥0.00");
                }
                CalculateTotalPrice();
            }
        }
    });
}

// 订单选项费用
function ResetOptionPrice() {
    var x = document.getElementsByName("submmitorder_optionPrice");
    var price = eval(0.00);
    for (var i = 0; i < x.length; i++) {
        if (x[i].value != "")
            price = price + eval(x[i].value);
    }
    $("#SubmitOrder_OrderOptionPriceLabel_v").val(price);
    $("#SubmitOrder_OrderOptionPriceLabel").html(Math.round(price * 100) / 100);
}

$(function() {
    //修改配送方式切换效果
    $("#sp_change").toggle(function() {
        $("#div_shipping").slideToggle(800);
    }, function() {
        $("#div_shipping").slideUp(800);
    });

    //选择配送方式，修改运费效果
    $("input[name='radshippingmode']").click(function() {
        if (this.checked) {
            $("#hdmodeId").val(this.value);
            $("#em_shipping").text($(this).parent().text().replace(/\s/g, ""));
            var regionId = $("#regionSelectorValue").val();
            if (regionId != null && regionId != "" && regionId > 0)
                CalculateFreight(regionId); //修改运费
        }
    });

    //选择支付方式，修改支付费用
    $("input[type='radio'][name='paymentModeList']").click(function() {
        if (this.checked) {
            $("#SubmitOrder_inputPaymentModeId").val(this.value);
        }
    });


    // 订单选项Radiolist触发事件
    $("input[name^='orderOptionList$_$dlstOrderLookupList$'][type='radio']").bind('click', function() {
        // 组合当前项相同的Id部分以便后面可以找到相同项的其他控件
    var id = this.id.replace('orderOptionList$_$dlstOrderLookupLis', '');
        id = id.substring(0, id.indexOf("_"));
        id = 'orderOptionList___dlstOrderLookupList_' + id + "_";
        ProcessorOrderOption(this.value, id);
    })

    // 订单选项为下拉框选择触发事件
    jQuery.myfn = {
        OrderOptionSelectForDropDownList: function(SelectControl) {
            // 组合当前项相同部分Id
        var id = SelectControl.name.replace('orderOptionList$_$dlstOrderLookupList$', '');
            id = id.substring(0, id.indexOf("$"));
            id = 'orderOptionList___dlstOrderLookupList_' + id + "_";

            if (SelectControl.selectedIndex == 0) {
                $("#" + id + "tdOption").css({ 'display': 'none' });
                $("#" + id + "litInputTitle").val('');
                $("#" + id + "txtInputContent").val('');
                document.getElementById(id + "tdOption").getElementsByTagName('input')[2].value = 0;
                ResetOptionPrice();
                CalculateTotalPrice();

                return;
            }
            ProcessorOrderOption(SelectControl.value, id);
        }
    };



    //创建订单
    $("#btntotal").click(function() {
        $options = $(".shouhuodzhi div span");
        if ($options.eq(1).text().replace(/\s/g, "") == "") {
            alert("请输入收货人详细地址");
            return false;
        }
        var reg = new RegExp("[\u4e00-\u9fa5a-zA-Z]+[\u4e00-\u9fa5_a-zA-Z0-9]*");
        var str = $options.eq(2).text().replace(/\s/g, "");
        if (str == "" || !reg.test(str)) {
            alert("请输入正确的收货人姓名");
            return false;
        }

        if ($options.eq(3).text().replace(/\s/g, "") == "") {
            alert("请输入联系号码");
            return false;
        }
        // 验证配送地区选择了没有
        var selectedRegionId = $("#regionSelectorValue").val();
        if (selectedRegionId == null || selectedRegionId.length == "" || selectedRegionId == "0") {
            alert("请选择您的收货人地址");
            return false;
        }

        if ($("#hdmodeId").val() == "" || $("#hdmodeId").val() == "0" || $("#hdmodeId").val()=="undefined") {
            alert("请选择配送方式");
            return false;
        }
        if ($("#SubmmitOrder_inputPaymentModeId").val() == "") {
            alert("请选择支付方式");
            return false;
        }

    });
});
