 var shop={
		     check:function(a){
			  for(var k=0;k<=a;k++)
			  {
			   if ($("#attr"+k).val()==''){
			     alert('请选择'+$("#attrname"+k).html().replace('：','！'));
				 return false;
			   }
			  }
			  if ($("#num").val()==0||$("#num").val()==''){
			   alert('请输入购买数量!');
			   return false;
			  }
			  if (parseInt($("#num").val())>parseInt($("#stock").text())){
			   alert('对不起，您最多只能购买'+$("#stock").text()+'!');
			   return false;
			  }
			  return true;
			  
			 },
       gobuy:function(a){
			  if (shop.check(a)){$("#cartform").submit();}
		     },
			 checkchangebuy:function(changebuyneedprice){
				 var totalprice=parseFloat($("#totalprice").text());
				 if (totalprice<parseFloat(changebuyneedprice)){
					  alert('对不起,您的订单费用不足,还差￥' +(parseFloat(changebuyneedprice)-totalprice).toFixed(2)+' 元!');
					  return false;
				 }
				 return true;
			 },
			 buynum:function(f){
			   if (f==0&&parseInt($("#num").val())<=1){
				   alert('对不起，您最少要购买1'+$("#unit").text()+'!');
				    return;
			   }
			   if (f==0 && parseInt($("#num").val())>0){
				   $("#num").val(parseInt($("#num").val())-1);
				   return;
			   }
			   if (f==1 && (parseInt($("#num").val())+1)>parseInt($("#stock").text()))
			   {
			    alert('对不起，您最多只能购买'+$("#stock").text()+$("#unit").text()+'!');
			    return;
			   }else{
				   $("#num").val(parseInt($("#num").val())+1) ;
			   }
			 },
			 buynums:function(type,proid,attrid,cartid,buynum,f){
				if (buynum!=0){$("#Q_"+cartid)[0].disabled=true;}
				var x="/inc/ajax_shop.asp?getcartstock/"+type+"/"+buynum+"/"+cartid+"/"+f+"/"+proid+"/"+attrid+".html"
				$.get(x+Math.random(),function(s){
				  s=unescape(s);
				  if (buynum!=0){$("#Q_"+cartid)[0].disabled=false;}
				   if (s!='succ'){
					  alert(s.split('|')[1]);
					  return;
				   }
					 else
					 {
					  if (f==0)
						{
						 $("#Q_"+cartid).val(parseInt($("#Q_"+cartid).val())-1);
					  }
						else if(f==1)
						{
						 $("#Q_"+cartid).val(parseInt($("#Q_"+cartid).val())+1);
						}else{$("#Q_"+cartid).val(parseInt($("#Q_"+cartid).val()));}
					  shop.changecart(cartid);
				   }
				});

			 },
			 changecart:function(cartid){
				 var price=$("#hidmyprice"+cartid).text();
				 var score=$("#hidmyscore"+cartid).text();
				 var num=parseInt($("#Q_"+cartid).val());
				 $("#myprice"+cartid).html((price*num).toFixed(2));
					  $("#myscore"+cartid).html(parseInt(score*num));//bug改动
					  var totalmyprice=0;
					  var totalmyscore=0;
					  $("#shoppingtable").find("SPAN[name=totalmyprice]").each(function(){
							totalmyprice+=parseFloat($(this).text());															
					  });
					  $("#shoppingtable").find("SPAN[name=totalmyscore]").each(function(){
							totalmyscore+=parseInt($(this).text());															
					  });
					 $("#totalprice").html(totalmyprice.toFixed(2));
					 $("#totalscore").html(parseInt(totalmyscore));//bug改动
			 },
			 getAttr:function(obj,i,a,l,num,ids){
				 if (obj.innerHTML.indexOf('IMG')==-1){
					$("#attr"+i).val(obj.innerHTML.replace(/<I><\/I>/g,"")); 
				 }else{
					$("#current_img").attr("src",$("#"+obj.id).find("IMG").attr("src"));
					$("#proimg").attr("href",$("#"+obj.id).find("IMG").attr("src"));
					$("#attr"+i).val($("#"+obj.id).find("IMG").attr("title"));
				 }
			  if (i==1){
			   $("#attr2").val('');
			   $("#attr3").val('');
			  }else if(a==3&&i==2){
			   $("#attr3").val('');
			  }
			  
			  for(var k=0;k<=l;k++)
			  {
			   $("#att"+i+k)[0].className='txt';
			  }
			  obj.className='curr';
			  
			  if (i==a){
               $("#attrid").val(ids);
			   $('#vipprice').html('￥' +itemattr[ids][3]+ '元');
			   $.getScript(dir+'shop/ajax.getdate.asp?action=getstock&attrid='+ids,function(){
				    $("#stock").html(data.amount);
					if (parseInt($("#num").val())>parseInt($("#stock").text())){
					 $("#num").val($("#stock").text());
				    }
				});

			  }else{
               $("#attrid").val('');
			  }
			  
			  if (num!=0&&ids!=undefined){
				var str2='';
				var str22='';
				var idsarr=ids.split(",");
				var maxprice=minprice=0;
				for(var k=0;k<idsarr.length;k++){
					 var id=idsarr[k];
					 var itemvalue=itemattr[id][1];
					 if (num==3){
						itemvalue=itemattr[id][2]; 
					 }
					  str2+=itemvalue+"^"+id+",,,";
					  if (str22.indexOf(itemvalue)==-1){
					    str22+=itemvalue+",,,";
					  }
					if (parseFloat(itemattr[id][3])<minprice||minprice==0){minprice=itemattr[id][3];}
					if (parseFloat(itemattr[id][3])>maxprice||maxprice==0){maxprice=itemattr[id][3];}
				}
				if (minprice==maxprice){
				   	$('#vipprice').html('￥' +minprice+ '元');
				}else{
					$('#vipprice').html('￥'+ minprice+'元~￥' +maxprice+'元');
				}
					
				str22arr=str22.split(',,,');
				str2arr=str2.split(',,,');
				var new2str='';
				for(var i=0;i<str22arr.length-1;i++){
				      var ids2="";
					  for(var k=0;k<str2arr.length-1;k++){
					    if (str22arr[i]==str2arr[k].split('^')[0]){
						  if (ids2==""){
						   ids2=str2arr[k].split('^')[1];
						  }else{
						   ids2=ids2 + "," +str2arr[k].split('^')[1]
						  }
						}
					  }
					  if (i==0){
					   new2str=str22arr[i] + "^" + ids2;
					  }else{
					   new2str=new2str + ",,," + str22arr[i] + "^" + ids2;
					  }	
				}
				
				new2str=new2str.split(',,,');
				var strs="<span id='attrname"+num+"'>"+ myitemname[num-1]+ "：</span>";
				var vlan=new2str.length-1;
				for(var k=0;k<=vlan;k++){
					
					itemvalue=new2str[k].split("^")[0];
					var itemids=new2str[k].split("^")[1];
					var itemcss='txt';
					
					var iiarr=itemvalue.split('|')
					var vv='';
					if (iiarr[1]!=''){
						vv="<img src='" + iiarr[1] +"' width='25' height='25' title='" + iiarr[0] +"'/>";
					}else{
					    vv=iiarr[0];
					}
					if (num==2){
					strs+='<span id="att'+num+k+'" class="'+itemcss+'" onclick="shop.getAttr(this,2,'+a+','+vlan+',3,\''+itemids+'\')">'+vv+'<i></i></span> ';
					}else{
					strs+='<span id="att'+num+k+'" class="'+itemcss+'" onclick="shop.getAttr(this,3,'+a+','+vlan+',0,'+itemids+')">'+vv+'<i></i></span> ';
					}
				}
				//alert(strs);
			    jQuery("#showattr"+num).html(strs);
               // alert(str22);
			  }
			  var attr='';
			  var attrs='';
			  for(var k=0;k<=a;k++)
			  {
			   if ($("#attr"+k).val()!='' && $("#attr"+k).val()!=undefined){
			    if (attr==''){
			      attr='“'+$("#attr"+k).val()+'”';
				  attrs=$("#attrname"+k).html()+$("#attr"+k).val();
			     }else{
			      attr+=',“'+$("#attr"+k).val()+'”';
				  attrs+='  '+$("#attrname"+k).html()+$("#attr"+k).val();
				 }
			   }
			  }
			  $("#AttributeCart").val(attrs);
			  $("#buyselect").html("<b>已选择：<font color=brown>"+attr+"</font></b>");
			 },
			 addCart:function(ev,id,a){
			  if (shop.check(a)){
			    var p=new KesionPopup().mousepop("<img src='"+imagesPath+"img/product/suc.gif' align='absmiddle'><span style='font-size:14px;color:#000;'>已成功添加到购物车！</span>","<form name='paymentform' action='/product/index.asp?shop/2.html' method='post'><div id='cartShow'></div><div class='jrgwc'><input type='image' src=\'"+imagesPath+"img/product/hesuan.gif\'> <a href=\'/product/index.asp?shop.html\'><img src=\'"+imagesPath+"img/product/chakangouwuche.gif\'></a> <a href=\'javascript:closeWindow()\'><img src=\'"+imagesPath+"img/product/jixugouwu.gif\'></a></form></div>",450);
				var kbid='';
				$("input[name=Bundid]").each(function(){
					if ($(this)[0].checked){
						kbid+=','+$(this)[0].value;
					}
				 });
				var x='/inc/ajax_shop.asp?kbid='+kbid+'&id='+id+'&attrid='+$("#attrid").val()+'&action=addCart&num='+$("#num").val()+'&AttributeCart='+escape($("#AttributeCart").val())
				jQuery.getScript(x,function(){
				  if (data.flag=='error'){
						    alert('对不起，您没有登录!');
					}else if (data.flag=='error1'){
						    alert('对不起，您所在的用户级别不能购买本商品!');
					   }else{
				        $("#cartShow").html(unescape(data.str));
					   }																														                 
				 });
				
			   }
			 }
 }
