function findValue(li) {
	if( li == null ) return alert("Nenhum Resultado!");

	if( !!li.extra ){
		var sValue = li.extra[0];
		var cValue = $("#funcoes").val();
	}
	
	$("#funcSel").append('<li class="funcoesEscolhidas" id="'+sValue+'">'+cValue+' <img class="excluirFunc" onclick="removeFunc($(this))" src="img/del.png" style="vertical-align:middle;cursor:pointer"  /></li>');
	$("#funcoes").val("");
	funcNova = $("#funcoesReal").val();
	
	if (funcNova == ""){
		funcNova = sValue;
	}else{
		funcNova = funcNova + "," + sValue;
	}
	
	$("#funcoesReal").val(funcNova);
	$("#funcoes").focus();
	$("#titFunc").fadeIn("slow");
}

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	return row[0];
}


function removeFunc(obj){
    funcExistente = $("#funcoesReal").val()
    idToRemove = obj.parent().attr("id")+"";
    funcNovas = funcExistente.replace(idToRemove,"");
    funcNovas = funcNovas.replace(",,",",");
    $("#funcoesReal").val(funcNovas);
	obj.parent().remove();
}
	
$(document).ready(function(){
   $("#funcoes").autocomplete(
		   'curriculo/getfuncoesjson', 
		   {
			minChars:3,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			cellSeparator:'|', 
			lineSeparator: ','
		   }
   );
});
