function addCart(Param, ModelId, ColId, Id) {
    var number = 1;
    var guige = "";
    var other = "";
    var value = XmlHttpPostMethodText("" + Param + "/user/shop/addCart.aspx", "ModelId=" + ModelId + "&ColId=" + ColId + "&Id=" + Id + "&Number=" + number + "&Guige=" + guige + "&Other=" + other + "");
    if (value.length == 0) {
        return;
    }
    else {
        switch (value.split("$")[0]) {
            case "101":
                alert(value.split("$")[1]);
                document.location.href = value.split("$")[2];
                break;
            case "200":
                alert(value.split("$")[1]);
                document.location.href = value.split("$")[2];
                break;
            case "210":
                alert(value.split("$")[1]);
                break;
            case "220":
                document.location.href = value.split("$")[2];
                break;
            case "230":
                break;
        }
    }
}

function qsaddCart(Param, ModelId, ColId, Id) {
    var number = 1;
    var guige = "";
    var other = "";
    var value = XmlHttpPostMethodText("" + Param + "/user/shop/addCart.aspx", "ModelId=" + ModelId + "&ColId=" + ColId + "&Id=" + Id + "&Number=" + number + "&Guige=" + guige + "&Other=" + other + "");
    if (value.length == 0) {
        return;
    }
    else {
        document.location.href = "" + Param + "/user/shop/ShoppingCheckOut.aspx";
    }
}

function updatecart(obj, a) {
    var id = obj.id.split("_")[0] + "_" + obj.id.split("_")[1] + "_" + obj.id.split("_")[2] + "_";
    var n = DianCMS_$(id + "txtNumber").value;
    var price = Number(DianCMS_$(id + "txtrealprice").value);

    switch (a) {
        case 1:
            if (Number(n) > 1) {
                n = Number(n) - 1;
            }
            break;
        case 0:
            break;
        case 2:
            n = Number(n) + 1;
            break;
    }

    DianCMS_$(id + "txtNumber").value = n;
    DianCMS_$(id + "xiaoji").innerText = "￥" + formatCurrency(n * price);

    tatalprice();
    editcart();
}

function tatalprice() {
    var tn = DianCMS_$("shopCart1_txtTatalNumber").value;
    var tprice = 0;
    for (var i = 1; i <= Number(tn) ; i++) {
        if (i < 10) {
            tprice += Number(DianCMS_$("shopCart1_RepCart_ctl0" + i + "_txtNumber").value) * Number(DianCMS_$("shopCart1_RepCart_ctl0" + i + "_txtrealprice").value);
        }
        else {
            tprice += Number(DianCMS_$("shopCart1_RepCart_ctl" + i + "_txtNumber").value) * Number(DianCMS_$("shopCart1_RepCart_ctl" + i + "_txtrealprice").value);
        }
    }

    DianCMS_$("labtatal").innerText = "￥" + formatCurrency(tprice);
}

function editcart() {
    var s = "";
    var tn = DianCMS_$("shopCart1_txtTatalNumber").value;
    for (var i = 1; i <= Number(tn) ; i++) {
        if (i < 10) {
            s += DianCMS_$("shopCart1_RepCart_ctl0" + i + "_txtCartId").value + "=" + DianCMS_$("shopCart1_RepCart_ctl0" + i + "_txtNumber").value + "=,";
        }
        else {
            s += DianCMS_$("shopCart1_RepCart_ctl" + i + "_txtCartId").value + "=" + DianCMS_$("shopCart1_RepCart_ctl" + i + "_txtNumber").value + "=,";
        }
    }
    D.Shop.BLL.B_Common.updatecartajax(s, callback);
}

function del(id) {
    if (confirm("确认删除该商品吗？")) {
        D.Shop.BLL.B_Common.delcartajax(id, callback);
    }
}

function setRealPrice(cbj, name) {
    var n = eval("Div" + name);
    if (cbj.id == name + "3") {
        n.style.display = "";
    }
    else {
        n.style.display = "none";
    }
}

function Submit() {
    if (DianCMS_$("shoppingCheckOut1_DropProvince").value == "0") {
        DianCMS_$("shoppingCheckOut1_LabExistsArea").innerHTML = "<font color=red>请选择省份</font>";
        return false;
    }
    if (DianCMS_$("shoppingCheckOut1_txtConsignee").value.length == 0) {
        DianCMS_$("shoppingCheckOut1_LabExistsConsignee").innerHTML = "<font color='red'>请输入收货人姓名</font>";
        DianCMS_$("shoppingCheckOut1_txtConsignee").focus();
        return false;
    }

    if (DianCMS_$("shoppingCheckOut1_txtAddress").value.length == 0) {
        DianCMS_$("shoppingCheckOut1_LabExistsAddress").innerHTML = "<font color='red'>请输入详细地址</font>";
        DianCMS_$("shoppingCheckOut1_txtAddress").focus();
        return false;
    }

    if (DianCMS_$("shoppingCheckOut1_txtTel").value.length == 0) {
        DianCMS_$("shoppingCheckOut1_LabExistsTel").innerHTML = "<font color='red'>请输入联系电话</font>";
        DianCMS_$("shoppingCheckOut1_txtTel").focus();
        return false;
    }
}

function callback(res) {
    var _v = res.value.split("☂");
    if (_v[1] == "del") {
        alert(_v[2]);
        window.location.reload();
    }
}

/**
 * 将数值四舍五入(保留2位小数)后格式化成金额形式
 *
 * @param num 数值(Number或者String)
 * @return 金额格式的字符串,如'1,234,567.45'
 * @type String
 */
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3) ; i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
        num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + num + '.' + cents);
}

/*商品规格*/
$(function () {
    $('.guigecheck').click(function () {
        var param = $('#guigeparam').val();
        var guigecontent = $('#txtSpecificationsContent').val();
        var guigeStr = "";
        $('input[name="guige"]:checked').each(function () {
            var sfruit = $(this).val();
            //$(this).checked();
            guigeStr += sfruit + "$";
        });
        $('#txtSpecificationsId').val(guigeStr);

        $.get('' + param + '/user/shop/ajaxguige.aspx', { "guige": guigeStr, "guigecontent": guigecontent }, function (data, status) {
            if (status == "success") {
                $('#SpanSpecificationsList').html(data);
                changeGuiGeValue();
            }
        });

    });
});


function changeGuiGeValue() {
    var idStr = DianCMS_$("txtSpecificationsIdStr").value;
    var content = "";
    for (var i = 0; i < idStr.split("☂").length; i++) {
        var huohao = DianCMS_$(idStr.split("☂")[i] + "_huohao").value;
        var price1 = DianCMS_$(idStr.split("☂")[i] + "_price1").value;
        var price2 = DianCMS_$(idStr.split("☂")[i] + "_price2").value;
        var price3 = DianCMS_$(idStr.split("☂")[i] + "_price3").value;
        var kucun = DianCMS_$(idStr.split("☂")[i] + "_kucun").value;
        var kg = DianCMS_$(idStr.split("☂")[i] + "_kg").value;

        content += idStr.split("☂")[i] + "☀" + huohao + "☀" + price1 + "☀" + price2 + "☀" + price3 + "☀" + kucun + "☀" + kg + "☂";
    }
   
    DianCMS_$("txtSpecificationsContent").value = content;
}