﻿//-------------------------------------------------------------------------------------------------------
//	La classe JSCollection viene utilizzata per istanziare oggetti di tipo collection strutturati in
//	array: uno contenente stringhe che rappresentano i codici associati agli indirizzi, ed una contenente
//	gli indirizzi. 
//------------------------------------------------------------------------------------------------------- 
function JSCollection(){
//Properties
this.items=new Array			//	Contiene le voci della collection.
this.itemCount=0				//	Contatore associato all'oggetto. Contiene il numero di elementi.
this.keys=new Array			//	Contiene le chiavi, sotto forma di codici-stringa.

//Methods
this.addItem=addItem			//	Aggiunge una voce alla collection
this.removeItem=removeItem			//	Rimuove un'elemento dalla collecction
this.getItemByKey=getItemByKey		//	Dato un codice-chiave mi viene ritornato l'item corrispondente o null 
this.getItemByNr=getItemByNr		//	Dato un numero mi viene ritornato l'item corrispondente o null
this.getKeyByNr=getKeyByNr		//	Dato un numero mi viene ritornato la chiave corrispondente o null
this.index=index				//	Mi ritorna l'indice associato alla chiave inserita come parametro o -1
					//	se non lo ha trovato.								
}

//-------------------------------------------------------------------------------------------------------
//Implementations
//-------------------------------------------------------------------------------------------------------

function index(key){
	var index=-1
	for(var i=0; i<this.itemCount && index==-1; i++){
		if (key==this.keys[i]) 
			{index=i
			}
	}
	return index
}

function addItem(key, obj){
//	key e' la chiave associata all'item da aggiungere
//	obj e' l'oggetto che vogliamo aggiungere alla collection (se gia' presente return=-1
	var i=this.index(key)
	if (i==-1){
		this.items[this.itemCount]=obj
		this.keys[this.itemCount]=key
		this.itemCount++
		return this.itemCount-1
	} 
	else 
		{return -1}
}

function removeItem(key){
//	key e' la chiave associata all'item da eliminare. Se non esiste non fa' niente
	var i=this.index(key)
	if (i>=0) {
		//Shifto in alto di uno ogni elemento dopo quello da eliminare.
		for (var j=i+1; j<this.itemCount; j++){
			this.items[j-1]=this.items[j]
			this.keys[j-1]=this.keys[j]
		}
		//decremento il contatore.
		this.itemCount--
	}			
}

function getItemByKey(key){
	return this.getItemByNr(this.index(key))
	
}

function getItemByNr(Nr){
	if (Nr >= 0 && Nr<this.itemCount){	
		return this.items[Nr]
	}
	else
		{return null}	
}

function getKeyByNr(Nr){
	if (Nr >= 0 && Nr<this.itemCount){
		return this.keys[Nr]
	}
	else
		{return ""}	
}

//-------------------------------------------------------------------------------------------------------
// end of the implementation
//-------------------------------------------------------------------------------------------------------

