﻿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,_price) { 
   if(proid != "" && proname != "") { 
   var ProIDList = cookie.Get("carList"); //车内商品ID列表 
   if(ProIDList!=null && ProIDList!="" && ProIDList!="null") 
   { 
  // alert(Common.hasOne(proid));
   if(Common.hasOne(proid)) 
   { 
   ProIDList += "&"+proid+"="+proid+"|"+quantity+"|"+escape(proname)+"|"+_price; 
   cookie.Set("carList",ProIDList,2,"/");//更新购物车清单 
   return true;
   //TotalPro = cookie.Get("TotalPro"); //当前车内含有商品的总数 
   //TotalPro++; //总数+1 
   //cookie.Set("TotalPro",TotalPro,2,"/"); 
   } 
   else 
   { 
   alert("购物车中已含有此商品");
   return false; 
   } 
   } 
   else { 
   
   ProIDList=proid+"="+proid+"|"+quantity+"|"+escape(proname)+"|"+_price; 
   cookie.Set("carList",ProIDList,2,"/");//更新购物车清单 
    return true;
   //cookie.Set("TotalPro",1,2,"/"); 
   } 
   return false;
   } 
   
   }, //添加物品结束 
   
   //重置购物车内个数 
   reloadcar:function() 
   { 
   var t=cookie.Get("TotalPro"); 
   if(t!=""&&t!="null") 
   document.getElementById("cart_num").innerText="(" + cookie.Get("TotalPro") + ")"; 
   else 
   document.getElementById("cart_num").innerText="(0)"; 
   }, //重置结束 
   
   //检验购物车内是否已经含有该商品 
   hasOne:function(pid){ 
   
   ProIDList = cookie.Get("carList"); //车内商品ID列表 
   
   if(ProIDList.lastIndexOf("&") != -1){ 
  
   var arr=ProIDList.split("&"); 
   for(i=0;i<arr.length;i++) 
   {      
   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; 
   }    
   
   }; 
   