////////////////////////////////////////////////
//this[i*3+0] 代表商品ID ;this[i*3+1] 代表经销商;
//this[i*3+2] 代表数量; tthis[i*3+3] 代表商品类型;
////////////////////////////////////////////////
var newChildWindowHandler = Object();
function product_data()
{
	var indx=0;
	this.length=100*4
	for(var x=0;x<400;x++)
			this[x]="0";
	this.insert=insert;
	this.SetCookies=SetCookies;
	this.LoadCookies=LoadCookies;
	this.SetCount=SetCount;
	this.GetCount=GetCount;
	this.empty_merchant=empty_merchant;
}
function del_merchant(mId)
{
	s_product = new product_data();
	s_product.LoadCookies();
	s_product.empty_merchant(mId);
	s_product.SetCookies();
}
function empty_merchant(mId)
{
	for(var i=0;i<100;i++)
	{
		if(this[i*4+1]==mId)
		{
			this[i*4+2] = 0;
		}
	}	
}
function GetCount()//得到商品数量
{
	var x=0
	for(var i=0;i<100;i++)
	{
		if(this[i*4+2]>0)
		{
			x += this[i*4+2]*1
		}
	}
	return x; 
}

function SetCount(sequence,count)//修改商品数量
{
	this[sequence*4+2]=count;
}
function insert(pId,mId,type)//插入新商品
{
	var s_null=0;
	for(var i=0;i<100;i++)//如果已经存在,数量加一
	{
		if(this[i*4]==pId && this[i*4+1]==mId && this[i*4+3]==type)
		{
			this[i*4+2]++;
			return true;
		}
	}
	for(i=0;i<100;i++) //如果没有该商品，查找一个空位置插入
	{
		if(this[i*4+2]==0)
		{
				this[i*4+0]=pId;
				this[i*4+1]=mId;
				this[i*4+2]=1;
				this[i*4+3]=type;
				return true;
		}
	}
	return false;
}
function SetCookies()//把数组写入cookie
{
	var tr,str,xtr="";
	var s_b,s_e;
	for(i=0;i<100;i++)// 冒泡排序
   {
		for(j=0;j<(100 - i);j++)
		{
			 if(this[j*4+1]<this[(j+1)*4+1])
			 {
			 	 for(k=0;k<4;k++)
				 {
				 	  temp = this[j*4+k];
					  this[j*4+k] = this[(j+1)*4+k]; 
					  this[(j+1)*4+k] = temp;	
				 }
			 }
		}
	}
	for(var i=0;i<100;i++)
	{
		if(this[i*4+2]!=0)
		{
			xtr+=this[i*4];
			xtr+=",";
			xtr+=this[i*4+1];
			xtr+=",";
			xtr+=this[i*4+2];
			xtr+=",";
			xtr+=this[i*4+3];
			xtr+=",";
		}
	}    
	xtr="ec_basket_tr="+xtr;
	xtr+="ec_basket_trend;path=/;";
	xtr+="domain=3qu.com;";
	document.cookie=xtr;
}
function LoadCookies()//把cookie读入数组
{
	var tr=document.cookie,str,xtr="";
	var s_b,s_e;
	s_b=tr.search("ec_basket_tr=");
	s_e=tr.search("ec_basket_trend");
		
	if(s_b>=0&&s_e>=0)
	{
		str=tr.substring((s_b+13),s_e);
		ar=str.split(",");
		for(var i=0;i<ar.length;i++)
			this[i]=ar[i];
	}
	
	tr="ec_basket_tr=;path=/;expires=Wednesday, 09-Nov-04 12:12:40 GMT;";
    
	document.cookie=tr;
}
function buyit(pId,mId,type) 
{
	s_product = new product_data();
	s_product.LoadCookies();
	s_product.insert(pId,mId,type);
	s_product.SetCookies();
	var dt = (new Date()).getTime();
	childWindowHandler = window.open('/mall/pop_basket.php?temp='+dt,'buy','toolbar=no,menubar=no,status=yes,scrollbars=no,resizable=yes,width=332,height=187,dependent=yes');
	//window.open('/basket.php?temp='+dt,'buy','toolbar=no,menubar=no,status=yes,scrollbars=no,resizable=yes,width=330,height=160')

}
function Modifyit(sequence,count) 
{
	s_product = new product_data();
	s_product.LoadCookies();
	s_product.SetCount(sequence,count);
	s_product.SetCookies();
}
function count() 
{
	s_product = new product_data();
	s_product.LoadCookies();
	s_product.SetCookies();
	return s_product.GetCount();
}


