function addSelect(name){
	// get all select with the same name
	var selects = $$('select[name^=' + name + ']');
	
	// get the parent paragraph node of the last select
	var lastSelectParagraph = selects.last().up('p');
	
	// get the number of available options
	var availableOptions = selects.last().getElementsBySelector('option');
	var maxSelects = (availableOptions.first().value == '') ? availableOptions.size() - 1 : availableOptions.size();
	
	if (maxSelects > selects.length) {
		// clone the paragraph add the class 'multi' and change the id of the select
		var newSelectParagraph = lastSelectParagraph.cloneNode(true);
		newSelectParagraph.down('select').addClassName('multi').id = name + selects.length;
		
		/*
		// append delete image to paragraph
		var deleteImage = new Element('p', { 'onclick': 'this.up("p").remove()'}).update('Löschen');
		new Insertion.Bottom(newSelectParagraph, deleteImage);
		*/
		
		// insert the new paragraph at the bottom of the parent div
		new Insertion.Bottom(lastSelectParagraph.up('fieldset'), newSelectParagraph);
	}
}

function checkAddSelect(element){
	if(element.name.endsWith('[]')){
		name = element.name.substring(0, element.name.length - 2);
	} else {
		name = element.name;
	}
	
	// check if the user has selected a value
	if(element.value != ''){
		// get all select with the same name
		var selects = $$('select[name^=' + name + ']');
		// if the changed select was the last of the group add another select
		if(element.id == selects.last().id){
			addSelect(name);
		}
		// show delete image
		element.next(0).show();
	} else {
		// hide delete image
		element.next(0).hide();		
	}
}

function toggleGroups(toggler, groupname, options){
	var togglerCheckbox = $(toggler);
	var groupElements = $$('input[class*=' + groupname + ']');
	togglerCheckbox.observe('click', function(){
		groupElements.each(function(checkbox){
			checkbox.checked = togglerCheckbox.checked;
		});
		if (options != null) {
			if (options.afterToggle) {
				options.afterToggle(togglerCheckbox.className, togglerCheckbox.name);
			}
		}
	});

 	groupElements.invoke('observe', 'click', function(){
		if(this.id != togglerCheckbox.id){
			togglerCheckbox.checked = false;
		}
	});
/*
	var togglerCheckbox = jQuery('#' + toggler);
	var groupElements = jQuery('.' + groupname);
	togglerCheckbox.click(function(){
		groupElements.attr("checked", togglerCheckbox.attr("checked"));
	});
	groupElements.click(function(){
		if(jQuery(this).attr("id") != jQuery('#' + toggler).attr("id")){
			togglerCheckbox.removeAttr("checked");			
		}
	});
*/
}


function toggleFieldset(event){
	var elm = Event.element(event);
	if(elm.nodeName != 'H3'){
		elm = elm.up('h3');
	}
	var fieldset = elm.next(0);
	fieldset.toggle();
	if(fieldset.style.display == 'none'){
		elm.addClassName('plus');
	} else  {
		elm.removeClassName('plus');
	}
}
	
function ajaxSave(event, formID, tabRel, tabUrl){
	var elm = Event.findElement(event, "input");
	Event.stop(event);
	try {
		window.formRescuer.submitted = true;
	}catch(e){}
	try{
		var formData = $(formID).serialize();
		var link = new Object();
		link.rel = tabRel;		
		link.href = tabUrl;
		link.postBody = formData;
		ajaxtab.newAjaxTab(link);
	}catch(e){debug('ajaxSave: ' + var_dump(e))}
}

function addRow(selector, options){
	var lastRow = $$(selector).last();
	var newRow = lastRow.cloneNode(true);
	if (options.reset) {
		newRow.select('select', 'textarea', 'input').each(function(elm){
			elm.clear().checked = '';
		});
	}
	if(options.beforeInsert) options.beforeInsert(newRow);
	lastRow.insert({'after': newRow});
	if(options.afterInsert) options.afterInsert(newRow);
	
	if (options.focusFirst) {
		newRow.select('input', 'select', 'textarea').first().focus();
	}
}

function updateTabHeaderCounts(fb){
	var checked = 0;
	var fbName = fb.gsub('studiengaenge', '').gsub('group', '');
	var fbChoices = new Array();
		$$('.' + fb).each(function(elm){
			if (elm.checked) {
				if (!elm.id.endsWith('all')) {
					fbChoices[elm.value] = $('studiengang' + elm.value).next('label').innerHTML;
					checked++;
				}
			}
		})
		$(fb).update('(' + checked + ')');
	if($(fbName + 'Choices') != null){
		if (checked > 0) {
			$(fbName + 'Choices').update('<b>' + fbName + '</b>: ' + fbChoices.compact().sort().toString().gsub(',', ', ')).show();
		} else {
			$(fbName + 'Choices').hide();
		}
	}
}
