$(document).ready(function(){
	//history.forward();
}); 
function postCart(url) {
	if ($("#totalnumber").text()==0) {
		alert("You havent' chosen any merchandise");
		return false;
	}else{
		document.cartform.action=url;
	}
}

//Edit Product
function showProductColor(obj,pId,source, from)
{
	var objOS=$(obj).offset();
	var $color = $("#productcolor");
	if($color.size() > 0) return ;
	var host = "http://" + document.location.host + "/";
	var fromCart = (arguments.length == 3);
	
	if (fromCart) from = "cart";
	$.ajax({type:"GET",
		url:host + 'ajax/getProductColor.asp?pid='+pId + '&source='+source,
		dataType:"json",
		success:function(result)
		{
			if ($color.size() == 0)
			{
				if (fromCart)
					$color=$(obj).closest("#cartdetails").append('<div id="productcolor"></div>').find("#productcolor");
				else
				{
					$color=$(obj).closest("li").append('<div id="productcolor"></div>').find("#productcolor");
				}
			}
			var str="", itemNum;
			str='<h3>'+result.productCode+'</h3>';
			str+='<table id="colorlist"><tr><th>Color</th><th>Qty</th></tr>';
			$.each(result.colors,function(i,item){
				itemNum = item.number;
				if (0 == itemNum) itemNum = ""; 
				str+='<tr><td>'+item.colorName+'</td><td><input name="color-'+item.colorId+'" id="'+item.colorId+'" type="text" value="'+itemNum+'" size="4" maxlength="5" onkeyup="value=value.replace(/[^\\d]/g,\'\');" onbeforepaste="clipboardData.setData(\'text\',clipboardData.getData(\'text\').replace(/[^\\d]/g,\'\'))" /></td></tr>';
			});
			str+='</table>';
			$color.append(str);
			$color.append('<p class="do"><input name="update" type="button" value="update" onclick="updateCart(this.parentNode.parentNode,'+pId+','+result.minNum+','+result.maxNum+','+source+',\'' + from + '\')" /><input name="close" type="button" value="close" onclick="closeProductColor()" /></p>');
			var s={width:$color.width(),height:$color.height()};

			if (fromCart)
			{
				$color.css({position:"absolute",top:objOS.top+"px",left:(objOS.left-20)+"px",width:"0",height:"0"}).animate({left:(objOS.left-s.width-50)+"px",width:s.width+"px",height:s.height+"px"},{duration: "slow"});
			} else
			{
				var topDiff;
				var colorHeight = 100 + (result.colors.length * 20);
				topDiff = -80 - 20 * (result.colors.length-1) ;
				var pos = {position:"absolute",top:objOS.top+"px",left:(objOS.left-300)+"px",width:"0",height:"0"};
				var animateParam = {left:(objOS.left+100)+"px", top:(objOS.top + topDiff)+"px", width:s.width+"px", height:colorHeight+"px"};
				$color.css(pos).animate(animateParam, {duration: "slow"} );
			}
		}
	});
}

function closeProductColor() {
	var $color=$("#productcolor");
	var s={width:$color.width(),height:$color.height(),left:$color.offset().left};
	$color.animate({left:(s.left+s.width+20)+"px",width:"0px",height:"0px"},{duration:"slow",complete:function(){$(this).remove();}});
}
//更新购物车
function updateCart(obj,pid,minNum,maxNum,source, from) {
	var colors="";
	var num,name;
	var total=0;
	var inputs=obj.getElementsByTagName("INPUT");
	for (var i=0; i<inputs.length; i++) {
		if (inputs[i].type=='text') {
			name=inputs[i].name;
			num=(inputs[i].value==""?0:parseInt(inputs[i].value));
			if (num>0) {
				colors+=name+":"+num+"||";
				total+=num;
			}
		}
	}
	if (total<minNum) {
		alert("The ordered quantity at least is "+minNum);
		return false;
	}else if (maxNum>0 && total>maxNum) {
		alert("The most ordered quantity of this item for the unregistered users is "+maxNum);
		return false;
	}
	colors=colors.substr(0,colors.length-2);

	$.ajax({type:"POST",
		data:{"action":"update","pid":pid,"colors":colors,"number":total,"source":source},
		url:urlPath+"ajax/writeShoppingCart.asp",
		dataType:"json",
		success:function(result){
			if(result.status==1){
				$("#price"+pid + "_" + source).text(result.productPrice);
				$("#number"+pid + "_" + source).text(result.productNumber);
				$("#total"+pid + "_" + source).text(result.productTotal);
				$("#totalweight").text(result.totalWeight);
				$("#totalnumber").text(result.totalNumber);
				$("#totalprice").text(result.totalPrice);
				$("#ids"+pid).val(result.ids);
				$("#cartcount").text(result.totalNumber);
				if ($("#cartnum").size() > 0)
				{
					$("#cartnum").text(result.totalNumber);
				}
				if ($("#carttotalprice").size() > 0)
				{
					$("#carttotalprice").text(result.totalPrice);
				}
				if ("cartbottom" == from)
				{
					self.location.href = self.location.href;
				} else
				{
					closeProductColor();
				}
			}
		}
	});
}

//从购物车删除
function delProducts(obj) {
	var $row=$(obj).closest("tr");
	var dIds=$row.find("input[name=ids]").val();
	if (dIds.length>0) {
		$.ajax({type:"POST",
			data:{"action":"del","ids":dIds},
			url:urlPath+"ajax/writeShoppingCart.asp",
			dataType:"json",
			success:function(result){
				if(result.status==1){
					$row.remove();
					$("#totalweight").text(result.totalWeight);
					$("#totalnumber").text(result.totalNumber);
					$("#totalprice").text(result.totalPrice);
					$("#cartcount").text(result.totalNumber);
				}
			}
		});
	}
}

function CartAfterLogin()
{
	window.location.href = window.location.href;
}
