﻿//CRIOnline 静静的黎明

HttpCookie = function(name)
{
	//
	// 属性
	//
	this.__name;
	this.__isExisted;
	this.__expires;
	this.__value;
	this.__path;
	this.__domain;
	
	//
	// 公有属性:values; NameValueCollection;
	// 
	this.values;

	//
	// 判定当前的 HttpCookie 实例是否存在。
	//
	this.isExisted = function()
	{
		return this.__isExisted;
	}

	//
	//  判断当前的cookie是否具有子键。
	//
	this.hasKeys = function()
	{
		return this.values.hasKeys();
	}

	//
	// 私有方法,分析cookie串
	//
	this.__analysisCooString = function(cstring)
	{
		var subCookies = cstring.split("&");
		
		for(var k = 0, l = subCookies.length; k < l; k++)
		{
			var key, values, splitPos;
			
			splitPos = subCookies[k].indexOf("=");

			if(splitPos != -1)
			{
				key   = subCookies[k].substring(0, splitPos);
				values = subCookies[k].substring(splitPos + 1);
				values = values.split(",");
				
				//插入键值
				for(var m = 0, n = values.length; m < n; m++)
				
					this.values.add(key, values[m]);
			}
			else
			{
				this.__value = 	subCookies[k];
			}				 
		}		
	}

	//
	// 私有方法，初始化 cookie 。
	// 如果以参数 name 为名字的cookie存在，则导入该cookie；
	// 调用isExisted 方法则返回 true; 否则初始化一个新实例；调用
	// isExisted()返回 false。
	//
	this.__init = function(name)
	{		
		if(typeof NameValueCollection != "function")
			throw new Error("NameValueCollection");
		
		if(typeof name != "string")
			throw new Error("Invalid type on HttpCookie's argument");

		this.__name = (name != null)? name : "";
		this.__value = "";
		this.__isExisted = false;
		this.__expires = null;
		this.__path = "/";
		this.values = new NameValueCollection();

		var cookies = document.cookie.split("; ");

		for(var i = 0, j = cookies.length; i < j; i++)
		{
			var name, value, splitPos;

			splitPos = cookies[i].indexOf("=");
	
			if(splitPos != -1)
			{
				name  = cookies[i].substring(0, splitPos);
				value = cookies[i].substring(splitPos + 1);
			}
			else
			{
				name = cookies[i];
			}
			
			if(this.__name.toLowerCase() == name.toLowerCase())
			{
				this.__isExisted = true;
				
				if(value == null) return;
				
				this.__analysisCooString(value);
				
				break;
			}
		}			
	}

	//
	// 返回当前HttpCookie实例的名字。
	//
	this.getName = function()
	{
		return this.__name;
	}
	
	//
	// 设定当前 HttpCookie 实例的值
	//	
	this.setValue = function(value)
	{
		if(value != null)
		{
			this.__value = value.toString();
			this.values = new NetBlog.NameValueCollection();
		}
	}

	//
	// 返回 当前 HttpCookie 实例的值
	//
	this.getValue = function()
	{
		var value = new String();

		if(this.values.hasKeys())
		{
			var keys = this.values.allKeys();

			for(var i = 0, j = keys.length; i < j; i++)
			{
				value += "&" + keys[i] + "=" + this.values.get(keys[i]);
			}
		}
		
		return  this.__value + ((this.__value == "")? value.substring(1) : value);

	}



	//
	// 设定当前 HttpCookie 实例的过期时间；以秒为单位
	// 当该方法未被调用时，实例默认跟随窗体存活期。
	//
	this.setExpires = function(secs)
	{
		if(typeof secs == "number")
		{
			var sysExpiresTime = new Date().getTime() + parseInt(secs) * 1000;

			this.__expires = new Date(sysExpiresTime).toGMTString();
		}
	}

	this.setDomain = function(domain)
	{
		if(domain != null)
		this.__domain = domain.toString();
	}

	//
	// 设定当前 HttpCookie 实例的的有效跟目录。
	// 
	this.setPath = function(path)
	{
		if(path != null)
			this.__path = path.toString();
	}	

	//
	// 将当前的HttpCookie实例配置写入 cookie。
	//
	this.save = function()
	{
		document.cookie = this.__name + "=" + this.getValue() + ";" + 
			((this.__expires != null)? "expires=" +	this.__expires + ";" : "") + 
			((this.__domain != null)? "domain=" + this.__domain + ";" : "") + 
			"path=" + this.__path + ";";

	}
	//初始化
	this.__init(name);		
}