/*
* nella pagina che include questo javascript devono esserci i seguenti input hidden:
* - h_testoTuttiIComuni
* - h_scegliCategoria
*/

// Funzioni per aggiungere e leggere le province

function addToProvList (obj) {
	if (!this.provList) {
		this.provList = new Array();
		this.countProv = 0
	}
	this.provList[this.countProv++] = obj;
}

function getProvCount () {
	return this.countProv;
}

function getProvAt (id) {
	if (id>=0)
		return this.provList[id];
	else
		return this.provList;
}



// Funzioni della classe provincia

function provincia(nome) {

	this.nomeProvincia = nome; 

	this.addComune = function (obj) { 

		if (!this.arrayComune) { 
			this.arrayComune = new Array(); 
			this.countComuni = 0; } 

		this.arrayComune[this.countComuni++] = obj; 
	}	

	this.getComuneAt = function (id) {
		if (!this.arrayComune) alert (this.nomeProvincia + ".arrayComune = false!");
		return this.arrayComune[id];
	}

	this.findComune = function (nome) {
		if (!this.arrayComune) return null;
		else
		{		
			var com=null;
			
			for(var i=0; i<this.arrayComune.length; i++)
			{
				if (this.arrayComune[i].nomeComune==nome)
				{
					com=this.arrayComune[i];
					break;
				}
			}
			return com;
		}
	}

	this.getComuneCount = function () {
		return this.countComuni;
	}
}



// Funzioni della classe comune

function comune(nome) { 

	this.nomeComune = nome; 

	this.addLocalita = function (nome) { 
	
		if (!this.arrayLoc) { 
			this.arrayLoc = new Array(); 
			this.countLoc = 0; 
		}
			
		this.arrayLoc[this.countLoc++] = nome; 
	}	

	this.getLocalitaAt = function (id) {
		if (!this.arrayLoc) alert (this.nomeComune + ".arrayLoc = false!");
		return this.arrayLoc[id];
	}

	this.getLocalitaCount = function () {
		return this.countLoc;
	}
}



// Funzioni per rimepire i controlli web
function insertOption (objList, text, value) {
	if (!objList) window.alert ("insertOption (...); objList = false;");
	objList.options[objList.length] = new Option(text, value);
}

function clearOptions (objList) {
	objList.length = 0;
}

function populateComune (idCtrlProv, idCtrlCom, idCtrlLoc) {
	
	// Recupera gli oggetti Select dagli ID
	var ctrlProv = document.getElementById (idCtrlProv);
	var ctrlCom = document.getElementById (idCtrlCom);
	var ctrlLoc = document.getElementById (idCtrlLoc);

	// Memorizza l'indice della provincia selezionata
	var idProv = ctrlProv.selectedIndex;
	
	// Se ha selezionato la prima voce (che è di commento), allora 
	// pulisce e disabilita i list Comuni e Localita
	if (idProv == 0) {
		clearOptions (ctrlCom);
		clearOptions (ctrlLoc);
		ctrlCom.disabled = true;
		ctrlLoc.disabled = true;
		ctrlCom.style.visibility = "hidden";
		ctrlLoc.style.visibility = "hidden";
		return;
	}

	// Riempie la list dei comuni con i comuni della provincia selezionata

	// Recupera l'oggetto provincia selezionato
	var prov = getProvAt (idProv-2); // meno due perchè il primo elemento della list è "Seleziona una..." e il secondo è 'Tutte le province'

	// Cancella le opzioni presenti nella list comuni
	clearOptions (ctrlCom);

	// Aggiunge i comuni nella list comuni
	insertOption (ctrlCom, document.getElementById("h_testoTuttiIComuni").value, "");

	var arr=new Array();
	if (idProv>1)
	{
		for (var i=0; i<prov.getComuneCount(); i++) {
			var com = prov.getComuneAt (i);
			arr.push(com.nomeComune);
			/*insertOption (ctrlCom, com.nomeComune, com.nomeComune);*/
		}
	}
	else
	{
		
		for (var n=0; n<prov.length; n++)
			for (var i=0; i<prov[n].getComuneCount(); i++) {
				var com = prov[n].getComuneAt (i);
				arr.push(com.nomeComune);
			}
	}
	
	arr.sort();
	for (var i=0; i<arr.length; i++)
		insertOption (ctrlCom, arr[i], arr[i]);
	
	// Abilita il controllo lista comuni
	ctrlCom.disabled = false;		
	ctrlCom.style.visibility = "";

	// Nasconde il controllo lista località
	ctrlLoc.style.visibility = "hidden";

}

function populateLocalita (idCtrlProv, idCtrlCom, idCtrlLoc) {

	// Recupera gli oggetti Select dagli ID
	var ctrlProv = document.getElementById (idCtrlProv);
	var ctrlCom = document.getElementById (idCtrlCom);
	var ctrlLoc = document.getElementById (idCtrlLoc);

	// Memorizza l'indice della provincia selezionata
	var idProv = ctrlProv.selectedIndex;

	// Memorizza l'indice del comune selezionato
	var idCom = ctrlCom.selectedIndex;

	// Se ha selezionato la prima voce (che è di commento), allora 
	// pulisce e disabilita il list Localita
	if (idCom == 0) {
		clearOptions (ctrlLoc);
		ctrlLoc.disabled = true;
		ctrlLoc.style.visibility = "hidden";
		return;
	}


	// Riempie la list delle localita con le localita del comune selezionato

	// Recupera l'oggetto provincia selezionato
	var prov = getProvAt (idProv-2); // meno due perchè il primo elemento della list è "Seleziona una..." e il secondo è 'Tutte le province'

	// Recupera l'oggetto comune selezionato
	var com;

	if (idCom>0)
	{
		var arr=new Array();
		if (idProv>=1)
			com = prov.getComuneAt (idCom-1); // meno uno perchè il primo elemento della list è "Seleziona un..."
		else
		{
			for (var i=0; i<prov.length; i++)
			{
				com=prov[i].findComune(ctrlCom[idCom].value);
				if (com!=null) break;
			}
		}
	
		// Cancella le opzioni presenti nella list localita
		clearOptions (ctrlLoc);

		// Aggiunge le localita nella list localita
		for (var i=0; i<com.getLocalitaCount(); i++) {
			var loc = com.getLocalitaAt (i);
			arr.push(loc);
		}

		arr.sort();
		
		insertOption (ctrlLoc, document.getElementById("h_testoTutteLeLocalita").value, "");
		for (var i=0; i<arr.length; i++)
		{
			insertOption (ctrlLoc, arr[i], arr[i]);
		}
	}
	
	// Abilita il controllo lista localita
	ctrlLoc.disabled = false;		
	ctrlLoc.style.visibility = "";

}

function tipologia (nome) {

	this.nomeTipologia = nome;

	this.addCategoria = function (value) {
		if (!this.arrayCat) { 
			this.arrayCat = new Array(); 
			this.countCat = 0; 
		}
			
		this.arrayCat[this.countCat++] = value; 
	}	

	this.getCategoriaCount = function () {
		return this.arrayCat.length;
	}

	this.getCategoriaAt = function (id) {
		return this.arrayCat[id];
	}

}


function addToTipList (obj) {
	if (!this.tipList) {
		this.tipList = new Array();
		this.countTip = 0
	}
	this.tipList[this.countTip++] = obj;
}

function getTipAt (id) {
	return this.tipList[id];
}

function getTipCount () {
	return this.tipList.length;
}



/*function populateTipologia (idCtrlDove, idCtrlTip) {

	var ctrlDove = document.getElementById (idCtrlDove);
	var ctrlTip = document.getElementById (idCtrlTip);

	// Recupera l'id dell'elemento selezionato nel DOVE
	var idTip = ctrlDove.selectedIndex;

	// Se non ha scelto niente (ovvero ha selezionato la prima voce) allora mostra un valore nullo nel TIPOLOGIA
	if (idTip == 0) {
		clearOptions (ctrlTip);
		insertOption (ctrlTip, "*****************", "");	
		return;
	}

	// Recupera l'oggetto Tipologia
	var tip = getTipAt (idTip - 1); // meno uno perchè la prima voce è Scegli

	clearOptions (ctrlTip);
	insertOption (ctrlTip, document.getElementById("h_scegliCategoria").value, "");
	
	// Riempie il list TIPOLOGIA con le categoria dell'oggetto tipologia
	for (var i=0; i<tip.getCategoriaCount(); i++) {
		var cat = tip.getCategoriaAt (i);
		insertOption (ctrlTip, cat, cat);
	}

}

function initTipList (idDove, idTip) {

	var ctrlDove = document.getElementById(idDove);
	
	// Riempie la lista con le tipologie
	insertOption (ctrlDove, document.getElementById("h_select").value, "");
	for (var i=0; i<getTipCount(); i++) {
		var tip = getTipAt (i);
		insertOption (ctrlDove, tip.nomeTipologia, tip.nomeTipologia);
	}

}*/

function selectOption (ctrlSel, str) {

	for (var i=0; i<ctrlSel.length; i++) {
		if (ctrlSel.options[i].value == str) {
			ctrlSel.selectedIndex = i;
			return;
		}
	}
}

function selItems (idProv, idCom, idLoc, prov, com, loc) {

	ctrlProv = document.getElementById(idProv);
	ctrlCom = document.getElementById(idCom);
	ctrlLoc = document.getElementById(idLoc);

	selectOption (ctrlProv, prov);
	populateComune (idProv, idCom, idLoc);
	
	selectOption (ctrlCom, com);
	populateLocalita (idProv, idCom, idLoc);

	selectOption (ctrlLoc, loc);
	if (loc=="")
	{
		ctrlLoc.style.visibility = "hidden";
		ctrlLoc.style.visibility = "hidden";
	}
}

function selDove (idCtrlDove, idCtrlTip, dove, tip) {
	var ctrlDove = document.getElementById (idCtrlDove);
	var ctrlTip = document.getElementById (idCtrlTip);
	
	selectOption (ctrlDove, dove);
	populateTipologia (idCtrlDove, idCtrlTip);
	selectOption (ctrlTip, tip);
}


function onReset (idCtrlProv, idCtrlCom, idCtrlLoc) {

	var ctrlProv = document.getElementById (idCtrlProv);
	var ctrlCom = document.getElementById (idCtrlCom);
	var ctrlLoc = document.getElementById (idCtrlLoc);
	
	clearOptions (ctrlCom);
	clearOptions (ctrlLoc);

	ctrlCom.disabled = true;
	ctrlLoc.disabled = true;
	ctrlCom.style.visibility = "hidden";
	ctrlLoc.style.visibility = "hidden";

	ctrlProv.selectedIndex = 0;

}

/*var tip = new tipologia ("Dove dormire");
tip.addCategoria ("Hotel");
tip.addCategoria ("Agriturismo");
tip.addCategoria ("B&B");
tip.addCategoria ("CasaVacanze");
tip.addCategoria ("Residence");
tip.addCategoria ("VillaggioTuristico");
addToTipList (tip);*/

