﻿/****************************************
*               RCKeyValueCollection
*****************************************/

/// <summary>
/// Richer Components - Key Value Collection Class
/// </summmary>
function RCKeyValueCollection()
{
    this._keys = new Array();
    this._values = new Array();
    this._size = 0;
} // RCKeyValueCollection

/// <summary>
/// Richer Components - Key Value Collection Class Prototype
/// </summary>
RCKeyValueCollection.prototype = 
{
    /// <summary>
    /// Returns the size of the collection
    /// </summary>
    GetSize : function()
    {
        return this._size;
    }, // GetSize
    
    /// <summary>
    /// Check if the collection contains 
    /// the given key
    /// </summary>
    ContainsKey : function( key )
    {
        var exist = false;
        
        for (var i = 0; i < this.GetSize() && ! exist; i++)
		    if ( this._keys[i] == key )
			    exist = true;
        
        return exist;
    }, // ContainsKey
    
    /// <summary>
    /// Check if the collection contains 
    /// the given value
    /// </summary>
    ContainsValue : function( value )
    {
        var exist = false;
        
        for (var i = 0; i < this.GetSize() && ! exist; i++)
		    if ( this._values[i] == value )
			    exist = true;
        
        return exist;
    }, // ContainsValue
    
    /// <summary>
    /// Add a new key and value to the collection
    /// </summary>
    Add : function( key, value )
    {
        this._keys[ this._size ] = key;
        this._values[ this._size ] = value;
        this._size++;
    }, // Add
    
    /// <summary>
    /// Returns the value of the key
    /// </summary>
    Get : function( key )
    {
        var obj = null;
        var index = this.IndexOfKey( key );
        
        if ( index >= 0 )
            obj = this._values[index];
        
        return obj;
    }, // Get
    
    /// <summary>
    /// Returns the value on the index position
    /// </summary>
    GetValueAt : function( index )
    {
        var obj = null;
        
        if ( index >= 0 )
            obj = this._values[index];
        
        return obj;
    }, // GetValueAt
    
    /// <summary>
    /// Returns the key on the index position
    /// </summary>
    GetKeyAt : function( index )
    {
        var obj = null;
        
        if ( index >= 0 )
            obj = this._keys[index];
        
        return obj;
    }, // GetKeyAt
    
    /// <summary>
    /// Returns the index of the given key
    /// on the collection
    /// </summary>
    IndexOfKey : function( key )
    {
        var index = -1;
        
        for (var i = 0; i < this.GetSize() && index == -1; i++)
		    if ( this._keys[i] == key )
			    index = i;
        
        return index;
    }, // IndexOfKey
    
    /// <summary>
    /// Returns the index of the given value
    /// on the collection
    /// </summary>
    IndexOfValue : function( value )
    {
        var index = -1;
        
        for (var i = 0; i < this.GetSize() && index == -1; i++)
		    if ( this._values[i] == value )
			    index = i;
        
        return index;
    }, // IndexOfValue
    
    /// <summary>
    /// Remove the key and value located on the given index
    /// from the collection
    /// </summary>
    RemoveAt : function( index )
    {
        for (var i = index; i < this.GetSize() - 1; i++) 
        {
            this._keys[i] = this._keys[i + 1];
			this._values[i] = this._values[i + 1];
		}
			
		this._keys[this.GetSize() - 1] = null;
		this._values[this.GetSize() - 1] = null;
    }, // RemoveAt
    
    /// <summary>
    /// Remove the the given key from the collection as
    /// well as the value object
    /// </summary>
    Remove : function( key )
    {
        var index = this.IndexOfKey( key );
        
        if ( index >= 0 )
            this.RemoveAt( index );
    }, // Remove
    
    /// <summary>
    /// Clear the collection
    /// </summary>
    Clear : function()
    {
        this._size = 0;
        
        for (var i = 0; i < this.GetSize(); i++) 
        {
			this._keys[i] = null;
			this._values[i] = null;
		}
    }, // Clear
    
    /// <summary>
    /// To String
    /// </summary>
    ToString : function()
    {
        var stringValue = '';
        
        for (var i = 0; i < this.GetSize(); i++)
        {
            if ( i > 0 )
                stringValue += '\n';
                
            stringValue += this._keys[i];
            stringValue += ' : ';
            stringValue += this._values[i];
        }
        
        return stringValue;
    }, // ToString
    
    /// <summary>
    /// Performs a call on every value object on the collection.
    /// It calls the method only if the value object has a method 
    /// with the given name, otherwise does nothing.
    /// </summary>
    PerformCallOnValueObject : function( methodName )
    {
        for (var i = 0; i < this.GetSize(); i++)
        {
            //Call the method only if such method 
            //exists on the value object
            if ( eval('this._values[i].' + methodName) )
                eval('this._values[i].' + methodName + '();')
        }
    } // PerformCallOnValueObject
} // RCKeyValueCollection.prototype