﻿		
		dwr.util.useLoadingMessage();
		
		//定义商品类型
		var typenew = 1;//新品
		var typeused = 2;//二手
		var typepresell = 3;//预售
		
		//当前商品数量（用于购物车修改商品数量）
		var currentNumber = 0;
		
		//来自API的商品数据
		var apiProduct = null;
		
		//商品列表
		var productList = new Array();
		
		//二手商品页minicart缓存数组
		var popupMiniCartList = new Array();
		
		//amazon的url加密
		function encrypt(str) {
			return jmallJQ.base64.encode(str).replace("/", "----").replace("+", " ");
		}

		//amazon的url解密
		function decrypt(str) {
			return jmallJQ.base64.decode(str.replace(" ", "+").replace("----", "/"));
		}
		
		//去掉价格字符串里的多余字符，并转换为整数
		function replacePrice(price) {
			price = String(price);
			price = price.replace("￥", "");
			price = price.replace(" ", "");
			price = price.replace(/,/g, "");
			price = price.replace("+", "");
			return isNaN(price) ? 0 : parseInt(price);
		}
		
		function getProduct() {
			var product = {
				"id" : "",
				"name" : "",
				"price" : 0,
				"shipcharge" : 0,
				"number" : 0,
				"seller" : "",
				"remark" : "",
				"type" : "",
				"condition" : "",
				"isbn" : "",
				"pid" : "",
				"sorttext" : "",
				"discnum" : "",
				"contentRating" : "",
				"link" : "",
				"handlingInfo" : null,
				"presetWeight" : null,
				"merchantID" : null,
				"quantityLimit" : 1
			};
			return product;
		}
		
		//把商品列表里的数字字符串转换为整数
		function formatProductList() {
			for (var i = 0; i < productList.length; i++) {
				productList[i].price = parseInt(productList[i].price);
				productList[i].shipcharge = parseInt(productList[i].shipcharge);
				productList[i].number = parseInt(productList[i].number);
			}
		}
		
		//获取当前数量（用于购物车修改商品数量）
		function getCurrentNumber(id) {
			var number = jmallJQ("#productNumber" + id).val();
			if (!isNaN(number) && number.length > 0 && number != "0") {
				number = parseInt(number);
				if (number > 0) {
					currentNumber = number;
				}
			}
		}
		
		//输入amazon页面后跳转
		function getLinkResult() {
			var link = jmallJQ("#jmall_amazonAddress").val();
			if (link.length == 0) {
				alert("请输入您要购买的AMAZON页面链接");
				return;
			}
			if (link.indexOf("http://www.amazon.co.jp") < 0) {
				alert("请输入正确的链接");
				return;
			}
			link = encrypt(link);
			window.location.href = contextPath + "/amazon/" + link + ".htm";
		}
		
		//点击“购买该页产品”按钮
		function checkBuy() {
			if (jmallJQ("#linkResult").html().length > 0) {
				if (jmallJQ("#btAsinTitle").text().length > 0 && jmallJQ("table[class=offerlistings]").length == 0) {
					if (isNeedToGoOffersPage()) {
						gotoUsedPage();
					} else {
						buy();
					}
				} else {
					alert("不是商品详细页");
					return;
				}
			} else {
				alert("请输入要购买的网址");
			}
		}
		
		function isNeedToGoOffersPage() {
			if (apiProduct != null) {
				// merchant is not amazon
				if (apiProduct.merchantID != null && apiProduct.merchantID != "AN1VRQENFRJN5") {
					// shipcharge is 0
					if (getShippingChargeForMiniCart() == 0) {
						return true;
					}
				}
			}
			return false;
		}
		
		//点击“加入购物车”按钮，适合商品详细页
		function buy() {
			var remark = "";
			var remarkObj = checkStock();
			if (jmallJQ("#jsSwatches").html() != null) {
				var error = false;
				var remark = "";
				jmallJQ(".variationSelected").each(function () {
					var text = jmallJQ(this).text();
					text = text.substring(text.indexOf(":") + 1).replace(" ", "");
					if (text.length == 0) {
						error = true;
						return false;
					} else {
						remark = remark + "[" + jmallJQ(this).text() + "]";
					}
				});
				if (error) {
					alert("请先定制商品");
					return;
				}
				remark = remark + "。" + remarkObj.remark;
			} else {
				remark = remarkObj.remark;
			}
			if (remarkObj.type.length == 0) {
				alert("该商品缺货中");
				return;
			}
			
			var name = jmallJQ("#btAsinTitle").text().replace(/\"/g, "\'");
			
			var price = jmallJQ("#priceBlock").find(".priceLarge").html();
			if (price == null) {
				if (jmallJQ(".priceLarge").parents("#buyboxusedDivId").length == 0) {
					price = jmallJQ(".priceLarge").html();
				}
			}
			if (price == null) {
				alert("该商品缺货中");
				return;
			}
			if (price.indexOf("-") >= 0) {
				alert("请先定制商品");
				return;
			}
			price = replacePrice(price);
			
			var number = jmallJQ("#quantity").val();
			if (number == null) {
				number = 1;
			}

			var isbnAndDiscNum = getIsbnAndDiscNum();
			
			var isbn = isbnAndDiscNum.isbn;

			var discnum = isbnAndDiscNum.discnum;
			
			var asin = getAsin();
			
			var product = getProduct();
			product.id = "";
			product.name = name;
			product.price = price;
			product.shipcharge = 0;
			product.number = parseInt(number);
			product.seller = "AMAZON";
			product.remark = remark;
			product.type = remarkObj.type;
			product.condition = "新品";
			product.isbn = isbn;
			product.pid = asin;
			product.sorttext = getSortText();
			product.discnum = discnum;
			product.contentRating = getContentRating();
			product.quantityLimit = getQuantityLimit();
			product.link = getAmazonDetailLink(asin);
			product.merchantID = getMerchantID();
			
			var stockHtml = getStock();
			if (stockHtml != null) {
				var bhtml = jmallJQ(stockHtml).parent().find("b");
				var sellerhtml = bhtml[bhtml.length - 1]
				if (jmallJQ(sellerhtml).text() != "Amazon.co.jp") {
					product.seller = jmallJQ(sellerhtml).text();
				}
			}
			
			product = getShippingCharge(product);
			
			if (apiProduct != null) {
				// get data from api
				product.name = apiProduct.name;
				product.price = apiProduct.price;
				product.isbn = apiProduct.isbn;
				product.discnum = apiProduct.discnum;
				product.contentRating = apiProduct.contentRating;
				product.quantityLimit = apiProduct.quantityLimit;
				product.sorttext = apiProduct.sorttext;
				product.seller = apiProduct.seller;
				product.condition = apiProduct.condition;
			}
			checkProductList(product);
		}
		
		//保存商品，更新商品列表
		function checkProductList(product) {
			Buy.addProduct(product, function(data) {
				if (Message_Library[data].type == "success") {
					Buy.findCart(function(data) {
						show(data);
					});
				}
				alert(Message_Library[data].content);
			});
		}
		
		//清空购物车的商品显示
		function clearCart() {
			jmallJQ("#cartTable").html("<thead><tr><td><input type='checkbox' name='allidx' value='all' onclick='checkSelect(this);'><a href='javascript:;' class='jmall_delthis' onclick='deleteProduct();'>删除</a></td><td>产品名</td><td>产品价格</td><td>手续费</td><td>日本国内运费</td><td>数量</td><td>小计</td></tr></thead>");
			jmallJQ("#showNumber").html("0");
			jmallJQ(".totalPrice").each(function() {
				this.innerHTML = "0";
			});
		}
		
		//确认库存
		function checkStock() {
			var remark = {
				"type" : "",
				"shippingDate" : "",
				"remark" : ""
			};
			
			jmallJQ(".availGreen").each(function() {
				if (jmallJQ(this).parent().attr("class") == "buying") {
					remark.remark = this.innerHTML;
					remark.type = typenew;
					return false;
				}
			});
			if (remark.remark.length > 0) {
				return remark;
			}
			
			jmallJQ(".availOrange").each(function() {
				if (jmallJQ(this).parent().attr("class") == "buying") {
					remark.remark = this.innerHTML;
					remark.type = typepresell;
					return false;
				}
			});
			
			return remark;
		}
		
		//获取ISBN-10和光碟数目
		function getIsbnAndDiscNum() {
			var isbn = "";
			var discnum = 0;
			var isbnFound = false;
			var discnumFound = false;
			jmallJQ(jmallJQ("a#productDetails").nextAll("table").get(0)).find("b").each(function() {
				if (!isbnFound) {
					if (jmallJQ(this).html().indexOf("ISBN-10") >= 0) {
						isbn = jmallJQ(this).parent().text();
						isbn = isbn.replace(jmallJQ(this).html(), "");
						isbnFound = true;
					}
				}
				if (!discnumFound) {
					if (jmallJQ(this).html().indexOf("光碟数目") >= 0
							|| jmallJQ(this).html().indexOf("Number of discs") >= 0
							|| jmallJQ(this).html().indexOf("ディスク枚数") >= 0) {
						discnum = jmallJQ(this).parent().text();
						discnum = parseInt(jmallJQ.trim(discnum.replace(jmallJQ(this).html(), "")));
						discnumFound = !isNaN(discnum);
					}
				}
				return !(isbnFound && discnumFound);
			});
			return {"isbn" : jmallJQ.trim(isbn), "discnum" : isNaN(discnum) ? 0 : discnum};
		}

		//获取ASIN
		function getAsin() {
			return jmallJQ("#ASIN").val();
		}
		
		//获取卖家ID
		function getMerchantID() {
			return jmallJQ("#merchantID").val();
		}

		//获取商品分类
		function getSortText() {
			var sortText = "";
			jmallJQ("#navCategoryBtn").find("a").find("span").each(function() {
				sortText = jmallJQ(this).html();
			});
			if (sortText == "") {
				jmallJQ("div[class=navCatSpc]").find("a[class=navCatA]").each(function() {
					sortText = jmallJQ(this).html();
				});
			}
			return sortText;
		}

		function getContentRating() {
			var contentRating = 0;
			jmallJQ("#adultWarning").each(function() {
				contentRating = 1;
				return false;
			});
			return contentRating;
		}
		
		function getQuantityLimit() {
			var quantityLimit = 1;
			if (jmallJQ("input[name=maap_bb_qty_limit]").length > 0) {
				quantityLimit = jmallJQ("input[name=maap_bb_qty_limit]").val();
			} else if (jmallJQ("#quantity").length > 0) {
				quantityLimit = jmallJQ("#quantity").children("option").length;
			}
			if (quantityLimit <= 0) {
				quantityLimit = 1;
			}
			return quantityLimit;
		}

		function getAmazonDetailLink(asin) {
			var link = null;
			if (asin != null && asin != "" && (jmallJQ("#jsSwatches").length > 0)) {
				link = "http://www.amazon.co.jp/o/ASIN/" + asin;
			} else {
				link = jmallJQ("#jmall_amazonAddress").val();
			}
			return link;
		}
		
		//获取卖家信息
		function getStock() {
			var stockHtml;
			var flag = false;
			
			jmallJQ(".availGreen").each(function() {
				if (jmallJQ(this).parent().attr("class") == "buying") {
					stockHtml = this;
					flag = true;
					return false;
				}
			});
			if (flag) {
				return stockHtml;
			}
			
			jmallJQ(".availOrange").each(function() {
				if (jmallJQ(this).parent().attr("class") == "buying") {
					stockHtml = this;
					flag = true;
					return false;
				}
			});
			if (flag) {
				return stockHtml;
			}
			
			return null;
		}
		
		//更新购物车商品序号
		function updateSequence() {
			var i = 1;
			jmallJQ("#cartTable").find(".productSeq").each(function() {
				this.innerHTML = i;
				i++;
			});
		}
		
		//删除购物车商品
		function deleteProduct() {
			var products = [];
			jmallJQ("#cartTable").find("input[type=checkbox]").each(function() {
				if (jmallJQ(this).attr("checked") && this.value != "all") {
					products.push(productList[this.value].id);
				}
			});
			if(products.length>0)
			Buy.removeProducts(products, function(data) {
				show(data);
			});
		}
		
		//获取日本国内快递
		function getShippingCharge(product) {
			var shippingText = jmallJQ("#buyboxTable").find(".plusShippingText");
			if (shippingText.length <= 0) {
				shippingText = jmallJQ("#buyboxDivId").find(".plusShippingText");
			}
			if (shippingText.length > 0) {
				var strs = jmallJQ(shippingText[0]).html().split(" ");
				for (var i=0; i < strs.length ; i++) {
					var str = strs[i].substring(0, strs[i].indexOf("&"));
					str = replacePrice(str);
					if (String(str).length > 0 && !isNaN(str)) {
						product.shipcharge = parseInt(str);
						break;
					}
				}
			}
			return product;
		}

		function getShippingChargeForMiniCart() {
			var shipcharge = 0;
			var shippingText = jmallJQ("#buyboxTable").find(".plusShippingText");
			if (shippingText.length <= 0) {
				shippingText = jmallJQ("#buyboxDivId").find(".plusShippingText");
			}
			if (shippingText.length > 0) {
				var strs = jmallJQ(shippingText[0]).html().split(" ");
				for (var i=0; i < strs.length ; i++) {
					var str = strs[i].substring(0, strs[i].indexOf("&"));
					str = replacePrice(str);
					if (String(str).length > 0 && !isNaN(str)) {
						shipcharge = parseInt(str);
						break;
					}
				}
			}
			return shipcharge;
		}
		
		//显示购物车
		function showCart() {
			clearCart();

			var cartnum = 0; 
			for(var pi=0;pi<productList.length;pi++) {
				cartnum += productList[pi].number;
			}
			jmallJQ("#showNumber").html(cartnum);
		}
		
		//点击购物车全选框
		function checkSelect(checkbox) {
			jmallJQ("#cartTable").find("input[type=checkbox]").each(function() {
				jmallJQ(this).attr("checked", jmallJQ(checkbox).attr("checked"));
			});
		}

		// 获取二手商品页商品对应详细页链接
		function getDetailLink() {
			var link = jmallJQ("td.productdetails").find("span.caretback").next().attr("href");
			if (link == null || link.length == 0) {
				jmallJQ("td[class=productdetails]").find("a").each(function() {
					link = jmallJQ(this).attr("href");
					return false;
				});
			}
			return link;
		}
		
		// 获取商品详细页上的二手商品页链接
		function getUsedLink() {
			var link = null;
			if (apiProduct != null) {
				if (apiProduct.allOffersLink != null && apiProduct.allOffersLink.length > 0) {
					link = contextPath + "/amazon/" + encrypt(apiProduct.allOffersLink) + ".htm";
				}
			}
			if (link == null || link.length == 0) {
				link = jmallJQ("#olpDivId").find(".olpCondLink").find("a").attr("href");
			}
			if (link == null || link.length == 0) {
				link = jmallJQ("#secondaryUsedAndNew").find("a.buyAction").attr("href");
			}
			return link;
		}
		
		function gotoUsedPage() {
			var link = getUsedLink();
			if (link != null || link.length > 0) {
				window.open(link);
			}
		}
		
		//点击购买二手商品
		function buySecondhandByAmazonBtn(n) {
			var i = 1;
			jmallJQ("tbody[class=result]").each(function() {
				if (i == n) {
					var productName = jmallJQ(".producttitle").html();
					if (productName == null || productName.length == 0) {
						productName = jmallJQ("#btAsinTitle").text();
					}
					productName = productName.replace(/\"/g, "\'");
					var type = typeused;
					var sellerName = "";
					var merchantID = undefined;
				
					var child = jmallJQ(this).find(".sellerInformation").children();
					if (child[0].nodeName == "IMG" && jmallJQ(child[0]).attr("alt") == "Amazon.co.jp") {
						type = typenew;
						sellerName = "AMAZON";
					} else if (child[0].nodeName == "A") {
						sellerName = jmallJQ(child[0]).find("img").attr("alt");
						merchantID = jmallJQ(child[0]).attr("href").replace(contextPath + "/amazon/", "").replace(".htm", "");
						if (merchantID != null && merchantID.length > 0) {
							merchantID = decrypt(merchantID);
						}
						var merchantIDArr = merchantID.split("/");
						merchantID = merchantIDArr[jmallJQ.inArray("shops", merchantIDArr) + 1];
					} else {
						sellerName = jmallJQ(this).find(".seller").find("b").html();
						merchantID = jmallJQ(this).find(".seller").find("a").attr("href").replace(contextPath + "/amazon/", "").replace(".htm", "");
						if (merchantID != null && merchantID.length > 0) {
							merchantID = decrypt(merchantID);
						}
						merchantID = merchantID.substring(merchantID.indexOf("seller=") + "seller=".length);
					}
					
					var price = jmallJQ(this).find(".price").html();
					price = replacePrice(price);
					var refPrice = jmallJQ("td[class=listprice]").html();
					if (refPrice != null) {
						refPrice = replacePrice(refPrice);
					} else {
						refPrice = 0;
					}
					
					var priceShipping = jmallJQ(this).find(".price_shipping").html();
					if (priceShipping != null) {
						priceShipping = replacePrice(priceShipping);
					} else {
						priceShipping = 0;
					}
					
					var condition = jmallJQ(this).find(".condition").html();
					
					var remark = jmallJQ(this).find(".availability").text();
					remark = remark.substring(0, remark.indexOf("。"));
					remark = remark.replace("発送について:", "");
					
					var product = getProduct();
					product.name = productName;
					product.price = price;
					product.refPrice = refPrice;
					product.shipcharge = priceShipping;
					product.number = 1;
					product.seller = sellerName;
					product.pid = getAsin();
					product.link = jmallJQ("#jmall_amazonAddress").val();
					product.remark = remark;
					product.condition = condition;
					product.merchantID = merchantID;
					
					if (sellerName == "AMAZON") {
						if (remark.indexOf("在庫") >= 0 || remark.indexOf("有库存") >= 0) {
							product.type = typenew;
						} else {
							product.type = typepresell;
						}
					} else {
						product.type = typeused;
					}
					
					var detailLink = getDetailLink();
					Buy.getUsedProduct(detailLink, product, function(data) {
						checkProductList(data);
					});
					
					return false;
				}
				i++;
			});
		}
		
		//点击组合商品加入购物车
		function addBothToCart() {
			var product = getProduct();
			product.id = "";
			product.name = "";
			product.price = 0;
			product.shipcharge = 0;
			product.number = 1;
			product.seller = "AMAZON";
			product.remark = "";
			product.type = typenew;
			product.condition = "新品";
			product.isbn = "";
			product.pid = "";
			product.sorttext = "";
			product.discnum = "";
			product.contentRating = "";
			product.link = "";
			
			jmallJQ("p[class=bxgy-text]").each(function() {
				jmallJQ(this).find("a").each(function() {
					product.name = jmallJQ(this).html().replace(/\"/g, "\'");
					product.link = jmallJQ(this).attr("href");
					return false;
				});
				return false;
			});
			Buy.getProduct(product.link, product, function(data) {
				data.price = replacePrice(data.price);
				data.shipcharge = replacePrice(data.shipcharge);
				data.number = parseInt(data.number);
				data.discnum = parseInt(data.discnum);
				Buy.addProduct(data, function(data) {
					if (Message_Library[data].type == "success") {
						checkBuy();
					}
				});
			});
		}
		
		function addCartByServer(html) {
			var product = getProduct();
			product.id = "";
			product.name = "";
			product.price = 0;
			product.shipcharge = 0;
			product.number = 1;
			product.seller = "AMAZON";
			product.remark = "";
			product.type = typenew;
			product.condition = "新品";
			product.isbn = "";
			product.pid = "";
			product.sorttext = "";
			product.discnum = "";
			product.contentRating = "";
			product.link = "";
			
			product.name = jmallJQ(html).find("a")[1].innerHTML.replace(/\"/g, "\'");
			product.link = jmallJQ(jmallJQ(html).find("a")[1]).attr("href");
			Buy.getProduct(product.link, product, function(data) {
				data.price = replacePrice(data.price);
				data.shipcharge = replacePrice(data.shipcharge);
				data.number = parseInt(data.number);
				data.discnum = parseInt(data.discnum);
				checkProductList(data);
			});
		}
		
		function addCartByServer2() {
			var product = getProduct();
			product.id = "";
			product.name = "";
			product.price = 0;
			product.shipcharge = 0;
			product.number = 1;
			product.seller = "AMAZON";
			product.remark = "";
			product.type = typenew;
			product.condition = "新品";
			product.isbn = "";
			product.pid = "";
			product.sorttext = "";
			product.discnum = "";
			product.contentRating = "";
			product.link = "";
			
			jmallJQ("div[class=cBoxInner]").find("div[class=description]").each(function() {
				jmallJQ(this).find("a").each(function() {
					product.name = jmallJQ(this).html().replace(/\"/g, "\'");
					product.link = jmallJQ(this).attr("href")
					return false;
				});
				if (product.link.length > 0) {
					Buy.getProduct(product.link, product, function(data) {
						data.price = replacePrice(data.price);
						data.shipcharge = replacePrice(data.shipcharge);
						data.number = parseInt(data.number);
						data.discnum = parseInt(data.discnum);
						checkProductList(data);
					});
				}
			});
		}
		
		function addCartByServer3(n) {
			var product = getProduct();
			product.id = "";
			product.name = "";
			product.price = 0;
			product.shipcharge = 0;
			product.number = 1;
			product.seller = "AMAZON";
			product.remark = "";
			product.type = typenew;
			product.condition = "新品";
			product.isbn = "";
			product.pid = "";
			product.sorttext = "";
			product.discnum = "";
			product.contentRating = "";
			product.link = "";
			
			var i = 1;
			jmallJQ("span[id=lm_asinlink95]").each(function() {
				if (i == n) {
					jmallJQ(this).find("a").each(function() {
						product.name = jmallJQ(this).html().replace(/\"/g, "\'");
						product.link = jmallJQ(this).attr("href")
						return false;
					});
				}
				i++;
			});
			if (product.link.length > 0) {
				Buy.getProduct(product.link, product, function(data) {
					data.price = replacePrice(data.price);
					data.shipcharge = replacePrice(data.shipcharge);
					data.number = parseInt(data.number);
					data.discnum = parseInt(data.discnum);
					checkProductList(data);
				});
			}
		}
		
		function addCartByServer4(n) {
			var product = getProduct();
			product.id =  "";
			product.name =  "";
			product.price =  0;
			product.shipcharge =  0;
			product.number =  1;
			product.seller =  "AMAZON";
			product.remark =  "";
			product.type =  typenew;
			product.condition =  "新品";
			product.isbn =  "";
			product.pid =  "";
			product.sorttext =  "";
			product.discnum =  "";
			product.contentRating =  "";
			product.link =  "";
			
			var i = 1;
			jmallJQ("tr[class=small]").each(function() {
				if (i == n) {
					var j = 1;
					jmallJQ(this).find("td").each(function() {
						if (j == 2) {
							jmallJQ(this).find("a").each(function() {
								product.name = jmallJQ(this).html().replace(/\"/g, "\'");
								product.link = jmallJQ(this).attr("href")
								return false;
							});
						}
						j++;
					});
				}
				i++;
			});
			if (product.link.length > 0) {
				Buy.getProduct(product.link, product, function(data) {
					data.price = replacePrice(data.price);
					data.shipcharge = replacePrice(data.shipcharge);
					data.number = parseInt(data.number);
					data.discnum = parseInt(data.discnum);
					checkProductList(data);
				});
			}
		}
		
		function addCartByServer5(n) {
			var product = getProduct();
			product.id =  "";
			product.name =  "";
			product.price =  0;
			product.shipcharge =  0;
			product.number =  1;
			product.seller =  "AMAZON";
			product.remark =  "";
			product.type =  typenew;
			product.condition =  "新品";
			product.isbn =  "";
			product.pid =  "";
			product.sorttext =  "";
			product.discnum =  "";
			product.contentRating =  "";
			product.link =  "";
			
			var i = 1;
			jmallJQ("#seriesList").find("tr").each(function() {
				if (i == n) {
					jmallJQ(this).find("h3").find("a").each(function() {
						product.name = jmallJQ(this).html().replace(/\"/g, "\'");
						product.link = jmallJQ(this).attr("href")
						return false;
					});
				}
				i++;
			});
			if (product.link.length > 0) {
				Buy.getProduct(product.link, product, function(data) {
					data.price = replacePrice(data.price);
					data.shipcharge = replacePrice(data.shipcharge);
					data.number = parseInt(data.number);
					data.discnum = parseInt(data.discnum);
					checkProductList(data);
				});
			}
		}
		
		function addCartByServer6() {
			var product = getProduct();
			product.id =  "";
			product.name =  "";
			product.price =  0;
			product.shipcharge =  0;
			product.number =  1;
			product.seller =  "AMAZON";
			product.remark =  "";
			product.type =  typenew;
			product.condition =  "新品";
			product.isbn =  "";
			product.pid =  "";
			product.sorttext =  "";
			product.discnum =  "";
			product.contentRating =  "";
			product.link =  "";
			
			jmallJQ(jmallJQ("div[class=simExplorer]").find("table")[0]).find("strong[class=sans]").find("a").each(function() {
				product.name = jmallJQ(this).html().replace(/\"/g, "\'");
				product.link = jmallJQ(this).attr("href")
				return false;
			});
			if (product.link.length > 0) {
				Buy.getProduct(product.link, product, function(data) {
					data.price = replacePrice(data.price);
					data.shipcharge = replacePrice(data.shipcharge);
					data.number = parseInt(data.number);
					data.discnum = parseInt(data.discnum);
					checkProductList(data);
				});
			}
		}
		
		//替换按钮
		function replaceButton() {
			jmallJQ("iframe").each(function() {
				if (jmallJQ(this).attr("src") && jmallJQ(this).attr("src").indexOf("/gp/advertising/iframeproxy") >= 0) {
					jmallJQ(this).parent().remove();
				}
			});
			var pattern = /^cachebust_[\d]*?_adcontent$/;
			jmallJQ("#rightcol").children("div").each(function() {
				if (pattern.test(jmallJQ(this).attr("id"))) {
					jmallJQ(this).remove();
				}
			});
			jmallJQ("#rightcolatfhidden").remove();
			
			jmallJQ("#navCartButton").find("a").each(function() {
				jmallJQ(this).attr("href", contextPath + "/amazon/cart.htm");
				jmallJQ(this).attr("target", "_blank");
			});
			
			jmallJQ("#navWishListButton").find("a").each(function() {
				jmallJQ(this).attr("href", "javascript:;");
			});
			
			jmallJQ("#buyboxDivId").find("div[class=oneClickDiv]").each(function() {
				jmallJQ(this).remove();
			});
			
			jmallJQ("#buyboxusedTable").find("div[class=oneClickDiv]").each(function() {
				jmallJQ(this).remove();
			});
			
			jmallJQ("input[type=image][class=dpSprite s_addBothToCart button-margin]").each(function() {
				var html = "<a class='dpSprite s_addBothToCart button-margin' href='javascript:;' onclick='addBothToCart();' title='加入购物车' style='width:154px;height:22px;'><img border='0' src='" + jmallJQ(this).attr("src") + "'></a>";
				jmallJQ(this).after(html);
				jmallJQ(this).remove();
			});
			
			jmallJQ("#buyboxDivId").find("input[type=image]").each(function() {
				if (jmallJQ(this).attr("name") == "submit.add-to-cart") {
					var html = "<a class='dpSprite s_bbAdd2Cart' href='javascript:;' onclick='checkBuy();' title='加入购物车' style='width:180px;height:27px;'><img border='0' src='" + jmallJQ(this).attr("src") + "'></a>";
					jmallJQ(this).parent().after(html);
					jmallJQ(this).parent().remove();
				} else {
					jmallJQ(this).remove();
				}
			});
			
			// goto Used Page on detail page
			jmallJQ("#buyboxusedTable").find("input[type=image]").each(function() {
				if (jmallJQ(this).attr("name") == "submit.add-to-cart-ubb") {
					var html = "<a class='dpSprite s_bbAdd2Cart' href='javascript:;' onclick='gotoUsedPage();' title='加入购物车' style='width:180px;height:27px;'><img border='0' src='" + jmallJQ(this).attr("src") + "'></a>";
					jmallJQ(this).parent().after(html);
					jmallJQ(this).parent().remove();
				} else {
					jmallJQ(this).remove();
				}
			});
			
			var tbodyNum = 1;
			jmallJQ("tbody[class=result]").each(function() {
				jmallJQ(this).find("input[type=image]").each(function() {
					this.setAttribute("onclick", "buySecondhandByAmazonBtn(" + tbodyNum + ");");
					var html = "<a href='javascript:;' onclick='buySecondhandByAmazonBtn(" + tbodyNum + ");' title='加入购物车'><img border='0' src='" + jmallJQ(this).attr("src") + "'></a>";
					jmallJQ(this).after(html);
					jmallJQ(this).remove();
				});
				jmallJQ(this).children("tr").children("td:first").mouseenter(function(event) {
					var index = jmallJQ.inArray(jmallJQ(event.currentTarget).parent().parent()[0], jmallJQ("tbody[class=result]"));
					showPopupMiniCart(index, event);
				});
				jmallJQ(this).children("tr").children("td:first").mousemove(function(event) {
					var index = jmallJQ.inArray(jmallJQ(event.currentTarget).parent().parent()[0], jmallJQ("tbody[class=result]"));
					movePopupMiniCart(index, event);
				});
				tbodyNum++;
			});
			
			jmallJQ("#cartViewForm").parent().remove();
			
			jmallJQ("#accessories-main-item").find("a[class=select-this-item]").each(function() {
				jmallJQ(this).attr("href", "javascript:;");
				this.setAttribute("onclick", "checkBuy();");
			});
			
			jmallJQ("div[class=shoveler-cell]").each(function() {
				jmallJQ(this).find("a[class=cartbutton]").each(function() {
					var html = "<a href='javascript:;' onclick='addCartByServer(this.parentNode);' title='加入购物车'>" + jmallJQ(this).html() + "</a>";
					jmallJQ(this).after(html);
					jmallJQ(this).remove();
				});
			});
			
			jmallJQ("div[class=accessory-item]").each(function() {
				jmallJQ(this).find("a[class=select-this-item]").each(function() {
					var html = "<a href='javascript:;' onclick='addCartByServer(this.parentNode);' title='加入购物车'>" + jmallJQ(this).html() + "</a>";
					jmallJQ(this).after(html);
					jmallJQ(this).remove();
				});
			});
			
			var i = 1;
			jmallJQ("div[class=buttonsBlock]").each(function() {
				jmallJQ(this).find("a").each(function() {
					if (i == 1) {
						var html = "<a href='javascript:;' onclick='addCartByServer2();' title='加入购物车'>" + this.innerHTML + "</a>";
						jmallJQ(this).after(html);
						jmallJQ(this).remove();
					} else {
						jmallJQ(this).remove();
					}
					i++;
				});
			});
			
			i = 1;
			jmallJQ(".listItem").each(function() {
				if (jmallJQ(this).find("#lm_asinlink95").length > 0) {
					jmallJQ(this).find("input[type=image][value=addToCart]").each(function() {
						var html = "<a href='javascript:;' onclick='addCartByServer3(" + i + ");' title='加入购物车'><img border='0' src='" + jmallJQ(this).attr("src") + "'></a>";
						jmallJQ(this).after(html);
						jmallJQ(this).remove();
					});
					i++;
				}
			});
			
			jmallJQ(".listItem").find("input[type=image][value=addToRegistry]").each(function() {
				jmallJQ(this).remove();
			});
			
			i = 1;
			jmallJQ("table[class=amabot_widget]").find("tr[class=small]").each(function() {
				jmallJQ(this).find("input[type=image]").each(function() {
					if (jmallJQ(this).attr("name") == "submit.add-to-cart") {
						var html = "<a href='javascript:;' onclick='addCartByServer4(" + (i - 1) + ");' title='加入购物车'><img border='0' src='" + jmallJQ(this).attr("src") + "'></a>";
						jmallJQ(this).after(html);
						jmallJQ(this).remove();
					} else {
						jmallJQ(this).remove();
					}
				});
				i++;
			});
			
			i = 1;
			jmallJQ("#seriesList").find("tr").each(function() {
				jmallJQ(this).find("input[type=image]").each(function() {
					if (jmallJQ(this).attr("name") == "submit.addToCart") {
						var html = "<a href='javascript:;' onclick='addCartByServer5(" + i + ");' title='加入购物车'><img border='0' src='" + jmallJQ(this).attr("src") + "'></a>";
						jmallJQ(this).after(html);
						jmallJQ(this).remove();
					} else {
						jmallJQ(this).remove();
					}
				});
				i++;
			});
			
			//i = 1;
			jmallJQ(".s_add2CartSm").each(function() {
				jmallJQ(this).attr("href", jmallJQ(jmallJQ("#secondaryUsedAndNew").find("a")[0]).attr("href"));
				jmallJQ(this).attr("target", "_blank");
				//jmallJQ(this).attr("id", "extendedBuyBox" + i);
				//jmallJQ(this).attr("onclick", "extendedBuy('extendedBuyBox" + i + "');");
				var a = jmallJQ(this).clone(true);
				jmallJQ(this).after(a);
				jmallJQ(this).remove();
				//i++;
			});
			
			jmallJQ("div[class=simExplorer]").find("table").find("div[class=buyButton]").each(function() {
				jmallJQ(this).find("input[type=image]").each(function() {
					if (jmallJQ(this).attr("name") == "submit.addToCart") {
						var html = "<a href='javascript:;' onclick='addCartByServer6();' title='加入购物车'><img border='0' src='" + jmallJQ(this).attr("src") + "'></a>";
						jmallJQ(this).after(html);
						jmallJQ(this).remove();
					} else {
						jmallJQ(this).remove();
					}
				});
			});
			
			jmallJQ("#SDPBlock").remove();
			
			jmallJQ("#navidWelcomeMsg").find("span[class=navMessage]").each(function() {
				jmallJQ(this).remove();
			});
			
			jmallJQ("#accessories").next().remove();
			jmallJQ("#accessories").remove();
			
			jmallJQ("#main").find("form").each(function() {
				jmallJQ(this).attr("action", jmallJQ(this).attr("action").replace("/amazon/", "/pcgame/"));
			});
			
			jmallJQ("#tpianbstrip").find("a").each(function() {
				jmallJQ(this).attr("onmouseover", "");
			});

			jmallJQ('a.dpSprite').each(function(){
				jmallJQ(this).css("background-image", "url(" + contextPath + "/images/Amazon_addtoCart.gif)");
			});

			// replace secondhand page sort dropdown option value
			var amazonUrl = "http://www.amazon.co.jp";
			jmallJQ("select[id=olp_sort_dropdown][name=menu_options]").children("option").each(function() {
				var sortLink = jmallJQ(this).val();
				if (sortLink.toLowerCase().indexOf(amazonUrl) < 0) {
					sortLink = amazonUrl + sortLink;
				}
				jmallJQ(this).val(contextPath + "/amazon/" + encrypt(sortLink) + ".htm");
			});

			// replace search page sort dropdown event
			jmallJQ("select[id=sort][name=sort][class=sortByDropdown]").removeAttr("onchange");
			jmallJQ("select[id=sort][name=sort][class=sortByDropdown]").change(function(event){
				var form = jmallJQ(this.form);
				var link = amazonUrl + decrypt(form.attr("action").replace(contextPath + "/amazon/", "").replace(".htm", "")) + "?";
				form.children("input[type=hidden]").each(function(){
					link += jmallJQ(this).attr("name") + "=" + jmallJQ(this).val() + "&";
				});
				link += jmallJQ(this).attr("name") + "=" + jmallJQ(this).children("option:selected").val();
				link = contextPath + "/amazon/" + encrypt(encodeURI(link)) + ".htm";
				window.location.href = link;
			});

			// bind minicart fresh event to jsSwatches
			jmallJQ("#jsSwatches").find("div[class=swatchOuter]").each(function(){
				jmallJQ(this).click(function(event){
					showMiniCart();
				});
				jmallJQ(this).keypress(function(event){
					if (event.keyCode == '13') {
						showMiniCart();
					}
				});
			});
			
			// get asin on secondHand page by YQL
			var asin = getAsin();
			if (asin == null || asin.length == 0) {
				var detailLink = getDetailLink();
				if (detailLink != null && detailLink.length != 0) {
					detailLink = amazonUrl + decrypt(detailLink.replace(contextPath + "/amazon/", "").replace(".htm", ""));
					var yqlUrl = createYQLQueryURL(detailLink, "//input[@id=\"ASIN\"]", "insertAsinOnPage");
					jmallJQ("body").append("<script type=\"text/javascript\" src=\"" + yqlUrl + "\"></script>");
				}
			}
		}
		
		function createYQLQueryURL(link, htmlXpath, callbackName) {
			var yqlUrl = null;
			var yql = "select * from html where url='" + link + "'";
			if (htmlXpath != null && htmlXpath.length != 0) {
				yql += " and xpath='" + htmlXpath + "'";
			}
			yqlUrl = "http://query.yahooapis.com/v1/public/yql?q=" + encodeURIComponent(yql) + "&format=json&callback=" + callbackName;
			return yqlUrl;
		}
		
		function insertAsinOnPage(yqlData) {
			var asin = yqlData.query.results.input.value;
			var inputObj = "<input type='hidden' id='ASIN' name='ASIN' value='" + asin + "'>";
			jmallJQ("body").append(inputObj);
			createPopupMiniCart();
		}
		
		//点击去结算
		function gosubmit() {
			if (jmallJQ("#logouturl").attr("id") == undefined) {
				window.location.href = jmallJQ("#loginurl").attr("href");
				return;
			}
			Buy.findCart(function(data) {
				productList = data;
				formatProductList();
				if (productList.length == 0) {
					alert("购物车内还没有商品");
					return;
				}
				jmallJQ("#mallForm").html(createMallInput(productList));
				jmallJQ("#mallForm").submit();
			});
		}
		
		function show(data) {
			if (data != null && data.length > 0) {
				productList = data;
				formatProductList();
				showCart();
			} else {
				productList = new Array();
				clearCart();
			}
		}
		
		function calculateNationFreight(productWeight) {
			if (productWeight == 0) {
				return;
			}
			var stateId = jmallJQ("select[name=miniCartState]").val();
			var paymentId = jmallJQ("select[name=miniCartPayment]").val();
			if (stateId == 0 || paymentId == 0) {
				jmallJQ("#miniCartNationFreight").html("0");
				jmallJQ("#miniCartTotal").html(jmallJQ("#miniCartTotalWithoutNation").val().toString());
				return;
			}
			var map = {
					"stateId" : stateId,
					"paymentId" : paymentId,
					"productWeight" : productWeight
			};
			var asyncFlag = dwr.engine._async;
			if(!asyncFlag){
				dwr.engine.setAsync(true);
			}
			Buy.calculateNationFreight(map, function(data){
				var miniCartTotal = parseInt(jmallJQ("#miniCartTotalWithoutNation").val()) + parseInt(data);
				jmallJQ("#miniCartNationFreight").html(data);
				jmallJQ("#miniCartTotal").html(miniCartTotal.toString());
			});
			if(!asyncFlag) {
				dwr.engine.setAsync(false);
			}
		}

		function showMiniCart() {
			var miniCartHtml = "<div class='cBox grayBox'>";
			miniCartHtml = miniCartHtml + "<span class='cBoxTL'><!-- &nbsp; --></span>";
			miniCartHtml = miniCartHtml + "<span class='cBoxTR'><!-- &nbsp; --></span>";
			miniCartHtml = miniCartHtml + "<span class='cBoxR'><!-- &nbsp; --></span>";
			miniCartHtml = miniCartHtml + "<span class='cBoxBL'><!-- &nbsp; --></span>";
			miniCartHtml = miniCartHtml + "<span class='cBoxBR'><!-- &nbsp; --></span>";
			miniCartHtml = miniCartHtml + "<span class='cBoxB'><!-- &nbsp; --></span>";
			miniCartHtml = miniCartHtml + "<div id='miniCartInner' class='cBoxInner'>";

			var innerHtmlMask = "";
			innerHtmlMask = innerHtmlMask + "<img src='" + contextPath + "/images/ajax-loader.gif'/>";
			innerHtmlMask = innerHtmlMask + "<span style='font-size: 12pt; font-style: italic;'>请稍候......</span>";

			var errorFleg = false;
			product = getProduct();
			var price = jmallJQ("#priceBlock").find(".priceLarge").html();
			if (price == null) {
				if (jmallJQ(".priceLarge").parents("#buyboxusedDivId").length == 0) {
					price = jmallJQ(".priceLarge").html();
				}
			}
			if (price == null) {
				innerHtmlMask = "该商品缺货中";
				errorFleg = true;
			} else if (price.indexOf("-") >= 0) {
				innerHtmlMask = "请先定制商品";
				errorFleg = true;
			} else {
				price = replacePrice(price);
				var remarkObj = checkStock();
				if (remarkObj.type.length == 0) {
					innerHtmlMask = "该商品缺货中";
					errorFleg = true;
				} else {
					var isbnAndDiscNum = getIsbnAndDiscNum();
					var asin = getAsin();
					product.name = jmallJQ("#btAsinTitle").text().replace(/\"/g, "\'");
					product.price = price;
					product.shipcharge = getShippingChargeForMiniCart();
					product.number = 1;
					product.seller = "AMAZON";
					product.type = remarkObj.type;
					product.condition = "新品";
					product.isbn = isbnAndDiscNum.isbn;
					product.pid = asin;
					product.sorttext = getSortText();
					product.discnum = isbnAndDiscNum.discnum;
					product.contentRating = getContentRating();
					product.quantityLimit = getQuantityLimit();
					product.link = getAmazonDetailLink(asin);
					product.merchantID = getMerchantID();
				}
			}
			miniCartHtml = miniCartHtml + "<ul>";
			miniCartHtml = miniCartHtml + "<li><span class='cartTitle'>萌购团送</span></li>";
			miniCartHtml = miniCartHtml + "</ul>";
			miniCartHtml = miniCartHtml + innerHtmlMask;
			miniCartHtml = miniCartHtml + "</div>";
			miniCartHtml = miniCartHtml + "</div>";
			jmallJQ("#handleBuy").children("table:last").children("tbody").children("tr:nth-child(8)").children("td").html(miniCartHtml);
			
			if (errorFleg) {
				Buy.isFavourite(getAsin(), getMerchantID(), function(data) {
					var favouriteHtml = "<span style='float: right'>";
					if(data=="notlogin") {
						favouriteHtml += "<a href='javascript:;' title='登陆后可收藏此商品' onclick='favourite();' class='fav-off'>收藏<span></span></a>";
					}else if(data=="notexist") {
						favouriteHtml += "<a href='javascript:;' title='收藏此商品' onclick='favourite(this);' class='fav-off'>收藏<span></span></a>";
					}else if(data=="isexist") {
						favouriteHtml += "<a href='javascript:;' title='进入收藏管理' onclick='favourite(this);' class='fav-on'>收藏<span></span></a>";
					}
					favouriteHtml += "</span>";
					jmallJQ("#miniCartInner").find("span.cartTitle").parent().append(favouriteHtml);
				});
				return;
			}
			var asyncFlag = dwr.engine._async;
			if(!asyncFlag){
				dwr.engine.setAsync(true);
			}
			Buy.getProductInfoForMiniCart(product, function(data) {
				product = data;
				apiProduct = data;
				product.sort = {"id" : product.sortId};
				var priceinfo = getPrice(product);
				var handlingChargeHtml = "<span class='lineHead'>代购手续费</span>：";
				// discount handling
				if (priceinfo.orgRmbHandling != null) {
					handlingChargeHtml = handlingChargeHtml + "<span class='disposed'><b>" + priceinfo.orgRmbHandling + "</b>&nbsp;RMB</span>&nbsp;&nbsp;";
					handlingChargeHtml = handlingChargeHtml + "优惠价：<span class='discount'><b>" + priceinfo.rmbHandlingCharge + "</b>&nbsp;RMB</span>";
					// no discount
				} else {
					handlingChargeHtml = handlingChargeHtml + "<b>" + priceinfo.rmbHandlingCharge + "</b>&nbsp;RMB&nbsp;&nbsp;";
				}
				var intFreightHtml = "<span class='lineHead'>团发国际运费</span>：";
				var nationFreight = "";
				// weight is unknown
				if (priceinfo.weight == "") {
					intFreightHtml = intFreightHtml + "未知 (" + priceinfo.weightPrice + " 元/50g)";
					nationFreight = "未知";
				} else {
					nationFreight = "0";
					// discount internal freight
					if (priceinfo.discountWeight < 0) {
						intFreightHtml = intFreightHtml + "<b>" + priceinfo.rmbIntfreight + "</b>&nbsp;RMB";
						// no discount
					} else {
						intFreightHtml = intFreightHtml + "<span class='disposed'><b>" + priceinfo.rmbIntfreight + "</b>&nbsp;RMB</span>&nbsp;&nbsp;";
						intFreightHtml = intFreightHtml + "优惠价：<span class='discount'><b>" + priceinfo.discountRmbIntfreight + "</b>&nbsp;RMB</span>";
					}
				}
				var miniCartTotalInit = (priceinfo.rmbTotalPrice + (priceinfo.discountWeight < 0 ? priceinfo.rmbIntfreight : priceinfo.discountRmbIntfreight) * product.number);
				var miniCartInnerHtml = "<input type='hidden' id='miniCartTotalWithoutNation' value='" + miniCartTotalInit + "'/>";
				miniCartInnerHtml = miniCartInnerHtml + "<ul>";
				
				miniCartInnerHtml = miniCartInnerHtml + "<li><span class='cartTitle'>萌购团送</span><span style='float: right'>";
				if(data.favourite=="notlogin") {
					miniCartInnerHtml += "<a href='javascript:;' title='登陆后可收藏此商品' onclick='favourite();' class='fav-off'>收藏<span></span></a>";
				}else if(data.favourite=="notexist") {
					miniCartInnerHtml += "<a href='javascript:;' title='收藏此商品' onclick='favourite(this);' class='fav-off'>收藏<span></span></a>";
				}else if(data.favourite=="isexist") {
					miniCartInnerHtml += "<a href='javascript:;' title='进入收藏管理' onclick='favourite(this);' class='fav-on'>收藏<span></span></a>";
				}
				miniCartInnerHtml += "</span></li>";
				
				miniCartInnerHtml = miniCartInnerHtml + "<li><span class='lineHead'>商品价格</span>：<b>" + priceinfo.rmbProductPrice + "</b>&nbsp;RMB</li>";
				miniCartInnerHtml = miniCartInnerHtml + "<li>" + handlingChargeHtml + "</li>";
				miniCartInnerHtml = miniCartInnerHtml + "<li>" + "<span class='lineHead'>日本国内运费</span>：<b>" + priceinfo.rmbShipping + "</b>&nbsp;RMB" + "</li>";
				miniCartInnerHtml = miniCartInnerHtml + "<li>" + intFreightHtml + "</li>";
				miniCartInnerHtml = miniCartInnerHtml + "<li>国内运费估算：";
				miniCartInnerHtml = miniCartInnerHtml + "<select name='miniCartState' onchange='calculateNationFreight(" + (priceinfo.weight.length == 0 ? "0" : priceinfo.weight) + ");'>";
				miniCartInnerHtml = miniCartInnerHtml + "<option value='0'>请选择</option>";
				miniCartInnerHtml = miniCartInnerHtml + areaOptionStr;
				miniCartInnerHtml = miniCartInnerHtml + "</select>&nbsp;&nbsp;";
				miniCartInnerHtml = miniCartInnerHtml + "<select name='miniCartPayment' onchange='calculateNationFreight(" + (priceinfo.weight.length == 0 ? "0" : priceinfo.weight) + ");'>";
				miniCartInnerHtml = miniCartInnerHtml + "<option value='0'>请选择</option>";
				miniCartInnerHtml = miniCartInnerHtml + groupOptionStr;
				miniCartInnerHtml = miniCartInnerHtml + "</select>&nbsp;&nbsp;";
				miniCartInnerHtml = miniCartInnerHtml + "<b id='miniCartNationFreight'>" + nationFreight + "</b>&nbsp;RMB</li>";
				miniCartInnerHtml = miniCartInnerHtml + "<li><hr noshade='noshade' size='1'></li>";
				miniCartInnerHtml = miniCartInnerHtml + "<li>总计：<span class='miniCartPrice'><b id='miniCartTotal'>" + miniCartTotalInit + "</b>&nbsp;RMB</span></li>";
				miniCartInnerHtml = miniCartInnerHtml + "<li>" + "当前汇率：" + exchange_rate + "&nbsp;RMB = 1JPY" + "</li>";
				miniCartInnerHtml = miniCartInnerHtml + "<li>该商品将在AMAZON发货后7-10个工作日内向用户投递。</li>";
				miniCartInnerHtml = miniCartInnerHtml + "<li>欲使用直送方式购买，可在提交订单后更改。</li>";
				if (isNeedToGoOffersPage()) {
					miniCartInnerHtml = miniCartInnerHtml + "<li>该商品由AMAZON上的第三方卖家出售，请前往<a title='第三方卖家列表' onclick='gotoUsedPage();' href='javascript:;'><b>第三方卖家列表</b></a>购买此商品。</li>";
				} else {
					miniCartInnerHtml = miniCartInnerHtml + "<li><a title='加入购物车' onclick='checkBuy();' href='javascript:;' class='dpSprite s_bbAdd2Cart' style='background: transparent url(" + contextPath 
						+ "/images/Amazon_addtoCart.gif) no-repeat scroll;width:180px;height:27px;'><img border='0' src='http://g-ec3.images-amazon.com/images/G/09/x-locale/common/transparent-pixel._V42752373_.gif'></a></li>";
				}
				miniCartInnerHtml = miniCartInnerHtml + "</ul>";
				jmallJQ("#miniCartInner").html(miniCartInnerHtml);
			});
			if(!asyncFlag) {
				dwr.engine.setAsync(false);
			}
		}
		
		function createPopupMiniCart() {
			var pageProduct = getProduct();
			var sortList = new Array();
			pageProduct.pid = getAsin();
			pageProduct.discnum = 0;
			pageProduct.contentRating = 0;
			pageProduct.quantityLimit = 1;
			var asyncFlag = dwr.engine._async;
			if(!asyncFlag){
				dwr.engine.setAsync(true);
			}
			Buy.getProductsInfoForPopupMiniCart(pageProduct, function(data) {
				pageProduct = data;
				var sortIdArr = eval(data.sortIdArr);
				for ( var i = 0; i < sortIdArr.length; i++) {
					sortList[i] = {"id" : sortIdArr[i]};
				}
				var popupProducts = new Array();
				jmallJQ("tbody[class=result]").each(function() {
					var product = getProduct();
					var price = jmallJQ(this).find(".price").html();
					price = replacePrice(price);
					var refPrice = jmallJQ("td[class=listprice]").html();
					if (refPrice != null) {
						refPrice = replacePrice(refPrice);
					} else {
						refPrice = 0;
					}
					var priceShipping = jmallJQ(this).find(".price_shipping").html();
					if (priceShipping != null) {
						priceShipping = replacePrice(priceShipping);
					} else {
						priceShipping = 0;
					}
					
					var type = typeused;
					var sellerName = "";
					var merchantID = undefined;
				
					var child = jmallJQ(this).find(".sellerInformation").children();
					if (child[0].nodeName == "IMG" && jmallJQ(child[0]).attr("alt") == "Amazon.co.jp") {
						type = typenew;
						sellerName = "AMAZON";
					} else if (child[0].nodeName == "A") {
						sellerName = jmallJQ(child[0]).find("img").attr("alt");
						merchantID = jmallJQ(child[0]).attr("href").replace(contextPath + "/amazon/", "").replace(".htm", "");
						if (merchantID != null && merchantID.length > 0) {
							merchantID = decrypt(merchantID);
						}
						var merchantIDArr = merchantID.split("/");
						merchantID = merchantIDArr[jmallJQ.inArray("shops", merchantIDArr) + 1];
					} else {
						sellerName = jmallJQ(this).find(".seller").find("b").html();
						merchantID = jmallJQ(this).find(".seller").find("a").attr("href").replace(contextPath + "/amazon/", "").replace(".htm", "");
						if (merchantID != null && merchantID.length > 0) {
							merchantID = decrypt(merchantID);
						}
						merchantID = merchantID.substring(merchantID.indexOf("seller=") + "seller=".length);
					}
					var remark = jmallJQ(this).find(".availability").text();
					remark = remark.substring(0, remark.indexOf("。"));
					remark = remark.replace("発送について:", "");
					
					product.price = price;
					product.refPrice = refPrice;
					product.shipcharge = priceShipping;
					product.number = 1;
					product.pid = pageProduct.pid;
					if (sellerName == "AMAZON") {
						if (remark.indexOf("在庫") >= 0 || remark.indexOf("有库存") >= 0) {
							product.type = typenew;
						} else {
							product.type = typepresell;
						}
					} else {
						product.type = typeused;
					}
					product.sort = sortList[product.type];
					product.discnum = pageProduct.discnum;
					popupProducts.push(product);
				});
				
				var priceinfoList = getPriceFromList(popupProducts);
				for ( var i = 0; i < priceinfoList.length; i++) {
					var priceinfo = priceinfoList[i];
					var product = popupProducts[i];
					var handlingChargeHtml = "<span class='lineHead'>代购手续费</span>：";
					// discount handling
					if (priceinfo.orgRmbHandling != null) {
						handlingChargeHtml = handlingChargeHtml + "<span class='disposed'><b>" + priceinfo.orgRmbHandling + "</b>&nbsp;RMB</span>&nbsp;&nbsp;";
						handlingChargeHtml = handlingChargeHtml + "优惠价：<span class='discount'><b>" + priceinfo.rmbHandlingCharge + "</b>&nbsp;RMB</span>";
						// no discount
					} else {
						handlingChargeHtml = handlingChargeHtml + "<b>" + priceinfo.rmbHandlingCharge + "</b>&nbsp;RMB&nbsp;&nbsp;";
					}
					var intFreightHtml = "<span class='lineHead'>团发国际运费</span>：";
					var nationFreight = "";
					// weight is unknown
					if (priceinfo.weight == "") {
						intFreightHtml = intFreightHtml + "未知 (" + priceinfo.weightPrice + " 元/50g)";
						nationFreight = "未知";
					} else {
						nationFreight = "0";
						// discount internal freight
						if (priceinfo.discountWeight < 0) {
							intFreightHtml = intFreightHtml + "<b>" + priceinfo.rmbIntfreight + "</b>&nbsp;RMB";
							// no discount
						} else {
							intFreightHtml = intFreightHtml + "<span class='disposed'><b>" + priceinfo.rmbIntfreight + "</b>&nbsp;RMB</span>&nbsp;&nbsp;";
							intFreightHtml = intFreightHtml + "优惠价：<span class='discount'><b>" + priceinfo.discountRmbIntfreight + "</b>&nbsp;RMB</span>";
						}
					}
					var miniCartTotalInit = (priceinfo.rmbTotalPrice + (priceinfo.discountWeight < 0 ? priceinfo.rmbIntfreight : priceinfo.discountRmbIntfreight) * product.number);
					var miniCartInnerHtml = "<input type='hidden' id='miniCartTotalWithoutNation' value='" + miniCartTotalInit + "'/>";
					miniCartInnerHtml = miniCartInnerHtml + "<ul>";
					
					miniCartInnerHtml = miniCartInnerHtml + "<li><span class='cartTitle'>萌购团送</span></li>";
					
					miniCartInnerHtml = miniCartInnerHtml + "<li><span class='lineHead'>商品价格</span>：<b>" + priceinfo.rmbProductPrice + "</b>&nbsp;RMB</li>";
					miniCartInnerHtml = miniCartInnerHtml + "<li>" + handlingChargeHtml + "</li>";
					miniCartInnerHtml = miniCartInnerHtml + "<li>" + "<span class='lineHead'>日本国内运费</span>：<b>" + priceinfo.rmbShipping + "</b>&nbsp;RMB" + "</li>";
					miniCartInnerHtml = miniCartInnerHtml + "<li>" + intFreightHtml + "</li>";
					miniCartInnerHtml = miniCartInnerHtml + "<li>国内运费估算：";
					miniCartInnerHtml = miniCartInnerHtml + "<select name='miniCartState' onchange='calculateNationFreight(" + (priceinfo.weight.length == 0 ? "0" : priceinfo.weight) + ");'>";
					miniCartInnerHtml = miniCartInnerHtml + "<option value='0'>请选择</option>";
					miniCartInnerHtml = miniCartInnerHtml + areaOptionStr;
					miniCartInnerHtml = miniCartInnerHtml + "</select>&nbsp;&nbsp;";
					miniCartInnerHtml = miniCartInnerHtml + "<select name='miniCartPayment' onchange='calculateNationFreight(" + (priceinfo.weight.length == 0 ? "0" : priceinfo.weight) + ");'>";
					miniCartInnerHtml = miniCartInnerHtml + "<option value='0'>请选择</option>";
					miniCartInnerHtml = miniCartInnerHtml + groupOptionStr;
					miniCartInnerHtml = miniCartInnerHtml + "</select>&nbsp;&nbsp;";
					miniCartInnerHtml = miniCartInnerHtml + "<b id='miniCartNationFreight'>" + nationFreight + "</b>&nbsp;RMB</li>";
					miniCartInnerHtml = miniCartInnerHtml + "<li><hr noshade='noshade' size='1'></li>";
					miniCartInnerHtml = miniCartInnerHtml + "<li>总计：<span class='miniCartPrice'><b id='miniCartTotal'>" + miniCartTotalInit + "</b>&nbsp;RMB</span></li>";
					miniCartInnerHtml = miniCartInnerHtml + "<li>" + "当前汇率：" + exchange_rate + "&nbsp;RMB = 1JPY" + "</li>";
					miniCartInnerHtml = miniCartInnerHtml + "<li>该商品将在AMAZON发货后7-10个工作日内向用户投递。</li>";
					miniCartInnerHtml = miniCartInnerHtml + "<li>欲使用直送方式购买，可在提交订单后更改。</li>";
					miniCartInnerHtml = miniCartInnerHtml + "</ul>";
					var miniCartHtml = "<div id='popupMiniCart' class='cBox grayBox' style='display: none;'>";
					miniCartHtml = miniCartHtml + "<span class='cBoxTL'><!-- &nbsp; --></span>";
					miniCartHtml = miniCartHtml + "<span class='cBoxTR'><!-- &nbsp; --></span>";
					miniCartHtml = miniCartHtml + "<span class='cBoxR'><!-- &nbsp; --></span>";
					miniCartHtml = miniCartHtml + "<span class='cBoxBL'><!-- &nbsp; --></span>";
					miniCartHtml = miniCartHtml + "<span class='cBoxBR'><!-- &nbsp; --></span>";
					miniCartHtml = miniCartHtml + "<span class='cBoxB'><!-- &nbsp; --></span>";
					miniCartHtml = miniCartHtml + "<a class='closeBtn' href='javascript:;' onclick='hidePopupMiniCart();'>×</a>";
					miniCartHtml = miniCartHtml + "<div id='miniCartInner' class='cBoxInner'>";
					miniCartHtml = miniCartHtml + miniCartInnerHtml;
					miniCartHtml = miniCartHtml + "</div>";
					miniCartHtml = miniCartHtml + "</div>";
					popupMiniCartList[i] = miniCartHtml;
				}
			});
			if(!asyncFlag) {
				dwr.engine.setAsync(false);
			}
		}
		
		function showPopupMiniCart(index, event) {
			if (popupMiniCartList[index] != null && popupMiniCartList[index].length != 0) {
				if (jmallJQ("#popupMiniCart").length != 0) {
					jmallJQ("#popupMiniCart").remove();
				}
				jmallJQ("body").append(popupMiniCartList[index]);
				var popupMiniCart = jmallJQ("#popupMiniCart");
				popupMiniCart.css("top", event.pageY + 10);
				popupMiniCart.css("left", event.pageX + 10);
				popupMiniCart.fadeIn();
			}
		}
		
		function hidePopupMiniCart() {
			var popupMiniCart = jmallJQ("#popupMiniCart");
			popupMiniCart.fadeOut(function() {
				popupMiniCart.remove();
			});
		}
		
		function movePopupMiniCart(index, event) {
			var elementTop = jmallJQ(event.currentTarget).position().top;
			var elementBottom = jmallJQ(event.currentTarget).position().top + jmallJQ(event.currentTarget).innerHeight();
			var popupMiniCart = jmallJQ("#popupMiniCart");
			if (popupMiniCart.length > 0) {
				popupMiniCart.css("top", event.pageY + 10);
				popupMiniCart.css("left", event.pageX + 10);
				if (event.pageY <= elementTop || event.pageY >= elementBottom) {
					hidePopupMiniCart();
				}
			} else {
				if (event.pageY > elementTop && event.pageY < elementBottom) {
					showPopupMiniCart(index, event);
				}
			}
		}
		
		function favourite(a) {
			if(a) {
				var product = apiProduct;
				if(product==null) {
					product = getProduct();
					product.pid = getAsin();
					product.name = jmallJQ("#btAsinTitle").text().replace(/\"/g, "\'");
					product.link = getAmazonDetailLink(product.pid);
					product.merchantID = getMerchantID();
					product.seller = "AMAZON";
				}
				
				var fav = {
					pid: product.pid,
					name: product.name,
					link: product.link,
					merchantID: product.merchantID,
					seller: product.seller
				};
				
				fav.imageURL = jmallJQ('#prodImage').attr('src');
				
				var fa =  jmallJQ(a);
				fa.addClass('fav-load');
				if(fa.hasClass('fav-off')) {
					Favourite.addFavourite(fav, function(msg) {
						fa.removeClass('fav-load');
						if(msg) {
							fa.removeClass('fav-off').addClass('fav-on');
							fa.attr('title', '进入收藏管理');
						}
					});
				} else {
					window.location = contextPath + "/user/favourite.htm";
				}
			}else {
				window.location = jmallJQ("#loginurl").attr("href");
			}
		}
		
