KISSY.ready(function(S) {
var dom = S.DOM, event = S.Event;
var tr = S.all("#carttable tr.enable");
var alltr = S.all("#carttable tr");
var total1 = S.one("#total2");
var total2 = S.one(".total");
var totalnum = S.one("#totalnum");
var totaljs = S.one("#totaljs");
var checkall = S.one("#SelectAll");
var count = tr.length;
var selectcount = 0;
var gift = S.one("#gift");
if (S.all(".J_CheckBoxItem").length == 0) {
if (checkall != undefined) {
total1.html("0.00");
total2.html("0.00");
totalnum.html("0");
totaljs.html("0.00");
ChangeOrderBtn(false);
}
}
else {
ChangeNum();
}
if (checkall != undefined) {
checkall.on("click", function(ev) {
if (S.one(this).attr("checked") == undefined) { // 取消全选
S.all(".J_CheckBoxItem").each(function() {
S.one(this).attr("checked", false);
});
tr.removeClass("selected");
total1.html("0.00");
total2.html("0.00");
totalnum.html("0");
totaljs.html("0.00");
selectcount = 0;
ChangeOrderBtn(false);
} else { // 全选
S.all(".J_CheckBoxItem").each(function() {
S.one(this).attr("checked", true);
});
ChangeNum();
}
});
tr.each(function(o) {
var self = S.one(this);
if (!self.hasClass("gift")) {
var iid = self.attr('cartid');
var pid = self.attr('pid');
var check = this.one("input.J_CheckBoxItem");
var amount = this.one(".s-amount");
var quantity = this.one("input.text-amount");
var js = this.one("td.s-agio").one("em");
var subprice = this.one("td.s-total").one("em");
var dj = parseFloat(this.one("td.s-price").one("em").html());
var jsdj = parseFloat(parseFloat(js.html()) / parseInt(quantity.attr('data-now')));
//选择
check.on("click", function(ev) {
if (check.attr('checked') == undefined) {
self.removeClass("selected");
checkall.attr("checked", false);
ChangeNum();
}
else {
self.addClass("selected");
ChangeNum();
}
});
//数量改变
quantity.on("valuechange", function(e) {
if (!(/(^[1-9]\d*$)/.test(e.newVal))) {
quantity.val(e.prevVal);
}
else {
var now = parseInt(quantity.attr('data-now'));
var max = parseInt(quantity.attr('data-max'));
var value = parseInt(e.newVal, 10);
if (isNaN(value) || value <= 0) {
value = 1;
}
if (value != now) {
if (value > max) {
quantity.val(now);
if (self.one(".s-danger-text") != undefined) {
self.one(".s-danger-text").remove();
}
if (self.one(".error-msg") == undefined) {
amount.append("最多只可购买" + max + "件");
}
}
else {
S.io.post("/shoppingcart.aspx",
{
action: 'UpdateCartQuantity',
iid: iid,
num: value
}, function(d) {
if (d == "OK") {
js.html((jsdj * value).toFixed(2));
subprice.html((dj * value).toFixed(2));
if (check.attr('checked') != undefined) {
ChangeNum();
}
if (self.one(".error-msg") != undefined) {
self.one(".error-msg").remove();
}
if (max < 4) {
if (self.one(".s-danger-text") == undefined) {
amount.append("库存紧张");
}
}
if (now == 1) {
amount.one(".no-minus").replaceClass("no-minus", "minus");
}
else {
if (value == 1) {
amount.one(".minus").replaceClass("minus", "no-minus");
}
}
quantity.attr('data-now', value);
quantity.val(value);
}
else {
ShowMsg(false, "更新数量失败,请重试");
}
});
}
}
}
});
//数量
this.one(".plus").on("click", function() {
var now = parseInt(quantity.attr('data-now'));
var max = parseInt(quantity.attr('data-max'));
if (now < max) {
now = now + 1;
S.io.post("/shoppingcart.aspx",
{
action: 'UpdateCartQuantity',
iid: iid,
num: now
}, function(d) {
if (d == "OK") {
quantity.attr('data-now', now);
quantity.val(now);
js.html((jsdj * now).toFixed(2));
subprice.html((dj * now).toFixed(2));
if (check.attr('checked') != undefined) {
ChangeNum();
}
if (now == 2) {
amount.one(".no-minus").replaceClass("no-minus", "minus");
}
}
else {
ShowMsg(false, "更新数量失败,请重试");
}
});
}
else {
if (self.one(".s-danger-text") != undefined) {
self.one(".s-danger-text").remove();
}
if (self.one(".error-msg") == undefined) {
amount.append("最多只可购买" + max + "件");
}
}
});
//事件委托
amount.delegate('click', '.minus', function() {
var now = parseInt(quantity.attr('data-now'));
var max = parseInt(quantity.attr('data-max'));
if (now > 1) {
now = now - 1;
S.io.post("/shoppingcart.aspx",
{
action: 'UpdateCartQuantity',
iid: iid,
num: now
}, function(d) {
if (d == "OK") {
quantity.attr('data-now', now);
quantity.val(now);
js.html((jsdj * now).toFixed(2));
subprice.html((dj * now).toFixed(2));
if (check.attr('checked') != undefined) {
ChangeNum();
}
if (now == 1) {
amount.one(".minus").replaceClass("minus", "no-minus");
}
if (self.one(".error-msg") != undefined) {
self.one(".error-msg").remove();
}
if (max < 4) {
if (self.one(".s-danger-text") == undefined) {
amount.append("库存紧张");
}
}
}
else {
ShowMsg(false, "更新数量失败,请重试");
}
});
}
else {
ShowMsg(false, "操作失败,购买数量不能为0");
}
});
}
});
event.delegate(document, 'click', '.J_Del', function(e) {
var btn = S.one(e.currentTarget);
var tr1 = btn.parent(2);
var iid = tr1.attr('cartid');
var pid = tr1.attr('pid');
var title = tr1.one(".carttitle").html();
var dj = parseFloat(tr1.one("td.s-price").one("em").html());
if (confirm('您确定要删除 ' + title + ' 吗?')) {
KISSY.io.get("/shoppingcart.aspx",
{
action: 'RemoveCartById',
iid: iid
}, function(d) {
if (d == "OK") {
S.one(".mc-count").html(parseInt(S.one(".mc-count").html() - 1));
if (S.all("#carttable tr").length == 1) {
window.location = "/mycart.aspx";
}
else {
ShowMsg(true, "您已经成功删除了图书 " + title);
//改价格
tr1.remove();
ChangeNum();
if (selectcount == S.all("#carttable tr.enable").length) {
checkall.attr("checked", true);
}
}
}
else {
ShowMsg(false, "删除失败,请重试");
}
});
}
});
S.one(".del-items").on("click", function() {
if (S.all(".J_CheckBoxItem").length == 0) {
ShowMsg(false, "没有所选图书,请直接清除缺货图书");
}
else {
var str = "";
var count = 0;
S.all(".J_CheckBoxItem").each(function() {
if (S.one(this).attr("checked") != undefined) {
str += S.one(this).val() + ",";
count = count + 1;
}
});
if (count == 0) {
ShowMsg(false, "操作失败,请选择要删除的图书");
}
else {
if (confirm('您确定要删除所选图书吗?')) {
var iids = str.substring(0, str.length - 1);
S.io.post("/shoppingcart.aspx",
{
action: 'RemoveCarts',
ids: iids
}, function(d) {
if (d == "OK") {
if (count == parseInt(S.one(".mc-count").html())) {
window.location = "/mycart.aspx";
}
else {
total1.html("0.00");
total2.html("0.00");
totalnum.html("0");
totaljs.html("0.00");
selectcount = 0;
S.one(".mc-count").html(parseInt(S.one(".mc-count").html() - count));
iids = iids.split("\,");
KISSY.use("sizzle", function(S) {
for (var j = 0; j < iids.length; j++) {
var tr = S.one("tr[cartid=" + iids[j] + "]");
tr.remove();
}
});
ChangeOrderBtn(false);
ShowMsg(true, "您已经成功删除所选图书");
}
}
else {
ShowMsg(false, " 操作失败,请重试");
}
});
}
}
}
});
S.one(".del-all").on("click", function() {
if (confirm('您确定要清空购物车吗?')) {
S.io.post("/shoppingcart.aspx",
{
action: 'ClearShoppingCart'
}, function(d) {
if (d == "OK") {
window.location = "/mycart.aspx";
}
else {
ShowMsg(false, " 操作失败,请重试");
}
});
}
});
S.one(".del-invalid").on("click", function() {
if (S.all("#carttable tr.disable").length == 0) {
ShowMsg(false, " 操作失败,没有缺货图书");
}
else {
if (confirm('您确定要清除缺货图书吗!')) {
var str = "";
var count = 0;
S.all("#carttable tr.disable").each(function() {
str += S.one(this).attr("cartid") + ",";
count++;
});
var iids = str.substring(0, str.length - 1);
S.io.post("/shoppingcart.aspx",
{
action: 'RemoveCarts',
ids: iids
}, function(d) {
if (d == "OK") {
if (count == parseInt(S.one(".mc-count").html())) {
window.location = "/mycart.aspx";
}
else {
S.one(".mc-count").html(parseInt(S.one(".mc-count").html() - count));
iids = iids.split("\,");
S.all("#carttable tr.disable").each(function() {
S.one(this).remove();
});
ShowMsg(true, "您已经成功清除缺货图书");
}
}
else {
ShowMsg(false, " 操作失败,请重试");
}
});
}
}
});
}
});
function ChangeNum() {
var S = KISSY;
var total1 = S.one("#total2");
var total2 = S.one(".total");
var totalnum = S.one("#totalnum");
var totaljs = S.one("#totaljs");
var selectcount = 0;
var tprice = 0, tjs = 0, tnum = 0;
S.all("#carttable tr.enable").each(function() {
var self = S.one(this);
if (self.one(".J_CheckBoxItem") != undefined) {
if (self.one(".J_CheckBoxItem").attr("checked") != undefined) {
tprice += parseFloat(self.one(".s-total").one("em").html());
tnum += parseInt(self.one(".text-amount").attr('data-now'));
tjs += parseFloat(self.one(".s-agio").one("em").html());
self.addClass("selected");
selectcount = selectcount + 1;
}
else {
self.removeClass("selected");
}
}
});
if (selectcount == S.all(".J_CheckBoxItem").length) {
S.one("#SelectAll").attr("checked", true);
}
if (selectcount > 0) {
ChangeOrderBtn(true);
}
else {
ChangeOrderBtn(false);
}
total1.html(tprice.toFixed(2));
total2.html(tprice.toFixed(2));
totalnum.html(tnum);
totaljs.html(tjs.toFixed(2));
}
function ChangeOrderBtn(enable) {
var S = KISSY;
var btn = S.one("#submitorder");
if (enable) {
btn.replaceClass("btn-noorder", "btn-order");
btn.attr("href", "javascript:ConfirmOrder();");
}
else {
btn.replaceClass("btn-order", "btn-noorder");
btn.attr("href", "javascript:void(0);");
}
}
function ConfirmOrder() {
var S = KISSY;
if (S.all(".J_CheckBoxItem").length == 0) {
ShowMsg(false, "请先选择你要购买的图书");
}
else {
var str = "";
var count = 0;
S.all(".J_CheckBoxItem").each(function() {
if (S.one(this).attr("checked") != undefined) {
str += S.one(this).val() + ",";
count = count + 1;
}
});
if (count == 0) {
ShowMsg(false, "请先选择你要购买的图书");
}
else {
var iids = str.substring(0, str.length - 1);
S.io.post("/shoppingcart.aspx",
{
action: 'UpdateSelectCart',
ids: iids
}, function(d) {
if (d == "OK") {
window.location = "/confirmorder.aspx";
}
else {
ShowMsg(false, "订单提交失败,请重试");
}
});
}
}
}