//记录商品信息
function SetCateInfo(cateid,desc,price){
	this.cateid = cateid;
	this.desc = desc;
	this.price = price;
}

//初始化Cookie信息
function InitCookieInfo(name,expires,path){
	//cookie名称
	if(name=='' || name==null)
		this.name = "HisCate";		
	else
		this.name = name;
	
	//cookie过期时间
	if(expires=='' || expires==null){
		var today = new Date();
		this.expires = new Date(today.getTime() + 7 * 24 * 60 * 60 * 1000);	//cookie过期时间
	}else
		this.expires = expires;

	if(path=='' || path==null)
		this.path = '/';
	else
		this.path = path;
}

//记录商品信息类
function CateCookieCls(ci,cookie){
	//设置Cookie
	this.SetCookie = function(){
		var gc = this.GetCookie();
		if(gc == null)
			gc = '';
		
		if(gc.indexOf(ci.cateid) == -1)
		{
			//堆栈
			var arrList = gc.split("||");
			var len=arrList.length;
			if(len >= ci.maxLen){
				gc = gc.replace('||' + arrList[ci.maxLen],'');
			}
			
			if(gc != null && gc != '')
				gc = '||' + gc;

			var curCookie = cookie.name + "=" + escape(CodeCookie(ci.cateid + "|" + ci.desc + "|" + ci.price + gc))+" ; expires=" + cookie.expires.toGMTString() + " ; path=" + cookie.path; 
			document.cookie = curCookie;
		}
	}
	//获取Cookie
	this.GetCookie = function(){
		var strArg= cookie.name + "="; 
		var nArgLen=strArg.length; 
		var nCookieLen=document.cookie.length; 
		var nEnd; 
		var i=0; 
		var j; 

		while (i<nCookieLen) 
		{ 
		　	j=i+nArgLen; 
			if (document.cookie.substring(i,j)==strArg){ 
		　		nEnd=document.cookie.indexOf (";",j); 
		　		if (nEnd==-1) nEnd=document.cookie.length; 
		　		return DecodeCookie(unescape(document.cookie.substring(j,nEnd))); 
			} 
		　	i=document.cookie.indexOf(" ",i)+1; 
		　		if (i==0) break; 
		} 
		return null; 
	}
}

//加密
function CodeCookie(str)
{
	var strRtn="";
	for (var i=str.length-1;i>=0;i--)
	{
		strRtn+=str.charCodeAt(i);
		if (i) strRtn+=","; //用,作分隔符 
	}
	return strRtn;
} 

//解码程序： 
function DecodeCookie(str)
{
	var strArr;
	var strRtn="";
	strArr=str.split(",");
	try{
	for (var i=strArr.length-1;i>=0;i--)
		strRtn+=String.fromCharCode(eval(strArr[i]));
	}catch(e){
	}
	return strRtn; 
}

/************测试数据**************
var ci = new SetCateInfo("999008","神舟行1502元","0.02");
var cookie = new InitCookieInfo();
var cookieObj = new CateCookieCls(ci,cookie);
cookieObj.SetCookie();
var str = cookieObj.GetCookie();
alert(str);
*********************************/
