var search_timeout = null;

function getKey(key){
if ( key == null ) {
keycode = event.keyCode;
// To Mozilla
} else {
keycode = key.keyCode;
}
// Return the key in lower case form
return keycode;
}

$(document).ready(function()
{
 $('#community_area').change(function(){

 $('#community_id').val(0);
 $('#community_autocomplete').val('');
 $('#community_ok').val(0);
 $('#submit_button').hide();     
 });
 $('#community_autocomplete').keyup(function()
 {
   var id = $('#area_id').val();
   var community = $(this).val();

  if(search_timeout != null) {
            clearTimeout(search_timeout);
        }
    search_timeout = setTimeout(function()
        {
           var dataString = 'id=' + id + '&community=' + community;

           search_timeout = null;
           $.ajax({
		type:"POST",
		url: '../ajax/community',
                data: dataString,
		cache: false,
		success: function(html){
		$('#community_ok').val(0);
                $('#submit_button').hide();
                $('#sugestions').html(html).slideDown(200);
                },
		error : function(){
			$('#sugestions').html('Wyst\u0105pił błąd');
		  }
		});

        },700);
 });
 $('.secs').change(function()
	{
		var s_id = $(this).attr('id');
		var id = $('#'+ s_id).val();
		
		$.ajax({
		type:"GET",
		url: url+'ajax/categories/' + id,
		cache: false,
		success: function(html){
		$("#categories" + s_id.substr(-1)).html(html);
		 },
		error : function(){
			$("#categories"+ s_id.substr(-1)).html('<option>Wyst\u0105pił błąd</option>');
		  }
		});
	});
});
