﻿// JScript 文件

$(document).ready(function() {

    //增加单品个数start
    $("img[name^=a1]").click(function() {

        $("div[name=navLoading]").css("display", "block");
        var currentCount = $(this).prev().prev().val();
        if (currentCount == "999") {
            $("div[name=navLoading]").css("display", "none");
            alert("此单品最大订购数量为999！");
        }
        else {
            var productParamId = $(this).attr("name").split("_")[1];
            OpratorCart(productParamId, "1");
        }

    });
    //增加单品个数end

    //减少单品个数start
    $("img[name^=a2]").click(function() {
        $("div[name=navLoading]").css("display", "block");
        var currentCount = $(this).prev().prev().prev().val();
        if (currentCount == "1") {
            $("div[name=navLoading]").css("display", "none");
            alert("商品最少订购数量为1，如果要取消订购此商品，请点击“删除”");
        }
        else {
            var productParamId = $(this).attr("name").split("_")[1];
            OpratorCart(productParamId, "0");
        }

    });
    //减少单品个数end


    //删除start
    $("a[name^=del]").click(function() {

        var productParamId = $(this).attr("name").split("_")[1];
        OpratorCart(productParamId, "3");

    });
    //删除end

    $("#txtCorpCouponsMoney").change(function() {
        GetExpressPrice();
    });



    //更改支付方式,更改配送方式 start
    $("input[name='radPay'],input[name='radExpress']").click(function() {
        GetExpressPrice();
    });
    //更改支付方式,更改配送方式 end 



    //更改优惠价格 start
    $("#txtOfferPrice,#txtExpressPrice,#navCorpCouponsRemark,#selExpressMode,#selArea").change(function() {
        GetExpressPrice();
    });
    //更改优惠价格 end




    //更多优惠券start
    $("#navMoreCorpCoupons").click(function() {

        var spanObject = $("#spanCorpCoupons span:last").clone(false);
        $("input", spanObject).val("");
        $("select", spanObject).attr("disabled",false);
        $("input", spanObject).css("background-color", "");
        $("Label", spanObject).text('请输入券号！');
        if ($("input[name=txtcheckcode]", spanObject).length > 0)
            $("input[name=txtcheckcode]", spanObject).remove();
        if ($("#check_img", spanObject).length > 0)
            $("#check_img", spanObject).remove();
        $("#spanCorpCoupons").append(spanObject);
    });
    //更多优惠券end

    $("input[name=txtAmount]").live("change", function() {
        if ($(this).val() == "")
            return;
        if (isNaN($(this).val()) || parseFloat($(this).val()) <= 0) {
            alert("面值必须为有效数字且大于0！");
            $(this).val("");
            $(this).focus();
            return;
        }
        var temp = 0;
        $("input[name=txtAmount]").each(function() {
            if ($(this).val() != "" && !isNaN($(this).val())) {
                temp += parseFloat($(this).val());
            }
        });
        $("#txtCorpCouponsMoney").val(temp);
    })

    //添加订单的商品 start
    $('#ChooseOrderProduct').click(function(e) {
        e.preventDefault();
        $('#ChooseOrderProductContent').modal({ onClose: function(dialog) {
            //关闭事件

            var storeId = $("#ddlStore").val();
            $.ajax({ url: 'OrderProducthtml.aspx?storeid=' + storeId,
                type: 'GET',
                dataType: 'html',
                timeout: 10000,
                error: function() {
                },
                success: function(html) {
                    $('#NavCart').html(html);
                    $('#txtOrderProduct').val(html);
                    GetExpressPrice();
                }
            }); //end of ajax


            $.modal.close();
        },
            onShow: function(dialog) {
                //展示事件

                var myw = parent.document.body.clientWidth - 250;
                var myh = parent.document.body.clientHeight - 120;

                $('#simplemodal-container').css("width", myw);
                $('#simplemodal-container').css("height", myh);
                $('#simplemodal-container').css("width", myw);
                $('#simplemodal-container').css("left", 20);
                $('#simplemodal-container').css("top", 20);
                $('#ChooseOrderProductContent iframe').css("height", myh - 35);
                $('#ChooseOrderProductContent iframe').css("width", myw - 10);
                $('#simplemodal-container .basicModalTitle').css("width", myw - 5);
                $('#simplemodal-container .basicModalBottom').css("width", myw - 20);

                var storeId = $("#ddlStore").val();
                var url = "orderproduct.aspx?storeid=" + storeId + "&v=" + Math.random();
                $('#ChooseOrderProductContent iframe').attr("src", url);

                $('#btnImgSubmit').click(function(e) {

                    $.modal.close();
                })
            }
        });
    });
    //添加订单的商品 end	 



    //编辑订单的商品 start
    $('#btnEditProduct').click(function(e) {

        e.preventDefault();
        //清空购物车
        $.ajax({ url: 'ClearCartHtml.aspx',
            type: 'GET',
            dataType: 'html',
            timeout: 10000,
            error: function() {
            },
            success: function(html) {
            }
        }); //end of ajax

        $('#EditContent').modal({ onClose: function(dialog) {

            $.modal.close();
        },
            onShow: function(dialog) {
                //展示事件

                $('#simplemodal-container .basicModalTitle').css("width", "745px");
                $('#simplemodal-container').css("width", "750px");
                $('#simplemodal-container .basicModalBottom').css("width", "730px");

                //确定
                $('#btnEditSubmit').click(function(e) {

                    //保存订单商品更改结果
                    var id = $("#txtId").val();
                    $.ajax({ url: '/order/edit/savehtml.aspx?id=' + id,
                        type: 'GET',
                        dataType: 'html',
                        timeout: 10000,
                        error: function() {
                        },
                        success: function(html) {
                            if (html == "1") {
                                alert("保存成功");
                                location.reload();
                            }
                            else {
                                alert("保存失败");
                            }
                        }
                    }); //end of ajax

                    $.modal.close();
                })
            }
        });
    });
    //编辑订单的商品 end





    //订单筛选姓名 start
    $('#ChooseName,#ChooseTelephone,#ChooseEmail').click(function(e) {
        e.preventDefault();
        var thisId = $(this).attr("id");
        $('#ChooseNameContent').modal({ onClose: function(dialog) {

            $.modal.close();
        },
            onShow: function(dialog) {

                //展示事件

                var url = "OrderName.aspx";
                if (thisId == "ChooseName")
                { var name = $('#txtNameView').val(); url += "?name=" + name; }
                else if (thisId == "ChooseTelephone")
                { var tel = $('#txtTelephone').val(); url += "?tel=" + tel; }
                else if (thisId == "ChooseEmail")
                { var email = $('#txtEmail').val(); url += "?email=" + email; }
                $('#ChooseNameContent iframe').attr("src", url);

                $('#btnClassSubmit').click(function(e) {
                    //取值
                    var trObject = $('#ChooseNameContent iframe').contents().find("input:checked[name=radSelect]").parent();
                    var consigneeValue = trObject.find("input[name='hidConsignee']").val();
                    var telValue = trObject.find("input[name='hidTelphone']").val();
                    var emailValue = trObject.find("input[name='hidEmail']").val();
                    var addressValue = trObject.find("input[name='hidAddress']").val();
                    var postIdValue = trObject.find("input[name='hidPostId']").val();
                    var areaIdValue = trObject.find("input[name='hidAreaId']").val();
                    var provinceIdValue = trObject.find("input[name='hidProvinceId']").val();
                    var citytIdValue = trObject.find("input[name='hidCitytId']").val();
                    var gradeIdValue = trObject.find("input[name='hidGradeId']").val();
                    var pwdValue = trObject.find("input[name='hidPwd']").val();
                    var accountsEmailValue = trObject.find("input[name='hidAccountsEmail']").val();
                    //赋值
                    $('#txtNameView').val(consigneeValue);
                    $('#txtTelephone').val(telValue);
                    $('#txtEmail').val(emailValue);
                    $('#txtAddress').val(addressValue);
                    $('#txtPostId').val(postIdValue);
                    $('#txtAccountsGradeId').val(gradeIdValue);
                    var jasonUrl = "/area/areaselect.ashx?type=json";
                    $("select[id=selProvince] option[value='" + provinceIdValue + "']").attr("selected", true);
                    $("#selCity").FillOptions(jasonUrl + "&p=" + provinceIdValue, { datatype: "json", textfield: "areaname", valuefiled: "id" });

                    $("select[id=selCity] option[value='" + citytIdValue + "']").attr("selected", true);
                    $("#selArea").FillOptions(jasonUrl + "&c=" + citytIdValue, { datatype: "json", textfield: "areaname", valuefiled: "id" });

                    $("select[id=selArea] option[value='" + areaIdValue + "']").attr("selected", true);




                    $.ajax({ url: "/order/OrderLoginAccounts.aspx?email=" + accountsEmailValue + "&pwd=" + pwdValue + "&v=" + Math.random(),
                        type: 'GET',
                        dataType: 'html',
                        timeout: 10000,
                        error: function() {
                        },
                        success: function(html) {
                        }
                    }); //end of ajax



                    var storeId = $("#ddlStore").val();
                    $.ajax({ url: 'orderproducthtml.aspx?storeid=' + storeId,
                        type: 'GET',
                        dataType: 'html',
                        timeout: 10000,
                        error: function() {
                            alert("操作超时");
                        },
                        success: function(html) {
                            $('#NavCart').html(html);

                        }
                    });
                    GetExpressPrice();
                    $.modal.close();
                })
            }
        });
    });
    //订单筛选姓名 end


});

function AutoSelectCustomer(jsonData) {
    clearAccounts();
    //取值
    var consigneeValue = jsonData.consignee;
    var telValue = jsonData.telphone;
    var emailValue = jsonData.email;
    var addressValue = jsonData.address;
    var postIdValue = jsonData.postId;
    var areaIdValue = jsonData.AreaId;
    var provinceIdValue = jsonData.provinceId;
    var citytIdValue = jsonData.CityId;
    var gradeIdValue = jsonData.GradeId;
    var accountsEmailValue = jsonData.AccountsEmail;

    //赋值
    $('#txtNameView').val(consigneeValue);
    $('#txtTelephone').val(telValue);
    $('#txtEmail').val(emailValue);
    $('#txtAddress').val(addressValue);
    $('#txtPostId').val(postIdValue);
    $('#txtAccountsGradeId').val(gradeIdValue);
    var jasonUrl = "/area/areaselect.ashx?type=json";
    $("select[id=selProvince] option[value='" + provinceIdValue + "']").attr("selected", true);
    $("#selCity").FillOptions(jasonUrl + "&p=" + provinceIdValue, { datatype: "json", textfield: "areaname", valuefiled: "id" });

    $("select[id=selCity] option[value='" + citytIdValue + "']").attr("selected", true);
    $("#selArea").FillOptions(jasonUrl + "&c=" + citytIdValue, { datatype: "json", textfield: "areaname", valuefiled: "id" });

    $("select[id=selArea] option[value='" + areaIdValue + "']").attr("selected", true);




    $.ajax({ url: "/order/OrderLoginAccounts.aspx?email=" + accountsEmailValue + "&v=" + Math.random(),
        type: 'GET',
        dataType: 'html',
        timeout: 10000,
        error: function(error) {
        },
        success: function(html) {
            if (html == "") {
                $("#tableaccount").hide();
            } else {
                $("#tableaccount").show();
                var data = eval(html.toString())[0];
                $("#userid").val(data.UserID);
                $("#spanaccountsamount").text(data.Money);
                $("#accountsmoney").val(data.Money);
                if (data.IsBind == 1) {
                    $("#trcardinfo").show();
                    $("#trcard").hide();
                    $("#trcardpwd").hide();
                    $("#spanrechargecard").text(data.CardNo);
                } else {
                    $("#trcardinfo").hide();
                    $("#trcard").show();
                    $("#trcardpwd").show();
                    $("#spanrechargecard").text("");
                }

            }
        }
    });                           //end of ajax
    var storeId = $("#ddlStore").val();
    $.ajax({ url: 'orderproducthtml.aspx?storeid=' + storeId,
        type: 'GET',
        dataType: 'html',
        timeout: 10000,
        error: function() {
            alert("操作超时");
        },
        success: function(html) {
            $('#NavCart').html(html);

        }
    });
    GetExpressPrice();
}


function OpratorCart(productParamId, type) {
    var ajaxUrl;
    var storeId = $("#ddlStore").val();
    if (type == "1") {
        ajaxUrl = "orderproducthtml.aspx?product=0_1_" + productParamId + "&storeid=" + storeId + "&v=" + Math.random()
    }
    else if (type == "0") {
        ajaxUrl = "orderproducthtml.aspx?product=0_-1_" + productParamId + "&storeid=" + storeId + "&v=" + Math.random()
    }
    else if (type == "3") {
        ajaxUrl = 'orderproducthtml.aspx?del=' + productParamId + "&storeid=" + storeId + "&v=" + Math.random()
    }

    $.ajax({ url: ajaxUrl,
        type: 'GET',
        dataType: 'html',
        timeout: 10000,
        error: function() {
            $("div[name=navLoading]").css("display", "none");
            alert("操作超时");
        },
        success: function(html) {
            $("div[name=navLoading]").css("display", "none");
            $('#NavCart').html(html);
            GetExpressPrice();
        }
    }); //end of ajax
}

//帐户余额使用
$("#txtaccountpay").change(function() {
    var accountsmoney = 0;
    var accountsPay = 0;
    if ($(this).val() == "" || isNaN($(this).val())) {
        alert("金额请输入有效数字！");
        $(this).val(0);
        $(this).focus();
        $(this).select();
        GetExpressPrice();
        return false;
    }
    else
        accountsPay = parseFloat($(this).val());
    if (accountsPay == 0)
        return false;

    if ($("#accountsmoney").val() != "" && !isNaN($("#accountsmoney").val())) {
        accountmoney = parseFloat($("#accountsmoney").val());
    }
    else {
        alert("帐户发生错误，不能使用！");
        $(this).val(0);
        $(this).focus();
        $(this).select();
        GetExpressPrice();
        return false;
    }

    if (parseFloat($(this).val()) > parseFloat($("#accountsmoney").val())) {
        alert("使用金额不能大于帐户余额！");
        $(this).val(0);
        $(this).focus();
        $(this).select();
        GetExpressPrice();
        return false;
    }
    GetExpressPrice();
});

// 绑定充值卡
$("#btnRecharge").click(function() {
    if ($("#UserID").val() == "") {
        alert("没有识别客户，无法绑定！");
        return false;
    }
    if ($("#txtcardno").val() == "") {
        alert("请输入卡号！");
        $("#txtcardno").focus();
        return false;
    }
    if ($("#txtcardpwd").val() == "") {
        alert("请输入密码！");
        $("#txtcardpwd").focus();
        return false;
    }
    var cardNo = $("#txtcardno").val();
    var pwd = $("#txtcardpwd").val();
    var userid = $("#userid").val();

    $.ajax({
        url: "OrderHandler.ashx?Method=RechargeCardBind&CardNo=" + cardNo + "&CardPwd=" + pwd + "&UserId=" + userid,
        type: "GET",
        datatype: "html",
        success: function(data) {
            var temp = eval(data.toString())[0];

            if (temp.status == 0) {
                alert(temp.errmsg);
            }
            else {

                $("#spanaccountsamount").text(temp.Money);
                $("#accountsmoney").val(temp.Money);
                $("#trcardinfo").show();
                $("#trcard").hide();
                $("#trcardpwd").hide();
                $("#spanrechargecard").text(temp.CardNo);
            }
        },
        error: function(error) {
        }
    })
    return false;
});

//配送价格 start

function GetExpressPrice() {
    var expressMode = $("#selExpressMode").val();

    var offerPrice = parseFloat($("#txtOfferPrice").val()).toFixed(2);
    var orderPrice = 0.00;
    var expressPrice = 0.00;
    var payPrice = 0.00;
    var accountsPays = 0.00;
    var accountmoney = 0.00;
    if ($("#accountsmoney").val() != "" && !isNaN($("#accountsmoney").val())) {
        accountmoney = parseFloat($("#accountsmoney").val());
    }
    var payId = $("input:checked[name='radPay']").val();

    //系统自动计算配送价格
    if (expressMode == "0") {
        var expressId = $("input:checked[name='radExpress']").val();
        var areaId = $("#selArea").val();
        var storeId = $("#ddlStore").val();
        var getUrl = "/order/expressprice.aspx?"
        getUrl = getUrl + "storeid=" + storeId + "&express=" + expressId + "&pay=" + payId + "&area=" + areaId + "&address=0&v=" + Math.random();

        $.ajax({ url: getUrl,
            type: 'GET',
            dataType: 'html',
            timeout: 10000,
            async: false,
            error: function() {
                alert("操作超时");
            },
            success: function(html) {
                expressPrice = parseFloat(html.split('|')[0]).toFixed(2);
                //orderPrice = html.split('|')[1];
                if (expressPrice == -1) {
                    expressPrice = 0.00;
                }
            }
        });
    }
    //以输入价格为准
    else {
        expressPrice = parseFloat($("#txtExpressPrice").val()).toFixed(2);
    }

    orderPrice = parseFloat($("#txtProductTotalPrice").val()).toFixed(2);

    //使用礼券
    if (payId == 15) {
        //礼券支付
        $("#navCorpCouponsRemark").show();
        payPrice = parseFloat($("#txtCorpCouponsMoney").val());
    }
    else {
        $("#navCorpCouponsRemark").hide();
        payPrice = 0.00;
    }
    if ($("#txtaccountpay").val() != "" && !isNaN($("#txtaccountpay").val()))
        accountsPays = parseFloat($("#txtaccountpay").val());
    orderPrice = parseFloat(orderPrice) - parseFloat(offerPrice);
    orderPrice = parseFloat(orderPrice) + parseFloat(expressPrice);
    orderPrice = parseFloat(orderPrice) - parseFloat(payPrice);
    if (accountsPays > accountmoney)
        accountsPays = accountmoney;
    if (orderPrice - accountsPays < 0) {
        accountsPays = orderPrice;
    }
    $("#txtaccountpay").val(accountsPays < 0 ? 0 : accountsPays);
    orderPrice = orderPrice - accountsPays;
    if (orderPrice < 0)
        orderPrice = 0;
    expressPrice = parseFloat(expressPrice).toFixed(2);
    orderPrice = parseFloat(orderPrice).toFixed(2);
    $("#txtExpressPrice").val(expressPrice);
    $("#navOrderTotalPrice").html(orderPrice);
}

//配送价格 end


function clearAccounts() {
    $("#accountsmoney").val();
    $("#userid").val();
}


