
	/* JavaScript.Net											*/
	/* System.Collections.Specialized.js						*/
	/* An Implementation of the NameValueCollection Class		*/
	/* Last Modified by Cory Dambach June 2, 2006				*/
	/* Get() no longer throws an error when key doesn't exist	*/
	function NameValueCollection()
	{
		this.Keys		= new Array();
		this.Values		= new Array();
		this.Type		= "NameValueCollection";

		this.Add = function( Key, Value )
		{
			if( arguments.length == 2 )
			{
				var KeyIndex = this.IndexOfKey( Key );
				if( KeyIndex == -1 ) //The Key Doesn't exist
				{
					this.Keys.push		( Key	);
					this.Values.push	( Value	);
				}
				else
				{
					this.Values[KeyIndex] = this.Values[KeyIndex] + "," + Value;
				}
			}
			else if ( arguments.length != 2 )
			{
				throw( "Incorrect Number of arguments in " + this.GetType() + ":Add( Key, Value )" );
			}
			return;
		}

		this.Set = function( Key, Value )
		{
			if( arguments.length == 2 )
			{
				var KeyIndex = this.IndexOfKey( Key );
				if( KeyIndex == -1 ) //if key does not already exist
				{
					this.Keys.push	( Key   );
					this.Values.push( Value );
				}
				else
				{
					this.Values[KeyIndex] = Value
				}
			}
			else if ( arguments.length != 2 )
			{
				throw( "Incorrect Number of arguments in " + this.GetType() + ":Add( Key, Value )" );
			}
			return;
		}
		
		/* [Unsafe] */
		this.GetValues = function( Key )
		{
			var PairIndex = this.IndexOfKey( Key );
			if( PairIndex > -1 )
			{
				return this.Values[PairIndex].split(',');
			}
			else
			{
				return null;
			}
		}
		
		//Used to throw an error if the key did not exists
		//Changed it to mirrow the functionality of the .NET Framework
		//Now it returns null if the key does not exist
		this.Get = function( Key )
		{
			var PairIndex = this.IndexOfKey( Key );
			if( PairIndex > -1 )
			{
				return this.Values[PairIndex];
			}
			else
			{
				return null;
			}
		}
		
		this.RemoveKey = function( key )
		{
			var PairIndex = this.IndexOfKey( key );
			if( PairIndex > -1 )
			{
				 this.Values.splice( PairIndex, 1 ); //remove the keys
				 this.Keys.splice( PairIndex, 1 );//and the values for that entry
				 return;
			}
			return; //don't remove the key, cause it doesn't exist
		};
		
		this.GetKeys	= function()		{ return this.Keys;	}
		this.GetValue	= function( Key )	{ return this.Get( Key ); }
		this.GetKeyAt	= function( Index )	{ return this.Keys[Index]; }
		this.GetValueAt	= function( Index ) { return this.Values[Index]; }
		this.GetType	= function()		{ return this.Type; }
		
		this.URLEncode = function()
		{
			var QueryString = new String();
			for( var i = 0; i < this.Keys.length; i++ )
			{
				QueryString += encodeURIComponent( this.GetKeyAt(i) ) + "=" + encodeURIComponent( this.GetValueAt(i) );
				if( (i+1) != this.Keys.length )
				{
					QueryString = QueryString + "&";
				}
			}
			return QueryString;
		}
		
		this.ContainsKey = function( Key )		
		{
			for( var i = 0; i < this.Keys.length; i++ )
			{
				if( this.Keys[i] == Key )
				{
					return true;
				}
			}
			return false;
		}

		this.IndexOfKey = function( Key )
		{
			for( var i = 0; i < this.Keys.length; i++ )
			{
				if( this.Keys[i] == Key )
				{
					return i;
				}
			}
			return -1;
		}
		
		this.ToString = function()
		{
			var myKeys = this.GetKeys();
			var Result = new String();
			for( var i = 0; i < myKeys.length; i++ )
			{
				Result = Result + myKeys[i] + " = " + this.Get( myKeys[i] ) + "\n";
			}
			return Result;
		}
	}//End Class: NameValueCollection
	
	/* Static Methods */
	NameValueCollection.PrintNVC = function( pNVC )
	{
		var myKeys	= pNVC.GetKeys();
		var res		= "";
		for( var i = 0; i < myKeys.length; i++ )
		{
			res += myKeys[i] + " = " + pNVC.Get( myKeys[i] ) + "<br />";			
		}
		document.write( res );
	}
	
	NameValueCollection.CreateFromQueryString = function( QueryString )
	{
		var outNVC			= new NameValueCollection();
		var arrQueryString	= QueryString.split( '&' );
		for( var i = 0; i < arrQueryString.length; i++ )
		{
			var arrPair	= arrQueryString[i].split('=');
			var Name	= arrPair[0];
			var Value	= arrPair[1];			
			outNVC.Add( decodeURIComponent(Name), decodeURIComponent(Value) );
		}
		return outNVC;
	}