function openPopupForSelect(URL, selectOpenerId, listId, listName, popupTitle, features) {

	window.open(URL+'&selectOpenerId='+selectOpenerId+'&listId='+listId+'&listName='+listName, popupTitle, features);
}
function openPopupForSelectEp(URL, selectOpenerId, listId, listName, popupTitle, features) {
	//window.alert(listId, listName);
	window.open(URL+'&amp;selectOpenerId='+selectOpenerId+'&amp;listId='+listId+'&amp;listName='+listName, popupTitle, features);
}

function openPopupForSelectDocenteTitolare(URL, selectOpenerId, listId, listName, flag, popupTitle, features) {

	
	window.open(URL+'&selectOpenerId='+selectOpenerId+'&listId='+listId+'&listName='+listName+'&flag='+flag, popupTitle, features);
}
function openPopupForSelectPropedeuticita(URL, selectOpenerId, listId, listName, flag, popupTitle, features) {
	window.open(URL+'&selectOpenerId='+selectOpenerId+'&listId='+listId+'&listName='+listName+'&flag='+flag, popupTitle, features);
}

function eraserDocenteTitolare(){
		
	var iddoc = document.getElementById("docenteTitolare");
	var doc = document.getElementById("docenteTitolareName");
	iddoc.value="";
	doc.value="";
	
}

function openPopupForSelectNew(URL,selectOpenerId,listId,listName,popupTitle,anno,semestre,percorso,idPianoDiStudi,idEdizione,features) {
	
	
	var selectObj = document.getElementById(selectOpenerId);
	var options = selectObj.options;
	var first='true';
	var listIdInt = '';
	for (var i = 0; i < options.length; i++){
		
			if (options[i].value != ''){
				listIdInt+=options[i].value +';'
			
			}
	}
	
	//alert(listIdInt);
	var link = URL+'&selectOpenerId='+selectOpenerId+'&listAttivitaId='+listIdInt+'&anno='+anno+'&semestre='+semestre+'&percorso='+percorso+'&idPianoDiStudi='+idPianoDiStudi+'&idEdizione='+idEdizione;
	//window.open(URL+'&selectOpenerId='+selectOpenerId+'&listId='+listId+'&listName='+listName+'&anno='+anno+'&semestre='+semestre+'&percorso='+percorso+'&idPianoDiStudi='+idPianoDiStudi+'&idEdizione='+idEdizione, popupTitle, features);
	createFakeFormAndSendForOpenPopup(link,popupTitle, features);
}

function openPopupForDelete(URL,selectOpenerId,listId,listName,popupTitle,anno,semestre,percorso,idPianoDiStudi,idEdizione,features, errorMessage) {
	
	var selectObj = document.getElementById(selectOpenerId);
	if (selectObj.selectedIndex == -1){
		
		alert(errorMessage);
	}
	
	else {
	
		window.open(URL+'&selectOpenerId='+selectOpenerId+'&listId='+listId+'&listName='+listName+'&anno='+anno+'&semestre='+semestre+'&percorso='+percorso+'&idPianoDiStudi='+idPianoDiStudi+'&idEdizione='+idEdizione, popupTitle, features);
	}
}




function sceltaconfirm(servlet,anno,semestre,percorso,idPianoDiStudi,idEdizione) 
   { 
   flag = confirm("Sei sicuro di voler inserire un nuovo percorso?"); 
   if (flag==true) 
      {
      //window.alert(window.document.getElementById("creaPercorso"));	
      //window.alert(servlet+"&anno="+anno+"&semestre="+semestre+"&percorso="+percorso+"&idPianoDiStudi="+idPianoDiStudi+"&idEdizione="+idEdizione);
      //window.alert(window.document.getElementById("creaPercorso").action);	
      document.getElementById("creaPercorso").action = servlet+"&anno="+anno+"&semestre="+semestre+"&percorso="+percorso+"&idPianoDiStudi="+idPianoDiStudi+"&idEdizione="+idEdizione;
      document.getElementById("creaPercorso").submit();
      
      } 
   else 
      {} 
   } 

function sceltaconfirm1(servlet,anno,semestre,percorso,idPianoDiStudi,idEdizione) 
   { 
   flag = confirm("Sei sicuro di voler eliminare il percorso?"); 
   if (flag==true) 
      {
      //window.alert(window.document.getElementById("cancellaPercorso"));	
      //window.alert(servlet+"&anno="+anno+"&semestre="+semestre+"&percorso="+percorso+"&idPianoDiStudi="+idPianoDiStudi+"&idEdizione="+idEdizione);
      //window.alert(window.document.getElementById("cancellaPercorso").action);	
      document.getElementById("cancellaPercorso").action = servlet+"&anno="+anno+"&semestre="+semestre+"&percorso="+percorso+"&idPianoDiStudi="+idPianoDiStudi+"&idEdizione="+idEdizione;
      document.getElementById("cancellaPercorso").submit();
      
      } 
   else 
      {} 
   }






function cancellaconfirm(servlet,anno,semestre,percorso,idPianoDiStudi,idEdizione,selectId, listId, listName) 
   { 

   
   flag = confirm("Sei sicuro di voler cancellare l'attivita scelta?"); 
   if (flag==true) 
      {
      //window.alert(window.document.getElementById("creaPercorso"));	
      //window.alert(servlet+"&anno="+anno+"&semestre="+semestre+"&percorso="+percorso+"&idPianoDiStudi="+idPianoDiStudi+"&idEdizione="+idEdizione);
      //window.alert(window.document.getElementById("creaPercorso").action);	
      //document.getElementById("creaPercorso").action = servlet+"&anno="+anno+"&semestre="+semestre+"&percorso="+percorso+"&idPianoDiStudi="+idPianoDiStudi+"&idEdizione="+idEdizione;
      //document.getElementById("creaPercorso").submit();
      
		  deleteFromSelectSingle(servlet,anno,semestre,percorso,idPianoDiStudi,idEdizione,selectId, listId, listName);    
    
      
      } 
   else 
      {} 
   } 




function openPopupForSelectWithParameter(URL, selectOpenerId, listId, listName, parameter, popupTitle, features) {

	/*alert ("URL-->"+URL);
	alert ("selectOpenerId-->"+selectOpenerId);
	alert ("listId-->"+listId);
	alert ("listName-->"+listName);
	alert ("parameter-->"+parameter);
	alert ("popupTitle-->"+popupTitle);*/

	window.open(URL+'&selectOpenerId='+selectOpenerId+'&listId='+listId+'&listName='+listName+'&parameter='+parameter, popupTitle, features);
}

function deleteFromSelect(selectId, listId, listName) {
	while (document.getElementById(selectId).length > 0)
		document.getElementById(selectId)[0]=null;
	document.getElementById(listId).value="";
	document.getElementById(listName).value="";
}




function deleteFromSelectSinglePopup(servlet,anno,semestre,percorso,idPianoDiStudi,idEdizione,selectId, listId, listName) {
	

	var x= window.opener.document.getElementById(selectId);
	var listIdL= window.opener.document.getElementById(listId).value;
	var listNameL=window.opener.document.getElementById(listName).value;
	var idToDeleteFromDb;

	if(x.selectedIndex==-1){
		window.close();
		return;
	}
	
	else {
		
		
		//alert(servlet);
		idToDeleteFromDb = x.options[x.selectedIndex].value;
		x.remove(x.selectedIndex);
		var link =servlet+"&anno="+anno+"&semestre="+semestre+"&percorso="+percorso+"&idPianoDiStudi="+idPianoDiStudi+"&idEdizione="+idEdizione+"&listId="+idToDeleteFromDb;
	   	var xmlHttpPop;
		if (window.ActiveXObject) {
			xmlHttpPop = new ActiveXObject("Microsoft.XMLHTTP");
		} else if (window.XMLHttpRequest) {
			xmlHttpPop = new XMLHttpRequest();
		}
		
		xmlHttpPop.onreadystatechange = function(){
			closeWindowOnHttpResponse(xmlHttpPop);
		}
		
		xmlHttpPop.open("get", link, true);
		xmlHttpPop.setRequestHeader('Content-Type', 'text/xml');
		
		xmlHttpPop.send(null);
	   	
	}
}




function deleteFromSelectSingleNew(selectId, listId, listName){

	var x = document.getElementById(selectId);
	var listIdL= document.getElementById(listId).value;
	var listNameL=document.getElementById(listName).value;
	
	
	
	if(x.selectedIndex==0){
	
		for(var i=0; i<listIdL.length; i++){
				
			if(listIdL.charAt(i)==";"){
				
				var originalList = listIdL;
				listIdL = listIdL.substring(i+1, (listIdL.length));
				
				
				break;
			}
		}
	}
	else{
	
		var count=0;
		var first=0;
		var second=0;
		for(var i=0; i<listIdL.length; i++){
				
			if(listIdL.charAt(i)==";"){
				count++;
				if(count==x.selectedIndex){					
					first=i;
								
					break;
				}
			}
		}
		for(var j= (first+1); j<listIdL.length; j++ ){
			if(listIdL.charAt(j)==";"){
					second=j;
							
					break;
			}
	
		}
		var originalList = listIdL;
		var listIdL1=listIdL.substring(0,first+1);
		var listIdL2= listIdL.substring(second+1,(listIdL.length));
		listIdL= listIdL1+listIdL2;

		
	}
	
	if(x.selectedIndex==0){
	
		for(var i=0; i<listNameL.length; i++){
				
			if(listNameL.charAt(i)==";"){
			
				listNameL = listNameL.substring(i+1, (listNameL.length));
	
				break;
			}
		}
	}
	else{
	
		var count=0;
		var first=0;
		var second=0;
		for(var i=0; i<listNameL.length; i++){
				
			if(listNameL.charAt(i)==";"){
				count++;
				if(count==x.selectedIndex){					
					first=i;
								
					break;
				}
			}
		}
		for(var j= (first+1); j<listNameL.length; j++ ){
			if(listNameL.charAt(j)==";"){
					second=j;
							
					break;
			}
	
		}
		
		var listNameL1=listNameL.substring(0,first+1);
		var listNameL2= listNameL.substring(second+1,(listNameL.length));
		listNameL= listNameL1+listNameL2;
	
	}
	
	
	
	x.remove(x.selectedIndex);
	
	document.getElementById(listId).value=listIdL;

	
	document.getElementById(listName).value=listNameL;

	
	
	



}





function deleteFromSelectSingle(servlet,anno,semestre,percorso,idPianoDiStudi,idEdizione,selectId, listId, listName) {
	
	var x= document.getElementById(selectId);
	var listIdL= document.getElementById(listId).value;
	var listNameL=document.getElementById(listName).value;
	var idToDeleteFromDb;
	
	
	
	if(x.selectedIndex==-1){
	
	return;
	}

	
	if(x.selectedIndex==0){
	
		for(var i=0; i<listIdL.length; i++){
				
			if(listIdL.charAt(i)==";"){
				
				var originalList = listIdL;
				listIdL = listIdL.substring(i+1, (listIdL.length));
				idToDeleteFromDb = originalList.substring(0, i);
					
				break;
			}
		}
	}
	else{
	
		var count=0;
		var first=0;
		var second=0;
		for(var i=0; i<listIdL.length; i++){
				
			if(listIdL.charAt(i)==";"){
				count++;
				if(count==x.selectedIndex){					
					first=i;
								
					break;
				}
			}
		}
		for(var j= (first+1); j<listIdL.length; j++ ){
			if(listIdL.charAt(j)==";"){
					second=j;
							
					break;
			}
	
		}
		var originalList = listIdL;
		var listIdL1=listIdL.substring(0,first+1);
		var listIdL2= listIdL.substring(second+1,(listIdL.length));
		listIdL= listIdL1+listIdL2;
	
		idToDeleteFromDb = originalList.substring(first + 1, second);

	}
	
	if(x.selectedIndex==0){
	
		for(var i=0; i<listNameL.length; i++){
				
			if(listNameL.charAt(i)==";"){
			
				listNameL = listNameL.substring(i+1, (listNameL.length));
				//alert(listNameL);		
				break;
			}
		}
	}
	else{
	
		var count=0;
		var first=0;
		var second=0;
		for(var i=0; i<listNameL.length; i++){
				
			if(listNameL.charAt(i)==";"){
				count++;
				if(count==x.selectedIndex){					
					first=i;
								
					break;
				}
			}
		}
		for(var j= (first+1); j<listNameL.length; j++ ){
			if(listNameL.charAt(j)==";"){
					second=j;
							
					break;
			}
	
		}
		
		var listNameL1=listNameL.substring(0,first+1);
		var listNameL2= listNameL.substring(second+1,(listNameL.length));
		listNameL= listNameL1+listNameL2;
	
	}
	
	
	
	x.remove(x.selectedIndex);
	
	document.getElementById(listId).value=listIdL;

	
	document.getElementById(listName).value=listNameL;

	
	document.getElementById("contentform").action = servlet+"&anno="+anno+"&semestre="+semestre+"&percorso="+percorso+"&idPianoDiStudi="+idPianoDiStudi+"&idEdizione="+idEdizione+"&listId="+idToDeleteFromDb;
   	document.getElementById("contentform").submit();
}

function deleteFields (fields) {
	for (i=0; i<fields.length; i++) {
		document.getElementById(fields[i]).value="";
		if (document.getElementById(fields[i]).type=="select-one") {
			for(var contatore=0 ; contatore < document.getElementById(fields[i]).length; contatore++) {
				document.getElementById(fields[i])[contatore] = null;
			}
		}
	}
}

function createFakeFormAndSendForOpenPopup(link, popupName, features){
	var href = link;
	var path = href.substring(0, href.indexOf('?'));
	var args = href.substring(href.indexOf('?') + 1);
	
	var argsArray = args.split('&');
	var myform=document.createElement("form");
	myform.method="post";
	myform.action=path;
	myform.name="f";
	myform.id="f";
	
	
	for (var i = 0; i < argsArray.length; i++){
		var input = document.createElement("input");
		var argsArraySplit = argsArray[i].split('=');
		input.name = argsArraySplit[0];
		input.type="hidden";
		input.value = argsArraySplit[1];
		myform.appendChild(input);
	}

	
	
	myform.target=popupName;
	myform.onsubmit=window.open('',popupName,features);
	var body = document.getElementsByTagName('body');
	body[0].appendChild(myform);
	myform.submit();
	
}

function closeWindowOnHttpResponse(xmlHttpPop){
	
	
	if (xmlHttpPop.readyState == 4) {
		
		if (xmlHttpPop.status == 200) {
			
			window.close();
		}
	}

}