var FrontOrders_showMyFavorite01 = {

   /*
	* 功能：向cookies中添加商品ID
	* 参数：cookieName cookies的名字
	*      productid   商品ID
	*/
	addFavorites:function(cookieName,productid){
		var cookieValue = productid;
	    var cookieString = document.cookie;
        var start = cookieString.indexOf(cookieName + '=');
        if (start == -1){ // 找不到这个cookie，那么新增一个
             document.cookie = cookieName+'=' + escape(cookieValue)+';path=/';
        }else{//找到了那就提取出来
            start += cookieName.length + 1;
            var end = cookieString.indexOf(';', start);
            var cookieContentWrited = '';
            if (end == -1){  //cookieName所对应的项已经在cookie字符串的最后了
                cookieContentWrited = unescape(cookieString.substring(start));
            }else{    //cookieName所对应的项不在cookie字符串的最后
                cookieContentWrited = unescape(cookieString.substring(start, end));
            }

            if(cookieContentWrited.indexOf(cookieValue)==-1){
                //如果用户是选中并且用户选择的内容没有被保存过,那么把记录保存
               	if(cookieContentWrited=="")
				{
                document.cookie = cookieName+'=' + escape(cookieValue)+';path=/';
				}else
				{
					document.cookie = cookieName+'=' + escape(cookieContentWrited+'_'+cookieValue)+';path=/';
				}
            }
            /*if(cookieContentWrited.indexOf(cookieValue)!=-1 && _getElementById(compId,"input",idCheckbox).checked==false){
                //如果checked==false但是用户选择的内容已经被保存过,那么把记录保存移出

                //如果在cookie里只有一个ID了，并且这个ID还要被删除，那么也一并删除cookie的名称
                if(cookieContentWrited==cookieValue){
                    var exp = new Date();
                    exp.setTime (exp.getTime() - 1);
                    document.cookie = cookieName + "=" +  "; expires="+ exp.toGMTString()+';path=/';
					return;
                }else if(cookieContentWrited.indexOf(cookieValue)!=0){//如果这个cookie不排在第一位
                    cookieContentWrited = cookieContentWrited.replace(':'+cookieValue,'');
                }else{//如果这个cookie排在第一位，直接去掉cookieValue+':'
                     cookieContentWrited = cookieContentWrited.replace(cookieValue+':','');
                }
                document.cookie = cookieName+'=' + escape(cookieContentWrited)+';path=/';
            }*/

        }
	},
		/*
		* 购买按钮
		* 参数1：商品id
		* 参数2：购买url
		* 参数3：商品url
		* 参数4：购买弹出与否标示
		* 参数5：商品详细弹出与否标示
		
	toBuyAction:function(pid,buyhref,producthref,buyOpen,detailOpen){
			frontProductsDWRTools.goShoppingByPid(pid,function(checkValue){
				if(checkValue=='1'){//购物车 
					if(buyOpen=='1'){
						window.open(buyhref);
					} else if(buyOpen=='0'){
						document.location.href = buyhref;
					} else {
						document.location.href = buyhref;
					}
				}else{
					if(detailOpen=='1'){//商品
						window.open(producthref);
					} else if(detailOpen=='0'){
						document.location.href = producthref;
					} else {
						document.location.href = producthref;
					}
				}
			});
		},*/
		
		
	/*
	 * 购买按钮 参数1：商品id 参数2：购买url，未经过标签转义的 参数3：商品url，经过标签转义的 参数4：弹出与否标示 参数5：弹出与否标示
	 */
	toBuyAction:function(pid, buyhref, producthref, buyOpen,detailOpen) {
		frontProductsDWRTools.goShoppingByPid(pid,function(checkValue) {
							if (checkValue == '1' || checkValue == '-3') {
								var compid = FrontOrders_showMiniCart01.compId;
								//var actionUrl = "/FrontOrders.do?method=addProductToCart&favorite=favorite&productId="+ pid + "&action_page=" + buyhref + "&compId="+compid;
								if (buyOpen == '1') {
									//window.open(actionUrl,"","toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, status=yes");
									FrontOrders_showMiniCart01.addProduct("&productId=" + pid+ "&action_page=" + buyhref, "_blank");
								} else if (buyOpen == '0') {
									//document.location.href = actionUrl;
									FrontOrders_showMiniCart01.addProduct("&productId=" + pid+ "&action_page=" + buyhref, "_blank");
								} else {
									//document.location.href = actionUrl;
									FrontOrders_showMiniCart01.addProduct("&productId=" + pid+ "&action_page=" + buyhref, "_self");
								}
							} else {
								if (detailOpen == '1') {
									window.open(producthref,"","toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, status=yes");
								} else if (detailOpen == '0') {
									document.location.href = producthref;
								} else {
									document.location.href = producthref;
								}
							}
						});
	},
		
	/*
	 *继续购买
	 * 参数1：继续购买url
	 * 参数2：是否弹出标示
	 */
	 goonbuy:function(buyURL,flag){
	 	if(flag=='1'){
	 		window.open(buyURL);
	 	}else if(flag=='0'){
	 		document.location.href = buyURL;
	 	}else{
	 		document.location.href = buyURL;
	 	}
	 },
	 /*
	 * 功能 取消收藏 单个
	 * 参数 compId：compId；actionURL：进行执行取消操作的url ；productId：要被取消的商品id
	 */
	cancelFavSingle:function(compId,actionURL,productId){
		var strCookie=document.cookie; 
		//id串开始位置
		var start = strCookie.indexOf("favoritesid=")+12;
		//id串结束位置
		//var end = strCookie.lastIndexOf(";");
	    var end = strCookie.indexOf(';', start);
		if(end==-1){
			end= strCookie.length;
		}
		var idStr = strCookie.substring(start, end);
		var idStrArray = idStr.split("_");
		//var idArr = idStr.split("_");
		
		var newIdStr = "";
		
		/*if(idArr){
			for( var i = 0 ; i < idArr.length ; i++ ){
				if(idArr[i]!=productId){
					newIdStr+=idArr[i]+"_";
				}
			}
		}*/
		var indexproductid = idStr.indexOf(productId);
		if(indexproductid==-1){
			/*alert(i18n_order_productnotinfavorites);*/
			var productFrom = document.getElementById(compId+"_pageForm");
			productFrom.action=actionURL;
		    productFrom.submit();
		}else if(indexproductid==0){
			if(idStrArray.length==1){
				newIdStr = idStr.replace(productId,"");
			}else{
				newIdStr = idStr.replace(productId+"_","");
			}
		}else{
		    newIdStr = idStr.replace("_"+productId,"");
		}
		strCookie = strCookie.replace(idStr,newIdStr);
		document.cookie = "favoritesid="+escape(newIdStr)+';path=/';
		var productFrom = document.getElementById(compId+"_pageForm");
		productFrom.action=actionURL;
		productFrom.submit();
	},
	/*
	 * 功能 清空 cookies ,confirm 提示
	 * 参数 compId：compId；actionURL：进行执行取消操作的url
	 */
	 confirmCancelFav:function(compId,actionURL){
	 	var id = compId;
	 	var url = actionURL;
		if(confirm(i18n_orders_areyousureclean)){
				FrontOrders_showMyFavorite01.cancelFav(id,url);
			}
	},
	
	
	/*
	 * 功能 清空 cookies
	 * 参数 compId：compId；actionURL：进行执行取消操作的url
	 */
	cancelFav:function(compId,actionURL){
		var productFrom = document.getElementById(compId+"_pageForm");
	   // var cookieName = 'favoritesid';
		//var exp = new Date();
		//	exp.setTime(exp.getTime()-10000);
		//	document.cookie=cookieName+"=n;expire="+exp.toGMTString();
		var cookieName = 'favoritesid';
			var strCookie=document.cookie; 
			//id串开始位置
			var start = strCookie.indexOf("favoritesid=")+12;
			//id串结束位置
	   		var end = strCookie.indexOf(';', start);
			if(end==-1){
				end= strCookie.length;
			}
			var idStr = strCookie.substring(start, end);
			var newIdStr ='';
			document.cookie = "favoritesid="+escape(newIdStr)+';path=/';
			productFrom.action=actionURL;
			productFrom.submit();
		
	},
	  /**
		* 功能：提示不能添加信息
		* errormessage 是否提示标示
		*
		*/
	errormessage:function(errormessage){
		
		if(errormessage=='1'){
			alert(i18n_orders_favoriteerrormessage);
			return false;
		}
		return true;
	},
	/**
	  *清空cookie 
	  *cleanall 是否操作标示
	  */
	cleanallcookies:function(cleanall){
		if(cleanall=='cleanall'){
			var cookieName = 'favoritesid';
			var strCookie=document.cookie; 
			//id串开始位置
			var start = strCookie.indexOf("favoritesid=")+12;
			//id串结束位置
	   		var end = strCookie.indexOf(';', start);
			if(end==-1){
				end= strCookie.length;
			}
			var idStr = strCookie.substring(start, end);
			var newIdStr ='';
			document.cookie = "favoritesid="+escape(newIdStr)+';path=/';
			//var exp = new Date();
			//exp.setTime(exp.getTime()-10000);
			//document.cookie=cookieName+"=n;expire="+exp.toGMTString();
		}
	}
}
