
//support functions 
function getObj(objId){

	return document.getElementById(objId);
}


function getHidden(tipo,hidName){
	try{
		var theName = 'HID_'+tipo.toUpperCase()+'_'+hidName; 

		//var theHid = getObj('HID_'+tipo+'_'+hidName);
		var theHid = getObj(theName);
		var theVal = theHid.value; 
		var theArr = new Array(); 
		
		if(theHid.value!=''){
			theArr = theVal.split(';');
		}
	}catch(e){
		alert('err getHidden');
		return;
	}
	return theArr; 
}


function findInArr(value,theArr){


	var  valRet = -1;
	for(i=0;i<theArr.length;i++){
		if(theArr[i]!=''){
			if(theArr[i]==value){
				valRet = i;
			}
		}
	
	}

	return valRet;
}

//cerca se tra i valori della drop down list 
//c'è quello passato in input
function findInDrop(ddList,theValue){
	var theRet = false; 

	//alert('checking start '+theValue); 
	for(i=0;i<ddList.options.length;i++){
		//alert('checking with '+ddList.options[i].value); 
		if(ddList.options[i].value ==theValue) {
			theRet = true;
			return theRet; 
		}
	}
	
	return theRet;
}

function checkItems(theDrop,theArr){

	
	//puliamo tutto
	theDrop.options.length=0;
	var optBK = document.createElement("option");
	optBK.text = "";
	optBK.value ="";
	theDrop.options.add(optBK);
	
	//aggiunta dei miei
	for(j=0;j<theArr.length;j++){
		if(theArr[j]!=''){
			
			if(!findInDrop(theDrop,theArr[j])){

				var opt = document.createElement("option");
				var theVal= "";
				var theLink = "";

				//clean the value from the link 
				if (theArr[j].indexOf('|')>-1){
					theArrSub = theArr[j].split('|');
					theVal = theArrSub[0];
					theLink = theArrSub[1];
				}
				
				opt.text = theVal;
				opt.value =theVal;
				theDrop.options.add(opt);

			}
			
		}
		
	}
	
}

function mustProceed(selectedFrom,selectedValue){
	try{
		var hidCheck = getObj('HID_SELECTED_FROM');
		var myReturn = true; 
	
	
		if(hidCheck.value=='') {
			hidCheck.value = selectedFrom;
			return true; 
		}
	
		//se la selezione non è vuota
		if (selectedValue!=''){
			if(hidCheck.value!=selectedFrom){
				myReturn = false;
			}	
		}else {
			//altrimenti resetta 
			hidCheck.value = selectedFrom; 
			myReturn = true; 
		}
	}catch(e){
		return false; 
	}
	return myReturn; 

}

//reimposta le drop down ai valori iniziali 

function resetDropsToFabric(){
	try{
		var objSede = getObj('HID_SEDE_START');
		var objAttiv = getObj('HID_ATTIV_START');
		var theDropAttiv = getObj('drop_attiv');//prende l'altro ddm
		var theDropSede = getObj('drop_sede');//prende l'altro ddm
		var arrSede = objSede.value.split(';');
		var arrAttiv= objAttiv.value.split(';');
		doDrop(theDropAttiv,arrAttiv);
		doDrop(theDropSede,arrSede);
		//resetta anche l'hidden che serve a tenere conto della precedenza
	 	var hidSel = getObj('HID_SELECTED_FROM');
	 	hidSel.value = "";
 	}catch(e){
 		alert('errore resetDrops');
 	}
}

function doDrop(theDrop, arrayDrops){
	try{
	
		theDrop.options.length=0;
		var optBK = document.createElement("option");
		optBK.text = "";
		optBK.value ="";
		theDrop.options.add(optBK);
		for(i=0;i<arrayDrops.length;i++){
	
			if(arrayDrops[i]!=''){
				var optBK = document.createElement("option");
				optBK.text = arrayDrops[i];
				optBK.value = arrayDrops[i];
				theDrop.options.add(optBK);
			}
	
		}
	}catch(e){
		alert('errore doDrop');
	}
}

//work functions
function select_from_sede(sede){

	if(sede!=''){
		try{
			if(!mustProceed("sede",sede)) {
				getLink("attiv",sede);
				return;
			}
			var theHidArr = getHidden('SEDE',sede);
			var theDrop = getObj('drop_attiv');//prende l'altro ddm
			
			checkItems(theDrop,theHidArr);
		}catch(e){
			alert('select from sede:'+e.message); 
		}	
	} else  {
		//resetDropsToFabric();
	}
}

function select_from_attiv(attiv){


	if(attiv!=''){	
		try{
			if(!mustProceed("attiv",attiv)) {
				getLink("sede",attiv); 
				return;
			} 
			var theHidArr = getHidden('ATTIV',attiv); 
			var theDrop = getObj('drop_sede');//prende l'altro ddm
			
			checkItems(theDrop,theHidArr);
		}catch(e){
			alert('select from attiv:'+e.message); 
		}	
	}else {
		resetDropsToFabric();
	}
	
}

function getLink(theType,theVal){

		
	//prende il valore dell'altra combo 
	var otherCombo = getObj('drop_'+theType); 
	var theIdx = otherCombo.selectedIndex;
	var theOtherVal= otherCombo.options[otherCombo.selectedIndex].value;


	var theOgg = getHidden(theType,theOtherVal);

	var theLink = "";	
	for(i=0;i<theOgg.length;i++){
		currVal = theOgg[i];
		currArr = currVal.split('|'); 
		if(currArr[0]==theVal){
			theLink = currArr[1];
			break;
		}
	}

	var theHid = getObj('HID_GOTO');
	theHid.value = theLink;
	
}

function doGoAction(){

	var theHidLink = getObj('HID_GOTO');

	var theLinkVal = theHidLink.value; 

	if(theLinkVal!=''){
		location.href=theLinkVal; 	
	}else {
		alert('Selezionare Sede e Attivita');
	}
}

