/* Example of a dumb use.
 * var objCollection = new Collection(2);
 * objCollection.add("A");
 * objCollection.add("B");
 * objCollection.add("C");
 * objCollection.add("D");
 * objCollection.add("E");
 * alert( objCollection.size() ); // retorna 5
 * objCollection.remove(2);
 * alert( objCollection.size() ); // retorna 4
 * alert( objCollection.get(0) );
 * alert( objCollection.get(1) );
 * alert( objCollection.get(2) );
 * alert( objCollection.get(3) );
 *
 */

//arrayIndexOutOfBounds = new Error("The index is greater than the size of collection, or is negative.");
//invalidInitialParameter = new Error ("The size of the collection must be a positive integer.");
// throw noNameError

//try {
//	if (document.forms[0].username.value == "")
//	{
//		throw noNameError;
//	}

//} catch (e) {
// any and all errors will go to this handler
//	mainExceptionHandler(e);
//} 


function Collection(initialSize){

	//Variables
	this.position 			= 0;
	this.array 				= new Array(initialSize);

	//Methods
    this.add 				= _add;
    this.get 				= _get;
    this.remove 			= _remove;
    this.ensureCapacity 	= _ensureCapacity;
    this.size 				= _size;
}



	function _add(obj)
	{
		if(this.array.length > this.position )
		{
			this.array[this.position] = obj;
			this.position++;
		}
		else
		{
			this.ensureCapacity();
			this.array[this.position] = obj;
			this.position++;
		}
	}

	function _get(index)
	{
		if( (index>=0) && (index<this.array.length) )
			return this.array[index];
	}

	function _remove(index)
	{
		if( (index>=0) && (index<this.array.length) )
			var newArray = new Array(this.array.length);
			var j = 0;
			for( i = 0; i < this.array.length; i++ )
			{
				if(i!=index){
					newArray[j++] = this.array[i];
				}
			}
			this.array = newArray;
			this.position--;	
	}

	function _ensureCapacity()
	{
		this.position = 0;
		var oldArray = this.array;
		this.array = new Array( (oldArray.length*2) + 1 );
		for(this.position = 0; this.position < oldArray.length; this.position++)
		{
			this.array[this.position] = oldArray[this.position];
		}
	}

	function _size()
	{
		return this.position;
	}