/*
requires jquery
*/
var ASEForm = $.extend({},{
	_in_check_all: false,

	checkAll: function(check_all_el) {
		var do_check = (check_all_el.checked ? true : false);
		ASEForm._in_check_all = true;
		for (var i=1; i < arguments.length; i++) {
			$("input[@id="+arguments[i]+"]").each(function() {
				var el = $(this);
				if (do_check) {
					if (!this.checked) {
						this.click();
						el.bind('click', function() {
							if (ASEForm._in_check_all) return;
							if (check_all_el.checked) check_all_el.checked = false;
						});
					}
				} else {
					// unchecked
					if (this.checked) {
						this.click();
					}
				}
			});
		}
		ASEForm._in_check_all = false;
	},

	checkSelectAll: function(select_all_el, skip_other) {
		// find out if we need to select all
		var do_select_all = false;
		$.each(select_all_el.options, function() {
			if (this.value == '_selectall' && this.selected) {
				do_select_all = true;
			}
		});

		// do select all
		if (do_select_all) {
			$.each(select_all_el.options, function() {
				if (this.value == '_selectall') {
					this.selected = false;
				} else if (skip_other && this.value == 'Other') {
					this.selected = false;
				} else {
					this.selected = true;
				}
			});
			
			// re-fire the onchange event
			if (typeof select_all_el.onchange != 'undefined') select_all_el.onchange();
		}
	},
	
	uncheckCheckAll: function(check_all_id) {
		// alert('uncheck '+check_all_id+'')
	},
	
	selectOtherMulti: function(el, prompt_text, other_label) {
		// find out of "Other" was selected
		var options = $(el).val();
		var other_selected = false;
		var select_all_exists = false;
		
		for (var i=0; i < el.options.length; i++) {
			var option = el.options[i];
			if (option.value == 'Other' && option.selected) {
				other_selected = true;
				option.selected = false;
			}
			if (option.value == '_selectall') {
				select_all_exists = true;
			}
		};
		
		if (other_selected) {
			// add the new option
			//  other has to be at the end
			var new_option = "";
			while (new_option == ""){
				new_option = prompt(prompt_text, "");
			}
			if (new_option != null) {
				if (select_all_exists) {
					var old_select_all_label = el.options[(el.options.length-2)].text;
					el.options[(el.options.length-2)]=new Option(new_option,new_option,true,true);
					el.options[(el.options.length-1)]=new Option(old_select_all_label,'_selectall');
					el.options[el.options.length]=new Option(other_label,'Other');
				} else {
					el.options[(el.options.length-1)]=new Option(new_option,new_option,true,true);
					el.options[el.options.length]=new Option(other_label,'Other');
				}
			}
		}
	},

	
	_end: {}
});