﻿/// <reference name="MicrosoftAjax.debug.js" />

Type.registerNamespace('Solarweb.Common.Collections');

Solarweb.Common.Collections.Hashtable = function()
{    
  /// <returns type="Hashtable">A hashtable object</returns>
  
  /// <field name="Items" type="Object" integer="false">Associative object array of hashtable items</field>  

  // ctor
  Solarweb.Common.Collections.Hashtable.initializeBase(this);
  
  // fields    
  this._items = {};   
  this._keys = new Array(); 
  this._count = 0;        
}

Solarweb.Common.Collections.Hashtable.prototype =
{
  initialize : function()
  {            
    Solarweb.Common.Collections.Hashtable.callBaseMethod(this, 'initialize');
  },
  dispose : function()
  {
    Solarweb.Common.Collections.Hashtable.callBaseMethod(this, 'dispose');
  },  
  
  // public functions
  add_item : function(strKey, varObject)
  {
    /// <returns></returns>
    var _keys = new Array();
  
    if( strKey !== null && varObject !== null)
    {  
        if(this._items[strKey] === undefined)   
        {                                                                      
            this._items[strKey] = varObject;
            this._keys.push(strKey);            
            this._count++;
        }
        else            
        {
            var varDupKeyError = Error.create("Cannot insert duplicate keys");            
            throw(varDupKeyError);
        }
    }
    else
    {
        if(strKey === null)
            throw Error.argumentNull('strKey');
        else
            throw Error.argumentNull('varObject');                    
    }
  },
  remove_item : function(strKey)
  {
    /// <returns></returns>
    delete this._items[strKey];
    Array.remove(this._keys, strKey);
    this._count--;
  },
  get_item : function(strKey)
  {
    /// <returns type="Object" integer="false" mayBeNull="false">Hashtable item value</returns>
    if(this._items[strKey] !== undefined)
      return this._items[strKey];
    else
    {
      var varItemDoesntExist = Error.create("Item does not exist");
      throw(varItemDoesntExist);
    }      
  },
  get_items : function()
  {
    /// <returns type="Object" integer="false" mayBeNull="false">returns our associative object array - index using keys</returns>
    return this._items;
  },
  update_item : function(strKey, varObject)
  {  
    /// <returns></returns>
    if(strKey !== null && varObject !== null)
    {
        this._items[strKey] = this._items[strKey];
        if(this._items[strKey] !== undefined)
            this._items[strKey] = varObject;
    }
    else
    {
      if(strKey === null)      
        throw Error.argumentNull('strKey');
      else
        throw Error.argumentNull('varObject');
    }     
  },
  contains_item : function(strKey)
  {
    /// <returns type="bool">Does key exist in hashtable</returns>
    if(this._items[strKey] !== undefined)
      return true;
    else
      return false;
  },  
  get_first_key_with_value : function(objValue)
  {   
    /// <returns type="String">name of key</returns> 
    var arrKeys = new Array();
    for(var strKey in this._keys)
    {     
      if(this._items[strKey] === objValue)
        return strKey;      
    }
    return null;
  },
  get_all_values : function()
  {
    /// <returns type="Array">Array of all values</returns>
    var arrValues = new Array();
    for(var i in this._items)
    {
      if( this._items[i] !== null )
        arrValues.push(this._items[i]);
    }
    return arrValues;
  },
  get_all_keys : function()
  {
    /// <returns type="Array">Array of all keys</returns>
    var arrKeys = new Array();
    for(var i in this._keys)
    {     
      arrKeys.push(this._keys[i]);
    }
    return arrKeys;
  },
  get_count : function()
  { 
    /// <returns type="Number" integer="true">Count of items in hashtable</returns>
    return this._count;    
  }
}

Solarweb.Common.Collections.Hashtable.registerClass(
            'Solarweb.Common.Collections.Hashtable', Sys.Component);
                
if( typeof(Sys) !== 'undefined' )
  Sys.Application.notifyScriptLoaded();