
//编码：陈绍鹏。功能描述：Cookie。
//-------------------------------------------------------------------------------------------------

//Ajax 表格选择物料
//使用示例: 
//var newCookie = new Cookie();
//提取
//newCookie.Load();
//保存
//newCookie.Save();
//添加
//newCookie.Add();
//修改
//newCookie.Modify();
//修改添加
//newCookie.ModifyAdd();
//删除
//newCookie.Delete()
//给出指定 Cookie 值
//newCookie.GetCookie()
//设置域
//newCookie.SetCookieDoMain()
//设置路径
//newCookie.SetCookiePath()
//给出 Cookie 数量
//newCookie.Count()
function WebCookie()
{
	this._Cookie = [];
	this._DoMain = "";
	this._Path = "/";

	//提取 Cookie
	this.Load = function()
	{
        if(document.cookie.length > 0)
        {
			var	cookie = document.cookie;
        	var items = cookie.split("; ");
        	var itemLength = items.length;
        	while(itemLength > 0)
        	{
        		var item = items[--itemLength].split("=");
        		var name = item[0];
        		var values = item[1].split(",");
        		this._Cookie[name] = [];
        		this._Cookie[name][0] = values[0];
        		this._Cookie[name][1] = unescape(values[1]);
        	}
            return true;
        }
        return false;
    }
    
    //保存 Cookie
	this.Save = function()
	{
        var key;
        for(key in this._Cookie)
        {
            if(!this._Cookie[key])return;
            var cookieInfo = "";
            cookieInfo += key + "=" + this._Cookie[key][0] + "," + escape(this._Cookie[key][1]) + ";";
            cookieInfo += "expires=" + new Date(parseInt(this._Cookie[key][0])).toGMTString() + ";";
            if(this._DoMain != "")
            {
            	cookieInfo += "domain=" + this._DoMain + ";";
            }
            //cookieInfo += "path=" + this._Path;
            document.cookie = cookieInfo;
        }
    }
    
    //添加 Cookie
    this.Add = function(name, value, days)
    {
    	try
    	{
    		if(this.GetCookie(name) != null)
    		{
				alert("名称为" + name + "的 Cookie 已经存在，添加失败。");
				return;
    		}
	    	if(days == null)
	    	{
	    		days = 36500;
	    	}
	        this._Cookie[name] = [];
	        this._Cookie[name][0] = new Date().getTime() + days * 86400000;
	        this._Cookie[name][1] = value;
        }
        catch(error)
        {
        	alert("在模块 this.Add 中发生如下错误：\n错误类型：" + error.name + "\n错误信息：" + error.message);
        }
    }
    
    //修改 Cookie
    this.Modify = function(name, value, days)
    {
    	try
    	{
    		if(this.GetCookie(name) == null)
    		{
				alert("名称为" + name + "的 Cookie 不存在，修改失败。");
				return;
    		}
	    	if(days != null)
			    this._Cookie[name][0] = new Date().getTime() + days * 86400000;
	        this._Cookie[name][1] = value;
	    }
        catch(error)
        {
        	alert("在模块 this.Modify 中发生如下错误：\n错误类型：" + error.name + "\n错误信息：" + error.message);
        }
    }
    
    //修改添加 Cookie 有则修改,没有则添加
    this.ModifyAdd = function(name, value, days)
    {
    	if(this.GetCookie(name) == null)
    	{
    		this.Add(name, value, days);
    	}
    	else
    	{
    		this.Modify(name, value, days);
    	}
    }
    
    //删除 Cookie
    this.Delete = function(name)
    {
        this._Cookie[name][0] = new Date().getTime();
    }
    
    //给出指定的 Cookie 的值
    this.GetCookie = function(name)
    {
    	try
    	{
    		if(this._Cookie[name] == null)
    			return null;
        	return this._Cookie[name][1];
        }
        catch(error)
        {
        	alert("在模块 this.GetCookie 中发生如下错误：\n错误类型：" + error.name + "\n错误信息：" + error.message);
        	return null;
        }
    }

	//设置 Cookie 的域
	
    this.SetCookieDoMain = function(domain)
    {
    	this._DoMain = domain;
    }

	//设置 Cookie 的路径    
    this.SetCookiePath = function(path)
    {
    	this._Path = path;
    }
    
    //返回 Cookie 的个数
    this.Count = function()
    {
        var count = 0, key;
        for(key in this._Cookie)
        {
        	count++;
        }
        return count;
    }
}

