﻿//CRIOnline 静静的黎明

if(NetBlog == null)var NetBlog = {};
//
//
//
NetBlog.NameValueCollection = NameValueCollection = function()
{
	this.__keys = new Array();
	this.__values = new Object();

	this.__checkArg = function()
	{
		for(var i = 0, j = arguments.length; i < j; i++)
		{
			if(typeof arguments[i] != "string")
				return false;
		}
		return true;
	}

	//
	// 返回包含当前实例的所有键的字符数组
	//
	this.allKeys = function()
	{
		return this.__keys.concat();
	}


	//
	// 获取与 NameValueCollection 中的指定键关联的值(字符数组)。
	//
	this.getValues = function(name)
	{
		if(!this.__checkArg(name))
		{
			throw new Error("Invalid type on NameValueCollection.getValues's argument.");
		}
	
		var values = this.__values[name.toLowerCase()];
		
		return (values instanceof Array)? values : null;
	}
	
	//
	// 将具有指定名称和值的项添加到 NameValueCollection。
	//
	this.add = function(name, value)
	{
		if(!this.__checkArg(name, value))
		{
			throw new Error("Invalid type on NameValueCollection.add's argument");
		}

		var key = name.toLowerCase();

		if(this.__values[key] == null)
		{
			this.__keys[this.__keys.length] = name;
			this.__values[key] = new Array(value);
		}
		else
		{
			this.__values[key][this.__values[key].length] = value;
		}
	}

	//
	// 获取与 NameValueCollection 中的指定键关联的值，
	// 如果对应多个值，以逗号分割，参考getValues。
	//
	this.get = function(name)
	{
		if(!this.__checkArg(name))
		{
			throw new Error("Invalid type on NameValueCollection.get's argument");
		}

		var value = this.__values[name.toLowerCase()];

		return (value instanceof Array)? value.join(",") : null;
	}

	//
	// 设置或改变(该项不为空)NameValueCollection 中某个项的值。
	//
	this.set = function(name, value)
	{
		if(!this.__checkArg(name, value))
		{
			throw new Error("Invalid type on NameValueCollection.set's argument");
		}

		var key = name.toLowerCase();

		if(this.__values[key] == null)
		{
			this.__keys[this.__keys.length] = name;
		}

		this.__values[key] = new Array(value);
	}
	
	this.hasKeys = function()
	{
		return this.__keys.length > 0;
	}
	//
	// 清空NameValueCollection的所有键值。
	//
	this.clear = function()
	{
		this.__keys = new Array();
		this.__values = new Object();
	}

	//
	// 将具有指定键的项从当前实例中删除。
	//
	this.remove = function(name)
	{
		if(!this.__checkArg(name))
		{
			throw new Error("Invalid type on NameValueCollection.remove's argument");
		}

		var key = name.toLowerCase();

		if(this.__values[key] == null)
			return;

		for(var i = 0, j = this.__keys.length; i < j; i++)
		{
			if(this.__keys[i] == key)
			{
				this.__keys.splice(i, 1);
				this.__values[key] = null;
				return;
			}
		}		
	}	
}