//==================================

 var allGoodsNumForDiv;
 var allPriceForDiv;
/**
 *描述：促销--添加促销商品到购物车
 *参数: customerId,sessionId,goodsId,regionId,goodsNum
 */
 function addGoodsToShopCart(/*用户id*/customerId,/**当前会话id*/sessionId,/**要添加的商品ID*/goodsId,/**区域ID*/regionId,/**商品数量*/goodsNum,path){
	var customerId = customerId||"";
	var sessionId = sessionId||"";
	var goodsId = goodsId||"";
	var regionId = regionId||"";
	var goodsNum = goodsNum||"1";
	var pars = "customerId="+customerId+"&sessionId="+sessionId+"&goodsId="+goodsId+"&regionId="+regionId+"&goodsNum="+goodsNum;
	var url = "http://api.crm.mamabb.com/shop/cart/addToCart.action?"+pars;
	$.ajax({
	   type: "POST",
	   url: path+"/list/ajaxGetContent.shtml",
	   data: "url="+EncodeUtf8(url),
	   success: function(transport){
	     	var re = transport;
     		addGoodsToShopCartSuccess(re,customerId,sessionId);
	   }
	}); 
}

/**
 *促销-- 添加促销商品到购物车回调
 */
 function addGoodsToShopCartSuccess(/**返回消息*/msg,/**用户Id*/customerId,/**会话Id*/sessionId){
	var obj = eval("("+msg+")");
	var status = obj.code;
	if(status == 0){
//		alert("update custoemrID = "+customerId+" SESSIONiD = "+sessionId);
		//updateShopcartNum(customerId,sessionId);
		updateShopcartNum(customerId,sessionId,"cart");
		/*alert(allGoodsNumForDiv);
		alert(allPriceForDiv);
		document.getElementById("head_cartNumDiv2").innerHTML=allGoodsNumForDiv; 
		document.getElementById("addcartPop_totalPrice").innerHTML=allPriceForDiv;
		var popcontent = $('#addtocartPopDiv').html();
		AlertDialog(popcontent);//alert("添加购物车成功!");*/
	}else{
		alert("添加购物车失败!");
	}
 }
 

/**
 *描述： 促销-- 添加商品到收藏夹
 *参数: goodsId,customerId
 */
 function addGoodsToFavorite(/**将要收藏的商品ID*/goodsId,/**用户ID*/customerId,path){
 	var goodsId = goodsId||"";
 	var customerId = customerId||"";
 	if(customerId == ""){
 		if(confirm("您还未登录，是否去登录?")){
 				window.location = "http://sso.mamabb.com/login.jsp?lastUrl="+window.location;
 		}else{
 			return;
 		}
 	}
 	var url = "http://api.crm.mamabb.com/shop/customer/addGoodsToFavorite.action";
	var pars = "goodsId="+goodsId+"&customerId="+customerId;
	var parv = url+"?"+pars;
	$.ajax({
	   type: "POST",
	   url: path+"/list/ajaxGetContent.shtml",
	   data: "url="+EncodeUtf8(parv),
	   success: function(transport){
 			addFavoriteSuccess(transport);
	   }
	}); 
	
 }


/**
 *描述：促销-- 添加商品到收藏夹回调
 *参数: msg
 */
 function addFavoriteSuccess(/**返回消息*/msg){
 	var obj = eval("("+msg+")");
	var status = obj.code;
	if(status == 0){
		alert("商品收藏成功!");
	}else{
		alert("收藏夹中已有该商品!");
	}
 }

 
function refreshCart(re,sessionId,type){
	data = re.replace(/\&quot\;/g, '"');
	if(data == undefined || data == ''){
		return;
	}
	var total = eval('('+data+')').total;
	var totalPrice = parseFloat(eval('('+data+')').totalPrice).toFixed(2);
	//绑定商品总数
	if(total != null && total != ""){
		$("#head_cartNumDiv").html(total); 
		allGoodsNumForDiv= total;
		allPriceForDiv = totalPrice ; 
	}
	//绑定商品条目
	var result = eval('('+data+')').list;
	if(result != null && result != ""){
		var c = "";
		var obj = document.getElementById("cartBox"); 
		//for(var i in result){
		for(var i=0;i<result.length;i++){
			//alert(i);
			c +="<div class='cartBoxContentList'>";
			c +="<div><a href='#'>"+result[i].goods_name+"</a></div>";
			c +="<div>";
			c +="<span class='float_l'><b class='textf60'>￥"+result[i].goods_price+"</b> x "+result[i].goods_number+"</span>";
			c +="<span class='float_r'><a href='#' class='deleteBtn' onclick='javascript:delFromCartHead("+result[i].goods_id+","+result[i].region_id+");return false;'>删除</a></span>";
			c +="</div>	<div class='clear'></div></div>";
		}
		c +="<div>总计：<b class='textf60'>￥"+ totalPrice +"</b></div>";
		c +="<div class='cartBoxbtns'><a href='#' onclick='javascript:document.getElementById("+'"cartBox"'+").style.display="+'"none"'+"; return false;'><img src='html/include/temp/b2c_images/btn_keepshopping.gif' /></a> <a href='http://cart.mamabb.com/shop/cart/enterCountCenter.action?seid="+sessionId+"'><img src='html/include/temp/images/ico_paybtn.gif' /></a></div>";
		obj.innerHTML=c;
	}else{
		var obj = document.getElementById("cartBox");
		obj.innerHTML ='您的购物车中暂无商品，赶快选择心爱的商品吧!';
	}
	document.getElementById("head_cartNumDiv2").innerHTML=allGoodsNumForDiv; 
	document.getElementById("addcartPop_totalPrice").innerHTML=allPriceForDiv;
	var popcontent = $('#addtocartPopDiv').html();
	

}



//================================================
/**
 *客户端cookie操作
 */
var cookieUtil = {
	//获取cookie中的值
	getCookieValue : function(/**cookie名称*/name,isEncode){
		var cookies = document.cookie;
		var cookieArray = cookies.split(';');
		for(var i=0;i<cookieArray.length;i++){
			var v_cookie = cookieArray[i];
			var name_value = v_cookie.split('=');
			if(trim(name_value[0]) == trim(name)){
				if(isEncode)
					return strdecode(name_value[1]);
				else
					return name_value[1];
			}
		}
		return '';
	},
	//设置cookie中的值
	setCookieValue : function(/**cookie名称*/name,/**cookie值*/value,/**cookie所在域*/domain,/**cookie所在路径*/path,/**过期时间*/expires){
		var expires = expires||null;
		var path = path || '/';
		var domain = domain || null;
		document.cookie = name + "=" + escape (value) +
			((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
			((path == null) ? "" : ("; path=" + path)) +
			((domain == null) ? "" : ("; domain=" + domain));
	},
	//删除cookie
	delCookie : function(/**cookie名称*/name){
		if(cookieUtil.getCookieValue(name)){
			var expdate = new Date(); 
			expdate.setTime(expdate.getTime() - (86400 * 1000 * 1)); 
			Cookies.set(name, "", expdate);
		}
	}
}

//去左空格;
function ltrim(s){
	return s.replace( /^\s*/, '');
}
//去右空格;
function rtrim(s){
	return s.replace( /\s*$/, '');
}
//左右空格;
function trim(s){
	return rtrim(ltrim(s));
}