var addanother_counts = {};

function generic_add_another(id, max) {
	var safe_id = id
		.replace(/\[/g, '\\[')
		.replace(/\]/g, '\\]');
		 
	displayed = (!addanother_counts[id]) ? 1 : addanother_counts[id];
	
	if (displayed < max) {
		for (i=0; i<max; i++) {
			var div = jQuery('#addanother_'+safe_id+'_'+i+':hidden');
			if (div.length) {
				div.show();
				div.children('span').children('input.autocomplete').focus();
				displayed++;
				if (displayed == max) {
					jQuery('div.addanother_'+safe_id+'_link').hide();
				}	
				break;
			}	
		}
	}
	
	addanother_counts[id] = displayed;
}

function generic_remove(id, i) {
	var safe_id = id
		.replace(/\[/g, '\\[')
		.replace(/\]/g, '\\]');
		
	displayed = (!addanother_counts[id]) ? 1 : addanother_counts[id];
	displayed--;
	jQuery('#addanother_'+safe_id+'_'+i).resetFormElements().hide();
	jQuery('div.addanother_'+safe_id+'_link').show();
	addanother_counts[id] = displayed;
	
	if (window.on_addanother_remove)
		window.on_addanother_remove(id, i);
	
}