function enhance_course_selection() {
	var ajax_form_submit = function(element, value) {
		new Ajax.Request(element.action, {
			asynchronous:true, evalScripts:true, parameters:value
		})
	}

	// Send form vie Ajax when selecting a state:
	new Form.EventObserver('state', ajax_form_submit);

	// Send form vie Ajax when selecting a school:
	if($('school')) {
		new Form.EventObserver('school', ajax_form_submit);
	}

	// Send form vie Ajax when selecting a section:
	if($('section')) {
		new Form.EventObserver('section', ajax_form_submit);
	}

	$$('input[value="Go"]').each(function(element) { element.hide(); });
}



function enhance_missing_course() {
	// Hide all the radio buttons
	$$('div#textbooks input[type=radio]').each(function(elem) { elem.hide(); });

	// Hide the submit button
	$$('#missing_course_form input[type=submit]').each(function(elem) { elem.hide(); });

  // Hide and prepare the name spans
//  $$('div#textbooks label span.description').each(function(elem) { elem.addClassName('floating').hide().setOpacity(0.9); });
  $$('div#textbooks label span.description').each(function(elem) { elem.addClassName('floating').hide().setOpacity(0.0); });

	// Add 'selected' class to element and remove it from peers
	function selectBookImg(element) {
	  $$('div#textbooks label img.selected').each(function(elem){
	    Element.removeClassName(elem, 'selected');
	  });
	  Element.addClassName(element, 'selected');
	}

	// Get element from event and select the book image
	function bookImgClicked(event) {
	  selectBookImg(Event.element(event));
	  window.setTimeout("$('missing_course_form').submit();", 0);
	}

	function bookImgMoused(event) {
    var image = event.element();
	  var description = event.findElement('label').select('span.description').first();
	  if (description == null) { return; }

	  if (event.type == 'mousemove') {
	    description.setStyle({top: event.pointerY() + 15 + 'px', left: event.pointerX() + 10 + 'px'});
	  } else if (event.type == 'mouseout') {
//	    description.hide();
      new Effect.Opacity(description, {duration:0.1, from:0.95, to:0.0, fps:50, afterFinish:function(obj){ obj.element.hide(); }});
//      description.hide().setOpacity(0.0);
	  } else {
	    description.show();
//      description.show().setOpacity(0.9);
      new Effect.Opacity(description, {delay: 0.05, duration:0.1, from:0.0, to:0.95, fps:50});
	  }
	}

	// Observe clicks on the label images of textbooks
	$$('div#textbooks label img').each(function(elem){
	  elem.observe('click', bookImgClicked);
	  elem.observe('mouseover', bookImgMoused);
	  elem.observe('mouseout', bookImgMoused);
	  elem.observe('mousemove', bookImgMoused);
	});

	// Select the label image of pre-selected radio buttons
	$$('div#textbooks input[type=radio][checked=checked] + label img').each(function(elem){
	  selectBookImg(elem);
	});
}


function enhance_passkey_entry(id) {
	if ($(id) == null) id = 'passkey';
  Event.observe($(id), 'keyup', function(event) {

    /*
  	var key = window.event ? event.keyCode : event.which;
  	var keychar = String.fromCharCode(key);
  	var alnum = /[a-zA-Z0-9]/
  	var printable = /./
  	if (alnum.test(keychar)) {
  	  // Alphanumeric
  	  $('passkey').value += keychar.toUpperCase();
  	  Event.stop(event);
  	  return;
	  } else if (printable.test(keychar)) {
  	  // Printable character
  	  Event.stop(event);
  	  return;
  	}
  	*/

    $(id).value = $F(id).toUpperCase().replace(/\W+/,'');
  });
}

function togglePassword() {
	if ($('password_plain').visible()) {
		$('hide_password').enable();
		$('password_plain').hide().disable();
		$('password_hidden').show().enable().value = $F('password_plain');
		$('toggle_pw').update('Show my password in plain text again.');
	} else {
		$('hide_password').disable();
		$('password_hidden').hide().disable();
		$('password_plain').show().enable().value = $F('password_hidden');
		$('toggle_pw').update('Hide my password again.');
	}
}

var Sm = Sm || {};

Sm.Tips = {
	eventPairs: { 'focus':     { show:'focus',     hide:'blur' }, 
	              'mouseover': { show:'mouseover', hide:'mouseout' } },

	disable: [],
	elements: [],

	disableWhen: function(selector) {
		Sm.Tips.disable.push(selector);
	},

	map: function(eventType, field_or_fields, help) {
		if (!field_or_fields instanceof Array) field_or_fields = [ field_or_fields ];
		field_or_fields = $A(field_or_fields);
		help = $(help);
		if (!Sm.Tips.shouldDisable(help.up())) {
			field_or_fields.each(function(field) {
				$(field).
					observe(Sm.Tips.eventPairs[eventType].show, function(e) { help.show(); }).
					observe(Sm.Tips.eventPairs[eventType].hide, function(e) { help.hide(); });
				Sm.Tips.elements.push($(field));
			});
		}
	},

	autoMap: function() {
		arguments = $A(arguments);
		arguments.each(function(field) {
			field = $(field);
			// Either we find it by id or we navigate to find if
			var tip = $('tip_for_' + field.id);
			if (!tip) {
				// Set a local variable to be the help div
				var startPoint = field;
				var enclosingDiv = startPoint.up('.fieldWithErrors');
				if (enclosingDiv != null) startPoint = enclosingDiv;
				tip = startPoint.previous();
			}
			if (tip && !Sm.Tips.shouldDisable(tip)) {
				var help = tip.select('div.help');
				if(help) help = help.first();
				// attach handlers to 'focus' and 'blur' events that show or hide the help if it is present
				if (help) {
					field.
						observe('focus', function(e) { help.show(); }).
						observe('blur', function(e) { help.hide(); });
					Sm.Tips.elements.push(field);
				}
			}
		});
	},

	shouldDisable: function(startPoint) {
		var disabled = false;
		Sm.Tips.disable.each(function(disableSelector) {
			if(startPoint.select(disableSelector).length > 0) disabled = true;
		});
		return disabled;
	},

	/*
	 * Will disable *all* event handlers on the input fields Sm.Tips has been targetting. Use with caution.
	 */
	disableAll: function() {
		Sm.Tips.elements.each(function(element) {
			$(element).stopObserving();
		});
	}
}