$(document).ready(function(){
	// rozwijanie podkategorii
	$('li.up label').click( function(){
		// pokazywanie
		if($(this).children('input').attr('checked')){
			$(this).next('a').css('display', 'inline');
			$(this).addClass('active');
			$(this).parent().children('ul').show('fast');
		}
		// chowanie
		else{
			$(this).next('a').css('display', 'none');
			$(this).parent().children('ul').hide('fast');
			$(this).parent().find('input').removeAttr('checked');
			$(this).parent().find('label').removeClass('active');
		}
	});

	// automatyczne rozwijanie kategorii, w których coś jest zaznaczone
	$('li.up').each( function(){
		if($(this).find('input[checked]').length > 0){
			$(this).children('label').children('input').attr('checked', 'checked');
			$(this).children('label').addClass('active');
			$(this).find('ul').css('display', 'block');
		}
	});

	// kolorowanie zaznaczonych kategorii - dodawanie firmy
	$('ul.subkat label').click( function(){
		if($(this).children('input').attr('checked')){
			$(this).addClass('active');
		}
		else{
			$(this).removeClass('active');
		}
	});

	// zaznacz wszystkie - dodawanie
	$('a.selectAll').click( function(){
		$(this).next('ul').children('li').each( function(){
			$(this).children('label').children('input').attr('checked', 'checked');
			$(this).children('label').addClass('active');
		});
	});

	// masked input
	$('#kod').mask("99-999");
	// $('#tel, #fax').mask("(0-99) 999-99-99");
	$('#nip').mask("999-999-99-99");

	// rozwijanie mapy
	$('#showOnMap').click( function(){
		$(this).hide("fast");
		$('#gMaps').show("fast", function(){
			eval( $('#initMap').text() );
		});
	});

	// zaznaczanie wybranych usług
	$('#wydanie_int label, #wydanie_gaz label').click( function(){
		if($(this).attr('class') == 'main'){
			if(!$(this).children('input').attr('checked')){
				$(this).parent().children('label').children('input').removeAttr('checked');
			}
		}
		else{
			if($(this).children('input').attr('checked')){
				$(this).parent().children('label.main').children('input').attr('checked', 'checked');
			}
		}
	});
	
	// autocomplete
	$('#add_fName').autocomplete('./autocomplete.php', {
										formatItem : formatItem,
										delay : 200,
										width: 256,
										selectFirst: false,
										scroll: true,
										scrollHeight: 100,
										autoFill: true
									});
});

function checkLength(len, elem, id){
	i = elem.value.length;
	if(i > len){
		elem.value = elem.value.substring(0, len);
		i = len;
	}
	$('#' + id).text(len - i);
}

function checkForm(id, kom){
	var ok = true;
	var i = $('#' + id + ' input.required');
	
	i.each( function(){
		if($(this).val().length < 2){
			$(this).addClass('invalid');
			ok = false;
		}
		else{
			$(this).removeClass('invalid');
		}
	});

	if(!ok){
		var loc = document.location.toString().replace(/#.+/, "");
		document.location.href = loc + '#top';
		
		alert(kom);
	}

	return ok;
}

function checkCheckbox(cl, kom){
	var ok = false;
	var i = $('.' + cl + ' input[type=checkbox]');

	i.each( function(){
		if($(this).attr('checked')){
			ok = true;
		}
	});

	if(!ok){
		var loc = document.location.toString().replace(/#.+/, "");
		document.location.href = loc + '#top2';

		alert(kom);
	}
	return ok;
}

function formatItem(row) {
	return row[1];// + " (" + row[1] + ")";
}


