// Svuota la combo. Prende in input il nome dell'oggetto Select 
function emptyCombo(nameCombo)
{
	
	if(document['formSearchEngine'].elements[nameCombo] == undefined)
		return;
	
	var numItemCombo = document['formSearchEngine'].elements[nameCombo].length;		
	var incr=0;

	for(var index=1; index < numItemCombo; index++)
	{
		document['formSearchEngine'].elements[nameCombo].options[index-incr] = null;
		//eval("document.formSearchEngine."+nameCombo+".options")[index-incr] = null;
		incr++;
	}	

}// end function



// Popola la combo-BOX degli anni 
function populateComboYears()
{
		var date=new Date();
		var year = date.getFullYear();

		for(index=0; index < 9; index++)
		{	
			document.formSearchEngine.year.options[index+1] = new Option(year-index ,year-index, false, false);
		}

}// end function populateComboYears()


	// Popola la combo-BOX dei Mesi in lingua Italiana
	function populateComboMonthsIta()
	{

		document.formSearchEngine.month.options[1] = new Option("Gennaio", "1", false, false);		
		document.formSearchEngine.month.options[2] = new Option("Febbraio", "2", false, false);
		document.formSearchEngine.month.options[3] = new Option("Marzo", "3", false, false);
		document.formSearchEngine.month.options[4] = new Option("Aprile", "4", false, false);
    document.formSearchEngine.month.options[5] = new Option("Maggio", "5", false, false);
		document.formSearchEngine.month.options[6] = new Option("Giugno", "6", false, false);
		document.formSearchEngine.month.options[7] = new Option("Luglio", "7", false, false);
		document.formSearchEngine.month.options[8] = new Option("Agosto", "8", false, false);		
		document.formSearchEngine.month.options[9] = new Option("Settembre", "9",false, false);
		document.formSearchEngine.month.options[10] = new Option("Ottobre", "10", false, false);
		document.formSearchEngine.month.options[11] = new Option("Novembre", "11",false, false);
		document.formSearchEngine.month.options[12] = new Option("Dicembre", "12", false, false);

	}// end function populateComboYears()	


	// Popola la combo-BOX dei Mesi in lingua Inglese
	function populateComboMonthsEng()
	{
		document.formSearchEngine.month.options[1] = new Option("January", "1", false, false);		
		document.formSearchEngine.month.options[2] = new Option("February", "2", false, false);
		document.formSearchEngine.month.options[3] = new Option("March", "3", false, false);
		document.formSearchEngine.month.options[4] = new Option("April", "4", false, false);
		document.formSearchEngine.month.options[5] = new Option("May", "5", false, false);
		document.formSearchEngine.month.options[6] = new Option("June", "6", false, false);
		document.formSearchEngine.month.options[7] = new Option("July", "7", false, false);
		document.formSearchEngine.month.options[8] = new Option("August", "8", false, false);
		document.formSearchEngine.month.options[9] = new Option("September", "9",false, false);
		document.formSearchEngine.month.options[10] = new Option("October", "10", false, false);
		document.formSearchEngine.month.options[11] = new Option("November", "11",false, false);
		document.formSearchEngine.month.options[12] = new Option("December", "12", false, false);
	}// end function populateComboYears()	


function getStatisticTypeIndex() 
{
	var i = 0;	
	if(document.formSearchEngine.statisticType.options != undefined)
	{//Caso in cui statisticType sia una combo poichč esistono pił famiglie
		i = document.formSearchEngine.statisticType.selectedIndex - 1;
	}	
	
	//alert("a: "+i);
	
	return i;
}	

function getFileTypeIndex() 
{
	var i = 0;	
	if(document.formSearchEngine.fileType.options != undefined)
	{//Caso in cui statisticType sia una combo poichč esistono pił famiglie
		i = document.formSearchEngine.fileType.selectedIndex - 1;
	}	
	
	//alert("b: "+i);
	
	return i;
}		

function getFileSubTypeIndex() 
{
	var i = 0;	
	if(document.formSearchEngine.fileSubtype.options != undefined)
	{//Caso in cui statisticType sia una combo poichč esistono pił famiglie
		i = document.formSearchEngine.fileSubtype.selectedIndex - 1;
	}	
	
	//alert("c: "+i);
	
	return i;
}		



// Seleziona l'array e popola la combo "Tipo File" in modo dinamico
// La selezione dell'array dipende dall'option della combo "statisticType" selezionato dall'utente web
function selectFileType(language) 
{		

	//Svuoto la combo "fileType"
	emptyCombo("fileType");

	//Svuota la combo "fileSubtype"
	emptyCombo("fileSubtype");	
		
	var a = getStatisticTypeIndex();
	
	populateFileType(a);

	populateComboYears();
	
	if(language == "it")
		populateComboMonthsIta();
	else
		populateComboMonthsEng();		

}// end function selectFileType

// Seleziona l'array e popola la combo "Tipo File" in modo dinamico escludendo la combo dei mesi
// La selezione dell'array dipende dall'option della combo "statisticType" selezionato dall'utente web
function selectFileTypeNoMonth(language) 
{		

	//Svuoto la combo "fileType"
	emptyCombo("fileType");

	//Svuota la combo "fileSubtype"
	emptyCombo("fileSubtype");	
		
	var a = getStatisticTypeIndex();
	
	populateFileType(a);

	populateComboYears();
	
//	if(language == "it")
//		populateComboMonthsIta();
//	else
//		populateComboMonthsEng();		

}// end function selectFileType





// Seleziona gli array e popola le combo "Sottotipo File", "Mese" e "Anno" in modo dinamico
// La selezione dell'array dipende sia dall'option della combo "statisticType" e sia 
// dall'option della combo "fileType" selezionati dall'utente web
function selectFileSubtype() 
{

		

	// Popola la combo "fileSubtype"

	var a = getStatisticTypeIndex();
	var b = getFileTypeIndex();
	
	if(document.formSearchEngine.fileSubtype != undefined)	
	{
		//Svuota la combo "fileSubtype"
		emptyCombo("fileSubtype");
		populateFileSubtype("fileSubtype","newChoiceFileSubtype",a,b);
	}
	else
	{
			//Svuota la combo "year"
			emptyCombo("year");		

			// Popola la combo "year"							
			populateComboYearMonth("year","newChoiceYear");

			//Svuota la combo "month"
			emptyCombo("month");		

			// Popola la combo "year"
			populateComboYearMonth("month","newChoiceMonth");	
		
	}		

}// end function selectFileSubtype


function selectYearAndMonth() 
{

	//Svuota la combo "year"
	emptyCombo("year");

	//Svuota la combo "year"
	emptyCombo("month");

	var a = getStatisticTypeIndex();
	var b = getFileTypeIndex();	
	var c = getFileSubTypeIndex();
	

	// Popola la combo "year"
	populateComboYearMonthFromFileSubType("year","newChoiceYear",a, b, c);		

	// Popola la combo "month"
	populateComboYearMonthFromFileSubType("month","newChoiceMonth",a, b, c);
		

}// end function selectFileSubtype

function selectYear() 
{

	//Svuota la combo "year"
	emptyCombo("year");

	var a = getStatisticTypeIndex();
	var b = getFileTypeIndex();	
	var c = getFileSubTypeIndex();
	

	// Popola la combo "year"
	populateComboYearMonthFromFileSubType("year","newChoiceYear",a, b, c);		


}// end function selectFileSubtype


///////////////SAL

/**
*Populate fileType combo from statisticType (combo selected index or 0 if hidden field)
* 
*/

function populateFileType(statisticTypeIndex)
{
				for(index=0; index < eval("newChoiceFileType"+statisticTypeIndex).length; index++)

				{	

					value = eval("newChoiceFileType"+statisticTypeIndex)[index];

					id = value.substring(0, value.indexOf(","));

					name = value.substring(value.indexOf(",")+1, value.length);

					document.formSearchEngine.fileType.options[index+1] = new Option(name ,id, false, false);

				}
	
}

/**
*Populate fileSubtype combo from statisticType (combo selected index or 0 if hidden field) and fileType (combo selected index)
*Prende in input il nome dell'oggetto Select 
* 
*/
function populateFileSubtype(nameCombo,nameNewChoice,statisticTypeIndex, fileTypeIndex)
{
				
				if ((statisticTypeIndex > -1) && (fileTypeIndex > -1))

				{ 

					var x=statisticTypeIndex;			
					var y=fileTypeIndex; 

					for(index=0; index < eval(nameNewChoice+x+"_"+y).length; index++)
					{

						value = eval(nameNewChoice+x+"_"+y)[index];

						id = value.substring(0, value.indexOf(","));

						name = value.substring(value.indexOf(",")+1, value.length);										

						if((name == "Nessun Sottotipo") || (name == "No Subtype"))
						{

							eval("document.formSearchEngine."+nameCombo+".options")[index+1] = new Option(name, id, false, false);						

							//Svuota la combo "year"
							emptyCombo("year");		

							// Popola la combo "year"							
							populateComboYearMonth("year","newChoiceYear");

							//Svuota la combo "month"
							emptyCombo("month");		

							// Popola la combo "year"
							populateComboYearMonth("month","newChoiceMonth");

						}

						else
						{
							eval("document.formSearchEngine."+nameCombo+".options")[index+1] = new Option(name, id, false, false);	
						}

					}


				}// end if
	
}


/** TODO: Verificare Se serve
*Populate Year or Month combo from 
*			statisticType (combo selected index or 0 if hidden field) 
*			and fileType (combo selected index)
*			and fileSubType (combo selected index)
*Prende in input il nome dell'oggetto Select 
* 
*/
function populateComboYearMonth(nameCombo,nameNewChoice)
{
	
					
				var a = getStatisticTypeIndex();
				var b = getFileTypeIndex();
			

				if ((a > -1) && (b > -1))
				{ 

					var x=a;					
					var y=b; 
					var w=0;

					for(index=0; index < eval(nameNewChoice+x+"_"+y+"_"+w).length; index++)
					{

						value = eval(nameNewChoice+x+"_"+y+"_"+w)[index];

						id = value.substring(0, value.indexOf(","));

						name = value.substring(value.indexOf(",")+1, value.length);										

						eval("document.formSearchEngine."+nameCombo+".options")[index+1] = new Option(name, id, false, false);						

					}


				}// end if
			
}

/**
*Populate Year or Month combo from 
*			statisticType (combo selected index or 0 if hidden field) 
*			and fileType (combo selected index)
*			and fileSubType (combo selected index)
*Prende in input il nome dell'oggetto Select 
* 
*/
function populateComboYearMonthFromFileSubType(nameCombo,nameNewChoice,statisticTypeIndex, fileTypeIndex, fileSubTypeIndex)
{
	
			
	
				// Valore dell'elemento selezionato dalla combo-box 			

				if ((statisticTypeIndex > -1) && (fileTypeIndex > -1) && (fileSubTypeIndex > -1))
				{ 

					var x=statisticTypeIndex;					
					var y=fileTypeIndex; 
					var w=fileSubTypeIndex;

					for(index=0; index < eval(nameNewChoice+x+"_"+y+"_"+w).length; index++)
					{

						value = eval(nameNewChoice+x+"_"+y+"_"+w)[index];

						id = value.substring(0, value.indexOf(","));

						name = value.substring(value.indexOf(",")+1, value.length);										

						eval("document.formSearchEngine."+nameCombo+".options")[index+1] = new Option(name, id, false, false);						

					}

				}// end if
				else if((statisticTypeIndex > -1) && (fileTypeIndex > -1) && (fileSubTypeIndex == -1) ) //fileSubType non selezionato
					populateFileSubtype("fileSubtype","newChoiceFileSubtype",statisticTypeIndex,fileTypeIndex);

			
}
