/*
 * Author RIGO MICHELE
 * Funzioni da utilizzare nel caso in qui bisogna passare i dati da una select multipla ad un'altra
 * funzione insert(Go,To) --> sposta i campi selezionati
 * funzione insertAll(Go,To) --> sposta tutti i campi della select 'Go' alla select 'To'
 * parametri : Go --> select di partenza (document.nomeForm.nomeSelect)
 *			   To --> select di arrivo 	(document.nomeForm.nomeSelect)
 */

function insert(Go,To){
	try{
		selected = new Array();
		while (Go.selectedIndex != -1)
	    {
	        if (Go.selectedIndex != -1){
		        selected.push(Go.options[Go.selectedIndex]);
	        }
	        Go.options[Go.selectedIndex] = null;
	    }
		for(var i=0;i<selected.length;i++){
			var opt = new Option(selected[i].text,selected[i].value);
			To.options[To.length] = opt;
		}
	}
	catch(e){
		alert(e);
	}
}

function insertAll(Go,To){
	try{
		selected = new Array();
		for(var i=0;i<Go.length;i++){
			Go.options[i].selected = true;
		}
		while (Go.selectedIndex != -1)
	    {
	        if (Go.selectedIndex != -1){
		        selected.push(Go.options[Go.selectedIndex]);
		        
	        }
	        Go.options[Go.selectedIndex] = null;
	    }
		
		for(var i=0;i<selected.length;i++){
			var opt = new Option(selected[i].text,selected[i].value);
			To.options[To.length] = opt;
		}
	}
	catch(e){
		alert(e);
	}	
}

function setSelectedAllLeft(){
		var select = document.getElementById('leftMultiboxSelected');
		for(var i=0;i<select.length;i++){
			select.options[i].selected = true;
		}		
}

function setSelectedAllRight(){
		var select = document.getElementById('rightMultiboxSelected');
		for(var i=0;i<select.length;i++){
			select.options[i].selected = true;
		}		
}


/*
*	Funzione che fa il submit passando il parametro e il form
*/

function redirectWithSelectForm(action,parameter,select){
	var value =	select.options[select.selectedIndex].value;
	document.location.href = action+'?'+parameter+'='+value;
}


/*Funzione che alliena le combo in base alla combo con larghezza maggiore sulla pagina 
 *	author: Rigo Michele
 *  Data: 12/12/2006	
 *  Da inserire all'onload della pagina
 */

	function lengthComboLoad(){
		var tot = document.getElementsByTagName('select');
		var numberCombo = tot.length;
		var maxWidth = 0;
		if(numberCombo != 0){
			for(var y=0;y<numberCombo;y++){
				var name = tot[y].getAttribute('id');
				if((name=='leftMultiboxSelected') || (name=='rightMultiboxSelected')){
				}
				else{
					var options = tot[y];
					for(var z=0;z<options.length;z++){
						var tmp = options[z].firstChild.nodeValue.length;
							if(tmp > maxWidth){
								maxWidth = tmp;
						}
					}
				}
			}
			if(maxWidth > 7){
				maxWidth = parseInt(maxWidth)*0.70;
				maxWidth = maxWidth+'em';
				for(var i=0;i<numberCombo;i++){
					var name = tot[i].getAttribute('id');
					if((name=='leftMultiboxSelected') || (name=='rightMultiboxSelected')){
					}
					else{
						tot[i].style.setAttribute('width',maxWidth);
					}
				}		
			}
		}
	}

	/** 
	 * Apre e chiude gli elementi tra il DIV <nameDiv>, 
	 * cambiando l'icona <nameDiv>Ico se presente.
	 * 
	 * Di default: [icona <nameDiv>Ico = piu.gif] e [<DIV <nameDiv> ha style="display:none">]
	 */
	function openCloseDiv(nameDiv) {	
		if (document.getElementById(nameDiv).style.display != "none") {
			document.getElementById(nameDiv).style.display = "none";
			if (document.getElementById(nameDiv + "Ico") != null) {
				document.getElementById(nameDiv + "Ico").src = "style/images/piu.gif";
			}
		} else {
			document.getElementById(nameDiv).style.display = "block";
			if (document.getElementById(nameDiv + "Ico") != null) {
				document.getElementById(nameDiv + "Ico").src = "style/images/meno.gif";
			}
		}
	}
