// JavaScript Document

var cookie={

//读取COOKIES,n为COOKIE名

Get:function(n){

var re=new RegExp(n+'=([^;]*);?','gi');

var r=re.exec(document.cookie)||[];

return (r.length>1?r[1]:null)

},

Get1:function(n){

var re=new RegExp(n+'=([^;]*);?','gi');

var r=re.exec(document.cookie)||[];

return unescape(r.length>1?r[1]:null)

},

//写入COOKIES,n为Cookie名，v为value

Set:function(n,v,e,p,d,s){

var t=new Date;

if(e){

// 8.64e7 一天 3.6e6 一小时

t.setTime(t.getTime() + (e*3.6e6));

}

document.cookie=n+'='+v+'; '+(!e?'':'; expires='+t.toUTCString())+(!p?'':'; path='+p)+(!d?'':'; domain='+d)+(!s?'':'; secure') // Set cookie

},

Set1:function(n,v,e,p,d,s){

var t=new Date;

if(e){

// 8.64e7 一天 3.6e6 一小时

t.setTime(t.getTime() + (e*8.64e7));

}

document.cookie=n+'='+escape(v)+'; '+(!e?'':'; expires='+t.toUTCString())+(!p?'':'; path='+p)+(!d?'':'; domain='+d)+(!s?'':'; secure') // Set cookie

},

Del:function(n,p,d){

var t=cookie.Get(n);

document.cookie=n+'='+(!p?'':'; path='+p)+(!d?'':'; domain='+d)+'; expires=Thu, 01-Jan-70 00:00:01 GMT';

return t

}

};

//var TotalPro = cookie.Get("TotalPro"); //当前车内含有商品的总数
var Common = {

//移除数组中指定项

delArr:function(ar,n) { //n表示第几项，从0开始算起。

if(n<0) //如果n<0，则不进行任何操作。

return ar;

else

return ar.slice(0,n).concat(ar.slice(n+1,ar.length));

},

//添加至购物车

intoCar:function(proid,quantity,proname,imgurl,_price) {

if(proid != "" && proname != "") {

var ProIDList = cookie.Get("carList"); //车内商品ID列表

if(ProIDList!=null && ProIDList!="" && ProIDList!="null")

{

if(Common.hasOne(proid))

{

ProIDList += "&"+proid+"="+proid+"|"+quantity+"|"+escape(proname)+"|"+escape(imgurl)+"|"+_price;

cookie.Set("carList",ProIDList,240,"/");//更新购物车清单

TotalPro = cookie.Get("TotalPro"); //当前车内含有商品的总数
totalprice=cookie.Get("totalprice"); //new

TotalPro++; //总数+1

totalprice=parseInt(totalprice)+parseInt(_price);  //new

cookie.Set("TotalPro",TotalPro,240,"/");
cookie.Set("totalprice",totalprice,240,"/");//new
alert("成功加入购物车");

}

else

{

alert("购物车中已含有此商品");

}

}

else {

ProIDList=proid+"="+proid+"|"+quantity+"|"+escape(proname)+"|"+escape(imgurl)+"|"+_price;

cookie.Set("carList",ProIDList,240,"/");//更新购物车清单

cookie.Set("TotalPro",1,240,"/");

cookie.Set("totalprice",_price,240,"/"); //new
alert("成功加入购物车");
}

Common.reloadcar();//更新顶部个数显示

//alert(ProIDList);

}

}, //添加物品结束

//重置购物车内个数

reloadcar:function()

{

var t=cookie.Get("TotalPro");

if(t!=null)
{

document.getElementById("cart_num").innerHTML="" + cookie.Get("TotalPro") + "";
document.getElementById("price").innerHTML="" + cookie.Get("totalprice") + "";//new
}

else
{
	
document.getElementById("cart_num").innerHTML="0";
document.getElementById("price").innerHTML="0"; //new
}
}, //重置结束


getcash:function(proid){
	ProIDList = cookie.Get("carList"); //车内商品ID列表
	if(ProIDList.lastIndexOf("&") != -1){

			var arr=ProIDList.split("&");
//alert(arr.length);
				for(i=0;i<arr.length;i++){
//alert("i:"+i+" yes:"+arr[i].substr(0,arr[i].indexOf("=")) +" proid="+proid);
						if(arr[i].substr(0,arr[i].indexOf("="))==proid) {
							
						
						
								
								var cc=arr[i].split("|");
								//alert(cc[4]);
								return cc[4];

						}
						else
						{
//alert("2");
								
						}
						}
	}
	},

//检验购物车内是否已经含有该商品

hasOne:function(pid){

ProIDList = cookie.Get("carList"); //车内商品ID列表

if(ProIDList.lastIndexOf("&") != -1){

var arr=ProIDList.split("&");

for(i=0;i<arr.length;i++)

{
	

//alert(arr.indexOf('='));

if(arr[i].substr(0,arr[i].indexOf("="))==pid)

{

return false;

}

}

}

else if(ProIDList!="null"&&ProIDList!="")

{

if(ProIDList.substr(0,ProIDList.indexOf("="))==pid)

return false;

}

return true;

}, //检测结束

//移除某商品

reMoveOne:function(proid){

if(!Common.hasOne(proid)){
	var t=cookie.Get("TotalPro");

if(t!=""&&t!="null")
{
	if (t==1)
	{
		
		
		var date = new Date();
date.setTime(date.getTime() - 10000);
 


		var t=new Date;
		cookie.Set("carList","",date.toGMTString(),"/");
		cookie.Set("totalprice",0,240,"/");
		cookie.Set("TotalPro",0,240,"/");
		}

else
{
		if(ProIDList.lastIndexOf("&") != -1){

			var arr=ProIDList.split("&");

				for(i=0;i<arr.length;i++){

						if(arr[i].substr(0,arr[i].indexOf("="))==proid) {
								
								
								var p=cookie.Get("totalprice");
								
								//alert(Common.getcash(proid));
								p=parseInt(p)-parseInt(Common.getcash(proid));
								
								cookie.Set("totalprice",p,240,"/");
								
								
								var arr2=Common.delArr(arr,i);

								var tempStr=arr2.join("&"); //由数组重组字符串

								cookie.Set("carList",tempStr,240,"/");//更新购物车清单

								var t=cookie.Get("TotalPro");

								cookie.Set("TotalPro",t-1,240,"/");//更新Cookies中的个数

								
// Common.reloadcar();//更新顶部个数显示

return;

}

}

}

else{

cookie.Set("carList","null");//更新购物车清单

var t=cookie.Get("TotalPro");

cookie.Set("TotalPro",0,240,"/");//更新购物车清单


//更新购物车清单
// Common.reloadcar();//更新顶部个数显示

}
}
	}
}
}, //移除物品结束

//修改某物品数量

updateQuantity:function(proid,quantity){

ProIDList = cookie.Get("carList"); //车内商品ID列表

if(ProIDList.lastIndexOf("&") != -1) {

var arr=ProIDList.split("&");

var sub=Common.getSubPlace(ProIDList,proid);//获取该物品在COOKIE数组中的下标位置

var arr2=arr[sub].split("|");

arr2[1]=quantity;

var tempStr=arr2.join("|");//由数组重组字符串

arr[sub] = tempStr;

var newProList = arr.join("&");//由数组重组字符串

cookie.Set("carList",newProList,240,"/");//更新购物车清单

//alert(newProList);

}

else {

var arr=ProIDList.split("|");

arr[1]=quantity;

var newProList=arr.join("|");

cookie.Set("carList",newProList,240,"/");//更新购物车清单

//alert(newProList);

}

}, //修改物品结束

//返回指定物品所在数组的下标位置

getSubPlace:function(list,proid){

var arr=list.split("&");

for(i=0;i<arr.length;i++){

if(arr.substr(0,arr.indexOf("="))==proid) {

return i;

}

}

} //返回下标结束

};
