// JavaScript Document

// JScript 文件 
  /**//* 
  by 码农.KEN (2007-11-9) 
  */ 
  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, 2110-Jan-70 00:00:01 GMT'; 
   return t 
   } 
  }; 
  //var TotalPro = cookie.Get("TotalPro"); //当前车内含有商品的总数 
  //下面该说说JS具体如何来操作购物车了，在这里，购物车需要的操作有：增加商品、修改商品、删除商品。 
  //这些操作偶就直接贴代码了，都做了注释的，如果有不明之处可以回帖，我尽力作答。 
   
  //说明下：如果JS本身具有能直接操作带有子键的COOKIES的话，那完全没有必要写出下面那么多行的代码了！ 
  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)); 
   }, 
   
   
   
   //重置购物车内个数 
   reloadcar:function(id) 
   { 
   var t=cookie.Get("TotalPro");
   if(t!=""&&t!="null"&&t!=null)
   document.getElementById("cart_num").innerText= t ;
   else
   document.getElementById("cart_num").innerText="0";
   
   }, //重置结束 
   
   //重置购物车内个数 
   carisnull:function() 
   {
   var t=cookie.Get("TotalPro"); 
   if(t!=""&&t!="null"&&t!=null&&t!='0') 
   document.write("<div>你还有"+t+"件商品没结算，<a href='/order/shopcar.html2'><font color=red><b>现在去结算</b></font></a></div>"); 
   else
   document.getElementById("shopcar").style.display="none";
   }, //重置结束
   
    //刷新页面 
   reflashcar:function() 
   { 
   window.location.reload();
   }, //刷新页面 结束 
   
   //去到购物车
   goToCar:function() 
   { 
   window.open("/order/shopcar.html2");
   }, //刷新页面 结束 
   
   //检验购物车内是否已经含有该药店
   hasStore:function(sid){ 
	   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("="))==sid) 
	   { 
	   
	   return false; 
	   } 
	   } 
	   } 
	   else if(ProIDList!="null"&&ProIDList!="") 
	   { 
	   if(ProIDList.substr(0,ProIDList.indexOf("="))==sid) 
	   return false; 
	   } 
	   return true; 
   }, //检测结束
   
   //更新产品数量
   updateProductCou:function(ProIDList,proid,quantity){ 
	   if(ProIDList.lastIndexOf("&") != -1) { 
	   var arr=ProIDList.split("&"); 
	   var sub=Common.getSubPlace(ProIDList,proid);//获取该物品在COOKIE数组中的下标位置 
	   var arr2=arr[sub].split("-"); 
	   //alert(arr2+"arr2"); 
	   if(arr2[1].indexOf("@")>0){
			var temparr2=arr2[1].split("@");
			temparr2[0]=quantity;
			arr2[1]=temparr2[0]+"@"+temparr2[1];
	   }
	   else{
			arr2[1]=quantity; 
	   }
	   
	   var tempStr=arr2.join("-");//由数组重组字符串 
	   arr[sub] = tempStr; 
	   var newProList = arr.join("&");//由数组重组字符串 
	   cookie.Set("carList",newProList,200,"/");//更新购物车清单 
	   //alert(newProList+"the1"); 
	   } 
	   else { 
	   
	   var arr=ProIDList.split("-"); 
	   arr[1]=quantity; 
	   var newProList=arr.join("-"); 
	   cookie.Set("carList",newProList,200,"/");//更新购物车清单 
	   //alert(newProList+"the2"); 
	   } 
   }, //更新产品数量
   
   //更新购买组合数量
   updateProductMix:function(productMixList,productMixID,cou){
	   if(productMixList!=null&&productMixList!=''){
		   if(productMixList.lastIndexOf("@") != -1) { 
			   var arr=productMixList.split("@"); 
			   var sub=Common.getSubPlace(productMixList,productMixID);//获取该物品在COOKIE数组中的下标位置 
			   var arr2=arr[sub].split("="); 
			   //alert(arr2+"arr2"); 
				arr2[1]=Number(cou); 
			   var tempStr=arr2.join("=");//由数组重组字符串 
			   arr[sub] = tempStr; 
			   var productMixList = arr.join("@");//由数组重组字符串 
			   cookie.Set("productMixList",productMixList,200,"/");//更新购物车清单 
			   //alert(newProList+"the1"); 
		   } 
		   else { 
			   var arr=productMixList.split("="); 
			   arr[1]=Number(arr[1])+Number(cou);
			   //alert(arr);
			   var productMixList=arr.join("="); 
			   cookie.Set("productMixList",productMixList,200,"/");//更新购物车清单 
			   //alert(newProList+"the2"); 
		   } 
	   }
	   else{
		   productMixList=productMixID+"=1";
		   cookie.Set("productMixList",productMixList,200,"/");//更新购物车清单 
	   }
   }, //更新购买组合数量
   
   //更新总数量
   updateTotalPro:function(cou){ 
	    var t=cookie.Get("TotalPro"); 
	   t=Number(t)+Number(cou);
	   cookie.Set("TotalPro",t,200,"/");//更新购物车清单 
   }, //更新总数量
   
   //检验购物车内是否已经含有该商品 
   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; 
   }, //检测结束 
   
   //添加至购物车 
   intoCar:function(proid,quantity,storeid,productMixID) { 
   if(proid != "" && quantity != "") { 
   var ProIDList = cookie.Get("carList"); //车内商品ID列表 
	   if(ProIDList!=null && ProIDList!="" && ProIDList!="null") 
	   { 
	   if(Common.hasStore(storeid)){
			   ProIDList +="@"+storeid+"="+storeid+"&"+proid+"::"+proid+"-"+quantity;
			   cookie.Set("carList",ProIDList,200,"/");//更新购物车清单
			   if(cookie.Get("TotalPro")==null||cookie.Get("TotalPro")==""){
				   t=quantity;
			   }
			   else{
				  var t=cookie.Get("TotalPro"); 
			   		t=Number(t)+Number(quantity); 
			   }
			   cookie.Set("TotalPro",t,200,"/");//更新Cookies中的个数 
			   //alert("商品成功添加进去购物车");
			   }
			   else{
				   if(Common.hasOne(proid)) 
				   { //alert(arr+"test1");
						if(ProIDList.lastIndexOf("@") != -1){ 
						   var arr=ProIDList.split("@");
						   //alert(arr+"2");
						   var tempStr="";
						   for(i=0;i<arr.length;i++){
							   if(arr[i].substr(0,arr[i].indexOf("="))==storeid) { 
								arr[i]=arr[i]+"&"+proid+"::"+proid+"-"+quantity;
							   }
							   if(i==0){
							   		tempStr=arr[i];
							   }
							   else{
								   tempStr+="@"+arr[i];
							   }
					   		} 
							//alert(tempStr+"2");
					   cookie.Set("carList",tempStr,200,"/");//更新购物车清单 
					   var t=cookie.Get("TotalPro"); 
					   t=Number(t)+Number(quantity); 
					   cookie.Set("TotalPro",t,200,"/");//更新Cookies中的个数 
					   // Common.reloadcar();//更新顶部个数显示
					   //alert("商品成功添加进去购物车"); 
					   }
					   else{
						   ProIDList +="&"+proid+"::"+proid+"-"+quantity;
						   cookie.Set("carList",ProIDList,200,"/");//更新购物车清单 
						   var t=cookie.Get("TotalPro"); 
						   t=Number(t)+Number(quantity);
						   cookie.Set("TotalPro",t,200,"/");//更新Cookies中的个数
						   //alert("商品成功添加进去购物车"); 
					   }
				   }
	   else 
	   { 
			alert("购物车中已含有此商品，请查看购物车"); 
	   } 
				   
		} 

	   }
	   else { 
	   ProIDList=storeid+"="+storeid+"&"+proid+"::"+proid+"-"+quantity; 
	   cookie.Set("carList",ProIDList,200,"/");//更新购物车清单 
	   cookie.Set("TotalPro",1,200,"/"); 
	   //alert("商品成功添加进去购物车"); 
	   }
	   if(productMixID!=''){
		var productMixList = cookie.Get("productMixList"); //购买组合
		//更新购买组合
		Common.updateProductMix(productMixList,productMixID,1);
	   }
	   Common.goToCar();//更新顶部个数显示
		//alert(ProIDList); 
	   
   	}  
   }, //添加物品结束 
   
   //移除某商品 
   reMoveOne:function(proid,storeid,cou,productMixID){ 
   if(!Common.hasOne(proid)){ 
   
   		if(ProIDList.lastIndexOf("@")!= -1){
		var tempstore='';	
			var arrstore=ProIDList.split("@");
			for(var i=0;i<arrstore.length;i++){
			//alert(arrstore[i]+"arrstore["+i);
		   if(arrstore[i].substr(0,arrstore[i].indexOf("="))==storeid) {
			   if(arrstore[i].lastIndexOf("&")!=-1){ 
		   			var arr=arrstore[i].split("&"); 
				   if(arr.length<=2){
				   }
				   else{
					   for(var j=1;j<arr.length;j++){ 
					   if(arr[j].substr(0,arr[j].indexOf("::"))==proid){ 
							   //alert(arr[j]+"test1");

							   var arr=Common.delArr(arr,j);
							   //alert(arr+"test2");
							   var tempStr=arr.join("&"); //由数组重组字符串 
							   //alert(tempStr+"test3");
							   if(tempstore==''){
							   	tempstore=tempStr;
							   }
							   else{
							   	tempstore+="@"+tempStr;
							   }
							   //alert(tempstore+"tempstore");
							   
							   //return; 
			   				}
			   			}
			   			
			   			}
			   }
			   else{ 
				   cookie.Set("carList","",200,"/");//更新购物车清单 
				   var t=cookie.Get("TotalPro"); 
				   cookie.Set("TotalPro",0,200,"/");//更新购物车清单 
				   Common.reflashcar();//刷新购物车
			   }
		   }
		   else{
		    //alert(i);
			   	if(tempstore==''){
			   	tempstore=arrstore[i];
			   	//alert(tempstore+"tempstore11");
			   	}
			   	else{
			   	tempstore=tempstore+"@"+arrstore[i];
			   		//alert(tempstore+"tempstore12");
			   	}
		   }
		   //alert(arrstore[i]);
		   }
		    //alert(tempstore+"tempstore2");
		   cookie.Set("carList",tempstore,200,"/");//更新购物车清单 
		   var t=cookie.Get("TotalPro"); 
		   t=Number(t)-Number(cou);
		   cookie.Set("TotalPro",t,200,"/");//更新购物车清单 
		   // Common.reloadcar();//更新顶部个数显示 
		   }
		   else{
		   //只有一家药店的情况
		   
		   if(ProIDList.lastIndexOf("&") != -1){ 
		   
		   var arr=ProIDList.split("&"); 
		   //alert(arr.length);
			   if(arr.length>2){
				   for(i=0;i<arr.length;i++){ 
				   if(arr[i].substr(0,arr[i].indexOf("::"))==proid){ 
					   //alert(arr[i]+"test11");
					   var arr2=Common.delArr(arr,i); 
					   //alert(arr+"test22");
					   var tempStr=arr2.join("&"); //由数组重组字符串 
					   //alert(tempStr+"test33");
					   //tempstore=tempstore+"@"+tempStr;
					   cookie.Set("carList",tempStr,200,"/");//更新购物车清单 
					   var t=cookie.Get("TotalPro"); 
					   t=Number(t)-Number(cou);
					   cookie.Set("TotalPro",t,200,"/");//更新购物车清单 
					   // Common.reloadcar();//更新顶部个数显示 
					   return; 
		   				}
		   			}
		   			}
		   		else{
		   		//alert("只有一家药店的情况");
		   			cookie.Set("carList","",200,"/");//更新购物车清单 
				   cookie.Set("TotalPro",0,200,"/");//更新购物车清单 
				   // Common.reloadcar();//更新顶部个数显示 
		   		}
		   }
		   else{ 
		   
		   cookie.Set("carList","",200,"/");//更新购物车清单 
		   var t=cookie.Get("TotalPro"); 
		   cookie.Set("TotalPro",0,200,"/");//更新购物车清单 
		   // Common.reloadcar();//更新顶部个数显示 
		   }
		   }
   		}
		if(productMixID>0){
			var productMixList = cookie.Get("productMixList"); //购买组合
			//更新购买组合
			Common.updateProductMix(productMixList,productMixID,cou);
		}
   	Common.reflashcar();//更新顶部个数显示 
   }, //移除物品结束 
   
   //修改某物品数量
   //proid产品id
   //thetype加数量和减数量
   //stockRestrict限购数量，如果是0，则不限购
   updateQuantity:function(proid,thetype,stockRestrict,productidlist,productMixID){
	//判断该产品是否还在cookies
	
		var ProIDList = cookie.Get("carList"); //车内商品ID列表 
		var productMixList = cookie.Get("productMixList"); //购买组合
	   if(ProIDList.lastIndexOf("&") != -1) {   
	   if(!Common.getSubPlace(ProIDList,proid)){
		   alert("该产品已经不在购物车，请刷新！");
		   return false;
	   }
	   }
	   
	   var quantity;
	   var buynum=document.getElementById("buynum"+proid);
	   var subtotal=document.getElementById("subtotal"+proid);
	   var centstmp=0;
	   var cents=document.getElementById("cents"+proid);
	   var centsspan=document.getElementById("centsspan"+proid);
	   var subtotaltmp=0;
	   var subtotalspan=document.getElementById("subtotalspan"+proid);
	   var price=document.getElementById("price"+proid).value;
	   var totaltmp=0;
	   var total=document.getElementById("total");
	   var oldQuantity=document.getElementById("oldQuantity"+proid);//旧的数量
	   var productMixList = cookie.Get("productMixList"); //购买组合
	   switch(thetype){
		   case 'change':
		   quantity=document.getElementById("buynum"+proid).value;
		   
		   if(quantity<=0){
			   alert("数量不能少于0，如不需要该产品，请点击右边删除");
			   return;
		   }
		   if(stockRestrict>0){
			   if(quantity>stockRestrict){
				   alert("数量不能超过限购的数量，修改数量不成功");
					return;
			   }
		   }
		   //更新单个更改的数量和小计
		   subtotaltmp=quantity*price;
		   subtotalspan.innerHTML=roundAmount(subtotaltmp).toString(2);
		   subtotal.value=subtotaltmp;
		   //更新积分
		   centstmp=quantity*cents.value
		   centsspan.innerHTML=roundAmount(centstmp).toString(2);
			//更新总的价格
		   var arr=productidlist.split(",");
		   if(arr.length>1){
			   for(i=0;i<arr.length;i++){
				   if(proid!=arr[i]){
					   totaltmp=Number(totaltmp)+Number(document.getElementById("subtotal"+arr[i]).value);
				   }
			   }
			   total.innerHTML=roundAmount(totaltmp+Number(subtotaltmp)).toString(2);
		   }
		   else{
			   total.innerHTML=roundAmount(subtotaltmp).toString(2);
		   }
		   if(productMixID>0){
			  //更新购买组合
			Common.updateProductMix(productMixList,productMixID,Number(quantity)-Number(oldQuantity.value));
		   }
		//更新总数
		Common.updateTotalPro(Number(quantity)-Number(oldQuantity.value));
		   break;
		
		   case 'plus':
		   quantity=document.getElementById("buynum"+proid).value;
		   quantity++;
		   if(stockRestrict>0){
			   if(quantity>stockRestrict){
				   alert("数量不能超过限购的数量");
					return;
			   }
		   }
		   //更新购买数量
		   buynum.value=quantity;
		   //更新单个更改的数量和小计
		   subtotaltmp=quantity*price;
		   subtotalspan.innerHTML=roundAmount(subtotaltmp).toString(2);
		   subtotal.value=subtotaltmp;
		   //更新积分
		   centstmp=quantity*cents.value
		   centsspan.innerHTML=roundAmount(centstmp).toString(2);
			//更新总的价格
		   var arr=productidlist.split(",");
		   if(arr.length>1){
			   for(i=0;i<arr.length;i++){
				   if(proid!=arr[i]){
					   totaltmp=Number(totaltmp)+Number(document.getElementById("subtotal"+arr[i]).value);
				   }
			   }
			   total.innerHTML=roundAmount(totaltmp+Number(subtotaltmp)).toString(2);
		   }
		   else{
			   total.innerHTML=roundAmount(subtotaltmp).toString(2);
		   }
		   if(productMixID>0){
		   //更新购买组合
			Common.updateProductMix(productMixList,productMixID,1);
		   }
			//更新总数
			Common.updateTotalPro(Number(quantity)-Number(oldQuantity.value));
		   break;
		
		   case 'minus':
		   quantity=document.getElementById("buynum"+proid).value;
		   quantity--;
		   if(quantity<=0){
				alert("数量不能少于0，如不需要该产品，请点击右边删除");
				return;
			}
			if(stockRestrict>0){
			   if(quantity>stockRestrict){
				   alert("数量不能超过限购的数量");
					return;
			   }
		   }
		   //更新购买数量
		   buynum.value=quantity;
		   //更新单个更改的数量和小计
		   subtotaltmp=quantity*price;
		   subtotalspan.innerHTML=roundAmount(subtotaltmp).toString(2);
		   subtotal.value=subtotaltmp;
		   //更新积分
		   centstmp=quantity*cents.value
		   centsspan.innerHTML=roundAmount(centstmp).toString(2);
			//更新总的价格
		   var arr=productidlist.split(",");
		   if(arr.length>1){
			   for(i=0;i<arr.length;i++){
				   if(proid!=arr[i]){
					   totaltmp=Number(totaltmp)+Number(document.getElementById("subtotal"+arr[i]).value);
				   }
			   }
			   total.innerHTML=roundAmount(totaltmp+Number(subtotaltmp)).toString(2);
		   }
		   else{
			   total.innerHTML=roundAmount(subtotaltmp).toString(2);
		   }
		   if(productMixID>0){
			   //更新购买组合
				Common.updateProductMix(productMixList,productMixID,-1);
		   }
			//更新总数
			Common.updateTotalPro(Number(quantity)-Number(oldQuantity.value));
		   break;
		}
	   //alert(quantity+"test");
	   //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("-"); 
	   //alert(arr2+"arr2"); 
	   if(arr2[1].indexOf("@")>0){
			var temparr2=arr2[1].split("@");
			temparr2[0]=quantity;
			arr2[1]=temparr2[0]+"@"+temparr2[1];
	   }
	   else{
			arr2[1]=quantity; 
	   }
	   
	   var tempStr=arr2.join("-");//由数组重组字符串 
	   arr[sub] = tempStr; 
	   var newProList = arr.join("&");//由数组重组字符串 
	   cookie.Set("carList",newProList,200,"/");//更新购物车清单 
	   //alert(newProList+"the1"); 
	   } 
	   else { 
	   
	   var arr=ProIDList.split("-"); 
	   arr[1]=quantity; 
	   var newProList=arr.join("-"); 
	   cookie.Set("carList",newProList,200,"/");//更新购物车清单 
	   //alert(newProList+"the2"); 
	   } */
	   Common.updateProductCou(ProIDList,proid,quantity);
	   oldQuantity.value=quantity;
		
	   //Common.reflashcar();//刷新购物车
   }, //修改物品结束 
   
   JHshNumberText:function()
	{
		if(!(((window.event.keyCode >= 49) && (window.event.keyCode <= 57))|| (window.event.keyCode == 13) || (window.event.keyCode == 46) || (window.event.keyCode == 45)))
		{
		window.event.keyCode = 0 ;
		}
	},

   //返回指定物品所在数组的下标位置 
   getSubPlace:function(list,proid){ 
		   var arr=list.split("&"); 
		   for(i=0;i<arr.length;i++){ 
		   if(arr[i].substr(0,arr[i].indexOf("::"))==proid) { 
		   return i; 
		   } 
		   } 
	   
	   } //返回下标结束 

   }
   //取得两位小数
function   roundAmount(n)   {   
      var   s   =   ""   +   Math.round(n   *   100)   /   100   
      var   i   =   s.indexOf('.')   
      if   (i   <   0)   return   s   +   ".00"   
      var   t   =   s.substring(0,   i   +   1)   +     
              s.substring(i   +   1,   i   +   3)   
      if   (i   +   2   ==   s.length)   t   +=   "0"   
        
      return   t   
  }
