//Have to use this here because of the funky nature of the URLs here being processed by a single PHP file (processor.php) for each instance.
var refUrl = location.href.substring(0,location.href.lastIndexOf('/')+1);

$(function()
{	
	$("#memberGroup").autocomplete(
	{
		minLength: 2,	
		open: function()
		{
			$(this).data("sItem", "");
		},		
		close: function(event, ui)
		{
			if (!$(this).data("sItem")) $(this).val("");
		},
		select: function(event, ui)
		{				
			$(this).data("sItem", ui.item);
			getDepartments(ui.item.groupID);
		},
		source: function(request, response)
		{	
			$.ajax(
			{
				url: refUrl+"signup.ajax?action=getMemberGroups",
				dataType: "json",
				data: 
				{ 
					search: request.term 
				},
				success: function(data)
				{					
					response(data);
				},
				error: function() { alert('error getting auto complete data!'); }
			});
		}
	}).change(function() 
	{
		$(this).val().length < 2 ? $("#submit").disable() : $("#submit").enable(); 
	}).blur(function()
	{
		if (!$(this).data("sItem")) $(this).val("");
	});
});
function getDepartments(groupID)
{
	$.ajax(
	{
		url: refUrl+"signup.ajax?action=getDepartments",
		data: { groupID: groupID },
		dataType: "json", 
		success: function(output)
		{
			if (output.error)
			{
				$(output.error).appendTo("body");
				return;
			}
			
			$("select.departments").html(output.content);
		}
	});
}
