
  var noExpires = new Date();
  noExpires.setTime(noExpires.getTime() + 60*60*1000*24*60 );
  
  var attributeSplit="\t";
  var itemSplit="\n";
    

  function CookieBuilder(name,maxItemNum,expires){
       var index;
       this.name=name;
       this.maxItemNum=maxItemNum;
       this.expires=expires;
       var value=getCookie(this.name);
 	   var s="";
	   if (value!=null){
		   var aId = value.split(itemSplit); 
		   if (aId.length>0){
		      index=parseInt(aId[0]);
		      if (isNaN(index)) index=0;
		   }
		   else{
		      index=0;
		   }
		   if (index>=maxItemNum) index=0;
	   }
	   else{
		   index=0;
	   }
	

	   this.addItemToCookie=function(item){
	        index=addItemToCookieByIndex(index,this.name,item,this.maxItemNum,this.expires);
	       
	    }
       this.getCookie=function(){
           return getTrueCookie(this.name);
       }
       this.getFullCookie=function(){
           return getCookie(this.name);
       }
       this.deleteCookie=function(){
           index=0;
           return deleteCookie(this.name);
       }
       
       this.getIndex=function(){
           return index;
       }
       
          
   }
   
   
   function getTrueCookie(name)
   {
        var value=getCookie(name);
        if (value!=null){
            firstSplitIndex=value.indexOf(itemSplit);
            if (firstSplitIndex!=-1){
               return value.substr(firstSplitIndex+itemSplit.length);
            }
        }
        return value;
    }
    
    
    function addIndexToCookieValue(index,value){
       if (index!=null&&value!=null&&value.length>0){
           return index.toString()+itemSplit+value;
       }
       return value;
    
    }
    
   
       
       

	/**
	 * Read the JavaScript cookies tutorial at:
	 *   http://www.netspade.com/articles/javascript/cookies.xml
	 */
	
	/**
	 * Sets a Cookie with the given name and value.
	 *
	 * name       Name of the cookie
	 * value      Value of the cookie
	 * [expires]  Expiration date of the cookie (default: end of current session)
	 * [path]     Path where the cookie is valid (default: path of calling document)
	 * [domain]   Domain where the cookie is valid
	 *              (default: domain of calling document)
	 * [secure]   Boolean value indicating if the cookie transmission requires a
	 *              secure transmission
	 */
	function setCookie(name, value, expires, path, domain, secure)
	{   
	   
         s=name + "=" + escape(value) +
	     ((expires) ? "; expires=" + expires.toGMTString() : "") +
	     ((path) ? "; path=" + path : "; path=/") +
	     ((domain) ? "; domain=" + domain : "; domain=alibaba.com") +
	     ((secure) ? "; secure" : "");
	     document.cookie=s; 
	}
	
	/**
	 * Gets the value of the specified cookie.
	 *
	 * name  Name of the desired cookie.
	 *
	 * Returns a string containing value of specified cookie,
	 *   or null if cookie does not exist.
	 */
	function getCookie(name)
	{
	    var dc = document.cookie;
	    var prefix = name + "=";
	    var begin = dc.indexOf("; " + prefix);
	    if (begin == -1)
	    {
	        begin = dc.indexOf(prefix);
	        if (begin != 0) return null;
	    }
	    else
	    {
	        begin += 2;
	    }
	    var end = document.cookie.indexOf(";", begin);
	    if (end == -1)
	    {
	        end = dc.length;
	    }
	    return unescape(dc.substring(begin + prefix.length, end));
	}
	
	/**
	 * Deletes the specified cookie.
	 *
	 * name      name of the cookie
	 * [path]    path of the cookie (must be same as path used to create cookie)
	 * [domain]  domain of the cookie (must be same as domain used to create cookie)
	 */
	function deleteCookie(name, path, domain)
	{
	    if (getCookie(name))
	    {
	        document.cookie = name + "=" + 
	            ((path) ? "; path=" + path : "") +
	            ((domain) ? "; domain=" + domain : "") +
	            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	    }
	}
	
	
	function addItemToCookie(name,ItemId,maxItemNum){
	     var value=getCookie(name);
	     var s=ItemId;
	     if (value!=null){
		     var aId = value.split(itemSplit); 
		     var k=1;
		     var n= containElem(aId,s);
		     for (var i=0; i < aId.length; i++) 
		     {
		       if (i==n){ continue;}
		        s=s+","+aId[i];
		        k=k+1;
		        if (k>=maxItemNum)break;
		     }
	     }
	     setCookie(name,s);
	}
	
	
	
	function addItemToCookieByIndex(index,name,ItemId,maxItemNum,expires){
		 var value=getTrueCookie(name);
	     var s="";
	     if (value!=null){
			 var aId = value.split(itemSplit); 
			 var k=1;
			 var n= containElem(aId,ItemId);
			 if (n!=-1) return index;
			 if (index>maxItemNum -1) index=0
			 aId[index]=ItemId;
			 for (var i=0; i < aId.length; i++) 
			 {
			    if (i==n){ continue;}
			    s=s+itemSplit+aId[i];
			    if (i>=maxItemNum - 1 )break;
			  }
			    s=s.substr(itemSplit.length);      
	     }
	     else{
	       s=ItemId;
	     }
	     s=addIndexToCookieValue(index+1,s);
	     setCookie(name,s,expires);
	     return index+1;
	}
	
	
	 function containElem(arrayObj,elem){
	   for (var i=0; i < arrayObj.length; i++) {
	       if (elem==arrayObj[i]) return i;
	  
	  }
	  return -1;
	 }




