﻿function addCart(product, _count, _price, _pubtype) {

    doCart("savecart", product, _count, _price, _pubtype);

}


var updateCart = function(product, _price, _pubtype) {
    var _count = $("#input_count" + product + "_" + _pubtype).val();

    doCart("updatecart", product, _count, _price, _pubtype);
};


var doCart = function(param, product, _count, _price, _pubtype) {

    $.ajax({
        url: 'cart.ashx',
        type: 'GET',
        dataType: "json",
        data: { Param: param, pro: product, count: _count, price: _price, type: _pubtype },
        cache: false,
        async: false,
        success: function(json) {
            //alert(json);
            //loadHtml(json);
            checkCookieValue(json);


        }
    });

};



var delCart = function(product, _pubtype) {
    $.ajax({
        url: 'cart.ashx',
        type: 'GET',
        dataType: "json",
        data: { Param: "delcart", pro: product, type: _pubtype },
        cache: false,
        async: false,
        success: function(json) {
            //                        if (json != "0") {
            //                            loadHtml(json);
            //                            //alert("删除成功！");
            //                        }
            //                        else
            //                            loadHtml("");

            //                        }
            checkCookieValue(json);
        }
    });
};

var delMultiple = function(products) {
    $.ajax({
        url: 'cart.ashx',
        type: 'GET',
        dataType: "json",
        data: { Param: "delmultiple", pros: products },
        cache: false,

        success: function(json) {

            //loadHtml(json);
            checkCookieValue(json);
            //alert("删除成功！");

        }
    });

}

var delSelectItem = function() {

    var pros = "";
    var arr = $.makeArray($("input[name='itemChx'][checked]"));

    $.each(arr, function(i, n) {
        pros += n.value + "_";
    }
                );

    if (pros != "")
        delMultiple(pros);

}

//            var addCount = function(product, _price) {
//                var count = parseInt($("#input_count" + product).val()) + 1;

//                updateCart(product, count, _price);
//            };
//            var reduceCount = function(product, _price) {
//                var count = parseInt($("#input_count" + product).val()) - 1;
//                if (count > 0) {

//                    updateCart(product, count, _price);
//                }
//            };

var loadHtml = function(txt) {


    var total = 0.00;

    var _html = "";
    var count = 0;
    var _score = 0;

    $.each(txt,
                function(i, item) {

                    var _price = parseFloat(item.price);

                    var _count = parseFloat(item.count);
                    total += _price * _count;
                    var _style = i % 2 == 0 ? "" : "bg2";
                    //_html += "<dd  class='" + _style + "'><ul>";
                    _html += "<ul  class='" + _style + "'>";

                    _html += "<li><div class='a1'><input name='itemChx' value=" + item.proId + "_" + item.type + " type='checkbox'></div></li>";
                    if (item.type == '3')//果篮
                    {
                        _html += "<li><div class='a2'><img alt='" + item.proName + "' src='" + item.sysName + "' /></div></li>";
                    }
                    else if (item.type == '5')
                        _html += "<li><div class='a2'><img alt='" + item.proName + "' src='" + item.sysName + "' /></div></li>";
                    else
                        _html += "<li><div class='a2'><a target='_blank' href='product_detail_" + item.proId + ".html'><img alt='" + item.proName + "' src='" + item.sysName + "' /></a></div></li>";

                    _html += "<li><div class='a3'><h1>" + item.proName + "</h1></div></li>";
                    if (item.type != '5') {
                        _score += parseInt((_price * _count).toFixed(0));
                        _html += "<li><div class='a4'>" + (_price * _count).toFixed(0) + "</div> </li>";
                    }
                    else {
                        _html += "<li><div class='a4'>0</div> </li>";
                        _score += 0;
                    }

                    _html += "<li><div class='a6'>" + item.price.toFixed(2) + "</div></li>";
                    _html += "<li><div class='a7'>";
                    _html += "<div class='item_no'><input onblur='checkNum(this);'  onkeyup='checkNum(this);' onafterpaste='checkNum(this);' style='text-align:center' id='input_count" + item.proId + "_" + item.type + "' type='text' value='" + item.count + "' style='width:20px;'></div>";
                    _html += "<div class='item_no'><a href='javascript:updateCart(" + item.proId + "," + item.price + "," + item.type + ")'>确定</a></div></div></li>";
                    _html += "<li><div class='a8'>" + (_price * _count).toFixed(2) + "</div></li>";

                    _html += " <li><div class='a9'><a href='javascript:delCart(" + item.proId + "," + item.type + ");'>删 除</a></div></li>";

                    //_html += "</ul></dd> ";
                    _html += "</ul>";

                    count++;
                }
                );
    $("#itemScore").html(_score.toString());
    $("#cartItem").html(_html);
    //alert(_html)
    //document.getElementById("cartItem").innerHTML = _html;
    $("#sumPrice").html(total.toFixed(2));
    $("#itemcount").html(count);
    $("#sp_headerCartCount").html(count.toString());

};


var checkCookieValue = function(json) {

    if (json != "0" && json != "") {

        //                    $(".buy").show();
        //                    $("#cartItemTitle").show();
        //                    $("#cartItemBottom").show();
        $("#cart_left").show();
        $("#noItem").hide();

        loadHtml(json);
    }
    else {

        //                    $("#cartItemTitle").hide();
        //                    $("#cartItemBottom").hide();
        //                    $(".buy").hide();


        $("#noItem").show();

        $("#sp_headerCartCount").html("0");
        $("#cartItem").html("");
        //                    $("#cart_left").width(1000);
        //                    $("#cart_left").height(10000);
        //$("#cart_left").css({ width: "0px", height: "0px",display:"none" });
        //alert($("#cart_left").height())
        //$("#cart_left").height(0);
        //$("#cart_left").css({ width: "0px", height: "0px", display/**/: "none" });
        $("#cart_left").hide();
        // document.getElementById("cart_left").className = "disn";
    }



};

var getCartList = function() {
    $.ajax({
        url: 'cart.ashx',
        type: 'GET',
        dataType: "json",
        data: { Param: "getcart" },
        cache: false,
        success: function(json) {
            checkCookieValue(json);
        }
    });



};
var selAllChx = function() {
    if ($("input[name='chx_all']").attr("checked") == true)
        $("input[name*='itemChx']").attr("checked", true);
    else
        $("input[name*='itemChx']").attr("checked", false);
}


$(document).ready(
            function() {
                getCartList();

            }
         );


function checkNum(obj) {
    if (obj.value != "" && obj.value != "0")
        obj.value = obj.value.replace(/\D/g, '');
    else
        obj.value = "1";
}
            
             
