(function(){Object.extend(Form.Element.Methods,{resetInput:function(element){element=$(element);if($F(element)==$(element).defaultValue&&!$(element).hasClassName('prefilled')){$(element).value='';}return element;}});Element.addMethods();var clearForms=function(){var form_elements=$$('input[type=text]','textarea');form_elements.invoke('observe','focus',function(){this.resetInput();});form_elements.invoke('observe','blur',function(){if($F(this).blank()){this.value=this.defaultValue;}});$$('form').invoke('observe','submit',function(){this.select('input[type=text]','textarea').invoke('resetInput');});};document.observe('dom:loaded',clearForms);})();

// Element.Storage API
// each element contains an ID attribute, which is the key of the Element.Storage hash
// storing separate pseudo-attributes on each element can cause memory leaks in IE,
// but by only holding the element's ID with the element, and then a hash of all the
// element's pseudo-attributes with the ID as a key we can reduce the memory leaks
// and only have one unique point of access for all elements, even if they have been
// removed from the document.
var Storage = {
	create: function (element) {
		if (typeof window.Element.Storage === 'undefined') {
			Element.Storage = new Hash();
		}
 
		element = $(element);
		var storage = Element.Storage,
		id = element.identify();
 
		if (!storage.keys().include(id)) {
			storage.set(id, new Hash());
		}
 
		return element;
	}
};
 
Element.addMethods({
	store: function (element, rules, value) {
		element = Storage.create(element);
		var storage = Element.Storage.get(element.identify()),
			attributes = {};
 
		if (typeof rules === 'object') {
			attributes = rules;
		} else {
			attributes[rules] = value;
		}
 
		for (var key in attributes) {
			if (attributes.hasOwnProperty(key) && typeof attributes[key] !== 'undefined') {
				storage.set(key, attributes[key]);
			}
		}
 
		return element;
	},
 
	retrieve: function (element, property_name, property_default_value) {
		element = Storage.create(element);
		var storage = Element.Storage.get(element.identify());
 
		if (!storage.keys().include(property_name)) {
			element.store(property_name, property_default_value);
		}
 
		return storage.get(property_name);
	},
 
	eliminate: function (element, property_name) {
		element = Storage.create(element);
		var storage = Element.Storage.get(element.identify());
 
		if (storage.keys().include(property_name)) {
			storage.unset(property_name);
		}
 
		return element;
	}
});

Element.addMethods({
	reorder_list: function (element, column_count) {
		// this function fixes the display order of floated lists
		element = $(element);
		element.select('ul').invoke('remove');
		var old_list = $A(element.select('li')),
			new_list = $A(old_list);

		var limit = old_list.size();
		for (var i = 0, j = 0, k = 1; i < limit; i++) {
			if (j >= limit) {
				j = k;
				k++;
			}

			new_list[j] = $(old_list[i]).cloneNode(true);
			j = j + column_count;
		}

		element.update('');

		for (var i = 0; i < limit; i++) {
			element.insert({
				bottom: new_list[i]
			});
		}
		
		return element;
	},
	
	alphabetise: function (element) {
		element = $(element);
		
		if (element.retrieve('sorted_order')) {
			// if we have already sorted this column, use a shortcut
			element.innerHTML = element.retrieve('sorted_order');
		} else {
			// otherwise, store the original order
			element.store('original_order', element.innerHTML);
			
			// select all of the list items
			var list_items = element.select('li');
			var list = new Hash();
			
			// work out the surname for each person
			list_items.each(function (li) {
				// find the surname without Prof or Sir to confuse
				var surname = $w(li.innerHTML.stripTags()).without('Prof').without('Sir')[1] + '__' + li.innerHTML.stripTags();
				
				// return the element with surname
				list.set(surname, li.cloneNode(true));
			});
			
			// empty the list
			element.update('');
			
			// insert the ordered list items
			list.keys().sort().each(function (surname) {
				element.insert(list.get(surname));
			});
			
			// save the new order to speed up next time
			element.store('sorted_order', element.innerHTML);
		}
		
		return element;
	}
});
document.observe('dom:loaded', function () {
	// $$('.four_columns ul').invoke('reorder_list', 4);
	$$('form').each(function (form) {
		var required_fields = form.select('input[id$=*]', 'textarea[id$=*]', 'select[id$=*]');
		if (required_fields[0]) {
			form.observe('submit', function (event) {
				required_fields.invoke('setStyle', { borderColor: '#ccc' });
				var incomplete_fields = required_fields.select(function (f) { return f.getValue().blank(); });
				if (incomplete_fields.length > 0) {
					event.stop();
					incomplete_fields.invoke('setStyle', { borderColor: '#f00' });
					alert('Please fill in all mandatory fields');
				}
			});
		}
	});
});