/* form.js */

var FormObj = {
	
	run: function(){
		
		// form validation
		$$('form.form').each(FormObj.attachValidate);
		
		// insert titles as default values
		$$('form.form input').each(FormObj.attachInputHints);
		$$('form.form textarea').each(FormObj.attachInputHints);
		
		// enable calendars
		$$('form.form .date input').each(FormObj.enableCalendar);
	},
	
	attachValidate: function(item) {
		Event.observe(item, 'submit', FormObj.validateSubmit.bindAsEventListener(item));
	},

	attachInputHints: function(item) {
		if (item.title && !item.value) {
			item.value = item.title;
			item.addClassName('default');
			Event.observe(item, 'click', FormObj.clearHint.bindAsEventListener(item));
			Event.observe(item, 'blur', FormObj.resetHint.bindAsEventListener(item));
			Event.observe(item, 'submit', FormObj.removeHint.bindAsEventListener(item));
		}		
	},	
	
	enableCalendar: function(item) {
		var calendar_img = $('calendar_' + item.id);
		Calendar.setup({
			inputField  :	item.id,
			ifFormat    :	item.getAttribute("rel"),
			button      :	'calendar_' + item.id,
			showsTime   :	false,
			singleClick :	true
		});
		calendar_img.style.display = 'inline';
	},

	validateSubmit: function(e) {

		// remove hints
		$$('form.form input').each(FormObj.removeHint);
		$$('form.form textarea').each(FormObj.removeHint);
		
		// todo run validation		
	},
	
	clearHint: function(e) {
		if (this.value == this.title) {
			this.value = '';
			this.removeClassName('default');
		}
	},
	
	resetHint: function(e) {
		if (!this.value) {
			this.value = this.title;
			this.addClassName('default');
		}
	},
	
	removeHint: function(item) {
		if (item.title && (item.value == item.title)) {
			item.value = '';
		}
	}
}

new StartUp(FormObj);