	function addLoadEvent(func) { 
		
		var oldonload = window.onload; 
		if (typeof window.onload != 'function') { 
			window.onload = func; 
		} else { 
			window.onload = function() { 
			if (oldonload) { 
				
				oldonload(); 
			} 

			//alert(func);
			func(); 
			} 
		} 
	} 

function fillcheckbox(idList){
		
		if (window.opener.document.getElementById(idList).value != ''){
			
			var idSplitted = window.opener.document.getElementById(idList).value.split(";");
			//alert('idSplitted'+idSplitted);
			var checkBoxElements = document.getElementsByName('select');
			
			for(var i = 0; i < idSplitted.length; i++){
						
					for (var j = 0; j < checkBoxElements.length; j++){
						if (checkBoxElements[j].id == idSplitted[i]){
							checkBoxElements[j].checked = true;
						}
						
					}
			}
			
			//alert('prima di chiamare la funzione');
		
		}
	}

	function updateSelectOnChange(box_value, box_description, selectId, idList, nameList){

		var flag = false;

		var exit = false;
		
		var contatore=0;
		
		
		for(contatore=0 ; contatore < window.opener.document.getElementById(selectId).length; contatore++) {
			//alert('id = ' + box_value);
			//alert(document.getElementById(box_value).checked);
			if(window.opener.document.getElementById(selectId)[contatore].value == box_value) {
				
				if (document.getElementById(box_value).checked == false){
					
					//alert('il checkbox ora e false');
					
					var idLista = window.opener.document.getElementById(idList).value;
					var nomeLista = window.opener.document.getElementById(nameList).value;
						
					if (contatore > 0){
						
						//alert("control1");
						
						var index = idLista.search(box_value);
						var indexName = nomeLista.search(box_description);
						
						if (index != -1){

							
							var firstPart = idLista.substr(0, index);
							var secondPart = idLista.substr(index);

							//nome
							var firstPartName = nomeLista.substr(0, indexName);
							var secondPartName = nomeLista.substr(indexName);			
							
							var separatorIndex = secondPart.indexOf(";");
							var strAppend = secondPart.substr(separatorIndex + 1);
							

							//nome
							var separatorIndexNome = secondPartName.indexOf(";");
							var strAppendNome = secondPartName.substr(separatorIndexNome + 1);
							

							
							window.opener.document.getElementById(idList).value = firstPart + ''+ strAppend;
							//nome
							window.opener.document.getElementById(nameList).value = firstPartName + ''+ strAppendNome;
							
							window.opener.document.getElementById(selectId).remove(contatore);

						}
					
						return;
					
					}
					
					
					
					else {


						
						var separatorIndex = idLista.indexOf(";");
						var str = idLista.substr(separatorIndex + 1);
						
						//nome
						var separatorIndexNome = nomeLista.indexOf(";");
						var strNome = nomeLista.substr(separatorIndexNome + 1);
							
						
						window.opener.document.getElementById(idList).value = str;
						//nome
						window.opener.document.getElementById(nameList).value = strNome;
						
						window.opener.document.getElementById(selectId).remove(contatore);
						
						return;
					}
				}

			}
			
		}
		
		if(!flag) {
		
				var option = opener.document.createElement("option");
				option.text = box_description;
				option.value = box_value;
				option.name = box_value;
				window.opener.document.getElementById(selectId)[contatore] = option;
		}

		var firstline = true;
		

		if (window.opener.document.getElementById(selectId).length > 0) {

			for( var contatoreTemp = 0; contatoreTemp < window.opener.document.getElementById(selectId).length; contatoreTemp++) {

			if(firstline) {

				window.opener.document.getElementById(idList).value = window.opener.document.getElementById(selectId)[contatoreTemp].value + ";";

				window.opener.document.getElementById(nameList).value = window.opener.document.getElementById(selectId)[contatoreTemp].text + ";";

				firstline = false;

				}

			else {

				window.opener.document.getElementById(idList).value = window.opener.document.getElementById(idList).value + window.opener.document.getElementById(selectId)[contatoreTemp].value + ";";

				window.opener.document.getElementById(nameList).value = window.opener.document.getElementById(nameList).value + window.opener.document.getElementById(selectId)[contatoreTemp].text + ";";

			}

		}

	}

	else {

		window.opener.document.getElementById(idList).value = "";

		window.opener.document.getElementById(nameList).value = ""; 

	}
	
	//alert(window.opener.document.getElementById(idList).value);
	//alert(window.opener.document.getElementById(nameList).value);
	//alert(window.opener.document.getElementById(idList).value);

	

}
	
function fillcheckboxdocentetitolare(docenteTitolareInputHiddenId){
	
	
	var docenteTitolareInputHidden = window.opener.document.getElementById(docenteTitolareInputHiddenId); //valore in hidden
	if (docenteTitolareInputHidden.value != ''){
		var formBaseId = docenteTitolareInputHidden.value;
		var checkBoxElements = document.getElementsByName('select');
		
		for (var j = 0; j < checkBoxElements.length; j++){
			if (checkBoxElements[j].id == formBaseId){
				checkBoxElements[j].checked = true;
			}
			
		}
	}
}

function insertdocentetitolare(id, nome,cognome, checkboxObj){
	
	if (checkboxObj.checked == true){
		docente=nome+" "+cognome;
		var iddocente = window.opener.document.getElementById("docenteTitolare"); //valore in hidden
		var doc = window.opener.document.getElementById("docenteTitolareName"); /////valore visualizzato nell'imput text
		
		iddocente.value=id;
		doc.value=docente;
		window.close();
	}
	else {
		var iddocente = window.opener.document.getElementById("docenteTitolare"); //valore in hidden
		var doc = window.opener.document.getElementById("docenteTitolareName"); /////valore visualizzato nell'imput text
		
		iddocente.value='';
		doc.value='';
	}
	
}

function checkboxforpianodistudi(checkboxObj, selectOpenerId, attivitaSelectedName, errorMessage) {
	
	var listIdValue = document.getElementById("listAttivitaId").value;
	var listNameValue = document.getElementById("listAttivitaName").value;
	
	var percorso = selectOpenerId.substring(selectOpenerId.indexOf('percorso'));
	
	var formBaseId = checkboxObj.id;
	var attivitaModuloChecked = formBaseId.split(":");
	var attivitaChecked = attivitaModuloChecked[0];
	var moduloChecked = attivitaModuloChecked[1];
	

	if (checkboxObj.checked == true){
		
		
		var update = true;
		
		
		
		var selectObjects = window.opener.document.getElementsByTagName("select");
		for (var i = 0; i < selectObjects.length; i++){
			
			var selectObj = selectObjects[i];
			
			var thisSelectId = selectObj.id;
			var thisPercorso = thisSelectId.substring(selectOpenerId.indexOf('percorso'));
			// se sono riferiti a percorsi diversi, esco
			// lo stesso modulo puo stare su piu percorsi
			if (thisPercorso != percorso) continue;
			
			var options = selectObj.options;
			// se sto analizzando lo stesso select, esco perche è un controllo
			// che non ha senso
			if (thisSelectId == selectOpenerId) continue;
			
			for (var j = 0; j < options.length; j++){
				
				var value = options[j].value;
				var valueSplitted = value.split(":");
				var idAttivita = valueSplitted[0];
				var idModulo = valueSplitted[1];
				
				if (idAttivita == attivitaChecked){
					// se l'attivita non ha moduli associati
					if (idModulo == 0){
						//alert(errorMessage);
						update = false;
						checkboxObj.checked = false;
						
					}
					
					else if (idModulo == moduloChecked){
						//alert(errorMessage);
						update = false;
						checkboxObj.checked = false;
						
					}
				}
			}
		}
		
		if (update == true){
			document.getElementById("listAttivitaId").value += formBaseId+';';
			document.getElementById("listAttivitaName").value += attivitaSelectedName+';';
			//alert(document.getElementById("listAttivitaId").value);
			//alert(document.getElementById("listAttivitaName").value);
		}
	
	}
	else {
		
		var index = listIdValue.indexOf(formBaseId);
		//alert(index);
		if (index == 0){
			
			var indexSeparator = listIdValue.indexOf(";");
			listIdValue = listIdValue.substring(indexSeparator + 1);
			document.getElementById("listAttivitaId").value = listIdValue;
			
			indexSeparator = listNameValue.indexOf(";");
			listNameValue = listNameValue.substring(indexSeparator + 1);
			document.getElementById("listAttivitaName").value = listNameValue;
			
			//alert(document.getElementById("listAttivitaName").value);
			//alert(document.getElementById("listId").value);
		}
		else {
			var firstPart = listIdValue.substring(0, index);
			var secondPart = listIdValue.substring(index + 1);
			var indexSeparator = secondPart.indexOf(";");
			secondPart = secondPart.substring(indexSeparator + 1);
			//alert('firstPart = '+firstPart);
			//alert('secondPart = '+secondPart);
			
			
			
			document.getElementById("listAttivitaId").value = firstPart + secondPart;
			//alert(document.getElementById("listAttivitaId").value);
			
			// cerco la posizione nella lista
			var positionToEliminate = 0;
			for (var i = index; i >= 0; i--){
				if (listIdValue[i] == ";"){
					positionToEliminate++;
				}
			}
			
			var listNameValueSplitted = listNameValue.split(";");
			var listNameResult = '';
			for (var i = 0; i < listNameValueSplitted.length; i++){
				
				if (i != positionToEliminate){
					
					if (listNameValueSplitted[i] != ''){
					
						listNameResult += listNameValueSplitted[i] +";";
					}
				}
				
				
			}
			
			//alert(listNameResult);
			document.getElementById("listAttivitaName").value = listNameResult;
			
			//alert(document.getElementById("listId").value);
		}
		
		//alert(document.getElementById("listAttivitaId").value);
		//alert(document.getElementById("listAttivitaName").value);
		
	}
	
}

function onloadpianodistudi(listIdValue, listNameValue){
	
	//alert(listIdValue);
	
	document.getElementById("listAttivitaId").value = listIdValue;
	document.getElementById("listAttivitaName").value = listNameValue;
	
	//alert(document.getElementById("listAttivitaName").value);
	var checkBoxElements = document.getElementsByName('select');
	
	if (listIdValue != ''){
		var formBaseIdSplitted = listIdValue.split(";");
		
		for (var i=0; i < formBaseIdSplitted.length; i++){
			
			var formBaseId = formBaseIdSplitted[i];
			for (var j = 0; j < checkBoxElements.length; j++){
				if (checkBoxElements[j].id == formBaseId){
					checkBoxElements[j].checked = true;
				}
				
			}
		}
	}
	
}

function appendValue(link, inputId, inputName, selectOpenerId, anno, semestre, percorso, idEdizione, idPianoDiStudi){
	
	if (link.indexOf('?') == -1){
		link+='?'+inputId+'='+document.getElementById(inputId).value;
		
	}
	else {
		//alert(link);
		//alert(document.getElementById(inputId));
		link+='&'+inputId+'='+document.getElementById(inputId).value;
		
		//alert(link);
	}
	link+='&'+inputName+'='+document.getElementById(inputName).value;
	link+='&selectOpenerId='+selectOpenerId;
	link+='&anno='+anno;
	link+='&semestre='+semestre;
	link+='&percorso='+percorso;
	link+='&idEdizione='+idEdizione;
	link+="&idPianoDiStudi="+idPianoDiStudi;
	
	createFakeFormAndSendForCheckBoxList(link);
}

function updateSelectOnUpdatePianoDiStudi(selectOpenerId, listAttivitaId, listAttivitaName){
	
	
	var selectObj = window.opener.document.getElementById(selectOpenerId);
	var options = selectObj.options;
	
	while( selectObj.options.length > 0 ){
		selectObj.remove(0);
	}
	
	var listAttivitaIdSplitted = listAttivitaId.split(";");
	var listAttivitaNameSplitted = listAttivitaName.split(";");
	
	
	for (var i = 0; i < listAttivitaIdSplitted.length; i++){
		
		if (listAttivitaIdSplitted[i] != ''){
			var option = window.opener.document.createElement("option");
			option.text = listAttivitaNameSplitted[i];
			option.value = listAttivitaIdSplitted[i];
			
			try{
				selectObj.add(option, null); // standards compliant
			}
			catch(ex){
				selectObj.add(option); // IE only
			}
		}
	}
	
	
}

function createFakeFormAndSendForCheckBoxList(link){
	var href = link;
	var path = href.substring(0, href.indexOf('?'));
	var args = href.substring(href.indexOf('?') + 1);
	
	/*myform = document.getElementById('form');
	myform.method="post";
	myform.action=path;
	myform.methodName.value='insert';
	myform.servlet.value='formdocente';
	
	myform.submit();*/
	
	
	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="_self";
	var body = document.getElementsByTagName('body');
	body[0].appendChild(myform);
	myform.submit();
	

}
