/**
	//
	Modelled after Dictionary object in  VBScript
*/
/*
	The key, if an object, should always implement a toString() method
	Have used textual comparison of objects.
*/

function CDictionary() {
	this.Keys		 = new Array();		
	this.Items		 = new Array();
	this.Count       = 0;
	this.Add		 = CDictionary_Add;
	this.GetIndex    = CDictionary_GetIndex;
	this.GetItem     = CDictionary_GetItem;
	this.Remove      = CDictionary_Remove;
	this.RemoveAll   = CDictionary_RemoveAll;
	this.SetKey      = CDictionary_SetKey;
	this.SetItem     = CDictionary_SetItem;
	this.Exists      = CDictionary_Exists;
}

/*  
 Returns false if the key is already there..
 To prevent any accidental removal of existing data.
*/

function CDictionary_Add(key,item) {
	
	var index	     = this.GetIndex(key);	
	if(index == null) {
		index				= this.Count;
		this.Keys[index]	= key;
		this.Items[index]	= item;	
		this.Count++;
		return true;		
	}
	else {		
		return false;
	}
}

function CDictionary_Exists(key) {
	var index		 = this.GetIndex(key);
	if(index == null) return false;
	else return true;
	
}

function CDictionary_GetIndex(key) {		
	var sKey		 = key.toString();	
	var keys		 = this.Keys;
	var ln			 = keys.length;
	for(var i=0;i <ln;i++) {			
		if(sKey == keys[i].toString()) {			
			return i;
		}
	}
	return null;
}

function CDictionary_GetItem(key) {
	var index		 = this.GetIndex(key);
	if(index == null) {
		return null;
	}
	else  {
		if(this.Items[index] != null)  return this.Items[index];
		else return null; 
	}
	
}

/**
sets a key value to a new key value
if there is no existing key value in the 
list, it return false
*/

function CDictionary_SetKey(oldkey,newkey) {
	var index		 = this.GetIndex(oldkey);
	if(index != null) {
		this.Keys[index] = newkey;		
		return true;
	}
	else {
		return false;
	}
}

function CDictionary_SetItem(key,item) {
	var index		  = this.GetIndex(key);
	if(index != null) {
		this.Items[index] = item;		
	}
	else {
		this.Add(key,item);
	}	
}


function CDictionary_RemoveAll() {
	this.Keys		 = new Array();
	this.Items		 = new Array();			
	this.Count		 = 0;
}

/*
	returns false if the key is not there
*/

function CDictionary_Remove(key) {
	var index		= this.GetIndex(key);
	if(index != null) {				
		var keys	= this.Keys;				
		var items	= this.Items;		
		keys		=(keys.slice(0,index)).concat(keys.slice(index + 1,keys.length));		
		items		=(items.slice(0,index)).concat(items.slice(index + 1,items.length));				
		this.Keys	= keys;
		this.Items	= items;		
		this.Count--;		
		return true;		
	}
	return false;
}




