var sortitems=0;  // Ordena itens nas listas? (1 ou 0)

function removeItem(fbox){
	//verificando se algum item foi selecionado
	var j=0;
	for(var i=0;i<fbox.options.length;i++) {
		if(fbox.options[i].selected && fbox.options[i].value!="") {				
			j++;
		}
	}
	
	//se tiver selecionado algun
	if(j>0){
		if(confirm('Deseja realmente remover esse item?')){
			var Moveu=false;
			for(var i=0;i<fbox.options.length;i++) {
				if(fbox.options[i].selected && fbox.options[i].value!="") {				
					fbox.options[i].value="";
					fbox.options[i].text="";
					Moveu=true;
				}
			}
			if(Moveu) {
				BumpUp(fbox);
			}
			return true;
		}else{
			return false;
		}
	}else{
		alert("Nenhum item foi selecionado.");
		return false;
	}
}

function moveTextSelect(fbox,tbox){
	//move o conteÃºdo de um txt para select		
	if(fbox.value!="") {
		var no=new Option();
		no.value=fbox.value;
		no.text=fbox.value;
		tbox.options[tbox.options.length]=no;
		fbox.value="";
		if (sortitems) SortD(tbox);
	}
	else {
		alert("Item nao preenchido.")
	}
}	

function moveSS(fbox,tbox) {
//Move conteÃºdo entre selects
  var Moveu=false;
  for(var i=0;i<fbox.options.length;i++) {
	if(fbox.options[i].selected && fbox.options[i].value!="") {
	  var no=new Option();
	  no.value=fbox.options[i].value;
	  no.text=fbox.options[i].text;
	  tbox.options[tbox.options.length]=no;
	  fbox.options[i].value="";
	  fbox.options[i].text="";
	  Moveu=true;
	}
  }
  if(Moveu) {
	BumpUp(fbox);
	if (sortitems) SortD(tbox);
	}
  else {
	alert("Nenhum item foi selecionado.")
  }
}

function BumpUp(box) {
  for(var i=0;i<box.options.length;i++) {
	if(box.options[i].value=="") {
	  for(var j=i;j<box.options.length-1;j++) {
		box.options[j].value=box.options[j+1].value;
		box.options[j].text=box.options[j+1].text;
	  }
	  var ln=i;
	  break;
	} 
  }
  if(ln<box.options.length) {
	box.options.length -= 1;
	BumpUp(box);
  }
}

function SortD(box)  {
  var temp_opts=new Array();
  var temp=new Object();
  for(var i=0;i<box.options.length;i++) {
	temp_opts[i]=box.options[i];
  }
  for(var x=0;x<temp_opts.length-1;x++) {
	for(var y=(x+1);y<temp_opts.length;y++) {
	  if(temp_opts[x].text>temp_opts[y].text) {
		temp=temp_opts[x].text;
		temp_opts[x].text=temp_opts[y].text;
		temp_opts[y].text=temp;
		temp=temp_opts[x].value;
		temp_opts[x].value=temp_opts[y].value;
		temp_opts[y].value=temp;
	  }
	}
  }
  for(var i=0;i<box.options.length;i++) {
	box.options[i].value=temp_opts[i].value;
	box.options[i].text=temp_opts[i].text;
  }
}

function checkSelect(id_catemae,catemae,idSelect,statusCheck){
	var Moveu=false;		
	var selectMultiple = document.getElementById(idSelect);
	
	//se estiver marcado adiciona no select
	if(statusCheck==true){
		var no=new Option();
		no.value = id_catemae;
		no.text  = catemae;
		selectMultiple.options[selectMultiple.options.length]=no;			
		if (sortitems) SortD(selectMultiple);
	}
	//remove do select
	else{
		for(var i=0;i<selectMultiple.options.length;i++) {
			if(selectMultiple.options[i].value==id_catemae) {				
				selectMultiple.options[i].value="";
				selectMultiple.options[i].text="";
				BumpUp(selectMultiple);
			}
		}
	}
}

function SelectAll(fbox) {
	//Seleciona todos os itens do select fbox
	for(var i=0;i<fbox.options.length;i++) {
		fbox.options[i].selected=true;
	}
}

function limpaSelect(idSelect){
	idSelect = document.getElementById(idSelect);
	//percorrendo todas as posiÃ§Ãµes do select
	for(var i=0;i<idSelect.options.length;i++) {
		//removendo o item
		idSelect.options[i].value="";
		idSelect.options[i].text="";
	}
	
	BumpUp(idSelect);
}
