$(function() {
	function split( val ) {
		return val.split( /,\s*/ );
	}
	function extractLast( term ) {
		return split( term ).pop();
	}

	$("#sports").autocomplete({

		minLength: 0,
		source: function(req, add){
			$.ajax({
				url: '/ajax/sports',
				dataType: 'json',
				type: 'POST',
				data: req,
				success: function(data){
					if(data.response =='true'){
					   add(data.message);
					}
				}
			});
		},
		focus: function() {
			// prevent value inserted on focus
			return false;
		},
		select: function( event, ui ) {
			var terms = split( this.value );
			// remove the current input
			terms.pop();
			// add the selected item
			terms.push( ui.item.value );
			// add placeholder to get the comma-and-space at the end
			terms.push( "" );
			this.value = terms.join( ", " );
			return false;
		}
	});

});
