function findValue(li) {
	if( li == null ) {
		$("#company-not-found").remove();
		return $("#business").slideDown('slow').prepend("<div id='company-not-found'><p>Uzupełnij formularz w celu dodania nowej firmy</p></div>");
	};

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	//alert("The value you selected was: " + sValue);
	$("#companyId").val(sValue);
}

function selectItem(li) {
	$("#findCompany").attr("disabled", "disabled");
	$("#company-found").remove();
	$("#findCompany").parent().append("<p id='company-found'>Wybrano firmę: <strong>" + $("#company").val() + "</strong> - <em>" + li.extra[1] + "</em>");
	$("#business").hide();

	findValue(li);
}

function formatItem(row) {
	return row[0] + "<br /><em>miasto: " + row[2] + "</em>";
}

function lookupAjax(){
	var oSuggest = $("#company")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

function findOrga(li) {
	if( li == null ) {
		return alert('Niestety podanej firmy nie ma w naszej bazie danych');
	};

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	//alert("The value you selected was: " + sValue);
	$("#orga_id").val(sValue);
}

function selectOrga(li) {
	// find input from which function is called
	var input = $("." + this.inputClass + "[value='" + li.selectValue + "']");

	input.parent().children("p.orga").remove();
	input.parent().append("<p class='orga'>Wybrano firmę: <strong>" + li.selectValue + "</strong> - <em>" + li.extra[1] + "</em>");

	findOrga(li);
}

function selectPartner(li) {
	var input = $("." + this.inputClass + "[value='" + li.selectValue + "']");
	var hidden = input.next();
	input.parent().append("<p class='orga'>Wybrano firmę: <strong>" + li.selectValue + "</strong> - <em>" + li.extra[1] + "</em>");
	input.parent().append("<input type='hidden' name='" + hidden.attr('name') + "' value='" + li.extra[0] + "' />")
	hidden.remove();
}

function addAutocomplete(addPartner) {
	var parent = $(addPartner).parent();
	parent.children('.partner').children('div').children('input:text').each(function(){
		$(this).autocomplete('/user/searchCompany', {
			delay: 0,
			onItemSelect: selectPartner,
			onFindValue: findOrga,
			formatItem: formatItem
		});
	});
}

$(document).ready(function(){
	$('#companyFind').prepend("<div class='form-title'><h1>firma</h1></div>");
	$("#business").hide();
	$("#findCompany").bind('click', function() {
		lookupAjax();
	});
	
	var type = $('#type').val();
	if (type=="firma")
		var t = 'Firma';
	else
		var t = 'Lokacja';

	$("#register #company").autocomplete("/user/searchCompany/type/" + t + "/", {
		delay: 0,
		minChars: 3,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		matchContains: true
	}).bind('focus', function(){
		$("#findCompany").removeAttr('disabled');
		$("#company-found").remove();
		$("#companyId").val("");
	});

	$("#forganizer").autocomplete("/company/autocomplete", {
		delay: 0,
		onItemSelect: selectOrga,
		onFindValue: findOrga,
		formatItem: formatItem
	});

	$("#floc").autocomplete('/localization/autocomplete', {
		delay: 0,
		onItemSelect: selectOrga,
		onFindValue: findOrga,
		formatItem: formatItem
	});

	$("#addPartner ").bind('click', function(){
		addAutocomplete($(this));
	});
});

