function retornarMatrizSel(obj_cmb) {
	var matriz = new Array();
		
	for(i=0; i< obj_cmb.options.length; i++)
		if(obj_cmb.options[i].selected) {
			obj_cmb.options[i].indice = i;
			matriz.push( obj_cmb.options[i] );			
		}
	
	return matriz;
}


function cls_ListMenu( idObjOrigem, idObjDestino, idBtnAdd, idBtnDel, prefixo ) {
	var self = this;
		
	var objOrigem = document.getElementById(idObjOrigem);
	var objDestino = document.getElementById(idObjDestino);
	var BtnAdd = document.getElementById(idBtnAdd);
	var BtnDel = document.getElementById(idBtnDel);
	
	this.maximo = 3;
	
	var form = objOrigem.form;	

	var div = document.createElement( "div");
	div.style.display = "none";
	form.appendChild( div );	

	function criarHidden(){
		var ops = objDestino.options;
		div.innerHTML = "";
		for( var i=0; i < ops.length; i++ ){
			var hid = document.createElement("input");
			hid.name = prefixo + '[]';
			hid.type = "hidden";
			hid.value = ops[i].value;
			div.appendChild( hid );
		}
	}
	
	
	function troca( p_objOrigem, p_objDestino, p_Maximo ) {
		if(typeof(p_Maximo) == "undefined")
			p_Maximo = 9000;
		
		var selecionados = retornarMatrizSel(p_objOrigem);		
		for(i=0; i < selecionados.length && p_objDestino.options.length < p_Maximo; i++) {
			p_objOrigem.remove( selecionados[i].indice );
			p_objDestino.options[p_objDestino.options.length] = new Option(selecionados[i].text, selecionados[i].value);						
		}
		criarHidden();
	}
	
	BtnAdd.onclick = function() {		
		if(objDestino.options.length < self.maximo)
			troca( objOrigem, objDestino, self.maximo);			
		if(objDestino.options.length >= self.maximo)
			BtnAdd.setAttribute("disabled", "disabled");
		
		BtnDel.removeAttribute("disabled");
	}
	BtnDel.onclick = function() {
		troca( objDestino, objOrigem );
		BtnAdd.removeAttribute("disabled");
		if(objDestino.options.length == 0)
			BtnDel.setAttribute("disabled", "disabled");
	}
} 