//把对比商品的ID号加入cookie中，最大长度为4.
var MaxVisit = 4;

Array.prototype.remove=function(dx)
{
    if(isNaN(dx)||dx>this.length){return false;}
    for(var i=0,n=0;i<this.length;i++)
    {
        if(this[i]!=this[dx])
        {
            this[n++]=this[i]
        }
    }
    this.length-=1
}

function AddCompare(ProductID)
{    
    var OldVisit = null;
    var Item = new Array();  
      
    Item.push(ProductID); 
    
    if(GetCookie("LasteVisit"))
    {
        Item.push("|");
        OldVisit = GetCookie("LasteVisit").split("|");
        
        for(var i=0; i<OldVisit.length; i++)
        {
            if(OldVisit[i].indexOf(ProductID) != -1)
            {
                OldVisit.remove(i);i--;
            }
        }
        
        while(OldVisit.length >= MaxVisit)
        {
            OldVisit.remove(0);
            
        }         
        Item.reverse();

       if(OldVisit.length >1)
        for(var i=0; i<OldVisit.length-1; i++)
            OldVisit[i] +="|";
        
        OldVisit.push(Item.join(""));   
        SetCookie("LasteVisit", OldVisit.join(""));
    }   
    else
    {
        Item.reverse();
        SetCookie("LasteVisit", Item.join(""));
    }
}

function GetProductIDS()
{
    var OldVisit = GetCookie("LasteVisit");
    
    if(!OldVisit)
        return "";
    else
    {               
        return OldVisit;
    }
}

//设置Cookie的值 
function SetCookie(key, value)
{
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var now = new Date();
	now.setDate(now.getDay() + 365);
	var expires = now;//(2 < argc) ? argv[2] : null;
	var path = (3 < argc) ? argv[3] : null;
	var domain = (4 < argc) ? argv[4] : null;
	var secure = (5 < argc) ? argv[5] : false;
	var cookieValue =  key + '=' + escape (value)
		+ ((expires == null) ? '' : ('; expires=' + expires.toGMTString()))
		+ ((path == null) ? '' : ('; path=' + path))
		+ ((domain == null) ? '' : ('; domain=' + domain))
		+ ((secure == true) ? '; secure' : '');
	document.cookie = cookieValue;
}

//得到Cookie的值
function GetCookie(key)
{
	var arg = key + '=';
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while( i < clen )
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		{
			var offset = j;
			var endstr = document.cookie.indexOf (';', offset);
			if (endstr == -1)
			{
				endstr = document.cookie.length;
			}
			return unescape(document.cookie.substring(offset, endstr));
		}
		i = document.cookie.indexOf(' ', i) + 1;
		if ( i==0 ) break;
	}
	return null;
}
