﻿/*
购物车类

暴露的方法:
ShopCart.Clear();//清除购物车
ShopCart.AddProduct(productID, name, repaireSN, repairePID, count, ext1, ext2, ext3, func);//增加商品
ShopCart.LoadShopCartList();//直接使用默认的处理方法装载购物车
ShopCart.GetShopCartList(func);//使用自定义的处理方法异步装载购物车数据
ShopCart.ChangeItemNumber(itemID, num, giftRate, min, max);//更改购物车中的某一项商品的数目 num>0:增加num个数商品 num<0:减少num个数商品 ,giftRate,min,max三个参数用于控制商品最大个数等,如无业务需求可以不输入
ShopCart.SetItemNumber(itemID, num, giftRate, min, max);//设定购物车中的某一项商品的数目,此方法每500毫秒调用一次有效(如果itemID相同的话),连续调用只生效最后一个
ShopCart.DeleteItem(itemID, showUI);//删除购物车中的某一项 showUI表示是否提醒用户,默认为true,如果不希望弹出提示,传false


*/

function ShopCart() { }
if (ShopCart.ProductItems == null)//防止重复引用
{

	ShopCart.ProductItems = [];
	ShopCart.Timers = {};
	ShopCart.AsyncTimer1 = null;
	ShopCart.AsyncTimer2 = null;
	ShopCart.EventDisabled = false;
	ShopCart.Event = {};

	/*
	清空购物车
	*/
	ShopCart.Clear = function(showUI) {
		if (ShopCart.ProductItems.length == 0) {
			ShopCart.ShowMessage("您当前的购物车为空");
			return;
		}
		if (showUI == null || showUI == true) {
			if (window.confirm("请问您确实要清除购物车中的商品么?") != true) {
				return;
			}
		}
		var url = "/Ashx/ShopCart.ashx?ajaxMethod=clear";
		ShopCart.SendRequest(url, ShopCart.LoadShopCartList);
	}

	/*
	将商品加入到购物车
	*/
	ShopCart.AddProduct = function(productID, name, repaireSN, repairePID, count, ext1, ext2, ext3, func) {
		name = name == null ? "" : name;
		repaireSN = repaireSN == null ? "" : repaireSN;
		repairePID = repairePID == null ? "" : repairePID;
		count = count == null ? "1" : count;
		ext1 = ext1 == null || ext1 == "" ? productID : ext1;
		ext2 = ext2 == null ? "" : ext2;
		ext3 = ext3 == null ? "" : ext3;

		var url = "/Ashx/ShopCart.ashx?ajaxMethod=addproduct&productID=" + productID + "&productName=" + encodeURIComponent(name) + "&repaireSN=" + encodeURIComponent(repaireSN) + "&repairePID=" + encodeURIComponent(repairePID) + "&productCount=" + count + "&ext1=" + encodeURIComponent(ext1) + "&ext2=" + encodeURIComponent(ext2) + "&ext3=" + encodeURIComponent(ext3);
		var callback;
		if (func == null) {
			callback = function(data) {
				ShopCart.LoadShopCartList();
				if (data != null) {
					if (data.Message != null && data.Message != "") {
						ShopCart.ShowMessage(data.Message);
					}
					if (data.Code == 0) {
						if (document.getElementById("ShopCartContainer") != null)//如果当前页面已经是购物车,则无需跳转,直接装载购物车列表
						{
							return;
						}
						else {
							ShopCart.ShowSuccess(data);
						}
						//window.location = "/shopflow/Cart.aspx";
					}
				}
			}
		}
		else {
			if (func != ShopCart.LoadShopCartList) {
				callback = function(data) {
					func(data);
					ShopCart.LoadShopCartList();
				}
			}
			else {
				callback = ShopCart.LoadShopCartList;
			}
		}

		ShopCart.SendRequest(url, callback);
	}

	/*
	显示一个购买成功的浮动层
	*/
	ShopCart.ShowSuccess = function(data) {
		var url = window.location + "";
		var html = "<div class=\"buy_success\">成功加入购物车!<br />购物车中共有 <a href=\"/shopflow/Cart.aspx\" target=\"_blank\"><span class=\"bold color_red\">" + data.ProductCount + "</span></a> 件商品<br /><input name=\"\" onclick=\"bamClose();\" type=\"button\" value=\"继续购物\" class=\"btn_buy_success\" />&nbsp;&nbsp;&nbsp;&nbsp;<input onclick=\"window.location='/shopflow/Cart.aspx';\" name=\"\" type=\"button\" value=\"去结算\" class=\"btn_buy_success\" />";
		var nav = document.getElementById("NavProductCount");
		if (nav != null) {
			nav.innerHTML = data.ProductCount;
		}
		bamlog("添加成功", null, html, 300, 200);
	}
	/*
	显示自定义的消息
	*/
	ShopCart.ShowMessage = function(message) {
		var html = "<div class=\"buy_success\">" + message + "<br /><input name=\"\" onclick=\"bamClose();\" type=\"button\" value=\"确定\" class=\"btn_buy_success\" />";
		bamlog("Bambook消息", null, html, 300, 200);
	}

	/*
	装载并处理赠品列表
	*/
	ShopCart.LoadGiftList = function() {
		ShopCart.GetGiftList(ShopCart.ShowGiftList);
	}

	/*
	使用自定义的方法装载并处理赠品列表
	*/
	ShopCart.GetGiftList = function(func) {
		return ShopCart.SendRequest("/Ashx/ShopCart.ashx?ajaxMethod=getgiftlist", func);
	}

	/*
	显示赠品列表
	*/
	ShopCart.ShowGiftList = function(data) {
		var giftContainer = document.getElementById("GiftContainer");
		var giftTemplate = document.getElementById("GiftTemplate");
		var giftMessageTemplate = document.getElementById("GiftMessageTemplate").innerHTML;
		var giftButtonTemplateA = document.getElementById("GiftButtonTemplateA").innerHTML;
		var giftButtonTemplateB = document.getElementById("GiftButtonTemplateB").innerHTML;
		var giftHTML = new Array();
		for (var i = 0; i < data.length; i++) {
			var templateHTML = giftTemplate.innerHTML;
			var item = data[i];
			var requiredMoney = data[i].RequiredMoney;
			if (requiredMoney > 0) {
				templateHTML = templateHTML.replace(/\$GiftMessage/gi, giftMessageTemplate);
				templateHTML = templateHTML.replace(/\$GiftButton/gi, giftButtonTemplateB);
			}
			else {
				templateHTML = templateHTML.replace(/\$GiftMessage/gi, "");
				templateHTML = templateHTML.replace(/\$GiftButton/gi, giftButtonTemplateA);
			}
			templateHTML = templateHTML.replace(/\$MeetPrice/gi, item.MeetPrice.toFixed(2));
			templateHTML = templateHTML.replace(/\$MarketPrice/gi, item.MarketPrice.toFixed(2));
			templateHTML = templateHTML.replace(/\$Price/gi, item.Price.toFixed(2));
			templateHTML = templateHTML.replace(/\$MeetPrice/gi, item.MeetPrice.toFixed(2));
			templateHTML = templateHTML.replace(/\$RequiredMoney/gi, item.RequiredMoney.toFixed(2));
			templateHTML = templateHTML.replace(/\$GiftName/gi, item.GiftName);
			templateHTML = templateHTML.replace(/\$ProductID/gi, item.ProductID);
			giftHTML.push(templateHTML);
		}
		giftContainer.innerHTML = giftHTML.join("");
	}


	/*
	命令装载购物车数据
	使用ShopCart.ShowShopCartList方法处理异步请求获得的数据
	*/
	ShopCart.LoadShopCartList = function() {
		ShopCart.GetShopCartList(ShopCart.ShowShopCartList);
	}
	/*
	命令装载购物车数据
	如果有传递处理函数 则使用异步请求 当购物车数据加载时调用处理函数完成处理
	如果不传递处理函数 则使用非异步请求 等待处理完成并返回数据
	*/
	ShopCart.GetShopCartList = function(func) {
		return ShopCart.SendRequest("/Ashx/ShopCart.ashx?ajaxMethod=getshopcartlist", func);
	}
	/*
	开始连续更改购物车中的某项商品的数值
	所谓的连续更改是指用户点击购物车-+两个按钮不放约一段时间后对应选项的数量会不断增长直到最大值或者用户释放按键为止
	*/
	ShopCart.BeginChangeItemNumber = function(itemID, num, giftRate, min, max) {
		if (ShopCart.AsyncTimer1 != null) {
			window.clearTimeout(ShopCart.AsyncTimer1);
			ShopCart.AsyncTimer1 = null;
		}
		if (ShopCart.AsyncTimer2 != null) {
			window.clearInterval(ShopCart.AsyncTimer2);
			ShopCart.AsyncTimer2 = null;
		}
		var func = function() {
			ShopCart.AsyncTimer2 = window.setInterval(function() {
				ShopCart.ChangeItemNumberContinus(itemID, num, giftRate, min, max);
			}, 100);
		}
		ShopCart.AsyncTimer1 = window.setTimeout(func, 500);
	}
	/*
	停止连续更改购物车中的某项商品的数值
	*/
	ShopCart.EndChangeItemNumber = function() {
		if (ShopCart.AsyncTimer1 != null) {
			window.clearTimeout(ShopCart.AsyncTimer1);
			ShopCart.AsyncTimer1 = null;
		}
		if (ShopCart.AsyncTimer2 != null) {
			window.clearInterval(ShopCart.AsyncTimer2);
			ShopCart.AsyncTimer2 = null;
			ShopCart.EventDisabled = true;
		}
	}
	/*
	调整购物车中的某个商品的数目
	*/
	ShopCart.ChangeItemNumberContinus = function(itemID, num, giftRate, min, max) {
		var item = ShopCart.ProductItems["Item" + itemID];
		var productCount = document.getElementById("ProductCount" + itemID);
		if (productCount == null) {
			return;
		}
		var newNum = parseInt(productCount.value) + parseInt(num);
		if (newNum >= min && newNum <= max) {
			ShopCart.ChangeItemNumber(itemID, num, giftRate, min, max);
			if (newNum == min || newNum == max) {
				ShopCart.EndChangeItemNumber();
			}
		}
		else {
			ShopCart.EndChangeItemNumber();
		}
	}

	/*
	调整购物车中的某个商品的数目
	*/
	ShopCart.ChangeItemNumber = function(itemID, num, giftRate, min, max) {
		if (ShopCart.EventDisabled == true) {
			ShopCart.EventDisabled = false;
			return;
		}
		var item = ShopCart.ProductItems["Item" + itemID];
		var productCount = document.getElementById("ProductCount" + itemID);
		if (productCount == null) {
			return;
		}
		var newNum = parseInt(productCount.value) + parseInt(num);
		productCount.value = newNum;
		ShopCart.SetItemNumber(itemID, newNum, giftRate, min, max);
	}


	/*
	设定购物车中的某个商品的数目
	*/
	ShopCart.SetItemNumber = function(itemID, num, giftRate, min, max) {
		ShopCart.SetCanCheckOut(false);
		var productCount = document.getElementById("ProductCount" + itemID);
		if (!/^\d+$/.test(num)) {
			if (min != null && max != null) {
				ShopCart.ShowMessage("请输入一个" + min + "-" + max + "之间的数字");
			}
			else {
				ShopCart.ShowMessage("请输入一个数字");
			}
			if (productCount != null && productCount.value == num) {
				ShopCart.LoadShopCartList();
			}
			return;
		}
		var item = ShopCart.ProductItems["Item" + itemID];
		if (productCount != null) {
			var newNum = parseInt(num);
			if (min > 0 && newNum < min) {
				if (window.confirm(item.ProductName + "最少需要购买" + min + "件\n\n请问您是否需要删除此商品?") == true) {
					ShopCart.DeleteItem(itemID, false);
					return;
				}
				else {
					productCount.value = min;
					newNum = min;
				}
			}
			if (newNum > max && max > 0 && max > min) {
				newNum = max - (max % min);
				ShopCart.ShowMessage("您可以购买  " + item.ProductName + "  的数量最多为" + newNum + "件");
				num = newNum;
			}
			if (newNum % min != 0 && min > 0) {
				ShopCart.ShowMessage(item.ProductName + "必须每" + min + "件开始起卖");
				if (newNum > min) {
					newNum = newNum - (newNum % min);
				}
				else {
					newNum = min;
				}
				num = newNum;
			}
			productCount.value = newNum;
			if (ShopCart.Timers[itemID] == null) {
				ShopCart.Timers[itemID] = {};
			}
		}
		ShopCart.Timers[itemID].Number = newNum;
		if (ShopCart.Timers[itemID].TimerID != null) {
			window.clearTimeout(ShopCart.Timers[itemID].TimerID);
			ShopCart.Timers[itemID].TimerID = null;
		}
		//准备刷新购物车
		/*
		此处之所以这么设计是有可能用户短时间内连续点击加减按钮或修改 这么设计可以减少访问请求 提高性能
		*/
		ShopCart.Timers[itemID].TimerID = window.setTimeout(function() { ShopCart.PrepareShowShopCartList(itemID); }, 500);
	}
	/*
	*/
	ShopCart.PrepareShowShopCartList = function(itemID) {
		ShopCart.SendRequest("/Ashx/ShopCart.ashx?ajaxMethod=setitem&itemid=" + itemID + "&num=" + ShopCart.Timers[itemID].Number);
		ShopCart.LoadShopCartList();
	}
	/*
	删除购物车中的某个商品
	*/
	ShopCart.DeleteItem = function(itemID, showUI, productName) {
		ShopCart.SetCanCheckOut(false);
		var item = ShopCart.ProductItems["Item" + itemID];
		if (productName == null) {
			if (item != null) {
				productName = item.ProductName;
			}
		}
		if (productName == null) {
			productName = "";
		}
		if (showUI == null) {
			showUI = true;
		}
		if (showUI && window.confirm("确实要删除这个商品吗?\n\n" + productName) == false) {
			ShopCart.SetCanCheckOut(true);
			return false;
		}
		ShopCart.SendRequest("/Ashx/ShopCart.ashx?ajaxMethod=deleteitem&itemid=" + itemID, ShopCart.LoadShopCartList);
		return true;
	}

	/*
	使用优惠券
	*/
	ShopCart.UseDiscountTicket = function() {
		if (ShopCart.ProductItems.length == 0) {
			ShopCart.ShowMessage("请先将商品添加到购物车后再使用优惠券");
			return;
		}
		var input = document.getElementById("DiscountTicket");
		var ticket = input.value;
		if (ticket == "" || input.defaultValue == ticket) {
			ShopCart.ShowMessage("请输入优惠券号码");
			return;
		}
		ShopCart.SetCanCheckOut(false);
		ShopCart.SendRequest("/Ashx/ShopCart.ashx?ajaxMethod=usediscountticket&ticket=" + encodeURIComponent(ticket), function(data) {
			if (data.Code != 0) {
				if (data.Message != null) {
					ShopCart.ShowMessage(data.Message);
				}
			}
			ShopCart.LoadShopCartList();
		});
	}
	/*
	取消使用优惠券
	*/
	ShopCart.CancelDiscountTicket = function() {
		ShopCart.SendRequest("/Ashx/ShopCart.ashx?ajaxMethod=usediscountticket&ticket=", function() { });
		document.getElementById("ExistDiscountTicket").style.display = "none";
		document.getElementById("UseDiscountTicket").style.display = "inline";
		ShopCart.LoadShopCartList();
	}

	ShopCart.CheckOut = function() {
		if (ShopCart.CanCheckOut == true) {
			window.location = '/shopflow/CheckOut.aspx';
			return;
		}
		window.setTimeout(ShopCart.CheckOut, 200);
	}
	//设置是否可以立即结算
	ShopCart.SetCanCheckOut = function(can) {
		//window.status = can;
		ShopCart.CanCheckOut = can;
		var checkout = document.getElementById("CheckOut");
		if (checkout == null) {
			return;
		}
		if (can) {
			checkout.disabled = false;
			checkout.className = "pay_now";
			checkout.setAttribute("class", "pay_now");
		}
		else {
			checkout.disabled = true;
			checkout.className = "pay_now_gray";
			checkout.setAttribute("class", "pay_now_gray");
		}
	}

	/*
	呈现装载的购物车数据
	*/
	ShopCart.ShowShopCartList = function(cart) {
		var repaireServiceContainer = document.getElementById("CartRepaireServiceContainer");
		if (repaireServiceContainer != null) {
			repaireServiceContainer.style.display = "none";
		}
		window.CheckOutTimeout = 0;
		var products = cart.Products;
		//头部购物车载入
		var headerContainer = document.getElementById("HeaderCartList");
		if (headerContainer != null && window.HeaderCartTemplate != null) {
			var headerContents = [];
			for (var i = 0; i < products.length; i++) {
				var itemContent = window.HeaderCartTemplate;
				//逐次替换以便生成购物车呈现的HTML

				var item = products[i];
	            if (item.ProductID == 9) // 读书灯需要显示额外说明文字
	                item.ProductName = item.ProductName + "&nbsp;&nbsp;&nbsp;&nbsp;<span style='color:red;'>仅支持Bambook经典版使用</span>";

				//产品真实ID
				itemContent = itemContent.replace(/\$ProductID/gi, item.ProductID);
				//最多购买数目
				itemContent = itemContent.replace(/\$MaxNumber/gi, item.MaxNumber);
				//最多购买数目
				itemContent = itemContent.replace(/\$MinNumber/gi, item.MinNumber);
				//每项商品包含的设备数量
				itemContent = itemContent.replace(/\$GiftRate/gi, item.GiftRate);
				//单价
				itemContent = itemContent.replace(/\$Price/gi, item.Price.toFixed(2));
				//市场价
				itemContent = itemContent.replace(/\$MarketPrice/gi, item.MarketPrice.toFixed(2));
				//产品名称
				itemContent = itemContent.replace(/\$ProductNameEncoded/gi, encodeURIComponent(item.ProductName));
				itemContent = itemContent.replace(/\$ProductName/gi, item.ProductName);
				//外保设备ID
				itemContent = itemContent.replace(/\$RepairePID/gi, item.RepairePID);
				//外保设备SN
				itemContent = itemContent.replace(/\$RepaireSN/gi, item.RepaireSN);
				//购买此件商品的实际价格,例如打折后的价格
				itemContent = itemContent.replace(/\$ProductPrice/gi, item.ProductPrice.toFixed(2));
				//商品数目
				itemContent = itemContent.replace(/\$ProductCount/gi, item.ProductCount);
				//外保设备SN
				itemContent = itemContent.replace(/\$ItemID/gi, item.ID);


				//非任何包含扩展属性的商品可以显示链接
				if (item.RepaireSN == "" && item.RepairePID == "" && ("1,2,3,4,5".indexOf(item.PCategoryID) != -1)) {
					itemContent = itemContent.replace(/\$ProductLink/gi, "<a href=\"" + item.Templateaspx + "?id=" + item.ProductID + "\">" + item.ProductName + "</a>");
				}
				else {
					itemContent = itemContent.replace(/\$ProductLink/gi, "<span>" + item.ProductName + "</span>");
				}
				headerContents.push(itemContent);
			}
			headerContainer.innerHTML = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" + headerContents.join("") + "</table>";
		}
		var navCount = 0;
		for (var i = 0; i < products.length; i++) {
			var item = products[i];
			if (item != null && item.ProductID != null && item.ProductCount > 0) {
				navCount += item.ProductCount;
			}
		}
		var nav = document.getElementById("NavProductCount");
		if (nav != null) {
			nav.innerHTML = navCount;
		}

		//购物车载入
		var container = document.getElementById("ShopCartContainer");
		if (container == null) {
			return;
		}
		var checkout = document.getElementById("CheckOut");
		ShopCart.SetCanCheckOut(products.length > 0);
		//处理优惠券
		if (cart.TicketAmount > 0) {
			document.getElementById("UseDiscountTicket").style.display = "none";
			var existTicket = document.getElementById("ExistDiscountTicket");
			if (ShopCart.TicketTemplate == null) {
				ShopCart.TicketTemplate = existTicket.innerHTML;
			}
			var ticketHTML = ShopCart.TicketTemplate;
			ticketHTML = ticketHTML.replace(/\$TicketAmount/gi, cart.TicketAmount);
			ticketHTML = ticketHTML.replace(/\$TicketSN/gi, cart.TicketSN);
			existTicket.innerHTML = ticketHTML;
			existTicket.style.display = "inline";
		}
		else {
			document.getElementById("UseDiscountTicket").style.display = "inline";
			document.getElementById("ExistDiscountTicket").style.display = "none";
		}

		//显示赠品
		ShopCart.ShowGiftList(cart.Gifts);

		//处理购物车中的商品
		var cartEmpty = document.getElementById("NoItemsExists");
		cartEmpty.style.display = products.length > 0 ? "none" : "inline";
		if (ShopCart.HTMLTemplate == null) {
			ShopCart.HTMLTemplate = container.innerHTML;
		}
		var list = document.getElementById("ShopCartList");
		var templateA = "<tr>" + document.getElementById("ShopCartTemplate").getElementsByTagName("tr")[0].innerHTML + "</tr>";
		var templateB = "<tr>" + document.getElementById("BookPackageTemplate").getElementsByTagName("tr")[0].innerHTML + "</tr>";
		var content = new Array();
		ShopCart.ProductItems = products;
		var promotion = cart.Promotion;
		var sum = 0;
		for (var i = 0; i < products.length; i++) {
			var item = products[i];
			if (item != null && item.ProductID != null && item.ProductCount > 0) {
				sum += item.ProductCount * item.ProductPrice;
				ShopCart.ProductItems["Item" + item.ID] = item;
				var id = "Product" + item.ProductID + "_" + item.RepairePID + "_" + item.Ext1 + "_" + item.Ext2 + "_" + item.Ext3;
				id = id.replace("-", "_");
				ShopCart.ProductItems[id] = item;
			}
		}
		document.getElementById("ProductPrice").innerHTML = sum.toFixed(2);
		document.getElementById("DiscountPrice").innerHTML = cart.TicketAmount.toFixed(2);
		document.getElementById("PromotionPrice").innerHTML = promotion.toFixed(2);
		document.getElementById("JiFen").innerHTML = ((sum - cart.TicketAmount - promotion) * 2).toFixed(0);
		document.getElementById("TotalPrice").innerHTML = (sum - cart.TicketAmount - promotion).toFixed(2);
		for (var i = 0; i < products.length; i++) {
			var item = products[i];
			if (item != null && item.ProductID != null && item.ProductCount > 0) {

				var itemContent;
				if (item.PCategoryID == 8) {

					itemContent = templateB;
				}
				else {
					itemContent = templateA;
				}
				//逐次替换以便生成购物车呈现的HTML

				//产品真实ID
				itemContent = itemContent.replace(/\$ProductID/gi, item.ProductID);
				//包装信息
				var prefix = item.ShopcartPackageDescription == "" ? "" : "<span class=\"color_gray\">包装清单：</span>";
				itemContent = itemContent.replace(/\$ShopcartPackageDescription/gi, prefix + item.ShopcartPackageDescription.replace(/<[^>]+>/gi, ""));
				//最多购买数目
				itemContent = itemContent.replace(/\$MaxNumber/gi, item.MaxNumber);
				//最多购买数目
				itemContent = itemContent.replace(/\$MinNumber/gi, item.MinNumber);
				//每项商品包含的设备数量
				itemContent = itemContent.replace(/\$GiftRate/gi, item.GiftRate);
				//单价
				itemContent = itemContent.replace(/\$Price/gi, item.Price.toFixed(2));
				//市场价
				itemContent = itemContent.replace(/\$MarketPrice/gi, item.MarketPrice.toFixed(2));
				//产品名称
				itemContent = itemContent.replace(/\$ProductNameEncoded/gi, encodeURIComponent(item.ProductName));
				itemContent = itemContent.replace(/\$ProductName/gi, item.ProductName);
				//外保设备ID
				itemContent = itemContent.replace(/\$RepairePID/gi, item.RepairePID);
				//外保设备SN
				itemContent = itemContent.replace(/\$RepaireSN/gi, item.RepaireSN);
				//购买此件商品的实际价格,例如打折后的价格
				itemContent = itemContent.replace(/\$ProductPrice/gi, item.ProductPrice.toFixed(2));
				//商品数目
				itemContent = itemContent.replace(/\$ProductCount/gi, item.ProductCount);
				//外保设备SN
				itemContent = itemContent.replace(/\$ItemID/gi, item.ID);


				//非任何包含扩展属性的商品可以显示链接
				if (item.RepaireSN == "" && item.RepairePID == "" && ("1,2,3,4,5".indexOf(item.PCategoryID) != -1)) {
					itemContent = itemContent.replace(/\$ProductLink/gi, "<a href=\"" + item.Templateaspx + "?id=" + item.ProductID + "\">" + item.ProductName + "</a>");
				}
				else {
					itemContent = itemContent.replace(/\$ProductLink/gi, "<span>" + item.ProductName + "</span>");
				}
				//外保设备SN
				itemContent = itemContent.replace(/\$ItemID/gi, item.ID);
				//外保设备SN
				itemContent = itemContent.replace(/\$ItemPrice/gi, (item.ProductCount * item.ProductPrice).toFixed(2));
				content.push(itemContent);
			}
		}
		var htmlTemplate = ShopCart.HTMLTemplate;
		var hasDevice = false;
		var canAddRepaire = 0;
		var html = "";
		//检查意外保修服务
		for (var i = 0; i < products.length; i++) {
			var item = products[i];
			if (item != null && item.GiftRate > 0) {
				hasDevice = true;
				var id = "Product19_" + item.ProductID + "_" + item.Ext1 + "_" + item.Ext2 + "_" + item.Ext3;
				id = id.replace("-", "_");
				var repaireItem = ShopCart.ProductItems[id];
				if (repaireItem == null || repaireItem.ProductCount < item.GiftRate * item.ProductCount) {
					canAddRepaire++;
					//var id = "RepaireService" + item.ProductID;
					html += "<dd><input id='" + id + "' type=\"checkbox\" value=\"" + item.ProductID + "\" Ext1=\"" + item.Ext1 + "\" GiftRate=\"" + item.GiftRate + "\" checked/>&nbsp;<label for=\"" + id + "\">" + item.ProductName + "</label></dd>";
				}
			}
		}
		var repaireTemplate = "";
		if (canAddRepaire > 0 && repaireServiceContainer != null) {
			repaireServiceContainer.style.display = "block";
			if (canAddRepaire == 1) {
				document.getElementById("BuyRepaireService").onclick = function() {
					ShopCart.AddReapaireService("buy_one_msg");
				}
			}
			else {
				document.getElementById("BuyRepaireService").onclick = function() {
					bamlog('购买意外保修服务', 'buy_one_msg');
				}
			}
		}
		//htmlTemplate = htmlTemplate.replace(/\<\!\-\-RepaireContentInsertPoint\-\-\>/gi, repaireTemplate);
		var repaireServiceDialog = document.getElementById("buy_one_msg");
		var repaireHtml = ShopCart.ReapireDialogHTML;
		if (repaireHtml == null) {
			repaireHtml = repaireServiceDialog.innerHTML;
			ShopCart.ReapireDialogHTML = repaireHtml;
		}
		repaireHtml = repaireHtml.replace(/\$RepaireDeviceList/gi, html);
		var repaireMesg = "请将包含Bambook的商品添加到购物车,就可以在这里选择贴心的意外保修服务了";
		if (hasDevice == true) {
			if (canAddRepaire > 0) {
				repaireMesg = "请选择需要绑定服务的产品";
			}
			else {
				repaireMesg = "您已经为购物车中所有的Bambook购买了意外保修服务";
			}
		}
		repaireHtml = repaireHtml.replace(/\$RepaireMessage/gi, repaireMesg);
		repaireServiceDialog.innerHTML = repaireHtml;

		htmlTemplate = htmlTemplate.replace(/\<\!\-\-ProductListInsertPoint\-\-\>/gi, content.join(""));
		document.getElementById("LoadingShopCart").style.display = "none";
		container.innerHTML = htmlTemplate;


	}
	ShopCart.AddReapaireService = function(id) {
		if (id == null) id = "bamlog_content";
		var panel = document.getElementById("buy_one_msg");
		var checkboxes = document.getElementById(id).getElementsByTagName("input");
		var reload = false;
		for (var i = 0; i < checkboxes.length; i++) {
			var checkbox = checkboxes[i];
			if (/^checkbox$/i.test(checkbox.type) && checkbox.checked == true) {
				var ext1 = checkbox.getAttribute("Ext1");
				var count = checkbox.getAttribute("GiftRate");
				if (count == null || count == "") {
					count = 1;
				}
				var id = "Product" + checkbox.value + "__" + ext1 + "__";
				id = id.replace("-", "_");
				ShopCart.AddProduct(19, "一年意外保修(" + ShopCart.ProductItems[id].ProductName + ")", "", checkbox.value, count, ext1);
				reload = true;
			}
		}
		bamClose();
	}



	/*
	注册到浏览器装载页面以完成购物车的呈现
	*/
	$(document).ready(ShopCart.LoadShopCartList);



	ShopCart.SendRequest = function(url, onload) {
		var request = new ShopCartRequest();
		request.Post(url, "", onload);
	}


	/*
	HTTP请求类
	*/
	function ShopCartRequest() {
	}
	ShopCartRequest.prototype.Post = function(url, data, onload) {
		var async = false;
		var request;
		if (window.ActiveXObject) {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else if (window.XMLHttpRequest) {
			request = new XMLHttpRequest();
		}
		if (onload != null) {
			async = true;
		}
		request.open("POST", url, async);
		if (async == true) {
			request.onreadystatechange = function() {
				if (request.readyState == 4) {
					if (request.status == 200 || request.status == 0) {
						var json;
						try {
							json = eval("(" + request.responseText + ")");
						}
						catch (e) {
							json = null;
						}
						onload(json);
					}
				}
			}
		}
		request.send(data);
		if (async == false) {
			if (request.status == 200 || request.status == 0) {
				var json;
				try {
					json = eval("(" + request.responseText + ")");
				}
				catch (e) {
					json = null;
				}
				return json;
			}
		}
	}
}
